Skip to content

Commit 4b67b34

Browse files
Set missing expansion span for copied inlined node (#18229)
Check if the copied inlined node's expansion exists, and if not set it to the original node's expansion. This commit was exctracted from #17055. This simplifies the next step that refactors inlined nodes to keep them until the backend phase.
2 parents 182331b + f22f9db commit 4b67b34

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
@@ -1344,10 +1344,17 @@ object Trees {
13441344
case tree: SeqLiteral if (elems eq tree.elems) && (elemtpt eq tree.elemtpt) => tree
13451345
case _ => finalize(tree, untpd.SeqLiteral(elems, elemtpt)(sourceFile(tree)))
13461346
}
1347-
def Inlined(tree: Tree)(call: tpd.Tree, bindings: List[MemberDef], expansion: Tree)(using Context): Inlined = tree match {
1348-
case tree: Inlined if (call eq tree.call) && (bindings eq tree.bindings) && (expansion eq tree.expansion) => tree
1349-
case _ => finalize(tree, untpd.Inlined(call, bindings, expansion)(sourceFile(tree)))
1350-
}
1347+
// Positions of trees are automatically pushed down except when we reach an Inlined tree. Therefore, we
1348+
// make sure the new expansion has a position by copying the one of the original Inlined tree.
1349+
def Inlined(tree: Inlined)(call: tpd.Tree, bindings: List[MemberDef], expansion: Tree)(using Context): Inlined =
1350+
if (call eq tree.call) && (bindings eq tree.bindings) && (expansion eq tree.expansion) then tree
1351+
else
1352+
// Copy the span from the original Inlined tree if the new expansion doesn't have a span.
1353+
val expansionWithSpan =
1354+
if expansion.span.exists then expansion
1355+
else expansion.withSpan(tree.expansion.span)
1356+
finalize(tree, untpd.Inlined(call, bindings, expansionWithSpan)(sourceFile(tree)))
1357+
13511358
def Quote(tree: Tree)(body: Tree, tags: List[Tree])(using Context): Quote = tree match {
13521359
case tree: Quote if (body eq tree.body) && (tags eq tree.tags) => tree
13531360
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
@@ -748,7 +748,7 @@ object tpd extends Trees.Instance[Type] with TypedTreeInfo {
748748
}
749749
}
750750

751-
override def Inlined(tree: Tree)(call: Tree, bindings: List[MemberDef], expansion: Tree)(using Context): Inlined = {
751+
override def Inlined(tree: Inlined)(call: Tree, bindings: List[MemberDef], expansion: Tree)(using Context): Inlined = {
752752
val tree1 = untpdCpy.Inlined(tree)(call, bindings, expansion)
753753
tree match {
754754
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
@@ -250,7 +250,7 @@ class PostTyper extends MacroTransform with InfoTransformer { thisPhase =>
250250

251251
private object dropInlines extends TreeMap {
252252
override def transform(tree: Tree)(using Context): Tree = tree match {
253-
case Inlined(call, _, expansion) =>
253+
case tree @ Inlined(call, _, expansion) =>
254254
val newExpansion = PruneErasedDefs.trivialErasedTree(tree)
255255
cpy.Inlined(tree)(call, Nil, newExpansion)
256256
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
@@ -1009,7 +1009,7 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
10091009
def apply(call: Option[Tree], bindings: List[Definition], expansion: Term): Inlined =
10101010
withDefaultPos(tpd.Inlined(call.getOrElse(tpd.EmptyTree), bindings.map { case b: tpd.MemberDef => b }, xCheckMacroValidExpr(expansion)))
10111011
def copy(original: Tree)(call: Option[Tree], bindings: List[Definition], expansion: Term): Inlined =
1012-
tpd.cpy.Inlined(original)(call.getOrElse(tpd.EmptyTree), bindings.asInstanceOf[List[tpd.MemberDef]], xCheckMacroValidExpr(expansion))
1012+
tpd.Inlined(call.getOrElse(tpd.EmptyTree), bindings.asInstanceOf[List[tpd.MemberDef]], xCheckMacroValidExpr(expansion)).withSpan(original.span).withType(original.tpe)
10131013
def unapply(x: Inlined): (Option[Tree /* Term | TypeTree */], List[Definition], Term) =
10141014
(optional(x.call), x.bindings, x.body)
10151015
end Inlined

0 commit comments

Comments
 (0)