Skip to content

Commit 4ebbdd0

Browse files
Merge pull request #8961 from dotty-staging/fix-#8514
Fix #8514: Add Reflection.Type.baseClasses
2 parents 4e7b4b9 + 0ca6d2a commit 4ebbdd0

File tree

6 files changed

+32
-0
lines changed

6 files changed

+32
-0
lines changed

compiler/src/dotty/tools/dotc/tastyreflect/ReflectionCompilerInterface.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1198,6 +1198,9 @@ class ReflectionCompilerInterface(val rootContext: core.Contexts.Context) extend
11981198
def Type_memberType(self: Type)(member: Symbol)(using ctx: Context): Type =
11991199
member.info.asSeenFrom(self, member.owner)
12001200

1201+
def Type_baseClasses(self: Type)(using ctx: Context): List[Symbol] =
1202+
self.baseClasses
1203+
12011204
def Type_derivesFrom(self: Type)(cls: Symbol)(using ctx: Context): Boolean =
12021205
self.derivesFrom(cls)
12031206

library/src/scala/tasty/Reflection.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1646,6 +1646,9 @@ class Reflection(private[scala] val internal: CompilerInterface) { self =>
16461646
def isSingleton(using ctx: Context): Boolean = internal.Type_isSingleton(self)
16471647
def memberType(member: Symbol)(using ctx: Context): Type = internal.Type_memberType(self)(member)
16481648

1649+
/** The base classes of this type with the class itself as first element. */
1650+
def baseClasses(using ctx: Context): List[Symbol] = internal.Type_baseClasses(self)
1651+
16491652
/** Is this type an instance of a non-bottom subclass of the given class `cls`? */
16501653
def derivesFrom(cls: Symbol)(using ctx: Context): Boolean =
16511654
internal.Type_derivesFrom(self)(cls)

library/src/scala/tasty/reflect/CompilerInterface.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -871,6 +871,9 @@ trait CompilerInterface {
871871

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

874+
/** The base classes of this type with the class itself as first element. */
875+
def Type_baseClasses(self: Type)(using ctx: Context): List[Symbol]
876+
874877
/** Is this type an instance of a non-bottom subclass of the given class `cls`? */
875878
def Type_derivesFrom(self: Type)(cls: Symbol)(using ctx: Context): Boolean
876879

tests/run-macros/i8514.check

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
List(class Object, class Any)
2+
List(class A, class Object, class Any)
3+
List(class B, class A, class Object, class Any)
4+
List(class C, class B, class A, class Object, class Any)

tests/run-macros/i8514/Macro_1.scala

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import scala.quoted._
2+
3+
class A
4+
class B extends A
5+
class C extends B
6+
7+
inline def test(): Unit = ${ testExpr }
8+
9+
def testExpr(using QuoteContext): Expr[Unit] = {
10+
import qctx.tasty._
11+
12+
'{
13+
println(${Expr('[Object].unseal.tpe.baseClasses.toString)})
14+
println(${Expr('[A].unseal.tpe.baseClasses.toString)})
15+
println(${Expr('[B].unseal.tpe.baseClasses.toString)})
16+
println(${Expr('[C].unseal.tpe.baseClasses.toString)})
17+
}
18+
}

tests/run-macros/i8514/Test_2.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
@main def Test = test()

0 commit comments

Comments
 (0)