Skip to content

Rename reflection Class[?] to Type conversion #9798

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
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 @@ -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
Expand All @@ -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)
Expand Down
8 changes: 5 additions & 3 deletions library/src-bootstrapped/scala/quoted/Liftable.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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]]]
}
}

Expand Down
6 changes: 5 additions & 1 deletion library/src/scala/internal/tasty/CompilerInterface.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)`.
Expand Down
2 changes: 0 additions & 2 deletions library/src/scala/tasty/Reflection.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down