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

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


Hello!

I found a difference between dominance rules for switch blocks in spec
draft (
http://cr.openjdk.java.net/~gbierman/jep406/jep406-20210527/specs/patterns-switch-jls.html#jls-14.11.1)

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

Spec:
> 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
above:
void test(Integer i) {
  switch (i) {
    case Integer in && in != null:
      break;
    case 1:
      break;
    case default:
      break;
  }
}

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

Thanks,
Ilyas
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.java.net/pipermail/compiler-dev/attachments/20210719/8e78372b/attachment.htm>


More information about the compiler-dev mailing list