Why is this switch over sealed types not exhaustive?
Gavin Bierman
gavin.bierman at oracle.com
Tue Jun 15 16:42:41 UTC 2021
Probably a bug. We changed the definition of exhaustiveness to cover cases like this a little while ago, so the compiler is still catching up.
Gavin
> On 15 Jun 2021, at 17:31, Cay Horstmann <cay at horstmann.com> wrote:
>
> Hi, I was surprised that the following switch expression "does not cover all possible input values", according to JDK build 17-ea+26-2439.
>
> sealed abstract class A permits B, C {}
> final class B extends A {}
> sealed abstract class C extends A permits D, E {}
> final class D extends C {}
> final class E extends C {}
>
> public class Test {
> public static void main(String[] args) {
> A a = new D();
> System.out.println(
> switch (a) {
> case B x -> "B";
> case D x -> "D";
> case E x -> "E";
> });
> }
> }
>
> However, the more combersome
>
> switch (a) {
> case B x -> "B";
> case C c -> switch (c) {
> case D x -> "D";
> case E x -> "E";
> };
> }
>
> is deemed exhaustive. Is that a bug or does it work as designed?
>
> Thanks,
>
> Cay
>
> --
>
> Cay S. Horstmann | http://horstmann.com | mailto:cay at horstmann.com
More information about the amber-dev
mailing list