Skip to content

Commit 790ed59

Browse files
committed
SI-8601 Don't treat newarray as dead code
Otherwise we lose the side effect of a `NegativeArraySizeException`. A test for this case already exists (run/t8601b.scala), but it currently enforces `-optimize -Ybackend:GenASM`, so it didn't trigger on the new backend. However, PR scala#4814 was merged into 2.12.x and moved that test over to the new backend and optimizer. After merging the 2.12.x into the current optimizer branch (push-pop elimination), the test started failing. Also disable the optimizer for `jvm/bytecode-test-example`: it counts the number of null checks in a method, the optimizer (rightly) eliminates one of the two.
1 parent 8d3be4d commit 790ed59

File tree

3 files changed

+3
-6
lines changed

3 files changed

+3
-6
lines changed

src/compiler/scala/tools/nsc/backend/jvm/opt/CopyProp.scala

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -382,10 +382,6 @@ class CopyProp[BT <: BTypes](val btypes: BT) {
382382
popAfterProd()
383383
}
384384

385-
case NEWARRAY | ANEWARRAY =>
386-
toRemove += prod
387-
handleInputs(prod, 1)
388-
389385
case MULTIANEWARRAY =>
390386
toRemove += prod
391387
handleInputs(prod, prod.asInstanceOf[MultiANewArrayInsnNode].dims)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
-Yopt:l:none

test/junit/scala/tools/nsc/backend/jvm/opt/MethodLevelOptsTest.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,15 +161,15 @@ class MethodLevelOptsTest extends ClearAfterClass {
161161
| val b = (a, y) // Tuple2
162162
| val c = (new Object, "krik", new String) // unused java/lang/Object, java/lang/String allocation and string constant is also eliminated
163163
| val d = new java.lang.Integer(x)
164-
| val e = new String(new Array[Char](23))
164+
| val e = new String(new Array[Char](23)) // array allocation not eliminated, as it may throw (negative size, SI-8601)
165165
| val f = new scala.runtime.IntRef(11)
166166
| x + y
167167
| }
168168
|}
169169
""".stripMargin
170170
val List(c) = compileClasses(methodOptCompiler)(code)
171171
assertEquals(getSingleMethod(c, "t").instructions.dropNonOp,
172-
List(VarOp(ILOAD, 1), VarOp(ILOAD, 2), Op(IADD), Op(IRETURN)))
172+
List(IntOp(BIPUSH, 23), IntOp(NEWARRAY, 5), Op(POP), VarOp(ILOAD, 1), VarOp(ILOAD, 2), Op(IADD), Op(IRETURN)))
173173
}
174174

175175
@Test

0 commit comments

Comments
 (0)