diff options
| author | Manoj Palat | 2019-04-25 10:09:38 +0000 |
|---|---|---|
| committer | Manoj Palat | 2019-04-26 03:20:53 +0000 |
| commit | a8d59be0a1fe090c489c1eb9fda14a27bfb7fb74 (patch) | |
| tree | 3ca5159f29cbad177c5747580e41063403c43819 | |
| parent | 6ff5a8082eda85190509b9f52c1c48456e25606b (diff) | |
| download | eclipse.jdt.core-a8d59be0a1fe090c489c1eb9fda14a27bfb7fb74.tar.gz eclipse.jdt.core-a8d59be0a1fe090c489c1eb9fda14a27bfb7fb74.tar.xz eclipse.jdt.core-a8d59be0a1fe090c489c1eb9fda14a27bfb7fb74.zip | |
returning a value
Change-Id: Ief731591c3c460e09df23c7110c8bdb62dc1d69a
7 files changed, 83 insertions, 11 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 04820c1a73..355791bf2b 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 @@ -1163,6 +1163,7 @@ public void test011_problem_categories() { expectedProblemAttributes.put("SwitchExpressionsNoResultExpression", new ProblemAttributes(CategorizedProblem.CAT_TYPE)); expectedProblemAttributes.put("SwitchExpressionSwitchLabeledBlockCompletesNormally", new ProblemAttributes(CategorizedProblem.CAT_INTERNAL)); expectedProblemAttributes.put("SwitchExpressionLastStatementCompletesNormally", new ProblemAttributes(CategorizedProblem.CAT_INTERNAL)); + expectedProblemAttributes.put("SwitchExpressionIllegalLastStatement", new ProblemAttributes(CategorizedProblem.CAT_INTERNAL)); expectedProblemAttributes.put("SwitchExpressionTrailingSwitchLabels", new ProblemAttributes(CategorizedProblem.CAT_INTERNAL)); expectedProblemAttributes.put("switchMixedCase", new ProblemAttributes(CategorizedProblem.CAT_SYNTAX)); expectedProblemAttributes.put("SwitchExpressionMissingDefaultCase", new ProblemAttributes(CategorizedProblem.CAT_INTERNAL)); @@ -2108,6 +2109,7 @@ public void test012_compiler_problems_tuning() { expectedProblemAttributes.put("SwitchExpressionsNoResultExpression",SKIP); expectedProblemAttributes.put("SwitchExpressionSwitchLabeledBlockCompletesNormally",SKIP); expectedProblemAttributes.put("SwitchExpressionLastStatementCompletesNormally",SKIP); + expectedProblemAttributes.put("SwitchExpressionIllegalLastStatement",SKIP); expectedProblemAttributes.put("SwitchExpressionTrailingSwitchLabels",SKIP); expectedProblemAttributes.put("switchMixedCase", SKIP); expectedProblemAttributes.put("SwitchExpressionMissingDefaultCase",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 e39937479f..66d8d68d83 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 @@ -2408,4 +2408,68 @@ public class SwitchExpressionTest extends AbstractRegressionTest { null, new String[] {"--enable-preview"}); } + public void testBug545983_01() { + this.runNegativeTest( + new String[] { + "X.java", + "\n"+ + "public class X {\n"+ + "\n"+ + " @SuppressWarnings(\"preview\")\n"+ + " public static int foo() {\n"+ + " for (int i = 0; i < 1; ++i) {\n"+ + " int k = switch (i) {\n"+ + " case 0:\n"+ + " break 1;\n"+ + " default:\n"+ + " continue;\n"+ + " };\n"+ + " System.out.println(k);\n"+ + " }\n"+ + " return 1;\n"+ + " }\n"+ + " public static void main(String[] args) {\n"+ + " X.foo();\n"+ + " }\n"+ + "}\n", + }, + "----------\n" + + "1. ERROR in X.java (at line 11)\n" + + " continue;\n" + + " ^^^^^^^^^\n" + + "Illegal last statement in Switch expression case body - continue, return not allowed\n" + + "----------\n"); + } + public void testBug545983_02() { + this.runNegativeTest( + new String[] { + "X.java", + "\n"+ + "public class X {\n"+ + "\n"+ + " @SuppressWarnings(\"preview\")\n"+ + " public static int foo() {\n"+ + " for (int i = 0; i < 1; ++i) {\n"+ + " int k = switch (i) {\n"+ + " case 0:\n"+ + " break 1;\n"+ + " default:\n"+ + " return 2;\n"+ + " };\n"+ + " System.out.println(k);\n"+ + " }\n"+ + " return 1;\n"+ + " }\n"+ + " public static void main(String[] args) {\n"+ + " X.foo();\n"+ + " }\n"+ + "}\n", + }, + "----------\n" + + "1. ERROR in X.java (at line 11)\n" + + " return 2;\n" + + " ^^^^^^^^^\n" + + "Illegal last statement in Switch expression case body - continue, return not allowed\n" + + "----------\n"); + } } diff --git a/org.eclipse.jdt.core/.settings/.api_filters b/org.eclipse.jdt.core/.settings/.api_filters index 26e4f57056..02d584a2f1 100644 --- a/org.eclipse.jdt.core/.settings/.api_filters +++ b/org.eclipse.jdt.core/.settings/.api_filters @@ -8,15 +8,6 @@ </message_arguments> </filter> </resource> - <resource path="compiler/org/eclipse/jdt/core/compiler/IProblem.java" type="org.eclipse.jdt.core.compiler.IProblem"> - <filter id="388194388"> - <message_arguments> - <message_argument value="org.eclipse.jdt.core.compiler.IProblem"/> - <message_argument value="IgnoreCategoriesMask"/> - <message_argument value="8388607"/> - </message_arguments> - </filter> - </resource> <resource path="dom/org/eclipse/jdt/core/dom/BreakStatement.java" type="org.eclipse.jdt.core.dom.BreakStatement"> <filter comment="For Java 12 support." id="336658481"> <message_arguments> 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 30ffd73679..f79bbdce1e 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 @@ -2106,4 +2106,6 @@ void setSourceStart(int sourceStart); int SwitchExpressionBreakMissingValue = Internal + 1610; /** @since 3.18 */ int SwitchExpressionMissingEnumConstantCase = Internal + 1611; + /** @since 3.18 */ + int SwitchExpressionIllegalLastStatement = Internal + 1612; } 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 1130f69256..4f91f26fee 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 @@ -152,8 +152,12 @@ public class SwitchExpression extends SwitchStatement implements IPolyExpression break; } } - if (lastNonCaseStmt != null && !lastNonCaseStmt.doesNotCompleteNormally()) { - blockScope.problemReporter().switchExpressionLastStatementCompletesNormally(lastNonCaseStmt); + if (lastNonCaseStmt != null) { + if (!lastNonCaseStmt.doesNotCompleteNormally()) + blockScope.problemReporter().switchExpressionLastStatementCompletesNormally(lastNonCaseStmt); + else if (lastNonCaseStmt instanceof ContinueStatement || lastNonCaseStmt instanceof ReturnStatement) { + blockScope.problemReporter().switchExpressionIllegalLastStatement(lastNonCaseStmt); + } } if (firstTrailingCaseStmt != null) { blockScope.problemReporter().switchExpressionTrailingSwitchLabels(firstTrailingCaseStmt); 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 2185a76108..2d627e1209 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 @@ -11078,6 +11078,14 @@ public void switchExpressionLastStatementCompletesNormally(Statement stmt) { stmt.sourceStart, stmt.sourceEnd); } +public void switchExpressionIllegalLastStatement(Statement stmt) { + this.handle( + IProblem.SwitchExpressionIllegalLastStatement, + NoArgument, + NoArgument, + stmt.sourceStart, + stmt.sourceEnd); +} public void switchExpressionTrailingSwitchLabels(Statement stmt) { this.handle( IProblem.SwitchExpressionTrailingSwitchLabels, 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 db11b648ae..83166a776e 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 @@ -982,6 +982,7 @@ 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 = A Switch expression should cover all possible values +1612 = Illegal last statement in Switch expression case body - continue, return not allowed ### ELABORATIONS ## Access restrictions |
