-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
8322477: order of subclasses in the permits clause can differ between compilations #17284
8322477: order of subclasses in the permits clause can differ between compilations #17284
Conversation
👋 Welcome back vromero! A progress list of the required criteria for merging this PR into |
@vicente-romero-oracle The following label will be automatically applied to this pull request:
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. |
Webrevs
|
@@ -1303,10 +1304,12 @@ public static class ClassSymbol extends TypeSymbol implements TypeElement { | |||
// sealed classes related fields | |||
/** The classes, or interfaces, permitted to extend this class, or interface | |||
*/ | |||
public List<Symbol> permitted; | |||
private java.util.List<PermittedClassWithPos> permitted; |
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 of the changes come only because of the fact that this field is not private. We can keep it public and the code will be simpler but there could be erroneous uses of the field
log.error(TreeInfo.diagnosticPositionFor(c, env.tree), Errors.SealedClassMustHaveSubclasses); | ||
} | ||
|
||
if (c.isSealed()) { | ||
Set<Symbol> permittedTypes = new HashSet<>(); | ||
boolean sealedInUnnamed = c.packge().modle == syms.unnamedModule || c.packge().modle == syms.noModule; | ||
for (Symbol subTypeSym : c.permitted) { | ||
for (Type subType : c.getPermittedSubclasses()) { |
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.
most changes here are due to the use of getPermittedSubclasses
, which is a previously existing API, that returns a list of types.
at the end the swap in the permits clause order can be boiled down to symbol completions that were triggered "before" expected by a call to Symbol::flags but, even if this was the right solution it is not future-proofed as a future change can include a call to Symbol::flags that can alter the expected order of the permits clause. This is why I think that relying on the original positions in the source code as the key for sorting the elements in the permits clause is a most. For example the patch below fixes the issue with current master, but as mentioned above this is not a good solution looking forward:
|
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.
While it might be nicer to have the order implied, I can see that's not really easy to achieve in case of arbitrary order of completion. So, this looks OK to me. One suggestion for consideration slightly simplify the code. No re-review needed if the suggestion is accepted.
@@ -1327,6 +1330,41 @@ public ClassSymbol(long flags, Name name, Symbol owner) { | |||
this.type.tsym = this; | |||
} | |||
|
|||
public void addPermittedSubclass(ClassSymbol csym, int pos) { | |||
if (isPermittedExplicit) { |
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 I understand it properly, the codepaths to add implicitly and explicitly declared permitted subclasses are completely different. And when the permitted subclasses are explicit, the addPermittedSubclass
won't be called.
For consideration: just using Assert.check(!isPermittedExplicit)
, drop the if.
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.
sure will do, thanks
@vicente-romero-oracle 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 89 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 |
public void setPermittedSubclasses(List<Symbol> permittedSubs) { | ||
permitted.clear(); | ||
for (Symbol csym : permittedSubs) { | ||
permitted.add(new PermittedClassWithPos(csym, 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.
If we have multiple 0
positions in the list, a future addPermittedSubclass
call may not produce a deterministic list if it's called with pos 0
, as that's how Arrays.binarySearch
behaves.
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.
right but as Jan mentioned we will be calling either addPermittedSubclass
or setPermittedSubclasses
not both. We need to set a default position value if none is provided, 0
is a good option or -1
/integrate |
Going to push as commit 5ba69e1.
Your commit was automatically rebased without conflicts. |
@vicente-romero-oracle Pushed as commit 5ba69e1. 💡 You may see a message that your pull request was closed with unmerged commits. This can be safely ignored. |
This is a very interesting issue. Given code like:
As we know javac will infer the
permits
clause of sealed interfaceSealed
logically the order should correspond to the order in which the permitted subclasses appear in the source code. Well it has been consistently observed by the reported of this bug, that some tools like Gradle while doing incremental compilation can make javac infer eitherR1, R2
orR2, R1
as permitted subclasses. The reason is not clear still under investigation on their side but the fact is that javac is generating inconsistent output for some classes with this shape. The proposed solution is to store the position of the permitted subclasses being discovered by javac so that the order of the permitted subclasses corresponds to the original order in the source file. Efforts to reduce the project where the issue was discovered to a small reproductor have been unsuccessful but the proposed patch have fixed the issue observed by the reporter.TIA
Progress
Issue
Reviewers
Reviewing
Using
git
Checkout this PR locally:
$ git fetch https://git.openjdk.org/jdk.git pull/17284/head:pull/17284
$ git checkout pull/17284
Update a local copy of the PR:
$ git checkout pull/17284
$ git pull https://git.openjdk.org/jdk.git pull/17284/head
Using Skara CLI tools
Checkout this PR locally:
$ git pr checkout 17284
View PR using the GUI difftool:
$ git pr show -t 17284
Using diff file
Download this PR as a diff file:
https://git.openjdk.org/jdk/pull/17284.diff
Webrev
Link to Webrev Comment