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

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

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

Jan Lahoda-3
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.

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

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

Changes: https://git.openjdk.java.net/jdk/pull/2336/files
 Webrev: https://webrevs.openjdk.java.net/?repo=jdk&pr=2336&range=00
  Issue: https://bugs.openjdk.java.net/browse/JDK-8222850
  Stats: 63 lines in 6 files changed: 59 ins; 0 del; 4 mod
  Patch: https://git.openjdk.java.net/jdk/pull/2336.diff
  Fetch: git fetch https://git.openjdk.java.net/jdk pull/2336/head:pull/2336

PR: https://git.openjdk.java.net/jdk/pull/2336
Reply | Threaded
Open this post in threaded view
|

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

Vicente Romero-3
On Mon, 1 Feb 2021 15:09:05 GMT, Jan Lahoda <[hidden email]> 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
Reply | Threaded
Open this post in threaded view
|

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

Jan Lahoda-3
In reply to this post by Jan Lahoda-3
On Mon, 1 Feb 2021 15:09:05 GMT, Jan Lahoda <[hidden email]> 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 <[hidden email]>
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