Skip to content

Commit b2a78ea

Browse files
julienrfTetchki
authored andcommitted
Fix position issues at the “copy” level
1 parent b300537 commit b2a78ea

File tree

8 files changed

+20
-18
lines changed

8 files changed

+20
-18
lines changed

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

Lines changed: 12 additions & 5 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,7 +1556,7 @@ 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) =>
1559+
case tree @ Inlined(call, bindings, expansion) =>
15531560
cpy.Inlined(tree)(call, transformSub(bindings), transform(expansion)(using inlineContext(call)))
15541561
case TypeTree() =>
15551562
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
@@ -127,7 +127,7 @@ object Inlines:
127127
case Block(stats, expr) =>
128128
bindings ++= stats.map(liftPos)
129129
liftBindings(expr, liftPos)
130-
case Inlined(call, stats, expr) =>
130+
case tree @ Inlined(call, stats, expr) =>
131131
bindings ++= stats.map(liftPos)
132132
val lifter = liftFromInlined(call)
133133
cpy.Inlined(tree)(call, Nil, liftBindings(expr, liftFromInlined(call).transform(_)))

compiler/src/dotty/tools/dotc/quoted/PickledQuotes.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ 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))
84+
val unpickled = withMode(Mode.ReadPositions)(unpickle(pickled, isType = false)).asInstanceOf[Inlined]
8585
val Inlined(call, Nil, expansion) = unpickled: @unchecked
8686
val inlineCtx = inlineContext(call)
8787
val expansion1 = spliceTypes(expansion, typeHole)(using inlineCtx)

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/Erasure.scala

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -637,7 +637,7 @@ object Erasure {
637637
if (tree.typeOpt.isRef(defn.UnitClass))
638638
tree.withType(tree.typeOpt)
639639
else if (tree.const.tag == Constants.ClazzTag)
640-
checkNotErasedClass(clsOf(tree.const.typeValue).withSpan(tree.span))
640+
checkNotErasedClass(clsOf(tree.const.typeValue))
641641
else
642642
super.typedLiteral(tree)
643643

@@ -895,11 +895,6 @@ object Erasure {
895895
tree.typeOpt
896896
else valueErasure(tree.typeOpt)
897897

898-
override def typedInlined(tree: untpd.Inlined, pt: Type)(using Context): Tree =
899-
super.typedInlined(tree, pt) match {
900-
case tree: Inlined => tree //Inlines.dropInlined(tree)
901-
}
902-
903898
override def typedValDef(vdef: untpd.ValDef, sym: Symbol)(using Context): Tree =
904899
if (sym.isEffectivelyErased) erasedDef(sym)
905900
else

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 !call.isEmpty =>
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.cpy.Inlined(original.asInstanceOf[Inlined])(call.getOrElse(tpd.EmptyTree), bindings.asInstanceOf[List[tpd.MemberDef]], xCheckMacroValidExpr(expansion))
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)