RFR: JDK-8222850: jshell tool: Misleading cascade compiler error in switch expression with undefined vars

Vicente Romero vromero at openjdk.java.net
Mon Feb 1 17:13:48 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.

looks good

-------------

Marked as reviewed by vromero (Reviewer).

PR: https://git.openjdk.java.net/jdk/pull/2336


More information about the compiler-dev mailing list