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

javac does not recognize variable assigned in switch expression as DA

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P4 P4
    • 13
    • 12, 13
    • tools
    • None
    • b15

      As reported here:
      http://mail.openjdk.java.net/pipermail/amber-dev/2019-March/004204.html

      javac fails to compile this code:
      ---
      enum MyEnum {
          X, Y;
           
          public static void main(String[] args) {
               MyEnum myEnum = MyEnum.X;
               int o;
               var f = switch(myEnum) {
                   case X -> o = 5;
                   case Y -> o = 10;
                   // automatic exception
               };
               System.out.println(o);
           }
      }
      ---

      ---
      $ javac --enable-preview --source 13 /tmp/MyEnum.java
      /tmp/MyEnum.java:12: error: variable o might not have been initialized
               System.out.println(o);
                                  ^
      Note: /tmp/MyEnum.java uses preview language features.
      Note: Recompile with -Xlint:preview for details.
      1 error
      ---

            jlahoda Jan Lahoda
            jlahoda Jan Lahoda
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: