-
Notifications
You must be signed in to change notification settings - Fork 139
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
Fix RecordPattern generation for standalone instanceof #2016
Fix RecordPattern generation for standalone instanceof #2016
Conversation
@srikanth-sankaran here is the updated fix for the bug |
Thanks David. These changes will be useful - I am still deciding whether this needs to go into 4.31 or be a part of the dozen or more cumulative fixes I expected to accumulate under the umbrella #2011 and be merged in when master reopens for 4.32 M1 development (first week of March 2024) The changes look in the right direction, but I am not done cleaning up the current implementation! In particular reference to the region you are touching this is my plan:
and stop the buck right there.
this should simply be:
Your changes will be useful, they just need to be adjusted for the other changes, I'll work on it. |
|
||
for (LocalVariableBinding patternVariableBinding : this.pattern.bindingsWhenTrue()) { | ||
codeStream.removeVariable(patternVariableBinding); | ||
} |
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 is wrong placement of code. You are perhaps following the problematic coding pattern prevalent in the code base that I am trying to weed out. In pattern matching implementation, variables are added and removed without much rhyme or reason. In this place, instanceof is true and the pattern binding is live - it should not be removed. The flow analysis in general should determine when it becomes not definitely assigned and close the ranges with a call to record to org.eclipse.jdt.internal.compiler.lookup.LocalVariableBinding.recordInitializationEndPC(int)
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.
Note however the call to codeStream.removeVariable(this.secretExpressionValue);
just above in line 127 is correct. We are done with the use of secret variable and it can be done away with. It being a compiler injected variable, its scope and live range is entirely upto us. We want to keep it alive for the minimum range required.
But pattern bindings are programmer defined variables and their scope and live ranges (pc intervals where are they are definitely assigned per flow scope) are spelled out by JLS. analyzeCode() phase computes the definite assignment ranges for locals.
BTW, you may find this aggregate junit |
codeStream.iconst_1(); | ||
codeStream.goto_(continueLabel); | ||
falseLabel.place(); | ||
codeStream.iconst_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.
That for
loop from lines 133-135 should actually be placed here just above the iconst_0
- in this place, the instanceof check is false and any pattern bindings injected by the instanceof true path cannot be live
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 the for loop as is - but a modified version should be placed here.
The variable should not be removed. Its definite assignment status must be adjusted and the live range closed.
Variable removable for user scoped variables should happen via org.eclipse.jdt.internal.compiler.codegen.CodeStream.exitUserScope(BlockScope, Predicate<LocalVariableBinding>)
which will happen already at the right places
OK, I have made the change for live range adjustment and also added regression tests for #1985 - which passes with this PR - and pushed. Let us see if we can get this included for M3, failing that for RC1. However there is some infra issue with Jenkins it seems, let us see. |
@mpalat @jarthana @iloveeclipse - I would like this included for 4.31 RC1, if not M3 which may be a bit late I agree |
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.
Changes look good to me. However since I also contributed, I'll ask Manoj for an additional review
While the changes are correct per my review and testing, there is room for further cleanup and improvement. The original code prior to this PR itself has suffered from copy + paste and so has some strange behavior which this PR persists with:
I'll clean up this up later along with all the tasks mentioned in #2016 (comment) |
Fixes eclipse-jdt#1804 Supercedes eclipse-jdt#1816 Signed-off-by: David Thompson <[email protected]>
- Add regression test for eclipse-jdt#1985
6004a86
to
af2b845
Compare
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.
+1
Am fine with this
this.pattern.generateOptimizedBoolean(currentScope, codeStream, trueLabel, falseLabel); | ||
|
||
trueLabel.place(); | ||
codeStream.iconst_1(); |
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.
codeStream.dup() in the original code should have been kept instead of const_1() and const_0() [later]. However, not a critical change.
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.
codeStream.dup() in the original code should have been kept instead of const_1() and const_0() [later]. However, not a critical change.
I will follow up on this along with various other cleanups I have called out in #2016 (comment)
+1 for M3 - |
Thanks @mpalat and @datho7561 |
) * Fix RecordPattern generation for standalone instanceof * Fixes eclipse-jdt#1804 * Fixes eclipse-jdt#1985 Signed-off-by: David Thompson <[email protected]> Co-authored-by: Srikanth Sankaran <[email protected]>
) * Fix RecordPattern generation for standalone instanceof * Fixes eclipse-jdt#1804 * Fixes eclipse-jdt#1985 Signed-off-by: David Thompson <[email protected]> Co-authored-by: Srikanth Sankaran <[email protected]>
) * Fix RecordPattern generation for standalone instanceof * Fixes eclipse-jdt#1804 * Fixes eclipse-jdt#1985 Signed-off-by: David Thompson <[email protected]> Co-authored-by: Srikanth Sankaran <[email protected]>
) * Fix RecordPattern generation for standalone instanceof * Fixes eclipse-jdt#1804 * Fixes eclipse-jdt#1985 Signed-off-by: David Thompson <[email protected]> Co-authored-by: Srikanth Sankaran <[email protected]>
Supercedes #1816
Signed-off-by: David Thompson [email protected]
What it does
Fixes #1804
How to test
Try running the snippets in the linked issue.
Author checklist