Skip to content

Commit e03231f

Browse files
committed
Remove quoted.Const.unapply
This extractor was replaced with the more versatile extractor `quoted.Expr.unapply`. Usually, it is better to use `expr.unlift`/`expr.unliftOrError` when it is simpler to do so. * Remove definition of `quoted.Const.unapply` * Replace uses of `Const` with `Expr` **Migration** * Consider using `expr.unlift` and them match on `Option` * Consider using `expr.unliftOrError` * Replace `Const(x)` with `Expr(x)` if the type is known * Replace `Const(x: Int)` with `'{ ${Expr(x)}: Int }` if type of expression must be checked at runtime
1 parent 8fb7783 commit e03231f

File tree

20 files changed

+28
-95
lines changed

20 files changed

+28
-95
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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(Exprs(args))}: _*) } => Some(StringContext(args: _*))
139+
case '{ StringContext(${Varargs(Exprs(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(Exprs(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 @ Expr(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 @ Expr(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(${Expr(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 '{ ${Expr(s)}: String } =>
1313
Expr(s.reverse) match
1414
case '{ $x: T } => x
1515
case _ => e // e had a singlton String type

tests/run-macros/expr-map-3/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 '{ ${Unlifted(s)}: String } =>
12+
case '{ ${Expr(s)}: String } =>
1313
// checkIfValid(s)
1414
val s2: String & T = s
1515
Expr(s2)

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(${Expr(x)}, ${Expr(y)}) } =>
3030
Expr(power(x, y))
31-
case '{ power($x, ${Const(y)}) } =>
31+
case '{ power($x, ${Expr(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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ private def rewriteMacro[T: Type](x: Expr[T])(using Quotes): Expr[T] = {
2525
case (_, Some(_)) => '{ $y * $x }
2626
case _ => '{ $x * $y }
2727
}
28-
case '{ power(${Const(x)}, ${Const(y)}) } =>
28+
case '{ power(${Expr(x)}, ${Expr(y)}) } =>
2929
Expr(power(x, y))
30-
case '{ power($x, ${Const(y)}) } =>
30+
case '{ power($x, ${Expr(y)}) } =>
3131
if y == 0 then '{1}
3232
else '{ times($x, power($x, ${Expr(y-1)})) }
3333
}

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(${Expr(n)}) } => Some(I(n))
15+
case '{ D(${Expr(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-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(Exprs(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(${ Expr(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(${Expr(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(${Expr(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](${Expr(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 '{ ${Expr(e)}: Int } => '{ $sym.int(${Expr(e)}).asInstanceOf[R[T]] }
29+
case '{ ${Expr(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/Macro_1.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ private def sumExprShow(argsExpr: Expr[Seq[Int]]) (using Quotes): Expr[String] =
99

1010
private def sumExpr(argsExpr: Expr[Seq[Int]])(using Quotes) : Expr[Int] = {
1111
UnsafeExpr.underlyingArgument(argsExpr) match {
12-
case Varargs(Consts(args)) => // args is of type Seq[Int]
12+
case Varargs(Exprs(args)) => // args is of type Seq[Int]
1313
Expr(args.sum) // precompute result of sum
1414
case Varargs(argExprs) => // argExprs is of type Seq[Expr[Int]]
1515
val staticSum: Int = argExprs.map(_.unlift.getOrElse(0)).sum

tests/run-macros/tasty-extractors-constants-1.check

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,3 @@
22
4
33
abc
44
null
5-
OK

tests/run-macros/tasty-extractors-constants-1/quoted_1.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ object Macros {
1111
val buff = new StringBuilder
1212
def stagedPrintln(x: Any): Unit = buff append java.util.Objects.toString(x) append "\n"
1313

14-
Expr(3) match { case Const(n) => stagedPrintln(n) }
15-
'{4} match { case Const(n) => stagedPrintln(n) }
16-
'{"abc"} match { case Const(n) => stagedPrintln(n) }
14+
Expr(3) match { case Expr(n) => stagedPrintln(n) }
15+
'{4} match { case Expr(n) => stagedPrintln(n) }
16+
'{"abc"} match { case Expr(n) => stagedPrintln(n) }
1717
'{null} match { case '{null} => stagedPrintln(null) }
1818

19-
'{new Object} match { case Const(n) => println(n); case _ => stagedPrintln("OK") }
19+
// '{new Object} match { case Expr(n) => println(n); case _ => stagedPrintln("OK") }
2020

2121
'{print(${Expr(buff.result())})}
2222
}

0 commit comments

Comments
 (0)