Skip to content

Commit 39e2f69

Browse files
committed
Preserve implicits in Quotes context
1 parent a5a7644 commit 39e2f69

File tree

4 files changed

+62
-1
lines changed

4 files changed

+62
-1
lines changed

compiler/src/dotty/tools/dotc/quoted/PickledQuotes.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import scala.collection.mutable
2222

2323
import QuoteUtils.*
2424
import dotty.tools.io.NoAbstractFile
25+
import dotty.tools.dotc.ast.TreeMapWithImplicits
2526

2627
object PickledQuotes {
2728
import tpd.*
@@ -99,7 +100,7 @@ object PickledQuotes {
99100

100101
/** Replace all term holes with the spliced terms */
101102
private def spliceTerms(tree: Tree, typeHole: TypeHole, termHole: ExprHole)(using Context): Tree = {
102-
def evaluateHoles = new TreeMapWithPreciseStatContexts {
103+
def evaluateHoles = new TreeMapWithImplicits {
103104
override def transform(tree: tpd.Tree)(using Context): tpd.Tree = tree match {
104105
case Hole(isTerm, idx, args, _) =>
105106
inContext(SpliceScope.contextWithNewSpliceScope(tree.sourcePos)) {

tests/run-macros/i22260.check

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
42
2+
42
3+
42
4+
42
5+
42
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import scala.quoted.*
2+
3+
object Macros:
4+
inline def inMethod: Int = ${ insideMethod }
5+
inline def usMethod: Int = ${ usingMethod }
6+
inline def inObject: Int = ${ insideObject }
7+
inline def inClass: Int = ${ insideClass }
8+
inline def usClass: Int = ${ usingClass }
9+
10+
def summon(using Quotes) =
11+
Expr.summon[Int].getOrElse('{ 0 })
12+
13+
def insideMethod(using Quotes): Expr[Int] = '{
14+
def foo =
15+
given Int = 42
16+
${summon}
17+
18+
foo
19+
}
20+
21+
def usingMethod(using Quotes): Expr[Int] = '{
22+
def foo(using Int) =
23+
${summon}
24+
25+
foo(using 42)
26+
}
27+
28+
def insideObject(using Quotes): Expr[Int] = '{
29+
object Foo:
30+
given Int = 42
31+
val x = ${summon}
32+
33+
Foo.x
34+
}
35+
36+
def insideClass(using Quotes): Expr[Int] = '{
37+
class Foo:
38+
given Int = 42
39+
val x = ${summon}
40+
41+
new Foo().x
42+
}
43+
44+
def usingClass(using Quotes): Expr[Int] = '{
45+
class Foo(using Int):
46+
val x = ${summon}
47+
48+
new Foo(using 42).x
49+
}

tests/run-macros/i22260/Test_2.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
@main def Test =
2+
println(Macros.inMethod)
3+
println(Macros.usMethod)
4+
println(Macros.inObject)
5+
println(Macros.inClass)
6+
println(Macros.usClass)

0 commit comments

Comments
 (0)