Skip to content

Commit 41b6410

Browse files
author
duke
committedSep 3, 2021
Automatic merge of jdk:master into master
2 parents 1374f06 + 93eec9a commit 41b6410

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed
 

‎src/jdk.compiler/share/classes/com/sun/tools/javac/comp/TransPatterns.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -340,9 +340,8 @@ private void handleSwitch(JCTree tree,
340340
JCCase lastCase = cases.last();
341341

342342
if (hasTotalPattern && !hasNullCase) {
343-
JCCase last = lastCase;
344-
if (last.labels.stream().noneMatch(l -> l.hasTag(Tag.DEFAULTCASELABEL))) {
345-
last.labels = last.labels.prepend(makeLit(syms.botType, null));
343+
if (cases.stream().flatMap(c -> c.labels.stream()).noneMatch(l -> l.hasTag(Tag.DEFAULTCASELABEL))) {
344+
lastCase.labels = lastCase.labels.prepend(makeLit(syms.botType, null));
346345
hasNullCase = true;
347346
}
348347
}

‎test/langtools/tools/javac/patterns/NullSwitch.java

+15-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* @test /nodynamiccopyright/
3-
* @bug 8262891
3+
* @bug 8262891 8272776
44
* @summary Check null handling for non-pattern switches.
55
* @compile --enable-preview -source ${jdk.version} NullSwitch.java
66
* @run main/othervm --enable-preview NullSwitch
@@ -57,6 +57,9 @@ void switchTest() {
5757
assertEquals(0, matchingSwitch12(""));
5858
assertEquals(2, matchingSwitch12(null));
5959
assertEquals(1, matchingSwitch12(0.0));
60+
assertEquals(0, matchingSwitch13(""));
61+
assertEquals(1, matchingSwitch13(0.0));
62+
assertEquals(2, matchingSwitch13(null));
6063
}
6164

6265
private int matchingSwitch1(Object obj) {
@@ -159,6 +162,17 @@ private int matchingSwitch12(Object obj) {
159162
}
160163
}
161164

165+
private int matchingSwitch13(Object obj) {
166+
try {
167+
switch (obj) {
168+
default: return 1;
169+
case String s: return 0;
170+
}
171+
} catch (NullPointerException ex) {
172+
return 2;
173+
}
174+
}
175+
162176
static void assertEquals(int expected, int actual) {
163177
if (expected != actual) {
164178
throw new AssertionError("Expected: " + expected + ", actual: " + actual);

0 commit comments

Comments
 (0)
Please sign in to comment.