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

jshell tool: Misleading cascade compiler error in switch expression with undefined vars

    XMLWordPrintable

Details

    • Bug
    • Resolution: Fixed
    • P4
    • 17
    • 12.0.1, 14
    • tools
    • b08
    • x86
    • 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

        Issue Links

          Activity

            People

              rfield Robert Field (Inactive)
              leder Lukas Eder
              Votes:
              0 Vote for this issue
              Watchers:
              6 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: