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

Jan Lahoda jlahoda at openjdk.java.net
Mon Feb 1 15:15:05 UTC 2021


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


More information about the compiler-dev mailing list