[jdk17] RFR: 8268871: Adjust javac to updated exhaustiveness specification [v2]

Jan Lahoda jlahoda at openjdk.java.net
Thu Jun 24 10:57:55 UTC 2021

> The current updates spec for pattern matching in switch:
> http://cr.openjdk.java.net/~gbierman/jep406/jep406-20210608/specs/patterns-switch-jls.html
> tweaks the definition of exhaustive switches so that it include handling special types, like intersection types, sealed subclasses of sealed types, like:
> sealed abstract class A permits B, C {}
> final class B extends A {}
> sealed abstract class C extends A permits D, E {}
> final class D extends C {}
> final class E extends C {}
> public class Test {
>   public static void main(String[] args) {
>      A a = new D();
>      System.out.println(
>         switch (a) {
>            case B x -> "B";
>            case D x -> "D";
>            case E x -> "E";
>         });
>   }
> }
> etc. This patch attempts to tweak javac to work according to this specification. It builds covered types based on what types are covered, i.e. if all subtypes of a sealed type are covered, the sealed type itself is covered, and this is done transitively, so that in the example above, `D` and `E` will mark `C` as covered, and then `B` and `C` will mark `A` as covered, and as the type if `a` is `A`, it means the switch is exhaustive.

Jan Lahoda has updated the pull request incrementally with one additional commit since the last revision:

  Improving tests, and performance of exhaustive switch detection.


  - all: https://git.openjdk.java.net/jdk17/pull/78/files
  - new: https://git.openjdk.java.net/jdk17/pull/78/files/b7c067c9..a7a61692

 - full: https://webrevs.openjdk.java.net/?repo=jdk17&pr=78&range=01
 - incr: https://webrevs.openjdk.java.net/?repo=jdk17&pr=78&range=00-01

  Stats: 129 lines in 2 files changed: 95 ins; 9 del; 25 mod
  Patch: https://git.openjdk.java.net/jdk17/pull/78.diff
  Fetch: git fetch https://git.openjdk.java.net/jdk17 pull/78/head:pull/78

PR: https://git.openjdk.java.net/jdk17/pull/78

More information about the compiler-dev mailing list