diff options
3 files changed, 100 insertions, 5 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 4432e80e53..0060cc8dd4 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 @@ -2304,4 +2304,66 @@ public class SwitchExpressionTest extends AbstractRegressionTest { new String[] { "--enable-preview"}, options); } + public void testBug545518a() { + if (this.complianceLevel < ClassFileConstants.JDK12) + return; + Map<String, String> options = getCompilerOptions(); + options.put(CompilerOptions.OPTION_EnablePreviews, CompilerOptions.ENABLED); + options.put(CompilerOptions.OPTION_ReportPreviewFeatures, CompilerOptions.IGNORE); + String message = + "----------\n" + + "1. WARNING in X.java (at line 5)\n" + + " case \"ABC\", (false ? (String) \"c\" : (String) \"d\") : break;\n" + + " ^^^^^^^^^^^^\n" + + "Dead code\n" + + "----------\n"; + + this.runNegativeTest(new String[] { + "X.java", + "public class X {\n" + + " public static void main(String [] args) {\n" + + " String arg = \"ABD\";\n" + + " switch(arg) {\n" + + " case \"ABC\", (false ? (String) \"c\" : (String) \"d\") : break;\n" + + " }\n" + + " }\n" + + "}\n" + }, + message, + null, + true, + new String[] { "--enable-preview"}, + options); + } + public void testBug545518b() { + if (this.complianceLevel < ClassFileConstants.JDK1_8) + return; + Map<String, String> options = getCompilerOptions(); + options.put(CompilerOptions.OPTION_EnablePreviews, CompilerOptions.DISABLED); + options.put(CompilerOptions.OPTION_ReportPreviewFeatures, CompilerOptions.WARNING); + String message = + "----------\n" + + "1. ERROR in X.java (at line 5)\n" + + " case \"ABC\", (false ? (String) \"c\" : (String) \"d\") : break;\n" + + " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n" + + "Multi constant case is a preview feature and disabled by default. Use --enable-preview to enable\n" + + "----------\n"; + + this.runNegativeTest(new String[] { + "X.java", + "public class X {\n" + + " public static void main(String [] args) {\n" + + " String arg = \"ABD\";\n" + + " switch(arg) {\n" + + " case \"ABC\", (false ? (String) \"c\" : (String) \"d\") : break;\n" + + " }\n" + + " }\n" + + "}\n" + }, + message, + null, + true, + new String[] { "--enable-preview"}, + options); + } } diff --git a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/SwitchTest.java b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/SwitchTest.java index cc7fc5fb1a..004768ede2 100644 --- a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/SwitchTest.java +++ b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/SwitchTest.java @@ -3106,6 +3106,30 @@ public void testBug533475() { "}\n" }); } +public void testBug545518() { + if (this.complianceLevel < ClassFileConstants.JDK1_8 || this.complianceLevel >= ClassFileConstants.JDK12) + return; + String message = + "----------\n" + + "1. ERROR in X.java (at line 5)\n" + + " case \"ABC\", (false ? (String) \"c\" : (String) \"d\") : break;\n" + + " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n" + + "The preview feature Multi constant case is only available with source level 12 and above\n" + + "----------\n"; + + this.runNegativeTest(new String[] { + "X.java", + "public class X {\n" + + " public static void main(String [] args) {\n" + + " String arg = \"ABD\";\n" + + " switch(arg) {\n" + + " case \"ABC\", (false ? (String) \"c\" : (String) \"d\") : break;\n" + + " }\n" + + " }\n" + + "}\n" + }, + message); +} public static Class testClass() { return SwitchTest.class; } diff --git a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/parser/Parser.java b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/parser/Parser.java index 73a13453b0..a63d6db510 100644 --- a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/parser/Parser.java +++ b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/parser/Parser.java @@ -2229,14 +2229,25 @@ protected void consumeCaseLabel() { // TODO : ERROR } CaseStatement caseStatement = new CaseStatement(constantExpressions[0], constantExpressions[length - 1].sourceEnd, this.intStack[this.intPtr--]); + if (constantExpressions.length > 1) { + if (this.parsingJava12Plus) { + if (this.options.enablePreviewFeatures) { + if (this.options.isAnyEnabled(IrritantSet.PREVIEW) && constantExpressions.length > 1) { + problemReporter().previewFeatureUsed(caseStatement.sourceStart, caseStatement.sourceEnd); + } + } else { + problemReporter().previewFeatureNotEnabled(caseStatement.sourceStart, caseStatement.sourceEnd, "Multi constant case"); //$NON-NLS-1$ + } + } else { + problemReporter().previewFeatureNotSupported(caseStatement.sourceStart, caseStatement.sourceEnd, "Multi constant case", CompilerOptions.VERSION_12); //$NON-NLS-1$ + } + } caseStatement.constantExpressions = constantExpressions; // Look for $fall-through$ tag in leading comment for case statement if (hasLeadingTagComment(FALL_THROUGH_TAG, caseStatement.sourceStart)) { caseStatement.bits |= ASTNode.DocumentedFallthrough; } - if (this.options.sourceLevel <= ClassFileConstants.JDK12 && constantExpressions.length > 1) { - problemReporter().previewFeatureUsed(caseStatement.sourceStart, caseStatement.sourceEnd); - } + pushOnAstStack(caseStatement); } protected void consumeCastExpressionLL1() { @@ -9529,7 +9540,6 @@ protected void consumeCaseLabelExpr() { consumeCaseLabel(); CaseStatement caseStatement = (CaseStatement) this.astStack[this.astPtr]; if (!this.parsingJava12Plus) { -// problemReporter().caseStatementWithArrowNotBelow12(caseStatement); problemReporter().previewFeatureNotSupported(caseStatement.sourceStart, caseStatement.sourceEnd, "Case Labels with '->'", CompilerOptions.VERSION_12); //$NON-NLS-1$ } else if (!this.options.enablePreviewFeatures){ problemReporter().previewFeatureNotEnabled(caseStatement.sourceStart, caseStatement.sourceEnd, "Case Labels with '->'"); //$NON-NLS-1$ @@ -9545,7 +9555,6 @@ protected void consumeDefaultLabelExpr() { consumeDefaultLabel(); CaseStatement defaultStatement = (CaseStatement) this.astStack[this.astPtr]; if (!this.parsingJava12Plus) { -// problemReporter().caseStatementWithArrowNotBelow12(defaultStatement); problemReporter().previewFeatureNotSupported(defaultStatement.sourceStart, defaultStatement.sourceEnd, "Case Labels with '->'", CompilerOptions.VERSION_12); //$NON-NLS-1$ } else if (!this.options.enablePreviewFeatures){ problemReporter().previewFeatureNotEnabled(defaultStatement.sourceStart, defaultStatement.sourceEnd, "Case Labels with '->'"); //$NON-NLS-1$ |