Skip to content

Commit ea57e36

Browse files
Merge pull request #6785 from dotty-staging/add-QuoteContext-to-Liftable
Add missing QuoteContext to Liftable.toExpr
2 parents e12ebdf + 0e5acc0 commit ea57e36

File tree

143 files changed

+243
-357
lines changed

Some content is hidden

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

143 files changed

+243
-357
lines changed

docs/docs/reference/metaprogramming/macros.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -251,8 +251,9 @@ The `toExpr` extension method is defined in package `quoted`:
251251
```scala
252252
package quoted
253253

254-
delegate LiftingOps {
255-
def (x: T) toExpr[T] given (ev: Liftable[T]): Expr[T] = ev.toExpr(x)
254+
delegate ExprOps {
255+
def (x: T) toExpr[T: Liftable] given QuoteContext: Expr[T] = the[Liftable[T]].toExpr(x)
256+
...
256257
}
257258
```
258259
The extension says that values of types implementing the `Liftable` type class can be
@@ -269,15 +270,16 @@ knowing anything about the representation of `Expr` trees. For
269270
instance, here is a possible instance of `Liftable[Boolean]`:
270271
```scala
271272
delegate for Liftable[Boolean] {
272-
def toExpr(b: Boolean) = if (b) '{ true } else '{ false }
273+
def toExpr(b: Boolean) given QuoteContext: Expr[Boolean] =
274+
if (b) '{ true } else '{ false }
273275
}
274276
```
275277
Once we can lift bits, we can work our way up. For instance, here is a
276278
possible implementation of `Liftable[Int]` that does not use the underlying
277279
tree machinery:
278280
```scala
279281
delegate for Liftable[Int] {
280-
def toExpr(n: Int): Expr[Int] = n match {
282+
def toExpr(n: Int) given QuoteContext: Expr[Int] = n match {
281283
case Int.MinValue => '{ Int.MinValue }
282284
case _ if n < 0 => '{ - ${ toExpr(-n) } }
283285
case 0 => '{ 0 }
@@ -290,7 +292,7 @@ Since `Liftable` is a type class, its instances can be conditional. For example,
290292
a `List` is liftable if its element type is:
291293
```scala
292294
delegate [T: Liftable] for Liftable[List[T]] {
293-
def toExpr(xs: List[T]): Expr[List[T]] = xs match {
295+
def toExpr(xs: List[T]) given QuoteContext: Expr[List[T]] = xs match {
294296
case head :: tail => '{ ${ toExpr(head) } :: ${ toExpr(tail) } }
295297
case Nil => '{ Nil: List[T] }
296298
}

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ is used.
2525

2626
```scala
2727
import scala.quoted._
28-
import scala.tasty._
2928

3029
inline def natConst(x: => Int): Int = ${natConstImpl('{x})}
3130

library/src-3.x/scala/quoted/Liftable.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import scala.runtime.quoted.Unpickler.liftedExpr
66
* without going through an explicit `'{...}` operation.
77
*/
88
abstract class Liftable[T] {
9-
def toExpr(x: T): Expr[T]
9+
def toExpr(x: T) given QuoteContext: Expr[T]
1010
}
1111

1212
/** Some liftable base types. To be completed with at least all types
@@ -27,7 +27,7 @@ object Liftable {
2727
implicit def ClassIsLiftable[T]: Liftable[Class[T]] = new PrimitiveLiftable
2828

2929
private class PrimitiveLiftable[T] extends Liftable[T] {
30-
override def toExpr(x: T): Expr[T] = liftedExpr(x)
30+
override def toExpr(x: T) given QuoteContext: Expr[T] = liftedExpr(x)
3131
}
3232

3333
}

library/src-3.x/scala/quoted/package.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,11 @@ package object quoted {
4848
private object NoResult
4949

5050
object autolift {
51-
implicit def autoToExpr[T: Liftable](x: T): Expr[T] = x.toExpr
51+
implicit def autoToExpr[T: Liftable](x: T) given QuoteContext: Expr[T] = x.toExpr
5252
}
5353

5454
implicit object ExprOps {
55-
def (x: T) toExpr[T] given Liftable[T]: Expr[T] = the[Liftable[T]].toExpr(x)
55+
def (x: T) toExpr[T: Liftable] given QuoteContext: Expr[T] = the[Liftable[T]].toExpr(x)
5656

5757
def (list: List[Expr[T]]) toExprOfList[T] given Type[T]: Expr[List[T]] = list match {
5858
case x :: xs => '{ $x :: ${xs.toExprOfList} }

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ package dotty.internal
44

55
import scala.quoted._
66
import scala.quoted.matching._
7-
import scala.tasty.Reflection
87
import reflect._
98

109
object StringContextMacro {

semanticdb/src/dotty/semanticdb/Main.scala

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package dotty.semanticdb
22

3-
import scala.tasty.Reflection
43
import scala.tasty.file._
54
import scala.NotImplementedError
65

semanticdb/src/dotty/semanticdb/TastyScalaFileInferrer.scala

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package dotty.semanticdb
22

3-
import scala.tasty.Reflection
4-
53
import scala.meta.internal.{semanticdb => s}
64
import scala.tasty.Reflection
75
import scala.tasty.file.TastyConsumer

semanticdb/src/dotty/semanticdb/Utils.scala

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package dotty.semanticdb
22

33

4-
import scala.tasty.Reflection
54
import scala.tasty.file._
65
import scala.collection.mutable.HashMap
76

@@ -11,7 +10,6 @@ import java.nio.file._
1110
import scala.meta.internal.{semanticdb => s}
1211
import scala.collection.JavaConverters._
1312
import java.io.File
14-
import scala.tasty.Reflection
1513
import scala.tasty.file.TastyConsumer
1614
import java.lang.reflect.InvocationTargetException
1715

0 commit comments

Comments
 (0)