Skip to content

introduce common macro for MutVisitor and Visitor to dedup code #141249

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

Merged
merged 1 commit into from
May 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions compiler/rustc_ast/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#![feature(associated_type_defaults)]
#![feature(box_patterns)]
#![feature(if_let_guard)]
#![feature(macro_metavar_expr)]
#![feature(negative_impls)]
#![feature(never_type)]
#![feature(rustdoc_internals)]
Expand Down
37 changes: 3 additions & 34 deletions compiler/rustc_ast/src/mut_visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use thin_vec::ThinVec;
use crate::ast::*;
use crate::ptr::P;
use crate::tokenstream::*;
use crate::visit::{AssocCtxt, BoundKind, FnCtxt};
use crate::visit::{AssocCtxt, BoundKind, FnCtxt, try_visit};

pub trait ExpectOne<A: Array> {
fn expect_one(self, err: &'static str) -> A::Item;
Expand Down Expand Up @@ -388,6 +388,8 @@ pub trait MutVisitor: Sized {
}
}

super::common_visitor_and_walkers!((mut) MutVisitor);

/// Use a map-style function (`FnOnce(T) -> T`) to overwrite a `&mut T`. Useful
/// when using a `flat_map_*` or `filter_map_*` method within a `visit_`
/// method.
Expand Down Expand Up @@ -777,15 +779,6 @@ fn visit_defaultness<T: MutVisitor>(vis: &mut T, defaultness: &mut Defaultness)
}
}

// No `noop_` prefix because there isn't a corresponding method in `MutVisitor`.
fn visit_safety<T: MutVisitor>(vis: &mut T, safety: &mut Safety) {
match safety {
Safety::Unsafe(span) => vis.visit_span(span),
Safety::Safe(span) => vis.visit_span(span),
Safety::Default => {}
}
}

// No `noop_` prefix because there isn't a corresponding method in `MutVisitor`.
fn visit_polarity<T: MutVisitor>(vis: &mut T, polarity: &mut ImplPolarity) {
match polarity {
Expand All @@ -794,14 +787,6 @@ fn visit_polarity<T: MutVisitor>(vis: &mut T, polarity: &mut ImplPolarity) {
}
}

// No `noop_` prefix because there isn't a corresponding method in `MutVisitor`.
fn visit_constness<T: MutVisitor>(vis: &mut T, constness: &mut Const) {
match constness {
Const::Yes(span) => vis.visit_span(span),
Const::No => {}
}
}

fn walk_closure_binder<T: MutVisitor>(vis: &mut T, binder: &mut ClosureBinder) {
match binder {
ClosureBinder::NotPresent => {}
Expand Down Expand Up @@ -940,15 +925,6 @@ pub fn walk_flat_map_generic_param<T: MutVisitor>(
smallvec![param]
}

fn walk_label<T: MutVisitor>(vis: &mut T, Label { ident }: &mut Label) {
vis.visit_ident(ident);
}

fn walk_lifetime<T: MutVisitor>(vis: &mut T, Lifetime { id, ident }: &mut Lifetime) {
vis.visit_id(id);
vis.visit_ident(ident);
}

fn walk_generics<T: MutVisitor>(vis: &mut T, generics: &mut Generics) {
let Generics { params, where_clause, span } = generics;
params.flat_map_in_place(|param| vis.flat_map_generic_param(param));
Expand Down Expand Up @@ -1340,13 +1316,6 @@ fn walk_const_item<T: MutVisitor>(vis: &mut T, item: &mut ConstItem) {
walk_define_opaques(vis, define_opaque);
}

fn walk_fn_header<T: MutVisitor>(vis: &mut T, header: &mut FnHeader) {
let FnHeader { safety, coroutine_kind, constness, ext: _ } = header;
visit_constness(vis, constness);
coroutine_kind.as_mut().map(|coroutine_kind| vis.visit_coroutine_kind(coroutine_kind));
visit_safety(vis, safety);
}

pub fn walk_crate<T: MutVisitor>(vis: &mut T, krate: &mut Crate) {
let Crate { attrs, items, spans, id, is_placeholder: _ } = krate;
vis.visit_id(id);
Expand Down
84 changes: 69 additions & 15 deletions compiler/rustc_ast/src/visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,75 @@ pub trait Visitor<'ast>: Sized {
}
}

#[macro_export]
macro_rules! common_visitor_and_walkers {
($(($mut: ident))? $Visitor:ident$(<$lt:lifetime>)?) => {
// this is only used by the MutVisitor. We include this symmetry here to make writing other functions easier
$(${ignore($lt)}
#[expect(unused, rustc::pass_by_value)]
#[inline]
)?
fn visit_span<$($lt,)? V: $Visitor$(<$lt>)?>(visitor: &mut V, span: &$($lt)? $($mut)? Span) $(-> <V as Visitor<$lt>>::Result)? {
$(
let _ = stringify!($mut);
visitor.visit_span(span);
)?
$(${ignore($lt)}V::Result::output())?
}

// this is only used by the MutVisitor. We include this symmetry here to make writing other functions easier
$(${ignore($lt)}
#[expect(unused, rustc::pass_by_value)]
#[inline]
)?
fn visit_id<$($lt,)? V: $Visitor$(<$lt>)?>(visitor: &mut V, id: &$($lt)? $($mut)? NodeId) $(-> <V as Visitor<$lt>>::Result)? {
$(
let _ = stringify!($mut);
visitor.visit_id(id);
)?
$(${ignore($lt)}V::Result::output())?
}

// this is only used by the MutVisitor. We include this symmetry here to make writing other functions easier
fn visit_safety<$($lt,)? V: $Visitor$(<$lt>)?>(vis: &mut V, safety: &$($lt)? $($mut)? Safety) $(-> <V as Visitor<$lt>>::Result)? {
match safety {
Safety::Unsafe(span) => visit_span(vis, span),
Safety::Safe(span) => visit_span(vis, span),
Safety::Default => { $(${ignore($lt)}V::Result::output())? }
}
}

fn visit_constness<$($lt,)? V: $Visitor$(<$lt>)?>(vis: &mut V, constness: &$($lt)? $($mut)? Const) $(-> <V as Visitor<$lt>>::Result)? {
match constness {
Const::Yes(span) => visit_span(vis, span),
Const::No => {
$(<V as Visitor<$lt>>::Result::output())?
}
}
}

pub fn walk_label<$($lt,)? V: $Visitor$(<$lt>)?>(visitor: &mut V, Label { ident }: &$($lt)? $($mut)? Label) $(-> <V as Visitor<$lt>>::Result)? {
visitor.visit_ident(ident)
}

pub fn walk_fn_header<$($lt,)? V: $Visitor$(<$lt>)?>(visitor: &mut V, header: &$($lt)? $($mut)? FnHeader) $(-> <V as Visitor<$lt>>::Result)? {
let FnHeader { safety, coroutine_kind, constness, ext: _ } = header;
try_visit!(visit_constness(visitor, constness));
if let Some(coroutine_kind) = coroutine_kind {
try_visit!(visitor.visit_coroutine_kind(coroutine_kind));
}
visit_safety(visitor, safety)
}

pub fn walk_lifetime<$($lt,)? V: $Visitor$(<$lt>)?>(visitor: &mut V, Lifetime { id, ident }: &$($lt)? $($mut)? Lifetime) $(-> <V as Visitor<$lt>>::Result)? {
try_visit!(visit_id(visitor, id));
visitor.visit_ident(ident)
}
};
}

common_visitor_and_walkers!(Visitor<'a>);

pub fn walk_crate<'a, V: Visitor<'a>>(visitor: &mut V, krate: &'a Crate) -> V::Result {
let Crate { attrs, items, spans: _, id: _, is_placeholder: _ } = krate;
walk_list!(visitor, visit_attribute, attrs);
Expand All @@ -334,15 +403,6 @@ pub fn walk_local<'a, V: Visitor<'a>>(visitor: &mut V, local: &'a Local) -> V::R
V::Result::output()
}

pub fn walk_label<'a, V: Visitor<'a>>(visitor: &mut V, Label { ident }: &'a Label) -> V::Result {
visitor.visit_ident(ident)
}

pub fn walk_lifetime<'a, V: Visitor<'a>>(visitor: &mut V, lifetime: &'a Lifetime) -> V::Result {
let Lifetime { id: _, ident } = lifetime;
visitor.visit_ident(ident)
}

pub fn walk_poly_trait_ref<'a, V>(visitor: &mut V, trait_ref: &'a PolyTraitRef) -> V::Result
where
V: Visitor<'a>,
Expand Down Expand Up @@ -926,12 +986,6 @@ pub fn walk_fn_ret_ty<'a, V: Visitor<'a>>(visitor: &mut V, ret_ty: &'a FnRetTy)
V::Result::output()
}

pub fn walk_fn_header<'a, V: Visitor<'a>>(visitor: &mut V, fn_header: &'a FnHeader) -> V::Result {
let FnHeader { safety: _, coroutine_kind, constness: _, ext: _ } = fn_header;
visit_opt!(visitor, visit_coroutine_kind, coroutine_kind.as_ref());
V::Result::output()
}

pub fn walk_fn_decl<'a, V: Visitor<'a>>(
visitor: &mut V,
FnDecl { inputs, output }: &'a FnDecl,
Expand Down
Loading