Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJay Arthanareeswaran2020-08-06 06:50:18 +0000
committerJay Arthanareeswaran2020-08-06 09:22:00 +0000
commit83eeb8ba1bb12d55b4a20f19f43db66d877ab6f0 (patch)
treea9f6e67b0a5fece6f4f518846736d3d7d75dbe8b
parentfffec414ab0eddec1965c39ad6ea55d7074d7580 (diff)
downloadeclipse.jdt.core-83eeb8ba1bb12d55b4a20f19f43db66d877ab6f0.tar.gz
eclipse.jdt.core-83eeb8ba1bb12d55b4a20f19f43db66d877ab6f0.tar.xz
eclipse.jdt.core-83eeb8ba1bb12d55b4a20f19f43db66d877ab6f0.zip
Bug 565844 - [14] Compiler rejects code with conditional expression in aI20200806-1800
switch expression's case Change-Id: I4807e10b62a516606f85adb0346f141fead11062 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/SwitchExpressionsYieldTest.java101
-rw-r--r--org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/parser/Scanner.java15
2 files changed, 114 insertions, 2 deletions
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 6bfeff329d..064afe61b8 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
@@ -5435,4 +5435,105 @@ public class SwitchExpressionsYieldTest extends AbstractRegressionTest {
},
"1");
}
+ public void testBug565844_1() {
+ runConformTest(
+ new String[] {
+ "X.java",
+ "public class X {\n"+
+ " public final static int j = 5;\n" +
+ " public static void main(String argv[]) {\n" +
+ " boolean b = \n" +
+ " switch (j) {\n" +
+ " case j != 1 ? 2 : 3 -> true;\n" +
+ " default -> false;\n" +
+ " }; \n" +
+ " System.out.println(b);\n" +
+ " }\n"+
+ "}"
+ },
+ "false");
+ }
+ public void testBug565844_2() {
+ runConformTest(
+ new String[] {
+ "X.java",
+ "public class X {\n"+
+ " public final static int j = 2;\n" +
+ " public static void main(String argv[]) {\n" +
+ " boolean b = \n" +
+ " switch (j) {\n" +
+ " case j != 1 ? 2 : (j == 2 ? 4 : 5) -> true;\n" +
+ " default -> false;\n" +
+ " }; \n" +
+ " System.out.println(b);\n" +
+ " }\n"+
+ "}"
+ },
+ "true");
+ }
+ public void testBug565844_3() {
+ runConformTest(
+ new String[] {
+ "X.java",
+ "public class X {\n"+
+ " public final static int j = 5;\n" +
+ " public static void main(String argv[]) {\n" +
+ " boolean b = \n" +
+ " switch (j) {\n" +
+ " case j != 1 ? 2 : 3 -> {\n" +
+ " yield true;\n" +
+ " }\n" +
+ " default -> { yield false;}\n" +
+ " }; \n" +
+ " System.out.println(b);\n" +
+ " }\n"+
+ "}"
+ },
+ "false");
+ }
+ public void testBug565844_4() {
+ runConformTest(
+ new String[] {
+ "X.java",
+ "public class X {\n"+
+ " public final static int j = 5;\n" +
+ " public static void main(String argv[]) {\n" +
+ " boolean b = \n" +
+ " switch (j) {\n" +
+ " case j != 1 ? 2 : 3 : {\n" +
+ " yield true;\n" +
+ " }\n" +
+ " default : { yield false;}\n" +
+ " }; \n" +
+ " System.out.println(b);\n" +
+ " }\n"+
+ "}"
+ },
+ "false");
+ }
+ public void testBug565844_5() {
+ runNegativeTest(
+ new String[] {
+ "X.java",
+ "public class X {\n"+
+ " public static int j = 5;\n" +
+ " public static void main(String argv[]) {\n" +
+ " boolean b = \n" +
+ " switch (j) {\n" +
+ " case j != 1 ? 2 : 3 -> {\n" +
+ " yield true;\n" +
+ " }\n" +
+ " default -> { yield false;}\n" +
+ " }; \n" +
+ " System.out.println(b);\n" +
+ " }\n"+
+ "}"
+ },
+ "----------\n" +
+ "1. ERROR in X.java (at line 6)\n" +
+ " case j != 1 ? 2 : 3 -> {\n" +
+ " ^^^^^^^^^^^^^^\n" +
+ "case expressions must be constant expressions\n" +
+ "----------\n");
+ }
}
diff --git a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/parser/Scanner.java b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/parser/Scanner.java
index 27abd0b8a5..020babfd86 100644
--- a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/parser/Scanner.java
+++ b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/parser/Scanner.java
@@ -111,6 +111,7 @@ public class Scanner implements TerminalTokens {
public boolean fakeInModule = false;
boolean inCase = false;
+ boolean inCondition = false;
/* package */ int yieldColons = -1;
boolean breakPreviewAllowed = false;
/**
@@ -1433,9 +1434,19 @@ private void updateCase(int token) {
if (token == TokenNamecase) {
this.inCase = true;
this.breakPreviewAllowed = true;
+ } else if (this.inCase) {
+ // This is for cases like this: case j != 1 ? 2 : (j == 2 ? 4 : 5) -> true;
+ // Turn off the inCase until we are past the ':' that belongs to the ConditionalExpression
+ if (token == TokenNameQUESTION) {
+ this.inCondition = true;
+ } else if (this.inCondition) {
+ if (token == TokenNameCOLON) {
+ this.inCondition = false;
+ }
+ } else if (token == TokenNameCOLON || token == TokenNameARROW) {
+ this.inCase = false;
+ }
}
- if (token == TokenNameCOLON || token == TokenNameARROW)
- this.inCase = false;
}
public int getNextToken() throws InvalidInputException {

Back to the top