Skip to content

Commit af72d93

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.value`/`expr.valueOrError` when it is simpler to do so. * Remove definition of `quoted.Const.unapply` * Replace uses of `Const` with `Expr` **Migration** * Consider using `expr.value` and them match on `Option` * Consider using `expr.valueOrError` * 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 121ced4 commit af72d93

File tree

24 files changed

+40
-99
lines changed

24 files changed

+40
-99
lines changed

docs/docs/reference/metaprogramming/macros.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -634,15 +634,15 @@ It is possible to deconstruct or extract values out of `Expr` using pattern matc
634634

635635
* `scala.quoted.Expr`/`scala.quoted.Exprs`: matches an expression of a value (or list of values) and returns the value (or list of values).
636636
* `scala.quoted.Const`/`scala.quoted.Consts`: Same as `Expr`/`Exprs` but only works on primitive values.
637-
* `scala.quoted.Varargs`: matches an explicit sequence of expressions and returns them. These sequences are useful to get individual `Expr[T]` out of a varargs expression of type `Expr[Seq[T]]`.
637+
* `scala.quoted.Varargs`: matches an explicit sequence of expresions and returns them. These sequences are useful to get individual `Expr[T]` out of a varargs expression of type `Expr[Seq[T]]`.
638638

639639

640640
These could be used in the following way to optimize any call to `sum` that has statically known values.
641641
```scala
642642
inline def sum(inline args: Int*): Int = ${ sumExpr('args) }
643643
private def sumExpr(argsExpr: Expr[Seq[Int]])(using Quotes): Expr[Int] = argsExpr match {
644-
case Varargs(args @ Exprs(argValues)) =>
645-
// args is of type Seq[Expr[Int]]
644+
case Varargs(argExprs @ Exprs(argValues)) =>
645+
// argExprs is of type Seq[Expr[Int]]
646646
// argValues is of type Seq[Int]
647647
Expr(argValues.sum) // precompute result of sum
648648
case Varargs(argExprs) => // argExprs is of type Seq[Expr[Int]]

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ object Expr {
3232
Block(statements.map(Term.of), Term.of(expr)).asExpr.asInstanceOf[Expr[T]]
3333
}
3434

35-
/** Creates an expression that will construct the value `x` */
35+
/** Lift a value into an expression containing the construction of that value */
3636
def apply[T](x: T)(using ToExpr[T])(using Quotes): Expr[T] =
3737
scala.Predef.summon[ToExpr[T]].apply(x)
3838

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,8 @@ object FromExpr {
136136
*/
137137
given StringContextFromExpr: FromExpr[StringContext] with {
138138
def unapply(x: Expr[StringContext])(using Quotes) = x match {
139-
case '{ new StringContext(${Varargs(Consts(args))}: _*) } => Some(StringContext(args: _*))
140-
case '{ StringContext(${Varargs(Consts(args))}: _*) } => Some(StringContext(args: _*))
139+
case '{ new StringContext(${Varargs(Exprs(args))}: _*) } => Some(StringContext(args: _*))
140+
case '{ StringContext(${Varargs(Exprs(args))}: _*) } => Some(StringContext(args: _*))
141141
case _ => None
142142
}
143143
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
package scala.quoted
22

33
abstract class Expr[+T] private[scala]
4+
5+
object Expr:
6+
def apply[T](x: T)(using ToExpr[T])(using Quotes): Expr[T] = ???
7+
def unapply[T](x: Expr[T])(using FromExpr[T])(using Quotes): Option[T] = ???
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package scala.quoted
2+
3+
trait ToExpr[T]:
4+
def apply(x: T)(using Quotes): T

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.

library/src/scala/quoted/Exprs.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package scala.quoted
22

3+
/** Value expressions */
34
object Exprs:
45

56
/** Matches literal sequence of literal constant value expressions and return a sequence of values.
@@ -9,7 +10,6 @@ object Exprs:
910
* inline def sum(args: Int*): Int = ${ sumExpr('args) }
1011
* def sumExpr(argsExpr: Expr[Seq[Int]])(using Quotes): Expr[Int] = argsExpr match
1112
* case Varargs(Exprs(args)) =>
12-
* case Varargs(Exprs(args)) =>
1313
* // args: Seq[Int]
1414
* ...
1515
* }

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(Exprs(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]: FromExpr[E[T]] = new FromExpr {
1212
def unapply(x: Expr[E[T]])(using Quotes) = 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/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]: FromExpr[E[T]] = new FromExpr { // TODO use type class derivation
1313
def unapply(x: Expr[E[T]])(using Quotes) = (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(_.value.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)