From c9de0e127a80aa4567f9253b794be2fb259742f6 Mon Sep 17 00:00:00 2001 From: Nicolas Stucki Date: Tue, 15 Sep 2020 13:17:42 +0200 Subject: [PATCH] Remove Reflect Type.apply We only require this type representation to be able to lift and handle generic arrays. We will use this internally only as long as no real use case for this functionality appears. --- .../dotc/quoted/reflect/ReflectionCompilerInterface.scala | 6 +++--- library/src-bootstrapped/scala/quoted/Liftable.scala | 8 +++++--- library/src/scala/internal/tasty/CompilerInterface.scala | 6 +++++- library/src/scala/tasty/Reflection.scala | 2 -- 4 files changed, 13 insertions(+), 9 deletions(-) diff --git a/compiler/src/dotty/tools/dotc/quoted/reflect/ReflectionCompilerInterface.scala b/compiler/src/dotty/tools/dotc/quoted/reflect/ReflectionCompilerInterface.scala index 8ca193a7538b..06a31ffe792f 100644 --- a/compiler/src/dotty/tools/dotc/quoted/reflect/ReflectionCompilerInterface.scala +++ b/compiler/src/dotty/tools/dotc/quoted/reflect/ReflectionCompilerInterface.scala @@ -1128,7 +1128,7 @@ class ReflectionCompilerInterface(val rootContext: Context) extends CompilerInte case _ => None } - def Type_apply(clazz: Class[?])(using Context): Type = + def Type_ofErasedClass(clazz: Class[?])(using Context): Type = if (clazz.isPrimitive) if (clazz == classOf[Boolean]) defn.BooleanType else if (clazz == classOf[Byte]) defn.ByteType @@ -1140,10 +1140,10 @@ class ReflectionCompilerInterface(val rootContext: Context) extends CompilerInte else if (clazz == classOf[Double]) defn.DoubleType else defn.UnitType else if (clazz.isArray) - defn.ArrayType.appliedTo(Type_apply(clazz.getComponentType)) + defn.ArrayType.appliedTo(Type_ofErasedClass(clazz.getComponentType)) else if (clazz.isMemberClass) { val name = clazz.getSimpleName.toTypeName - val enclosing = Type_apply(clazz.getEnclosingClass) + val enclosing = Type_ofErasedClass(clazz.getEnclosingClass) if (enclosing.member(name).exists) enclosing.select(name) else enclosing.classSymbol.companionModule.termRef.select(name) diff --git a/library/src-bootstrapped/scala/quoted/Liftable.scala b/library/src-bootstrapped/scala/quoted/Liftable.scala index 3744c8302477..e99f43a00077 100644 --- a/library/src-bootstrapped/scala/quoted/Liftable.scala +++ b/library/src-bootstrapped/scala/quoted/Liftable.scala @@ -58,9 +58,11 @@ object Liftable { /** Default liftable for Class[T] */ given ClassLiftable[T] as Liftable[Class[T]] = new Liftable[Class[T]] { - def toExpr(x: Class[T]) = qctx ?=> { - import qctx.tasty._ - Ref(defn.Predef_classOf).appliedToType(Type(x)).seal.asInstanceOf[Expr[Class[T]]] + def toExpr(x: Class[T]) = { + val qctx1 = scala.internal.tasty.CompilerInterface.quoteContextWithCompilerInterface(qctx) + import qctx1.tasty._ + val tpe = qctx1.tasty.Type_ofErasedClass(x) + Ref(defn.Predef_classOf).appliedToType(tpe).seal.asInstanceOf[Expr[Class[T]]] } } diff --git a/library/src/scala/internal/tasty/CompilerInterface.scala b/library/src/scala/internal/tasty/CompilerInterface.scala index 080e059900ff..573c16ea3a54 100644 --- a/library/src/scala/internal/tasty/CompilerInterface.scala +++ b/library/src/scala/internal/tasty/CompilerInterface.scala @@ -512,7 +512,11 @@ trait CompilerInterface extends scala.tasty.reflect.Types { def Type_TypeTest(using ctx: Context): TypeTest[Type, Type] - def Type_apply(clazz: Class[_])(using ctx: Context): Type + /** Returns the type of the runtime class. This type is the erased representation + * that of the type that is used by arrays. + * + */ + def Type_ofErasedClass(clazz: Class[_])(using ctx: Context): Type /** Is `self` type the same as `that` type? * This is the case iff `Type_isSubType(self, that)` and `Type_isSubType(that, self)`. diff --git a/library/src/scala/tasty/Reflection.scala b/library/src/scala/tasty/Reflection.scala index ea0f67761f81..15fff08808d5 100644 --- a/library/src/scala/tasty/Reflection.scala +++ b/library/src/scala/tasty/Reflection.scala @@ -1353,8 +1353,6 @@ trait Reflection extends reflect.Types { reflectSelf: CompilerInterface => def of[T <: AnyKind](using qtype: scala.quoted.Type[T], ctx: Context): Type = qtype.asInstanceOf[scala.internal.quoted.Type[TypeTree]].typeTree.tpe - def apply(clazz: Class[_])(using ctx: Context): Type = - reflectSelf.Type_apply(clazz) end Type given TypeOps as AnyRef: