Skip to content

Commit edf718b

Browse files
julienrfKordyjan
authored andcommitted
Set missing expansion span for copied inlined node
Check if the copied inlined node's expansion exists, and if not set it to the original node's expansion [Cherry-picked f22f9db]
1 parent f93adb5 commit edf718b

File tree

7 files changed

+17
-10
lines changed

7 files changed

+17
-10
lines changed

compiler/src/dotty/tools/dotc/ast/Trees.scala

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1333,10 +1333,17 @@ object Trees {
13331333
case tree: SeqLiteral if (elems eq tree.elems) && (elemtpt eq tree.elemtpt) => tree
13341334
case _ => finalize(tree, untpd.SeqLiteral(elems, elemtpt)(sourceFile(tree)))
13351335
}
1336-
def Inlined(tree: Tree)(call: tpd.Tree, bindings: List[MemberDef], expansion: Tree)(using Context): Inlined = tree match {
1337-
case tree: Inlined if (call eq tree.call) && (bindings eq tree.bindings) && (expansion eq tree.expansion) => tree
1338-
case _ => finalize(tree, untpd.Inlined(call, bindings, expansion)(sourceFile(tree)))
1339-
}
1336+
// Positions of trees are automatically pushed down except when we reach an Inlined tree. Therefore, we
1337+
// make sure the new expansion has a position by copying the one of the original Inlined tree.
1338+
def Inlined(tree: Inlined)(call: tpd.Tree, bindings: List[MemberDef], expansion: Tree)(using Context): Inlined =
1339+
if (call eq tree.call) && (bindings eq tree.bindings) && (expansion eq tree.expansion) then tree
1340+
else
1341+
// Copy the span from the original Inlined tree if the new expansion doesn't have a span.
1342+
val expansionWithSpan =
1343+
if expansion.span.exists then expansion
1344+
else expansion.withSpan(tree.expansion.span)
1345+
finalize(tree, untpd.Inlined(call, bindings, expansionWithSpan)(sourceFile(tree)))
1346+
13401347
def Quote(tree: Tree)(body: Tree, tags: List[Tree])(using Context): Quote = tree match {
13411348
case tree: Quote if (body eq tree.body) && (tags eq tree.tags) => tree
13421349
case _ => finalize(tree, untpd.Quote(body, tags)(sourceFile(tree)))

compiler/src/dotty/tools/dotc/ast/tpd.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -745,7 +745,7 @@ object tpd extends Trees.Instance[Type] with TypedTreeInfo {
745745
}
746746
}
747747

748-
override def Inlined(tree: Tree)(call: Tree, bindings: List[MemberDef], expansion: Tree)(using Context): Inlined = {
748+
override def Inlined(tree: Inlined)(call: Tree, bindings: List[MemberDef], expansion: Tree)(using Context): Inlined = {
749749
val tree1 = untpdCpy.Inlined(tree)(call, bindings, expansion)
750750
tree match {
751751
case tree: Inlined if sameTypes(bindings, tree.bindings) && (expansion.tpe eq tree.expansion.tpe) =>

compiler/src/dotty/tools/dotc/inlines/Inlines.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ object Inlines:
117117
case Block(stats, expr) =>
118118
bindings ++= stats.map(liftPos)
119119
liftBindings(expr, liftPos)
120-
case Inlined(call, stats, expr) =>
120+
case tree @ Inlined(call, stats, expr) =>
121121
bindings ++= stats.map(liftPos)
122122
val lifter = liftFromInlined(call)
123123
cpy.Inlined(tree)(call, Nil, liftBindings(expr, liftFromInlined(call).transform(_)))

compiler/src/dotty/tools/dotc/transform/BetaReduce.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ object BetaReduce:
8282
case _ => None
8383
case Block(stats, expr) if stats.forall(isPureBinding) =>
8484
recur(expr, argss).map(cpy.Block(fn)(stats, _))
85-
case Inlined(call, bindings, expr) if bindings.forall(isPureBinding) =>
85+
case fn @ Inlined(call, bindings, expr) if bindings.forall(isPureBinding) =>
8686
recur(expr, argss).map(cpy.Inlined(fn)(call, bindings, _))
8787
case Typed(expr, tpt) =>
8888
recur(expr, argss)

compiler/src/dotty/tools/dotc/transform/PickleQuotes.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ class PickleQuotes extends MacroTransform {
115115
holeContents += content
116116
val holeType = getPicklableHoleType(tree.tpe, stagedClasses)
117117
val hole = untpd.cpy.Hole(tree)(content = EmptyTree).withType(holeType)
118-
cpy.Inlined(tree)(EmptyTree, Nil, hole)
118+
Inlined(EmptyTree, Nil, hole).withSpan(tree.span)
119119
case tree: DefTree =>
120120
if tree.symbol.isClass then
121121
stagedClasses += tree.symbol

compiler/src/dotty/tools/dotc/transform/PostTyper.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ class PostTyper extends MacroTransform with IdentityDenotTransformer { thisPhase
246246

247247
private object dropInlines extends TreeMap {
248248
override def transform(tree: Tree)(using Context): Tree = tree match {
249-
case Inlined(call, _, expansion) =>
249+
case tree @ Inlined(call, _, expansion) =>
250250
val newExpansion = PruneErasedDefs.trivialErasedTree(tree)
251251
cpy.Inlined(tree)(call, Nil, newExpansion)
252252
case _ => super.transform(tree)

compiler/src/scala/quoted/runtime/impl/QuotesImpl.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1007,7 +1007,7 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
10071007
def apply(call: Option[Tree], bindings: List[Definition], expansion: Term): Inlined =
10081008
withDefaultPos(tpd.Inlined(call.getOrElse(tpd.EmptyTree), bindings.map { case b: tpd.MemberDef => b }, xCheckMacroValidExpr(expansion)))
10091009
def copy(original: Tree)(call: Option[Tree], bindings: List[Definition], expansion: Term): Inlined =
1010-
tpd.cpy.Inlined(original)(call.getOrElse(tpd.EmptyTree), bindings.asInstanceOf[List[tpd.MemberDef]], xCheckMacroValidExpr(expansion))
1010+
tpd.Inlined(call.getOrElse(tpd.EmptyTree), bindings.asInstanceOf[List[tpd.MemberDef]], xCheckMacroValidExpr(expansion)).withSpan(original.span).withType(original.tpe)
10111011
def unapply(x: Inlined): (Option[Tree /* Term | TypeTree */], List[Definition], Term) =
10121012
(optional(x.call), x.bindings, x.body)
10131013
end Inlined

0 commit comments

Comments
 (0)