Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

8272776: NullPointerException not reported #5312

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -340,9 +340,8 @@ private void handleSwitch(JCTree tree,
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;
}
}
Expand Down
16 changes: 15 additions & 1 deletion test/langtools/tools/javac/patterns/NullSwitch.java
@@ -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
Expand Down Expand Up @@ -57,6 +57,9 @@ void switchTest() {
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) {
Expand Down Expand Up @@ -159,6 +162,17 @@ private int matchingSwitch12(Object obj) {
}
}

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);
Expand Down