Skip to content

Fix: Make sure compact constructor assignments resolve correctly to the fields#6687

Closed
Luro02 wants to merge 1 commit intoINRIA:masterfrom
Luro02:fix/record-regressions
Closed

Fix: Make sure compact constructor assignments resolve correctly to the fields#6687
Luro02 wants to merge 1 commit intoINRIA:masterfrom
Luro02:fix/record-regressions

Conversation

@Luro02
Copy link
Copy Markdown
Contributor

@Luro02 Luro02 commented Mar 30, 2026

While updating my code to newer spoon versions, I observed that in code like:

record MyRecord(String string) {
    MyRecord {
        string = string + "!!!";
     // ^^^^^^ this is no longer a CtFieldWrite
    }
}

The left side of the assignment isn't a field write anymore. It is currently a CtVariableWrite.

Then I thought, okay, if the getDeclaration resolves to the field anyway, it wouldn't be a big problem, but unfortunately it resolves to the implicit parameter, which is wrong.

For example here is how IntelliJ resolves it:
image
image

When looking at this, it seems like there isn't supposed to be a parameter? Both references resolve to the field? Maybe it is just a visual thing...

I added an explicit test case for what is failing and wrote a minor surface-level patch to address this issue, but I am not very familiar with the JDT code or how the tree builder works. I think there might be a better place to resolve this issue, or is it a regression introduced by JDT?

The root cause of this seems to be PR #6316.

@Luro02 Luro02 marked this pull request as draft March 30, 2026 07:26
@Luro02
Copy link
Copy Markdown
Contributor Author

Luro02 commented Mar 30, 2026

Further investigation:

According to the JLS a compact constructor body like

MyRecord {
    string = string + "!!!";
}

does never assign to a component field (= the field of the record) of the record, and here in explicit words:

If a record declaration has a record component named c, then the simple name c in the body of a compact constructor denotes the implicit formal parameter named c, and not the component field named c.

So the current behavior seems to be correct, even though it is confusing...

@Luro02 Luro02 closed this Mar 30, 2026
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.

1 participant