RFR: 8273328: Compiler implementation for Pattern Matching for switch (Second Preview)

Maurizio Cimadamore mcimadamore at openjdk.java.net
Thu Nov 4 13:03:14 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

Looks good. Left one comment about the logic in Flow.

src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Flow.java line 788:

> 786: 
> 787:         private boolean isTransitivelyCovered(Type seltype, Symbol sealed, Set<Symbol> covered) {
> 788:             DeferredCompletionFailureHandler.Handler prevHandler =

Why do we need to guard against completion failures here in Flow?

-------------

Marked as reviewed by mcimadamore (Reviewer).

PR: https://git.openjdk.java.net/jdk/pull/6209


More information about the compiler-dev mailing list