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
'{}

0 commit comments

Comments
 (0)