diff options
author | Manoj Palat | 2020-05-11 06:56:25 +0000 |
---|---|---|
committer | Manoj Palat | 2020-05-11 06:56:25 +0000 |
commit | 797a7afd3bf03afa301d82425a68573ea9e76d2c (patch) | |
tree | 09206a4cb50282c168f589b0cced2e61c742b222 | |
parent | 3e93547d4ae42cdb05d1f31c1c89cd13ef956056 (diff) | |
download | eclipse.jdt.core-797a7afd3bf03afa301d82425a68573ea9e76d2c.tar.gz eclipse.jdt.core-797a7afd3bf03afa301d82425a68573ea9e76d2c.tar.xz eclipse.jdt.core-797a7afd3bf03afa301d82425a68573ea9e76d2c.zip |
inside (edit)
Change-Id: Icd5d9e758fe903e2d12bf49ea920596dd95d3a99
6 files changed, 63 insertions, 6 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 064aa6ef83..5bd4f85dfe 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 @@ -1223,6 +1223,7 @@ public void test011_problem_categories() { expectedProblemAttributes.put("SwitchExpressionsNotSupported", new ProblemAttributes(CategorizedProblem.CAT_SYNTAX)); expectedProblemAttributes.put("SwitchExpressionsBreakOutOfSwitchExpression", new ProblemAttributes(CategorizedProblem.CAT_SYNTAX)); expectedProblemAttributes.put("SwitchExpressionsContinueOutOfSwitchExpression", new ProblemAttributes(CategorizedProblem.CAT_SYNTAX)); + expectedProblemAttributes.put("SwitchExpressionsReturnWithinSwitchExpression", new ProblemAttributes(CategorizedProblem.CAT_SYNTAX)); expectedProblemAttributes.put("RecordIllegalModifierForRecord", new ProblemAttributes(CategorizedProblem.CAT_PREVIEW_RELATED)); expectedProblemAttributes.put("RecordIllegalModifierForInnerRecord", new ProblemAttributes(CategorizedProblem.CAT_PREVIEW_RELATED)); expectedProblemAttributes.put("RecordIllegalComponentNameInRecord", new ProblemAttributes(CategorizedProblem.CAT_PREVIEW_RELATED)); @@ -2245,6 +2246,7 @@ public void test012_compiler_problems_tuning() { expectedProblemAttributes.put("SwitchExpressionsNotSupported", SKIP); expectedProblemAttributes.put("SwitchExpressionsBreakOutOfSwitchExpression", SKIP); expectedProblemAttributes.put("SwitchExpressionsContinueOutOfSwitchExpression", SKIP); + expectedProblemAttributes.put("SwitchExpressionsReturnWithinSwitchExpression", SKIP); expectedProblemAttributes.put("RecordIllegalModifierForRecord", SKIP); expectedProblemAttributes.put("RecordIllegalModifierForInnerRecord", SKIP); expectedProblemAttributes.put("RecordIllegalComponentNameInRecord", SKIP); diff --git a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/SwitchExpressionsYieldTest.java b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/SwitchExpressionsYieldTest.java index c7cc87a67b..dd142895b0 100644 --- a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/SwitchExpressionsYieldTest.java +++ b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/SwitchExpressionsYieldTest.java @@ -2166,7 +2166,7 @@ public class SwitchExpressionsYieldTest extends AbstractRegressionTest { " };\n"+ " System.out.println(k);\n"+ " }\n"+ - " return 1;\n"+ + " return 100;\n"+ " }\n"+ " public static void main(String[] args) {\n"+ " X.foo();\n"+ @@ -2177,7 +2177,7 @@ public class SwitchExpressionsYieldTest extends AbstractRegressionTest { "1. ERROR in X.java (at line 11)\n" + " return 2;\n" + " ^^^^^^^^^\n" + - "'continue' or 'return' cannot be the last statement in a Switch expression case body\n" + + "Return within switch expressions not permitted\n" + "----------\n"); } public void testBug544073_078() { @@ -4841,4 +4841,35 @@ public class SwitchExpressionsYieldTest extends AbstractRegressionTest { "A switch labeled block in a switch expression should not complete normally\n" + "----------\n"); } + public void testBug562728_007() { + this.runNegativeTest( + new String[] { + "X.java", + "public class X { \n"+ + " public static int foo(int i) { \n"+ + " int v; \n"+ + " int t = switch (i) { \n"+ + " case 0 -> { \n"+ + " return 1;\n"+ + " } \n"+ + " default ->100;\n"+ + " }; \n"+ + " return t; \n"+ + " } \n"+ + " \n"+ + " public boolean bar() { \n"+ + " return true; \n"+ + " }\n"+ + " public static void main(String[] args) {\n"+ + " System.out.println(foo(3));\n"+ + " } \n"+ + "}\n" + }, + "----------\n" + + "1. ERROR in X.java (at line 6)\n" + + " return 1;\n" + + " ^^^^^^^^^\n" + + "Return within switch expressions not permitted\n" + + "----------\n"); +} } 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 6eb059540e..40864bc1bb 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 @@ -2234,6 +2234,8 @@ void setSourceStart(int sourceStart); int SwitchExpressionsBreakOutOfSwitchExpression = Syntax + 1722; /** @since 3.22 */ int SwitchExpressionsContinueOutOfSwitchExpression = Syntax + 1723; + /** @since 3.22 */ + int SwitchExpressionsReturnWithinSwitchExpression = Syntax + 1724; /* records - begin */ /** @since 3.22 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 42b6e6342a..be79ed528b 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 @@ -105,9 +105,6 @@ public class SwitchExpression extends SwitchStatement implements IPolyExpression if (block.doesNotCompleteNormally()) { return BREAKING; } - //JLS 12 15.28.1 Given a switch expression, if the switch block consists of switch labeled rules, - //then it is a compile-time error if any switch labeled block can complete normally. - blockScope.problemReporter().switchExpressionSwitchLabeledBlockCompletesNormally(block); } return FALLTHROUGH; } @@ -144,9 +141,20 @@ public class SwitchExpression extends SwitchStatement implements IPolyExpression @Override protected void completeNormallyCheck(BlockScope blockScope) { - if (this.switchLabeledRules) return; // already taken care in getFallThroughState() int sz = this.statements != null ? this.statements.length : 0; if (sz == 0) return; + /* JLS 12 15.28.1 Given a switch expression, if the switch block consists of switch labeled rules + * then it is a compile-time error if any switch labeled block can complete normally. + */ + if (this.switchLabeledRules) { + for (Statement stmt : this.statements) { + if (!(stmt instanceof Block)) + continue; + if (!stmt.doesNotCompleteNormally()) + blockScope.problemReporter().switchExpressionLastStatementCompletesNormally(stmt); + } + return; + } /* JLS 12 15.28.1 * If, on the other hand, the switch block consists of switch labeled statement groups, then it is a * compile-time error if either the last statement in the switch block can complete normally, or the @@ -427,6 +435,11 @@ public class SwitchExpression extends SwitchStatement implements IPolyExpression return true; } @Override + public boolean visit(ReturnStatement stmt, BlockScope blockScope) { + blockScope.problemReporter().switchExpressionsReturnWithinSwitchExpression(stmt); + return false; + } + @Override public boolean visit(TypeDeclaration stmt, BlockScope blockScope) { return false; } 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 2ba8fdcf14..69f9dd75ea 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 @@ -11479,6 +11479,14 @@ public void switchExpressionsContinueOutOfSwitchExpression(ASTNode statement) { statement.sourceStart, statement.sourceEnd); } +public void switchExpressionsReturnWithinSwitchExpression(ASTNode statement) { + this.handle( + IProblem.SwitchExpressionsReturnWithinSwitchExpression, + NoArgument, + NoArgument, + statement.sourceStart, + statement.sourceEnd); +} public void illegalModifierForInnerRecord(SourceTypeBinding type) { if (!this.options.enablePreviewFeatures) return; 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 76ac8be9d2..1518d0abb7 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 @@ -1020,6 +1020,7 @@ 1721 = Switch Expressions are supported from Java 14 onwards only 1722 = Breaking out of switch expressions not permitted 1723 = Continue out of switch expressions not permitted +1724 = Return within switch expressions not permitted # Java 14 Preview - begin # Records 1730 = Illegal modifier for the record {0}; only public, private, protected, static, final and strictfp are permitted |