Skip to content

Commit 4b732ce

Browse files
nicolasstuckiliufengyun
authored andcommitted
Replace quoted toExpr with Expr.apply
1 parent 8258caf commit 4b732ce

File tree

76 files changed

+218
-215
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

76 files changed

+218
-215
lines changed

docs/docs/reference/changed-features/numeric-literals.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ implementation method `fromDigitsImpl`. Here is its definition:
206206
case Const(ds) =>
207207
try {
208208
val BigFloat(m, e) = apply(ds)
209-
'{BigFloat(${m.toExpr}, ${e.toExpr})}
209+
'{BigFloat(${Expr(m)}, ${Expr(e)})}
210210
}
211211
catch {
212212
case ex: FromDigits.FromDigitsException =>

docs/docs/reference/metaprogramming/macros.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ import given scala.quoted._
228228

229229
def compile(e: Exp, env: Map[String, Expr[Int]]): Expr[Int] = e match {
230230
case Num(n) =>
231-
n.toExpr
231+
Expr(n)
232232
case Plus(e1, e2) =>
233233
'{ ${ compile(e1, env) } + ${ compile(e2, env) } }
234234
case Var(x) =>
@@ -241,7 +241,7 @@ Running `compile(letExp, Map())` would yield the following Scala code:
241241
```scala
242242
'{ val y = 3; (2 + y) + 4 }
243243
```
244-
The body of the first clause, `case Num(n) => n.toExpr`, looks suspicious. `n`
244+
The body of the first clause, `case Num(n) => Expr(n)`, looks suspicious. `n`
245245
is declared as an `Int`, yet it is converted to an `Expr[Int]` with `toExpr`.
246246
Shouldn’t `n` be quoted? In fact this would not
247247
work since replacing `n` by `'n` in the clause would not be phase
@@ -308,7 +308,7 @@ Using lifting, we can now give the missing definition of `showExpr` in the intro
308308
```scala
309309
def showExpr[T](expr: Expr[T]): Expr[String] = {
310310
val code: String = expr.show
311-
code.toExpr
311+
Expr(code)
312312
}
313313
```
314314
That is, the `showExpr` method converts its `Expr` argument to a string (`code`), and lifts

docs/docs/reference/metaprogramming/tasty-reflect.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ The method `qctx.tasty.Term.seal[T]` provides a way to go back to a
6868
`quoted.Expr[Any]`. Note that the type is `Expr[Any]`. Consequently, the type
6969
must be set explicitly with a checked `cast` call. If the type does not conform
7070
to it an exception will be thrown. In the code above, we could have replaced
71-
`n.toExpr` by `xTree.seal.cast[Int]`.
71+
`Expr(n)` by `xTree.seal.cast[Int]`.
7272

7373
### Obtaining the underlying argument
7474

library/src-bootstrapped/dotty/internal/StringContextMacro.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -751,6 +751,6 @@ object StringContextMacro {
751751
}
752752

753753
// macro expansion
754-
'{(${parts.mkString.toExpr}).format(${argsExpr}: _*)}
754+
'{(${Expr(parts.mkString)}).format(${argsExpr}: _*)}
755755
}
756756
}

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

Lines changed: 40 additions & 40 deletions
Large diffs are not rendered by default.

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package scala
33
package object quoted {
44

55
implicit object ExprOps {
6-
def (x: T) toExpr[T: Liftable](given QuoteContext): Expr[T] = summon[Liftable[T]].toExpr(x)
6+
@deprecated("Use scala.quoted.Expr.apply instead", "0.19.0")
7+
def (x: T) toExpr[T: Liftable](given QuoteContext): Expr[T] = Expr(x)
78
}
89
}

library/src/scala/quoted/Expr.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ package quoted {
6565
Block(statements.map(_.unseal), expr.unseal).seal.asInstanceOf[Expr[T]]
6666
}
6767

68+
/** Lift a value into an expression containing the construction of that value */
69+
def apply[T: Liftable](x: T)(given QuoteContext): Expr[T] = summon[Liftable[T]].toExpr(x)
70+
6871
/** Lifts this sequence of expressions into an expression of a sequence
6972
*
7073
* Transforms a sequence of expression

library/src/scala/quoted/autolift.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ package scala.quoted
33
/** Enable implicit conversion from a term of type `T` to an expression of type `Expr[T]` */
44
object autolift {
55
/** Implicit conversion from a term of type `T` to an expression of type `Expr[T]` */
6-
given autoToExpr[T](given Liftable[T], QuoteContext): Conversion[T, Expr[T]] = _.toExpr
6+
given autoToExpr[T](given Liftable[T], QuoteContext): Conversion[T, Expr[T]] = Expr(_)
77
}

tests/disabled/pos/quote-whitebox/Macro_1.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ object Macro {
55

66
inline def charOrString(inline str: String) <: Char | String = ${ impl(str) }
77

8-
def impl(str: String) = if (str.length == 1) str.charAt(0).toExpr else str.toExpr
8+
def impl(str: String) = if (str.length == 1) Expr(str.charAt(0)) else Expr(str)
99

1010
}

tests/neg/GenericNumLits/Even_1.scala renamed to tests/neg-macros/GenericNumLits/Even_1.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ object Even {
2020
ctx.error(ex.getMessage)
2121
Even(0)
2222
}
23-
'{Even(${ev.n.toExpr})}
23+
'{Even(${Expr(ev.n)})}
2424
case _ =>
2525
'{evenFromDigits($digits)}
2626
}

tests/neg-macros/inline-case-objects/Macro_1.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import scala.quoted._
33

44
object Macros {
5-
def impl(foo: Any)(given QuoteContext): Expr[String] = foo.getClass.getCanonicalName.toExpr
5+
def impl(foo: Any)(given QuoteContext): Expr[String] = Expr(foo.getClass.getCanonicalName)
66
}
77

88
class Bar {

tests/neg-macros/inline-option/Macro_1.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import scala.quoted._
33

44
object Macro {
55
def impl(opt: Option[Int])(given QuoteContext): Expr[Int] = opt match {
6-
case Some(i) => i.toExpr
6+
case Some(i) => Expr(i)
77
case None => '{-1}
88
}
99
}

tests/neg-macros/quote-macro-complex-arg-0.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@ import scala.quoted._
22

33
object Macros {
44
inline def foo(inline i: Int, dummy: Int, j: Int): Int = ${ bar(i + 1, 'j) } // error: i + 1 is not a parameter or field reference
5-
def bar(x: Int, y: Expr[Int])(given QuoteContext): Expr[Int] = '{ ${x.toExpr} + $y }
5+
def bar(x: Int, y: Expr[Int])(given QuoteContext): Expr[Int] = '{ ${Expr(x)} + $y }
66
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ object Even {
2020
ctx.error(ex.getMessage)
2121
Even(0)
2222
}
23-
'{Even(${ev.n.toExpr})}
23+
'{Even(${Expr(ev.n)})}
2424
case _ =>
2525
'{evenFromDigits($digits)}
2626
}

tests/neg/BigFloat/BigFloat_1.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ object BigFloat extends App {
3535
case Const(ds) =>
3636
try {
3737
val BigFloat(m, e) = apply(ds)
38-
'{BigFloat(${m.toExpr}, ${e.toExpr})}
38+
'{BigFloat(${Expr(m)}, ${Expr(e)})}
3939
}
4040
catch {
4141
case ex: FromDigits.FromDigitsException =>
@@ -60,7 +60,7 @@ object BigFloat extends App {
6060

6161
given Liftable[BigInt] {
6262
def toExpr(x: BigInt) =
63-
'{BigInt(${x.toString.toExpr})}
63+
'{BigInt(${Expr(x.toString)})}
6464
}
6565
}
6666

tests/pending/pos/i4987.scala

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ object Foo {
1212

1313
implicit def NilIsLiftable: Liftable[Nil.type] = ???
1414

15-
Nil.toExpr(NilIsLiftable)
16-
(Nil.toExpr: Expr[Nil.type])
17-
Nil.toExpr
15+
Expr(Nil)(given NilIsLiftable)
16+
Expr(Nil)
1817
}

tests/pos-macros/quote-whitebox-2/Macro_1.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ object Macro {
55

66
inline def charOrString(inline str: String) <: Any = ${ impl(str) }
77

8-
def impl(str: String)(given QuoteContext) = if (str.length == 1) str.charAt(0).toExpr else str.toExpr
8+
def impl(str: String)(given QuoteContext) = if (str.length == 1) Expr(str.charAt(0)) else Expr(str)
99

1010
}

tests/pos/i5547.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ object scalatest {
55
${assertImpl('condition, '{""})}
66

77
inline def assert2(condition: => Boolean): Unit =
8-
${ assertImpl('condition, "".toExpr) }
8+
${ assertImpl('condition, Expr("")) }
99

1010
def assertImpl(condition: Expr[Boolean], clue: Expr[Any])(given QuoteContext): Expr[Unit] =
1111
'{}

tests/pos/i6214.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ import scala.quoted._
22
object Test {
33
def res(x: quoted.Expr[Int])(given QuoteContext): quoted.Expr[Int] = x match {
44
case '{ val a: Int = $y; 1} => y // owner of `y` is `res`
5-
case _ => '{ val b: Int = ${val c = 2; c.toExpr}; 1} // owner of `c` is `b`, but that seems to be OK
5+
case _ => '{ val b: Int = ${val c = 2; Expr(c)}; 1} // owner of `c` is `b`, but that seems to be OK
66
}
77
}

tests/pos/quote-lift.scala

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

1111
'{ ${summon[Liftable[Int]].toExpr(1)} }
1212

13-
'{ ${1.toExpr} }
13+
'{ ${Expr(1)} }
1414

1515
}
1616

tests/pos/quote-liftable.scala

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,20 @@ def test(given QuoteContext) = {
2121

2222
implicit def ListIsLiftable[T: Liftable: Type]: Liftable[List[T]] = new {
2323
def toExpr(xs: List[T]) = xs match {
24-
case x :: xs1 => '{ ${ implicitly[Liftable[T]].toExpr(x) } :: ${ toExpr(xs1) } }
24+
case x :: xs1 => '{ ${ Expr(x) } :: ${ toExpr(xs1) } }
2525
case Nil => '{Nil: List[T]}
2626
}
2727
}
2828

29-
true.toExpr
30-
1.toExpr
31-
'a'.toExpr
32-
1.toExpr
33-
1.toExpr
34-
1L.toExpr
35-
1.0f.toExpr
36-
1.0.toExpr
37-
"abc".toExpr
29+
Expr(true)
30+
Expr(1)
31+
Expr('a')
32+
Expr(1)
33+
Expr(1)
34+
Expr(1L)
35+
Expr(1.0f)
36+
Expr(1.0)
37+
Expr("abc")
3838

39-
val xs: Expr[List[Int]] = (1 :: 2 :: 3 :: Nil).toExpr
39+
val xs: Expr[List[Int]] = Expr(1 :: 2 :: 3 :: Nil)
4040
}

tests/run-custom-args/Yretain-trees/tasty-load-tree-1/quoted_1.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ object Foo {
1010
import qctx.tasty._
1111

1212
def definitionString(tree: Tree): Expr[String] = tree.symbol match {
13-
case IsClassDefSymbol(sym) => sym.tree.showExtractors.toExpr
14-
case IsDefDefSymbol(sym) => sym.tree.showExtractors.toExpr
15-
case IsValDefSymbol(sym) => sym.tree.showExtractors.toExpr
13+
case IsClassDefSymbol(sym) => Expr(sym.tree.showExtractors)
14+
case IsDefDefSymbol(sym) => Expr(sym.tree.showExtractors)
15+
case IsValDefSymbol(sym) => Expr(sym.tree.showExtractors)
1616
case _ => '{"NO DEFINTION"}
1717
}
1818

tests/run-custom-args/Yretain-trees/tasty-load-tree-2/quoted_1.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ object Foo {
99
import qctx.tasty._
1010

1111
def definitionString(tree: Tree): Expr[String] = tree.symbol match {
12-
case IsClassDefSymbol(sym) => sym.tree.showExtractors.toExpr
13-
case IsDefDefSymbol(sym) => sym.tree.showExtractors.toExpr
14-
case IsValDefSymbol(sym) => sym.tree.showExtractors.toExpr
12+
case IsClassDefSymbol(sym) => Expr(sym.tree.showExtractors)
13+
case IsDefDefSymbol(sym) => Expr(sym.tree.showExtractors)
14+
case IsValDefSymbol(sym) => Expr(sym.tree.showExtractors)
1515
case _ => '{"NO DEFINTION"}
1616
}
1717

tests/run-macros/gestalt-type-toolbox-reflect/Macro_1.scala

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,23 @@ object TypeToolbox {
99
private def tpEqImpl[A, B](a: Type[A], b: Type[B])(given qctx: QuoteContext): Expr[Boolean] = {
1010
import qctx.tasty._
1111
val res = a.unseal.tpe =:= b.unseal.tpe
12-
res.toExpr
12+
Expr(res)
1313
}
1414

1515
/** is `tp1` a subtype of `tp2` */
1616
inline def <:<[A, B]: Boolean = ${tpLEqImpl('[A], '[B])}
1717
private def tpLEqImpl[A, B](a: Type[A], b: Type[B])(given qctx: QuoteContext): Expr[Boolean] = {
1818
import qctx.tasty._
1919
val res = a.unseal.tpe <:< b.unseal.tpe
20-
res.toExpr
20+
Expr(res)
2121
}
2222

2323
/** type associated with the tree */
2424
inline def typeOf[T, Expected](a: T): Boolean = ${typeOfImpl('a, '[Expected])}
2525
private def typeOfImpl(a: Expr[_], expected: Type[_])(given qctx: QuoteContext): Expr[Boolean] = {
2626
import qctx.tasty._
2727
val res = a.unseal.tpe =:= expected.unseal.tpe
28-
res.toExpr
28+
Expr(res)
2929
}
3030

3131
/** does the type refer to a case class? */
@@ -36,67 +36,67 @@ object TypeToolbox {
3636
case IsClassDefSymbol(sym) => sym.flags.is(Flags.Case)
3737
case _ => false
3838
}
39-
res.toExpr
39+
Expr(res)
4040
}
4141

4242
/** val fields of a case class Type -- only the ones declared in primary constructor */
4343
inline def caseFields[T]: List[String] = ${caseFieldsImpl('[T])}
4444
private def caseFieldsImpl(tp: Type[_])(given qctx: QuoteContext): Expr[List[String]] = {
4545
import qctx.tasty._
4646
val fields = tp.unseal.symbol.asClassDef.caseFields.map(_.name)
47-
fields.toExpr
47+
Expr(fields)
4848
}
4949

5050
inline def fieldIn[T](inline mem: String): String = ${fieldInImpl('[T], mem)}
5151
private def fieldInImpl(t: Type[_], mem: String)(given qctx: QuoteContext): Expr[String] = {
5252
import qctx.tasty._
5353
val field = t.unseal.symbol.asClassDef.field(mem)
54-
field.map(_.name).getOrElse("").toExpr
54+
Expr(field.map(_.name).getOrElse(""))
5555
}
5656

5757
inline def fieldsIn[T]: Seq[String] = ${fieldsInImpl('[T])}
5858
private def fieldsInImpl(t: Type[_])(given qctx: QuoteContext): Expr[Seq[String]] = {
5959
import qctx.tasty._
6060
val fields = t.unseal.symbol.asClassDef.fields
61-
fields.map(_.name).toList.toExpr
61+
Expr(fields.map(_.name).toList)
6262
}
6363

6464
inline def methodIn[T](inline mem: String): Seq[String] = ${methodInImpl('[T], mem)}
6565
private def methodInImpl(t: Type[_], mem: String)(given qctx: QuoteContext): Expr[Seq[String]] = {
6666
import qctx.tasty._
67-
t.unseal.symbol.asClassDef.classMethod(mem).map(_.name).toExpr
67+
Expr(t.unseal.symbol.asClassDef.classMethod(mem).map(_.name))
6868
}
6969

7070
inline def methodsIn[T]: Seq[String] = ${methodsInImpl('[T])}
7171
private def methodsInImpl(t: Type[_])(given qctx: QuoteContext): Expr[Seq[String]] = {
7272
import qctx.tasty._
73-
t.unseal.symbol.asClassDef.classMethods.map(_.name).toExpr
73+
Expr(t.unseal.symbol.asClassDef.classMethods.map(_.name))
7474
}
7575

7676
inline def method[T](inline mem: String): Seq[String] = ${methodImpl('[T], mem)}
7777
private def methodImpl(t: Type[_], mem: String)(given qctx: QuoteContext): Expr[Seq[String]] = {
7878
import qctx.tasty._
79-
t.unseal.symbol.asClassDef.method(mem).map(_.name).toExpr
79+
Expr(t.unseal.symbol.asClassDef.method(mem).map(_.name))
8080
}
8181

8282
inline def methods[T]: Seq[String] = ${methodsImpl('[T])}
8383
private def methodsImpl(t: Type[_])(given qctx: QuoteContext): Expr[Seq[String]] = {
8484
import qctx.tasty._
85-
t.unseal.symbol.asClassDef.methods.map(_.name).toExpr
85+
Expr(t.unseal.symbol.asClassDef.methods.map(_.name))
8686
}
8787

8888
inline def typeTag[T](x: T): String = ${typeTagImpl('[T])}
8989
private def typeTagImpl(tp: Type[_])(given qctx: QuoteContext): Expr[String] = {
9090
import qctx.tasty._
9191
val res = tp.unseal.tpe.show
92-
res.toExpr
92+
Expr(res)
9393
}
9494

9595
inline def companion[T1, T2]: Boolean = ${companionImpl('[T1], '[T2])}
9696
private def companionImpl(t1: Type[_], t2: Type[_])(given qctx: QuoteContext): Expr[Boolean] = {
9797
import qctx.tasty._
9898
val res = t1.unseal.symbol.asClassDef.companionModule.contains(t2.unseal.symbol)
99-
res.toExpr
99+
Expr(res)
100100
}
101101

102102
inline def companionName[T1]: String = ${companionNameImpl('[T1])}
@@ -107,7 +107,7 @@ object TypeToolbox {
107107
case IsValDefSymbol(sym) => sym.companionClass
108108
case _ => None
109109
}
110-
companionClassOpt.map(_.fullName).getOrElse("").toExpr
110+
Expr(companionClassOpt.map(_.fullName).getOrElse(""))
111111
}
112112

113113
}

tests/run-macros/i5629/Macro_1.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@ object Macros {
1414

1515
def thisLineNumberImpl(given qctx: QuoteContext): Expr[Int] = {
1616
import qctx.tasty._
17-
rootPosition.startLine.toExpr
17+
Expr(rootPosition.startLine)
1818
}
1919
}

tests/run-macros/i6765-b/Macro_1.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ inline def foo = ${fooImpl}
55

66
def fooImpl(given qctx: QuoteContext) = {
77
val res = Expr.ofList(List('{"One"}))
8-
res.show.toExpr
8+
Expr(res.show)
99
}

tests/run-macros/i6765-c/Macro_1.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ import scala.quoted.given
44
inline def foo(inline n: Int) = ${fooImpl(n)}
55

66
def fooImpl(n: Int)(given qctx: QuoteContext) = {
7-
val res = Expr.ofList(List.tabulate(n)(i => ("#" + i).toExpr))
8-
'{ ${res.show.toExpr} + "\n" + $res.toString + "\n" }
7+
val res = Expr.ofList(List.tabulate(n)(i => Expr("#" + i)))
8+
'{ ${Expr(res.show)} + "\n" + $res.toString + "\n" }
99
}

tests/run-macros/i6765/Macro_1.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ inline def foo = ${fooImpl}
66
def fooImpl(given qctx: QuoteContext) = {
77
import qctx.tasty._
88
val res = Expr.ofList(List('{"One"}))
9-
res.show.toExpr
9+
Expr(res.show)
1010
}

0 commit comments

Comments
 (0)