Skip to content

Commit 0ff3fd7

Browse files
committed
Add more tests
1 parent 10ce2e8 commit 0ff3fd7

File tree

1 file changed

+109
-0
lines changed

1 file changed

+109
-0
lines changed

compiler/test/dotty/tools/backend/jvm/InlineBytecodeTests.scala

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,4 +153,113 @@ class InlineBytecodeTests extends DottyBytecodeTest {
153153

154154
}
155155
}
156+
157+
@Test def i4947c = {
158+
val source = """class Foo {
159+
| transparent def track2[T](f: => T): T = {
160+
| println("tracking2") // line 3
161+
| f // line 4
162+
| }
163+
| transparent def track[T](f: => T): T = {
164+
| track2 { // line 7
165+
| println("fgh") // line 8
166+
| f // line 9
167+
| }
168+
| }
169+
| def main(args: Array[String]): Unit = { // line 12
170+
| track { // line 13
171+
| println("abc") // line 14
172+
| }
173+
| }
174+
|}
175+
""".stripMargin
176+
177+
checkBCode(source) { dir =>
178+
val clsIn = dir.lookupName("Foo.class", directory = false).input
179+
val clsNode = loadClassNode(clsIn, skipDebugInfo = false)
180+
181+
val track = clsNode.methods.asScala.find(_.name == "track")
182+
assert(track.isEmpty, "method `track` should have been erased")
183+
184+
val main = getMethod(clsNode, "main")
185+
val instructions = instructionsFromMethod(main)
186+
val expected =
187+
List(
188+
Label(0),
189+
LineNumber(12, Label(0)), // Position of the method start
190+
LineNumber(13, Label(0)), // Position of the call to `track`
191+
Field(GETSTATIC, "scala/Predef$", "MODULE$", "Lscala/Predef$;"),
192+
Ldc(LDC, "tracking2"),
193+
Invoke(INVOKEVIRTUAL, "scala/Predef$", "println", "(Ljava/lang/Object;)V", false),
194+
Field(GETSTATIC, "scala/Predef$", "MODULE$", "Lscala/Predef$;"),
195+
Ldc(LDC, "fgh"),
196+
Invoke(INVOKEVIRTUAL, "scala/Predef$", "println","(Ljava/lang/Object;)V", false),
197+
Label(9),
198+
LineNumber(14, Label(9)), // Actual position
199+
Field(GETSTATIC, "scala/Predef$", "MODULE$", "Lscala/Predef$;"),
200+
Ldc(LDC, "abc"),
201+
Invoke(INVOKEVIRTUAL, "scala/Predef$", "println","(Ljava/lang/Object;)V", false),
202+
Op(RETURN),
203+
Label(15)
204+
)
205+
assert(instructions == expected,
206+
"`track` was not properly inlined in `main`\n" + diffInstructions(instructions, expected))
207+
208+
}
209+
}
210+
211+
@Test def i4947d = {
212+
val source = """class Foo {
213+
| transparent def track2[T](f: => T): T = {
214+
| println("tracking2") // line 3
215+
| f // line 4
216+
| }
217+
| transparent def track[T](f: => T): T = {
218+
| track2 { // line 7
219+
| track2 { // line 8
220+
| f // line 9
221+
| }
222+
| }
223+
| }
224+
| def main(args: Array[String]): Unit = { // line 13
225+
| track { // line 14
226+
| println("abc") // line 15
227+
| }
228+
| }
229+
|}
230+
""".stripMargin
231+
232+
checkBCode(source) { dir =>
233+
val clsIn = dir.lookupName("Foo.class", directory = false).input
234+
val clsNode = loadClassNode(clsIn, skipDebugInfo = false)
235+
236+
val track = clsNode.methods.asScala.find(_.name == "track")
237+
assert(track.isEmpty, "method `track` should have been erased")
238+
239+
val main = getMethod(clsNode, "main")
240+
val instructions = instructionsFromMethod(main)
241+
val expected =
242+
List(
243+
Label(0),
244+
LineNumber(13, Label(0)), // Position of the method start
245+
LineNumber(14, Label(0)), // Position of the call to `track`
246+
Field(GETSTATIC, "scala/Predef$", "MODULE$", "Lscala/Predef$;"),
247+
Ldc(LDC, "tracking2"),
248+
Invoke(INVOKEVIRTUAL, "scala/Predef$", "println", "(Ljava/lang/Object;)V", false),
249+
Field(GETSTATIC, "scala/Predef$", "MODULE$", "Lscala/Predef$;"),
250+
Ldc(LDC, "tracking2"),
251+
Invoke(INVOKEVIRTUAL, "scala/Predef$", "println", "(Ljava/lang/Object;)V", false),
252+
Label(9),
253+
LineNumber(15, Label(9)), // Actual position
254+
Field(GETSTATIC, "scala/Predef$", "MODULE$", "Lscala/Predef$;"),
255+
Ldc(LDC, "abc"),
256+
Invoke(INVOKEVIRTUAL, "scala/Predef$", "println","(Ljava/lang/Object;)V", false),
257+
Op(RETURN),
258+
Label(15)
259+
)
260+
assert(instructions == expected,
261+
"`track` was not properly inlined in `main`\n" + diffInstructions(instructions, expected))
262+
263+
}
264+
}
156265
}

0 commit comments

Comments
 (0)