rustc_ast/
visit.rs

1//! AST walker. Each overridden visit method has full control over what
2//! happens with its node, it can do its own traversal of the node's children,
3//! call `visit::walk_*` to apply the default traversal algorithm, or prevent
4//! deeper traversal by doing nothing.
5//!
6//! Note: it is an important invariant that the default visitor walks the body
7//! of a function in "execution order" (more concretely, reverse post-order
8//! with respect to the CFG implied by the AST), meaning that if AST node A may
9//! execute before AST node B, then A is visited first. The borrow checker in
10//! particular relies on this property.
11//!
12//! Note: walking an AST before macro expansion is probably a bad idea. For
13//! instance, a walker looking for item names in a module will miss all of
14//! those that are created by the expansion of a macro.
15
16pub use rustc_ast_ir::visit::VisitorResult;
17pub use rustc_ast_ir::{try_visit, visit_opt, walk_list, walk_visitable_list};
18use rustc_span::{Ident, Span};
19use thin_vec::ThinVec;
20
21use crate::ast::*;
22use crate::ptr::P;
23
24#[derive(Copy, Clone, Debug, PartialEq)]
25pub enum AssocCtxt {
26    Trait,
27    Impl { of_trait: bool },
28}
29
30#[derive(Copy, Clone, Debug, PartialEq)]
31pub enum FnCtxt {
32    Free,
33    Foreign,
34    Assoc(AssocCtxt),
35}
36
37#[derive(Copy, Clone, Debug)]
38pub enum BoundKind {
39    /// Trait bounds in generics bounds and type/trait alias.
40    /// E.g., `<T: Bound>`, `type A: Bound`, or `where T: Bound`.
41    Bound,
42
43    /// Trait bounds in `impl` type.
44    /// E.g., `type Foo = impl Bound1 + Bound2 + Bound3`.
45    Impl,
46
47    /// Trait bounds in trait object type.
48    /// E.g., `dyn Bound1 + Bound2 + Bound3`.
49    TraitObject,
50
51    /// Super traits of a trait.
52    /// E.g., `trait A: B`
53    SuperTraits,
54}
55impl BoundKind {
56    pub fn descr(self) -> &'static str {
57        match self {
58            BoundKind::Bound => "bounds",
59            BoundKind::Impl => "`impl Trait`",
60            BoundKind::TraitObject => "`dyn` trait object bounds",
61            BoundKind::SuperTraits => "supertrait bounds",
62        }
63    }
64}
65
66#[derive(Copy, Clone, Debug)]
67pub enum FnKind<'a> {
68    /// E.g., `fn foo()`, `fn foo(&self)`, or `extern "Abi" fn foo()`.
69    Fn(FnCtxt, &'a Visibility, &'a Fn),
70
71    /// E.g., `|x, y| body`.
72    Closure(&'a ClosureBinder, &'a Option<CoroutineKind>, &'a FnDecl, &'a Expr),
73}
74
75impl<'a> FnKind<'a> {
76    pub fn header(&self) -> Option<&'a FnHeader> {
77        match *self {
78            FnKind::Fn(_, _, Fn { sig, .. }) => Some(&sig.header),
79            FnKind::Closure(..) => None,
80        }
81    }
82
83    pub fn ident(&self) -> Option<&Ident> {
84        match self {
85            FnKind::Fn(_, _, Fn { ident, .. }) => Some(ident),
86            _ => None,
87        }
88    }
89
90    pub fn decl(&self) -> &'a FnDecl {
91        match self {
92            FnKind::Fn(_, _, Fn { sig, .. }) => &sig.decl,
93            FnKind::Closure(_, _, decl, _) => decl,
94        }
95    }
96
97    pub fn ctxt(&self) -> Option<FnCtxt> {
98        match self {
99            FnKind::Fn(ctxt, ..) => Some(*ctxt),
100            FnKind::Closure(..) => None,
101        }
102    }
103}
104
105#[derive(Copy, Clone, Debug)]
106pub enum LifetimeCtxt {
107    /// Appears in a reference type.
108    Ref,
109    /// Appears as a bound on a type or another lifetime.
110    Bound,
111    /// Appears as a generic argument.
112    GenericArg,
113}
114
115pub trait WalkItemKind {
116    type Ctxt;
117    fn walk<'a, V: Visitor<'a>>(
118        &'a self,
119        span: Span,
120        id: NodeId,
121        visibility: &'a Visibility,
122        ctxt: Self::Ctxt,
123        visitor: &mut V,
124    ) -> V::Result;
125}
126
127/// Each method of the `Visitor` trait is a hook to be potentially
128/// overridden. Each method's default implementation recursively visits
129/// the substructure of the input via the corresponding `walk` method;
130/// e.g., the `visit_item` method by default calls `visit::walk_item`.
131///
132/// If you want to ensure that your code handles every variant
133/// explicitly, you need to override each method. (And you also need
134/// to monitor future changes to `Visitor` in case a new method with a
135/// new default implementation gets introduced.)
136pub trait Visitor<'ast>: Sized {
137    /// The result type of the `visit_*` methods. Can be either `()`,
138    /// or `ControlFlow<T>`.
139    type Result: VisitorResult = ();
140
141    fn visit_ident(&mut self, _ident: &'ast Ident) -> Self::Result {
142        Self::Result::output()
143    }
144    fn visit_foreign_item(&mut self, i: &'ast ForeignItem) -> Self::Result {
145        walk_item(self, i)
146    }
147    fn visit_item(&mut self, i: &'ast Item) -> Self::Result {
148        walk_item(self, i)
149    }
150    fn visit_local(&mut self, l: &'ast Local) -> Self::Result {
151        walk_local(self, l)
152    }
153    fn visit_block(&mut self, b: &'ast Block) -> Self::Result {
154        walk_block(self, b)
155    }
156    fn visit_stmt(&mut self, s: &'ast Stmt) -> Self::Result {
157        walk_stmt(self, s)
158    }
159    fn visit_param(&mut self, param: &'ast Param) -> Self::Result {
160        walk_param(self, param)
161    }
162    fn visit_arm(&mut self, a: &'ast Arm) -> Self::Result {
163        walk_arm(self, a)
164    }
165    fn visit_pat(&mut self, p: &'ast Pat) -> Self::Result {
166        walk_pat(self, p)
167    }
168    fn visit_anon_const(&mut self, c: &'ast AnonConst) -> Self::Result {
169        walk_anon_const(self, c)
170    }
171    fn visit_expr(&mut self, ex: &'ast Expr) -> Self::Result {
172        walk_expr(self, ex)
173    }
174    /// This method is a hack to workaround unstable of `stmt_expr_attributes`.
175    /// It can be removed once that feature is stabilized.
176    fn visit_method_receiver_expr(&mut self, ex: &'ast Expr) -> Self::Result {
177        self.visit_expr(ex)
178    }
179    fn visit_ty(&mut self, t: &'ast Ty) -> Self::Result {
180        walk_ty(self, t)
181    }
182    fn visit_ty_pat(&mut self, t: &'ast TyPat) -> Self::Result {
183        walk_ty_pat(self, t)
184    }
185    fn visit_generic_param(&mut self, param: &'ast GenericParam) -> Self::Result {
186        walk_generic_param(self, param)
187    }
188    fn visit_generics(&mut self, g: &'ast Generics) -> Self::Result {
189        walk_generics(self, g)
190    }
191    fn visit_closure_binder(&mut self, b: &'ast ClosureBinder) -> Self::Result {
192        walk_closure_binder(self, b)
193    }
194    fn visit_contract(&mut self, c: &'ast FnContract) -> Self::Result {
195        walk_contract(self, c)
196    }
197    fn visit_where_predicate(&mut self, p: &'ast WherePredicate) -> Self::Result {
198        walk_where_predicate(self, p)
199    }
200    fn visit_where_predicate_kind(&mut self, k: &'ast WherePredicateKind) -> Self::Result {
201        walk_where_predicate_kind(self, k)
202    }
203    fn visit_fn(&mut self, fk: FnKind<'ast>, _: Span, _: NodeId) -> Self::Result {
204        walk_fn(self, fk)
205    }
206    fn visit_assoc_item(&mut self, i: &'ast AssocItem, ctxt: AssocCtxt) -> Self::Result {
207        walk_assoc_item(self, i, ctxt)
208    }
209    fn visit_trait_ref(&mut self, t: &'ast TraitRef) -> Self::Result {
210        walk_trait_ref(self, t)
211    }
212    fn visit_param_bound(&mut self, bounds: &'ast GenericBound, _ctxt: BoundKind) -> Self::Result {
213        walk_param_bound(self, bounds)
214    }
215    fn visit_precise_capturing_arg(&mut self, arg: &'ast PreciseCapturingArg) -> Self::Result {
216        walk_precise_capturing_arg(self, arg)
217    }
218    fn visit_poly_trait_ref(&mut self, t: &'ast PolyTraitRef) -> Self::Result {
219        walk_poly_trait_ref(self, t)
220    }
221    fn visit_variant_data(&mut self, s: &'ast VariantData) -> Self::Result {
222        walk_struct_def(self, s)
223    }
224    fn visit_field_def(&mut self, s: &'ast FieldDef) -> Self::Result {
225        walk_field_def(self, s)
226    }
227    fn visit_enum_def(&mut self, enum_definition: &'ast EnumDef) -> Self::Result {
228        walk_enum_def(self, enum_definition)
229    }
230    fn visit_variant(&mut self, v: &'ast Variant) -> Self::Result {
231        walk_variant(self, v)
232    }
233    fn visit_variant_discr(&mut self, discr: &'ast AnonConst) -> Self::Result {
234        self.visit_anon_const(discr)
235    }
236    fn visit_label(&mut self, label: &'ast Label) -> Self::Result {
237        walk_label(self, label)
238    }
239    fn visit_lifetime(&mut self, lifetime: &'ast Lifetime, _: LifetimeCtxt) -> Self::Result {
240        walk_lifetime(self, lifetime)
241    }
242    fn visit_mac_call(&mut self, mac: &'ast MacCall) -> Self::Result {
243        walk_mac(self, mac)
244    }
245    fn visit_mac_def(&mut self, _mac: &'ast MacroDef, _id: NodeId) -> Self::Result {
246        Self::Result::output()
247    }
248    fn visit_path(&mut self, path: &'ast Path, _id: NodeId) -> Self::Result {
249        walk_path(self, path)
250    }
251    fn visit_use_tree(
252        &mut self,
253        use_tree: &'ast UseTree,
254        id: NodeId,
255        _nested: bool,
256    ) -> Self::Result {
257        walk_use_tree(self, use_tree, id)
258    }
259    fn visit_path_segment(&mut self, path_segment: &'ast PathSegment) -> Self::Result {
260        walk_path_segment(self, path_segment)
261    }
262    fn visit_generic_args(&mut self, generic_args: &'ast GenericArgs) -> Self::Result {
263        walk_generic_args(self, generic_args)
264    }
265    fn visit_generic_arg(&mut self, generic_arg: &'ast GenericArg) -> Self::Result {
266        walk_generic_arg(self, generic_arg)
267    }
268    fn visit_assoc_item_constraint(
269        &mut self,
270        constraint: &'ast AssocItemConstraint,
271    ) -> Self::Result {
272        walk_assoc_item_constraint(self, constraint)
273    }
274    fn visit_attribute(&mut self, attr: &'ast Attribute) -> Self::Result {
275        walk_attribute(self, attr)
276    }
277    fn visit_vis(&mut self, vis: &'ast Visibility) -> Self::Result {
278        walk_vis(self, vis)
279    }
280    fn visit_fn_ret_ty(&mut self, ret_ty: &'ast FnRetTy) -> Self::Result {
281        walk_fn_ret_ty(self, ret_ty)
282    }
283    fn visit_fn_header(&mut self, header: &'ast FnHeader) -> Self::Result {
284        walk_fn_header(self, header)
285    }
286    fn visit_expr_field(&mut self, f: &'ast ExprField) -> Self::Result {
287        walk_expr_field(self, f)
288    }
289    fn visit_pat_field(&mut self, fp: &'ast PatField) -> Self::Result {
290        walk_pat_field(self, fp)
291    }
292    fn visit_crate(&mut self, krate: &'ast Crate) -> Self::Result {
293        walk_crate(self, krate)
294    }
295    fn visit_inline_asm(&mut self, asm: &'ast InlineAsm) -> Self::Result {
296        walk_inline_asm(self, asm)
297    }
298    fn visit_format_args(&mut self, fmt: &'ast FormatArgs) -> Self::Result {
299        walk_format_args(self, fmt)
300    }
301    fn visit_inline_asm_sym(&mut self, sym: &'ast InlineAsmSym) -> Self::Result {
302        walk_inline_asm_sym(self, sym)
303    }
304    fn visit_capture_by(&mut self, _capture_by: &'ast CaptureBy) -> Self::Result {
305        Self::Result::output()
306    }
307    fn visit_coroutine_kind(&mut self, _coroutine_kind: &'ast CoroutineKind) -> Self::Result {
308        Self::Result::output()
309    }
310    fn visit_fn_decl(&mut self, fn_decl: &'ast FnDecl) -> Self::Result {
311        walk_fn_decl(self, fn_decl)
312    }
313    fn visit_qself(&mut self, qs: &'ast Option<P<QSelf>>) -> Self::Result {
314        walk_qself(self, qs)
315    }
316}
317
318#[macro_export]
319macro_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
385common_visitor_and_walkers!(Visitor<'a>);
386
387pub fn walk_crate<'a, V: Visitor<'a>>(visitor: &mut V, krate: &'a Crate) -> V::Result {
388    let Crate { attrs, items, spans: _, id: _, is_placeholder: _ } = krate;
389    walk_list!(visitor, visit_attribute, attrs);
390    walk_list!(visitor, visit_item, items);
391    V::Result::output()
392}
393
394pub fn walk_local<'a, V: Visitor<'a>>(visitor: &mut V, local: &'a Local) -> V::Result {
395    let Local { id: _, super_: _, pat, ty, kind, span: _, colon_sp: _, attrs, tokens: _ } = local;
396    walk_list!(visitor, visit_attribute, attrs);
397    try_visit!(visitor.visit_pat(pat));
398    visit_opt!(visitor, visit_ty, ty);
399    if let Some((init, els)) = kind.init_else_opt() {
400        try_visit!(visitor.visit_expr(init));
401        visit_opt!(visitor, visit_block, els);
402    }
403    V::Result::output()
404}
405
406pub fn walk_poly_trait_ref<'a, V>(visitor: &mut V, trait_ref: &'a PolyTraitRef) -> V::Result
407where
408    V: Visitor<'a>,
409{
410    let PolyTraitRef { bound_generic_params, modifiers: _, trait_ref, span: _ } = trait_ref;
411    walk_list!(visitor, visit_generic_param, bound_generic_params);
412    visitor.visit_trait_ref(trait_ref)
413}
414
415pub fn walk_trait_ref<'a, V: Visitor<'a>>(visitor: &mut V, trait_ref: &'a TraitRef) -> V::Result {
416    let TraitRef { path, ref_id } = trait_ref;
417    visitor.visit_path(path, *ref_id)
418}
419
420impl WalkItemKind for ItemKind {
421    type Ctxt = ();
422    fn walk<'a, V: Visitor<'a>>(
423        &'a self,
424        span: Span,
425        id: NodeId,
426        vis: &'a Visibility,
427        _ctxt: Self::Ctxt,
428        visitor: &mut V,
429    ) -> V::Result {
430        match self {
431            ItemKind::ExternCrate(_rename, ident) => try_visit!(visitor.visit_ident(ident)),
432            ItemKind::Use(use_tree) => try_visit!(visitor.visit_use_tree(use_tree, id, false)),
433            ItemKind::Static(box StaticItem {
434                ident,
435                ty,
436                safety: _,
437                mutability: _,
438                expr,
439                define_opaque,
440            }) => {
441                try_visit!(visitor.visit_ident(ident));
442                try_visit!(visitor.visit_ty(ty));
443                visit_opt!(visitor, visit_expr, expr);
444                try_visit!(walk_define_opaques(visitor, define_opaque));
445            }
446            ItemKind::Const(box ConstItem {
447                defaultness: _,
448                ident,
449                generics,
450                ty,
451                expr,
452                define_opaque,
453            }) => {
454                try_visit!(visitor.visit_ident(ident));
455                try_visit!(visitor.visit_generics(generics));
456                try_visit!(visitor.visit_ty(ty));
457                visit_opt!(visitor, visit_expr, expr);
458                try_visit!(walk_define_opaques(visitor, define_opaque));
459            }
460            ItemKind::Fn(func) => {
461                let kind = FnKind::Fn(FnCtxt::Free, vis, &*func);
462                try_visit!(visitor.visit_fn(kind, span, id));
463            }
464            ItemKind::Mod(_unsafety, ident, mod_kind) => {
465                try_visit!(visitor.visit_ident(ident));
466                match mod_kind {
467                    ModKind::Loaded(items, _inline, _inner_span, _) => {
468                        walk_list!(visitor, visit_item, items);
469                    }
470                    ModKind::Unloaded => {}
471                }
472            }
473            ItemKind::ForeignMod(ForeignMod { extern_span: _, safety: _, abi: _, items }) => {
474                walk_list!(visitor, visit_foreign_item, items);
475            }
476            ItemKind::GlobalAsm(asm) => try_visit!(visitor.visit_inline_asm(asm)),
477            ItemKind::TyAlias(box TyAlias {
478                generics,
479                ident,
480                bounds,
481                ty,
482                defaultness: _,
483                where_clauses: _,
484            }) => {
485                try_visit!(visitor.visit_ident(ident));
486                try_visit!(visitor.visit_generics(generics));
487                walk_list!(visitor, visit_param_bound, bounds, BoundKind::Bound);
488                visit_opt!(visitor, visit_ty, ty);
489            }
490            ItemKind::Enum(ident, enum_definition, generics) => {
491                try_visit!(visitor.visit_ident(ident));
492                try_visit!(visitor.visit_generics(generics));
493                try_visit!(visitor.visit_enum_def(enum_definition));
494            }
495            ItemKind::Impl(box Impl {
496                defaultness: _,
497                safety: _,
498                generics,
499                constness: _,
500                polarity: _,
501                of_trait,
502                self_ty,
503                items,
504            }) => {
505                try_visit!(visitor.visit_generics(generics));
506                visit_opt!(visitor, visit_trait_ref, of_trait);
507                try_visit!(visitor.visit_ty(self_ty));
508                walk_list!(
509                    visitor,
510                    visit_assoc_item,
511                    items,
512                    AssocCtxt::Impl { of_trait: of_trait.is_some() }
513                );
514            }
515            ItemKind::Struct(ident, struct_definition, generics)
516            | ItemKind::Union(ident, struct_definition, generics) => {
517                try_visit!(visitor.visit_ident(ident));
518                try_visit!(visitor.visit_generics(generics));
519                try_visit!(visitor.visit_variant_data(struct_definition));
520            }
521            ItemKind::Trait(box Trait {
522                safety: _,
523                is_auto: _,
524                ident,
525                generics,
526                bounds,
527                items,
528            }) => {
529                try_visit!(visitor.visit_ident(ident));
530                try_visit!(visitor.visit_generics(generics));
531                walk_list!(visitor, visit_param_bound, bounds, BoundKind::SuperTraits);
532                walk_list!(visitor, visit_assoc_item, items, AssocCtxt::Trait);
533            }
534            ItemKind::TraitAlias(ident, generics, bounds) => {
535                try_visit!(visitor.visit_ident(ident));
536                try_visit!(visitor.visit_generics(generics));
537                walk_list!(visitor, visit_param_bound, bounds, BoundKind::Bound);
538            }
539            ItemKind::MacCall(mac) => try_visit!(visitor.visit_mac_call(mac)),
540            ItemKind::MacroDef(ident, ts) => {
541                try_visit!(visitor.visit_ident(ident));
542                try_visit!(visitor.visit_mac_def(ts, id))
543            }
544            ItemKind::Delegation(box Delegation {
545                id,
546                qself,
547                path,
548                ident,
549                rename,
550                body,
551                from_glob: _,
552            }) => {
553                try_visit!(visitor.visit_qself(qself));
554                try_visit!(visitor.visit_path(path, *id));
555                try_visit!(visitor.visit_ident(ident));
556                visit_opt!(visitor, visit_ident, rename);
557                visit_opt!(visitor, visit_block, body);
558            }
559            ItemKind::DelegationMac(box DelegationMac { qself, prefix, suffixes, body }) => {
560                try_visit!(visitor.visit_qself(qself));
561                try_visit!(visitor.visit_path(prefix, id));
562                if let Some(suffixes) = suffixes {
563                    for (ident, rename) in suffixes {
564                        visitor.visit_ident(ident);
565                        if let Some(rename) = rename {
566                            visitor.visit_ident(rename);
567                        }
568                    }
569                }
570                visit_opt!(visitor, visit_block, body);
571            }
572        }
573        V::Result::output()
574    }
575}
576
577pub fn walk_enum_def<'a, V: Visitor<'a>>(
578    visitor: &mut V,
579    EnumDef { variants }: &'a EnumDef,
580) -> V::Result {
581    walk_list!(visitor, visit_variant, variants);
582    V::Result::output()
583}
584
585pub fn walk_variant<'a, V: Visitor<'a>>(visitor: &mut V, variant: &'a Variant) -> V::Result
586where
587    V: Visitor<'a>,
588{
589    let Variant { attrs, id: _, span: _, vis, ident, data, disr_expr, is_placeholder: _ } = variant;
590    walk_list!(visitor, visit_attribute, attrs);
591    try_visit!(visitor.visit_vis(vis));
592    try_visit!(visitor.visit_ident(ident));
593    try_visit!(visitor.visit_variant_data(data));
594    visit_opt!(visitor, visit_variant_discr, disr_expr);
595    V::Result::output()
596}
597
598pub fn walk_expr_field<'a, V: Visitor<'a>>(visitor: &mut V, f: &'a ExprField) -> V::Result {
599    let ExprField { attrs, id: _, span: _, ident, expr, is_shorthand: _, is_placeholder: _ } = f;
600    walk_list!(visitor, visit_attribute, attrs);
601    try_visit!(visitor.visit_ident(ident));
602    try_visit!(visitor.visit_expr(expr));
603    V::Result::output()
604}
605
606pub fn walk_pat_field<'a, V: Visitor<'a>>(visitor: &mut V, fp: &'a PatField) -> V::Result {
607    let PatField { ident, pat, is_shorthand: _, attrs, id: _, span: _, is_placeholder: _ } = fp;
608    walk_list!(visitor, visit_attribute, attrs);
609    try_visit!(visitor.visit_ident(ident));
610    try_visit!(visitor.visit_pat(pat));
611    V::Result::output()
612}
613
614pub fn walk_ty<'a, V: Visitor<'a>>(visitor: &mut V, typ: &'a Ty) -> V::Result {
615    let Ty { id, kind, span: _, tokens: _ } = typ;
616    match kind {
617        TyKind::Slice(ty) | TyKind::Paren(ty) => try_visit!(visitor.visit_ty(ty)),
618        TyKind::Ptr(MutTy { ty, mutbl: _ }) => try_visit!(visitor.visit_ty(ty)),
619        TyKind::Ref(opt_lifetime, MutTy { ty, mutbl: _ })
620        | TyKind::PinnedRef(opt_lifetime, MutTy { ty, mutbl: _ }) => {
621            visit_opt!(visitor, visit_lifetime, opt_lifetime, LifetimeCtxt::Ref);
622            try_visit!(visitor.visit_ty(ty));
623        }
624        TyKind::Tup(tuple_element_types) => {
625            walk_list!(visitor, visit_ty, tuple_element_types);
626        }
627        TyKind::BareFn(function_declaration) => {
628            let BareFnTy { safety: _, ext: _, generic_params, decl, decl_span: _ } =
629                &**function_declaration;
630            walk_list!(visitor, visit_generic_param, generic_params);
631            try_visit!(visitor.visit_fn_decl(decl));
632        }
633        TyKind::UnsafeBinder(binder) => {
634            walk_list!(visitor, visit_generic_param, &binder.generic_params);
635            try_visit!(visitor.visit_ty(&binder.inner_ty));
636        }
637        TyKind::Path(maybe_qself, path) => {
638            try_visit!(visitor.visit_qself(maybe_qself));
639            try_visit!(visitor.visit_path(path, *id));
640        }
641        TyKind::Pat(ty, pat) => {
642            try_visit!(visitor.visit_ty(ty));
643            try_visit!(visitor.visit_ty_pat(pat));
644        }
645        TyKind::Array(ty, length) => {
646            try_visit!(visitor.visit_ty(ty));
647            try_visit!(visitor.visit_anon_const(length));
648        }
649        TyKind::TraitObject(bounds, _syntax) => {
650            walk_list!(visitor, visit_param_bound, bounds, BoundKind::TraitObject);
651        }
652        TyKind::ImplTrait(_id, bounds) => {
653            walk_list!(visitor, visit_param_bound, bounds, BoundKind::Impl);
654        }
655        TyKind::Typeof(expression) => try_visit!(visitor.visit_anon_const(expression)),
656        TyKind::Infer | TyKind::ImplicitSelf | TyKind::Dummy => {}
657        TyKind::Err(_guar) => {}
658        TyKind::MacCall(mac) => try_visit!(visitor.visit_mac_call(mac)),
659        TyKind::Never | TyKind::CVarArgs => {}
660    }
661    V::Result::output()
662}
663
664pub fn walk_ty_pat<'a, V: Visitor<'a>>(visitor: &mut V, tp: &'a TyPat) -> V::Result {
665    let TyPat { id: _, kind, span: _, tokens: _ } = tp;
666    match kind {
667        TyPatKind::Range(start, end, _include_end) => {
668            visit_opt!(visitor, visit_anon_const, start);
669            visit_opt!(visitor, visit_anon_const, end);
670        }
671        TyPatKind::Or(variants) => walk_list!(visitor, visit_ty_pat, variants),
672        TyPatKind::Err(_) => {}
673    }
674    V::Result::output()
675}
676
677fn walk_qself<'a, V: Visitor<'a>>(visitor: &mut V, qself: &'a Option<P<QSelf>>) -> V::Result {
678    if let Some(qself) = qself {
679        let QSelf { ty, path_span: _, position: _ } = &**qself;
680        try_visit!(visitor.visit_ty(ty));
681    }
682    V::Result::output()
683}
684
685pub fn walk_path<'a, V: Visitor<'a>>(visitor: &mut V, path: &'a Path) -> V::Result {
686    let Path { span: _, segments, tokens: _ } = path;
687    walk_list!(visitor, visit_path_segment, segments);
688    V::Result::output()
689}
690
691pub fn walk_use_tree<'a, V: Visitor<'a>>(
692    visitor: &mut V,
693    use_tree: &'a UseTree,
694    id: NodeId,
695) -> V::Result {
696    let UseTree { prefix, kind, span: _ } = use_tree;
697    try_visit!(visitor.visit_path(prefix, id));
698    match kind {
699        UseTreeKind::Simple(rename) => {
700            // The extra IDs are handled during AST lowering.
701            visit_opt!(visitor, visit_ident, rename);
702        }
703        UseTreeKind::Glob => {}
704        UseTreeKind::Nested { items, span: _ } => {
705            for &(ref nested_tree, nested_id) in items {
706                try_visit!(visitor.visit_use_tree(nested_tree, nested_id, true));
707            }
708        }
709    }
710    V::Result::output()
711}
712
713pub fn walk_path_segment<'a, V: Visitor<'a>>(
714    visitor: &mut V,
715    segment: &'a PathSegment,
716) -> V::Result {
717    let PathSegment { ident, id: _, args } = segment;
718    try_visit!(visitor.visit_ident(ident));
719    visit_opt!(visitor, visit_generic_args, args);
720    V::Result::output()
721}
722
723pub fn walk_generic_args<'a, V>(visitor: &mut V, generic_args: &'a GenericArgs) -> V::Result
724where
725    V: Visitor<'a>,
726{
727    match generic_args {
728        GenericArgs::AngleBracketed(AngleBracketedArgs { span: _, args }) => {
729            for arg in args {
730                match arg {
731                    AngleBracketedArg::Arg(a) => try_visit!(visitor.visit_generic_arg(a)),
732                    AngleBracketedArg::Constraint(c) => {
733                        try_visit!(visitor.visit_assoc_item_constraint(c))
734                    }
735                }
736            }
737        }
738        GenericArgs::Parenthesized(data) => {
739            let ParenthesizedArgs { span: _, inputs, inputs_span: _, output } = data;
740            walk_list!(visitor, visit_ty, inputs);
741            try_visit!(visitor.visit_fn_ret_ty(output));
742        }
743        GenericArgs::ParenthesizedElided(_span) => {}
744    }
745    V::Result::output()
746}
747
748pub fn walk_generic_arg<'a, V>(visitor: &mut V, generic_arg: &'a GenericArg) -> V::Result
749where
750    V: Visitor<'a>,
751{
752    match generic_arg {
753        GenericArg::Lifetime(lt) => visitor.visit_lifetime(lt, LifetimeCtxt::GenericArg),
754        GenericArg::Type(ty) => visitor.visit_ty(ty),
755        GenericArg::Const(ct) => visitor.visit_anon_const(ct),
756    }
757}
758
759pub fn walk_assoc_item_constraint<'a, V: Visitor<'a>>(
760    visitor: &mut V,
761    constraint: &'a AssocItemConstraint,
762) -> V::Result {
763    let AssocItemConstraint { id: _, ident, gen_args, kind, span: _ } = constraint;
764    try_visit!(visitor.visit_ident(ident));
765    visit_opt!(visitor, visit_generic_args, gen_args);
766    match kind {
767        AssocItemConstraintKind::Equality { term } => match term {
768            Term::Ty(ty) => try_visit!(visitor.visit_ty(ty)),
769            Term::Const(c) => try_visit!(visitor.visit_anon_const(c)),
770        },
771        AssocItemConstraintKind::Bound { bounds } => {
772            walk_list!(visitor, visit_param_bound, bounds, BoundKind::Bound);
773        }
774    }
775    V::Result::output()
776}
777
778pub fn walk_pat<'a, V: Visitor<'a>>(visitor: &mut V, pattern: &'a Pat) -> V::Result {
779    let Pat { id, kind, span: _, tokens: _ } = pattern;
780    match kind {
781        PatKind::TupleStruct(opt_qself, path, elems) => {
782            try_visit!(visitor.visit_qself(opt_qself));
783            try_visit!(visitor.visit_path(path, *id));
784            walk_list!(visitor, visit_pat, elems);
785        }
786        PatKind::Path(opt_qself, path) => {
787            try_visit!(visitor.visit_qself(opt_qself));
788            try_visit!(visitor.visit_path(path, *id))
789        }
790        PatKind::Struct(opt_qself, path, fields, _rest) => {
791            try_visit!(visitor.visit_qself(opt_qself));
792            try_visit!(visitor.visit_path(path, *id));
793            walk_list!(visitor, visit_pat_field, fields);
794        }
795        PatKind::Box(subpattern) | PatKind::Deref(subpattern) | PatKind::Paren(subpattern) => {
796            try_visit!(visitor.visit_pat(subpattern));
797        }
798        PatKind::Ref(subpattern, _ /*mutbl*/) => {
799            try_visit!(visitor.visit_pat(subpattern));
800        }
801        PatKind::Ident(_bmode, ident, optional_subpattern) => {
802            try_visit!(visitor.visit_ident(ident));
803            visit_opt!(visitor, visit_pat, optional_subpattern);
804        }
805        PatKind::Expr(expression) => try_visit!(visitor.visit_expr(expression)),
806        PatKind::Range(lower_bound, upper_bound, _end) => {
807            visit_opt!(visitor, visit_expr, lower_bound);
808            visit_opt!(visitor, visit_expr, upper_bound);
809        }
810        PatKind::Guard(subpattern, guard_condition) => {
811            try_visit!(visitor.visit_pat(subpattern));
812            try_visit!(visitor.visit_expr(guard_condition));
813        }
814        PatKind::Missing | PatKind::Wild | PatKind::Rest | PatKind::Never => {}
815        PatKind::Err(_guar) => {}
816        PatKind::Tuple(elems) | PatKind::Slice(elems) | PatKind::Or(elems) => {
817            walk_list!(visitor, visit_pat, elems);
818        }
819        PatKind::MacCall(mac) => try_visit!(visitor.visit_mac_call(mac)),
820    }
821    V::Result::output()
822}
823
824impl WalkItemKind for ForeignItemKind {
825    type Ctxt = ();
826    fn walk<'a, V: Visitor<'a>>(
827        &'a self,
828        span: Span,
829        id: NodeId,
830        vis: &'a Visibility,
831        _ctxt: Self::Ctxt,
832        visitor: &mut V,
833    ) -> V::Result {
834        match self {
835            ForeignItemKind::Static(box StaticItem {
836                ident,
837                ty,
838                mutability: _,
839                expr,
840                safety: _,
841                define_opaque,
842            }) => {
843                try_visit!(visitor.visit_ident(ident));
844                try_visit!(visitor.visit_ty(ty));
845                visit_opt!(visitor, visit_expr, expr);
846                try_visit!(walk_define_opaques(visitor, define_opaque));
847            }
848            ForeignItemKind::Fn(func) => {
849                let kind = FnKind::Fn(FnCtxt::Foreign, vis, &*func);
850                try_visit!(visitor.visit_fn(kind, span, id));
851            }
852            ForeignItemKind::TyAlias(box TyAlias {
853                generics,
854                ident,
855                bounds,
856                ty,
857                defaultness: _,
858                where_clauses: _,
859            }) => {
860                try_visit!(visitor.visit_ident(ident));
861                try_visit!(visitor.visit_generics(generics));
862                walk_list!(visitor, visit_param_bound, bounds, BoundKind::Bound);
863                visit_opt!(visitor, visit_ty, ty);
864            }
865            ForeignItemKind::MacCall(mac) => {
866                try_visit!(visitor.visit_mac_call(mac));
867            }
868        }
869        V::Result::output()
870    }
871}
872
873pub fn walk_param_bound<'a, V: Visitor<'a>>(visitor: &mut V, bound: &'a GenericBound) -> V::Result {
874    match bound {
875        GenericBound::Trait(trait_ref) => visitor.visit_poly_trait_ref(trait_ref),
876        GenericBound::Outlives(lifetime) => visitor.visit_lifetime(lifetime, LifetimeCtxt::Bound),
877        GenericBound::Use(args, _span) => {
878            walk_list!(visitor, visit_precise_capturing_arg, args);
879            V::Result::output()
880        }
881    }
882}
883
884pub fn walk_precise_capturing_arg<'a, V: Visitor<'a>>(
885    visitor: &mut V,
886    arg: &'a PreciseCapturingArg,
887) -> V::Result {
888    match arg {
889        PreciseCapturingArg::Lifetime(lt) => visitor.visit_lifetime(lt, LifetimeCtxt::GenericArg),
890        PreciseCapturingArg::Arg(path, id) => visitor.visit_path(path, *id),
891    }
892}
893
894pub fn walk_generic_param<'a, V: Visitor<'a>>(
895    visitor: &mut V,
896    param: &'a GenericParam,
897) -> V::Result {
898    let GenericParam { id: _, ident, attrs, bounds, is_placeholder: _, kind, colon_span: _ } =
899        param;
900    walk_list!(visitor, visit_attribute, attrs);
901    try_visit!(visitor.visit_ident(ident));
902    walk_list!(visitor, visit_param_bound, bounds, BoundKind::Bound);
903    match kind {
904        GenericParamKind::Lifetime => (),
905        GenericParamKind::Type { default } => visit_opt!(visitor, visit_ty, default),
906        GenericParamKind::Const { ty, default, kw_span: _ } => {
907            try_visit!(visitor.visit_ty(ty));
908            visit_opt!(visitor, visit_anon_const, default);
909        }
910    }
911    V::Result::output()
912}
913
914pub fn walk_generics<'a, V: Visitor<'a>>(visitor: &mut V, generics: &'a Generics) -> V::Result {
915    let Generics { params, where_clause, span: _ } = generics;
916    let WhereClause { has_where_token: _, predicates, span: _ } = where_clause;
917    walk_list!(visitor, visit_generic_param, params);
918    walk_list!(visitor, visit_where_predicate, predicates);
919    V::Result::output()
920}
921
922pub fn walk_closure_binder<'a, V: Visitor<'a>>(
923    visitor: &mut V,
924    binder: &'a ClosureBinder,
925) -> V::Result {
926    match binder {
927        ClosureBinder::NotPresent => {}
928        ClosureBinder::For { generic_params, span: _ } => {
929            walk_list!(visitor, visit_generic_param, generic_params)
930        }
931    }
932    V::Result::output()
933}
934
935pub fn walk_contract<'a, V: Visitor<'a>>(visitor: &mut V, c: &'a FnContract) -> V::Result {
936    let FnContract { requires, ensures } = c;
937    if let Some(pred) = requires {
938        visitor.visit_expr(pred);
939    }
940    if let Some(pred) = ensures {
941        visitor.visit_expr(pred);
942    }
943    V::Result::output()
944}
945
946pub fn walk_where_predicate<'a, V: Visitor<'a>>(
947    visitor: &mut V,
948    predicate: &'a WherePredicate,
949) -> V::Result {
950    let WherePredicate { attrs, kind, id: _, span: _, is_placeholder: _ } = predicate;
951    walk_list!(visitor, visit_attribute, attrs);
952    visitor.visit_where_predicate_kind(kind)
953}
954
955pub fn walk_where_predicate_kind<'a, V: Visitor<'a>>(
956    visitor: &mut V,
957    kind: &'a WherePredicateKind,
958) -> V::Result {
959    match kind {
960        WherePredicateKind::BoundPredicate(WhereBoundPredicate {
961            bounded_ty,
962            bounds,
963            bound_generic_params,
964        }) => {
965            walk_list!(visitor, visit_generic_param, bound_generic_params);
966            try_visit!(visitor.visit_ty(bounded_ty));
967            walk_list!(visitor, visit_param_bound, bounds, BoundKind::Bound);
968        }
969        WherePredicateKind::RegionPredicate(WhereRegionPredicate { lifetime, bounds }) => {
970            try_visit!(visitor.visit_lifetime(lifetime, LifetimeCtxt::Bound));
971            walk_list!(visitor, visit_param_bound, bounds, BoundKind::Bound);
972        }
973        WherePredicateKind::EqPredicate(WhereEqPredicate { lhs_ty, rhs_ty }) => {
974            try_visit!(visitor.visit_ty(lhs_ty));
975            try_visit!(visitor.visit_ty(rhs_ty));
976        }
977    }
978    V::Result::output()
979}
980
981pub fn walk_fn_ret_ty<'a, V: Visitor<'a>>(visitor: &mut V, ret_ty: &'a FnRetTy) -> V::Result {
982    match ret_ty {
983        FnRetTy::Default(_span) => {}
984        FnRetTy::Ty(output_ty) => try_visit!(visitor.visit_ty(output_ty)),
985    }
986    V::Result::output()
987}
988
989pub fn walk_fn_decl<'a, V: Visitor<'a>>(
990    visitor: &mut V,
991    FnDecl { inputs, output }: &'a FnDecl,
992) -> V::Result {
993    walk_list!(visitor, visit_param, inputs);
994    visitor.visit_fn_ret_ty(output)
995}
996
997pub fn walk_fn<'a, V: Visitor<'a>>(visitor: &mut V, kind: FnKind<'a>) -> V::Result {
998    match kind {
999        FnKind::Fn(
1000            _ctxt,
1001            _vis,
1002            Fn {
1003                defaultness: _,
1004                ident,
1005                sig: FnSig { header, decl, span: _ },
1006                generics,
1007                contract,
1008                body,
1009                define_opaque,
1010            },
1011        ) => {
1012            // Visibility is visited as a part of the item.
1013            try_visit!(visitor.visit_ident(ident));
1014            try_visit!(visitor.visit_fn_header(header));
1015            try_visit!(visitor.visit_generics(generics));
1016            try_visit!(visitor.visit_fn_decl(decl));
1017            visit_opt!(visitor, visit_contract, contract);
1018            visit_opt!(visitor, visit_block, body);
1019            try_visit!(walk_define_opaques(visitor, define_opaque));
1020        }
1021        FnKind::Closure(binder, coroutine_kind, decl, body) => {
1022            try_visit!(visitor.visit_closure_binder(binder));
1023            visit_opt!(visitor, visit_coroutine_kind, coroutine_kind.as_ref());
1024            try_visit!(visitor.visit_fn_decl(decl));
1025            try_visit!(visitor.visit_expr(body));
1026        }
1027    }
1028    V::Result::output()
1029}
1030
1031impl WalkItemKind for AssocItemKind {
1032    type Ctxt = AssocCtxt;
1033    fn walk<'a, V: Visitor<'a>>(
1034        &'a self,
1035        span: Span,
1036        id: NodeId,
1037        vis: &'a Visibility,
1038        ctxt: Self::Ctxt,
1039        visitor: &mut V,
1040    ) -> V::Result {
1041        match self {
1042            AssocItemKind::Const(box ConstItem {
1043                defaultness: _,
1044                ident,
1045                generics,
1046                ty,
1047                expr,
1048                define_opaque,
1049            }) => {
1050                try_visit!(visitor.visit_ident(ident));
1051                try_visit!(visitor.visit_generics(generics));
1052                try_visit!(visitor.visit_ty(ty));
1053                visit_opt!(visitor, visit_expr, expr);
1054                try_visit!(walk_define_opaques(visitor, define_opaque));
1055            }
1056            AssocItemKind::Fn(func) => {
1057                let kind = FnKind::Fn(FnCtxt::Assoc(ctxt), vis, &*func);
1058                try_visit!(visitor.visit_fn(kind, span, id));
1059            }
1060            AssocItemKind::Type(box TyAlias {
1061                generics,
1062                ident,
1063                bounds,
1064                ty,
1065                defaultness: _,
1066                where_clauses: _,
1067            }) => {
1068                try_visit!(visitor.visit_generics(generics));
1069                try_visit!(visitor.visit_ident(ident));
1070                walk_list!(visitor, visit_param_bound, bounds, BoundKind::Bound);
1071                visit_opt!(visitor, visit_ty, ty);
1072            }
1073            AssocItemKind::MacCall(mac) => {
1074                try_visit!(visitor.visit_mac_call(mac));
1075            }
1076            AssocItemKind::Delegation(box Delegation {
1077                id,
1078                qself,
1079                path,
1080                ident,
1081                rename,
1082                body,
1083                from_glob: _,
1084            }) => {
1085                try_visit!(visitor.visit_qself(qself));
1086                try_visit!(visitor.visit_path(path, *id));
1087                try_visit!(visitor.visit_ident(ident));
1088                visit_opt!(visitor, visit_ident, rename);
1089                visit_opt!(visitor, visit_block, body);
1090            }
1091            AssocItemKind::DelegationMac(box DelegationMac { qself, prefix, suffixes, body }) => {
1092                try_visit!(visitor.visit_qself(qself));
1093                try_visit!(visitor.visit_path(prefix, id));
1094                if let Some(suffixes) = suffixes {
1095                    for (ident, rename) in suffixes {
1096                        visitor.visit_ident(ident);
1097                        if let Some(rename) = rename {
1098                            visitor.visit_ident(rename);
1099                        }
1100                    }
1101                }
1102                visit_opt!(visitor, visit_block, body);
1103            }
1104        }
1105        V::Result::output()
1106    }
1107}
1108
1109pub fn walk_item<'a, V: Visitor<'a>>(
1110    visitor: &mut V,
1111    item: &'a Item<impl WalkItemKind<Ctxt = ()>>,
1112) -> V::Result {
1113    walk_item_ctxt(visitor, item, ())
1114}
1115
1116pub fn walk_assoc_item<'a, V: Visitor<'a>>(
1117    visitor: &mut V,
1118    item: &'a AssocItem,
1119    ctxt: AssocCtxt,
1120) -> V::Result {
1121    walk_item_ctxt(visitor, item, ctxt)
1122}
1123
1124fn walk_item_ctxt<'a, V: Visitor<'a>, K: WalkItemKind>(
1125    visitor: &mut V,
1126    item: &'a Item<K>,
1127    ctxt: K::Ctxt,
1128) -> V::Result {
1129    let Item { id, span, vis, attrs, kind, tokens: _ } = item;
1130    walk_list!(visitor, visit_attribute, attrs);
1131    try_visit!(visitor.visit_vis(vis));
1132    try_visit!(kind.walk(*span, *id, vis, ctxt, visitor));
1133    V::Result::output()
1134}
1135
1136pub fn walk_struct_def<'a, V: Visitor<'a>>(
1137    visitor: &mut V,
1138    struct_definition: &'a VariantData,
1139) -> V::Result {
1140    walk_list!(visitor, visit_field_def, struct_definition.fields());
1141    V::Result::output()
1142}
1143
1144pub fn walk_field_def<'a, V: Visitor<'a>>(visitor: &mut V, field: &'a FieldDef) -> V::Result {
1145    let FieldDef { attrs, id: _, span: _, vis, ident, ty, is_placeholder: _, safety: _, default } =
1146        field;
1147    walk_list!(visitor, visit_attribute, attrs);
1148    try_visit!(visitor.visit_vis(vis));
1149    visit_opt!(visitor, visit_ident, ident);
1150    try_visit!(visitor.visit_ty(ty));
1151    visit_opt!(visitor, visit_anon_const, &*default);
1152    V::Result::output()
1153}
1154
1155pub fn walk_block<'a, V: Visitor<'a>>(visitor: &mut V, block: &'a Block) -> V::Result {
1156    let Block { stmts, id: _, rules: _, span: _, tokens: _ } = block;
1157    walk_list!(visitor, visit_stmt, stmts);
1158    V::Result::output()
1159}
1160
1161pub fn walk_stmt<'a, V: Visitor<'a>>(visitor: &mut V, statement: &'a Stmt) -> V::Result {
1162    let Stmt { id: _, kind, span: _ } = statement;
1163    match kind {
1164        StmtKind::Let(local) => try_visit!(visitor.visit_local(local)),
1165        StmtKind::Item(item) => try_visit!(visitor.visit_item(item)),
1166        StmtKind::Expr(expr) | StmtKind::Semi(expr) => try_visit!(visitor.visit_expr(expr)),
1167        StmtKind::Empty => {}
1168        StmtKind::MacCall(mac) => {
1169            let MacCallStmt { mac, attrs, style: _, tokens: _ } = &**mac;
1170            walk_list!(visitor, visit_attribute, attrs);
1171            try_visit!(visitor.visit_mac_call(mac));
1172        }
1173    }
1174    V::Result::output()
1175}
1176
1177pub fn walk_mac<'a, V: Visitor<'a>>(visitor: &mut V, mac: &'a MacCall) -> V::Result {
1178    let MacCall { path, args: _ } = mac;
1179    visitor.visit_path(path, DUMMY_NODE_ID)
1180}
1181
1182pub fn walk_anon_const<'a, V: Visitor<'a>>(visitor: &mut V, constant: &'a AnonConst) -> V::Result {
1183    let AnonConst { id: _, value } = constant;
1184    visitor.visit_expr(value)
1185}
1186
1187pub fn walk_inline_asm<'a, V: Visitor<'a>>(visitor: &mut V, asm: &'a InlineAsm) -> V::Result {
1188    let InlineAsm {
1189        asm_macro: _,
1190        template: _,
1191        template_strs: _,
1192        operands,
1193        clobber_abis: _,
1194        options: _,
1195        line_spans: _,
1196    } = asm;
1197    for (op, _span) in operands {
1198        match op {
1199            InlineAsmOperand::In { expr, reg: _ }
1200            | InlineAsmOperand::Out { expr: Some(expr), reg: _, late: _ }
1201            | InlineAsmOperand::InOut { expr, reg: _, late: _ } => {
1202                try_visit!(visitor.visit_expr(expr))
1203            }
1204            InlineAsmOperand::Out { expr: None, reg: _, late: _ } => {}
1205            InlineAsmOperand::SplitInOut { in_expr, out_expr, reg: _, late: _ } => {
1206                try_visit!(visitor.visit_expr(in_expr));
1207                visit_opt!(visitor, visit_expr, out_expr);
1208            }
1209            InlineAsmOperand::Const { anon_const } => {
1210                try_visit!(visitor.visit_anon_const(anon_const))
1211            }
1212            InlineAsmOperand::Sym { sym } => try_visit!(visitor.visit_inline_asm_sym(sym)),
1213            InlineAsmOperand::Label { block } => try_visit!(visitor.visit_block(block)),
1214        }
1215    }
1216    V::Result::output()
1217}
1218
1219pub fn walk_inline_asm_sym<'a, V: Visitor<'a>>(
1220    visitor: &mut V,
1221    InlineAsmSym { id, qself, path }: &'a InlineAsmSym,
1222) -> V::Result {
1223    try_visit!(visitor.visit_qself(qself));
1224    visitor.visit_path(path, *id)
1225}
1226
1227pub fn walk_format_args<'a, V: Visitor<'a>>(visitor: &mut V, fmt: &'a FormatArgs) -> V::Result {
1228    let FormatArgs { span: _, template: _, arguments, uncooked_fmt_str: _ } = fmt;
1229    for FormatArgument { kind, expr } in arguments.all_args() {
1230        match kind {
1231            FormatArgumentKind::Named(ident) | FormatArgumentKind::Captured(ident) => {
1232                try_visit!(visitor.visit_ident(ident))
1233            }
1234            FormatArgumentKind::Normal => {}
1235        }
1236        try_visit!(visitor.visit_expr(expr));
1237    }
1238    V::Result::output()
1239}
1240
1241pub fn walk_expr<'a, V: Visitor<'a>>(visitor: &mut V, expression: &'a Expr) -> V::Result {
1242    let Expr { id, kind, span, attrs, tokens: _ } = expression;
1243    walk_list!(visitor, visit_attribute, attrs);
1244    match kind {
1245        ExprKind::Array(subexpressions) => {
1246            walk_list!(visitor, visit_expr, subexpressions);
1247        }
1248        ExprKind::ConstBlock(anon_const) => try_visit!(visitor.visit_anon_const(anon_const)),
1249        ExprKind::Repeat(element, count) => {
1250            try_visit!(visitor.visit_expr(element));
1251            try_visit!(visitor.visit_anon_const(count));
1252        }
1253        ExprKind::Struct(se) => {
1254            let StructExpr { qself, path, fields, rest } = &**se;
1255            try_visit!(visitor.visit_qself(qself));
1256            try_visit!(visitor.visit_path(path, *id));
1257            walk_list!(visitor, visit_expr_field, fields);
1258            match rest {
1259                StructRest::Base(expr) => try_visit!(visitor.visit_expr(expr)),
1260                StructRest::Rest(_span) => {}
1261                StructRest::None => {}
1262            }
1263        }
1264        ExprKind::Tup(subexpressions) => {
1265            walk_list!(visitor, visit_expr, subexpressions);
1266        }
1267        ExprKind::Call(callee_expression, arguments) => {
1268            try_visit!(visitor.visit_expr(callee_expression));
1269            walk_list!(visitor, visit_expr, arguments);
1270        }
1271        ExprKind::MethodCall(box MethodCall { seg, receiver, args, span: _ }) => {
1272            try_visit!(visitor.visit_expr(receiver));
1273            try_visit!(visitor.visit_path_segment(seg));
1274            walk_list!(visitor, visit_expr, args);
1275        }
1276        ExprKind::Binary(_op, left_expression, right_expression) => {
1277            try_visit!(visitor.visit_expr(left_expression));
1278            try_visit!(visitor.visit_expr(right_expression));
1279        }
1280        ExprKind::AddrOf(_kind, _mutbl, subexpression) => {
1281            try_visit!(visitor.visit_expr(subexpression));
1282        }
1283        ExprKind::Unary(_op, subexpression) => {
1284            try_visit!(visitor.visit_expr(subexpression));
1285        }
1286        ExprKind::Cast(subexpression, typ) | ExprKind::Type(subexpression, typ) => {
1287            try_visit!(visitor.visit_expr(subexpression));
1288            try_visit!(visitor.visit_ty(typ));
1289        }
1290        ExprKind::Let(pat, expr, _span, _recovered) => {
1291            try_visit!(visitor.visit_pat(pat));
1292            try_visit!(visitor.visit_expr(expr));
1293        }
1294        ExprKind::If(head_expression, if_block, optional_else) => {
1295            try_visit!(visitor.visit_expr(head_expression));
1296            try_visit!(visitor.visit_block(if_block));
1297            visit_opt!(visitor, visit_expr, optional_else);
1298        }
1299        ExprKind::While(subexpression, block, opt_label) => {
1300            visit_opt!(visitor, visit_label, opt_label);
1301            try_visit!(visitor.visit_expr(subexpression));
1302            try_visit!(visitor.visit_block(block));
1303        }
1304        ExprKind::ForLoop { pat, iter, body, label, kind: _ } => {
1305            visit_opt!(visitor, visit_label, label);
1306            try_visit!(visitor.visit_pat(pat));
1307            try_visit!(visitor.visit_expr(iter));
1308            try_visit!(visitor.visit_block(body));
1309        }
1310        ExprKind::Loop(block, opt_label, _span) => {
1311            visit_opt!(visitor, visit_label, opt_label);
1312            try_visit!(visitor.visit_block(block));
1313        }
1314        ExprKind::Match(subexpression, arms, _kind) => {
1315            try_visit!(visitor.visit_expr(subexpression));
1316            walk_list!(visitor, visit_arm, arms);
1317        }
1318        ExprKind::Closure(box Closure {
1319            binder,
1320            capture_clause,
1321            coroutine_kind,
1322            constness: _,
1323            movability: _,
1324            fn_decl,
1325            body,
1326            fn_decl_span: _,
1327            fn_arg_span: _,
1328        }) => {
1329            try_visit!(visitor.visit_capture_by(capture_clause));
1330            try_visit!(visitor.visit_fn(
1331                FnKind::Closure(binder, coroutine_kind, fn_decl, body),
1332                *span,
1333                *id
1334            ));
1335        }
1336        ExprKind::Block(block, opt_label) => {
1337            visit_opt!(visitor, visit_label, opt_label);
1338            try_visit!(visitor.visit_block(block));
1339        }
1340        ExprKind::Gen(_capt, body, _kind, _decl_span) => try_visit!(visitor.visit_block(body)),
1341        ExprKind::Await(expr, _span) => try_visit!(visitor.visit_expr(expr)),
1342        ExprKind::Use(expr, _span) => try_visit!(visitor.visit_expr(expr)),
1343        ExprKind::Assign(lhs, rhs, _span) => {
1344            try_visit!(visitor.visit_expr(lhs));
1345            try_visit!(visitor.visit_expr(rhs));
1346        }
1347        ExprKind::AssignOp(_op, left_expression, right_expression) => {
1348            try_visit!(visitor.visit_expr(left_expression));
1349            try_visit!(visitor.visit_expr(right_expression));
1350        }
1351        ExprKind::Field(subexpression, ident) => {
1352            try_visit!(visitor.visit_expr(subexpression));
1353            try_visit!(visitor.visit_ident(ident));
1354        }
1355        ExprKind::Index(main_expression, index_expression, _span) => {
1356            try_visit!(visitor.visit_expr(main_expression));
1357            try_visit!(visitor.visit_expr(index_expression));
1358        }
1359        ExprKind::Range(start, end, _limit) => {
1360            visit_opt!(visitor, visit_expr, start);
1361            visit_opt!(visitor, visit_expr, end);
1362        }
1363        ExprKind::Underscore => {}
1364        ExprKind::Path(maybe_qself, path) => {
1365            try_visit!(visitor.visit_qself(maybe_qself));
1366            try_visit!(visitor.visit_path(path, *id));
1367        }
1368        ExprKind::Break(opt_label, opt_expr) => {
1369            visit_opt!(visitor, visit_label, opt_label);
1370            visit_opt!(visitor, visit_expr, opt_expr);
1371        }
1372        ExprKind::Continue(opt_label) => {
1373            visit_opt!(visitor, visit_label, opt_label);
1374        }
1375        ExprKind::Ret(optional_expression) => {
1376            visit_opt!(visitor, visit_expr, optional_expression);
1377        }
1378        ExprKind::Yeet(optional_expression) => {
1379            visit_opt!(visitor, visit_expr, optional_expression);
1380        }
1381        ExprKind::Become(expr) => try_visit!(visitor.visit_expr(expr)),
1382        ExprKind::MacCall(mac) => try_visit!(visitor.visit_mac_call(mac)),
1383        ExprKind::Paren(subexpression) => try_visit!(visitor.visit_expr(subexpression)),
1384        ExprKind::InlineAsm(asm) => try_visit!(visitor.visit_inline_asm(asm)),
1385        ExprKind::FormatArgs(f) => try_visit!(visitor.visit_format_args(f)),
1386        ExprKind::OffsetOf(container, fields) => {
1387            try_visit!(visitor.visit_ty(container));
1388            walk_list!(visitor, visit_ident, fields.iter());
1389        }
1390        ExprKind::Yield(kind) => {
1391            visit_opt!(visitor, visit_expr, kind.expr());
1392        }
1393        ExprKind::Try(subexpression) => try_visit!(visitor.visit_expr(subexpression)),
1394        ExprKind::TryBlock(body) => try_visit!(visitor.visit_block(body)),
1395        ExprKind::Lit(_token) => {}
1396        ExprKind::IncludedBytes(_bytes) => {}
1397        ExprKind::UnsafeBinderCast(_kind, expr, ty) => {
1398            try_visit!(visitor.visit_expr(expr));
1399            visit_opt!(visitor, visit_ty, ty);
1400        }
1401        ExprKind::Err(_guar) => {}
1402        ExprKind::Dummy => {}
1403    }
1404
1405    V::Result::output()
1406}
1407
1408pub fn walk_param<'a, V: Visitor<'a>>(visitor: &mut V, param: &'a Param) -> V::Result {
1409    let Param { attrs, ty, pat, id: _, span: _, is_placeholder: _ } = param;
1410    walk_list!(visitor, visit_attribute, attrs);
1411    try_visit!(visitor.visit_pat(pat));
1412    try_visit!(visitor.visit_ty(ty));
1413    V::Result::output()
1414}
1415
1416pub fn walk_arm<'a, V: Visitor<'a>>(visitor: &mut V, arm: &'a Arm) -> V::Result {
1417    let Arm { attrs, pat, guard, body, span: _, id: _, is_placeholder: _ } = arm;
1418    walk_list!(visitor, visit_attribute, attrs);
1419    try_visit!(visitor.visit_pat(pat));
1420    visit_opt!(visitor, visit_expr, guard);
1421    visit_opt!(visitor, visit_expr, body);
1422    V::Result::output()
1423}
1424
1425pub fn walk_vis<'a, V: Visitor<'a>>(visitor: &mut V, vis: &'a Visibility) -> V::Result {
1426    let Visibility { kind, span: _, tokens: _ } = vis;
1427    match kind {
1428        VisibilityKind::Restricted { path, id, shorthand: _ } => {
1429            try_visit!(visitor.visit_path(path, *id));
1430        }
1431        VisibilityKind::Public | VisibilityKind::Inherited => {}
1432    }
1433    V::Result::output()
1434}
1435
1436pub fn walk_attribute<'a, V: Visitor<'a>>(visitor: &mut V, attr: &'a Attribute) -> V::Result {
1437    let Attribute { kind, id: _, style: _, span: _ } = attr;
1438    match kind {
1439        AttrKind::Normal(normal) => {
1440            let NormalAttr { item, tokens: _ } = &**normal;
1441            let AttrItem { unsafety: _, path, args, tokens: _ } = item;
1442            try_visit!(visitor.visit_path(path, DUMMY_NODE_ID));
1443            try_visit!(walk_attr_args(visitor, args));
1444        }
1445        AttrKind::DocComment(_kind, _sym) => {}
1446    }
1447    V::Result::output()
1448}
1449
1450pub fn walk_attr_args<'a, V: Visitor<'a>>(visitor: &mut V, args: &'a AttrArgs) -> V::Result {
1451    match args {
1452        AttrArgs::Empty => {}
1453        AttrArgs::Delimited(_args) => {}
1454        AttrArgs::Eq { expr, .. } => try_visit!(visitor.visit_expr(expr)),
1455    }
1456    V::Result::output()
1457}
1458
1459fn walk_define_opaques<'a, V: Visitor<'a>>(
1460    visitor: &mut V,
1461    define_opaque: &'a Option<ThinVec<(NodeId, Path)>>,
1462) -> V::Result {
1463    if let Some(define_opaque) = define_opaque {
1464        for (id, path) in define_opaque {
1465            try_visit!(visitor.visit_path(path, *id));
1466        }
1467    }
1468    V::Result::output()
1469}