Skip to content

Commit 2f8f05d

Browse files
committed
Fix issue with macro results having unexpected source switches
Problem seems to have been derived from the fact that we would only remap the source of the outermost tree inserted in a Hole, instead of going through the whole tree inserted there. This meant that when inserting `Block` with `Imports` in the contents, the Block node would have a new source assigned (causing a source change encoded in the TASTy), and all contents would still have the previous source assigned (causing more source changes encoded). Now we reassign sources to nodes recursively, meaning that only the outermost node has a source change assigned in TASTy.
1 parent 09d64c6 commit 2f8f05d

File tree

3 files changed

+25
-2
lines changed

3 files changed

+25
-2
lines changed

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,12 +111,18 @@ object PickledQuotes {
111111
case ExprHole.V2(evalHole) =>
112112
evalHole.nn.apply(idx, reifyExprHoleV2Args(args), QuotesImpl())
113113

114+
val sourceReassigner = new TreeMapWithImplicits {
115+
def transform(innerTree: tpd.Tree) = {
116+
if innerTree.source == ctx.source then innerTree
117+
else innerTree.cloneIn(ctx.source).withSpan(tree.span)
118+
}
119+
}
120+
114121
val filled = PickledQuotes.quotedExprToTree(quotedExpr)
115122

116123
// We need to make sure a hole is created with the source file of the surrounding context, even if
117124
// it filled with contents a different source file.
118-
if filled.source == ctx.source then filled
119-
else filled.cloneIn(ctx.source).withSpan(tree.span)
125+
sourceReassigner.transform(filled)
120126
else
121127
// For backwards compatibility with 3.0.x and 3.1.x
122128
// In 3.2.0+ all these holes are handled by `spliceTypes` before we call `spliceTerms`.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import scala.quoted._
2+
3+
object TestBuilder:
4+
// transparent is needed
5+
transparent inline def apply(inline expr: Unit): Any =
6+
${ TestBuilder.processTests('expr) }
7+
8+
def processTests(using Quotes)(body: Expr[Unit]): Expr[Any] =
9+
import quotes.reflect._
10+
body.asTerm match {
11+
case Inlined(_, _, bindings) =>
12+
'{ ${bindings.asExpr}; () }
13+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
object breaks {
2+
TestBuilder:
3+
import List.empty
4+
}

0 commit comments

Comments
 (0)