Skip to content

Commit c307589

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 **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 67d9790 commit c307589

File tree

19 files changed

+37
-104
lines changed

19 files changed

+37
-104
lines changed

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

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,58 +28,58 @@ object Unliftable {
2828
* - Unlifts `'{false}` into `Some(false)`
2929
* - Otherwise unlifts to `None`
3030
*/
31-
given BooleanUnliftable as Unliftable[Boolean] = new PrimitiveUnliftable
31+
given BooleanUnliftable[T <: Boolean] as Unliftable[T] = new PrimitiveUnliftable
3232

3333
/** Default unliftable for Byte
3434
* - Unlifts `'{n}` into `Some(n)` for a literal `n` of type `Byte`
3535
* - Otherwise unlifts to `None`
3636
*/
37-
given ByteUnliftable as Unliftable[Byte] = new PrimitiveUnliftable
37+
given ByteUnliftable[T <: Byte] as Unliftable[T] = new PrimitiveUnliftable
3838

3939
/** Default unliftable for Short
4040
* - Unlifts `'{n}` into `Some(n)` for a literal `n` of type `Short`
4141
* - Otherwise unlifts to `None`
4242
*/
43-
given ShortUnliftable as Unliftable[Short] = new PrimitiveUnliftable
43+
given ShortUnliftable[T <: Short] as Unliftable[T] = new PrimitiveUnliftable
4444

4545
/** Default unliftable for Int
4646
* - Unlifts `'{n}` into `Some(n)` for a literal `n` of type `Int`
4747
* - Otherwise unlifts to `None`
4848
*/
49-
given IntUnliftable as Unliftable[Int] = new PrimitiveUnliftable
49+
given IntUnliftable[T <: Int] as Unliftable[T] = new PrimitiveUnliftable
5050

5151
/** Default unliftable for Long
5252
* - Unlifts `'{n}` into `Some(n)` for a literal `n` of type `Long`
5353
* - Otherwise unlifts to `None`
5454
*/
55-
given LongUnliftable as Unliftable[Long] = new PrimitiveUnliftable
55+
given LongUnliftable[T <: Long] as Unliftable[T] = new PrimitiveUnliftable
5656

5757
/** Default unliftable for Float
5858
* - Unlifts `'{n}` into `Some(n)` for a literal `n` of type `Float`
5959
* - Otherwise unlifts to `None`
6060
*/
61-
given FloatUnliftable as Unliftable[Float] = new PrimitiveUnliftable
61+
given FloatUnliftable[T <: Float] as Unliftable[T] = new PrimitiveUnliftable
6262

6363
/** Default unliftable for Double
6464
* - Unlifts `'{n}` into `Some(n)` for a literal `n` of type `Double`
6565
* - Otherwise unlifts to `None`
6666
*/
67-
given DoubleUnliftable as Unliftable[Double] = new PrimitiveUnliftable
67+
given DoubleUnliftable[T <: Double] as Unliftable[T] = new PrimitiveUnliftable
6868

6969
/** Default unliftable for Char
7070
* - Unlifts `'{c}` into `Some(c)` for a literal `c` of type `Char`
7171
* - Otherwise unlifts to `None`
7272
*/
73-
given CharUnliftable as Unliftable[Char] = new PrimitiveUnliftable
73+
given CharUnliftable[T <: Char] as Unliftable[T] = new PrimitiveUnliftable
7474

7575
/** Default unliftable for String
7676
* - Unlifts `'{str}` into `Some(str)` for a literal `str` of type `String`
7777
* - Otherwise unlifts to `None`
7878
*/
79-
given StringUnliftable as Unliftable[String] = new PrimitiveUnliftable
79+
given StringUnliftable[T <: String] as Unliftable[T] = new PrimitiveUnliftable
8080

8181
/** Lift a quoted primitive value `'{ x }` into `x` */
82-
private class PrimitiveUnliftable[T <: Int | Boolean | Byte | Short | Int | Long | Float | Double | Char | String] extends Unliftable[T] {
82+
private class PrimitiveUnliftable[T <: Boolean | Byte | Short | Int | Long | Float | Double | Char | String] extends Unliftable[T] {
8383
def fromExpr(expr: Expr[T]) =
8484
import quotes.reflect._
8585
def rec(tree: Term): Option[T] = tree match {
@@ -135,8 +135,8 @@ object Unliftable {
135135
*/
136136
given StringContextUnliftable as Unliftable[StringContext] = new {
137137
def fromExpr(x: Expr[StringContext]) = x match {
138-
case '{ new StringContext(${Varargs(Consts(args))}: _*) } => Some(StringContext(args: _*))
139-
case '{ StringContext(${Varargs(Consts(args))}: _*) } => Some(StringContext(args: _*))
138+
case '{ new StringContext(${Varargs(Unlifted(args))}: _*) } => Some(StringContext(args: _*))
139+
case '{ StringContext(${Varargs(Unlifted(args))}: _*) } => Some(StringContext(args: _*))
140140
case _ => None
141141
}
142142
}

library/src/scala/quoted/Const.scala

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

library/src/scala/quoted/Consts.scala

Lines changed: 0 additions & 28 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/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/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 Type[T])(using Quotes): 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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ private def rewriteMacro[T: Type](x: Expr[T])(using Quotes): Expr[T] = {
2626
case (_, Some(_)) => '{ $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
}),

0 commit comments

Comments
 (0)