Pattern matching for switch: Spec and Javac inconsistency regarding dominance in switch blocks

Ilyas Selimov ilyas.selimov at
Mon Jul 19 10:02:30 UTC 2021


I found a difference between dominance rules for switch blocks in spec
draft (

and its javac implementation (OpenJDK 17 build 17-ea+31-2664).

> A switch label that has a pattern case label element p dominates another
switch label that has a constant case label element c if either of the
following is true:
> - the type of c is a primitive type and its wrapper class (5.1.7) is a
subtype of the erasure of the type of p.
> - the type of c is a reference type and is a subtype of the erasure of
the type of p.

> It is a compile-time error if a switch label in a switch block dominates
any switch label that follows it in the switch block.

The next code compiles correctly, but it seems to contradict the rules
void test(Integer i) {
  switch (i) {
    case Integer in && in != null:
    case 1:
    case default:

Possibly p should be total for the type of selector expression like it was
made for pattern-over-null dominance?

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the compiler-dev mailing list