8268333: javac crashes when pattern matching switch contains default case which is not last
Reviewed-by: vromero
This commit is contained in:
parent
b318535452
commit
abe20c188c
@ -431,6 +431,7 @@ public class TransPatterns extends TreeTranslator {
|
||||
|
||||
int i = 0;
|
||||
boolean previousCompletesNormally = false;
|
||||
boolean hasDefault = false;
|
||||
|
||||
for (var c : cases) {
|
||||
List<JCCaseLabel> clearedPatterns = c.labels;
|
||||
@ -477,7 +478,9 @@ public class TransPatterns extends TreeTranslator {
|
||||
for (var p : c.labels) {
|
||||
if (p.hasTag(Tag.DEFAULTCASELABEL)) {
|
||||
translatedLabels.add(p);
|
||||
} else if (hasTotalPattern && c == lastCase && p.isPattern()) {
|
||||
hasDefault = true;
|
||||
} else if (hasTotalPattern && !hasDefault &&
|
||||
c == lastCase && p.isPattern()) {
|
||||
//If the switch has total pattern, the last case will contain it.
|
||||
//Convert the total pattern to default:
|
||||
translatedLabels.add(make.DefaultCaseLabel());
|
||||
|
@ -27,7 +27,7 @@ import java.util.function.Function;
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @bug 8262891
|
||||
* @bug 8262891 8268333
|
||||
* @summary Check behavior of pattern switches.
|
||||
* @compile --enable-preview -source ${jdk.version} Switches.java
|
||||
* @run main/othervm --enable-preview Switches
|
||||
@ -46,6 +46,8 @@ public class Switches {
|
||||
assertTrue(testNullSwitch(""));
|
||||
runArrayTypeTest(this::testArrayTypeStatement);
|
||||
runArrayTypeTest(this::testArrayTypeExpression);
|
||||
runDefaultTest(this::testDefaultDoesNotDominateStatement);
|
||||
runDefaultTest(this::testDefaultDoesNotDominateExpression);
|
||||
runEnumTest(this::testEnumExpression1);
|
||||
runEnumTest(this::testEnumExpression2);
|
||||
runEnumTest(this::testEnumWithGuards1);
|
||||
@ -81,6 +83,13 @@ public class Switches {
|
||||
assertEquals("", mapper.apply(1.0));
|
||||
}
|
||||
|
||||
void runDefaultTest(Function<Object, String> mapper) {
|
||||
assertEquals("default", mapper.apply(new int[0]));
|
||||
assertEquals("str6", mapper.apply("string"));
|
||||
assertEquals("default", mapper.apply(1));
|
||||
assertEquals("default", mapper.apply(1.0));
|
||||
}
|
||||
|
||||
void runEnumTest(Function<E, String> mapper) {
|
||||
assertEquals("a", mapper.apply(E.A));
|
||||
assertEquals("b", mapper.apply(E.B));
|
||||
@ -172,6 +181,22 @@ public class Switches {
|
||||
};
|
||||
}
|
||||
|
||||
String testDefaultDoesNotDominateStatement(Object o) {
|
||||
String res;
|
||||
switch (o) {
|
||||
default -> res = "default";
|
||||
case String str -> res = "str" + str.length();
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
String testDefaultDoesNotDominateExpression(Object o) {
|
||||
return switch (o) {
|
||||
case default -> "default";
|
||||
case String str -> "str" + str.length();
|
||||
};
|
||||
}
|
||||
|
||||
int testStringWithConstant(String str) {
|
||||
switch (str) {
|
||||
case "A": return 1;
|
||||
|
Loading…
Reference in New Issue
Block a user