Skip to content

Fix #4947: Do not replace positions of inlined arguments #4949

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 19 commits into from
Aug 24, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions compiler/src/dotty/tools/dotc/typer/Inliner.scala
Original file line number Diff line number Diff line change
Expand Up @@ -149,12 +149,26 @@ object Inliner {

/** Replace `Inlined` node by a block that contains its bindings and expansion */
def dropInlined(inlined: tpd.Inlined)(implicit ctx: Context): Tree = {
val reposition = new TreeMap {
override def transform(tree: Tree)(implicit ctx: Context): Tree = {
super.transform(tree).withPos(inlined.call.pos)
if (enclosingInlineds.nonEmpty) inlined // remove in the outer inlined call
else {
val reposition = new TreeMap {
private[this] var pos: Position = inlined.pos
override def transform(tree: Tree)(implicit ctx: Context): Tree = {
tree match {
case inlined: Inlined =>
val last = pos
pos = inlined.call.pos
val res = tpd.seq(inlined.bindings.map(transform), transform(inlined.expansion)).withPos(last)
pos = last
res
case tree =>
if (pos.exists) super.transform(tree).withPos(pos)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nicolasstucki says !pos.exists is only possible if inlined.call is emptyTree, which means that this is not a call to an inline method but an argument to it — and then bindings should be empty.
We should update docs for Inlined about this.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does not look like the right algorithm. We have enclosingInlineds, which is interpreted as follows:

  • An Inlined node with a non-empty call is a plain inlined node
  • An inlined node with an empty tree as call in an inlined argument which cancels the enclosing inlined call.

So one should start with the outermost InlinedCall and update all positions in its expansion to the call position, except in those regions where there are as many empty as non-empty calls in enclosingInlineds.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That sounds like the algorithm I implemented though I use the pos state to know if it has been cancelled or not. I will change it to use enclosingInlineds directly.

else super.transform(tree)
}
}
}
reposition.transform(inlined)
}
tpd.seq(inlined.bindings, reposition.transform(inlined.expansion))
}
}

Expand Down
2 changes: 2 additions & 0 deletions tests/run/i4947.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
failed
Test$.main(i4947.scala:9)
18 changes: 18 additions & 0 deletions tests/run/i4947.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
object Test {

transparent def track[T](f: => T): T = f

def main(args: Array[String]): Unit = {
try {
track {
val a = 9
throw new Exception("failed")
}
} catch {
case ex: Throwable =>
println(ex.getMessage)
println(ex.getStackTrace.head)
}
}

}