RFR: 8262889: Compiler implementation for Record Patterns

Aggelos Biboudis abimpoudis at openjdk.java.net
Thu May 5 12:09:18 UTC 2022


On Tue, 3 May 2022 12:07:50 GMT, Jan Lahoda <jlahoda at openjdk.org> wrote:

> 8262889: Compiler implementation for Record Patterns
> 
> A first version of a patch that introduces record patterns into javac as a preview feature. For the specification, please see:
> http://cr.openjdk.java.net/~gbierman/jep427+405/jep427+405-20220426/specs/patterns-switch-record-patterns-jls.html
> 
> There are two notable tricky parts:
> -in the parser, it was necessary to improve the `analyzePattern` method to handle nested/record patterns, while still keeping error recovery reasonable
> -in the `TransPatterns`, the desugaring of the record patterns is very straightforward - effectivelly the record patterns are desugared into guards/conditions. This will likely be improved in some future version/preview
> 
> `MatchException` has been extended to cover additional cases related to record patterns.

src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Flow.java line 783:

> 781:             }
> 782:             for (Entry<Symbol, List<JCDeconstructionPattern>> e : categorizedDeconstructionPatterns.entrySet()) {
> 783:                 if (coversDeconstructionStartingFromComponent(pos, targetType, e.getValue(), 0)) {

Here, the result of `e.getValue` is a reversed list of the observed patterns. 

For the switch below,


switch (r) {
            case R(A a, A b) -> 1;
            case R(A a, B b) -> 2;
            case R(B a, A b) -> 3;
            case R(B a, B b) -> 4;
}


The 0th element of that list is the `R(B a, B b)` pattern, the 1st the `R(B a, A b)`. I am 99% sure that this is not a problem but I am pointing it out regardless, in case there is any underlying danger to that.

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

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


More information about the compiler-dev mailing list