8270151: IncompatibleClassChangeError on empty pattern switch statement case

Reviewed-by: mcimadamore
This commit is contained in:
Jan Lahoda 2021-07-09 11:03:40 +00:00
parent 885f7b1141
commit 1196b35684
3 changed files with 24 additions and 3 deletions

View File

@ -3598,7 +3598,7 @@ public class Lower extends TreeTranslator {
JCThrow thr = make.Throw(makeNewClass(syms.incompatibleClassChangeErrorType,
List.nil()));
JCCase c = make.Case(JCCase.STATEMENT, List.of(make.DefaultCaseLabel()), List.of(thr), null);
cases = cases.append(c);
cases = cases.prepend(c);
}
return cases;

View File

@ -23,7 +23,7 @@
/*
* @test
* @bug 8262891
* @bug 8262891 8270151
* @summary Verify pattern switches work properly when the set of sealed types changes.
* @compile --enable-preview -source ${jdk.version} SealedTypeChanges.java
* @compile --enable-preview -source ${jdk.version} SealedTypeChanges2.java
@ -43,6 +43,7 @@ public class SealedTypeChanges {
doRun(this::statementIntf, this::validateIncompatibleClassChangeError);
doRun(this::expressionCls, this::validateIncompatibleClassChangeError);
doRun(this::statementCls, this::validateIncompatibleClassChangeError);
doRun(this::statementFallThrough, this::validateIncompatibleClassChangeError);
doRun(this::expressionCoveredIntf, this::validateTestException);
doRun(this::statementCoveredIntf, this::validateTestException);
doRun(this::expressionCoveredCls, this::validateTestException);
@ -89,6 +90,12 @@ public class SealedTypeChanges {
}
}
void statementFallThrough(SealedTypeChangesCls obj) {
switch (obj) {
case A a: System.err.println(1);
}
}
int expressionCls(SealedTypeChangesCls obj) {
return switch (obj) {
case A a -> 0;

View File

@ -28,7 +28,7 @@ import java.util.function.Function;
/*
* @test
* @bug 8262891 8268333 8268896 8269802 8269808
* @bug 8262891 8268333 8268896 8269802 8269808 8270151
* @summary Check behavior of pattern switches.
* @compile --enable-preview -source ${jdk.version} Switches.java
* @run main/othervm --enable-preview Switches
@ -73,6 +73,9 @@ public class Switches {
npeTest(this::npeTestExpression);
exhaustiveStatementSane("");
exhaustiveStatementSane(null);
exhaustiveStatementSane2(null);
exhaustiveStatementSane2(new A());
exhaustiveStatementSane2(new B());
switchNestingTest(this::switchNestingStatementStatement);
switchNestingTest(this::switchNestingStatementExpression);
switchNestingTest(this::switchNestingExpressionStatement);
@ -452,6 +455,17 @@ public class Switches {
}
}
void exhaustiveStatementSane2(I i) {
switch (i) {
case A a: break;
case null, B b:; //no break intentionally - should not fall through to any possible default
}
switch (i) {
case A a -> {}
case null, B b -> {}
}
}
String switchNestingStatementStatement(Object o1, Object o2) {
switch (o1) {
case String s1 -> {