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

Exhaustive switch expression rejected by for not covering all possible values

XMLWordPrintable

    • generic
    • generic

      ADDITIONAL SYSTEM INFORMATION :
      Debian 12.11
      6.1.0-38-amd64 #1 SMP PREEMPT_DYNAMIC Debian 6.1.147-1 (2025-08-02) x86_64 GNU/Linux

      Tested with similar results on multiple temurin Docker images, from eclipse-temurin:21-jdk (d6596e18495b) to :24-jdk.

      A DESCRIPTION OF THE PROBLEM :
      A switch expression involving sealed and non-sealed interfaces is not accepted by javac despite all possible cases being covered. As a workaround a redundant case can be appended in order to have the program compile.

      In the demo code all Special instances are necessarily also Value instances, since SpecialValue is a Value and also the only permitted descendant of Special. Yet javac doesn't consider the Value case to cover Special.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Compile the demo code with javac with no extra options.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      The code compiles without error.
      ACTUAL -
      Demo.java:12: error: the switch expression does not cover all possible input values
              return switch (base) {
                     ^
      1 error

      ---------- BEGIN SOURCE ----------
      class Demo {

          sealed interface Base permits Special, Value {}

          non-sealed interface Value extends Base {}

          sealed interface Special extends Base permits SpecialValue {}

          non-sealed interface SpecialValue extends Value, Special {}

          static int demo(final Base base) {
              return switch (base) {
                  case final Value value -> 0;
                  // Uncommenting the following line will make javac accept this program
                  //case final Special value -> throw new AssertionError();
              };

          }

      }

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

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

              Created:
              Updated: