Skip to content

Commit b8c8a0b

Browse files
committed
Remove quoted.Const.unapply
This extractor was replaced with the more versatil extractor `quoted.Unlifted.unapply`. * Remove definition of `quoted.Const.unapply` * Replace uses of `Const` with `Unlifted` * Allow unlifting precise types for primitives * Remove `Unlifted[Unit]` **Migration** * Replace `Const(x)` with `Unlifted(x)` if the type is known * Replace `Const(x: Int)` with `'{ ${Unlifted(x)}: Int }` if type of expression must be checked at runtime * Consider using `expr.unlift` and them match on `Option`
1 parent 2efaeb0 commit b8c8a0b

File tree

32 files changed

+78
-130
lines changed

32 files changed

+78
-130
lines changed

library/src-bootstrapped/scala/quoted/Unliftable.scala

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,39 +14,46 @@ trait Unliftable[T] {
1414

1515
object Unliftable {
1616

17-
/** Default unliftable for Unit */
18-
given UnitUnliftable as Unliftable[Unit] = new PrimitiveUnliftable
19-
2017
/** Default unliftable for Boolean */
21-
given BooleanUnliftable as Unliftable[Boolean] = new PrimitiveUnliftable
18+
given BooleanUnliftable[T <: Boolean] as Unliftable[T] = new PrimitiveUnliftable
2219

2320
/** Default unliftable for Byte */
24-
given ByteUnliftable as Unliftable[Byte] = new PrimitiveUnliftable
21+
given ByteUnliftable[T <: Byte] as Unliftable[T] = new PrimitiveUnliftable
2522

2623
/** Default unliftable for Short */
27-
given ShortUnliftable as Unliftable[Short] = new PrimitiveUnliftable
24+
given ShortUnliftable[T <: Short] as Unliftable[T] = new PrimitiveUnliftable
2825

2926
/** Default unliftable for Int */
30-
given IntUnliftable as Unliftable[Int] = new PrimitiveUnliftable
27+
given IntUnliftable[T <: Int] as Unliftable[T] = new PrimitiveUnliftable
3128

3229
/** Default unliftable for Long */
33-
given LongUnliftable as Unliftable[Long] = new PrimitiveUnliftable
30+
given LongUnliftable[T <: Long] as Unliftable[T] = new PrimitiveUnliftable
3431

3532
/** Default unliftable for Float */
36-
given FloatUnliftable as Unliftable[Float] = new PrimitiveUnliftable
33+
given FloatUnliftable[T <: Float] as Unliftable[T] = new PrimitiveUnliftable
3734

3835
/** Default unliftable for Double */
39-
given DoubleUnliftable as Unliftable[Double] = new PrimitiveUnliftable
36+
given DoubleUnliftable[T <: Double] as Unliftable[T] = new PrimitiveUnliftable
4037

4138
/** Default unliftable for Char */
42-
given CharUnliftable as Unliftable[Char] = new PrimitiveUnliftable
39+
given CharUnliftable[T <: Char] as Unliftable[T] = new PrimitiveUnliftable
4340

4441
/** Default unliftable for String */
45-
given StringUnliftable as Unliftable[String] = new PrimitiveUnliftable
42+
given StringUnliftable[T <: String] as Unliftable[T] = new PrimitiveUnliftable
4643

47-
private class PrimitiveUnliftable[T <: Unit | Null | Int | Boolean | Byte | Short | Int | Long | Float | Double | Char | String] extends Unliftable[T] {
44+
private class PrimitiveUnliftable[T <: Int | Boolean | Byte | Short | Int | Long | Float | Double | Char | String] extends Unliftable[T] {
4845
/** Lift a quoted primitive value `'{ n }` into `n` */
49-
def fromExpr(x: Expr[T]) = Const.unapply(x)
46+
def fromExpr(expr: Expr[T]) = {
47+
import quotes.reflect._
48+
def rec(tree: Term): Option[T] = tree match {
49+
case Literal(c) => Some(c.value.asInstanceOf[T])
50+
case Block(Nil, e) => rec(e)
51+
case Typed(e, _) => rec(e)
52+
case Inlined(_, Nil, e) => rec(e)
53+
case _ => None
54+
}
55+
rec(Term.of(expr))
56+
}
5057
}
5158

5259
/** Default unliftable for Option */
@@ -79,8 +86,8 @@ object Unliftable {
7986
/** Default unliftable for StringContext */
8087
given StringContextUnliftable as Unliftable[StringContext] = new {
8188
def fromExpr(x: Expr[StringContext]) = x match {
82-
case '{ new StringContext(${Varargs(Consts(args))}: _*) } => Some(StringContext(args: _*))
83-
case '{ StringContext(${Varargs(Consts(args))}: _*) } => Some(StringContext(args: _*))
89+
case '{ new StringContext(${Varargs(Unlifted(args))}: _*) } => Some(StringContext(args: _*))
90+
case '{ StringContext(${Varargs(Unlifted(args))}: _*) } => Some(StringContext(args: _*))
8491
case _ => None
8592
}
8693
}

library/src/scala/quoted/Const.scala

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

library/src/scala/quoted/Consts.scala

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

tests/bench/string-interpolation-macro/Macro.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ object Macro {
99
var res: Expr[String] = null
1010
for _ <- 0 to 5_000 do
1111
(strCtxExpr, argsExpr) match {
12-
case ('{ StringContext(${Varargs(Consts(parts))}: _*) }, Varargs(Consts(args))) =>
12+
case ('{ StringContext(${Varargs(Unlifted(parts))}: _*) }, Varargs(Unlifted(args))) =>
1313
res = Expr(StringContext(parts: _*).s(args: _*))
1414
case _ => ???
1515
}

tests/neg-macros/BigFloat/BigFloatFromDigitsImpl_1.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import scala.quoted._
66
object BigFloatFromDigitsImpl:
77
def apply(digits: Expr[String])(using Quotes): Expr[BigFloat] =
88
digits match
9-
case Const(ds) =>
9+
case Unlifted(ds) =>
1010
try
1111
val BigFloat(m, e) = BigFloat(ds)
1212
'{BigFloat(${Expr(m)}, ${Expr(e)})}

tests/neg-macros/GenericNumLits/EvenFromDigitsImpl_1.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import Even._
55

66
object EvenFromDigitsImpl:
77
def apply(digits: Expr[String])(using Quotes): Expr[Even] = digits match {
8-
case Const(ds) =>
8+
case Unlifted(ds) =>
99
val ev =
1010
try evenFromDigits(ds)
1111
catch {

tests/neg-macros/i6432/Macro_1.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ object Macro {
99
import quotes.reflect._
1010
sc match {
1111
case '{ StringContext(${Varargs(parts)}: _*) } =>
12-
for (part @ Const(s) <- parts)
12+
for (part @ Unlifted(s) <- parts)
1313
report.error(s, Term.of(part).pos)
1414
}
1515
'{}

tests/neg-macros/i6432b/Macro_1.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ object Macro {
99
import quotes.reflect._
1010
sc match {
1111
case '{ StringContext(${Varargs(parts)}: _*) } =>
12-
for (part @ Const(s) <- parts)
12+
for (part @ Unlifted(s) <- parts)
1313
report.error(s, Term.of(part).pos)
1414
}
1515
'{}

tests/neg-macros/inline-macro-staged-interpreter/Macro_1.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ object E {
1010

1111
implicit def ev1[T: Type]: Unliftable[E[T]] = new Unliftable {
1212
def fromExpr(x: Expr[E[T]]) = x match {
13-
case '{ I(${Const(n)}) } => Some(I(n).asInstanceOf[E[T]])
13+
case '{ I(${Unlifted(n)}) } => Some(I(n).asInstanceOf[E[T]])
1414
case '{ Plus[T](${Value(x)}, ${Value(y)})(using $op) } if op.matches('{Plus2.IPlus}) => Some(Plus(x, y)(using Plus2.IPlus.asInstanceOf[Plus2[T]]).asInstanceOf[E[T]])
1515
case _ => None
1616
}

tests/neg-with-compiler/GenericNumLits/EvenFromDigitsImpl_1.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import scala.quoted._
44
import Even._
55

66
object EvenFromDigitsImpl:
7-
def apply(digits: Expr[String])(using Quotes): Expr[Even] = digits match {
8-
case Const(ds) =>
7+
def apply(digits: Expr[String])(using Quotes): Expr[Even] = digits.unlift match {
8+
case Some(ds) =>
99
val ev =
1010
try evenFromDigits(ds)
1111
catch {

tests/run-macros/BigFloat/BigFloatFromDigitsImpl_1.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import scala.quoted._
66
object BigFloatFromDigitsImpl:
77
def apply(digits: Expr[String])(using Quotes): Expr[BigFloat] =
88
digits match
9-
case Const(ds) =>
9+
case Unlifted(ds) =>
1010
try
1111
val BigFloat(m, e) = BigFloat(ds)
1212
'{BigFloat(${Expr(m)}, ${Expr(e)})}

tests/run-macros/enum-nat-macro/Macros_2.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,6 @@ import Nat._
2525
case 0 => acc
2626
case n => inner[Succ[N]](n - 1, '{Succ($acc)})
2727

28-
val Const(i) = int
28+
val Unlifted(i) = int
2929
require(i >= 0)
3030
inner[Zero.type](i, '{Zero})

tests/run-macros/expr-map-1/Macro_1.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ private def stringRewriter(e: Expr[Any])(using Quotes): Expr[Any] =
99
private object StringRewriter extends ExprMap {
1010

1111
def transform[T](e: Expr[T])(using Quotes, Type[T]): Expr[T] = e match
12-
case Const(s: String) =>
12+
case '{ ${Unlifted(s)}: String } =>
1313
Expr(s.reverse) match
1414
case '{ $x: T } => x
1515
case _ => e // e had a singlton String type

tests/run-macros/flops-rewrite-2/Macro_1.scala

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,22 @@ private def rewriteMacro[T: Type](x: Expr[T])(using Quotes): Expr[T] = {
1313
Transformation[Int] {
1414
case '{ plus($x, $y) } =>
1515
(x, y) match {
16-
case (Const(0), _) => y
17-
case (Const(a), Const(b)) => Expr(a + b)
18-
case (_, Const(_)) => '{ $y + $x }
16+
case (Unlifted(0), _) => y
17+
case (Unlifted(a), Unlifted(b)) => Expr(a + b)
18+
case (_, Unlifted(_)) => '{ $y + $x }
1919
case _ => '{ $x + $y }
2020
}
2121
case '{ times($x, $y) } =>
2222
(x, y) match {
23-
case (Const(0), _) => '{0}
24-
case (Const(1), _) => y
25-
case (Const(a), Const(b)) => Expr(a * b)
26-
case (_, Const(_)) => '{ $y * $x }
23+
case (Unlifted(0), _) => '{0}
24+
case (Unlifted(1), _) => y
25+
case (Unlifted(a), Unlifted(b)) => Expr(a * b)
26+
case (_, Unlifted(_)) => '{ $y * $x }
2727
case _ => '{ $x * $y }
2828
}
29-
case '{ power(${Const(x)}, ${Const(y)}) } =>
29+
case '{ power(${Unlifted(x)}, ${Unlifted(y)}) } =>
3030
Expr(power(x, y))
31-
case '{ power($x, ${Const(y)}) } =>
31+
case '{ power($x, ${Unlifted(y)}) } =>
3232
if y == 0 then '{1}
3333
else '{ times($x, power($x, ${Expr(y-1)})) }
3434
}),

tests/run-macros/flops-rewrite-3/Macro_1.scala

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,22 @@ private def rewriteMacro[T: Type](x: Expr[T])(using Quotes): Expr[T] = {
1212
Transformation.safe[Int] {
1313
case '{ plus($x, $y) } =>
1414
(x, y) match {
15-
case (Const(0), _) => y
16-
case (Const(a), Const(b)) => Expr(a + b)
17-
case (_, Const(_)) => '{ $y + $x }
15+
case (Unlifted(0), _) => y
16+
case (Unlifted(a), Unlifted(b)) => Expr(a + b)
17+
case (_, Unlifted(_)) => '{ $y + $x }
1818
case _ => '{ $x + $y }
1919
}
2020
case '{ times($x, $y) } =>
2121
(x, y) match {
22-
case (Const(0), _) => '{0}
23-
case (Const(1), _) => y
24-
case (Const(a), Const(b)) => Expr(a * b)
25-
case (_, Const(_)) => '{ $y * $x }
22+
case (Unlifted(0), _) => '{0}
23+
case (Unlifted(1), _) => y
24+
case (Unlifted(a), Unlifted(b)) => Expr(a * b)
25+
case (_, Unlifted(_)) => '{ $y * $x }
2626
case _ => '{ $x * $y }
2727
}
28-
case '{ power(${Const(x)}, ${Const(y)}) } =>
28+
case '{ power(${Unlifted(x)}, ${Unlifted(y)}) } =>
2929
Expr(power(x, y))
30-
case '{ power($x, ${Const(y)}) } =>
30+
case '{ power($x, ${Unlifted(y)}) } =>
3131
if y == 0 then '{1}
3232
else '{ times($x, power($x, ${Expr(y-1)})) }
3333
}

tests/run-macros/i8671/Macro_1.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ object FileName {
1414
def createFileName(fileName: Expr[String])(using Quotes): Expr[FileName] =
1515
import quotes.reflect.report
1616
fileName match {
17-
case e@Const(s) =>
17+
case e@Unlifted(s) =>
1818
fileNameFromString(s) match {
1919
case Right(fn) =>
2020
'{FileName.unsafe(${Expr(fn.name)})} // Or `Expr(fn)` if there is a `Liftable[FileName]`

tests/run-macros/inline-macro-staged-interpreter/Macro_1.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ object E {
1111

1212
implicit def ev1[T: Type]: Unliftable[E[T]] = new Unliftable { // TODO use type class derivation
1313
def fromExpr(x: Expr[E[T]]) = (x match {
14-
case '{ I(${Const(n)}) } => Some(I(n))
15-
case '{ D(${Const(n)}) } => Some(D(n))
14+
case '{ I(${Unlifted(n)}) } => Some(I(n))
15+
case '{ D(${Unlifted(n)}) } => Some(D(n))
1616
case '{ Plus[Int](${Value(x)}, ${Value(y)})(using $op) } => Some(Plus(x, y)(using Plus2.IPlus))
1717
case '{ Plus[Double](${Value(x)}, ${Value(y)})(using $op) } => Some(Plus(x, y)(using Plus2.DPlus))
1818
case '{ Times[Int](${Value(x)}, ${Value(y)})(using $op) } => Some(Times(x, y)(using Times2.ITimes))

tests/run-macros/quote-matcher-string-interpolator-2/quoted_1.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ object Macros {
99
private def impl(self: Expr[StringContext], args: Expr[Seq[String]])(using Quotes): Expr[String] = {
1010
(self, args) match {
1111
case ('{ StringContext(${Varargs(parts)}: _*) }, Varargs(args1)) =>
12-
val strParts = parts.map { case Const(str) => str.reverse }
13-
val strArgs = args1.map { case Const(str) => str }
12+
val strParts = parts.map { case Unlifted(str) => str.reverse }
13+
val strArgs = args1.map { case Unlifted(str) => str }
1414
Expr(StringContext(strParts: _*).s(strArgs: _*))
1515
case _ => ???
1616
}

tests/run-macros/quote-matcher-string-interpolator-3/quoted_1.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ object Macros {
88

99
private def impl(self: Expr[StringContext], args: Expr[Seq[String]])(using Quotes): Expr[String] = {
1010
self match {
11-
case '{ StringContext(${Varargs(Consts(parts))}: _*) } =>
11+
case '{ StringContext(${Varargs(Unlifted(parts))}: _*) } =>
1212
val upprerParts: List[String] = parts.toList.map(_.toUpperCase)
1313
val upprerPartsExpr: Expr[List[String]] = Expr.ofList(upprerParts.map(Expr(_)))
1414
'{ StringContext($upprerPartsExpr: _*).s($args: _*) }

tests/run-macros/quote-matcher-symantics-1/quoted_1.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ object Macros {
1010

1111
def lift(e: Expr[DSL]): Expr[T] = e match {
1212

13-
case '{ LitDSL(${ Const(c) }) } =>
13+
case '{ LitDSL(${ Unlifted(c) }) } =>
1414
'{ $sym.value(${Expr(c)}) }
1515

1616
case '{ ($x: DSL) + ($y: DSL) } =>

tests/run-macros/quote-matcher-symantics-2/quoted_1.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ object Macros {
2121

2222
def lift(e: Expr[DSL])(implicit env: Map[Int, Expr[T]]): Expr[T] = e match {
2323

24-
case '{ LitDSL(${Const(c)}) } => sym.value(c)
24+
case '{ LitDSL(${Unlifted(c)}) } => sym.value(c)
2525

2626
case '{ ($x: DSL) + ($y: DSL) } => sym.plus(lift(x), lift(y))
2727

@@ -35,7 +35,7 @@ object Macros {
3535
lift(close(body1)(nEnvVar))(env + (i -> lift(value)))
3636
}
3737

38-
case '{ envVar(${Const(i)}) } => env(i)
38+
case '{ envVar(${Unlifted(i)}) } => env(i)
3939

4040
case _ =>
4141
import quotes.reflect._

tests/run-macros/quote-matcher-symantics-3/quoted_1.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@ object Macros {
1818
object FromEnv {
1919
def unapply[T](e: Expr[Any])(using env: Env): Option[Expr[R[T]]] =
2020
e match
21-
case '{envVar[t](${Const(id)})} =>
21+
case '{envVar[t](${Unlifted(id)})} =>
2222
env.get(id).asInstanceOf[Option[Expr[R[T]]]] // We can only add binds that have the same type as the refs
2323
case _ =>
2424
None
2525
}
2626

2727
def lift[T: Type](e: Expr[T])(using env: Env): Expr[R[T]] = ((e: Expr[Any]) match {
28-
case Const(e: Int) => '{ $sym.int(${Expr(e)}).asInstanceOf[R[T]] }
29-
case Const(e: Boolean) => '{ $sym.bool(${Expr(e)}).asInstanceOf[R[T]] }
28+
case '{ ${Unlifted(e)}: Int } => '{ $sym.int(${Expr(e)}).asInstanceOf[R[T]] }
29+
case '{ ${Unlifted(e)}: Boolean } => '{ $sym.bool(${Expr(e)}).asInstanceOf[R[T]] }
3030

3131
case '{ ($x: Int) + ($y: Int) } =>
3232
'{ $sym.add(${lift(x)}, ${lift(y)}).asInstanceOf[R[T]] }

tests/run-macros/quoted-matching-docs-2/Macro_1.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@ private def sumExpr(args1: Seq[Expr[Int]])(using Quotes): Expr[Int] = {
2626
}
2727
val args2 = args1.flatMap(flatSumArgs)
2828
val staticSum: Int = args2.map {
29-
case Const(arg) => arg
29+
case Unlifted(arg) => arg
3030
case _ => 0
3131
}.sum
3232
val dynamicSum: Seq[Expr[Int]] = args2.filter {
33-
case Const(_) => false
33+
case Unlifted(_) => false
3434
case arg => true
3535
}
3636
dynamicSum.foldLeft(Expr(staticSum))((acc, arg) => '{ $acc + $arg })

0 commit comments

Comments
 (0)