Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8222850

Misleading compiler error in switch expression when using jshell

    Details

    • Type: Bug
    • Status: Open
    • Priority: P4
    • Resolution: Unresolved
    • Affects Version/s: 12.0.1
    • Fix Version/s: tbd
    • Component/s: tools
    • Labels:
    • Subcomponent:
    • CPU:
      x86
    • OS:
      generic

      Description

      When writing the following code:

        var x = switch (foo) { case A, B -> "x"; };

      I expect there to be a compiler error because foo is not defined as a variable. In jshell, I'm getting quite a few error messages:

        jshell> var x = switch (foo) { case A, B -> "x"; };
        | Error:
        | cannot find symbol
        | symbol: variable foo
        | var x = switch (foo) { case A, B -> "x"; };
        | ^-^
        | Error:
        | illegal parenthesized expression
        | var x = switch (foo) { case A, B -> "x"; };
        | ^---^
        | Error:
        | cannot find symbol
        | symbol: variable A
        | var x = switch (foo) { case A, B -> "x"; };
        | ^
        | Error:
        | cannot find symbol
        | symbol: variable B
        | var x = switch (foo) { case A, B -> "x"; };
        | ^
        | Error:
        | the switch expression does not cover all possible input values
        | var x = switch (foo) { case A, B -> "x"; };
        | ^--------------------------------^

      The last error message about not covering all possible input values is a bit confusing, as it seems to be premature. I don't get that error when using javac directly on:

        class Foo {
          public static void main(String[] args) {
            var x = switch (foo) { case A, B -> "x"; };
          }
        }

      Instead, I'm getting:

        C:\Users\lukas>"\Program Files\Java\jdk-12.0.1\bin\javac.exe" --enable- preview --release 12 Foo.java
        Foo.java:3: error: cannot find symbol
            var x = switch (foo) { case A, B -> "x"; };
                            ^
          symbol: variable foo
          location: class Foo
        Foo.java:3: error: illegal parenthesized expression
            var x = switch (foo) { case A, B -> "x"; };
                           ^
        Foo.java:3: error: cannot find symbol
            var x = switch (foo) { case A, B -> "x"; };
                                        ^
          symbol: variable A
          location: class Foo
        Foo.java:3: error: cannot find symbol
            var x = switch (foo) { case A, B -> "x"; };
                                         ^
          symbol: variable B
          location: class Foo
        Note: Foo.java uses preview language features.
        Note: Recompile with -Xlint:preview for details.
        4 errors

      I suggest removing the premature error message in jshell

        Attachments

          Activity

            People

            • Assignee:
              rfield Robert Field
              Reporter:
              leder Lukas Eder
            • Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

              • Created:
                Updated: