Skip to content

Commit 70b912a

Browse files
committed
SI-8601 Don't treat newarray as dead code
Otherwise we lose the side effect of a `NegativeArraySizeException`.
1 parent 99b4ef8 commit 70b912a

File tree

2 files changed

+4
-1
lines changed

2 files changed

+4
-1
lines changed

src/compiler/scala/tools/nsc/backend/opt/DeadCodeElimination.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ abstract class DeadCodeElimination extends SubComponent {
169169

170170
case RETURN(_) | JUMP(_) | CJUMP(_, _, _, _) | CZJUMP(_, _, _, _) | STORE_FIELD(_, _) | LOAD_FIELD(_, _) | // Why LOAD_FIELD? It can NPE!
171171
THROW(_) | LOAD_ARRAY_ITEM(_) | STORE_ARRAY_ITEM(_) | SCOPE_ENTER(_) | SCOPE_EXIT(_) | STORE_THIS(_) |
172-
LOAD_EXCEPTION(_) | SWITCH(_, _) | MONITOR_ENTER() | MONITOR_EXIT() | CHECK_CAST(_) =>
172+
LOAD_EXCEPTION(_) | SWITCH(_, _) | MONITOR_ENTER() | MONITOR_EXIT() | CHECK_CAST(_) | CREATE_ARRAY(_, _) =>
173173
moveToWorkList()
174174

175175
case CALL_METHOD(m1, _) if isSideEffecting(m1) =>

test/files/run/t8601b.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
object Test {
22
def len(x: Array[String]): Unit = x.length
33
def load(x: Array[String]): Unit = x(0)
4+
def newarray(i: Int): Unit = new Array[Int](i)
45

56
def check(x: => Any) = try { x; sys.error("failed to throw NPE!") } catch { case _: NullPointerException => }
7+
def checkNegSize(x: => Any) = try { x; sys.error("failed to throw NegativeArraySizeException!") } catch { case _: NegativeArraySizeException => }
68

79
def main(args: Array[String]) {
810
check(len(null)) // bug: did not NPE
911
check(load(null))
12+
checkNegSize(newarray(-1))
1013
}
1114
}

0 commit comments

Comments
 (0)