Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorManoj Palat2019-04-08 09:53:25 -0400
committerManoj Palat2019-04-08 09:53:25 -0400
commite726626e8993c67caac2b8e36af66902248ae6a8 (patch)
treeb6cbafc171eb5741e4365778b3c8c5f2e7da46f9
parent9e12782ba2ffa4c85cbfa25013efa0ede16bdd90 (diff)
downloadeclipse.jdt.core-e726626e8993c67caac2b8e36af66902248ae6a8.tar.gz
eclipse.jdt.core-e726626e8993c67caac2b8e36af66902248ae6a8.tar.xz
eclipse.jdt.core-e726626e8993c67caac2b8e36af66902248ae6a8.zip
Bug 545716 - [12] expression switch doesn't consider that having all
cases of an enum is enough to be exhaustive
-rw-r--r--org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/SwitchExpressionTest.java23
-rw-r--r--org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/SwitchExpression.java4
-rw-r--r--org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/SwitchStatement.java5
3 files changed, 31 insertions, 1 deletions
diff --git a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/SwitchExpressionTest.java b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/SwitchExpressionTest.java
index e4e6e8ed0b..0d8ac4a28d 100644
--- a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/SwitchExpressionTest.java
+++ b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/SwitchExpressionTest.java
@@ -2385,4 +2385,27 @@ public class SwitchExpressionTest extends AbstractRegressionTest {
null,
new String[] {"--enable-preview"});
}
+ public void testBug545916_01() {
+ runConformTest(
+ new String[] {
+ "X.java",
+ "enum X {\n"+
+ " A, B;\n"+
+ " \n"+
+ " @SuppressWarnings(\"preview\")\n"+
+ " public static void main(String[] args) {\n"+
+ " X myEnum = X.A;\n"+
+ " int o;\n"+
+ " var f = switch(myEnum) {\n"+
+ " case A -> o = 5;\n"+
+ " case B -> o = 10;\n"+
+ " };\n"+
+ " System.out.println(o);\n"+
+ " }\n"+
+ "} \n"
+ },
+ "5",
+ null,
+ new String[] {"--enable-preview"});
+ }
}
diff --git a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/SwitchExpression.java b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/SwitchExpression.java
index faba6e578f..1130f69256 100644
--- a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/SwitchExpression.java
+++ b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/SwitchExpression.java
@@ -160,6 +160,10 @@ public class SwitchExpression extends SwitchStatement implements IPolyExpression
}
}
@Override
+ protected boolean checkNullDefaultFlow() { // JLS 12 16.1.8
+ return !this.switchLabeledRules;
+ }
+ @Override
public Expression[] getPolyExpressions() {
List<Expression> polys = new ArrayList<>();
for (Expression e : this.resultExpressions) {
diff --git a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/SwitchStatement.java b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/SwitchStatement.java
index a694b0a101..eebcfca755 100644
--- a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/SwitchStatement.java
+++ b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/SwitchStatement.java
@@ -112,6 +112,9 @@ public class SwitchStatement extends Expression {
protected void completeNormallyCheck(BlockScope blockScope) {
// do nothing
}
+ protected boolean checkNullDefaultFlow() {
+ return true;
+ }
@Override
public FlowInfo analyseCode(BlockScope currentScope, FlowContext flowContext, FlowInfo flowInfo) {
try {
@@ -175,7 +178,7 @@ public class SwitchStatement extends Expression {
this.synthetic = sourceTypeBinding.addSyntheticMethodForSwitchEnum(resolvedTypeBinding, this);
}
// if no default case, then record it may jump over the block directly to the end
- if (this.defaultCase == null) {
+ if (this.defaultCase == null && checkNullDefaultFlow()) {
// only retain the potential initializations
flowInfo.addPotentialInitializationsFrom(caseInits.mergedWith(switchContext.initsOnBreak));
this.mergedInitStateIndex = currentScope.methodScope().recordInitializationStates(flowInfo);

Back to the top