8000 SI-10069 Fix code gen errors with array updates, Nothing · retronym/scala@99abf68 · GitHub
[go: up one dir, main page]

Skip to content

Commit 99abf68

Browse files
committed
SI-10069 Fix code gen errors with array updates, Nothing
Crashes in ASM or VerifyErrors used to occur when assigning an expression of type Nothing to an element of a primitive array. This commit adapts the RHS of the assignment to the element type to correct this. `adapt` contains logic to insert an `ATHROW` of the slot of type `Nothing$`, which makes everything line up. The subsequent array stores become dead code and are dropped later on in code gen, so the test case compiles to: public void foo0(double[]); Code: 0: bipush 42 2: istore_2 3: aload_1 4: iconst_0 5: aload_0 6: invokevirtual #30 // Method throwExpected:()Lscala/runtime/Nothing$; 9: athrow
1 parent 73678d4 commit 99abf68

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

src/compiler/scala/tools/nsc/backend/jvm/BCodeBodyBuilder.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ abstract class BCodeBodyBuilder extends BCodeSkelBuilder {
169169
val List(a1, a2) = args
170170
genLoad(a1, INT)
171171
genLoad(a2)
172+
adapt(tpeTK(a2), elementType)
172173
generatedType = UNIT
173174
bc.astore(elementType)
174175
} else {

test/files/run/t10069.scala

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
object Expected extends Exception
2+
object Test {
3+
def throwExpected: Nothing = throw Expected
4+
def foo0(a: Array[Double]) = { // does compile for Int instead of Double
5+
val v = 42
6+
a(0) = throwExpected // was crash in code gen: java.lang.NegativeArraySizeException
7+
}
8+
9+
def foo1(a: Array[Double]) = { // does compile for Int instead of Double
10+
a(0) = throwExpected // was VerifyError at runtime
11+
}
12+
13+
def foo2(a: Array[Int]) = { // does compile for Int instead of Double
14+
a(0) = throwExpected // was VerifyError at runtime
15+
}
16+
17+
def foo3(a: Array[String]) = { // does compile for Int instead of Double
18+
a(0) = throwExpected // was already working
19+
}
20+
21+
22+
def main(args: Array[String]): Unit = {
23+
check(foo0(new Array[Double](1)))
24+
check(foo1(new Array[Double](1)))
25+
check(foo2(new Array[Int](1)))
26+
check(foo3(new Array[String](1)))
27+
}
28+
def check(f: => Any) {
29+
try {f ; sys.error("no exception thrown")
30+
} catch {
31+
case Expected =>
32+
}
33+
}
34+
}

0 commit comments

Comments
 (0)
0