Skip to content

Commit

Permalink
Backport 93eec9a103de7f4d9a87eac5b295c9a50702ee94
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexey Pavlyutkin committed Sep 14, 2022
1 parent b196bc4 commit 9edac7c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
Expand Up @@ -341,9 +341,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

0 comments on commit 9edac7c

Please sign in to comment.