diff options
7 files changed, 61 insertions, 2 deletions
diff --git a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/CompilerInvocationTests.java b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/CompilerInvocationTests.java index cbe132bce7..d40a5652d9 100644 --- a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/CompilerInvocationTests.java +++ b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/CompilerInvocationTests.java @@ -1175,6 +1175,7 @@ public void test011_problem_categories() { expectedProblemAttributes.put("SwitchExpressionPreviewDisabled", new ProblemAttributes(CategorizedProblem.CAT_SYNTAX)); expectedProblemAttributes.put("SwitchCaseLabelWithArrowPreviewDisabled", new ProblemAttributes(CategorizedProblem.CAT_SYNTAX)); expectedProblemAttributes.put("SwitchExpressionBreakMissingValue", new ProblemAttributes(CategorizedProblem.CAT_INTERNAL)); + expectedProblemAttributes.put("SwitchExpressionMissingEnumConstantCase", new ProblemAttributes(CategorizedProblem.CAT_INTERNAL)); expectedProblemAttributes.put("PreviewFeatureDisabled", new ProblemAttributes(CategorizedProblem.CAT_COMPLIANCE)); expectedProblemAttributes.put("PreviewFeatureUsed", new ProblemAttributes(CategorizedProblem.CAT_COMPLIANCE)); expectedProblemAttributes.put("PreviewFeatureNotSupported", new ProblemAttributes(CategorizedProblem.CAT_COMPLIANCE)); @@ -2119,6 +2120,7 @@ public void test012_compiler_problems_tuning() { expectedProblemAttributes.put("SwitchExpressionPreviewDisabled", SKIP); expectedProblemAttributes.put("SwitchCaseLabelWithArrowPreviewDisabled", SKIP); expectedProblemAttributes.put("SwitchExpressionBreakMissingValue", SKIP); + expectedProblemAttributes.put("SwitchExpressionMissingEnumConstantCase", SKIP); expectedProblemAttributes.put("PreviewFeatureDisabled", SKIP); expectedProblemAttributes.put("PreviewFeatureUsed", SKIP); expectedProblemAttributes.put("PreviewFeatureNotSupported", SKIP); 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 fbc2ac18e9..d50878833f 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 @@ -1675,4 +1675,42 @@ public class SwitchExpressionTest extends AbstractRegressionTest { null, new String[] {"--enable-preview"}); } + public void testBug544258_01() { + this.runNegativeTest( + new String[] { + "X.java", + "public class X {\n" + + " public void foo(Day day) {\n" + + " var today = 1;\n" + + " today = switch (day) {\n" + + " case SATURDAY,SUNDAY :\n" + + " today=1;\n" + + " break today;\n" + + " case MONDAY,TUESDAY,WEDNESDAY,THURSDAY :\n" + + " today=2;\n" + + " break today;\n" + + " };\n" + + " }\n" + + " public static void main(String argv[]) {\n" + + " new X().foo(Day.FRIDAY);\n" + + " }\n" + + "}\n" + + "\n" + + "enum Day {\n" + + " SUNDAY,\n" + + " MONDAY,\n" + + " TUESDAY,\n" + + " WEDNESDAY,\n" + + " THURSDAY,\n" + + " FRIDAY,\n" + + " SATURDAY\n" + + "}\n", + }, + "----------\n" + + "1. ERROR in X.java (at line 4)\n" + + " today = switch (day) {\n" + + " ^^^\n" + + "missing enum constant(s) in switch expression as all enum constants are required in case statements if there is no default case.\n" + + "----------\n"); + } }
\ No newline at end of file diff --git a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/core/compiler/IProblem.java b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/core/compiler/IProblem.java index d82cf88369..fc4f419191 100644 --- a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/core/compiler/IProblem.java +++ b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/core/compiler/IProblem.java @@ -2108,4 +2108,6 @@ void setSourceStart(int sourceStart); int SwitchExpressionMissingDefaultCase = Internal + 1607; /** @since 3.17 BETA_JAVA_12 */ int SwitchExpressionBreakMissingValue = Internal + 1610; + /** @since 3.17 BETA_JAVA_12 */ + int SwitchExpressionMissingEnumConstantCase = Internal + 1611; } 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 7763555c82..eedb567edb 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 @@ -37,6 +37,7 @@ import org.eclipse.jdt.internal.compiler.impl.CompilerOptions; import org.eclipse.jdt.internal.compiler.impl.Constant; import org.eclipse.jdt.internal.compiler.lookup.Binding; import org.eclipse.jdt.internal.compiler.lookup.BlockScope; +import org.eclipse.jdt.internal.compiler.lookup.FieldBinding; import org.eclipse.jdt.internal.compiler.lookup.LookupEnvironment; import org.eclipse.jdt.internal.compiler.lookup.MethodBinding; import org.eclipse.jdt.internal.compiler.lookup.PolyTypeBinding; @@ -84,6 +85,10 @@ public class SwitchExpression extends SwitchStatement implements IPolyExpression return isEnumSwitch; // mandatory error if not enum in switch expressions } @Override + protected void reportMissingEnumConstantCase(BlockScope upperScope, FieldBinding enumConstant) { + upperScope.problemReporter().missingEnumConstantCase(this, enumConstant); + } + @Override protected int getFallThroughState(Statement stmt, BlockScope blockScope) { if (stmt instanceof Expression || stmt instanceof ThrowStatement) return BREAKING; 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 6c6bfcd939..cf2efddbf8 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 @@ -693,7 +693,7 @@ public class SwitchStatement extends Expression { // enum constant did not get referenced from switch boolean suppress = (this.defaultCase != null && (this.defaultCase.bits & DocumentedCasesOmitted) != 0); if (!suppress) { - upperScope.problemReporter().missingEnumConstantCase(this, enumConstant); + reportMissingEnumConstantCase(upperScope, enumConstant); } } } @@ -704,6 +704,9 @@ public class SwitchStatement extends Expression { if (this.scope != null) this.scope.enclosingCase = null; // no longer inside switch case block } } + protected void reportMissingEnumConstantCase(BlockScope upperScope, FieldBinding enumConstant) { + upperScope.problemReporter().missingEnumConstantCase(this, enumConstant); + } protected boolean ignoreMissingDefaultCase(CompilerOptions compilerOptions, boolean isEnumSwitch) { return compilerOptions.getSeverity(CompilerOptions.MissingDefaultCase) == ProblemSeverities.Ignore; } diff --git a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/problem/ProblemReporter.java b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/problem/ProblemReporter.java index 652c32698c..a761f81770 100644 --- a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/problem/ProblemReporter.java +++ b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/problem/ProblemReporter.java @@ -6494,7 +6494,15 @@ public void missingEnumConstantCase(SwitchStatement switchStatement, FieldBindin missingEnumConstantCase(switchStatement.defaultCase, enumConstant, switchStatement.expression); } public void missingEnumConstantCase(SwitchExpression switchExpression, FieldBinding enumConstant) { - missingEnumConstantCase(switchExpression.defaultCase, enumConstant, switchExpression.expression); + missingSwitchExpressionEnumConstantCase(switchExpression.defaultCase, enumConstant, switchExpression.expression); +} +private void missingSwitchExpressionEnumConstantCase(CaseStatement defaultCase, FieldBinding enumConstant, ASTNode expression) { + this.handle( + IProblem.SwitchExpressionMissingEnumConstantCase, + new String[] {new String(enumConstant.declaringClass.readableName()), new String(enumConstant.name) }, + new String[] {new String(enumConstant.declaringClass.shortReadableName()), new String(enumConstant.name) }, + expression.sourceStart, + expression.sourceEnd); } private void missingEnumConstantCase(CaseStatement defaultCase, FieldBinding enumConstant, ASTNode expression) { this.handle( diff --git a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/problem/messages.properties b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/problem/messages.properties index 58da829b9b..e53b622318 100644 --- a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/problem/messages.properties +++ b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/problem/messages.properties @@ -985,6 +985,7 @@ 1608 = Switch expressions are allowed only at source level 12 or above 1609 = Switch Case Labels with '->' are allowed only at source level 12 or above 1610 = Break of a switch expression should have a value +1611 = missing enum constant(s) in switch expression as all enum constants are required in case statements if there is no default case. ### ELABORATIONS ## Access restrictions |