Skip to content

Commit d0c39a5

Browse files
committed
Fix scala#4947: Do not replace positions of inlined arguments
1 parent 01c5eac commit d0c39a5

File tree

4 files changed

+33
-4
lines changed

4 files changed

+33
-4
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -592,6 +592,8 @@ object Trees {
592592
extends Tree[T] {
593593
type ThisTree[-T >: Untyped] = Inlined[T]
594594
override def initialPos = call.pos
595+
/** Is this an argument inlined in an inlined function call */
596+
def isInlinedArgument: Boolean = call.isEmpty
595597
}
596598

597599
/** A type tree that represents an existing or inferred type */

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

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -149,12 +149,19 @@ object Inliner {
149149

150150
/** Replace `Inlined` node by a block that contains its bindings and expansion */
151151
def dropInlined(inlined: tpd.Inlined)(implicit ctx: Context): Tree = {
152-
val reposition = new TreeMap {
153-
override def transform(tree: Tree)(implicit ctx: Context): Tree = {
154-
super.transform(tree).withPos(inlined.call.pos)
152+
if (inlined.isInlinedArgument) inlined // remove in the outer inlined call
153+
else {
154+
val reposition = new TreeMap {
155+
override def transform(tree: Tree)(implicit ctx: Context): Tree = tree match {
156+
case inlined: Inlined =>
157+
// Stop changing positions as this tree already has the correct positions
158+
assert(inlined.isInlinedArgument)
159+
tpd.seq(inlined.bindings, inlined.expansion)
160+
case tree => super.transform(tree).withPos(inlined.call.pos)
161+
}
155162
}
163+
tpd.seq(inlined.bindings, reposition.transform(inlined.expansion))
156164
}
157-
tpd.seq(inlined.bindings, reposition.transform(inlined.expansion))
158165
}
159166
}
160167

tests/run/i4947.check

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
failed
2+
Test$.main(i4947.scala:9)

tests/run/i4947.scala

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
object Test {
2+
3+
transparent def track[T](f: => T): T = f
4+
5+
def main(args: Array[String]): Unit = {
6+
try {
7+
track {
8+
val a = 9
9+
throw new Exception("failed")
10+
}
11+
} catch {
12+
case ex: Throwable =>
13+
println(ex.getMessage)
14+
println(ex.getStackTrace.head)
15+
}
16+
}
17+
18+
}

0 commit comments

Comments
 (0)