8224031: Cannot parse switch expressions after type cast
Summary: Correctly categorize parentheses followed by the switch keyword as cast.
Reviewed-by: mcimadamore
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/parser/JavacParser.java Thu May 16 09:21:49 2019 +0200
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/parser/JavacParser.java Thu May 16 16:42:14 2019 +0200
@@ -1655,6 +1655,7 @@
case DOUBLELITERAL: case CHARLITERAL: case STRINGLITERAL:
case TRUE: case FALSE: case NULL:
case NEW: case IDENTIFIER: case ASSERT: case ENUM: case UNDERSCORE:
+ case SWITCH:
case BYTE: case SHORT: case CHAR: case INT:
case LONG: case FLOAT: case DOUBLE: case BOOLEAN: case VOID:
return ParensResult.CAST;
--- a/test/langtools/tools/javac/switchexpr/ExpressionSwitch-old.out Thu May 16 09:21:49 2019 +0200
+++ b/test/langtools/tools/javac/switchexpr/ExpressionSwitch-old.out Thu May 16 16:42:14 2019 +0200
@@ -1,4 +1,4 @@
-ExpressionSwitch.java:39:16: compiler.err.preview.feature.disabled.plural: (compiler.misc.feature.switch.expressions)
-ExpressionSwitch.java:40:20: compiler.err.preview.feature.disabled.plural: (compiler.misc.feature.switch.rules)
-ExpressionSwitch.java:92:31: compiler.err.preview.feature.disabled.plural: (compiler.misc.feature.multiple.case.labels)
+ExpressionSwitch.java:40:16: compiler.err.preview.feature.disabled.plural: (compiler.misc.feature.switch.expressions)
+ExpressionSwitch.java:41:20: compiler.err.preview.feature.disabled.plural: (compiler.misc.feature.switch.rules)
+ExpressionSwitch.java:93:31: compiler.err.preview.feature.disabled.plural: (compiler.misc.feature.multiple.case.labels)
3 errors
--- a/test/langtools/tools/javac/switchexpr/ExpressionSwitch.java Thu May 16 09:21:49 2019 +0200
+++ b/test/langtools/tools/javac/switchexpr/ExpressionSwitch.java Thu May 16 16:42:14 2019 +0200
@@ -1,6 +1,6 @@
/*
* @test /nodynamiccopyright/
- * @bug 8206986 8222169
+ * @bug 8206986 8222169 8224031
* @summary Check expression switch works.
* @compile/fail/ref=ExpressionSwitch-old.out -source 9 -Xlint:-options -XDrawDiagnostics ExpressionSwitch.java
* @compile --enable-preview -source ${jdk.version} ExpressionSwitch.java
@@ -33,6 +33,7 @@
assertEquals(convert2("C"), 1);
assertEquals(convert2(""), -1);
localClass(T.A);
+ assertEquals(castSwitchExpressions(T.A), "A");
}
private String print(T t) {
@@ -121,6 +122,13 @@
}
}
+ private String castSwitchExpressions(T t) {
+ return (String) switch (t) {
+ case A -> "A";
+ default -> 1;
+ };
+ }
+
private void check(T t, String expected) {
String result = print(t);
assertEquals(result, expected);