-
Notifications
You must be signed in to change notification settings - Fork 6.2k
8043179: Lambda expression can mutate final field #10381
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 archiecobbs! A progress list of the required criteria for merging this PR into |
|
@archiecobbs 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
|
df8a09a to
47df9c9
Compare
|
I've updated this pull request to include a new unit test (apologies for omitting it previously). Also there are a couple of changes to other unit tests which get broken because this change reflects changes to the JLS spec per JDK-8043176. I'm a little unclear on precisely what the new behavior is supposed to be, but in any case the actual behavior now makes a lot more sense: basically, everything that happens in a lambda is assumed to happen some indefinite time later in the future (same as if you were instantiating the equivalent local class), so the previous errors expected by In summary:
With the above changes now all javac tests should succeed. |
|
@archiecobbs Please do not rebase or force-push to an active PR as it invalidates existing review comments. All changes will be squashed into a single commit automatically when integrating. See OpenJDK Developers’ Guide for more information. |
…les.
Before this change, compiling this class:
class LambdaMutateFinalVar {
LambdaMutateFinalVar() {
final String x;
Runnable r1 = () -> x = "not ok";
x = "ok";
}
}
would report this error:
local variables referenced from a lambda expression must be final or effectively final
That error is not really appropriate; after all, the variable IS final. The
real problem is that it can't be assigned from within the lambda because it
can't be assumed to be DU.
After this change, this error is reported instead:
variable x might already have been assigned
9352f98 to
06b39a5
Compare
|
@archiecobbs Please do not rebase or force-push to an active PR as it invalidates existing review comments. All changes will be squashed into a single commit automatically when integrating. See OpenJDK Developers’ Guide for more information. |
|
@archiecobbs This pull request has been inactive for more than 4 weeks and will be automatically closed if another 4 weeks passes without any activity. To avoid this, simply add a new comment to the pull request. Feel free to ask for assistance if you need help with progressing this pull request towards integration! |
|
@archiecobbs This pull request has been inactive for more than 8 weeks and will now be automatically closed. If you would like to continue working on this pull request in the future, feel free to reopen it! This can be done using the |
|
/open |
|
@archiecobbs This pull request is now open |
|
@archiecobbs This pull request has been inactive for more than 4 weeks and will be automatically closed if another 4 weeks passes without any activity. To avoid this, simply add a new comment to the pull request. Feel free to ask for assistance if you need help with progressing this pull request towards integration! |
|
/open |
|
@archiecobbs This pull request is already open |
|
@archiecobbs This pull request has been inactive for more than 4 weeks and will be automatically closed if another 4 weeks passes without any activity. To avoid this, simply add a new comment to the pull request. Feel free to ask for assistance if you need help with progressing this pull request towards integration! |
|
/keepalive |
|
@archiecobbs Unknown command |
|
@archiecobbs This pull request has been inactive for more than 4 weeks and will be automatically closed if another 4 weeks passes without any activity. To avoid this, simply add a new comment to the pull request. Feel free to ask for assistance if you need help with progressing this pull request towards integration! |
|
/pingbot |
|
@archiecobbs Unknown command |
| // body. This is by design: a variable that was definitely unassigned before the | ||
| // lambda body may end up being assigned to later on, so we cannot conclude that | ||
| // the variable will be unassigned when the body is executed. | ||
| uninits.excludeFrom(firstadr); |
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 will exclude all tracked variables not only fields, shouldn't we exclude fields only?
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 bug refers only to final fields, but JLS section 16 applies to both fields and variables, so I think this code is following the spec as written.
Here's an example of where this change would catch an illegal final variable assignment:
class LambdaMutateFinalVariable {
void foo() {
final String x;
Runnable r1 = () -> x = "not ok";
x = "ok";
}
}So I don't think the code in the PR is wrong. However, there is a subtlety: currently, the compiler already correctly handles the above example, but that's only by coincidence and thanks to the separate logic for "effectively final":
LambdaMutateFinalVariable.java:4: error: local variables referenced from a lambda expression must be final or effectively final
Runnable r1 = () -> x = "not ok";
^
With the change in this PR, the DA/DU analysis will instead get there first, and so you get a different error:
LambdaMutateFinalVariable.java:4: error: variable x might already have been assigned
Runnable r1 = () -> x = "not ok";
^
It seems to me either error is appropriate, because the code violates both rules. But I think the change in the error message is actually an improvement, because the current error is complaining about the variable not being final even though it actually is final, which is confusing.
Your thoughts?
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.
yep I agree that the error message is better with the patch. My concern was not that the patch was wrong but about the fact that we were applying the change for all variables and fields, but yes it probably makes sense to go for this and have the side effect of better error messages
vicente-romero-oracle
left a comment
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
|
@archiecobbs 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 34 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. As you do not have Committer status in this project an existing Committer must agree to sponsor your change. Possible candidates are the reviewers of this PR (@vicente-romero-oracle) but any other Committer may sponsor as well. ➡️ To flag this PR as ready for integration with the above commit message, type |
|
/integrate |
|
@archiecobbs |
|
/sponsor |
|
@vicente-romero-oracle The PR has been updated since the change author (@archiecobbs) issued the |
|
/integrate |
|
@archiecobbs |
|
/sponsor |
|
Going to push as commit c00d088.
Your commit was automatically rebased without conflicts. |
|
@vicente-romero-oracle @archiecobbs Pushed as commit c00d088. 💡 You may see a message that your pull request was closed with unmerged commits. This can be safely ignored. |
(Description revised)
JDK-8043176 updated the JLS 16.1.10 to describe how within a lambda body, any variables that were DU before the lambda expression must not be considered DU at the start of the lambda block, because arbitrary code could have executed in the meantime:
This fix to
Flow.AssignAnalyzerfollows in a straightforward way: mark all variables as not DU before recursing into the lambda body.Progress
Issue
Reviewers
Reviewing
Using
gitCheckout this PR locally:
$ git fetch https://git.openjdk.org/jdk.git pull/10381/head:pull/10381$ git checkout pull/10381Update a local copy of the PR:
$ git checkout pull/10381$ git pull https://git.openjdk.org/jdk.git pull/10381/headUsing Skara CLI tools
Checkout this PR locally:
$ git pr checkout 10381View PR using the GUI difftool:
$ git pr show -t 10381Using diff file
Download this PR as a diff file:
https://git.openjdk.org/jdk/pull/10381.diff