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

Exhaustive Switch Statement is Claimed to be Missing a Case

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P4 P4
    • None
    • 22.0.2
    • tools
    • generic
    • generic

      A DESCRIPTION OF THE PROBLEM :
      sealed interface I_A {
          }

          sealed interface I_B {
          }

          record R_A(I_A a) implements I_A {
          }

          record R_B(R_A a) implements I_A {
          }

          record R_D(R_A a, I_A b) 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(var a), R_B(var c)) -> 1;
                  case R_D(R_A(R_A(var a)), var d) -> 2;
                  case R_D(R_A(R_B(var a)), R_A(var c)) -> 3;
              };
          }

      The compiler claims the above program to contain a switch that does not cover all possible input values. But if we replace case 2 with case R_D(R_A(R_A(var a)), R_A(var d)) -> 2; (which is a more specific case), it suddenly thinks it is exhaustive.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Describe in the description above

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      The original switch statement should behave the same way as the more specific switch statement, both should be exhaustive.
      ACTUAL -
      The switch statement with var d instead of R_A(var d) is claimed to be inexhaustive.

      ---------- BEGIN SOURCE ----------

      class TestClass {
          sealed interface I_A {
          }

          sealed interface I_B {
          }

          record R_A(I_A a) implements I_A {
          }

          record R_B(R_A a) implements I_A {
          }

          record R_D(R_A a, I_A b) 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(var a), R_B(var c)) -> 1;
                  case R_D(R_A(R_A(var a)), var d) -> 2; // case R_D(R_A(R_A(var a)), R_A(var d)) -> 3;
                  case R_D(R_A(R_B(var a)), R_A(var c)) -> 3;
              };
          }

      };

      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Replacing the pattern with the more specific case

      FREQUENCY : always


            cgaffney Cody Gaffney
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: