RFR: 8274363: Transitively sealed classes not considered exhaustive in switches
Vicente Romero
vromero at openjdk.java.net
Mon Sep 27 18:53:58 UTC 2021
On Mon, 27 Sep 2021 18:05:26 GMT, Jan Lahoda <jlahoda at openjdk.org> wrote:
> Consider code like:
>
> public class SwitchCoverage {
> sealed interface A {}
> sealed interface B1 extends A {}
> sealed interface B2 extends A {}
> sealed interface C extends A {}
> final class D1 implements B1, C {}
> final class D2 implements B2, C {}
>
> void test(A arg) {
> int i = switch (arg) {
> case B1 b1 -> 1;
> case B2 b2 -> 2;
> };
> }
>
> }
>
>
> Note that `B1` covers `D1` and `B2` covers `D2`. So, `B1` and `B2` cover `C`, and hence `B1` and `B2` cover `A`. To detect this, when looking for coverage of permitted types of `A`, we need to transitively look if any of the permitted type is covered.
>
> The check uses the `DeferredCompletionFailureHandler`, because if don't want to fail the compilation on unresolvable permitted subclass. (In case the permitted subclass is covered by some other means, of course.)
src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Flow.java line 776:
> 774: for (Type sup : types.directSupertypes(sym.type)) {
> 775: if (sup.tsym.kind == TYP) {
> 776: if (isTransitivellyCovered(sup.tsym, covered) &&
typo: Transitively?
-------------
PR: https://git.openjdk.java.net/jdk/pull/5717
More information about the compiler-dev
mailing list