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

Overhaul the rust visitors #506

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

Nadrieril
Copy link
Member

@Nadrieril Nadrieril commented Dec 23, 2024

The derive-visitor crate has a few shortcomings:

  • Doesn't support generics, which made it hard to properly account for all the cases where we may encounter Binder<SomeType>.
  • Doesn't give control over recursion, which I worked around with the Ty::visit_inside hack (a regular source of confusion).
  • Doesn't support exfiltrating visited references out of the visitor, which I have wanted for reconstructing comments.

The last two points could be remedied by adding features to the upstream crate, but the first one requires a completely different approach. Over the last few weeks I came up with a new design for automatically-derived visitors, which I made into a crate over the winter break.

That crate centered around the main boilerplate required for building visitors, namely the code that calls v.visit(self.field) for each field of a type. A derive macro provides that boilerplate, and with that boilerplate out of the way it becomes easy to define flexible visitors. The crate provides more macros for that.

This PR replaces all uses of derive-visitor with a custom visitor infrastructure based on my new derive-generic-visitor crate. This adds a AstVisitable trait implemented for all the types of the AST, as well as a pair of VisitAst[Mut] traits used to traverse such types. They provide a convenient overrideable interface to define visitors, of which you can see examples in this PR. Moreover we still support the extremely practical dynamic-dispatched-based API that we had with derive-visitors.

The second-to-last commit fixes #505.

@Nadrieril Nadrieril force-pushed the rework-rust-visitors branch 3 times, most recently from 282f3a4 to 0a1347a Compare December 23, 2024 15:14
@Nadrieril Nadrieril force-pushed the rework-rust-visitors branch from 0a1347a to a27cd0d Compare December 23, 2024 15:25
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.

Compilation error with 2024-12-19 rust toolchain
1 participant