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 18 commits
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
32 changes: 28 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,36 @@ 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 most inlined call
else {
val inlinedAtPos = inlined.call.pos

/** Removes all Inlined trees, replacing them with blocks.
* Repositions all trees directly inside an inlined expansion of a non empty call to the position of the call.
* Any tree directly inside an empty call (inlined in the inlined code) retains their position.
*/
class Reposition extends TreeMap {
override def transform(tree: Tree)(implicit ctx: Context): Tree = {
tree match {
case tree: Inlined => transformInline(tree)
case _ =>
val transformed = super.transform(tree)
enclosingInlineds match {
case call :: _ if call.symbol.sourceFile != ctx.owner.sourceFile =>
// Until we implement JSR-45, we cannot represent in output positions in other source files.
// So, reposition inlined code from other files with the call position:
transformed.withPos(inlinedAtPos)
case _ => transformed
}
}
}
def transformInline(tree: tpd.Inlined)(implicit ctx: Context): Tree = {
tpd.seq(transformSub(tree.bindings), transform(tree.expansion)(inlineContext(tree.call)))
}
}

(new Reposition).transformInline(inlined)
}
tpd.seq(inlined.bindings, reposition.transform(inlined.expansion))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -335,4 +335,5 @@ class TestBCode extends DottyBytecodeTest {
assert(!fooInvoke, "foo should not be called\n")
}
}

}
244 changes: 244 additions & 0 deletions compiler/test/dotty/tools/backend/jvm/InlineBytecodeTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ package dotty.tools.backend.jvm
import org.junit.Assert._
import org.junit.Test

import scala.tools.asm.Opcodes._

import scala.collection.JavaConverters._

class InlineBytecodeTests extends DottyBytecodeTest {
import ASMConverters._
@Test def inlineUnit = {
Expand Down Expand Up @@ -37,4 +41,244 @@ class InlineBytecodeTests extends DottyBytecodeTest {
diffInstructions(instructions2, instructions3))
}
}

@Test def i4947 = {
val source = """class Foo {
| transparent def track[T](f: => T): T = {
| foo("tracking") // line 3
| f // line 4
| }
| def main(args: Array[String]): Unit = { // line 6
| track { // line 7
| foo("abc") // line 8
| track { // line 9
| foo("inner") // line 10
| }
| } // line 11
| }
| def foo(str: String): Unit = ()
|}
""".stripMargin

checkBCode(source) { dir =>
val clsIn = dir.lookupName("Foo.class", directory = false).input
val clsNode = loadClassNode(clsIn, skipDebugInfo = false)

val track = clsNode.methods.asScala.find(_.name == "track")
assert(track.isEmpty, "method `track` should have been erased")

val main = getMethod(clsNode, "main")
val instructions = instructionsFromMethod(main)
val expected =
List(
Label(0),
LineNumber(6, Label(0)),
LineNumber(3, Label(0)),
VarOp(ALOAD, 0),
Ldc(LDC, "tracking"),
Invoke(INVOKEVIRTUAL, "Foo", "foo", "(Ljava/lang/String;)V", false),
Label(6),
LineNumber(8, Label(6)),
VarOp(ALOAD, 0),
Ldc(LDC, "abc"),
Invoke(INVOKEVIRTUAL, "Foo", "foo", "(Ljava/lang/String;)V", false),
Label(11),
LineNumber(3, Label(11)),
VarOp(ALOAD, 0),
Ldc(LDC, "tracking"),
Invoke(INVOKEVIRTUAL, "Foo", "foo", "(Ljava/lang/String;)V", false),
Label(16),
LineNumber(10, Label(16)),
VarOp(ALOAD, 0),
Ldc(LDC, "inner"),
Invoke(INVOKEVIRTUAL, "Foo", "foo", "(Ljava/lang/String;)V", false),
Op(RETURN),
Label(22)
)
assert(instructions == expected,
"`track` was not properly inlined in `main`\n" + diffInstructions(instructions, expected))

}
}

@Test def i4947b = {
val source = """class Foo {
| transparent def track2[T](f: => T): T = {
| foo("tracking2") // line 3
| f // line 4
| }
| transparent def track[T](f: => T): T = {
| foo("tracking") // line 7
| track2 { // line 8
| f // line 9
| }
| }
| def main(args: Array[String]): Unit = { // line 12
| track { // line 13
| foo("abc") // line 14
| }
| }
| def foo(str: String): Unit = ()
|}
""".stripMargin

checkBCode(source) { dir =>
val clsIn = dir.lookupName("Foo.class", directory = false).input
val clsNode = loadClassNode(clsIn, skipDebugInfo = false)

val track = clsNode.methods.asScala.find(_.name == "track")
assert(track.isEmpty, "method `track` should have been erased")

val track2 = clsNode.methods.asScala.find(_.name == "track2")
assert(track2.isEmpty, "method `track2` should have been erased")

val main = getMethod(clsNode, "main")
val instructions = instructionsFromMethod(main)
val expected =
List(
Label(0),
LineNumber(12, Label(0)),
LineNumber(7, Label(0)),
VarOp(ALOAD, 0),
Ldc(LDC, "tracking"),
Invoke(INVOKEVIRTUAL, "Foo", "foo", "(Ljava/lang/String;)V", false),
Label(6),
LineNumber(3, Label(6)),
VarOp(ALOAD, 0),
Ldc(LDC, "tracking2"),
Invoke(INVOKEVIRTUAL, "Foo", "foo", "(Ljava/lang/String;)V", false),
Label(11),
LineNumber(14, Label(11)),
VarOp(ALOAD, 0),
Ldc(LDC, "abc"),
Invoke(INVOKEVIRTUAL, "Foo", "foo", "(Ljava/lang/String;)V", false),
Op(RETURN),
Label(17)
)
assert(instructions == expected,
"`track` was not properly inlined in `main`\n" + diffInstructions(instructions, expected))

}
}

@Test def i4947c = {
val source = """class Foo {
| transparent def track2[T](f: => T): T = {
| foo("tracking2") // line 3
| f // line 4
| }
| transparent def track[T](f: => T): T = {
| track2 { // line 7
| foo("fgh") // line 8
| f // line 9
| }
| }
| def main(args: Array[String]): Unit = { // line 12
| track { // line 13
| foo("abc") // line 14
| }
| }
| def foo(str: String): Unit = ()
|}
""".stripMargin

checkBCode(source) { dir =>
val clsIn = dir.lookupName("Foo.class", directory = false).input
val clsNode = loadClassNode(clsIn, skipDebugInfo = false)

val track = clsNode.methods.asScala.find(_.name == "track")
assert(track.isEmpty, "method `track` should have been erased")

val track2 = clsNode.methods.asScala.find(_.name == "track2")
assert(track2.isEmpty, "method `track2` should have been erased")

val main = getMethod(clsNode, "main")
val instructions = instructionsFromMethod(main)
val expected =
List(
Label(0),
LineNumber(12, Label(0)),
LineNumber(3, Label(0)),
VarOp(ALOAD, 0),
Ldc(LDC, "tracking2"),
Invoke(INVOKEVIRTUAL, "Foo", "foo", "(Ljava/lang/String;)V", false),
Label(6),
LineNumber(8, Label(6)),
VarOp(ALOAD, 0),
Ldc(LDC, "fgh"),
Invoke(INVOKEVIRTUAL, "Foo", "foo", "(Ljava/lang/String;)V", false),
Label(11),
LineNumber(14, Label(11)),
VarOp(ALOAD, 0),
Ldc(LDC, "abc"),
Invoke(INVOKEVIRTUAL, "Foo", "foo", "(Ljava/lang/String;)V", false),
Op(RETURN),
Label(17)
)
assert(instructions == expected,
"`track` was not properly inlined in `main`\n" + diffInstructions(instructions, expected))

}
}

@Test def i4947d = {
val source = """class Foo {
| transparent def track2[T](f: => T): T = {
| foo("tracking2") // line 3
| f // line 4
| }
| transparent def track[T](f: => T): T = {
| track2 { // line 7
| track2 { // line 8
| f // line 9
| }
| }
| }
| def main(args: Array[String]): Unit = { // line 13
| track { // line 14
| foo("abc") // line 15
| }
| }
| def foo(str: String): Unit = ()
|}
""".stripMargin

checkBCode(source) { dir =>
val clsIn = dir.lookupName("Foo.class", directory = false).input
val clsNode = loadClassNode(clsIn, skipDebugInfo = false)

val track = clsNode.methods.asScala.find(_.name == "track")
assert(track.isEmpty, "method `track` should have been erased")

val track2 = clsNode.methods.asScala.find(_.name == "track2")
assert(track2.isEmpty, "method `track2` should have been erased")

val main = getMethod(clsNode, "main")
val instructions = instructionsFromMethod(main)
val expected =
List(
Label(0),
LineNumber(13, Label(0)),
LineNumber(3, Label(0)),
VarOp(ALOAD, 0),
Ldc(LDC, "tracking2"),
Invoke(INVOKEVIRTUAL, "Foo", "foo", "(Ljava/lang/String;)V", false),
Label(6),
LineNumber(3, Label(6)),
VarOp(ALOAD, 0),
Ldc(LDC, "tracking2"),
Invoke(INVOKEVIRTUAL, "Foo", "foo", "(Ljava/lang/String;)V", false),
Label(11),
LineNumber(15, Label(11)),
VarOp(ALOAD, 0),
Ldc(LDC, "abc"),
Invoke(INVOKEVIRTUAL, "Foo", "foo", "(Ljava/lang/String;)V", false),
Op(RETURN),
Label(17)
)
assert(instructions == expected,
"`track` was not properly inlined in `main`\n" + diffInstructions(instructions, expected))

}
}
}
4 changes: 4 additions & 0 deletions tests/run/i4947.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
track: Test$.main(i4947.scala:4)
Copy link
Contributor

Choose a reason for hiding this comment

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

These testcases are good and go beyond #4947.

track: Test$.main(i4947.scala:5)
main1: Test$.main(i4947.scala:15)
main2: Test$.main(i4947.scala:16)
20 changes: 20 additions & 0 deletions tests/run/i4947.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
object Test {

transparent def track[T](f: => T): T = {
printStack("track")
printStack("track")
f
}

def printStack(tag: String): Unit = {
println(tag + ": "+ new Exception().getStackTrace().apply(1))
}

def main(args: Array[String]): Unit = {
track {
printStack("main1")
printStack("main2")
}
}

}
14 changes: 14 additions & 0 deletions tests/run/i4947a.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
track (i = 0): Test$.main(i4947a.scala:4)
track (i = 0): Test$.main(i4947a.scala:5)
track (i = 2): Test$.main(i4947a.scala:4)
track (i = 2): Test$.main(i4947a.scala:5)
main1 (i = -1): Test$.main(i4947a.scala:21)
main2 (i = -1): Test$.main(i4947a.scala:22)
track (i = 1): Test$.main(i4947a.scala:4)
track (i = 1): Test$.main(i4947a.scala:5)
main1 (i = -1): Test$.main(i4947a.scala:21)
main2 (i = -1): Test$.main(i4947a.scala:22)
track (i = 0): Test$.main(i4947a.scala:4)
track (i = 0): Test$.main(i4947a.scala:5)
main1 (i = -1): Test$.main(i4947a.scala:21)
main2 (i = -1): Test$.main(i4947a.scala:22)
27 changes: 27 additions & 0 deletions tests/run/i4947a.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
object Test {

transparent def fact[T](transparent i: Int)(f: => T): Int = {
printStack("track", i)
printStack("track", i)
f
if (i == 0)
1
else {
i * fact(i-1)(f)
}
}

def printStack(tag: String, i: Int): Unit = {
println(s"$tag (i = $i): ${new Exception().getStackTrace().apply(1)}")
}

def main(args: Array[String]): Unit = {
fact(0) {
fact(2) {
printStack("main1", -1)
printStack("main2", -1)
}
}
}

}
Loading