Skip to content

Fix #8514: Add Reflection.Type.baseClasses #8961

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 @@ -1198,6 +1198,9 @@ class ReflectionCompilerInterface(val rootContext: core.Contexts.Context) extend
def Type_memberType(self: Type)(member: Symbol)(using ctx: Context): Type =
member.info.asSeenFrom(self, member.owner)

def Type_baseClasses(self: Type)(using ctx: Context): List[Symbol] =
self.baseClasses

def Type_derivesFrom(self: Type)(cls: Symbol)(using ctx: Context): Boolean =
self.derivesFrom(cls)

Expand Down
3 changes: 3 additions & 0 deletions library/src/scala/tasty/Reflection.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1646,6 +1646,9 @@ class Reflection(private[scala] val internal: CompilerInterface) { self =>
def isSingleton(using ctx: Context): Boolean = internal.Type_isSingleton(self)
def memberType(member: Symbol)(using ctx: Context): Type = internal.Type_memberType(self)(member)

/** The base classes of this type with the class itself as first element. */
def baseClasses(using ctx: Context): List[Symbol] = internal.Type_baseClasses(self)

/** Is this type an instance of a non-bottom subclass of the given class `cls`? */
def derivesFrom(cls: Symbol)(using ctx: Context): Boolean =
internal.Type_derivesFrom(self)(cls)
Expand Down
3 changes: 3 additions & 0 deletions library/src/scala/tasty/reflect/CompilerInterface.scala
Original file line number Diff line number Diff line change
Expand Up @@ -871,6 +871,9 @@ trait CompilerInterface {

def Type_memberType(self: Type)(member: Symbol)(using ctx: Context): Type

/** The base classes of this type with the class itself as first element. */
def Type_baseClasses(self: Type)(using ctx: Context): List[Symbol]

/** Is this type an instance of a non-bottom subclass of the given class `cls`? */
def Type_derivesFrom(self: Type)(cls: Symbol)(using ctx: Context): Boolean

Expand Down
4 changes: 4 additions & 0 deletions tests/run-macros/i8514.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
List(class Object, class Any)
List(class A, class Object, class Any)
List(class B, class A, class Object, class Any)
List(class C, class B, class A, class Object, class Any)
18 changes: 18 additions & 0 deletions tests/run-macros/i8514/Macro_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import scala.quoted._

class A
class B extends A
class C extends B

inline def test(): Unit = ${ testExpr }

def testExpr(using QuoteContext): Expr[Unit] = {
import qctx.tasty._

'{
println(${Expr('[Object].unseal.tpe.baseClasses.toString)})
println(${Expr('[A].unseal.tpe.baseClasses.toString)})
println(${Expr('[B].unseal.tpe.baseClasses.toString)})
println(${Expr('[C].unseal.tpe.baseClasses.toString)})
}
}
1 change: 1 addition & 0 deletions tests/run-macros/i8514/Test_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@main def Test = test()