Skip to content

Commit d0db6d5

Browse files
Merge pull request #10705 from dotty-staging/rework-reflect-Symbol-fields-API
Rework reflect Symbol fields API
2 parents f8289ee + 74c69a5 commit d0db6d5

File tree

8 files changed

+29
-12
lines changed

8 files changed

+29
-12
lines changed

community-build/src/scala/dotty/communitybuild/FieldsImpl.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@ object FieldsImpl:
1212
def isProjectField(s: Symbol) =
1313
s.isValDef && s.tree.asInstanceOf[ValDef].tpt.tpe <:< retType
1414
val projectsTree = from.asTerm
15-
val symbols = TypeTree.of[V].symbol.fields.filter(isProjectField)
15+
val symbols = TypeTree.of[V].symbol.declaredFields.filter(isProjectField)
1616
val selects = symbols.map(Select(projectsTree, _).asExprOf[T])
1717
'{ println(${Expr(retType.show)}); ${Varargs(selects)} }

compiler/src/scala/quoted/runtime/impl/QuotesImpl.scala

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2335,12 +2335,23 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
23352335
self.is(dotc.core.Flags.Case, butNot = Enum | Module) && !self.isClass
23362336
def isNoSymbol: Boolean = self == Symbol.noSymbol
23372337
def exists: Boolean = self != Symbol.noSymbol
2338-
def fields: List[Symbol] = self.unforcedDecls.filter(isField)
23392338

2340-
def field(name: String): Symbol =
2339+
def declaredField(name: String): Symbol =
23412340
val sym = self.unforcedDecls.find(sym => sym.name == name.toTermName)
23422341
if (isField(sym)) sym else dotc.core.Symbols.NoSymbol
23432342

2343+
def declaredFields: List[Symbol] = self.unforcedDecls.filter(isField)
2344+
2345+
def memberField(name: String): Symbol =
2346+
appliedTypeRef(self).allMembers.iterator.map(_.symbol).find {
2347+
sym => isField(sym) && sym.name.toString == name
2348+
}.getOrElse(dotc.core.Symbols.NoSymbol)
2349+
2350+
def memberFields: List[Symbol] =
2351+
appliedTypeRef(self).allMembers.iterator.map(_.symbol).collect {
2352+
case sym if isField(sym) => sym.asTerm
2353+
}.toList
2354+
23442355
def declaredMethod(name: String): List[Symbol] =
23452356
self.typeRef.decls.iterator.collect {
23462357
case sym if isMethod(sym) && sym.name.toString == name => sym.asTerm

library/src/scala/quoted/Quotes.scala

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3159,11 +3159,17 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
31593159
/** Does this symbol represent a definition? */
31603160
def exists: Boolean
31613161

3162+
/** Field with the given name directly declared in the class */
3163+
def declaredField(name: String): Symbol
3164+
31623165
/** Fields directly declared in the class */
3163-
def fields: List[Symbol]
3166+
def declaredFields: List[Symbol]
31643167

3165-
/** Field with the given name directly declared in the class */
3166-
def field(name: String): Symbol
3168+
/** Get named non-private fields declared or inherited */
3169+
def memberField(name: String): Symbol
3170+
3171+
/** Get all non-private fields declared or inherited */
3172+
def memberFields: List[Symbol]
31673173

31683174
/** Get non-private named methods defined directly inside the class */
31693175
def declaredMethod(name: String): List[Symbol]

scala3doc/src/dotty/dokka/tasty/ClassLikeSupport.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ trait ClassLikeSupport:
243243
}.toList
244244

245245
def getParameterModifier(parameter: Symbol): String =
246-
val fieldSymbol = c.symbol.field(parameter.normalizedName)
246+
val fieldSymbol = c.symbol.declaredField(parameter.normalizedName)
247247
if fieldSymbol.flags.is(Flags.Mutable) then "var "
248248
else if fieldSymbol.flags.is(Flags.ParamAccessor) && !c.symbol.flags.is(Flags.Case) && !fieldSymbol.flags.is(Flags.Private) then "val "
249249
else ""

scala3doc/test/dotty/dokka/tasty/comments/MemberLookupTests.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ class LookupTestCases[Q <: Quotes](val q: Quotes) {
8787
def fld(name: String) =
8888
def hackResolveModule(s: q.reflect.Symbol): q.reflect.Symbol =
8989
if s.flags.is(q.reflect.Flags.Module) then s.moduleClass else s
90-
Sym(hackResolveModule(symbol.field(name)))
90+
Sym(hackResolveModule(symbol.declaredField(name)))
9191
def fun(name: String) =
9292
val List(sym) = symbol.memberMethod(name)
9393
Sym(sym)

tests/pos-macros/i8879/Macro_1.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ object Test {
1010
import util._
1111

1212
val foo = TypeRepr.of[Foo[String]]
13-
val symbol = foo.typeSymbol.field("a")
13+
val symbol = foo.typeSymbol.memberField("a")
1414
val a = foo.select(symbol)
1515
assert(a <:< TypeRepr.of[String])
1616

tests/run-macros/gestalt-type-toolbox-reflect/Macro_1.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,14 @@ object TypeToolbox {
4444
inline def fieldIn[T](inline mem: String): String = ${fieldInImpl[T]('mem)}
4545
private def fieldInImpl[T: Type](mem: Expr[String])(using Quotes) : Expr[String] = {
4646
import quotes.reflect._
47-
val field = TypeTree.of[T].symbol.field(mem.valueOrError)
47+
val field = TypeTree.of[T].symbol.declaredField(mem.valueOrError)
4848
Expr(if field.isNoSymbol then "" else field.name)
4949
}
5050

5151
inline def fieldsIn[T]: Seq[String] = ${fieldsInImpl[T]}
5252
private def fieldsInImpl[T: Type](using Quotes) : Expr[Seq[String]] = {
5353
import quotes.reflect._
54-
val fields = TypeTree.of[T].symbol.fields
54+
val fields = TypeTree.of[T].symbol.declaredFields
5555
Expr(fields.map(_.name).toList)
5656
}
5757

0 commit comments

Comments
 (0)