Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJay Arthanareeswaran2019-03-19 02:14:57 -0400
committerJay Arthanareeswaran2019-03-19 02:55:33 -0400
commit3376a0f9b0b41ccc98b973fa93072064b1f74fd9 (patch)
tree1fe0846924451b2270d82d75e1ab075fb82a78cb
parent902a54ea511d65383bbb81e4e688af3e167b06d1 (diff)
downloadeclipse.jdt.core-3376a0f9b0b41ccc98b973fa93072064b1f74fd9.tar.gz
eclipse.jdt.core-3376a0f9b0b41ccc98b973fa93072064b1f74fd9.tar.xz
eclipse.jdt.core-3376a0f9b0b41ccc98b973fa93072064b1f74fd9.zip
Bug 545518: [12] Flow analysis is skipped for multi-constant case
statement Also fixes bug 545516 to report usage of multi-constant case statement preview feature Change-Id: I7b0428d1e9eb1666847ae8c65f26d7a554da0de4 Signed-off-by: Jay Arthanareeswaran <jarthana@in.ibm.com>
-rw-r--r--org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/SwitchExpressionTest.java39
-rw-r--r--org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/CaseStatement.java24
-rw-r--r--org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/parser/Parser.java3
3 files changed, 54 insertions, 12 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 4413510866..4432e80e53 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
@@ -18,6 +18,7 @@ package org.eclipse.jdt.core.tests.compiler.regression;
import java.util.Map;
+import org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants;
import org.eclipse.jdt.internal.compiler.impl.CompilerOptions;
import junit.framework.Test;
@@ -2267,4 +2268,40 @@ public class SwitchExpressionTest extends AbstractRegressionTest {
"A switch expression should have at least one result expression\n" +
"----------\n");
}
-} \ No newline at end of file
+ public void testBug545518() {
+ if (this.complianceLevel < ClassFileConstants.JDK12)
+ return;
+ Map<String, String> options = getCompilerOptions();
+ options.put(CompilerOptions.OPTION_EnablePreviews, CompilerOptions.ENABLED);
+ options.put(CompilerOptions.OPTION_ReportPreviewFeatures, CompilerOptions.WARNING);
+ String message =
+ "----------\n" +
+ "1. WARNING in X.java (at line 5)\n" +
+ " case \"ABC\", (false ? (String) \"c\" : (String) \"d\") : break;\n" +
+ " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n" +
+ "You are using a preview language feature that may or may not be supported in a future release\n" +
+ "----------\n" +
+ "2. 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);
+ }
+}
diff --git a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/CaseStatement.java b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/CaseStatement.java
index d47025f353..cebeb89f5c 100644
--- a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/CaseStatement.java
+++ b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/CaseStatement.java
@@ -56,24 +56,26 @@ public FlowInfo analyseCode(
FlowInfo flowInfo) {
if (this.constantExpressions != null && this.constantExpressions.length > 1) {
for (Expression e : this.constantExpressions) {
- if (e.constant == Constant.NotAConstant
- && !e.resolvedType.isEnum()) {
- currentScope.problemReporter().caseExpressionMustBeConstant(e);
- }
- this.constantExpression.analyseCode(currentScope, flowContext, flowInfo);
+ analyseConstantExpression(currentScope, flowContext, flowInfo, e);
}
-
} else {
if (this.constantExpression != null) {
- if (this.constantExpression.constant == Constant.NotAConstant
- && !this.constantExpression.resolvedType.isEnum()) {
- currentScope.problemReporter().caseExpressionMustBeConstant(this.constantExpression);
- }
- this.constantExpression.analyseCode(currentScope, flowContext, flowInfo);
+ analyseConstantExpression(currentScope, flowContext, flowInfo, this.constantExpression);
}
}
return flowInfo;
}
+private void analyseConstantExpression(
+ BlockScope currentScope,
+ FlowContext flowContext,
+ FlowInfo flowInfo,
+ Expression e) {
+ if (e.constant == Constant.NotAConstant
+ && !e.resolvedType.isEnum()) {
+ currentScope.problemReporter().caseExpressionMustBeConstant(e);
+ }
+ e.analyseCode(currentScope, flowContext, flowInfo);
+}
@Override
public StringBuffer printStatement(int tab, StringBuffer output) {
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 8a13c67062..73a13453b0 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
@@ -2234,6 +2234,9 @@ protected void consumeCaseLabel() {
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() {

Back to the top