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 11 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 {
// Position used for any tree that was inlined (including recursive inlines)
val inlinedAtPos = inlined.call.pos

/** Removes all Inlined trees, replacing them with blocks.
* Repositions all trees directly inside an inlined expantion 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.topLevelClass != ctx.owner.topLevelClass =>
// reposition tree inlined from some other file
Copy link
Contributor

Choose a reason for hiding this comment

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

The comment makes sense, but the code is stricter, and resets the position even if the toplevel class is different but the source file is the same. In that case, it appears we don't need to reset the position. Fixing.

Copy link
Contributor

Choose a reason for hiding this comment

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

Also, this is an expensive (O(n)) way to test if enclosingInlineds.nonEmpty and get the head, and that could be expensive, but that's really enclosingInlineds's fault and I think can be fixed separately without affecting this code.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think the documentation is too complicated. We're dropping Inlined nodes, and repositioning nodes from other files to the call position because we can't express their position otherwise in their debug info, pending JSR-45. The stuff about "empty call" is confusing and expressed in terms of the implementation.

Other issues that I think are there and that I'm trying to address:

  • The comment makes sense, but the code is stricter, and resets the position even if the toplevel class is different but the source file is the same. In that case, it appears we don't need to reset the position. Addressed by one commit I pushed.

  • Also, this is an expensive (O(n)) way to test if enclosingInlineds.nonEmpty and get the head, and that could be expensive, but that's really enclosingInlineds's fault and I think can be fixed separately without affecting this code.

Copy link
Contributor

Choose a reason for hiding this comment

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

So #4990 addresses performance.

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")
}
}

}
240 changes: 240 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,240 @@ class InlineBytecodeTests extends DottyBytecodeTest {
diffInstructions(instructions2, instructions3))
}
}

@Test def i4947 = {
val source = """class Foo {
| transparent def track[T](f: => T): T = {
| println("tracking") // line 3
Copy link
Contributor

Choose a reason for hiding this comment

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

What about we call a new virtual method m1(String) in Foo, rather than println, a bit like the existing test? This test will break if any code figures that Predef$.MODULE$ needn't be reloaded.

Do these tests ever break? (Guess: yes). I don't know and I'm curious about the existing guidelines, but people maintaining them might like info on what bits matter and which don't (beyond what's already in this PR).

Nicolas mentioned he already tried this but the resulting test was more complex (which... well, ALOAD 0 seems pretty stable to me), and it's easy to fix the test by pasting the output.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Changed

| f // line 4
| }
| def main(args: Array[String]): Unit = { // line 6
| track { // line 7
| println("abc") // line 8
| track { // line 9
| println("inner") // line 10
| }
| } // line 11
| }
|}
""".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)),
Field(GETSTATIC, "scala/Predef$", "MODULE$", "Lscala/Predef$;"),
Ldc(LDC, "tracking"),
Invoke(INVOKEVIRTUAL, "scala/Predef$", "println", "(Ljava/lang/Object;)V", false),
Label(6),
LineNumber(8, Label(6)),
Field(GETSTATIC, "scala/Predef$", "MODULE$", "Lscala/Predef$;"),
Ldc(LDC, "abc"),
Invoke(INVOKEVIRTUAL, "scala/Predef$", "println","(Ljava/lang/Object;)V", false),
Label(11),
LineNumber(3, Label(11)),
Field(GETSTATIC, "scala/Predef$", "MODULE$", "Lscala/Predef$;"),
Ldc(LDC, "tracking"),
Invoke(INVOKEVIRTUAL, "scala/Predef$", "println","(Ljava/lang/Object;)V", false),
Label(16),
LineNumber(10, Label(16)),
Field(GETSTATIC, "scala/Predef$", "MODULE$", "Lscala/Predef$;"),
Ldc(LDC, "inner"),
Invoke(INVOKEVIRTUAL, "scala/Predef$", "println","(Ljava/lang/Object;)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 = {
| println("tracking2") // line 3
| f // line 4
| }
| transparent def track[T](f: => T): T = {
| println("tracking") // line 7
| track2 { // line 8
| f // line 9
| }
| }
| def main(args: Array[String]): Unit = { // line 12
| track { // line 13
| println("abc") // line 14
| }
| }
|}
""".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)),
Field(GETSTATIC, "scala/Predef$", "MODULE$", "Lscala/Predef$;"),
Ldc(LDC, "tracking"),
Invoke(INVOKEVIRTUAL, "scala/Predef$", "println", "(Ljava/lang/Object;)V", false),
Label(6),
LineNumber(3, Label(6)),
Field(GETSTATIC, "scala/Predef$", "MODULE$", "Lscala/Predef$;"),
Ldc(LDC, "tracking2"),
Invoke(INVOKEVIRTUAL, "scala/Predef$", "println","(Ljava/lang/Object;)V", false),
Label(11),
LineNumber(14, Label(11)),
Field(GETSTATIC, "scala/Predef$", "MODULE$", "Lscala/Predef$;"),
Ldc(LDC, "abc"),
Invoke(INVOKEVIRTUAL, "scala/Predef$", "println","(Ljava/lang/Object;)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 = {
| println("tracking2") // line 3
| f // line 4
| }
| transparent def track[T](f: => T): T = {
| track2 { // line 7
| println("fgh") // line 8
| f // line 9
| }
| }
| def main(args: Array[String]): Unit = { // line 12
| track { // line 13
| println("abc") // line 14
| }
| }
|}
""".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)),
Field(GETSTATIC, "scala/Predef$", "MODULE$", "Lscala/Predef$;"),
Ldc(LDC, "tracking2"),
Invoke(INVOKEVIRTUAL, "scala/Predef$", "println", "(Ljava/lang/Object;)V", false),
Label(6),
LineNumber(8, Label(6)),
Field(GETSTATIC, "scala/Predef$", "MODULE$", "Lscala/Predef$;"),
Ldc(LDC, "fgh"),
Invoke(INVOKEVIRTUAL, "scala/Predef$", "println","(Ljava/lang/Object;)V", false),
Label(11),
LineNumber(14, Label(11)),
Field(GETSTATIC, "scala/Predef$", "MODULE$", "Lscala/Predef$;"),
Ldc(LDC, "abc"),
Invoke(INVOKEVIRTUAL, "scala/Predef$", "println","(Ljava/lang/Object;)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 = {
| println("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
| println("abc") // line 15
| }
| }
|}
""".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)),
Field(GETSTATIC, "scala/Predef$", "MODULE$", "Lscala/Predef$;"),
Ldc(LDC, "tracking2"),
Invoke(INVOKEVIRTUAL, "scala/Predef$", "println", "(Ljava/lang/Object;)V", false),
Label(6),
LineNumber(3, Label(6)),
Field(GETSTATIC, "scala/Predef$", "MODULE$", "Lscala/Predef$;"),
Ldc(LDC, "tracking2"),
Invoke(INVOKEVIRTUAL, "scala/Predef$", "println", "(Ljava/lang/Object;)V", false),
Label(11),
LineNumber(15, Label(11)),
Field(GETSTATIC, "scala/Predef$", "MODULE$", "Lscala/Predef$;"),
Ldc(LDC, "abc"),
Invoke(INVOKEVIRTUAL, "scala/Predef$", "println","(Ljava/lang/Object;)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")
}
}

}
4 changes: 4 additions & 0 deletions tests/run/i4947b.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
track: Test$.main(Test_2.scala:3)
track: Test$.main(Test_2.scala:3)
main1: Test$.main(Test_2.scala:4)
main2: Test$.main(Test_2.scala:5)
10 changes: 10 additions & 0 deletions tests/run/i4947b/Lib_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
object Lib {
transparent def track[T](f: => T): T = {
printStack("track")
printStack("track")
f
}
def printStack(tag: String): Unit = {
println(tag + ": "+ new Exception().getStackTrace().apply(1))
}
}
9 changes: 9 additions & 0 deletions tests/run/i4947b/Test_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
object Test {
def main(args: Array[String]): Unit = {
Lib.track {
Lib.printStack("main1")
Lib.printStack("main2")
}
}

}