From 93e0140e28792ed2f19dff5ee3fc8cc07079d522 Mon Sep 17 00:00:00 2001 From: Nicolas Stucki Date: Tue, 12 May 2020 11:56:01 +0200 Subject: [PATCH] Fix #8879: Add Reflection.Type.select --- .../ReflectionCompilerInterface.scala | 3 +++ library/src/scala/tasty/Reflection.scala | 3 +++ .../tasty/reflect/CompilerInterface.scala | 4 +++- tests/pos-macros/i8879/Macro_1.scala | 23 +++++++++++++++++++ tests/pos-macros/i8879/Test_2.scala | 6 +++++ 5 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 tests/pos-macros/i8879/Macro_1.scala create mode 100644 tests/pos-macros/i8879/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..3491375726f7 100644 --- a/compiler/src/dotty/tools/dotc/tastyreflect/ReflectionCompilerInterface.scala +++ b/compiler/src/dotty/tools/dotc/tastyreflect/ReflectionCompilerInterface.scala @@ -1215,6 +1215,9 @@ class ReflectionCompilerInterface(val rootContext: core.Contexts.Context) extend tpNoRefinement != self && defn.isNonRefinedFunction(tpNoRefinement) } + def Type_select(self: Type)(sym: Symbol)(using ctx: Context): Type = + self.select(sym) + type ConstantType = Types.ConstantType def isInstanceOfConstantType(using ctx: Context): IsInstanceOf[ConstantType] = new { diff --git a/library/src/scala/tasty/Reflection.scala b/library/src/scala/tasty/Reflection.scala index d3f6e63719b7..f981cb1db3ba 100644 --- a/library/src/scala/tasty/Reflection.scala +++ b/library/src/scala/tasty/Reflection.scala @@ -1678,6 +1678,9 @@ class Reflection(private[scala] val internal: CompilerInterface) { self => * @see `isFunctionType` */ def isDependentFunctionType(using ctx: Context): Boolean = internal.Type_isDependentFunctionType(self) + + /** The type , reduced if possible */ + def select(sym: Symbol)(using ctx: Context): Type = internal.Type_select(self)(sym) } given (using ctx: Context) as IsInstanceOf[Type] = internal.isInstanceOfType diff --git a/library/src/scala/tasty/reflect/CompilerInterface.scala b/library/src/scala/tasty/reflect/CompilerInterface.scala index 55ac971e4ffb..4648c91511aa 100644 --- a/library/src/scala/tasty/reflect/CompilerInterface.scala +++ b/library/src/scala/tasty/reflect/CompilerInterface.scala @@ -885,7 +885,6 @@ trait CompilerInterface { */ def Type_isFunctionType(self: Type)(using ctx: Context): Boolean - /** Is this type an context function type? * * @see `Type_isFunctionType` @@ -904,6 +903,9 @@ trait CompilerInterface { */ def Type_isDependentFunctionType(self: Type)(using ctx: Context): Boolean + /** The type , reduced if possible */ + def Type_select(self: Type)(sym: Symbol)(using ctx: Context): Type + /** A singleton type representing a known constant value */ type ConstantType <: Type diff --git a/tests/pos-macros/i8879/Macro_1.scala b/tests/pos-macros/i8879/Macro_1.scala new file mode 100644 index 000000000000..fda2363167b0 --- /dev/null +++ b/tests/pos-macros/i8879/Macro_1.scala @@ -0,0 +1,23 @@ +case class Foo[A](a: A) + +object Test { + + import scala.quoted._ + + def impl[T](t: T)(using qctx: QuoteContext, tt: Type[T]): Expr[Any] = { + + import qctx.tasty._ + import util._ + + val foo = typeOf[Foo[String]] + val symbol = foo.typeSymbol.field("a") + val a = foo.select(symbol) + assert(a <:< defn.StringType) + + '{???} + } + + + inline def apply[T](inline t: T) = ${ Test.impl('t )} + +} diff --git a/tests/pos-macros/i8879/Test_2.scala b/tests/pos-macros/i8879/Test_2.scala new file mode 100644 index 000000000000..1398b274b0d6 --- /dev/null +++ b/tests/pos-macros/i8879/Test_2.scala @@ -0,0 +1,6 @@ + +object Run { + def test(): Unit = { + Test[Foo[String]](Foo("moo")) + } +}