RFR: 8282274: Compiler implementation for Pattern Matching for switch (Third Preview)

Jan Lahoda jlahoda at openjdk.java.net
Mon Apr 11 16:45:18 UTC 2022


This is a (preliminary) patch for javac implementation for the third preview of pattern matching for switch (type patterns in switches).

Draft JLS:
http://cr.openjdk.java.net/~gbierman/PatternSwitchPlusRecordPatterns/PatternSwitchPlusRecordPatterns-20220407/specs/patterns-switch-jls.html

The changes are:
-there are no guarded patterns anymore, guards are not bound to the CaseElement (JLS 15.28)
-a new contextual keyword `when` is used to add a guard, instead of `&&`
-`null` selector value is handled on switch level (if a switch has `case null`, it is used, otherwise a NPE is thrown), rather than on pattern matching level.
-total patterns are allowed in `instanceof`
-`java.lang.MatchException` is added for the case where a switch is exhaustive (due to sealed types) at compile-time, but not at runtime.

Feedback is welcome!

Thanks!

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

Commit messages:
 - Adding a test for a NPE from guards.
 - Cleanup
 - Merge branch 'master' into type-patterns-third
 - Guards should be a property of (pattern) case labels, not cases.
 - Merge branch 'master' into type-patterns-third
 - Adding MatchException.
 - Merge branch 'switch-null' into type-patterns-third
 - Updating null handling.
 - Merge branch 'case-guard' into type-patterns-third
 - Moving guards to cases.
 - ... and 1 more: https://git.openjdk.java.net/jdk/compare/d6f01e9d...da8401d4

Changes: https://git.openjdk.java.net/jdk/pull/8182/files
 Webrev: https://webrevs.openjdk.java.net/?repo=jdk&pr=8182&range=00
  Issue: https://bugs.openjdk.java.net/browse/JDK-8282274
  Stats: 824 lines in 49 files changed: 392 ins; 245 del; 187 mod
  Patch: https://git.openjdk.java.net/jdk/pull/8182.diff
  Fetch: git fetch https://git.openjdk.java.net/jdk pull/8182/head:pull/8182

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


More information about the compiler-dev mailing list