Skip to content

Commit

Permalink
Merge pull request scala#8962 from dotty-staging/fix-#8520-2
Browse files Browse the repository at this point in the history
Fix scala#8520: Add Reflection.Symbol.typeMembers
  • Loading branch information
anatoliykmetyuk authored May 12, 2020
2 parents 4ebbdd0 + 5a4fe89 commit eeda658
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1738,6 +1738,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 @@ -2225,6 +2225,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 @@ -1313,6 +1313,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]
}

0 comments on commit eeda658

Please sign in to comment.