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

[Feat]: Add a method to get the parent node of a given node #242

Open
xFrednet opened this issue Sep 11, 2023 · 4 comments
Open

[Feat]: Add a method to get the parent node of a given node #242

xFrednet opened this issue Sep 11, 2023 · 4 comments
Labels
A-api Area: Stable API C-enhancement Category: New feature or request
Milestone

Comments

@xFrednet
Copy link
Member

Summary

Requesting the parent of a given node, is a fundamental feature required to build context-sensitive lints. It's useful to check the context that an expression occurs in or to adapt the suggestion when required. Rustc provides a find_parent(HirId) function and an iterator function to walk through the parents.

I've already planned a bit for this issue, and it's surprisingly complex. It might take some time to implement it properly and feedback from the community.

@xFrednet xFrednet added C-enhancement Category: New feature or request S-needs-triage Status: This issue needs triage labels Sep 11, 2023
@xFrednet xFrednet added this to the v0.3.0 milestone Sep 11, 2023
@xFrednet xFrednet self-assigned this Sep 11, 2023
@xFrednet xFrednet added A-api Area: Stable API and removed S-needs-triage Status: This issue needs triage labels Sep 11, 2023
@xFrednet
Copy link
Member Author

The new addition of the NodeId struct can be used for the development of this issue.

I still have a tiny hope that I can work on this before v0.3.0, but it's likely that this will be moved to v0.4.0 as major breaking changes are currently taking priority for me.

@xFrednet xFrednet removed this from the v0.3.0 milestone Sep 28, 2023
@xFrednet xFrednet removed their assignment Sep 28, 2023
@xFrednet
Copy link
Member Author

I did some work in preparation for this issue, but it's unlikely that I'll get this done before v0.3. It's free for the taking :)

@Veetaha
Copy link
Contributor

Veetaha commented Oct 20, 2023

I've been thinking of implementing iterator API for traversing the AST tree. There is a major problem with implementing such API in that the iterator's state needs to have a dynamically allocated data structure (e.g. a stack) to track the position of the cursor inside of the AST. For example, each time the iteration enters a subtree the stack needs to grow and each time it exists the stack shrinks.

This dynamic state is conceptually unavoidable because the cursor needs to know where to return (how to go up in the tree). If there are parent node references it would reduce the number of state needed to implement the iterator API, but still not entirely solve the problem. When we go up in the tree we also need to know where to continue (the index of the child node that was exited in the parent node plus one).

Because rust-analyzer's nodes have references to parents and adjacent nodes it was possible to implement an allocation-free iterator in there. See e.g. the node representation there.

@xFrednet
Copy link
Member Author

We could also consider storing references of the parents in the nodes.

For the iterator, I think it depends on the flexibility we want to provide. If we only allow the traversal upwards, we only need a single ID in the iterator, that is used to request the next parent. If we allow going down, then it will be more complicated, like you described.

In Clippy, we usually only traverse upwards, if needed we might use a visitor to go downwards. I'm not sure if there is a lint, which would require an iterator that is traversable in both directions.

@xFrednet xFrednet modified the milestones: v0.4.0, v0.5.0 Nov 16, 2023
@xFrednet xFrednet modified the milestones: v0.5.0, v0.6.0 Dec 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-api Area: Stable API C-enhancement Category: New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants