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
}

0 commit comments

Comments
 (0)