Skip to content

Remove Reflection StandardTypes #9815

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
10 changes: 5 additions & 5 deletions library/src-bootstrapped/dotty/internal/StringContextMacro.scala
Original file line number Diff line number Diff line change
Expand Up @@ -585,11 +585,11 @@ object StringContextMacro {
* nothing otherwise
*/
def checkTypeWithArgs(argument : (Type, Int), conversionChar : Char, partIndex : Int, flags : List[(Char, Int)]) = {
val booleans = List(defn.BooleanType, defn.NullType)
val dates = List(defn.LongType, Type.of[java.util.Calendar], Type.of[java.util.Date])
val floatingPoints = List(defn.DoubleType, defn.FloatType, Type.of[java.math.BigDecimal])
val integral = List(defn.IntType, defn.LongType, defn.ShortType, defn.ByteType, Type.of[java.math.BigInteger])
val character = List(defn.CharType, defn.ByteType, defn.ShortType, defn.IntType)
val booleans = List(Type.of[Boolean], Type.of[Null])
val dates = List(Type.of[Long], Type.of[java.util.Calendar], Type.of[java.util.Date])
val floatingPoints = List(Type.of[Double], Type.of[Float], Type.of[java.math.BigDecimal])
val integral = List(Type.of[Int], Type.of[Long], Type.of[Short], Type.of[Byte], Type.of[java.math.BigInteger])
val character = List(Type.of[Char], Type.of[Byte], Type.of[Short], Type.of[Int])

val (argType, argIndex) = argument
conversionChar match {
Expand Down
36 changes: 27 additions & 9 deletions library/src-bootstrapped/scala/internal/quoted/Type.scala
Original file line number Diff line number Diff line change
Expand Up @@ -47,30 +47,48 @@ object Type {
}

def Unit: QuoteContext ?=> quoted.Type[Unit] =
qctx.tasty.defn.UnitType.seal.asInstanceOf[quoted.Type[Unit]]
val qctx1 = quoteContextWithCompilerInterface(qctx)
qctx1.tasty.Definitions_UnitType.seal.asInstanceOf[quoted.Type[Unit]]


def Boolean: QuoteContext ?=> quoted.Type[Boolean] =
qctx.tasty.defn.BooleanType.seal.asInstanceOf[quoted.Type[Boolean]]
val qctx1 = quoteContextWithCompilerInterface(qctx)
qctx1.tasty.Definitions_BooleanType.seal.asInstanceOf[quoted.Type[Boolean]]


def Byte: QuoteContext ?=> quoted.Type[Byte] =
qctx.tasty.defn.ByteType.seal.asInstanceOf[quoted.Type[Byte]]
val qctx1 = quoteContextWithCompilerInterface(qctx)
qctx1.tasty.Definitions_ByteType.seal.asInstanceOf[quoted.Type[Byte]]


def Char: QuoteContext ?=> quoted.Type[Char] =
qctx.tasty.defn.CharType.seal.asInstanceOf[quoted.Type[Char]]
val qctx1 = quoteContextWithCompilerInterface(qctx)
qctx1.tasty.Definitions_CharType.seal.asInstanceOf[quoted.Type[Char]]


def Short: QuoteContext ?=> quoted.Type[Short] =
qctx.tasty.defn.ShortType.seal.asInstanceOf[quoted.Type[Short]]
val qctx1 = quoteContextWithCompilerInterface(qctx)
qctx1.tasty.Definitions_ShortType.seal.asInstanceOf[quoted.Type[Short]]


def Int: QuoteContext ?=> quoted.Type[Int] =
qctx.tasty.defn.IntType.seal.asInstanceOf[quoted.Type[Int]]
val qctx1 = quoteContextWithCompilerInterface(qctx)
qctx1.tasty.Definitions_IntType.seal.asInstanceOf[quoted.Type[Int]]


def Long: QuoteContext ?=> quoted.Type[Long] =
qctx.tasty.defn.LongType.seal.asInstanceOf[quoted.Type[Long]]
val qctx1 = quoteContextWithCompilerInterface(qctx)
qctx1.tasty.Definitions_LongType.seal.asInstanceOf[quoted.Type[Long]]


def Float: QuoteContext ?=> quoted.Type[Float] =
qctx.tasty.defn.FloatType.seal.asInstanceOf[quoted.Type[Float]]
val qctx1 = quoteContextWithCompilerInterface(qctx)
qctx1.tasty.Definitions_FloatType.seal.asInstanceOf[quoted.Type[Float]]


def Double: QuoteContext ?=> quoted.Type[Double] =
qctx.tasty.defn.DoubleType.seal.asInstanceOf[quoted.Type[Double]]
val qctx1 = quoteContextWithCompilerInterface(qctx)
qctx1.tasty.Definitions_DoubleType.seal.asInstanceOf[quoted.Type[Double]]

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we do the same for Type.Int, etc as in Reflection.Type?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That definition is not visible in the public API. It is used as an optimization of '[Int]. I'm looking for other ways to optimize it differently but it will take some time.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, it makes sense.


}
14 changes: 7 additions & 7 deletions library/src-bootstrapped/scala/quoted/util/ExprMap.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ trait ExprMap {
def localCtx(definition: Definition): Context = definition.symbol.localContext
tree match {
case tree: Term =>
transformTerm(tree, defn.AnyType)
transformTerm(tree, Type.of[Any])
case tree: Definition =>
transformDefinition(tree)
case tree: Import =>
Expand Down Expand Up @@ -53,9 +53,9 @@ trait ExprMap {
tree
case tree @ Apply(fun, args) =>
val MethodType(_, tpes, _) = fun.tpe.widen
Apply.copy(tree)(transformTerm(fun, defn.AnyType), transformTerms(args, tpes))
Apply.copy(tree)(transformTerm(fun, Type.of[Any]), transformTerms(args, tpes))
case TypeApply(fun, args) =>
TypeApply.copy(tree)(transformTerm(fun, defn.AnyType), args)
TypeApply.copy(tree)(transformTerm(fun, Type.of[Any]), args)
case _: Literal =>
tree
case New(tpt) =>
Expand All @@ -74,7 +74,7 @@ trait ExprMap {
Block.copy(tree)(transformStats(stats), transformTerm(expr, tpe))
case If(cond, thenp, elsep) =>
If.copy(tree)(
transformTerm(cond, defn.BooleanType),
transformTerm(cond, Type.of[Boolean]),
transformTerm(thenp, tpe),
transformTerm(elsep, tpe))
case _: Closure =>
Expand All @@ -87,9 +87,9 @@ trait ExprMap {
// Return.copy(tree)(transformTerm(expr, expr.tpe))
tree
case While(cond, body) =>
While.copy(tree)(transformTerm(cond, defn.BooleanType), transformTerm(body, defn.AnyType))
While.copy(tree)(transformTerm(cond, Type.of[Boolean]), transformTerm(body, Type.of[Any]))
case Try(block, cases, finalizer) =>
Try.copy(tree)(transformTerm(block, tpe), transformCaseDefs(cases, defn.AnyType), finalizer.map(x => transformTerm(x, defn.AnyType)))
Try.copy(tree)(transformTerm(block, tpe), transformCaseDefs(cases, Type.of[Any]), finalizer.map(x => transformTerm(x, Type.of[Any])))
case Repeated(elems, elemtpt) =>
Repeated.copy(tree)(transformTerms(elems, elemtpt.tpe), elemtpt)
case Inlined(call, bindings, expansion) =>
Expand All @@ -113,7 +113,7 @@ trait ExprMap {
def transformTypeTree(tree: TypeTree)(using ctx: Context): TypeTree = tree

def transformCaseDef(tree: CaseDef, tpe: Type)(using ctx: Context): CaseDef =
CaseDef.copy(tree)(tree.pattern, tree.guard.map(x => transformTerm(x, defn.BooleanType)), transformTerm(tree.rhs, tpe))
CaseDef.copy(tree)(tree.pattern, tree.guard.map(x => transformTerm(x, Type.of[Boolean])), transformTerm(tree.rhs, tpe))

def transformTypeCaseDef(tree: TypeCaseDef)(using ctx: Context): TypeCaseDef = {
TypeCaseDef.copy(tree)(transformTypeTree(tree.pattern), transformTypeTree(tree.rhs))
Expand Down
67 changes: 1 addition & 66 deletions library/src/scala/tasty/Reflection.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2193,7 +2193,7 @@ trait Reflection extends reflect.Types { reflectSelf: CompilerInterface =>
/** A value containing all standard definitions in [[DefinitionsAPI]]
* @group Definitions
*/
object defn extends StandardSymbols with StandardTypes
object defn extends StandardSymbols

/** Defines standard symbols (and types via its base trait).
* @group API
Expand Down Expand Up @@ -2366,71 +2366,6 @@ trait Reflection extends reflect.Types { reflectSelf: CompilerInterface =>

}

/** Defines standard types.
* @group Definitions
*/
trait StandardTypes {
/** The type of primitive type `Unit`. */
def UnitType: Type = reflectSelf.Definitions_UnitType

/** The type of primitive type `Byte`. */
def ByteType: Type = reflectSelf.Definitions_ByteType

/** The type of primitive type `Short`. */
def ShortType: Type = reflectSelf.Definitions_ShortType

/** The type of primitive type `Char`. */
def CharType: Type = reflectSelf.Definitions_CharType

/** The type of primitive type `Int`. */
def IntType: Type = reflectSelf.Definitions_IntType

/** The type of primitive type `Long`. */
def LongType: Type = reflectSelf.Definitions_LongType

/** The type of primitive type `Float`. */
def FloatType: Type = reflectSelf.Definitions_FloatType

/** The type of primitive type `Double`. */
def DoubleType: Type = reflectSelf.Definitions_DoubleType

/** The type of primitive type `Boolean`. */
def BooleanType: Type = reflectSelf.Definitions_BooleanType

/** The type of core type `Any`. */
def AnyType: Type = reflectSelf.Definitions_AnyType

/** The type of core type `AnyVal`. */
def AnyValType: Type = reflectSelf.Definitions_AnyValType

/** The type of core type `AnyRef`. */
def AnyRefType: Type = reflectSelf.Definitions_AnyRefType

/** The type of core type `Object`. */
def ObjectType: Type = reflectSelf.Definitions_ObjectType

/** The type of core type `Nothing`. */
def NothingType: Type = reflectSelf.Definitions_NothingType

/** The type of core type `Null`. */
def NullType: Type = reflectSelf.Definitions_NullType

/** The type for `scala.String`. */
def StringType: Type = reflectSelf.Definitions_StringType

/** The type for `scala.Tuple`. */
def TupleType: Type = reflectSelf.Definitions_TupleType

/** The type for `scala.EmptyTuple`. */
def EmptyTupleType: Type = reflectSelf.Definitions_EmptyTupleType

/** The type for `scala.NonEmptyTuple`. */
def NonEmptyTupleType: Type = reflectSelf.Definitions_NonEmptyTupleType

/** The type for `scala.*:`. */
def TupleConsType: Type = reflectSelf.Definitions_TupleConsType
}


///////////////
// FLAGS //
Expand Down
2 changes: 1 addition & 1 deletion tests/pos-macros/i8879/Macro_1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ object Test {
val foo = Type.of[Foo[String]]
val symbol = foo.typeSymbol.field("a")
val a = foo.select(symbol)
assert(a <:< defn.StringType)
assert(a <:< Type.of[String])

'{???}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class TastyInterpreter extends TastyInspector {
override def traverseTree(tree: Tree)(implicit ctx: Context): Unit = tree match {
// TODO: check the correct sig and object enclosement for main
case DefDef("main", _, _, _, Some(rhs)) =>
val interpreter = new jvm.Interpreter(this.reflect)
val interpreter = new jvm.Interpreter

interpreter.eval(rhs)(using Map.empty)
// TODO: recurse only for PackageDef, ClassDef
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package scala.tasty.interpreter

import scala.quoted._
import scala.tasty.interpreter.jvm.JVMReflection
import scala.tasty.Reflection

abstract class TreeInterpreter[R <: Reflection & Singleton](val reflect: R) {
import reflect.{_, given _}
abstract class TreeInterpreter[QCtx <: QuoteContext & Singleton](using val qctx: QCtx) {
import qctx.tasty._

final val LOG = false

Expand Down Expand Up @@ -196,15 +197,15 @@ abstract class TreeInterpreter[R <: Reflection & Singleton](val reflect: R) {
isIntegralPrimitive(tpe) || isFractionalPrimitive(tpe)

private def isIntegralPrimitive(tpe: Type): Boolean = {
tpe <:< defn.ByteType ||
tpe <:< defn.CharType ||
tpe <:< defn.ShortType ||
tpe <:< defn.IntType ||
tpe <:< defn.LongType
tpe <:< Type.of[Byte] ||
tpe <:< Type.of[Char] ||
tpe <:< Type.of[Short] ||
tpe <:< Type.of[Int] ||
tpe <:< Type.of[Long]
}

private def isFractionalPrimitive(tpe: Type): Boolean =
tpe <:< defn.FloatType || tpe <:< defn.DoubleType
tpe <:< Type.of[Float] || tpe <:< Type.of[Double]


private object Call {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
package scala.tasty.interpreter
package jvm

import scala.quoted._
import scala.tasty.interpreter.jvm.JVMReflection
import scala.tasty.Reflection

class Interpreter[R <: Reflection & Singleton](reflect0: R) extends TreeInterpreter[R](reflect0) {
import reflect.{_, given _}
class Interpreter[QCtx <: QuoteContext & Singleton](using qctx0: QCtx) extends TreeInterpreter[QCtx] {
import qctx.tasty._

// All references are represented by themselves and values are boxed
type AbstractAny = Any

val jvmReflection = new JVMReflection(reflect)
val jvmReflection = new JVMReflection(using qctx)

def interpretNew(fn: Tree, argss: List[List[Term]]): Result = {
if (fn.symbol.isDefinedInCurrentRun) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package scala.tasty.interpreter.jvm

import scala.quoted._
import scala.tasty.Reflection

class JVMReflection[R <: Reflection & Singleton](val reflect: R) {
import reflect.{_, given _}
class JVMReflection[QCtx <: QuoteContext & Singleton](using val tasty: QCtx) {
import qctx.tasty._

import java.lang.reflect.{InvocationTargetException, Method}
private val classLoader: ClassLoader = getClass.getClassLoader

Expand Down
2 changes: 1 addition & 1 deletion tests/run-macros/f-interpolation-1/FQuote_1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ object FQuote {
val Typed(Repeated(allArgs, _), _) = args.unseal.underlyingArgument

for ((arg, part) <- allArgs.zip(parts.tail)) {
if (part.startsWith("%d") && !(arg.tpe <:< defn.IntType)) {
if (part.startsWith("%d") && !(arg.tpe <:< Type.of[Int])) {
return '{s"`${${Expr(arg.show)}}` is not of type Int"}
}

Expand Down
4 changes: 2 additions & 2 deletions tests/run-macros/refined-selectable-macro/Macro_1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,11 @@ object Macro {
(seen + name, (name, info))
case _ =>
report.error("Tuple type was not explicit expected `(S, T)` where S is a singleton string", s)
(seen, ("<error>", defn.AnyType))
(seen, ("<error>", Type.of[Any]))
}
}
def rec(tpe: Type, seen: Set[String]): List[(String, Type)] = {
if tpe =:= defn.EmptyTupleType then Nil
if tpe =:= Type.of[EmptyTuple] then Nil
else tpe match {
// head *: tail
case AppliedType(parent, List(head, tail: Type)) if isTupleCons(parent.typeSymbol) =>
Expand Down
14 changes: 7 additions & 7 deletions tests/run-macros/tasty-definitions-1.check
Original file line number Diff line number Diff line change
Expand Up @@ -163,10 +163,10 @@ TypeRef(ThisType(TypeRef(NoPrefix(), "scala")), "Long")
TypeRef(ThisType(TypeRef(NoPrefix(), "scala")), "Float")
TypeRef(ThisType(TypeRef(NoPrefix(), "scala")), "Double")
TypeRef(ThisType(TypeRef(NoPrefix(), "scala")), "Boolean")
TypeRef(ThisType(TypeRef(NoPrefix(), "scala")), "Any")
TypeRef(ThisType(TypeRef(NoPrefix(), "scala")), "AnyVal")
TypeRef(ThisType(TypeRef(NoPrefix(), "scala")), "AnyRef")
TypeRef(ThisType(TypeRef(NoPrefix(), "lang")), "Object")
TypeRef(ThisType(TypeRef(NoPrefix(), "scala")), "Nothing")
TypeRef(ThisType(TypeRef(NoPrefix(), "scala")), "Null")
TypeRef(ThisType(TypeRef(NoPrefix(), "lang")), "String")
TypeRef(TermRef(ThisType(TypeRef(NoPrefix(), "<root>")), "scala"), "Any")
TypeRef(TermRef(ThisType(TypeRef(NoPrefix(), "<root>")), "scala"), "AnyVal")
TypeRef(TermRef(ThisType(TypeRef(NoPrefix(), "java")), "lang"), "Object")
TypeRef(TermRef(ThisType(TypeRef(NoPrefix(), "java")), "lang"), "Object")
TypeRef(TermRef(ThisType(TypeRef(NoPrefix(), "<root>")), "scala"), "Nothing")
TypeRef(TermRef(ThisType(TypeRef(NoPrefix(), "<root>")), "scala"), "Null")
TypeRef(TermRef(ThisType(TypeRef(NoPrefix(), "java")), "lang"), "String")
30 changes: 15 additions & 15 deletions tests/run-macros/tasty-definitions-1/quoted_1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -74,21 +74,21 @@ object Macros {
printout(defn.ScalaPrimitiveValueClasses.map(_.name).toString)
printout(defn.ScalaNumericValueClasses.map(_.name).toString)

printout(defn.UnitType.showExtractors)
printout(defn.ByteType.showExtractors)
printout(defn.CharType.showExtractors)
printout(defn.IntType.showExtractors)
printout(defn.LongType.showExtractors)
printout(defn.FloatType.showExtractors)
printout(defn.DoubleType.showExtractors)
printout(defn.BooleanType.showExtractors)
printout(defn.AnyType.showExtractors)
printout(defn.AnyValType.showExtractors)
printout(defn.AnyRefType.showExtractors)
printout(defn.ObjectType.showExtractors)
printout(defn.NothingType.showExtractors)
printout(defn.NullType.showExtractors)
printout(defn.StringType.showExtractors)
printout(Type.of[Unit].showExtractors)
printout(Type.of[Byte].showExtractors)
printout(Type.of[Char].showExtractors)
printout(Type.of[Int].showExtractors)
printout(Type.of[Long].showExtractors)
printout(Type.of[Float].showExtractors)
printout(Type.of[Double].showExtractors)
printout(Type.of[Boolean].showExtractors)
printout(Type.of[Any].showExtractors)
printout(Type.of[AnyVal].showExtractors)
printout(Type.of[AnyRef].showExtractors)
printout(Type.of[Object].showExtractors)
printout(Type.of[Nothing].showExtractors)
printout(Type.of[Null].showExtractors)
printout(Type.of[String].showExtractors)


'{println(${Expr(buff.result().mkString("\n"))})}
Expand Down
Loading