-
Notifications
You must be signed in to change notification settings - Fork 6k
8358604: Trees for var
do not have end positions
#25664
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
👋 Welcome back acobbs! A progress list of the required criteria for merging this PR into |
❗ This change is not yet ready to be integrated. |
@archiecobbs The following label will be automatically applied to this pull request:
When this pull request is ready to be reviewed, an "RFR" email will be sent to the corresponding mailing list. If you would like to change these labels, use the /label pull request command. |
@archiecobbs this pull request can not be integrated into git checkout JDK-8358604
git fetch https://git.openjdk.org/jdk.git master
git merge FETCH_HEAD
# resolve conflicts and follow the instructions given by git merge
git commit -m "Merge master"
git push |
I guess I would like to understand the rationale for adding the end position for the type. Generally, when javac synthesizes trees, the new trees don't have an end position (like for the synthetic constructor, and several other cases). So, I think there's a question why this specific synthetic tree should have an end position, while the other shouldn't. Also, currently the missing end position is used as a "marker" for synthetic trees. Not particularly nice, but given the synthetic trees are/were (I think) considered a workaround. When the tree has an end position, how will the Trees API clients find out this tree is synthetic? |
@cushon is better positioned (no pun intended) to answer that question than me. In the issue, this rationale is given:
Re:
I didn't realize that lack of end position was being overloaded in that way. But overloaded by whom? Is this an "official" thing per the API, or just a workaround currently being used in practice by some 3rd party tools?
I don't know how frozen the Trees API is, but it seems like the most straightforward answer to that would be for us to expose the type of declaration (explicit, implicit, or |
The position information is useful for Tree API clients that want to do refactorings (e.g. to replace the type of a particular variable, even if it's As a general approach I think it's helpful if javac provides a post-flow AST to clients with minimal rewriting/lowering, so it's possible to map back to the original source code, while still exposing information from flow to clients (e.g. the inferred types of local variables). I think this is closely related to discussion in https://bugs.openjdk.org/browse/JDK-8024098, and looking at that bug again I see there's an issue specifically about |
Yes, that is related to JDK-8024098, I've fixed a few of the modelling issues in the past, but many of them are quite difficult. While I don't doubt the end position is sometimes useful, the ability to detect the tree is synthetic is also sometimes useful (and it may be that one client would desire to know both these properties depending on the context - like imagine you would want to add something that detects Overall, I think just putting an end position to one of the synthetic trees we create is not very helpful, and is going to cause trouble. The options I see are roughly:
For 2. - the question is, what is the right model that closely models the original code. One possibly truthful model would be:
|
I think this↑ option makes the most sense.
Just to clarify, it would only be second guessing for I think it would be nice to add such a method. But perhaps that can come later as a separate enhancement. @lahodaj what do you think about this plan? I'm happy to prototype it if needed. |
Thinking about this more... First let's clarify that there are three separate issues floating around here:
My current thinking on each of these:
So I am closing this PR and will open a new one for JDK-8359383. |
The compiler replaces
var
nodes with synthetic tree nodes representing the inferred type. Previously, these new synthetic nodes were not being assigned either starting or ending source code positions.JDK-8329951 added starting positions, but not ending positions. This PR adds ending positions, and fixes some related bugs discovered in the research process:
final var x
, the fix for JDK-8329951 was returning a starting position incorrectly pointing tofinal
instead ofvar
.final var x
was correctly starting atfinal
for normal declarations, but incorrectly starting atvar
for a lambda parameter declarations.JCVariableDecl.declaredUsingVar()
was incorrectly returningfalse
forvar
variable declarations in record deconstruction patterns (thanks to @cushon for reporting this)Background: When a
var
is parsed or a variable is implicitly typed such as a lambda parameter,JCVariableDecl.vartype
is set to null until the type can be inferred. This causes various problems when trying to determine the starting position of (a) the declaration's type, whenvar
is used, because the source position of thevar
has been lost forever (especially if there are also modifiers); and (b) the starting position of the declaration itself, when there are no modifiers, because normally that's computed from the start position ofJCVariableDecl.vartype
, which is (sometimes) null. Previously there was a fieldJCVariableDecl.startPos
which was put there to workaround problem (b), but that workaround was being applied even when there were modifiers, which is wrong. Etc.This patch attempts to clarify things and includes the following changes:
var
's starting source position properly recoverable from aJCVariableDecl
even whilevartype
is null.TreeMaker.at()
capable of configuring both the starting and ending position; do some minor cleanup/refactoring.var
var
asvar
instead of/*missing*/
(drive-by improvement)Progress
Issue
var
do not have end positions (Bug - P4)Reviewing
Using
git
Checkout this PR locally:
$ git fetch https://git.openjdk.org/jdk.git pull/25664/head:pull/25664
$ git checkout pull/25664
Update a local copy of the PR:
$ git checkout pull/25664
$ git pull https://git.openjdk.org/jdk.git pull/25664/head
Using Skara CLI tools
Checkout this PR locally:
$ git pr checkout 25664
View PR using the GUI difftool:
$ git pr show -t 25664
Using diff file
Download this PR as a diff file:
https://git.openjdk.org/jdk/pull/25664.diff
Using Webrev
Link to Webrev Comment