-
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
Closed
Closed
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
989627b
8043179: Lambda expression can mutate final field
archiecobbs 5ee88e7
Use /nodynamiccopyright/ for "golden file" test per instructions.
archiecobbs db7dd0e
Move unit test into a more appropriate subdirectory.
archiecobbs faac971
Fix @author in test to match github username per instructions.
archiecobbs 9699523
Fix previously incorrect logic and update unit tests.
archiecobbs 06b39a5
Add a unit test verifying a change in reported error for final variab…
archiecobbs b1d5b3c
Remove @author tags from tests.
archiecobbs 0081d19
Update failed test output line numbers after removing @author tags.
archiecobbs 1e38c30
Merge branch 'master' into JDK-8043179
archiecobbs 86808b2
Merge branch 'master' into JDK-8043179
archiecobbs e1cd797
Merge branch 'master' into JDK-8043179
archiecobbs File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
test/langtools/tools/javac/lambda/8043179/LambdaMutateFinalField.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
/* | ||
* @test /nodynamiccopyright/ | ||
* @summary Verify lambda expression can't mutate a final field | ||
* @bug 8043179 | ||
* @compile/fail/ref=LambdaMutateFinalField.out -XDrawDiagnostics LambdaMutateFinalField.java | ||
*/ | ||
class LambdaMutateFinalField { | ||
final String x; | ||
LambdaMutateFinalField() { | ||
Runnable r1 = () -> x = "not ok"; | ||
this.x = "ok"; | ||
} | ||
} |
2 changes: 2 additions & 0 deletions
2
test/langtools/tools/javac/lambda/8043179/LambdaMutateFinalField.out
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
LambdaMutateFinalField.java:10:29: compiler.err.var.might.already.be.assigned: x | ||
1 error |
13 changes: 13 additions & 0 deletions
13
test/langtools/tools/javac/lambda/8043179/LambdaMutateFinalVar.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
/* | ||
* @test /nodynamiccopyright/ | ||
* @summary Verify lambda expression can't mutate a final variable | ||
* @bug 8043179 | ||
* @compile/fail/ref=LambdaMutateFinalVar.out -XDrawDiagnostics LambdaMutateFinalVar.java | ||
*/ | ||
class LambdaMutateFinalVar { | ||
LambdaMutateFinalVar() { | ||
final String x; | ||
Runnable r1 = () -> x = "not ok"; | ||
x = "ok"; | ||
} | ||
} |
2 changes: 2 additions & 0 deletions
2
test/langtools/tools/javac/lambda/8043179/LambdaMutateFinalVar.out
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
LambdaMutateFinalVar.java:10:29: compiler.err.var.might.already.be.assigned: x | ||
1 error |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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:
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":
With the change in this PR, the DA/DU analysis will instead get there first, and so you get a different error:
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