8272776: NullPointerException not reported

Reviewed-by: vromero
This commit is contained in:
Jan Lahoda 2021-09-03 09:31:54 +00:00
parent 7b023a3f60
commit 93eec9a103
2 changed files with 17 additions and 4 deletions

View File

@ -340,9 +340,8 @@ public class TransPatterns extends TreeTranslator {
JCCase lastCase = cases.last();
if (hasTotalPattern && !hasNullCase) {
JCCase last = lastCase;
if (last.labels.stream().noneMatch(l -> l.hasTag(Tag.DEFAULTCASELABEL))) {
last.labels = last.labels.prepend(makeLit(syms.botType, null));
if (cases.stream().flatMap(c -> c.labels.stream()).noneMatch(l -> l.hasTag(Tag.DEFAULTCASELABEL))) {
lastCase.labels = lastCase.labels.prepend(makeLit(syms.botType, null));
hasNullCase = true;
}
}

View File

@ -1,6 +1,6 @@
/*
* @test /nodynamiccopyright/
* @bug 8262891
* @bug 8262891 8272776
* @summary Check null handling for non-pattern switches.
* @compile --enable-preview -source ${jdk.version} NullSwitch.java
* @run main/othervm --enable-preview NullSwitch
@ -57,6 +57,9 @@ public class NullSwitch {
assertEquals(0, matchingSwitch12(""));
assertEquals(2, matchingSwitch12(null));
assertEquals(1, matchingSwitch12(0.0));
assertEquals(0, matchingSwitch13(""));
assertEquals(1, matchingSwitch13(0.0));
assertEquals(2, matchingSwitch13(null));
}
private int matchingSwitch1(Object obj) {
@ -159,6 +162,17 @@ public class NullSwitch {
}
}
private int matchingSwitch13(Object obj) {
try {
switch (obj) {
default: return 1;
case String s: return 0;
}
} catch (NullPointerException ex) {
return 2;
}
}
static void assertEquals(int expected, int actual) {
if (expected != actual) {
throw new AssertionError("Expected: " + expected + ", actual: " + actual);