From 0ca6d2a197ef041f1cc8cfcc388f6707872b7698 Mon Sep 17 00:00:00 2001 From: Nicolas Stucki Date: Tue, 12 May 2020 15:42:27 +0200 Subject: [PATCH] Fix #8514: Add Reflection.Type.baseClasses --- .../ReflectionCompilerInterface.scala | 3 +++ library/src/scala/tasty/Reflection.scala | 3 +++ .../tasty/reflect/CompilerInterface.scala | 3 +++ tests/run-macros/i8514.check | 4 ++++ tests/run-macros/i8514/Macro_1.scala | 18 ++++++++++++++++++ tests/run-macros/i8514/Test_2.scala | 1 + 6 files changed, 32 insertions(+) create mode 100644 tests/run-macros/i8514.check create mode 100644 tests/run-macros/i8514/Macro_1.scala create mode 100644 tests/run-macros/i8514/Test_2.scala diff --git a/compiler/src/dotty/tools/dotc/tastyreflect/ReflectionCompilerInterface.scala b/compiler/src/dotty/tools/dotc/tastyreflect/ReflectionCompilerInterface.scala index c2bb1173aced..bc08d43a79fb 100644 --- a/compiler/src/dotty/tools/dotc/tastyreflect/ReflectionCompilerInterface.scala +++ b/compiler/src/dotty/tools/dotc/tastyreflect/ReflectionCompilerInterface.scala @@ -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) diff --git a/library/src/scala/tasty/Reflection.scala b/library/src/scala/tasty/Reflection.scala index d3f6e63719b7..456a7a3ede0a 100644 --- a/library/src/scala/tasty/Reflection.scala +++ b/library/src/scala/tasty/Reflection.scala @@ -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) diff --git a/library/src/scala/tasty/reflect/CompilerInterface.scala b/library/src/scala/tasty/reflect/CompilerInterface.scala index 55ac971e4ffb..5adb3adb2747 100644 --- a/library/src/scala/tasty/reflect/CompilerInterface.scala +++ b/library/src/scala/tasty/reflect/CompilerInterface.scala @@ -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 diff --git a/tests/run-macros/i8514.check b/tests/run-macros/i8514.check new file mode 100644 index 000000000000..f4acc82925cb --- /dev/null +++ b/tests/run-macros/i8514.check @@ -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) diff --git a/tests/run-macros/i8514/Macro_1.scala b/tests/run-macros/i8514/Macro_1.scala new file mode 100644 index 000000000000..4df22f3c6c51 --- /dev/null +++ b/tests/run-macros/i8514/Macro_1.scala @@ -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)}) + } +} diff --git a/tests/run-macros/i8514/Test_2.scala b/tests/run-macros/i8514/Test_2.scala new file mode 100644 index 000000000000..c96956b4554d --- /dev/null +++ b/tests/run-macros/i8514/Test_2.scala @@ -0,0 +1 @@ +@main def Test = test()