Integrated: 8273328: Compiler implementation for Pattern Matching for switch (Second Preview)
Jan Lahoda
jlahoda at openjdk.java.net
Wed Nov 24 10:11:12 UTC 2021
On Tue, 2 Nov 2021 10:51:58 GMT, Jan Lahoda <jlahoda at openjdk.org> wrote:
> 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
This pull request has now been integrated.
Changeset: d085c2b8
Author: Jan Lahoda <jlahoda at openjdk.org>
URL: https://git.openjdk.java.net/jdk/commit/d085c2b8a790a5ddfbb01a0ea4edd4051cfb704b
Stats: 223 lines in 9 files changed: 174 ins; 16 del; 33 mod
8273328: Compiler implementation for Pattern Matching for switch (Second Preview)
Reviewed-by: vromero, mcimadamore
-------------
PR: https://git.openjdk.java.net/jdk/pull/6209
More information about the compiler-dev
mailing list