Skip to content

Commit 8925d6f

Browse files
committed
Remove scala.internal.quoted.LiftedExpr
1 parent ea57e36 commit 8925d6f

File tree

20 files changed

+129
-95
lines changed

20 files changed

+129
-95
lines changed

compiler/src/dotty/tools/dotc/core/Definitions.scala

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -797,7 +797,6 @@ class Definitions {
797797
def QuotedMatchingBindingClass(implicit ctx: Context): ClassSymbol = QuotedMatchingBindingType.symbol.asClass
798798

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

803802
@threadUnsafe lazy val TastyReflectionType: TypeRef = ctx.requiredClassRef("scala.tasty.Reflection")

compiler/src/dotty/tools/dotc/core/quoted/PickledQuotes.scala

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,6 @@ object PickledQuotes {
4545
}
4646
}
4747
forceAndCleanArtefacts.transform(unpickled)
48-
case expr: LiftedExpr[T] =>
49-
expr.value match {
50-
case value: Class[_] => ref(defn.Predef_classOf).appliedToType(classToType(value))
51-
case value => Literal(Constant(value))
52-
}
5348
case expr: TastyTreeExpr[Tree] @unchecked => healOwner(expr.tree)
5449
case expr: FunctionAppliedTo[_] =>
5550
functionAppliedTo(quotedExprToTree(expr.f), expr.args.map(arg => quotedExprToTree(arg)).toList)
@@ -174,29 +169,6 @@ object PickledQuotes {
174169
seq(argVals.flatten, rec(fn))
175170
}
176171

177-
private def classToType(clazz: Class[_])(implicit ctx: Context): Type = {
178-
if (clazz.isPrimitive) {
179-
if (clazz == classOf[Boolean]) defn.BooleanType
180-
else if (clazz == classOf[Byte]) defn.ByteType
181-
else if (clazz == classOf[Char]) defn.CharType
182-
else if (clazz == classOf[Short]) defn.ShortType
183-
else if (clazz == classOf[Int]) defn.IntType
184-
else if (clazz == classOf[Long]) defn.LongType
185-
else if (clazz == classOf[Float]) defn.FloatType
186-
else if (clazz == classOf[Double]) defn.DoubleType
187-
else defn.UnitType
188-
} else if (clazz.isArray) {
189-
defn.ArrayType.appliedTo(classToType(clazz.getComponentType))
190-
} else if (clazz.isMemberClass) {
191-
val name = clazz.getSimpleName.toTypeName
192-
val enclosing = classToType(clazz.getEnclosingClass)
193-
if (enclosing.member(name).exists) enclosing.select(name)
194-
else {
195-
enclosing.classSymbol.companionModule.termRef.select(name)
196-
}
197-
} else ctx.getClassIfDefined(clazz.getCanonicalName).typeRef
198-
}
199-
200172
/** Make sure that the owner of this tree is `ctx.owner` */
201173
private def healOwner(tree: Tree)(implicit ctx: Context): Tree = {
202174
val getCurrentOwner = new TreeAccumulator[Option[Symbol]] {

compiler/src/dotty/tools/dotc/quoted/QuoteCompiler.scala

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ class QuoteCompiler extends Compiler {
4949
class QuotedFrontend extends Phase {
5050
import tpd._
5151

52-
5352
def phaseName: String = "quotedFrontend"
5453

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

68-
val quoted = PickledQuotes.quotedExprToTree(checkValidRunExpr(exprUnit.exprBuilder.apply(new QuoteContext(ReflectionImpl(ctx)))))(ctx.withOwner(meth))
67+
val quoted = PickledQuotes.quotedExprToTree(exprUnit.exprBuilder.apply(new QuoteContext(ReflectionImpl(ctx))))(ctx.withOwner(meth))
6968

7069
getLiteral(quoted) match {
7170
case Some(value) =>
@@ -82,12 +81,6 @@ class QuoteCompiler extends Compiler {
8281
}
8382
}
8483

85-
private def checkValidRunExpr(expr: Expr[_]): Expr[_] = expr match {
86-
case expr: scala.internal.quoted.TastyTreeExpr[Tree] @unchecked =>
87-
throw new Exception("Cannot call `Expr.run` on an `Expr` that comes from a macro argument.")
88-
case _ => expr
89-
}
90-
9184
/** Get the literal value if this tree only contains a literal tree */
9285
@tailrec private def getLiteral(tree: Tree): Option[Any] = tree match {
9386
case Literal(lit) => Some(lit.value)

compiler/src/dotty/tools/dotc/tastyreflect/KernelImpl.scala

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1055,6 +1055,29 @@ class KernelImpl(val rootContext: core.Contexts.Context, val rootPosition: util.
10551055
case _ => Some(x)
10561056
}
10571057

1058+
def Type_apply(clazz: Class[_])(implicit ctx: Context): Type = {
1059+
if (clazz.isPrimitive) {
1060+
if (clazz == classOf[Boolean]) defn.BooleanType
1061+
else if (clazz == classOf[Byte]) defn.ByteType
1062+
else if (clazz == classOf[Char]) defn.CharType
1063+
else if (clazz == classOf[Short]) defn.ShortType
1064+
else if (clazz == classOf[Int]) defn.IntType
1065+
else if (clazz == classOf[Long]) defn.LongType
1066+
else if (clazz == classOf[Float]) defn.FloatType
1067+
else if (clazz == classOf[Double]) defn.DoubleType
1068+
else defn.UnitType
1069+
} else if (clazz.isArray) {
1070+
defn.ArrayType.appliedTo(Type_apply(clazz.getComponentType))
1071+
} else if (clazz.isMemberClass) {
1072+
val name = clazz.getSimpleName.toTypeName
1073+
val enclosing = Type_apply(clazz.getEnclosingClass)
1074+
if (enclosing.member(name).exists) enclosing.select(name)
1075+
else {
1076+
enclosing.classSymbol.companionModule.termRef.select(name)
1077+
}
1078+
} else ctx.getClassIfDefined(clazz.getCanonicalName).typeRef
1079+
}
1080+
10581081
def `Type_=:=`(self: Type)(that: Type)(implicit ctx: Context): Boolean = self =:= that
10591082

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

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

1457+
def matchConstant(constant: Constant): Option[Unit | Null | Int | Boolean | Byte | Short | Int | Long | Float | Double | Char | String | Type] =
1458+
Some(constant.asInstanceOf[Unit | Null | Int | Boolean | Byte | Short | Int | Long | Float | Double | Char | String | Type])
1459+
14341460
def matchConstant_Unit(x: Constant): Boolean = x.tag == Constants.UnitTag
14351461
def matchConstant_Null(x: Constant): Boolean = x.tag == Constants.NullTag
14361462
def matchConstant_Boolean(x: Constant): Option[Boolean] =
@@ -1454,6 +1480,9 @@ class KernelImpl(val rootContext: core.Contexts.Context, val rootPosition: util.
14541480
def matchConstant_ClassTag(x: Constant): Option[Type] =
14551481
if (x.tag == Constants.ClazzTag) Some(x.typeValue) else None
14561482

1483+
def Constant_apply(x: Unit | Null | Int | Boolean | Byte | Short | Int | Long | Float | Double | Char | String | Type): Constant =
1484+
Constants.Constant(x)
1485+
14571486
def Constant_Unit_apply(): Constant = Constants.Constant(())
14581487
def Constant_Null_apply(): Constant = Constants.Constant(null)
14591488
def Constant_Boolean_apply(x: Boolean): Constant = Constants.Constant(x)
@@ -1809,6 +1838,7 @@ class KernelImpl(val rootContext: core.Contexts.Context, val rootPosition: util.
18091838
def Definitions_ClassClass: Symbol = defn.ClassClass
18101839
def Definitions_ArrayClass: Symbol = defn.ArrayClass
18111840
def Definitions_PredefModule: Symbol = defn.ScalaPredefModule.asTerm
1841+
def Definitions_Predef_classOf: Symbol = defn.Predef_classOf.asTerm
18121842

18131843
def Definitions_JavaLangPackage: Symbol = defn.JavaLangPackageVal
18141844

compiler/src/dotty/tools/dotc/transform/ReifyQuotes.scala

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -199,8 +199,9 @@ class ReifyQuotes extends MacroTransform {
199199
}
200200

201201
private def pickledQuote(body: Tree, splices: List[Tree], originalTp: Type, isType: Boolean)(implicit ctx: Context) = {
202-
def pickleAsValue[T](value: T) =
203-
ref(defn.Unpickler_liftedExpr).appliedToType(originalTp.widen).appliedTo(Literal(Constant(value)))
202+
// FIXME use Liftable.XYZ
203+
// def pickleAsValue[T](value: T) =
204+
// ref(defn.Unpickler_liftedExpr).appliedToType(originalTp.widen).appliedTo(Literal(Constant(value)))
204205
def pickleAsTasty() = {
205206
val meth =
206207
if (isType) ref(defn.Unpickler_unpickleType).appliedToType(originalTp)
@@ -216,7 +217,7 @@ class ReifyQuotes extends MacroTransform {
216217
else pickleAsTasty()
217218
}
218219
else toValue(body) match {
219-
case Some(value) => pickleAsValue(value)
220+
// case Some(value) => pickleAsValue(value)
220221
case _ => pickleAsTasty()
221222
}
222223
}

library/src-3.x/scala/quoted/Expr.scala

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,6 @@ package internal {
6767
override def toString: String = s"Expr(<pickled tasty>)"
6868
}
6969

70-
/** An Expr backed by a lifted value.
71-
* Values can only be of type Boolean, Byte, Short, Char, Int, Long, Float, Double, Unit, String or Null.
72-
*/
73-
final class LiftedExpr[+T](val value: T) extends Expr[T] {
74-
override def toString: String = s"Expr($value)"
75-
}
76-
7770
/** An Expr backed by a tree. Only the current compiler trees are allowed.
7871
*
7972
* These expressions are used for arguments of macros. They contain and actual tree

library/src-3.x/scala/quoted/Liftable.scala

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
package scala.quoted
22

3-
import scala.runtime.quoted.Unpickler.liftedExpr
4-
53
/** A typeclass for types that can be turned to `quoted.Expr[T]`
64
* without going through an explicit `'{...}` operation.
75
*/
8-
abstract class Liftable[T] {
6+
trait Liftable[T] {
7+
8+
/** Lift a value into an expression containing the construction of that value */
99
def toExpr(x: T) given QuoteContext: Expr[T]
10+
1011
}
1112

1213
/** Some liftable base types. To be completed with at least all types
@@ -24,10 +25,21 @@ object Liftable {
2425
implicit val Liftable_Double_delegate: Liftable[Double] = new PrimitiveLiftable
2526
implicit val Liftable_Char_delegate: Liftable[Char] = new PrimitiveLiftable
2627
implicit val Liftable_String_delegate: Liftable[String] = new PrimitiveLiftable
27-
implicit def ClassIsLiftable[T]: Liftable[Class[T]] = new PrimitiveLiftable
2828

29-
private class PrimitiveLiftable[T] extends Liftable[T] {
30-
override def toExpr(x: T) given QuoteContext: Expr[T] = liftedExpr(x)
29+
private class PrimitiveLiftable[T <: Unit | Null | Int | Boolean | Byte | Short | Int | Long | Float | Double | Char | String] extends Liftable[T] {
30+
/** Lift a primitive value `n` into `'{ n }` */
31+
def toExpr(x: T) given (qctx: QuoteContext): Expr[T] = {
32+
import qctx.tasty._
33+
Literal(Constant(x)).seal.asInstanceOf[Expr[T]]
34+
}
35+
}
36+
37+
implicit def ClassIsLiftable[T]: Liftable[Class[T]] = new Liftable[Class[T]] {
38+
/** Lift a `Class[T]` into `'{ classOf[T] }` */
39+
def toExpr(x: Class[T]) given (qctx: QuoteContext): Expr[Class[T]] = {
40+
import qctx.tasty._
41+
Ref(definitions.Predef_classOf).appliedToType(Type(x)).seal.asInstanceOf[Expr[Class[T]]]
42+
}
3143
}
3244

3345
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package scala
2+
3+
package object quoted {
4+
5+
def run[T](expr: given QuoteContext => Expr[T]) given (toolbox: Toolbox): T =
6+
throw new Exception("Non bootsrapped library")
7+
8+
def withQuoteContext[T](thunk: given QuoteContext => T) given (toolbox: Toolbox): T =
9+
throw new Exception("Non bootsrapped library")
10+
11+
}

library/src/scala/runtime/quoted/Unpickler.scala

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package scala.runtime.quoted
22

3-
import scala.internal.quoted.{LiftedExpr, TastyExpr, TastyType}
3+
import scala.internal.quoted.{TastyExpr, TastyType}
44
import scala.quoted.{Expr, Type}
55

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

19-
/** Lift the `value` to an `Expr` tree.
20-
* Values can only be of type Boolean, Byte, Short, Char, Int, Long, Float, Double, Unit, String, Null or Class.
21-
*/
22-
def liftedExpr[T](value: T): Expr[T] = new LiftedExpr[T](value)
23-
2419
/** Unpickle `repr` which represents a pickled `Type` tree,
2520
* replacing splice nodes with `args`
2621
*/

library/src/scala/tasty/reflect/ConstantOps.scala

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@ trait ConstantOps extends Core {
1010
/** Module of Constant literals */
1111
object Constant {
1212

13+
def apply(x: Unit | Null | Int | Boolean | Byte | Short | Int | Long | Float | Double | Char | String | Type): Constant =
14+
kernel.Constant_apply(x)
15+
16+
def unapply(constant: Constant): Option[Unit | Null | Int | Boolean | Byte | Short | Int | Long | Float | Double | Char | String | Type] =
17+
kernel.matchConstant(constant)
18+
19+
// TODO remove all extractors bellow and use only use the two above
20+
1321
/** Module of Null literals */
1422
object Unit {
1523
/** Unit `()` literal */

library/src/scala/tasty/reflect/Kernel.scala

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -829,6 +829,8 @@ trait Kernel {
829829

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

832+
def Type_apply(clazz: Class[_])(implicit ctx: Context): Type
833+
832834
def `Type_=:=`(self: Type)(that: Type)(implicit ctx: Context): Boolean
833835
def `Type_<:<`(self: Type)(that: Type)(implicit ctx: Context): Boolean
834836

@@ -1176,6 +1178,7 @@ trait Kernel {
11761178

11771179
def Constant_value(const: Constant): Any
11781180

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

1195+
def Constant_apply(x: Unit | Null | Int | Boolean | Byte | Short | Int | Long | Float | Double | Char | String | Type): Constant
11921196
def Constant_Unit_apply(): Constant
11931197
def Constant_Null_apply(): Constant
11941198
def Constant_Boolean_apply(x: Boolean): Constant
@@ -1470,6 +1474,7 @@ trait Kernel {
14701474
def Definitions_ClassClass: Symbol
14711475
def Definitions_ArrayClass: Symbol
14721476
def Definitions_PredefModule: Symbol
1477+
def Definitions_Predef_classOf: Symbol
14731478

14741479
def Definitions_JavaLangPackage: Symbol
14751480

library/src/scala/tasty/reflect/StandardDefinitions.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ trait StandardDefinitions extends Core {
8585
/** The module symbol of module `scala.Predef`. */
8686
def PredefModule: Symbol = kernel.Definitions_PredefModule
8787

88+
/** The method symbol of method `scala.Predef.classOf`. */
89+
def Predef_classOf: Symbol = kernel.Definitions_Predef_classOf
90+
8891
/** The module symbol of package `java.lang`. */
8992
def JavaLangPackage: Symbol = kernel.Definitions_JavaLangPackage
9093

library/src/scala/tasty/reflect/TypeOrBoundsOps.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ trait TypeOrBoundsOps extends Core {
6464

6565
object Type {
6666

67+
def apply(clazz: Class[_])(implicit ctx: Context): Type = kernel.Type_apply(clazz)
68+
6769
object IsConstantType {
6870
/** Matches any ConstantType and returns it */
6971
def unapply(tpe: TypeOrBounds)(implicit ctx: Context): Option[ConstantType] =
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
Inlined(Ident(Test$),List(),Literal(Constant(true)))
2+
Inlined(Ident(Test$),List(),Literal(Constant(a)))
3+
Inlined(Ident(Test$),List(),Literal(Constant(
4+
)))
5+
Inlined(Ident(Test$),List(),Literal(Constant(")))
6+
Inlined(Ident(Test$),List(),Literal(Constant(')))
7+
Inlined(Ident(Test$),List(),Literal(Constant(\)))
8+
Inlined(Ident(Test$),List(),Literal(Constant(1)))
9+
Inlined(Ident(Test$),List(),Block(List(),Block(List(),Literal(Constant(2)))))
10+
Inlined(Ident(Test$),List(),Literal(Constant(3)))
11+
Inlined(Ident(Test$),List(),Literal(Constant(4.0)))
12+
Inlined(Ident(Test$),List(),Literal(Constant(5.0)))
13+
Inlined(Ident(Test$),List(),Literal(Constant(xyz)))
14+
Inlined(Ident(Test$),List(),Block(List(),Literal(Constant(()))))
15+
Inlined(Ident(Test$),List(),Literal(Constant(())))
16+
Inlined(Ident(Test$),List(),Block(List(),Literal(Constant(()))))
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import scala.quoted._
2+
3+
object Test {
4+
5+
implicit val toolbox: scala.quoted.Toolbox = scala.quoted.Toolbox.make(getClass.getClassLoader)
6+
7+
def main(args: Array[String]): Unit = withQuoteContext {
8+
val qctx = the[QuoteContext]
9+
def check[T](expr: Expr[T]): Unit = {
10+
import qctx.tasty._
11+
println(expr.unseal)
12+
}
13+
14+
check('{true})
15+
check('{ 'a' })
16+
check('{ '\n' })
17+
check('{ '"' })
18+
check('{ '\'' })
19+
check('{ '\\' })
20+
check('{1})
21+
check('{ { { 2 } } })
22+
check('{3L})
23+
check('{4.0f})
24+
check('{5.0d})
25+
check('{"xyz"})
26+
check('{})
27+
check('{()})
28+
check('{{()}})
29+
}
30+
}

tests/run/quote-compile-constants.check

Lines changed: 0 additions & 15 deletions
This file was deleted.

tests/run/quote-compile-constants.scala

Lines changed: 0 additions & 21 deletions
This file was deleted.

0 commit comments

Comments
 (0)