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

Javac incorrectly collects enum fields when verifying switch expression exhaustivness

XMLWordPrintable

    • b22
    • Not verified

      A DESCRIPTION OF THE PROBLEM :
      Since enum nested classes in enum classes are also counted as constants, switch expressions containing outer enum classes can never be compiled.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      javac SwitchEnumBug.java

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      SwitchEnumBug.java:8: error: the switch expression does not cover all possible input values
                      var result = switch (A) {
                                   ^
      1 error

      ---------- BEGIN SOURCE ----------
      public enum SwitchEnumBug {
      A;
      public enum InnerEnum { }
      static {
      var result = switch (A) {
      case A -> A;
      };
      }
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      https://github.com/AdoptOpenJDK/openjdk-jdk/blob/master/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Flow.java#L702
      Attach the filter to condition (&& s.kind == VAR)

      FREQUENCY : always


            jlahoda Jan Lahoda
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: