Skip to content

Commit d8dce0f

Browse files
julienrfTetchki
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
1 parent 2753a17 commit d8dce0f

File tree

8 files changed

+26
-19
lines changed

8 files changed

+26
-19
lines changed

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

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1341,10 +1341,17 @@ object Trees {
13411341
case tree: SeqLiteral if (elems eq tree.elems) && (elemtpt eq tree.elemtpt) => tree
13421342
case _ => finalize(tree, untpd.SeqLiteral(elems, elemtpt)(sourceFile(tree)))
13431343
}
1344-
def Inlined(tree: Tree)(call: tpd.Tree, bindings: List[MemberDef], expansion: Tree)(using Context): Inlined = tree match {
1345-
case tree: Inlined if (call eq tree.call) && (bindings eq tree.bindings) && (expansion eq tree.expansion) => tree
1346-
case _ => finalize(tree, untpd.Inlined(call, bindings, expansion)(sourceFile(tree)))
1347-
}
1344+
// Positions of trees are automatically pushed down except when we reach an Inlined tree. Therefore, we
1345+
// make sure the new expansion has a position by copying the one of the original Inlined tree.
1346+
def Inlined(tree: Inlined)(call: tpd.Tree, bindings: List[MemberDef], expansion: Tree)(using Context): Inlined =
1347+
if (call eq tree.call) && (bindings eq tree.bindings) && (expansion eq tree.expansion) then tree
1348+
else
1349+
// Copy the span from the original Inlined tree if the new expansion doesn't have a span.
1350+
val expansionWithSpan =
1351+
if expansion.span.exists then expansion
1352+
else expansion.withSpan(tree.expansion.span)
1353+
finalize(tree, untpd.Inlined(call, bindings, expansionWithSpan)(sourceFile(tree)))
1354+
13481355
def Quote(tree: Tree)(body: Tree, tags: List[Tree])(using Context): Quote = tree match {
13491356
case tree: Quote if (body eq tree.body) && (tags eq tree.tags) => tree
13501357
case _ => finalize(tree, untpd.Quote(body, tags)(sourceFile(tree)))
@@ -1549,8 +1556,8 @@ object Trees {
15491556
cpy.Try(tree)(transform(block), transformSub(cases), transform(finalizer))
15501557
case SeqLiteral(elems, elemtpt) =>
15511558
cpy.SeqLiteral(tree)(transform(elems), transform(elemtpt))
1552-
case Inlined(call, bindings, expansion) =>
1553-
cpy.Inlined(tree)(call, transformSub(bindings), transform(expansion)(using inlineContext(call)))
1559+
case tree @ Inlined(call, bindings, expansion) =>
1560+
cpy.Inlined(tree)(call, transformSub(bindings), transform(expansion)(using inlineContext(tree)))
15541561
case TypeTree() =>
15551562
tree
15561563
case SingletonTypeTree(ref) =>

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/quoted/PickledQuotes.scala

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,12 @@ object PickledQuotes {
8181

8282
/** Unpickle the tree contained in the TastyExpr */
8383
def unpickleTerm(pickled: String | List[String], typeHole: TypeHole, termHole: ExprHole)(using Context): Tree = {
84-
val unpickled = withMode(Mode.ReadPositions)(unpickle(pickled, isType = false))
85-
val Inlined(call, Nil, expansion) = unpickled: @unchecked
86-
val inlineCtx = inlineContext(call)
87-
val expansion1 = spliceTypes(expansion, typeHole)(using inlineCtx)
88-
val expansion2 = spliceTerms(expansion1, typeHole, termHole)(using inlineCtx)
89-
cpy.Inlined(unpickled)(call, Nil, expansion2)
84+
withMode(Mode.ReadPositions)(unpickle(pickled, isType = false)) match
85+
case tree @ Inlined(call, Nil, expansion) =>
86+
val inlineCtx = inlineContext(tree)
87+
val expansion1 = spliceTypes(expansion, typeHole)(using inlineCtx)
88+
val expansion2 = spliceTerms(expansion1, typeHole, termHole)(using inlineCtx)
89+
cpy.Inlined(tree)(call, Nil, expansion2)
9090
}
9191

9292

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: 2 additions & 2 deletions
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)
@@ -363,7 +363,7 @@ class PostTyper extends MacroTransform with InfoTransformer { thisPhase =>
363363
case _ =>
364364
super.transform(tree1)
365365
}
366-
case Inlined(call, bindings, expansion) if !call.isEmpty =>
366+
case tree @ Inlined(call, bindings, expansion) if !tree.inlinedFromOuterScope =>
367367
val pos = call.sourcePos
368368
CrossVersionChecks.checkExperimentalRef(call.symbol, pos)
369369
withMode(Mode.InlinedCall)(transform(call))

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

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

0 commit comments

Comments
 (0)