Skip to content

Fix #8520: Add Reflection.Symbol.typeMembers #8962

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 1 commit into from
May 12, 2020
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 @@ -1732,6 +1732,12 @@ class ReflectionCompilerInterface(val rootContext: core.Contexts.Context) extend
case sym if isMethod(sym) && sym.name.toString == name => sym.asTerm
}.toList

def Symbol_typeMembers(self: Symbol)(using ctx: Context): List[Symbol] =
self.unforcedDecls.filter(_.isType)

def Symbol_typeMember(self: Symbol)(name: String)(using ctx: Context): Symbol =
self.unforcedDecls.find(sym => sym.name == name.toTypeName)

def Symbol_classMethods(self: Symbol)(using ctx: Context): List[Symbol] =
self.typeRef.decls.iterator.collect {
case sym if isMethod(sym) => sym.asTerm
Expand Down
8 changes: 8 additions & 0 deletions library/src/scala/tasty/Reflection.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2219,6 +2219,14 @@ class Reflection(private[scala] val internal: CompilerInterface) { self =>
def classMethods(using ctx: Context): List[Symbol] =
internal.Symbol_classMethods(sym)

/** Type member directly declared in the class */
def typeMembers(using ctx: Context): List[Symbol] =
internal.Symbol_typeMembers(sym)

/** Type member with the given name directly declared in the class */
def typeMember(name: String)(using ctx: Context): Symbol =
internal.Symbol_typeMember(sym)(name)

/** Get named non-private methods declared or inherited */
def method(name: String)(using ctx: Context): List[Symbol] =
internal.Symbol_method(sym)(name)
Expand Down
6 changes: 6 additions & 0 deletions library/src/scala/tasty/reflect/CompilerInterface.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1308,6 +1308,12 @@ trait CompilerInterface {
/** Get all non-private methods declared or inherited */
def Symbol_methods(self: Symbol)(using ctx: Context): List[Symbol]

/** Type member directly declared in the class */
def Symbol_typeMembers(self: Symbol)(using ctx: Context): List[Symbol]

/** Type member with the given name directly declared in the class */
def Symbol_typeMember(self: Symbol)(name: String)(using ctx: Context): Symbol

/** The symbols of each type parameter list and value parameter list of this
* method, or Nil if this isn't a method.
*/
Expand Down
3 changes: 3 additions & 0 deletions tests/run-macros/i8520.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
List((A,+))
List((B,-))
List((C, ))
13 changes: 13 additions & 0 deletions tests/run-macros/i8520/Macro_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import scala.quoted._

inline def test[T[_]]: Unit = ${ testExpr[T] }

def testExpr[T[_]: Type](using QuoteContext): Expr[Unit] = {
import qctx.tasty._
def variance(f: Flags) =
if f.is(Flags.Covariant) then "+"
else if f.is(Flags.Contravariant) then "-"
else " "
val t = '[T].unseal.tpe.typeSymbol.typeMembers.map(x => (x.name, variance(x.flags)))
'{ println(${Expr(t.toString)}) }
}
9 changes: 9 additions & 0 deletions tests/run-macros/i8520/Test_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
trait X[+A]
trait Y[-B]
trait Z[C]

@main def Test = {
test[X]
test[Y]
test[Z]
}