Skip to content

Commit 6c8891e

Browse files
committed
Reposition trees in erasure using source from tree
This decouples the repositioning from Inlined tree nodes
1 parent 3be62c1 commit 6c8891e

File tree

3 files changed

+24
-10
lines changed

3 files changed

+24
-10
lines changed

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

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -603,7 +603,7 @@ object Erasure {
603603
if (tree.typeOpt.isRef(defn.UnitClass))
604604
tree.withType(tree.typeOpt)
605605
else if (tree.const.tag == Constants.ClazzTag)
606-
clsOf(tree.const.typeValue)
606+
clsOf(tree.const.typeValue).withSpan(tree.span)
607607
else
608608
super.typedLiteral(tree)
609609

@@ -739,6 +739,23 @@ object Erasure {
739739
checkValue(checkNotErased(recur(qual1)), pt)
740740
}
741741

742+
// TODO track in a cleaner way
743+
private var enclosingSpan: util.Spans.Span = util.Spans.NoSpan
744+
745+
override def typed(tree: untpd.Tree, pt: Type, locked: TypeVars)(using Context): Tree =
746+
val old = enclosingSpan
747+
enclosingSpan = tree.span
748+
val tree1 =
749+
try super.typed(tree, pt, locked)
750+
finally enclosingSpan = old
751+
752+
if tree1.source.exists && ctx.source != tree1.source && ctx.source == ctx.owner.topLevelClass.source
753+
then
754+
// TODO reposition while erasing instead of retraversing
755+
Inliner.reposition(tree1, enclosingSpan)
756+
else tree1
757+
758+
742759
override def typedThis(tree: untpd.This)(using Context): Tree =
743760
if (tree.symbol == ctx.owner.lexicallyEnclosingClass || tree.symbol.isStaticOwner) promote(tree)
744761
else {

compiler/src/dotty/tools/dotc/typer/Inliner.scala

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -207,16 +207,13 @@ object Inliner {
207207

208208
/** Replace `Inlined` node by a block that contains its bindings and expansion */
209209
def dropInlined(inlined: Inlined)(using Context): Tree =
210-
val tree1 =
211-
if inlined.bindings.isEmpty then inlined.expansion
212-
else cpy.Block(inlined)(inlined.bindings, inlined.expansion)
213-
// Reposition in the outer most inlined call
214-
if (enclosingInlineds.nonEmpty) tree1 else reposition(tree1, inlined.span)
210+
if inlined.bindings.isEmpty then inlined.expansion
211+
else cpy.Block(inlined)(inlined.bindings, inlined.expansion)
215212

216213
def reposition(tree: Tree, callSpan: Span)(using Context): Tree = {
217214
// Reference test tests/run/i4947b
218215

219-
val curSource = ctx.compilationUnit.source
216+
val curSource = ctx.owner.topLevelClass.source
220217

221218
// Tree copier that changes the source of all trees to `curSource`
222219
val cpyWithNewSource = new TypedTreeCopier {

tests/run-macros/i4947e.check

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
assertImpl: Test$.main(Test_2.scala:7)
1+
assertImpl: Test$.main(Test_2.scala:3)
22
true
3-
assertImpl: Test$.main(Test_2.scala:8)
3+
assertImpl: Test$.main(Test_2.scala:3)
44
false
5-
assertImpl: Test$.main(Test_2.scala:9)
5+
assertImpl: Test$.main(Test_2.scala:3)
66
hi: Test$.main(Test_2.scala:10)
77
hi again: Test$.main(Test_2.scala:11)
88
false

0 commit comments

Comments
 (0)