Skip to content
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 nested pattern code generation #1816

Closed

Conversation

datho7561
Copy link
Contributor

@datho7561 datho7561 commented Jan 5, 2024

What it does

  • Fixes boolean assignments that use instanceof with nested patterns.
  • Fixes cases labels that use nested patterns.

Fixes #1804

How to test

See the sample snippets that reproduce the error in the linked issue.

Author checklist

@datho7561 datho7561 force-pushed the 1804-record-pattern-value branch 2 times, most recently from c9fa43f to 0d2e7f1 Compare January 5, 2024 21:49
@datho7561 datho7561 mentioned this pull request Jan 8, 2024
3 tasks
@danthe1st
Copy link

danthe1st commented Jan 8, 2024

It might be a good idea to also create a test for something like the following:

public class Test{
    record Box<T>(T t){}
  
    public static void main(String[] args){
        Box<Object> box = new Box<>(null);
        boolean b = box instanceof Box(Object o);//true
        System.out.println(b);
        boolean c = box instanceof Box(String s);//false
        System.out.println(c);
    }
}

Changing the static type of box from Box<Object> to Box<String> does not change anything about that. My fault - with Box<String> box = new Box<>(null);, both are true

@datho7561 datho7561 force-pushed the 1804-record-pattern-value branch 3 times, most recently from b7d3b11 to 2d42344 Compare January 9, 2024 20:04
@datho7561 datho7561 marked this pull request as ready for review January 9, 2024 20:04
@datho7561 datho7561 changed the title [WIP] fix nested patterns in boolean assignment Fix nested pattern code generation Jan 9, 2024
@datho7561
Copy link
Contributor Author

@iloveeclipse @jarthana if you have time, could you please review this PR?

@mpalat
Copy link
Contributor

mpalat commented Jan 10, 2024

@datho7561 I see that this pr fixes the #1796 issue as well when I checked. You can include that test case also here. Thanks!

@datho7561
Copy link
Contributor Author

@datho7561 I see that this pr fixes the #1796 issue as well when I checked. You can include that test case also here. Thanks!

This PR doesn't seem to fix that bug from my testing.

Boolean assignments are working, but switch statements aren't.
Fully removes unused pattern variables from stack map frame.

Fixes eclipse-jdt#1804

Signed-off-by: David Thompson <davthomp@redhat.com>
Signed-off-by: David Thompson <davthomp@redhat.com>
Signed-off-by: David Thompson <davthomp@redhat.com>
Signed-off-by: David Thompson <davthomp@redhat.com>
Signed-off-by: David Thompson <davthomp@redhat.com>
@srikanth-sankaran
Copy link
Contributor

@datho7561 - This is my plan for this PR - I have assigned myself as a joint owner of this PR. As mentioned in #1517 (comment) I am investigating and possibly undertaking a overhaul of code generation for patterns. If you don't mind, I would subsume this PR as a part of the larger work. Let us not rush this please. What do you say ?

falseLabel.place();
codeStream.iconst_0();
continueLabel.place();
} else if (this.elementVariable != null) {
BranchLabel actionLabel = new BranchLabel(codeStream);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This creates dead code - this.elementVariable will be null when(this.pattern == null)`

public void fullWrapupGeneration(CodeStream codeStream) {
this.primaryPattern.fullWrapupGeneration(codeStream);
}
@Override
protected void generatePatternVariable(BlockScope currentScope, CodeStream codeStream, BranchLabel trueLabel,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Much water has flown under the bridge - the method wrapupGeneration doesn't exist anymore on master and it doesn't likely make sense to introduce this.

Copy link
Contributor Author

@datho7561 datho7561 Feb 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've rebased this PR against your changes in eclipse-jdtls#9 , I'll just need to replicate those changes here seems there are new changes to the Pattern classes since then

@@ -117,15 +117,30 @@ public void generateCode(BlockScope currentScope, CodeStream codeStream, boolean

int pc = codeStream.position;

if (this.elementVariable != null) {
if (this.elementVariable != null || this.pattern != null) {
addAssignment(currentScope, codeStream, this.secretInstanceOfPatternExpressionValue);
codeStream.load(this.secretInstanceOfPatternExpressionValue);
} else {
this.expression.generateCode(currentScope, codeStream, true);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can simply say if (this.pattern != null)

@@ -256,7 +263,7 @@ private static LocalVariableBinding getNewLocalVariableBinding(BlockScope scope,
// int delta = ((TypeBinding.equalsEquals(type, TypeBinding.LONG)) ||
// (TypeBinding.equalsEquals(type, TypeBinding.DOUBLE))) ?
// 2 : 1;
l.resolvedPosition = scope.localIndex;
l.resolvedPosition = scope.startIndex + scope.localIndex;
return l;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method is gone.

@srikanth-sankaran
Copy link
Contributor

Interestingly I ran into this in another context and raised #1985 - I will include the tests from here and see if the changes in org.eclipse.jdt.internal.compiler.ast.InstanceOfExpression.generateCode(BlockScope, CodeStream, boolean) are leverageable.

@datho7561
Copy link
Contributor Author

Looking through this code, I think it's worthwhile completely rewriting this fix, since the code has changed so much since when I wrote it. I'll make sure to leave the branch intact, since I think the code here might contain a hint towards the root cause.

@datho7561 datho7561 closed this Feb 13, 2024
datho7561 added a commit to datho7561/eclipse.jdt.core that referenced this pull request Feb 13, 2024
Fixes eclipse-jdt#1804

Supercedes eclipse-jdt#1816

Signed-off-by: David Thompson <davthomp@redhat.com>
srikanth-sankaran pushed a commit to datho7561/eclipse.jdt.core that referenced this pull request Feb 14, 2024
Fixes eclipse-jdt#1804

Supercedes eclipse-jdt#1816

Signed-off-by: David Thompson <davthomp@redhat.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Record pattern matching with null gives wrong result
5 participants