Skip to content
This repository was archived by the owner on Sep 2, 2022. It is now read-only.
/ jdk17 Public archive

Commit abe20c1

Browse files
committed
8268333: javac crashes when pattern matching switch contains default case which is not last
Reviewed-by: vromero
1 parent b318535 commit abe20c1

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

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

+4-1
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,7 @@ private void handleSwitch(JCTree tree,
431431

432432
int i = 0;
433433
boolean previousCompletesNormally = false;
434+
boolean hasDefault = false;
434435

435436
for (var c : cases) {
436437
List<JCCaseLabel> clearedPatterns = c.labels;
@@ -477,7 +478,9 @@ private void handleSwitch(JCTree tree,
477478
for (var p : c.labels) {
478479
if (p.hasTag(Tag.DEFAULTCASELABEL)) {
479480
translatedLabels.add(p);
480-
} else if (hasTotalPattern && c == lastCase && p.isPattern()) {
481+
hasDefault = true;
482+
} else if (hasTotalPattern && !hasDefault &&
483+
c == lastCase && p.isPattern()) {
481484
//If the switch has total pattern, the last case will contain it.
482485
//Convert the total pattern to default:
483486
translatedLabels.add(make.DefaultCaseLabel());

test/langtools/tools/javac/patterns/Switches.java

+26-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727

2828
/*
2929
* @test
30-
* @bug 8262891
30+
* @bug 8262891 8268333
3131
* @summary Check behavior of pattern switches.
3232
* @compile --enable-preview -source ${jdk.version} Switches.java
3333
* @run main/othervm --enable-preview Switches
@@ -46,6 +46,8 @@ void run() {
4646
assertTrue(testNullSwitch(""));
4747
runArrayTypeTest(this::testArrayTypeStatement);
4848
runArrayTypeTest(this::testArrayTypeExpression);
49+
runDefaultTest(this::testDefaultDoesNotDominateStatement);
50+
runDefaultTest(this::testDefaultDoesNotDominateExpression);
4951
runEnumTest(this::testEnumExpression1);
5052
runEnumTest(this::testEnumExpression2);
5153
runEnumTest(this::testEnumWithGuards1);
@@ -81,6 +83,13 @@ void runArrayTypeTest(Function<Object, String> mapper) {
8183
assertEquals("", mapper.apply(1.0));
8284
}
8385

86+
void runDefaultTest(Function<Object, String> mapper) {
87+
assertEquals("default", mapper.apply(new int[0]));
88+
assertEquals("str6", mapper.apply("string"));
89+
assertEquals("default", mapper.apply(1));
90+
assertEquals("default", mapper.apply(1.0));
91+
}
92+
8493
void runEnumTest(Function<E, String> mapper) {
8594
assertEquals("a", mapper.apply(E.A));
8695
assertEquals("b", mapper.apply(E.B));
@@ -172,6 +181,22 @@ String testArrayTypeExpression(Object o) {
172181
};
173182
}
174183

184+
String testDefaultDoesNotDominateStatement(Object o) {
185+
String res;
186+
switch (o) {
187+
default -> res = "default";
188+
case String str -> res = "str" + str.length();
189+
}
190+
return res;
191+
}
192+
193+
String testDefaultDoesNotDominateExpression(Object o) {
194+
return switch (o) {
195+
case default -> "default";
196+
case String str -> "str" + str.length();
197+
};
198+
}
199+
175200
int testStringWithConstant(String str) {
176201
switch (str) {
177202
case "A": return 1;

0 commit comments

Comments
 (0)