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

Aggelos Biboudis duke at openjdk.java.net
Thu Apr 14 08:53:10 UTC 2022

On Tue, 12 Apr 2022 13:18:14 GMT, Jan Lahoda <jlahoda at openjdk.org> wrote:

>> 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!
> Jan Lahoda has updated the pull request incrementally with one additional commit since the last revision:
>   Cleanup.

src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java line 177:

> 175:         allowPatternSwitch = (preview.isEnabled() || !preview.isPreview(Feature.PATTERN_SWITCH)) &&
> 176:                              Feature.PATTERN_SWITCH.allowedInSource(source);
> 177:         allowTotalPatternsInstance = (preview.isEnabled() || !preview.isPreview(Feature.TOTAL_PATTERN_IN_INSTACEOF)) &&

BTW, do we need to rename total -> unconditional with the recent update in the spec?

src/jdk.compiler/share/classes/com/sun/tools/javac/tree/TreeInfo.java line 1349:

> 1347:     public static PatternPrimaryType primaryPatternType(JCTree pat) {
> 1348:         return switch (pat.getTag()) {
> 1349:             case BINDINGPATTERN -> new PatternPrimaryType(((JCBindingPattern) pat).type);

Cast may be redundant here since the signature of primaryPatternType changed to access JCTrees

src/jdk.compiler/share/classes/com/sun/tools/javac/tree/TreeInfo.java line 1377:

> 1375:         }
> 1376:         JCExpression guard = ((JCPattern) cse).guard;
> 1377:         if (guard != null && guard.type.hasTag(BOOLEAN)) {

In the spec we see the following:

> A pattern case element is said to be unrefined if either (i) it has no associated when expression, or (ii) it has an associated when expression that is a constant expression ([15.29](https://docs.oracle.com/javase/specs/jls/se18/html/jls-15.html#jls-15.29)) with value true. A case element is unrefined if it is not a pattern case element or it is an unrefined pattern case element.

However, in the definition of `unconditional` I don't see the part for the constant expression.

I wonder if the spec should be updated or the code at this point


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

More information about the compiler-dev mailing list