RFR: 8262889: Compiler implementation for Record Patterns
Maurizio Cimadamore
mcimadamore at openjdk.java.net
Wed May 4 10:58:33 UTC 2022
On Wed, 4 May 2022 09:59:33 GMT, Maurizio Cimadamore <mcimadamore 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/Attr.java line 4180:
>
>> 4178: type = attribTree(tree.var.vartype, env, varInfo);
>> 4179: } else {
>> 4180: type = resultInfo.pt;
>
> Looks good - infers the binging var type from the record component being processed. If not in a record, then I suspect resultInfo.pt is just the target expression type (e.g. var in toplevel environment).
That said, I'm not sure how this connects with `instanceof`. This patch doesn't seem to alter `visitTypeTest`. In the current code I can see this:
if (tree.pattern.getTag() == BINDINGPATTERN ||
tree.pattern.getTag() == PARENTHESIZEDPATTERN) {
attribTree(tree.pattern, env, unknownExprInfo);
...
This seems wrong for two reasons:
* it doesn't take into account the new pattern tag
* it uses an "unknown" result info when attributing, meaning that any toplevel `var` pattern will not be attributed correctly
But we seem to have tests covering record patterns and instanceof. so I'm wondering if I'm missing some code update?
-------------
PR: https://git.openjdk.java.net/jdk/pull/8516
More information about the compiler-dev
mailing list