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

Unstable exhaustiveness for record pattern

XMLWordPrintable

    • generic
    • generic

      ADDITIONAL SYSTEM INFORMATION :
      Java 21 Build 30 (2023/7/6)
      Windows

      A DESCRIPTION OF THE PROBLEM :
      Consider this code:
      ```
      class Test22 {
          record Pair(I i1, I i2) {
          }

          sealed interface I {
          }

          record C() implements I {
          }

          record D() implements I {
          }

          void exhaustinvenessWithInterface(Pair pairI) {
              switch (pairI) {
                  case Pair(C fst, D snd) -> {
                  }
                  case Pair(C fst, C snd) -> {
                  }
                  case Pair(I fst, C snd) -> {
                  }
                  case Pair(D fst, D snd) -> {
                  }
              }
          }
      }
      ```
      This code is compilable.
      But if we change first component to second and second component to first, it produces error

      ```
      class Test22 {

          record Pair(I i1, I i2) { }

          sealed interface I { }

          record C() implements I { }

          record D() implements I { }

          void exhaustinvenessWithInterface(Pair pairI) {
              switch (pairI) {
                  case Pair(D fst, C snd) -> {}
                  case Pair(C fst, C snd) -> {}
                  case Pair(C fst, I snd) -> {}
                  case Pair(D fst, D snd) -> {}
              }
          }
      }
      ```

      But it must compile according to the JEP 440+441, because it doesn't specify the order to find a distinguish component q (14.1.1)

      REGRESSION : Last worked in version 21

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :

      ```
      class Test22 {

          record Pair(I i1, I i2) { }

          sealed interface I { }

          record C() implements I { }

          record D() implements I { }

          void exhaustinvenessWithInterface(Pair pairI) {
              switch (pairI) {
                  case Pair(D fst, C snd) -> {}
                  case Pair(C fst, C snd) -> {}
                  case Pair(C fst, I snd) -> {}
                  case Pair(D fst, D snd) -> {}
              }
          }
      }
      ```

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      This code is compiled
      ACTUAL -
      java: the switch statement does not cover all possible input values

      FREQUENCY : always


            adev Anupam Dev
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: