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

Wrong inference in switch expression with "null" arm

    XMLWordPrintable

    Details

    • Type: Enhancement
    • Status: Resolved
    • Priority: P4
    • Resolution: Fixed
    • Affects Version/s: 14, 15, 16
    • Fix Version/s: 16
    • Component/s: tools
    • Labels:
      None
    • Subcomponent:
    • Resolved In Build:
      b22

      Description

      This code does not compile:

      interface I {
          void m();
      }
      interface I1 extends I {}
      interface I2 extends I {}

      static void n(I1 i1, I2 i2, int s) {
          var i_ = switch (s) { //<- Object
              case 1 -> i1;
              case 2 -> null;
              default -> i2;
          };
          if (i_ != null) {
              i_.m(); //cannot find symbol m()
          }
      }

      Javac infers Object instead of I, suggesting some issues around handling of `null` in switch expression.

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              jlahoda Jan Lahoda
              Reporter:
              mcimadamore Maurizio Cimadamore
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved: