Skip to content

Commit 7247651

Browse files
Merge pull request #6924 from dotty-staging/recontextualize-liftable-toexpr
Re-contextualize Liftable.toExpr
2 parents b692093 + 2425707 commit 7247651

File tree

18 files changed

+40
-35
lines changed

18 files changed

+40
-35
lines changed

compiler/src/dotty/tools/dotc/transform/ReifyQuotes.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ class ReifyQuotes extends MacroTransform {
204204
private def pickledQuote(body: Tree, splices: List[Tree], originalTp: Type, isType: Boolean)(implicit ctx: Context) = {
205205

206206
def liftedValue[T](value: T, name: TermName, qctx: Tree) =
207-
ref(defn.LiftableModule).select(name).select("toExpr".toTermName).appliedTo(Literal(Constant(value))).appliedTo(qctx)
207+
ref(defn.LiftableModule).select(name).select("toExpr".toTermName).appliedTo(Literal(Constant(value))).select(nme.apply).appliedTo(qctx)
208208

209209
def pickleAsValue[T](value: T) = {
210210
val qctx = ctx.typer.inferImplicitArg(defn.QuoteContextClass.typeRef, body.span)

docs/docs/reference/metaprogramming/macros.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ knowing anything about the representation of `Expr` trees. For
270270
instance, here is a possible instance of `Liftable[Boolean]`:
271271
```scala
272272
delegate for Liftable[Boolean] {
273-
def toExpr(b: Boolean) given QuoteContext: Expr[Boolean] =
273+
def toExpr(b: Boolean) =
274274
if (b) '{ true } else '{ false }
275275
}
276276
```
@@ -279,7 +279,7 @@ possible implementation of `Liftable[Int]` that does not use the underlying
279279
tree machinery:
280280
```scala
281281
delegate for Liftable[Int] {
282-
def toExpr(n: Int) given QuoteContext: Expr[Int] = n match {
282+
def toExpr(n: Int) = n match {
283283
case Int.MinValue => '{ Int.MinValue }
284284
case _ if n < 0 => '{ - ${ toExpr(-n) } }
285285
case 0 => '{ 0 }
@@ -292,7 +292,7 @@ Since `Liftable` is a type class, its instances can be conditional. For example,
292292
a `List` is liftable if its element type is:
293293
```scala
294294
delegate [T: Liftable] for Liftable[List[T]] {
295-
def toExpr(xs: List[T]) given QuoteContext: Expr[List[T]] = xs match {
295+
def toExpr(xs: List[T]) = xs match {
296296
case head :: tail => '{ ${ toExpr(head) } :: ${ toExpr(tail) } }
297297
case Nil => '{ Nil: List[T] }
298298
}

library/src/scala/quoted/Liftable.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ package scala.quoted
66
trait Liftable[T] {
77

88
/** Lift a value into an expression containing the construction of that value */
9-
def toExpr(x: T) given QuoteContext: Expr[T]
9+
def toExpr(x: T): given QuoteContext => Expr[T]
1010

1111
}
1212

@@ -29,15 +29,15 @@ object Liftable {
2929

3030
private class PrimitiveLiftable[T <: Unit | Null | Int | Boolean | Byte | Short | Int | Long | Float | Double | Char | String] extends Liftable[T] {
3131
/** Lift a primitive value `n` into `'{ n }` */
32-
def toExpr(x: T) given (qctx: QuoteContext): Expr[T] = {
32+
def toExpr(x: T) = given qctx => {
3333
import qctx.tasty._
3434
Literal(Constant(x)).seal.asInstanceOf[Expr[T]]
3535
}
3636
}
3737

3838
implicit def ClassIsLiftable[T]: Liftable[Class[T]] = new Liftable[Class[T]] {
3939
/** Lift a `Class[T]` into `'{ classOf[T] }` */
40-
def toExpr(x: Class[T]) given (qctx: QuoteContext): Expr[Class[T]] = {
40+
def toExpr(x: Class[T]) = given qctx => {
4141
import qctx.tasty._
4242
Ref(definitions.Predef_classOf).appliedToType(Type(x)).seal.asInstanceOf[Expr[Class[T]]]
4343
}

tests/pos/quote-liftable-list-2.scala

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,7 @@ import scala.quoted._
33
object Test {
44

55
implicit def ListIsLiftableOr[T: Type, U: Type]: Liftable[List[T | U]] = new {
6-
def toExpr(xs: List[T | U]) given QuoteContext: Expr[List[T | U]] = '{ Nil: List[T | U] }
7-
}
8-
9-
implicit def ListIsLiftableAnd[T: Type, U: Type]: Liftable[List[T & U]] = new {
10-
def toExpr(xs: List[T & U]) given QuoteContext: Expr[List[T & U]] = '{ Nil: List[T & U] }
6+
def toExpr(xs: List[T | U]) = '{ Nil: List[T | U] }
117
}
128

139
}

tests/pos/quote-liftable-list-3.scala

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import scala.quoted._
2+
3+
object Test {
4+
5+
implicit def ListIsLiftableAnd[T: Type, U: Type]: Liftable[List[T & U]] = new {
6+
def toExpr(xs: List[T & U]) = '{ Nil: List[T & U] }
7+
}
8+
9+
}

tests/pos/quote-liftable-list.scala

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

55
implicit def ListIsLiftable[T: Liftable: Type]: Liftable[List[T]] = new {
6-
def toExpr(xs: List[T]) given QuoteContext: Expr[List[T]] = '{ Nil: List[T] }
6+
def toExpr(xs: List[T]) = '{ Nil: List[T] }
77
}
88

99
}

tests/pos/quote-liftable.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ def test given QuoteContext = {
55
delegate for QuoteContext = ???
66

77
implicit def IntIsLiftable: Liftable[Int] = new {
8-
def toExpr(n: Int) given QuoteContext: Expr[Int] = n match {
8+
def toExpr(n: Int) = n match {
99
case Int.MinValue => '{Int.MinValue}
1010
case _ if n < 0 => '{- ${toExpr(n)}}
1111
case 0 => '{0}
@@ -15,12 +15,12 @@ def test given QuoteContext = {
1515
}
1616

1717
implicit def BooleanIsLiftable: Liftable[Boolean] = new {
18-
implicit def toExpr(b: Boolean) given QuoteContext: Expr[Boolean] =
18+
implicit def toExpr(b: Boolean) =
1919
if (b) '{true} else '{false}
2020
}
2121

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

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ object TypeToolbox {
112112

113113
// TODO add to the std lib
114114
private implicit def listIsLiftable[T: Type: Liftable]: Liftable[List[T]] = new Liftable {
115-
def toExpr(list: List[T]) given QuoteContext: Expr[List[T]] = list match {
115+
def toExpr(list: List[T]) = list match {
116116
case x :: xs => '{${x.toExpr} :: ${toExpr(xs)}}
117117
case Nil => '{Nil}
118118
}

tests/run-macros/quote-force/quoted_1.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ object Location {
1313
}
1414

1515
private implicit def ListIsLiftable[T : Liftable : Type]: Liftable[List[T]] = new Liftable[List[T]] {
16-
def toExpr(x: List[T]) given QuoteContext: Expr[List[T]] = x match {
16+
def toExpr(x: List[T]) = x match {
1717
case x :: xs => '{ ${x} :: ${xs} }
1818
case Nil => '{ List.empty[T] }
1919
}

tests/run-macros/tasty-interpolation-1/Macro.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,10 @@ abstract class MacroStringInterpolator[T] {
7777
}
7878

7979
protected implicit def StringContextIsLiftable: Liftable[StringContext] = new Liftable[StringContext] {
80-
def toExpr(strCtx: StringContext) given QuoteContext: Expr[StringContext] = {
80+
def toExpr(strCtx: StringContext) = {
8181
// TODO define in stdlib?
8282
implicit def ListIsLiftable: Liftable[List[String]] = new Liftable[List[String]] {
83-
override def toExpr(list: List[String]) given QuoteContext: Expr[List[String]] = list match {
83+
override def toExpr(list: List[String]) = list match {
8484
case x :: xs => '{${x.toExpr} :: ${toExpr(xs)}}
8585
case Nil => '{Nil}
8686
}

tests/run-macros/tasty-location/quoted_1.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ object Location {
1919
}
2020

2121
private implicit def ListIsLiftable[T : Liftable : Type]: Liftable[List[T]] = new Liftable[List[T]] {
22-
def toExpr(x: List[T]) given QuoteContext: Expr[List[T]] = x match {
22+
def toExpr(x: List[T]) = x match {
2323
case x :: xs => '{ $x :: $xs }
2424
case Nil => '{ List.empty[T] }
2525
}

tests/run-with-compiler/i3847-b.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import scala.reflect.ClassTag
44
object Arrays {
55
implicit def ArrayIsLiftable[T: Liftable](implicit t: Type[T], qctx: QuoteContext): Liftable[Array[List[T]]] = {
66
new Liftable[Array[List[T]]] {
7-
def toExpr(arr: Array[List[T]]) given QuoteContext: Expr[Array[List[T]]] = '{
7+
def toExpr(arr: Array[List[T]]) = '{
88
new Array[List[$t]](${arr.length.toExpr})
99
// TODO add elements
1010
}

tests/run-with-compiler/i3847.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import scala.reflect.ClassTag
44
object Arrays {
55
implicit def ArrayIsLiftable[T: Liftable](implicit t: Type[T], ct: Expr[ClassTag[T]]): Liftable[Array[T]] = {
66
new Liftable[Array[T]] {
7-
def toExpr(arr: Array[T]) given QuoteContext: Expr[Array[T]] = '{
7+
def toExpr(arr: Array[T]) = '{
88
new Array[$t](${arr.length.toExpr})($ct)
99
// TODO add elements
1010
}

tests/run-with-compiler/quote-lib.scala

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ package liftable {
5353

5454
object Units {
5555
implicit def UnitIsLiftable: Liftable[Unit] = new Liftable[Unit] {
56-
def toExpr(x: Unit) given QuoteContext: Expr[Unit] = '{}
56+
def toExpr(x: Unit) = '{}
5757
}
5858
}
5959

@@ -74,24 +74,24 @@ package liftable {
7474
object Tuples {
7575

7676
implicit def Tuple1IsLiftable[T1: Liftable](implicit t1: Type[T1]): Liftable[Tuple1[T1]] = new Liftable[Tuple1[T1]] {
77-
def toExpr(x: Tuple1[T1]) given QuoteContext: Expr[Tuple1[T1]] =
77+
def toExpr(x: Tuple1[T1]) =
7878
'{ Tuple1[$t1](${ x._1}) }
7979
}
8080

8181
implicit def Tuple2IsLiftable[T1: Liftable, T2: Liftable](implicit t1: Type[T1], t2: Type[T2]): Liftable[(T1, T2)] = new Liftable[(T1, T2)] {
82-
def toExpr(x: (T1, T2)) given QuoteContext: Expr[(T1, T2)] =
82+
def toExpr(x: (T1, T2)) =
8383
'{ Tuple2[$t1, $t2](${x._1}, ${x._2}) }
8484

8585
}
8686

8787
implicit def Tuple3IsLiftable[T1: Liftable, T2: Liftable, T3: Liftable](implicit t1: Type[T1], t2: Type[T2], t3: Type[T3]): Liftable[(T1, T2, T3)] = new Liftable[(T1, T2, T3)] {
88-
def toExpr(x: (T1, T2, T3)) given QuoteContext: Expr[(T1, T2, T3)] =
88+
def toExpr(x: (T1, T2, T3)) =
8989
'{ Tuple3[$t1, $t2, $t3](${x._1}, ${x._2}, ${x._3}) }
9090

9191
}
9292

9393
implicit def Tuple4IsLiftable[T1: Liftable, T2: Liftable, T3: Liftable, T4: Liftable](implicit t1: Type[T1], t2: Type[T2], t3: Type[T3], t4: Type[T4]): Liftable[(T1, T2, T3, T4)] = new Liftable[(T1, T2, T3, T4)] {
94-
def toExpr(x: (T1, T2, T3, T4)) given QuoteContext: Expr[(T1, T2, T3, T4)] =
94+
def toExpr(x: (T1, T2, T3, T4)) =
9595
'{ Tuple4[$t1, $t2, $t3, $t4](${x._1}, ${x._2}, ${x._3}, ${x._4}) }
9696
}
9797

@@ -102,7 +102,7 @@ package liftable {
102102

103103
object Lists {
104104
implicit def ListIsLiftable[T: Liftable](implicit t: Type[T]): Liftable[List[T]] = new Liftable[List[T]] {
105-
def toExpr(x: List[T]) given QuoteContext: Expr[List[T]] = x match {
105+
def toExpr(x: List[T]): given QuoteContext => Expr[List[T]] = x match {
106106
case x :: xs => '{ (${xs}).::[$t](${x}) }
107107
case Nil => '{ Nil: List[$t] }
108108
}
@@ -128,7 +128,7 @@ package liftable {
128128

129129
object Arrays {
130130
implicit def ArrayIsLiftable[T: Liftable](implicit t: Type[T], ct: Expr[ClassTag[T]]): Liftable[Array[T]] = new Liftable[Array[T]] {
131-
def toExpr(arr: Array[T]) given QuoteContext: Expr[Array[T]] = '{ new Array[$t](${arr.length})($ct) }
131+
def toExpr(arr: Array[T]) = '{ new Array[$t](${arr.length})($ct) }
132132
}
133133
}
134134

tests/run-with-compiler/quote-unrolled-foreach.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ object Test {
118118
}
119119

120120
implicit object ArrayIntIsLiftable extends Liftable[Array[Int]] {
121-
override def toExpr(x: Array[Int]) given QuoteContext: Expr[Array[Int]] = '{
121+
override def toExpr(x: Array[Int]) = '{
122122
val array = new Array[Int](${x.length})
123123
${ foreachInRange(0, x.length)(i => '{ array(${i}) = ${x(i)}}) }
124124
array

tests/run-with-compiler/shonan-hmm-simple.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ case class Complex[T](re: T, im: T)
7272

7373
object Complex {
7474
implicit def isLiftable[T: Type: Liftable]: Liftable[Complex[T]] = new Liftable[Complex[T]] {
75-
def toExpr(comp: Complex[T]) given QuoteContext: Expr[Complex[T]] = '{Complex(${comp.re}, ${comp.im})}
75+
def toExpr(comp: Complex[T]) = '{Complex(${comp.re}, ${comp.im})}
7676
}
7777
}
7878

tests/run-with-compiler/shonan-hmm/Complex.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ case class Complex[T](re: T, im: T)
55

66
object Complex {
77
implicit def complexIsLiftable[T: Type: Liftable]: Liftable[Complex[T]] = new Liftable {
8-
def toExpr(c: Complex[T]) given QuoteContext: Expr[Complex[T]] = '{ Complex(${c.re.toExpr}, ${c.im.toExpr}) }
8+
def toExpr(c: Complex[T]) = '{ Complex(${c.re.toExpr}, ${c.im.toExpr}) }
99
}
1010

1111
def of_complex_expr(x: Expr[Complex[Int]]) given QuoteContext: Complex[Expr[Int]] = Complex('{$x.re}, '{$x.im})

tests/run-with-compiler/shonan-hmm/Lifters.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@ object Lifters {
1111
}
1212

1313
implicit def ArrayIsLiftable[T : Type: ClassTag](implicit l: Liftable[T]): Liftable[Array[T]] = new Liftable[Array[T]] {
14-
def toExpr(x: Array[T]) given QuoteContext: Expr[Array[T]] = '{
14+
def toExpr(x: Array[T]) = '{
1515
val array = new Array[T](${x.length})(${implicitly[Expr[ClassTag[T]]]})
1616
${initArray(x, 'array)}
1717
}
1818
}
1919

2020
implicit def IntArrayIsLiftable: Liftable[Array[Int]] = new Liftable[Array[Int]] {
21-
def toExpr(x: Array[Int]) given QuoteContext: Expr[Array[Int]] = '{
21+
def toExpr(x: Array[Int]) = '{
2222
val array = new Array[Int](${x.length})
2323
${initArray(x, 'array)}
2424
}

0 commit comments

Comments
 (0)