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

The exhaustiveness errors could be improved

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Unresolved
    • Icon: P2 P2
    • 26
    • 26
    • tools
    • None

      Consider the following code/switch:
      ```
      package test;
      public class Test {
          private int test(Root r) {
              return switch (r) {
                  case Root(R2(R1 _), R2(R1 _)) -> 0;
                  case Root(R2(R1 _), R2(R2 _)) -> 0;
                  case Root(R2(R2 _), R2(R1 _)) -> 0;
              };
          }
          sealed interface Base {}
          record R1() implements Base {}
          record R2(Base b1) implements Base {}
          record Root(R2 b2, R2 b3) {}
      }
      ```

      This switch is obviously not exhaustive, as there's a case missing for `Root(R2(R2 _), R2(R2 _))`. But, javac currently is not particularly helpful in finding this missing case:
      ```
      $ javac test/Test.java
      .../test/Test.java:4: error: the switch expression does not cover all possible input values
              return switch (r) {
                     ^
      1 error
      ```

      It would be better if javac produced an error message pointing out the missing possibility/ies, at least in some cases.

            jlahoda Jan Lahoda
            jlahoda Jan Lahoda
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated: