Skip to content

Add Symbol.allMembers to reflection API #13391

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

Closed
wants to merge 4 commits into from
Closed
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
7 changes: 7 additions & 0 deletions compiler/src/scala/quoted/runtime/impl/QuotesImpl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2603,6 +2603,13 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
def declarations: List[Symbol] =
self.typeRef.info.decls.toList

def allMembers: List[Symbol] =
lookupPrefix.allMembers.iterator.map(_.symbol).collect {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Based on the test I added, the order of the members is not deterministic.

case sym if sym.isType => sym.asType
case sym if isMethod(sym) => sym.asTerm
case sym if isField(sym) => sym.asTerm
}.toList

def paramSymss: List[List[Symbol]] = self.denot.paramSymss
def primaryConstructor: Symbol = self.denot.primaryConstructor
def allOverriddenSymbols: Iterator[Symbol] = self.denot.allOverriddenSymbols
Expand Down
3 changes: 3 additions & 0 deletions library/src/scala/quoted/Quotes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3756,6 +3756,9 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
/** All members directly declared in the class */
def declarations: List[Symbol]

/** All members declared or inherited */
def allMembers: List[Symbol]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Like in the documentation of Type#allMembers, we should mention that this can be slow to compute.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need then to update other methods documentation that use in their implementation Type#allMembers

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even better would be to fix these methods to not call allMembers :) Instead, we should be able to use memberDenots in most cases

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should make this an iterator. We already do this for allOverriddenSymbols which has a similar complexity.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method should also be marked as @experimental


/** The symbols of each type parameter list and value parameter list of this
* method, or Nil if this isn't a method.
*/
Expand Down
1 change: 1 addition & 0 deletions project/MiMaFilters.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ object MiMaFilters {
ProblemFilters.exclude[MissingClassProblem]("scala.compiletime.ops.float$"),
ProblemFilters.exclude[MissingClassProblem]("scala.compiletime.ops.long"),
ProblemFilters.exclude[MissingClassProblem]("scala.compiletime.ops.long$"),
ProblemFilters.exclude[ReversedMissingMethodProblem]("scala.quoted.Quotes#reflectModule#SymbolMethods.allMembers"),
ProblemFilters.exclude[ReversedMissingMethodProblem]("scala.quoted.Quotes#reflectModule#CompilationInfoModule.XmacroSettings"),
ProblemFilters.exclude[DirectMissingMethodProblem]("scala.quoted.Quotes#reflectModule#CompilationInfoModule.XmacroSettings"),

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,8 @@ trait ClassLikeSupport:
def membersToDocument = c.body.filterNot(_.symbol.isHiddenByVisibility)

def getNonTrivialInheritedMemberTrees =
c.symbol.getmembers.filterNot(s => s.isHiddenByVisibility || s.maybeOwner == c.symbol)
reflect.ClassDef.copy(c)(c.name, c.constructor, c.parents, None, c.body).symbol.allMembers
.filterNot(s => s.isHiddenByVisibility || s.maybeOwner == c.symbol)
.filter(s => s.maybeOwner != defn.ObjectClass && s.maybeOwner != defn.AnyClass)
.map(_.tree)

Expand Down
2 changes: 2 additions & 0 deletions tests/run-macros/allMembers.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
List(java.lang.Object.eq, scala.Any.hashCode, java.lang.Object.synchronized, scala.Any.$isInstanceOf$, scala.Any.$asInstanceOf$, java.lang.Object.notifyAll, java.lang.Object.clone, scala.Any.!=, scala.Any.getClass, java.lang.Object.finalize, scala.Any.toString, java.lang.Object.wait, java.lang.Object.wait, java.lang.Object.wait, java.lang.Object.notify, java.lang.Object.ne, scala.Any.##, scala.Any.equals, scala.Any.isInstanceOf, scala.Any.asInstanceOf, scala.Any.==)
List(A.foo, java.lang.Object.eq, scala.Any.hashCode, java.lang.Object.synchronized, scala.Any.$isInstanceOf$, A.x, scala.Any.$asInstanceOf$, java.lang.Object.notifyAll, java.lang.Object.clone, A.X, scala.Any.!=, scala.Any.getClass, java.lang.Object.finalize, scala.Any.toString, java.lang.Object.wait, java.lang.Object.wait, java.lang.Object.wait, java.lang.Object.notify, java.lang.Object.ne, scala.Any.##, scala.Any.equals, scala.Any.isInstanceOf, scala.Any.asInstanceOf, scala.Any.==)
8 changes: 8 additions & 0 deletions tests/run-macros/allMembers/Macro_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import scala.quoted.*

inline def allMembers[T]: List[String] =
${ allMembersExpr[T] }

private def allMembersExpr[T: Type](using Quotes): Expr[List[String]] =
import quotes.reflect.*
Expr(TypeRepr.of[T].typeSymbol.allMembers.map(_.fullName).toList)
7 changes: 7 additions & 0 deletions tests/run-macros/allMembers/Test_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class A:
type X
val x: Int = 1
def foo: Int = 1
@main def Test: Unit =
println(allMembers[Object])
println(allMembers[A])