-
Notifications
You must be signed in to change notification settings - Fork 13.6k
Implement AST visitors using a derive macro. #143897
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
base: master
Are you sure you want to change the base?
Conversation
@bors try @rust-timer queue |
This comment has been minimized.
This comment has been minimized.
Implement AST visitors using a derive macro. AST visitors are large and error-prone beasts. This PR attempts to write them using a derive macro. The design uses three traits: `Visitor`, `Visitable`, `Walkable`. - `Visitor` is the trait implemented by downstream crates, it lists `visit_stuff` methods, which call `Walkable::walk_ref` by default; - `Walkable` is derived using the macro, the generated `walk_ref` method calls `Visitable::visit` on each component; - `Visitable` is implemented by `common_visitor_and_walkers` macro, to call the proper `Visitor::visit_stuff` method if it exists, to call `Walkable::walk_ref` if there is none. I agree this is quite a lot of spaghetti macros. I'm open to suggestions on how to reduce the amount of boilerplate code. If this PR is accepted, I believe the same design can be used for the HIR visitor.
☀️ Try build successful - checks-actions |
This comment has been minimized.
This comment has been minimized.
Finished benchmarking commit (1140f76): comparison URL. Overall result: ❌✅ regressions and improvements - no action neededBenchmarking this pull request means it may be perf-sensitive – we'll automatically label it not fit for rolling up. You can override this, but we strongly advise not to, due to possible changes in compiler perf. @bors rollup=never Instruction countOur most reliable metric. Used to determine the overall result above. However, even this metric can be noisy.
Max RSS (memory usage)Results (secondary 2.4%)A less reliable metric. May be of interest, but not used to determine the overall result above.
CyclesResults (primary 4.5%, secondary -1.2%)A less reliable metric. May be of interest, but not used to determine the overall result above.
Binary sizeThis benchmark run did not return any relevant results for this metric. Bootstrap: 465.776s -> 467.271s (0.32%) |
r? compiler (review capacity) |
I think this need a reviewer who is familiar with AST and macros. |
The code in |
I think in this PR the remaining code in |
At high level, there are two calls to common_visitor_and_walkers!(Visitor<'a>);
common_visitor_and_walkers!((mut) MutVisitor); At the same time the logic in helper macros is duplicated for mutable OR immutable visitor, e.g. like this #[macro_export]
macro_rules! impl_visitable_noop {
(<$lt:lifetime> $($ty:ty,)*) => {
$(
impl_visitable!(|&$lt self: $ty, _vis: &mut V, _extra: ()| {
V::Result::output()
});
)*
};
(<mut> $($ty:ty,)*) => {
$(
impl_visitable!(|&mut self: $ty, _vis: &mut V, _extra: ()| {});
)*
}
} If the two visitors are placed into the same file, then perhaps the macro_rules! impl_visitable_noop {
(<$lt:lifetime> $($ty:ty,)*) => {
$(
impl_visitable!(|&$lt self: $ty, _vis: &mut V, _extra: ()| {
V::Result::output()
});
)*
$(
impl_visitable!(|&mut self: $ty, _vis: &mut V, _extra: ()| {});
)*
};
} |
@petrochenkov Merging both files seems a bit tricky, as they are used by downstream code to chose which version of |
Yeah, certainly not a change for this PR, this one is already quite large. |
r=me after squashing commits. |
Reminder, once the PR becomes ready for a review, use |
AST visitors are large and error-prone beasts. This PR attempts to write them using a derive macro.
The design uses three traits:
Visitor
,Visitable
,Walkable
.Visitor
is the trait implemented by downstream crates, it listsvisit_stuff
methods, which callWalkable::walk_ref
by default;Walkable
is derived using the macro, the generatedwalk_ref
method callsVisitable::visit
on each component;Visitable
is implemented bycommon_visitor_and_walkers
macro, to call the properVisitor::visit_stuff
method if it exists, to callWalkable::walk_ref
if there is none.I agree this is quite a lot of spaghetti macros. I'm open to suggestions on how to reduce the amount of boilerplate code.
If this PR is accepted, I believe the same design can be used for the HIR visitor.