-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Changes from 11 commits
24b0d82
3a6b56b
21f7aa3
10ce2e8
0ff3fd7
2837365
857f53e
a11db37
a12688a
53e85d6
d52c3b2
27ab7ee
f043599
e21ae3f
cc6ca5f
dc66cce
bf1ae93
7850b1e
4c51bc3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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:
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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)) | ||
} | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -335,4 +335,5 @@ class TestBCode extends DottyBytecodeTest { | |
assert(!fooInvoke, "foo should not be called\n") | ||
} | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 = { | ||
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What about we call a new virtual method 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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)) | ||
|
||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
track: Test$.main(i4947.scala:4) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) |
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") | ||
} | ||
} | ||
|
||
} |
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) |
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)) | ||
} | ||
} |
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") | ||
} | ||
} | ||
|
||
} |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 reallyenclosingInlineds
's fault and I think can be fixed separately without affecting this code.