-
Notifications
You must be signed in to change notification settings - Fork 6.1k
8258897: wrong translation of capturing local classes inside nested lambdas #1479
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
Hi @hltj, welcome to this OpenJDK project and thanks for contributing! We do not recognize you as Contributor and need to ensure you have signed the Oracle Contributor Agreement (OCA). If you have not signed the OCA, please follow the instructions. Please fill in your GitHub username in the "Username" field of the application. Once you have signed the OCA, please let us know by writing If you already are an OpenJDK Author, Committer or Reviewer, please click here to open a new issue so that we can record that fact. Please use "Add GitHub user hltj" as summary for the issue. If you are contributing this work on behalf of your employer and your employer has signed the OCA, please let us know by writing |
I had signed OCA, but named as |
/signed |
Thank you! Please allow for up to two weeks to process your OCA, although it is usually done within one to two business days. Also, please note that pull requests that are pending an OCA check will not usually be evaluated, so your patience is appreciated! |
@hltj 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! |
Webrevs
|
Bernard's reply: The problem seems to be that 'lambdaTranslationMap' links the diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/LambdaToMethod.java b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/LambdaToMethod.java
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/LambdaToMethod.java
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/LambdaToMethod.java
@@ -2057,7 +2057,13 @@
};
break;
case LOCAL_VAR:
- ret = new VarSymbol(sym.flags() & FINAL, sym.name, sym.type, translatedSym);
+ ret = new VarSymbol(sym.flags() & FINAL, sym.name, sym.type, translatedSym) {
+ @Override
+ public Symbol baseSymbol() {
+ //keep mapping with original symbol
+ return sym;
+ }
+ };
((VarSymbol) ret).pos = ((VarSymbol) sym).pos;
break;
case PARAM:
diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Lower.java b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Lower.java
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Lower.java
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Lower.java
@@ -1221,7 +1221,7 @@
//sym is a local variable - check the lambda translation map to
//see if sym has been translated to something else in the current
//scope (by LambdaToMethod)
- Symbol translatedSym = lambdaTranslationMap.get(sym);
+ Symbol translatedSym = lambdaTranslationMap.get(sym.baseSymbol());
if (translatedSym != null) {
tree = make.at(tree.pos).Ident(translatedSym);
} |
/contributor add bsrbnd |
@hltj |
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.
Seems sensible to me. Some recommendations on improving the text comment format are inline.
@@ -0,0 +1,65 @@ | |||
import java.util.function.Supplier; | |||
|
|||
/** |
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.
It would be nice to tweak the test to so that:
-the comment with @test
is first, and imports follow
-@test
is augmented with /nodynamiccopyright/
-@bug
is added
-@summary
is added
(applies to both tests)
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.
And the action tag @run main CaptureVariables
should be added.
@hltj Some documents are helpful to you:
Regression Test Harness for the JDK: jtreg
The JDK Test Framework: Tag Language Specification
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.
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.
@lahodaj I think the tests in this case can use the copyright header instead of /nodynamiccopyright/
. What's your opinion?
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.
@lgxbslgx - you are right, I am sorry. /nodymiccopyright/
is used when line numbers are significant, and I somehow thought they are here, but you are right they are not. So there should be a normal header 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.
Thanks. The copyright comments added.
|
@hltj 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 456 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 (@lahodaj) but any other Committer may sponsor as well. ➡️ To flag this PR as ready for integration with the above commit message, type |
/integrate |
/sponsor |
@lahodaj @hltj Since your change was applied there have been 807 commits pushed to the
Your commit was automatically rebased without conflicts. Pushed as commit de3f519. 💡 You may see a message that your pull request was closed with unmerged commits. This can be safely ignored. |
Issue:
javac
crashes with a NPE when compiling such code:Affects all versions from Java 8 to 16-ea, here is a example output with OpenJDK 15.0.1:
The minimal reproducing code is:
If the captured variable is a primitive type, the code will compile but cause a runtime crash at startup. e.g.:
Reason & Experimental solution:
During compilation, the captured variable was correctly synthetized as a synthetic argument in the synthetic methods for the nested lambda after
desugar()
.But the synthetic argument was not used when loading free variables, and it always use the original symbol for the original variable as if not in a nested lambda.
My experimental solution is substituting the symbols in free variables that were captured as synthetic arguments in nested lambda.
I'm not sure whether the code style as well as the solution itself are appropriate. Welcome any advice and better solution.
Progress
Issue
Reviewers
Contributors
<bsrbnd@openjdk.org>
Download
$ git fetch https://git.openjdk.java.net/jdk pull/1479/head:pull/1479
$ git checkout pull/1479