Skip to content

Commit f9c052c

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 f9c052c

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
@@ -115,8 +115,14 @@ object PickledQuotes {
115115

116116
// We need to make sure a hole is created with the source file of the surrounding context, even if
117117
// 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)
118+
val sourceReassigner = new TreeMapWithImplicits {
119+
override def transform(innerTree: tpd.Tree)(using Context) = {
120+
if innerTree.source == tree.source then innerTree
121+
else
122+
innerTree.cloneIn(tree.source).withSpan(tree.span)
123+
}
124+
}
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)