RFR: 8287236: Reorganize AST related to pattern matching for switch

Jan Lahoda jlahoda at openjdk.java.net
Tue May 31 16:20:24 UTC 2022


Under JEP 427, case label elements may be: a) expressions; b) patterns; c) the default. Currently, this is modeled in the Trees API in a way where all expressions and patterns extend `CaseLabelTree`.

When guarded patterns were removed in favor of guards on pattern case label element, it was necessary to augment all patterns with a guard, which is only used when the pattern is used as a case label element. This is somewhat inconsistent for the Trees API.

A use a layer of indirection is proposed here: - the `CaseLabelTree` has three subtypes: `ConstantCaseLabelTree`, which contains `ExpressionTree` as a subnode; `PatternCaseLabelTree`, which contains `PatternTree` and a guard (`ExpressionTree`) as subnodes; and the `DefaultCaseLabelTree` for the default clause.

This patch also fixes `TreeScanner.visitCase`, which currently iterates only over `CaseTree.getExpressions()`, ignoring patterns. It is changed to iterate over `CaseTree.getLabels()`, to get all labels.

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

Commit messages:
 - Renaming ExpressionCaseLabelTree to ConstantCaseLabelTree and corresponding updates.
 - Cleanup.
 - Cleanup.
 - Cleanup.
 - Cleanup.
 - Merge branch 'master' into JDK-8287236
 - Cleanup.
 - Cleaning up the pattern/caselabel API.

Changes: https://git.openjdk.java.net/jdk/pull/8959/files
 Webrev: https://webrevs.openjdk.java.net/?repo=jdk&pr=8959&range=00
  Issue: https://bugs.openjdk.java.net/browse/JDK-8287236
  Stats: 647 lines in 27 files changed: 421 ins; 100 del; 126 mod
  Patch: https://git.openjdk.java.net/jdk/pull/8959.diff
  Fetch: git fetch https://git.openjdk.java.net/jdk pull/8959/head:pull/8959

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


More information about the compiler-dev mailing list