RFR: 8274363: Transitively sealed classes not considered exhaustive in switches
Jan Lahoda
jlahoda at openjdk.java.net
Mon Sep 27 18:19:43 UTC 2021
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.)
-------------
Commit messages:
- Fixing tests.
- 8274363: Transitively sealed classes not considered exhaustive in switches
Changes: https://git.openjdk.java.net/jdk/pull/5717/files
Webrev: https://webrevs.openjdk.java.net/?repo=jdk&pr=5717&range=00
Issue: https://bugs.openjdk.java.net/browse/JDK-8274363
Stats: 72 lines in 2 files changed: 54 ins; 8 del; 10 mod
Patch: https://git.openjdk.java.net/jdk/pull/5717.diff
Fetch: git fetch https://git.openjdk.java.net/jdk pull/5717/head:pull/5717
PR: https://git.openjdk.java.net/jdk/pull/5717
More information about the compiler-dev
mailing list