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

Exhaustive Switch Statement is Claimed to be Missing a Case

    XMLWordPrintable

Details

    • generic
    • generic

    Description

      A DESCRIPTION OF THE PROBLEM :
      The switch expression is claimed to be inexhaustive, even though it covers every case.

      We have to consider all the combinations of parameters c,d of record R_D.

      If we only expand c twice, we get either R_A(R_A(...),...) or R_A(R_B(),...), as c is of type R_A and R_A's first parameter a is of type I_A. Since R_A and R_B are the only two records that extend I_A, this is all we have to consider.

      R_A(R_A... is trivially covered in case 1.

      R_A(R_B is more complex, it is only paired up with R_A(R_B in case 2, but it would have to be paired up with every possible implementation of I_A (as that is the type of parameter d).

      R_A and R_B implement I_A. R_B is covered in case 4. (var a can be R_A(R_B )
      For R_A we have the same two possibilities as before, namely R_A(R_B and R_A(R_A.

      Both of them are covered in cases 2 and 3.

      With that the switch statement covers all possible values and is exhaustive

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Run the source code with javac 24.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      The compiler should not issue an error for a switch expression that does not cover all possible input values.
      ACTUAL -
      It reports an error

      ---------- BEGIN SOURCE ----------
      class TestClass {
          sealed interface I_A {
          }

          sealed interface I_B {
          }

          record R_A(I_A a, I_B b) implements I_A {
          }

          record R_B() implements I_A {
          }

          record R_D(R_A c, I_A d) implements I_B {
          }

          public static void main(String args[]) {

              I_B v_a = null;
              Integer v_b = switch (v_a) {
                  case R_D(R_A(R_A(var a, var b), var c), var d) -> 1;

                  case R_D(R_A(R_B(), var b), R_A(R_B(), var x)) -> 2;

                  case R_D(var a, R_A(R_A(var b, var c), var d)) -> 3;
                  case R_D(var a, R_B()) -> 4;
              };
          }

      };
      ---------- END SOURCE ----------

      FREQUENCY : always


      Attachments

        Activity

          People

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

            Dates

              Created:
              Updated:
              Resolved: