Integrated: JDK-8222850: jshell tool: Misleading cascade compiler error in switch expression with undefined vars
Jan Lahoda
jlahoda at openjdk.java.net
Wed Feb 3 09:25:43 UTC 2021
On Mon, 1 Feb 2021 15:09:05 GMT, Jan Lahoda <jlahoda at openjdk.org> wrote:
> Consider code like:
> switch (undefined) {
> case A -> {}
> case B, C -> {}
> case D -> {}
> }
>
> javac will produce these errors for it:
> $ javac Switch.java
> Switch.java:3: error: cannot find symbol
> switch (undefined) {
> ^
> symbol: variable undefined
> location: class Switch
> Switch.java:3: error: illegal parenthesized expression
> switch (undefined) {
> ^
> Switch.java:4: error: cannot find symbol
> case A -> {}
> ^
> symbol: variable A
> location: class Switch
> Switch.java:5: error: cannot find symbol
> case B, C -> {}
> ^
> symbol: variable B
> location: class Switch
> Switch.java:5: error: cannot find symbol
> case B, C -> {}
> ^
> symbol: variable C
> location: class Switch
> Switch.java:6: error: cannot find symbol
> case D -> {}
> ^
> symbol: variable D
> location: class Switch
> 6 errors
>
> For jshell and switch expressions, there may be one more error reported at the end:
> jshell> var v = switch (undefined) {case A -> 0;};
> | Error:
> | cannot find symbol
> | symbol: variable undefined
> | var v = switch (undefined) {case A -> 0;};
> | ^-------^
> | Error:
> | illegal parenthesized expression
> | var v = switch (undefined) {case A -> 0;};
> | ^---------^
> | Error:
> | cannot find symbol
> | symbol: variable A
> | var v = switch (undefined) {case A -> 0;};
> | ^
> | Error:
> | the switch expression does not cover all possible input values
> | var v = switch (undefined) {case A -> 0;};
> | ^-------------------------------^
>
> We could prevent all the follow-up error reports, and keep only the first one, which is the real error in the code.
This pull request has now been integrated.
Changeset: 90376156
Author: Jan Lahoda <jlahoda at openjdk.org>
URL: https://git.openjdk.java.net/jdk/commit/90376156
Stats: 63 lines in 6 files changed: 59 ins; 0 del; 4 mod
8222850: jshell tool: Misleading cascade compiler error in switch expression with undefined vars
Reviewed-by: vromero
-------------
PR: https://git.openjdk.java.net/jdk/pull/2336
More information about the compiler-dev
mailing list