Skip to content

8294942: Compiler implementation for Record Patterns (Second Preview) #10814

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 15 commits into from

Conversation

lahodaj
Copy link
Contributor

@lahodaj lahodaj commented Oct 21, 2022

This is a partial implementation of JEP 432: Record Patterns (Second Preview) and JEP 433: Pattern Matching for switch (Fourth Preview). 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.

For more information on the changes please see:

Current total specdiff for both this PR and the enhanced for PR is here.

Any feedback is welcome.

Thanks!


Progress

  • Change must be properly reviewed (1 review required, with at least 1 Reviewer)
  • Change must not contain extraneous whitespace
  • Commit message must refer to an issue
  • Change requires a CSR request to be approved

Issues

  • JDK-8294942: Compiler implementation for Record Patterns (Second Preview)
  • JDK-8294945: Compiler implementation for Pattern Matching for switch (Fourth Preview)
  • JDK-8294944: Compiler implementation for Record Patterns (Second Preview) (CSR)

Reviewers

Contributors

  • Aggelos Biboudis <abimpoudis@openjdk.org>
  • Maurizio Cimadamore <mcimadamore@openjdk.org>

Reviewing

Using git

Checkout this PR locally:
$ git fetch https://git.openjdk.org/jdk pull/10814/head:pull/10814
$ git checkout pull/10814

Update a local copy of the PR:
$ git checkout pull/10814
$ git pull https://git.openjdk.org/jdk pull/10814/head

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 10814

View PR using the GUI difftool:
$ git pr show -t 10814

Using diff file

Download this PR as a diff file:
https://git.openjdk.org/jdk/pull/10814.diff

@bridgekeeper
Copy link

bridgekeeper bot commented Oct 21, 2022

👋 Welcome back jlahoda! A progress list of the required criteria for merging this PR into master will be added to the body of your pull request. There are additional pull request commands available for use with this pull request.

@openjdk openjdk bot added the csr Pull request needs approved CSR before integration label Oct 21, 2022
@openjdk
Copy link

openjdk bot commented Oct 21, 2022

@lahodaj The following label will be automatically applied to this pull request:

  • compiler

When this pull request is ready to be reviewed, an "RFR" email will be sent to the corresponding mailing list. If you would like to change these labels, use the /label pull request command.

@openjdk openjdk bot added the compiler compiler-dev@openjdk.org label Oct 21, 2022
Copy link
Member

@biboudis biboudis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some minor comments.

return switch (o) {
case Integer i when i > 0 -> 0;
case 0 -> 0;
case Integer i -> 0;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This case is not needed, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, omitting the unconditional case would make the switch non-exhaustive. While that would not be a blocker for a testcase that verifies that error are reported, I think it is cleaner to avoid unrelated errors in the source code.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I checked: the exhaustivity errors are not reported after these points. The dominance check short circuits the structure test. But I see what you mean: while the tests do not isolate the error, they are type correct modulo dominance. Sounds good to me.

return switch (o) {
case E e when e == E.A -> 0;
case B -> 0;
case E e -> 0;

This comment was marked as outdated.

return switch (o) {
default -> 0;
case (Integer i) when i > 0 -> 0;
case (Integer i) when i > 0 -> 0;

This comment was marked as outdated.

public class Test {
private int test(Integer o) {
return switch (o) {
case (Integer i) when i > 0 -> 0;

This comment was marked as outdated.

return switch (o) {
case String s when s.isEmpty() -> 0;
case "a" -> 0;
case String s -> 0;

This comment was marked as outdated.

@@ -651,6 +652,81 @@ public Type instantiateFunctionalInterface(DiagnosticPosition pos, Type funcInte
return owntype;
}
}

public Type instantiatePatternType(DiagnosticPosition pos, Type expressionType, TypeSymbol patternTypeSymbol) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pos seems unused so far.

@lahodaj lahodaj marked this pull request as ready for review October 30, 2022 18:47
@openjdk openjdk bot added the rfr Pull request is ready for review label Oct 30, 2022
@mlbridge
Copy link

mlbridge bot commented Oct 30, 2022

Webrevs

undet.setInst(bounds.head);
} else {
List<Type> upperBounds = undet.getBounds(InferenceBound.UPPER);
Type bound = upperBounds.isEmpty() ? syms.objectType : types.glb(upperBounds);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

capturedWildcards.forEach(s -> ((UndetVar) c.asUndetVar(s)).setNormal());

//step 2:
Set<Symbol> patternTypeSuperTypes = new HashSet<>();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you use Infer::getParameterizedSupers for this?

checkAsSub("A<T1>", "B", "B<T1>");
}

private void checkAsSub(String base, String test, String expected) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Contributor

@mcimadamore mcimadamore left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good - left some minor comments

List<Type> bounds = InferenceStep.EQ.filterBounds(undet, c);
if (bounds.nonEmpty()) {
undet.setInst(bounds.head);
} else {
List<Type> upperBounds = undet.getBounds(InferenceBound.UPPER);
Type bound = upperBounds.isEmpty() ? syms.objectType : types.glb(upperBounds);
Type bound;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should call this upper, for uniformity

lahodaj and others added 2 commits November 7, 2022 11:42
Co-authored-by: Maurizio Cimadamore <54672762+mcimadamore@users.noreply.github.com>
Type patternType = c.asUndetVar(patternTypeSymbol.type);
Type exprType = c.asUndetVar(expressionTypeCaptured);

capturedWildcards.forEach(s -> ((UndetVar) c.asUndetVar(s)).setNormal());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could be a minor issue but still: it doesn't feel correct to set an UndetVar created from a captured type as normal. I think that we should either create a TypeVar with the info from the CapturedType and then create a normal UndetVar with that TypeVar or we should create another category inside enum Kind:

enum Kind {
    NORMAL,
    CAPTURED,
    THROWS;
}

MODIFIABLE_CAPTURED dunno, just my opinion


try {
//step 2:
if (commonSuperWithDiffParameterization(patternType, exprType)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is not correct according to the spec. The spec says:

If T' is a parameterization of a generic class G, and there exists a supertype of R<α1, ..., αn> that is also a parameterization of G, let R' be that supertype...

the current code could look for any common supertype even if that common supertype is not G but a super type of G that is also common to R. More than that the current code will look for all common parameterized supertypes and create bonds etc. I think the right thing to do here according to the spec is:

           if (!types.isSameType(types.asSuper(patternType, exprType.tsym), exprType)) {
                return null;
            }

unless I'm missing something here


doIncorporation(c, types.noWarnings);

while (c.solveBasic(varsToSolve, EnumSet.of(InferenceStep.EQ)).nonEmpty()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if varsToSolve is equal to the number of variables in the inference context this step is a no-op and I think this should be always the case, so I think that the real resolution is happening below when instantiatePatternVars is invoked, unless I'm missing something this while loop could be safely removed

@@ -651,6 +654,90 @@ public Type instantiateFunctionalInterface(DiagnosticPosition pos, Type funcInte
return owntype;
}
}

Copy link
Contributor

@vicente-romero-oracle vicente-romero-oracle Nov 9, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: before we push this I think some javadoc, not too formal mostly for ourselves, should be added explaining what this method is doing etc, now we remember but later on we could forget some details, also a reference to the section of the spec will be very helpful

@vicente-romero-oracle
Copy link
Contributor

vicente-romero-oracle commented Nov 9, 2022

side: I extracted this example from the spec:

import java.util.function.*;

record Mapper<T>(T in, T out) implements UnaryOperator<T> {
    public T apply(T arg) { return in.equals(arg) ? out : null; }
}


class Test {
    void test(Function<? super String, ? extends CharSequence> f) {
        if (f instanceof Mapper(var in, var out)) {
            boolean shorter = out.length() < in.length();
        }
    }
}

it is not accepted by the current code, we should either modify the spec or go deeper and see if there is something missing in the current implementation

@@ -651,6 +654,90 @@ public Type instantiateFunctionalInterface(DiagnosticPosition pos, Type funcInte
return owntype;
}
}

public Type instantiatePatternType(Type expressionType, TypeSymbol patternTypeSymbol) {
if (expressionType.tsym == patternTypeSymbol)
Copy link
Contributor

@vicente-romero-oracle vicente-romero-oracle Nov 9, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure we are dealing with cases where expressionType is a type variable or an intersection type, are there tests covering these cases, sorry if I'm missing something here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, thanks! Should be fixed in 5afc602. Thanks!

ListBuffer<Type> todo = new ListBuffer<>();

//step 1 - create fresh tvars
for (Type t : vars) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure about this so just a question: shouldn't we run this loop until no more undetVars need to be instantiated? we could also limit until a maximum number of steps in case we find cases that doesn't converge to a solution

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my understanding, we are not creating new undet vars here, and we instantiate them all in this loop, and fix their bounds if needed in the next step, so this should resolve all the vars.

Copy link
Contributor

@vicente-romero-oracle vicente-romero-oracle Nov 14, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, yes I guess we should be fine and if we find any issue in the future we can always go back and react then

}
}
case TYPEVAR -> {
todo = todo.prepend(((TypeVar) current).getUpperBound());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here you probably want to skip all possible upper bounds that happen to be type vars using Types::skipTypeVars

Copy link
Contributor

@vicente-romero-oracle vicente-romero-oracle left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just added a nit comment. Really nice job, very complex patch, lots of details and moving parts, very vertical also with mostly all phases affected adding to the complexity, lots of fun to reviewed it too. Well done!

@openjdk openjdk bot removed the csr Pull request needs approved CSR before integration label Nov 16, 2022
@openjdk
Copy link

openjdk bot commented Nov 16, 2022

@lahodaj This change now passes all automated pre-integration checks.

ℹ️ This project also has non-automated pre-integration requirements. Please see the file CONTRIBUTING.md for details.

After integration, the commit message for the final commit will be:

8294942: Compiler implementation for Record Patterns (Second Preview)
8294945: Compiler implementation for Pattern Matching for switch (Fourth Preview)

Co-authored-by: Aggelos Biboudis <abimpoudis@openjdk.org>
Co-authored-by: Maurizio Cimadamore <mcimadamore@openjdk.org>
Reviewed-by: mcimadamore, vromero

You can use pull request commands such as /summary, /contributor and /issue to adjust it as needed.

At the time when this comment was updated there had been 23 new commits pushed to the master branch:

As there are no conflicts, your changes will automatically be rebased on top of these commits when integrating. If you prefer to avoid this automatic rebasing, please check the documentation for the /integrate command for further details.

➡️ To integrate this PR with the above commit message to the master branch, type /integrate in a new comment.

@lahodaj
Copy link
Contributor Author

lahodaj commented Nov 21, 2022

/issue JDK-8294945

@openjdk
Copy link

openjdk bot commented Nov 21, 2022

@lahodaj
Adding additional issue to issue list: 8294945: Compiler implementation for Pattern Matching for switch (Fourth Preview).

@lahodaj
Copy link
Contributor Author

lahodaj commented Nov 21, 2022

/contributor abimpoudis

@lahodaj
Copy link
Contributor Author

lahodaj commented Nov 21, 2022

/contributor mcimadamore

@openjdk
Copy link

openjdk bot commented Nov 21, 2022

@lahodaj this pull request can not be integrated into master due to one or more merge conflicts. To resolve these merge conflicts and update this pull request you can run the following commands in the local repository for your personal fork:

git checkout JDK-8294942
git fetch https://git.openjdk.org/jdk master
git merge FETCH_HEAD
# resolve conflicts and follow the instructions given by git merge
git commit -m "Merge master"
git push

@openjdk openjdk bot added merge-conflict Pull request has merge conflict with target branch and removed ready Pull request is ready to be integrated labels Nov 21, 2022
@openjdk
Copy link

openjdk bot commented Nov 21, 2022

@lahodaj Syntax: /contributor (add|remove) [@user | openjdk-user | Full Name <email@address>]. For example:

  • /contributor add @openjdk-bot
  • /contributor add duke
  • /contributor add J. Duke <duke@openjdk.org>

User names can only be used for users in the census associated with this repository. For other contributors you need to supply the full name and email address.

@openjdk
Copy link

openjdk bot commented Nov 21, 2022

@lahodaj Syntax: /contributor (add|remove) [@user | openjdk-user | Full Name <email@address>]. For example:

  • /contributor add @openjdk-bot
  • /contributor add duke
  • /contributor add J. Duke <duke@openjdk.org>

User names can only be used for users in the census associated with this repository. For other contributors you need to supply the full name and email address.

@lahodaj
Copy link
Contributor Author

lahodaj commented Nov 21, 2022

/contributor add abimpoudis

@openjdk
Copy link

openjdk bot commented Nov 21, 2022

@lahodaj
Contributor Aggelos Biboudis <abimpoudis@openjdk.org> successfully added.

@lahodaj
Copy link
Contributor Author

lahodaj commented Nov 21, 2022

/contributor add mcimadamore

@openjdk
Copy link

openjdk bot commented Nov 21, 2022

@lahodaj
Contributor Maurizio Cimadamore <mcimadamore@openjdk.org> successfully added.

@openjdk openjdk bot added ready Pull request is ready to be integrated and removed merge-conflict Pull request has merge conflict with target branch labels Nov 23, 2022
@lahodaj
Copy link
Contributor Author

lahodaj commented Dec 1, 2022

/integrate

@openjdk
Copy link

openjdk bot commented Dec 1, 2022

Going to push as commit 756dd5b.
Since your change was applied there have been 39 commits pushed to the master branch:

Your commit was automatically rebased without conflicts.

@openjdk openjdk bot added the integrated Pull request has been integrated label Dec 1, 2022
@openjdk openjdk bot closed this Dec 1, 2022
@openjdk openjdk bot removed ready Pull request is ready to be integrated rfr Pull request is ready for review labels Dec 1, 2022
@openjdk
Copy link

openjdk bot commented Dec 1, 2022

@lahodaj Pushed as commit 756dd5b.

💡 You may see a message that your pull request was closed with unmerged commits. This can be safely ignored.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler compiler-dev@openjdk.org integrated Pull request has been integrated
Development

Successfully merging this pull request may close these issues.

4 participants