Skip to content

Fix issue with macro results having unexpected source switches in TASTy #23397

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions compiler/src/dotty/tools/dotc/quoted/PickledQuotes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,18 @@ object PickledQuotes {
case ExprHole.V2(evalHole) =>
evalHole.nn.apply(idx, reifyExprHoleV2Args(args), QuotesImpl())

val sourceReassigner = new TreeMapWithImplicits {
def transform(innerTree: tpd.Tree) = {
if innerTree.source == ctx.source then innerTree
else innerTree.cloneIn(ctx.source).withSpan(tree.span)
}
}

val filled = PickledQuotes.quotedExprToTree(quotedExpr)

// We need to make sure a hole is created with the source file of the surrounding context, even if
// it filled with contents a different source file.
if filled.source == ctx.source then filled
else filled.cloneIn(ctx.source).withSpan(tree.span)
sourceReassigner.transform(filled)
else
// For backwards compatibility with 3.0.x and 3.1.x
// In 3.2.0+ all these holes are handled by `spliceTypes` before we call `spliceTerms`.
Expand Down
13 changes: 13 additions & 0 deletions scaladoc-testcases/src/tests/i22265/macro.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import scala.quoted._

object TestBuilder:
// transparent is needed
transparent inline def apply(inline expr: Unit): Any =
${ TestBuilder.processTests('expr) }

def processTests(using Quotes)(body: Expr[Unit]): Expr[Any] =
import quotes.reflect._
body.asTerm match {
case Inlined(_, _, bindings) =>
'{ ${bindings.asExpr}; () }
}
4 changes: 4 additions & 0 deletions scaladoc-testcases/src/tests/i22265/main.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
object breaks {
TestBuilder:
import List.empty
}
Loading