1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
|
package scala.issues
import org.junit.runner.RunWith
import org.junit.runners.JUnit4
import org.junit.Test
import scala.tools.asm.Opcodes._
import org.junit.Assert._
import scala.tools.nsc.backend.jvm.{AsmUtils, CodeGenTools}
import CodeGenTools._
import scala.tools.partest.ASMConverters
import ASMConverters._
import AsmUtils._
import scala.tools.testing.ClearAfterClass
object OptimizedBytecodeTest extends ClearAfterClass.Clearable {
val args = "-Yopt:l:classpath -Yopt-warnings"
var compiler = newCompiler(extraArgs = args)
def clear(): Unit = { compiler = null }
}
@RunWith(classOf[JUnit4])
class OptimizedBytecodeTest extends ClearAfterClass {
ClearAfterClass.stateToClear = OptimizedBytecodeTest
val compiler = OptimizedBytecodeTest.compiler
@Test
def t2171(): Unit = {
val code =
"""class C {
| final def m(msg: => String) = try 0 catch { case ex: Throwable => println(msg) }
| def t(): Unit = while (true) m("...")
|}
""".stripMargin
val List(c) = compileClasses(compiler)(code)
assertSameCode(getSingleMethod(c, "t"), List(Label(0), Jump(GOTO, Label(0))))
}
@Test
def t3430(): Unit = {
val code =
"""class C {
| final def m(f: String => Boolean) = f("a")
| def t(): Boolean =
| m { s1 =>
| m { s2 =>
| while (true) { }
| true
| }
| }
|}
""".stripMargin
val List(c) = compileClasses(compiler)(code)
assertSameSummary(getSingleMethod(c, "t"), List(
LDC, ASTORE, ALOAD /*0*/, ALOAD /*1*/, "C$$$anonfun$1", IRETURN))
assertSameSummary(getSingleMethod(c, "C$$$anonfun$1"), List(LDC, "C$$$anonfun$2", IRETURN))
assertSameSummary(getSingleMethod(c, "C$$$anonfun$2"), List(-1 /*A*/, GOTO /*A*/))
}
@Test
def t3252(): Unit = {
val code =
"""class C {
| def t(x: Boolean): Thread = {
| g {
| x match {
| case false => Tat.h { }
| }
| }
| }
|
| private def g[T](block: => T) = ???
|}
|object Tat {
| def h(block: => Unit): Nothing = ???
|}
""".stripMargin
val List(c, t, tMod) = compileClasses(compiler)(code, allowMessage = _.msg.contains("not be exhaustive"))
assertSameSummary(getSingleMethod(c, "t"), List(GETSTATIC, "$qmark$qmark$qmark", ATHROW))
}
@Test
def t6157(): Unit = {
val code =
"""class C {
| def t = println(ErrorHandler.defaultIfIOException("String")("String"))
|}
|object ErrorHandler {
| import java.io.IOException
| @inline
| def defaultIfIOException[T](default: => T)(closure: => T): T = try closure catch {
| case e: IOException => default
| }
|}
""".stripMargin
val msg =
"""ErrorHandler$::defaultIfIOException(Lscala/Function0;Lscala/Function0;)Ljava/lang/Object; is annotated @inline but could not be inlined:
|The operand stack at the callsite in C::t()V contains more values than the
|arguments expected by the callee ErrorHandler$::defaultIfIOException(Lscala/Function0;Lscala/Function0;)Ljava/lang/Object;. These values would be discarded
|when entering an exception handler declared in the inlined method.""".stripMargin
compileClasses(compiler)(code, allowMessage = _.msg == msg)
}
@Test
def t6547(): Unit = { // "pos" test -- check that it compiles
val code =
"""trait ConfigurableDefault[@specialized V] {
| def fillArray(arr: Array[V], v: V) = (arr: Any) match {
| case x: Array[Int] => null
| case x: Array[Long] => v.asInstanceOf[Long]
| }
|}
""".stripMargin
compileClasses(compiler)(code)
}
@Test
def t8062(): Unit = {
val c1 =
"""package warmup
|object Warmup { def filter[A](p: Any => Boolean): Any = filter[Any](p) }
""".stripMargin
val c2 = "class C { def t = warmup.Warmup.filter[Any](x => false) }"
val List(c, _, _) = compileClassesSeparately(List(c1, c2), extraArgs = OptimizedBytecodeTest.args)
assertInvoke(getSingleMethod(c, "t"), "warmup/Warmup$", "filter")
}
@Test
def t8306(): Unit = { // "pos" test
val code =
"""class C {
| def foo: Int = 123
| lazy val extension: Int = foo match {
| case idx if idx != -1 => 15
| case _ => 17
| }
|}
""".stripMargin
compileClasses(compiler)(code)
}
@Test
def t8359(): Unit = { // "pos" test
// This is a minimization of code that crashed the compiler during bootstrapping
// in the first iteration of https://github.com/scala/scala/pull/4373, the PR
// that adjusted the order of free and declared params in LambdaLift.
// Was:
// java.lang.AssertionError: assertion failed:
// Record Record(<$anon: Function1>,Map(value a$1 -> Deref(LocalVar(value b)))) does not contain a field value b$1
// at scala.tools.nsc.Global.assert(Global.scala:262)
// at scala.tools.nsc.backend.icode.analysis.CopyPropagation$copyLattice$State.getFieldNonRecordValue(CopyPropagation.scala:113)
// at scala.tools.nsc.backend.icode.analysis.CopyPropagation$copyLattice$State.getFieldNonRecordValue(CopyPropagation.scala:122)
// at scala.tools.nsc.backend.opt.ClosureElimination$ClosureElim$$anonfun$analyzeMethod$1$$anonfun$apply$2.replaceFieldAccess$1(ClosureElimination.scala:124)
val code =
"""package test
|class Typer {
| def bar(a: Boolean, b: Boolean): Unit = {
| @inline
| def baz(): Unit = {
| ((_: Any) => (Typer.this, a, b)).apply("")
| }
| ((_: Any) => baz()).apply("")
| }
|}
""".stripMargin
compileClasses(compiler)(code)
}
@Test
def t9123(): Unit = { // "pos" test
val code =
"""trait Setting {
| type T
| def value: T
|}
|object Test {
| def test(x: Some[Setting]) = x match {
| case Some(dep) => Some(dep.value) map (_ => true)
| }
|}
""".stripMargin
compileClasses(compiler)(code)
}
@Test
def traitForceInfo(): Unit = {
// This did NOT crash unless it's in the interactive package.
// error: java.lang.AssertionError: assertion failed: trait Contexts.NoContext$ linkedModule: <none>List()
// at scala.Predef$.assert(Predef.scala:160)
// at scala.tools.nsc.symtab.classfile.ClassfileParser$innerClasses$.innerSymbol$1(ClassfileParser.scala:1211)
// at scala.tools.nsc.symtab.classfile.ClassfileParser$innerClasses$.classSymbol(ClassfileParser.scala:1223)
// at scala.tools.nsc.symtab.classfile.ClassfileParser.classNameToSymbol(ClassfileParser.scala:489)
// at scala.tools.nsc.symtab.classfile.ClassfileParser.sig2type$1(ClassfileParser.scala:757)
// at scala.tools.nsc.symtab.classfile.ClassfileParser.sig2type$1(ClassfileParser.scala:789)
val code =
"""package scala.tools.nsc
|package interactive
|
|trait MyContextTrees {
| val self: Global
| val NoContext = self.analyzer.NoContext
|}
""".stripMargin
compileClasses(compiler)(code)
}
@Test
def t9160(): Unit = {
val code =
"""class C {
| def getInt: Int = 0
| def t(trees: Object): Int = {
| trees match {
| case Some(elems) =>
| case tree => getInt
| }
| 55
| }
|}
""".stripMargin
val List(c) = compileClasses(compiler)(code)
assertSameSummary(getSingleMethod(c, "t"), List(
ALOAD /*1*/, INSTANCEOF /*Some*/, IFNE /*A*/,
ALOAD /*0*/, "getInt", POP,
-1 /*A*/, BIPUSH, IRETURN))
}
@Test
def t8796(): Unit = {
val code =
"""final class C {
| def pr(): Unit = ()
| def t(index: Int): Unit = index match {
| case 0 => pr()
| case 1 => pr()
| case _ => t(index - 2)
| }
|}
""".stripMargin
val List(c) = compileClasses(compiler)(code)
assertSameSummary(getSingleMethod(c, "t"), List(
-1 /*A*/, ILOAD /*1*/, TABLESWITCH,
-1, ALOAD, "pr", RETURN,
-1, ALOAD, "pr", RETURN,
-1, ILOAD, ICONST_2, ISUB, ISTORE, GOTO /*A*/))
}
@Test
def t8524(): Unit = {
val c1 =
"""package library
|object Library {
| @inline def pleaseInlineMe() = 1
| object Nested { @inline def pleaseInlineMe() = 2 }
|}
""".stripMargin
val c2 =
"""class C {
| def t = library.Library.pleaseInlineMe() + library.Library.Nested.pleaseInlineMe()
|}
""".stripMargin
val cls = compileClassesSeparately(List(c1, c2), extraArgs = OptimizedBytecodeTest.args)
val c = cls.find(_.name == "C").get
assertSameSummary(getSingleMethod(c, "t"), List(
GETSTATIC, IFNONNULL, ACONST_NULL, ATHROW, // module load and null checks not yet eliminated
-1, ICONST_1, GETSTATIC, IFNONNULL, ACONST_NULL, ATHROW,
-1, ICONST_2, IADD, IRETURN))
}
@Test
def privateInline(): Unit = {
val code =
"""final class C {
| private var x1 = false
| var x2 = false
|
| @inline private def wrapper1[T](body: => T): T = {
| val saved = x1
| x1 = true
| try body
| finally x1 = saved
| }
|
| @inline private def wrapper2[T](body: => T): T = {
| val saved = x2
| x2 = true
| try body
| finally x2 = saved
| }
| // inlined
| def f1a() = wrapper1(5)
| // not inlined: even after inlining `identity`, the Predef module is already on the stack for the
| // subsequent null check (the receiver of an inlined method, in this case Predef, is checked for
| // nullness, to ensure an NPE is thrown)
| def f1b() = identity(wrapper1(5))
|
| def f2a() = wrapper2(5) // inlined
| def f2b() = identity(wrapper2(5)) // not inlined
|}
""".stripMargin
val List(c) = compileClasses(compiler)(code, allowMessage = _.msg.contains("exception handler declared in the inlined method"))
assertInvoke(getSingleMethod(c, "f1a"), "C", "C$$$anonfun$1")
assertInvoke(getSingleMethod(c, "f1b"), "C", "wrapper1")
assertInvoke(getSingleMethod(c, "f2a"), "C", "C$$$anonfun$3")
assertInvoke(getSingleMethod(c, "f2b"), "C", "wrapper2")
}
@Test
def t7060(): Unit = {
val code =
"""class C {
| @inline final def mbarray_apply_minibox(array: Any, tag: Byte): Long =
| if (tag == 0) array.asInstanceOf[Array[Long]](0)
| else array.asInstanceOf[Array[Byte]](0).toLong
|
| def t = mbarray_apply_minibox(null, 0)
|}
""".stripMargin
val List(c) = compileClasses(compiler)(code)
assertNoInvoke(getSingleMethod(c, "t"))
}
@Test
def t8315(): Unit = {
val code =
"""class C {
| def t(as: Listt): Unit = {
| map(as, (_: Any) => return)
| }
| final def map(x: Listt, f: Any => Any): Any = {
| if (x eq Nill) "" else f("")
| }
|}
|object Nill extends Listt
|class Listt
""".stripMargin
val List(c, nil, nilMod, listt) = compileClasses(compiler)(code)
assertInvoke(getSingleMethod(c, "t"), "C", "C$$$anonfun$1")
}
@Test
def t8315b(): Unit = {
val code =
"""class C {
| def crash: Unit = {
| val key = ""
| try map(new F(key))
| catch { case _: Throwable => }
| }
| final def map(f: F): Any = f.apply("")
|}
|final class F(key: String) {
| final def apply(a: Any): Any = throw new RuntimeException(key)
|}
""".stripMargin
val List(c, f) = compileClasses(compiler)(code)
assertInvoke(getSingleMethod(c, "crash"), "C", "map")
}
@Test
def optimiseEnablesNewOpt(): Unit = {
val code = """class C { def t = (1 to 10) foreach println }"""
val List(c) = readAsmClasses(compile(newCompiler(extraArgs = "-optimise -deprecation"))(code, allowMessage = _.msg.contains("is deprecated")))
assertInvoke(getSingleMethod(c, "t"), "C", "C$$$anonfun$1") // range-foreach inlined from classpath
}
}
|