RFR: 8294942: Compiler implementation for Record Patterns (Second Preview)

Maurizio Cimadamore mcimadamore at openjdk.org
Tue Nov 1 12:40:48 UTC 2022


On Fri, 21 Oct 2022 11:56:50 GMT, Jan Lahoda <jlahoda at openjdk.org> wrote:

> This is a partial implementation of [JEP 432: Record Patterns (Second Preview)](https://openjdk.org/jeps/432) and [JEP 433: Pattern Matching for switch (Fourth Preview)](https://openjdk.org/jeps/433). Namely, it implements:
> 
>  - removal of named record patterns
>  - (preview) type inference for type test and record patterns
>  - cleaner switch case specification (e.g. no combination of `null` constants and type test patterns)
>  - fixing exhaustiveness of certain switches
> 
> The patch does not contain support for record patterns in enhanced for statements, that is [part of a separate pull request](https://github.com/openjdk/jdk/pull/10798).
> 
> For more information on the changes please see:
>  - the JEPs: [JEP 432](https://openjdk.org/jeps/432) and [JEP 433](https://openjdk.org/jeps/433)
>  - the CSRs: [JEP 432 - JDK-8294944](https://bugs.openjdk.org/browse/JDK-8294944) and [JEP 433 - JDK-8294946](https://bugs.openjdk.org/browse/JDK-8294946)
>  - the current [specification draft](http://cr.openjdk.java.net/~gbierman/jep432%2b433/jep432%2b433-20221028/specs/patterns-switch-record-patterns-jls.html)
> 
> Current total specdiff for both this PR and [the enhanced for PR](https://github.com/openjdk/jdk/pull/10798) is [here](http://cr.openjdk.java.net/~jlahoda/8294945/specdiff.preliminary.00/overview-summary.html).
> 
> Any feedback is welcome.
> 
> Thanks!

src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Infer.java line 678:

> 676: 
> 677:         //step 2:
> 678:         Set<Symbol> patternTypeSuperTypes = new HashSet<>();

Can you use `Infer::getParameterizedSupers` for this?

src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Infer.java line 714:

> 712:             } else {
> 713:                 List<Type> upperBounds = undet.getBounds(InferenceBound.UPPER);
> 714:                 Type bound = upperBounds.isEmpty() ? syms.objectType : types.glb(upperBounds);

I'm not sure this is 100% correct. E.g. what if an under variable has an upper bound that refers to itself - e.g. `T <: Foo<T>`. In this case we need to step - first create the new tvars, then replace new tvars in the bound of the old ones. We do this in `Infer::instantiateAsUninferredVars` - perhaps some code from there should be borrowed?

test/langtools/tools/javac/patterns/InferenceUnitTest.java line 164:

> 162:     }
> 163: 
> 164:     private void checkAsSub(String base, String test, String expected) {

maybe change the name of this function, since Types::asSub is no longer used

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

PR: https://git.openjdk.org/jdk/pull/10814


More information about the compiler-dev mailing list