Skip to content

Rework reflect Symbol fields API #10705

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ object FieldsImpl:
def isProjectField(s: Symbol) =
s.isValDef && s.tree.asInstanceOf[ValDef].tpt.tpe <:< retType
val projectsTree = from.asTerm
val symbols = TypeTree.of[V].symbol.fields.filter(isProjectField)
val symbols = TypeTree.of[V].symbol.declaredFields.filter(isProjectField)
val selects = symbols.map(Select(projectsTree, _).asExprOf[T])
'{ println(${Expr(retType.show)}); ${Varargs(selects)} }
15 changes: 13 additions & 2 deletions compiler/src/scala/quoted/runtime/impl/QuotesImpl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2335,12 +2335,23 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
self.is(dotc.core.Flags.Case, butNot = Enum | Module) && !self.isClass
def isNoSymbol: Boolean = self == Symbol.noSymbol
def exists: Boolean = self != Symbol.noSymbol
def fields: List[Symbol] = self.unforcedDecls.filter(isField)

def field(name: String): Symbol =
def declaredField(name: String): Symbol =
val sym = self.unforcedDecls.find(sym => sym.name == name.toTermName)
if (isField(sym)) sym else dotc.core.Symbols.NoSymbol

def declaredFields: List[Symbol] = self.unforcedDecls.filter(isField)

def memberField(name: String): Symbol =
appliedTypeRef(self).allMembers.iterator.map(_.symbol).find {
sym => isField(sym) && sym.name.toString == name
}.getOrElse(dotc.core.Symbols.NoSymbol)

def memberFields: List[Symbol] =
appliedTypeRef(self).allMembers.iterator.map(_.symbol).collect {
case sym if isField(sym) => sym.asTerm
}.toList

def declaredMethod(name: String): List[Symbol] =
self.typeRef.decls.iterator.collect {
case sym if isMethod(sym) && sym.name.toString == name => sym.asTerm
Expand Down
12 changes: 9 additions & 3 deletions library/src/scala/quoted/Quotes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3159,11 +3159,17 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
/** Does this symbol represent a definition? */
def exists: Boolean

/** Field with the given name directly declared in the class */
def declaredField(name: String): Symbol

/** Fields directly declared in the class */
def fields: List[Symbol]
def declaredFields: List[Symbol]

/** Field with the given name directly declared in the class */
def field(name: String): Symbol
/** Get named non-private fields declared or inherited */
def memberField(name: String): Symbol

/** Get all non-private fields declared or inherited */
def memberFields: List[Symbol]

/** Get non-private named methods defined directly inside the class */
def declaredMethod(name: String): List[Symbol]
Expand Down
2 changes: 1 addition & 1 deletion scala3doc/src/dotty/dokka/tasty/ClassLikeSupport.scala
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ trait ClassLikeSupport:
}.toList

def getParameterModifier(parameter: Symbol): String =
val fieldSymbol = c.symbol.field(parameter.normalizedName)
val fieldSymbol = c.symbol.declaredField(parameter.normalizedName)
if fieldSymbol.flags.is(Flags.Mutable) then "var "
else if fieldSymbol.flags.is(Flags.ParamAccessor) && !c.symbol.flags.is(Flags.Case) && !fieldSymbol.flags.is(Flags.Private) then "val "
else ""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ class LookupTestCases[Q <: Quotes](val q: Quotes) {
def fld(name: String) =
def hackResolveModule(s: q.reflect.Symbol): q.reflect.Symbol =
if s.flags.is(q.reflect.Flags.Module) then s.moduleClass else s
Sym(hackResolveModule(symbol.field(name)))
Sym(hackResolveModule(symbol.declaredField(name)))
def fun(name: String) =
val List(sym) = symbol.memberMethod(name)
Sym(sym)
Expand Down
2 changes: 1 addition & 1 deletion tests/pos-macros/i8879/Macro_1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ object Test {
import util._

val foo = TypeRepr.of[Foo[String]]
val symbol = foo.typeSymbol.field("a")
val symbol = foo.typeSymbol.memberField("a")
val a = foo.select(symbol)
assert(a <:< TypeRepr.of[String])

Expand Down
4 changes: 2 additions & 2 deletions tests/run-macros/gestalt-type-toolbox-reflect/Macro_1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,14 @@ object TypeToolbox {
inline def fieldIn[T](inline mem: String): String = ${fieldInImpl[T]('mem)}
private def fieldInImpl[T: Type](mem: Expr[String])(using Quotes) : Expr[String] = {
import quotes.reflect._
val field = TypeTree.of[T].symbol.field(mem.valueOrError)
val field = TypeTree.of[T].symbol.declaredField(mem.valueOrError)
Expr(if field.isNoSymbol then "" else field.name)
}

inline def fieldsIn[T]: Seq[String] = ${fieldsInImpl[T]}
private def fieldsInImpl[T: Type](using Quotes) : Expr[Seq[String]] = {
import quotes.reflect._
val fields = TypeTree.of[T].symbol.fields
val fields = TypeTree.of[T].symbol.declaredFields
Expr(fields.map(_.name).toList)
}

Expand Down