-
Notifications
You must be signed in to change notification settings - Fork 5.8k
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
Conversation
👋 Welcome back jlahoda! A progress list of the required criteria for merging this PR into |
There was a problem hiding this 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; |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
This comment was marked as outdated.
Sorry, something went wrong.
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.
This comment was marked as outdated.
Sorry, something went wrong.
public class Test { | ||
private int test(Integer o) { | ||
return switch (o) { | ||
case (Integer i) when i > 0 -> 0; |
This comment was marked as outdated.
This comment was marked as outdated.
Sorry, something went wrong.
return switch (o) { | ||
case String s when s.isEmpty() -> 0; | ||
case "a" -> 0; | ||
case String s -> 0; |
This comment was marked as outdated.
This comment was marked as outdated.
Sorry, something went wrong.
@@ -651,6 +652,81 @@ public Type instantiateFunctionalInterface(DiagnosticPosition pos, Type funcInte | |||
return owntype; | |||
} | |||
} | |||
|
|||
public Type instantiatePatternType(DiagnosticPosition pos, Type expressionType, TypeSymbol patternTypeSymbol) { |
There was a problem hiding this comment.
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.
Webrevs
|
undet.setInst(bounds.head); | ||
} else { | ||
List<Type> upperBounds = undet.getBounds(InferenceBound.UPPER); | ||
Type bound = upperBounds.isEmpty() ? syms.objectType : types.glb(upperBounds); |
There was a problem hiding this comment.
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<>(); |
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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
There was a problem hiding this 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
src/jdk.compiler/share/classes/com/sun/source/tree/InstanceOfTree.java
Outdated
Show resolved
Hide resolved
src/jdk.compiler/share/classes/com/sun/source/tree/InstanceOfTree.java
Outdated
Show resolved
Hide resolved
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; |
There was a problem hiding this comment.
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
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()); |
There was a problem hiding this comment.
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)) { |
There was a problem hiding this comment.
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()) { |
There was a problem hiding this comment.
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; | |||
} | |||
} | |||
|
There was a problem hiding this comment.
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
side: I extracted this example from the spec:
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) |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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()); |
There was a problem hiding this comment.
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
There was a problem hiding this 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!
@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:
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
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 |
/issue JDK-8294945 |
@lahodaj |
/contributor abimpoudis |
/contributor mcimadamore |
@lahodaj this pull request can not be integrated into 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 |
@lahodaj Syntax:
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 Syntax:
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. |
/contributor add abimpoudis |
@lahodaj |
/contributor add mcimadamore |
@lahodaj |
/integrate |
Going to push as commit 756dd5b.
Your commit was automatically rebased without conflicts. |
This is a partial implementation of JEP 432: Record Patterns (Second Preview) and JEP 433: Pattern Matching for switch (Fourth Preview). Namely, it implements:
null
constants and type test patterns)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
Issues
Reviewers
Contributors
<abimpoudis@openjdk.org>
<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