RFR: 8262889: Compiler implementation for Record Patterns
Maurizio Cimadamore
mcimadamore at openjdk.java.net
Thu May 5 15:48:37 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.
I've added some renaming suggestions for the code in Flow, after some discussions with @biboudis.
More generally, that code should contain comments, with example of what it's trying to do - e.g. how it's partitioning the set of patterns, etc.
src/java.base/share/classes/jdk/internal/javac/PreviewFeature.java line 64:
> 62: public enum Feature {
> 63: SWITCH_PATTERN_MATCHING,
> 64: DECONSTRUCTION_PATTERNS,
The spec and JEP talks about record patterns - I think we should follow the correct name here
src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Flow.java line 751:
> 749: Iterable<? extends JCCaseLabel> labels) {
> 750: Set<Symbol> constants = new HashSet<>();
> 751: Map<Symbol, List<JCDeconstructionPattern>> categorizedDeconstructionPatterns = new HashMap<>();
maybe rename to `deconstructionPatternsBySymbol` ?
src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Flow.java line 790:
> 788: }
> 789:
> 790: private boolean coversDeconstructionStartingFromComponent(DiagnosticPosition pos,
maybe rename as `coverDeconstructionFromComponent`/`coverDeconstructionAt`
src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Flow.java line 792:
> 790: private boolean coversDeconstructionStartingFromComponent(DiagnosticPosition pos,
> 791: Type targetType,
> 792: List<JCDeconstructionPattern> patterns,
patterns -> "deconstructionPatterns"
src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Flow.java line 800:
> 798: }
> 799:
> 800: Type parameterizedComponentType = types.memberType(targetType, components.get(component));
maybe `instantiatedComponentType`
src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Flow.java line 802:
> 800: Type parameterizedComponentType = types.memberType(targetType, components.get(component));
> 801: List<JCPattern> nestedComponentPatterns = patterns.map(d -> d.nested.get(component));
> 802: Set<Symbol> nestedCovered = coveredSymbols(pos, parameterizedComponentType,
maybe `coveredComponents`
src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Flow.java line 807:
> 805: Set<Symbol> covered = new HashSet<>();
> 806:
> 807: for (JCDeconstructionPattern subTypeCandidate : patterns) {
`subTypeCandidate` -> `deconstructionPattern`
src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Flow.java line 809:
> 807: for (JCDeconstructionPattern subTypeCandidate : patterns) {
> 808: JCPattern nestedPattern = subTypeCandidate.nested.get(component);
> 809: Symbol currentPatternType;
`currentPatternType` -> `componentPatternType`
src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Flow.java line 823:
> 821: }
> 822: }
> 823: for (Symbol currentType : nestedCovered) {
`currentType` -> `coveredComponent`
src/jdk.compiler/share/classes/com/sun/tools/javac/tree/JCTree.java line 246:
> 244: PARENTHESIZEDPATTERN,
> 245:
> 246: DECONSTRUCTIONPATTERN,
might want to rename to RECORDPATTERN (but this is impl dependent, so less important to fix)
src/jdk.compiler/share/classes/com/sun/tools/javac/tree/JCTree.java line 2373:
> 2371: }
> 2372:
> 2373: public static class JCDeconstructionPattern extends JCPattern
JCRecordPattern (although this is impl-only, so less important to fix)
-------------
PR: https://git.openjdk.java.net/jdk/pull/8516
More information about the core-libs-dev
mailing list