Skip to content

Remove scala.internal.quoted.LiftedExpr #6793

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 1 commit into from
Jul 4, 2019
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
4 changes: 3 additions & 1 deletion compiler/src/dotty/tools/dotc/core/Definitions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -758,13 +758,16 @@ class Definitions {

@threadUnsafe lazy val QuotedExprType: TypeRef = ctx.requiredClassRef("scala.quoted.Expr")
def QuotedExprClass(implicit ctx: Context): ClassSymbol = QuotedExprType.symbol.asClass
def QuotedExprModule(implicit ctx: Context): Symbol = QuotedExprClass.companionModule

@threadUnsafe lazy val QuoteContextType: TypeRef = ctx.requiredClassRef("scala.quoted.QuoteContext")
def QuoteContextClass(implicit ctx: Context): ClassSymbol = QuoteContextType.symbol.asClass

@threadUnsafe lazy val QuoteContextModule: TermSymbol = ctx.requiredModule("scala.quoted.QuoteContext")
@threadUnsafe lazy val QuoteContext_macroContext: TermSymbol = QuoteContextModule.requiredMethod("macroContext")

@threadUnsafe lazy val LiftableModule: TermSymbol = ctx.requiredModule("scala.quoted.Liftable")

@threadUnsafe lazy val InternalQuotedModuleRef: TermRef = ctx.requiredModuleRef("scala.internal.Quoted")
def InternalQuotedModule: Symbol = InternalQuotedModuleRef.symbol
@threadUnsafe lazy val InternalQuoted_exprQuoteR: TermRef = InternalQuotedModule.requiredMethodRef("exprQuote")
Expand Down Expand Up @@ -797,7 +800,6 @@ class Definitions {
def QuotedMatchingBindingClass(implicit ctx: Context): ClassSymbol = QuotedMatchingBindingType.symbol.asClass

def Unpickler_unpickleExpr: TermSymbol = ctx.requiredMethod("scala.runtime.quoted.Unpickler.unpickleExpr")
def Unpickler_liftedExpr: TermSymbol = ctx.requiredMethod("scala.runtime.quoted.Unpickler.liftedExpr")
def Unpickler_unpickleType: TermSymbol = ctx.requiredMethod("scala.runtime.quoted.Unpickler.unpickleType")

@threadUnsafe lazy val TastyReflectionType: TypeRef = ctx.requiredClassRef("scala.tasty.Reflection")
Expand Down
28 changes: 0 additions & 28 deletions compiler/src/dotty/tools/dotc/core/quoted/PickledQuotes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,6 @@ object PickledQuotes {
}
}
forceAndCleanArtefacts.transform(unpickled)
case expr: LiftedExpr[T] =>
expr.value match {
case value: Class[_] => ref(defn.Predef_classOf).appliedToType(classToType(value))
case value => Literal(Constant(value))
}
case expr: TastyTreeExpr[Tree] @unchecked => healOwner(expr.tree)
case expr: FunctionAppliedTo[_] =>
functionAppliedTo(quotedExprToTree(expr.f), expr.args.map(arg => quotedExprToTree(arg)).toList)
Expand Down Expand Up @@ -174,29 +169,6 @@ object PickledQuotes {
seq(argVals.flatten, rec(fn))
}

private def classToType(clazz: Class[_])(implicit ctx: Context): Type = {
if (clazz.isPrimitive) {
if (clazz == classOf[Boolean]) defn.BooleanType
else if (clazz == classOf[Byte]) defn.ByteType
else if (clazz == classOf[Char]) defn.CharType
else if (clazz == classOf[Short]) defn.ShortType
else if (clazz == classOf[Int]) defn.IntType
else if (clazz == classOf[Long]) defn.LongType
else if (clazz == classOf[Float]) defn.FloatType
else if (clazz == classOf[Double]) defn.DoubleType
else defn.UnitType
} else if (clazz.isArray) {
defn.ArrayType.appliedTo(classToType(clazz.getComponentType))
} else if (clazz.isMemberClass) {
val name = clazz.getSimpleName.toTypeName
val enclosing = classToType(clazz.getEnclosingClass)
if (enclosing.member(name).exists) enclosing.select(name)
else {
enclosing.classSymbol.companionModule.termRef.select(name)
}
} else ctx.getClassIfDefined(clazz.getCanonicalName).typeRef
}

/** Make sure that the owner of this tree is `ctx.owner` */
private def healOwner(tree: Tree)(implicit ctx: Context): Tree = {
val getCurrentOwner = new TreeAccumulator[Option[Symbol]] {
Expand Down
9 changes: 1 addition & 8 deletions compiler/src/dotty/tools/dotc/quoted/QuoteCompiler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ class QuoteCompiler extends Compiler {
class QuotedFrontend extends Phase {
import tpd._


def phaseName: String = "quotedFrontend"

override def runOn(units: List[CompilationUnit])(implicit ctx: Context): List[CompilationUnit] = {
Expand All @@ -65,7 +64,7 @@ class QuoteCompiler extends Compiler {
cls.enter(ctx.newDefaultConstructor(cls), EmptyScope)
val meth = ctx.newSymbol(cls, nme.apply, Method, ExprType(defn.AnyType), coord = pos).entered

val quoted = PickledQuotes.quotedExprToTree(checkValidRunExpr(exprUnit.exprBuilder.apply(new QuoteContext(ReflectionImpl(ctx)))))(ctx.withOwner(meth))
val quoted = PickledQuotes.quotedExprToTree(exprUnit.exprBuilder.apply(new QuoteContext(ReflectionImpl(ctx))))(ctx.withOwner(meth))

getLiteral(quoted) match {
case Some(value) =>
Expand All @@ -82,12 +81,6 @@ class QuoteCompiler extends Compiler {
}
}

private def checkValidRunExpr(expr: Expr[_]): Expr[_] = expr match {
case expr: scala.internal.quoted.TastyTreeExpr[Tree] @unchecked =>
throw new Exception("Cannot call `Expr.run` on an `Expr` that comes from a macro argument.")
case _ => expr
}

/** Get the literal value if this tree only contains a literal tree */
@tailrec private def getLiteral(tree: Tree): Option[Any] = tree match {
case Literal(lit) => Some(lit.value)
Expand Down
30 changes: 30 additions & 0 deletions compiler/src/dotty/tools/dotc/tastyreflect/KernelImpl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1055,6 +1055,29 @@ class KernelImpl(val rootContext: core.Contexts.Context, val rootPosition: util.
case _ => Some(x)
}

def Type_apply(clazz: Class[_])(implicit ctx: Context): Type = {
if (clazz.isPrimitive) {
if (clazz == classOf[Boolean]) defn.BooleanType
else if (clazz == classOf[Byte]) defn.ByteType
else if (clazz == classOf[Char]) defn.CharType
else if (clazz == classOf[Short]) defn.ShortType
else if (clazz == classOf[Int]) defn.IntType
else if (clazz == classOf[Long]) defn.LongType
else if (clazz == classOf[Float]) defn.FloatType
else if (clazz == classOf[Double]) defn.DoubleType
else defn.UnitType
} else if (clazz.isArray) {
defn.ArrayType.appliedTo(Type_apply(clazz.getComponentType))
} else if (clazz.isMemberClass) {
val name = clazz.getSimpleName.toTypeName
val enclosing = Type_apply(clazz.getEnclosingClass)
if (enclosing.member(name).exists) enclosing.select(name)
else {
enclosing.classSymbol.companionModule.termRef.select(name)
}
} else ctx.getClassIfDefined(clazz.getCanonicalName).typeRef
}

def `Type_=:=`(self: Type)(that: Type)(implicit ctx: Context): Boolean = self =:= that

def `Type_<:<`(self: Type)(that: Type)(implicit ctx: Context): Boolean = self <:< that
Expand Down Expand Up @@ -1431,6 +1454,9 @@ class KernelImpl(val rootContext: core.Contexts.Context, val rootPosition: util.

def Constant_value(const: Constant): Any = const.value

def matchConstant(constant: Constant): Option[Unit | Null | Int | Boolean | Byte | Short | Int | Long | Float | Double | Char | String | Type] =
Some(constant.asInstanceOf[Unit | Null | Int | Boolean | Byte | Short | Int | Long | Float | Double | Char | String | Type])

def matchConstant_Unit(x: Constant): Boolean = x.tag == Constants.UnitTag
def matchConstant_Null(x: Constant): Boolean = x.tag == Constants.NullTag
def matchConstant_Boolean(x: Constant): Option[Boolean] =
Expand All @@ -1454,6 +1480,9 @@ class KernelImpl(val rootContext: core.Contexts.Context, val rootPosition: util.
def matchConstant_ClassTag(x: Constant): Option[Type] =
if (x.tag == Constants.ClazzTag) Some(x.typeValue) else None

def Constant_apply(x: Unit | Null | Int | Boolean | Byte | Short | Int | Long | Float | Double | Char | String | Type): Constant =
Constants.Constant(x)
Copy link
Contributor

Choose a reason for hiding this comment

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

Why 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 is the encoding for a classOf[T]


def Constant_Unit_apply(): Constant = Constants.Constant(())
def Constant_Null_apply(): Constant = Constants.Constant(null)
def Constant_Boolean_apply(x: Boolean): Constant = Constants.Constant(x)
Expand Down Expand Up @@ -1809,6 +1838,7 @@ class KernelImpl(val rootContext: core.Contexts.Context, val rootPosition: util.
def Definitions_ClassClass: Symbol = defn.ClassClass
def Definitions_ArrayClass: Symbol = defn.ArrayClass
def Definitions_PredefModule: Symbol = defn.ScalaPredefModule.asTerm
def Definitions_Predef_classOf: Symbol = defn.Predef_classOf.asTerm

def Definitions_JavaLangPackage: Symbol = defn.JavaLangPackageVal

Expand Down
28 changes: 26 additions & 2 deletions compiler/src/dotty/tools/dotc/transform/ReifyQuotes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import typer.Implicits.SearchFailureType

import scala.collection.mutable
import dotty.tools.dotc.core.Annotations.Annotation
import dotty.tools.dotc.core.Names._
import dotty.tools.dotc.core.StdNames._
import dotty.tools.dotc.core.quoted._
import dotty.tools.dotc.transform.TreeMapWithStages._
Expand Down Expand Up @@ -71,6 +72,8 @@ class ReifyQuotes extends MacroTransform {

override def phaseName: String = ReifyQuotes.name

override def allowsImplicitSearch: Boolean = true

override def checkPostCondition(tree: Tree)(implicit ctx: Context): Unit = {
tree match {
case tree: RefTree if !ctx.inInlineMethod =>
Expand Down Expand Up @@ -199,8 +202,29 @@ class ReifyQuotes extends MacroTransform {
}

private def pickledQuote(body: Tree, splices: List[Tree], originalTp: Type, isType: Boolean)(implicit ctx: Context) = {
def pickleAsValue[T](value: T) =
ref(defn.Unpickler_liftedExpr).appliedToType(originalTp.widen).appliedTo(Literal(Constant(value)))

def liftedValue[T](value: T, name: TermName, qctx: Tree) =
ref(defn.LiftableModule).select(name).select("toExpr".toTermName).appliedTo(Literal(Constant(value))).appliedTo(qctx)

def pickleAsValue[T](value: T) = {
val qctx = ctx.typer.inferImplicitArg(defn.QuoteContextType, body.span)
if (qctx.tpe.isInstanceOf[SearchFailureType])
ctx.error(ctx.typer.missingArgMsg(qctx, defn.QuoteContextType, ""), ctx.source.atSpan(body.span))
value match {
case null => ref(defn.QuotedExprModule).select("nullExpr".toTermName).appliedTo(qctx)
case _: Unit => ref(defn.QuotedExprModule).select("unitExpr".toTermName).appliedTo(qctx)
case _: Boolean => liftedValue(value, "Liftable_Boolean_delegate".toTermName, qctx)
case _: Byte => liftedValue(value, "Liftable_Byte_delegate".toTermName, qctx)
case _: Short => liftedValue(value, "Liftable_Short_delegate".toTermName, qctx)
case _: Int => liftedValue(value, "Liftable_Int_delegate".toTermName, qctx)
case _: Long => liftedValue(value, "Liftable_Long_delegate".toTermName, qctx)
case _: Float => liftedValue(value, "Liftable_Float_delegate".toTermName, qctx)
case _: Double => liftedValue(value, "Liftable_Double_delegate".toTermName, qctx)
case _: Char => liftedValue(value, "Liftable_Char_delegate".toTermName, qctx)
case _: String => liftedValue(value, "Liftable_String_delegate".toTermName, qctx)
}
}

Copy link
Contributor

Choose a reason for hiding this comment

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

We still need special compiler support for lifting primitives? Or it's for backward-compatibility.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We do not need it anymore.

def pickleAsTasty() = {
val meth =
if (isType) ref(defn.Unpickler_unpickleType).appliedToType(originalTp)
Expand Down
21 changes: 13 additions & 8 deletions library/src-3.x/scala/quoted/Expr.scala
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,19 @@ package quoted {
tg.untupled(args => new FunctionAppliedTo[R](f, args.toArray.map(_.asInstanceOf[Expr[_]])))
}

/** Returns A expression containing a block with the given statements and ending with the expresion
/** Returns a null expresssion equivalent to `'{null}` */
def nullExpr given (qctx: QuoteContext): Expr[Null] = {
import qctx.tasty._
Literal(Constant(null)).seal.asInstanceOf[Expr[Null]]
}

/** Returns a unit expresssion equivalent to `'{}` or `'{()}` */
def unitExpr given (qctx: QuoteContext): Expr[Unit] = {
import qctx.tasty._
Literal(Constant(())).seal.asInstanceOf[Expr[Unit]]
}

/** Returns an expression containing a block with the given statements and ending with the expresion
* Given list of statements `s1 :: s2 :: ... :: Nil` and an expression `e` the resulting expression
* will be equivalent to `'{ $s1; $s2; ...; $e }`.
*/
Expand All @@ -67,13 +79,6 @@ package internal {
override def toString: String = s"Expr(<pickled tasty>)"
}

/** An Expr backed by a lifted value.
* Values can only be of type Boolean, Byte, Short, Char, Int, Long, Float, Double, Unit, String or Null.
*/
final class LiftedExpr[+T](val value: T) extends Expr[T] {
override def toString: String = s"Expr($value)"
}

/** An Expr backed by a tree. Only the current compiler trees are allowed.
*
* These expressions are used for arguments of macros. They contain and actual tree
Expand Down
25 changes: 19 additions & 6 deletions library/src-3.x/scala/quoted/Liftable.scala
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package scala.quoted

import scala.runtime.quoted.Unpickler.liftedExpr

/** A typeclass for types that can be turned to `quoted.Expr[T]`
* without going through an explicit `'{...}` operation.
*/
abstract class Liftable[T] {
trait Liftable[T] {

/** Lift a value into an expression containing the construction of that value */
def toExpr(x: T) given QuoteContext: Expr[T]

}

/** Some liftable base types. To be completed with at least all types
Expand All @@ -17,17 +18,29 @@ abstract class Liftable[T] {
object Liftable {

implicit val Liftable_Boolean_delegate: Liftable[Boolean] = new PrimitiveLiftable
implicit val Liftable_Byte_delegate: Liftable[Byte] = new PrimitiveLiftable
implicit val Liftable_Short_delegate: Liftable[Short] = new PrimitiveLiftable
implicit val Liftable_Int_delegate: Liftable[Int] = new PrimitiveLiftable
implicit val Liftable_Long_delegate: Liftable[Long] = new PrimitiveLiftable
implicit val Liftable_Float_delegate: Liftable[Float] = new PrimitiveLiftable
implicit val Liftable_Double_delegate: Liftable[Double] = new PrimitiveLiftable
implicit val Liftable_Char_delegate: Liftable[Char] = new PrimitiveLiftable
implicit val Liftable_String_delegate: Liftable[String] = new PrimitiveLiftable
implicit def ClassIsLiftable[T]: Liftable[Class[T]] = new PrimitiveLiftable

private class PrimitiveLiftable[T] extends Liftable[T] {
override def toExpr(x: T) given QuoteContext: Expr[T] = liftedExpr(x)
private class PrimitiveLiftable[T <: Unit | Null | Int | Boolean | Byte | Short | Int | Long | Float | Double | Char | String] extends Liftable[T] {
/** Lift a primitive value `n` into `'{ n }` */
def toExpr(x: T) given (qctx: QuoteContext): Expr[T] = {
import qctx.tasty._
Literal(Constant(x)).seal.asInstanceOf[Expr[T]]
}
}

implicit def ClassIsLiftable[T]: Liftable[Class[T]] = new Liftable[Class[T]] {
/** Lift a `Class[T]` into `'{ classOf[T] }` */
def toExpr(x: Class[T]) given (qctx: QuoteContext): Expr[Class[T]] = {
import qctx.tasty._
Ref(definitions.Predef_classOf).appliedToType(Type(x)).seal.asInstanceOf[Expr[Class[T]]]
}
}

}
11 changes: 11 additions & 0 deletions library/src-non-bootstrapped/scala/quoted/package.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package scala

package object quoted {

def run[T](expr: given QuoteContext => Expr[T]) given (toolbox: Toolbox): T =
throw new Exception("Non bootsrapped library")

def withQuoteContext[T](thunk: given QuoteContext => T) given (toolbox: Toolbox): T =
throw new Exception("Non bootsrapped library")

}
7 changes: 1 addition & 6 deletions library/src/scala/runtime/quoted/Unpickler.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package scala.runtime.quoted

import scala.internal.quoted.{LiftedExpr, TastyExpr, TastyType}
import scala.internal.quoted.{TastyExpr, TastyType}
import scala.quoted.{Expr, Type}

/** Provides methods to unpickle `Expr` and `Type` trees. */
Expand All @@ -16,11 +16,6 @@ object Unpickler {
*/
def unpickleExpr[T](repr: Pickled, args: Seq[Any]): Expr[T] = new TastyExpr[T](repr, args)

/** Lift the `value` to an `Expr` tree.
* Values can only be of type Boolean, Byte, Short, Char, Int, Long, Float, Double, Unit, String, Null or Class.
*/
def liftedExpr[T](value: T): Expr[T] = new LiftedExpr[T](value)

/** Unpickle `repr` which represents a pickled `Type` tree,
* replacing splice nodes with `args`
*/
Expand Down
8 changes: 8 additions & 0 deletions library/src/scala/tasty/reflect/ConstantOps.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ trait ConstantOps extends Core {
/** Module of Constant literals */
object Constant {

def apply(x: Unit | Null | Int | Boolean | Byte | Short | Int | Long | Float | Double | Char | String | Type): Constant =
kernel.Constant_apply(x)

def unapply(constant: Constant): Option[Unit | Null | Int | Boolean | Byte | Short | Int | Long | Float | Double | Char | String | Type] =
kernel.matchConstant(constant)

// TODO remove all extractors bellow and use only use the two above

/** Module of Null literals */
object Unit {
/** Unit `()` literal */
Expand Down
5 changes: 5 additions & 0 deletions library/src/scala/tasty/reflect/Kernel.scala
Original file line number Diff line number Diff line change
Expand Up @@ -829,6 +829,8 @@ trait Kernel {

def matchType(x: TypeOrBounds)(implicit ctx: Context): Option[Type]

def Type_apply(clazz: Class[_])(implicit ctx: Context): Type

def `Type_=:=`(self: Type)(that: Type)(implicit ctx: Context): Boolean
def `Type_<:<`(self: Type)(that: Type)(implicit ctx: Context): Boolean

Expand Down Expand Up @@ -1176,6 +1178,7 @@ trait Kernel {

def Constant_value(const: Constant): Any

def matchConstant(constant: Constant): Option[Unit | Null | Int | Boolean | Byte | Short | Int | Long | Float | Double | Char | String | Type]
def matchConstant_Unit(constant: Constant): Boolean
def matchConstant_Null(constant: Constant): Boolean
def matchConstant_Boolean(constant: Constant): Option[Boolean]
Expand All @@ -1189,6 +1192,7 @@ trait Kernel {
def matchConstant_String(constant: Constant): Option[String]
def matchConstant_ClassTag(constant: Constant): Option[Type]

def Constant_apply(x: Unit | Null | Int | Boolean | Byte | Short | Int | Long | Float | Double | Char | String | Type): Constant
def Constant_Unit_apply(): Constant
def Constant_Null_apply(): Constant
def Constant_Boolean_apply(x: Boolean): Constant
Expand Down Expand Up @@ -1470,6 +1474,7 @@ trait Kernel {
def Definitions_ClassClass: Symbol
def Definitions_ArrayClass: Symbol
def Definitions_PredefModule: Symbol
def Definitions_Predef_classOf: Symbol

def Definitions_JavaLangPackage: Symbol

Expand Down
3 changes: 3 additions & 0 deletions library/src/scala/tasty/reflect/StandardDefinitions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ trait StandardDefinitions extends Core {
/** The module symbol of module `scala.Predef`. */
def PredefModule: Symbol = kernel.Definitions_PredefModule

/** The method symbol of method `scala.Predef.classOf`. */
def Predef_classOf: Symbol = kernel.Definitions_Predef_classOf

/** The module symbol of package `java.lang`. */
def JavaLangPackage: Symbol = kernel.Definitions_JavaLangPackage

Expand Down
2 changes: 2 additions & 0 deletions library/src/scala/tasty/reflect/TypeOrBoundsOps.scala
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ trait TypeOrBoundsOps extends Core {

object Type {

def apply(clazz: Class[_])(implicit ctx: Context): Type = kernel.Type_apply(clazz)

object IsConstantType {
Copy link
Contributor

Choose a reason for hiding this comment

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

Why the two neg-with-compiler tests are disabled?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Those tests where testing scope extrusion detection through an euristic which happens to no work anymore. I will work on more reliable mechanism for scope extrusion detection later.

/** Matches any ConstantType and returns it */
def unapply(tpe: TypeOrBounds)(implicit ctx: Context): Option[ConstantType] =
Expand Down
Loading