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

Enum: difference of behaviour between exhaustive switch vs. using default:

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Unresolved
    • Icon: P4 P4
    • 25
    • None
    • tools
    • None

      This is possibly a bug with DA/DU.

      public enum Action { IGNORE, REJECT }
      private static void bad() {
          String s;
          switch (getAction()) {
              case IGNORE:
                  s = "foo";
                  break;
              case REJECT:
                  throw new RuntimeException("REJECTED");
          };
          System.out.println(s); // <------- variable s might not have been initialized
      }

      private static void ok() {
          String s = switch (getAction()) {
              case IGNORE:
                  yield "foo";
              case REJECT:
                  throw new RuntimeException("REJECTED");
          };
          System.out.println(s); // ok !
      }

      reported and discussed in: https://mail.openjdk.org/pipermail/amber-dev/2024-December/009139.html

            jlahoda Jan Lahoda
            abimpoudis Angelos Bimpoudis
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated: