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

Exhaustive switch over a generic sealed abstract class

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Unresolved
    • Icon: P3 P3
    • tbd
    • 25
    • tools
    • None

      The following enhanced switch is accepted as exhaustive by javac, but fails at runtime with a MatchException.

      From my reading of https://docs.oracle.com/javase/specs/jls/se23/html/jls-14.html#jls-14.11.1.1 this seems like a bug.

      ```
      class B {

        abstract static sealed class Sealed<T extends Sealed<T>> permits One, Two {}

        abstract static non-sealed class One extends Sealed<One> {}

        abstract static non-sealed class Two<V extends Two<V>> extends Sealed<V> {}

        public static void test(Sealed<?> sealed) {
          switch (sealed) {
            case One one -> {}
          }
        }

        public static void main(String[] args) {
          class F extends Two<F> {}
          test(new F());
        }
      }
      ```

      $ java -fullversion
      openjdk full version "25-ea+4-344"
      $ javac -Xlint:all -Werror B.java
      $ java B
      Exception in thread "main" java.lang.MatchException
              at B.test(B.java:10)
              at B.main(B.java:17)

            Unassigned Unassigned
            cushon Liam Miller-Cushon
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated: