RFR: 8273328: Compiler implementation for Pattern Matching for switch (Second Preview)
Jan Lahoda
jlahoda at openjdk.java.net
Tue Nov 2 10:59:35 UTC 2021
The specification for pattern matching for switch (preview)[1] has been updated with two changes:
-any type of pattern (including guarded patterns) dominates constant cases. Dominance across pattern/non-constant cases is unchanged. (See the change in `Attr.java`.)
-for sealed hierarchies, it may happen some of the subtypes are impossible (not castable) to the selector type. Like, for example:
sealed interface I<T> {}
final class A implements I<String> {}
final class B<T> implements I<T> {}
...
I<Integer> i = ...;
switch (i) {
case A a -> {} //case not allowed as I<Integer> is not castable to A
case B b -> {}
}
But, the exhaustiveness checks in JDK 17 required all the permitted subclasses, including `A`, are covered by the switch, which forced a `default` clause in place of `case A a` for cases like this. The updated specification excludes such impossible permitted subclasses from exhaustiveness checks, so the default is no longer needed and this:
I<Integer> i = ...;
switch (i) {
case B b -> {}
}
compiles OK.
(See the change in `Flow.java`.)
[1] http://cr.openjdk.java.net/~gbierman/jep420/jep420-20211020/specs/patterns-switch-jls.html
-------------
Commit messages:
- Fixing tests.
- Merge branch 'master' into JDK-8273328
- Updating to reflect the new spec.
Changes: https://git.openjdk.java.net/jdk/pull/6209/files
Webrev: https://webrevs.openjdk.java.net/?repo=jdk&pr=6209&range=00
Issue: https://bugs.openjdk.java.net/browse/JDK-8273328
Stats: 223 lines in 9 files changed: 174 ins; 16 del; 33 mod
Patch: https://git.openjdk.java.net/jdk/pull/6209.diff
Fetch: git fetch https://git.openjdk.java.net/jdk pull/6209/head:pull/6209
PR: https://git.openjdk.java.net/jdk/pull/6209
More information about the compiler-dev
mailing list