@@ -315,6 +315,75 @@ pub trait Visitor<'ast>: Sized {
315
315
}
316
316
}
317
317
318
+ #[macro_export]
319
+ macro_rules! common_visitor_and_walkers {
320
+ ($(($mut: ident))? $Visitor:ident$(<$lt:lifetime>)?) => {
321
+ // this is only used by the MutVisitor. We include this symmetry here to make writing other functions easier
322
+ $(${ignore($lt)}
323
+ #[expect(unused, rustc::pass_by_value)]
324
+ #[inline]
325
+ )?
326
+ fn visit_span<$($lt,)? V: $Visitor$(<$lt>)?>(visitor: &mut V, span: &$($lt)? $($mut)? Span) $(-> <V as Visitor<$lt>>::Result)? {
327
+ $(
328
+ let _ = stringify!($mut);
329
+ visitor.visit_span(span);
330
+ )?
331
+ $(${ignore($lt)}V::Result::output())?
332
+ }
333
+
334
+ // this is only used by the MutVisitor. We include this symmetry here to make writing other functions easier
335
+ $(${ignore($lt)}
336
+ #[expect(unused, rustc::pass_by_value)]
337
+ #[inline]
338
+ )?
339
+ fn visit_id<$($lt,)? V: $Visitor$(<$lt>)?>(visitor: &mut V, id: &$($lt)? $($mut)? NodeId) $(-> <V as Visitor<$lt>>::Result)? {
340
+ $(
341
+ let _ = stringify!($mut);
342
+ visitor.visit_id(id);
343
+ )?
344
+ $(${ignore($lt)}V::Result::output())?
345
+ }
346
+
347
+ // this is only used by the MutVisitor. We include this symmetry here to make writing other functions easier
348
+ fn visit_safety<$($lt,)? V: $Visitor$(<$lt>)?>(vis: &mut V, safety: &$($lt)? $($mut)? Safety) $(-> <V as Visitor<$lt>>::Result)? {
349
+ match safety {
350
+ Safety::Unsafe(span) => visit_span(vis, span),
351
+ Safety::Safe(span) => visit_span(vis, span),
352
+ Safety::Default => { $(${ignore($lt)}V::Result::output())? }
353
+ }
354
+ }
355
+
356
+ fn visit_constness<$($lt,)? V: $Visitor$(<$lt>)?>(vis: &mut V, constness: &$($lt)? $($mut)? Const) $(-> <V as Visitor<$lt>>::Result)? {
357
+ match constness {
358
+ Const::Yes(span) => visit_span(vis, span),
359
+ Const::No => {
360
+ $(<V as Visitor<$lt>>::Result::output())?
361
+ }
362
+ }
363
+ }
364
+
365
+ pub fn walk_label<$($lt,)? V: $Visitor$(<$lt>)?>(visitor: &mut V, Label { ident }: &$($lt)? $($mut)? Label) $(-> <V as Visitor<$lt>>::Result)? {
366
+ visitor.visit_ident(ident)
367
+ }
368
+
369
+ pub fn walk_fn_header<$($lt,)? V: $Visitor$(<$lt>)?>(visitor: &mut V, header: &$($lt)? $($mut)? FnHeader) $(-> <V as Visitor<$lt>>::Result)? {
370
+ let FnHeader { safety, coroutine_kind, constness, ext: _ } = header;
371
+ try_visit!(visit_constness(visitor, constness));
372
+ if let Some(coroutine_kind) = coroutine_kind {
373
+ try_visit!(visitor.visit_coroutine_kind(coroutine_kind));
374
+ }
375
+ visit_safety(visitor, safety)
376
+ }
377
+
378
+ pub fn walk_lifetime<$($lt,)? V: $Visitor$(<$lt>)?>(visitor: &mut V, Lifetime { id, ident }: &$($lt)? $($mut)? Lifetime) $(-> <V as Visitor<$lt>>::Result)? {
379
+ try_visit!(visit_id(visitor, id));
380
+ visitor.visit_ident(ident)
381
+ }
382
+ };
383
+ }
384
+
385
+ common_visitor_and_walkers!(Visitor<'a>);
386
+
318
387
pub fn walk_crate<'a, V: Visitor<'a>>(visitor: &mut V, krate: &'a Crate) -> V::Result {
319
388
let Crate { attrs, items, spans: _, id: _, is_placeholder: _ } = krate;
320
389
walk_list!(visitor, visit_attribute, attrs);
@@ -334,15 +403,6 @@ pub fn walk_local<'a, V: Visitor<'a>>(visitor: &mut V, local: &'a Local) -> V::R
334
403
V::Result::output()
335
404
}
336
405
337
- pub fn walk_label<'a, V: Visitor<'a>>(visitor: &mut V, Label { ident }: &'a Label) -> V::Result {
338
- visitor.visit_ident(ident)
339
- }
340
-
341
- pub fn walk_lifetime<'a, V: Visitor<'a>>(visitor: &mut V, lifetime: &'a Lifetime) -> V::Result {
342
- let Lifetime { id: _, ident } = lifetime;
343
- visitor.visit_ident(ident)
344
- }
345
-
346
406
pub fn walk_poly_trait_ref<'a, V>(visitor: &mut V, trait_ref: &'a PolyTraitRef) -> V::Result
347
407
where
348
408
V: Visitor<'a>,
@@ -926,12 +986,6 @@ pub fn walk_fn_ret_ty<'a, V: Visitor<'a>>(visitor: &mut V, ret_ty: &'a FnRetTy)
926
986
V::Result::output()
927
987
}
928
988
929
- pub fn walk_fn_header<'a, V: Visitor<'a>>(visitor: &mut V, fn_header: &'a FnHeader) -> V::Result {
930
- let FnHeader { safety: _, coroutine_kind, constness: _, ext: _ } = fn_header;
931
- visit_opt!(visitor, visit_coroutine_kind, coroutine_kind.as_ref());
932
- V::Result::output()
933
- }
934
-
935
989
pub fn walk_fn_decl<'a, V: Visitor<'a>>(
936
990
visitor: &mut V,
937
991
FnDecl { inputs, output }: &'a FnDecl,
0 commit comments