Skip to content

Commit cf30aa8

Browse files
committed
WIP Remove scala.internal.quoted.LiftedExpr
Replace by reflection using the QuoteContext
1 parent e531fe9 commit cf30aa8

File tree

14 files changed

+69
-62
lines changed

14 files changed

+69
-62
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 & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,6 @@ object PickledQuotes {
4343
}
4444
}
4545
forceAndCleanArtefacts.transform(unpickled)
46-
case expr: LiftedExpr[T] =>
47-
expr.value match {
48-
case value: Class[_] => ref(defn.Predef_classOf).appliedToType(classToType(value))
49-
case value => Literal(Constant(value))
50-
}
5146
case expr: TastyTreeExpr[Tree] @unchecked => healOwner(expr.tree)
5247
case expr: FunctionAppliedTo[_] =>
5348
functionAppliedTo(quotedExprToTree(expr.f), expr.args.map(arg => quotedExprToTree(arg)).toList)

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

Lines changed: 2 additions & 3 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] = {
@@ -83,8 +82,8 @@ class QuoteCompiler extends Compiler {
8382
}
8483

8584
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.")
85+
// case expr: scala.internal.quoted.TastyTreeExpr[Tree] @unchecked =>
86+
// throw new Exception("Cannot call `Expr.run` on an `Expr` that comes from a macro argument.")
8887
case _ => expr
8988
}
9089

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1445,6 +1445,8 @@ class KernelImpl(val rootContext: core.Contexts.Context, val rootPosition: util.
14451445
def matchConstant_ClassTag(x: Constant): Option[Type] =
14461446
if (x.tag == Constants.ClazzTag) Some(x.typeValue) else None
14471447

1448+
def Constant_apply(x: Unit | Null | Int | Boolean | Byte | Short | Int | Long | Float | Double | Char | String | Class[_]): Constant =
1449+
Constants.Constant(x)
14481450
def Constant_Unit_apply(): Constant = Constants.Constant(())
14491451
def Constant_Null_apply(): Constant = Constants.Constant(null)
14501452
def Constant_Boolean_apply(x: Boolean): Constant = Constants.Constant(x)

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: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
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
*/
@@ -24,10 +22,16 @@ object Liftable {
2422
implicit val Liftable_Double_delegate: Liftable[Double] = new PrimitiveLiftable
2523
implicit val Liftable_Char_delegate: Liftable[Char] = new PrimitiveLiftable
2624
implicit val Liftable_String_delegate: Liftable[String] = new PrimitiveLiftable
27-
implicit def ClassIsLiftable[T]: Liftable[Class[T]] = new PrimitiveLiftable
25+
implicit def ClassIsLiftable[T]: Liftable[Class[T]] = new PrimitiveLiftable[Class[T]]
2826

29-
private class PrimitiveLiftable[T] extends Liftable[T] {
30-
override def toExpr(x: T) given QuoteContext: Expr[T] = liftedExpr(x)
27+
private class PrimitiveLiftable[T <: Unit | Null | Int | Boolean | Byte | Short | Int | Long | Float | Double | Char | String | Class[_]] extends Liftable[T] {
28+
override def toExpr(x: T) given (qctx: QuoteContext): Expr[T] = {
29+
import qctx.tasty._
30+
x match {
31+
// case value: Class[_] => ref(defn.Predef_classOf).appliedToType(classToType(value))
32+
case x => Literal(Constant(x)).seal.asInstanceOf[Expr[T]]
33+
}
34+
}
3135
}
3236

3337
}

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

Lines changed: 2 additions & 2 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. */
@@ -19,7 +19,7 @@ object Unpickler {
1919
/** Lift the `value` to an `Expr` tree.
2020
* Values can only be of type Boolean, Byte, Short, Char, Int, Long, Float, Double, Unit, String, Null or Class.
2121
*/
22-
def liftedExpr[T](value: T): Expr[T] = new LiftedExpr[T](value)
22+
def liftedExpr[T](value: T): Expr[T] = ??? // deprecated
2323

2424
/** Unpickle `repr` which represents a pickled `Type` tree,
2525
* replacing splice nodes with `args`

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ 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 | Class[_]): Constant =
14+
kernel.Constant_apply(x)
15+
1316
/** Module of Null literals */
1417
object Unit {
1518
/** Unit `()` literal */

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1185,6 +1185,7 @@ trait Kernel {
11851185
def matchConstant_String(constant: Constant): Option[String]
11861186
def matchConstant_ClassTag(constant: Constant): Option[Type]
11871187

1188+
def Constant_apply(x: Unit | Null | Int | Boolean | Byte | Short | Int | Long | Float | Double | Char | String | Class[_]): Constant
11881189
def Constant_Unit_apply(): Constant
11891190
def Constant_Null_apply(): Constant
11901191
def Constant_Boolean_apply(x: Boolean): Constant
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)