]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_ast/src/visit.rs
Auto merge of #96447 - petrochenkov:docregr, r=GuillaumeGomez
[rust.git] / compiler / rustc_ast / src / 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
16 use crate::ast::*;
17 use crate::token;
18
19 use rustc_span::symbol::{Ident, Symbol};
20 use rustc_span::Span;
21
22 #[derive(Copy, Clone, Debug, PartialEq)]
23 pub enum AssocCtxt {
24     Trait,
25     Impl,
26 }
27
28 #[derive(Copy, Clone, Debug, PartialEq)]
29 pub enum FnCtxt {
30     Free,
31     Foreign,
32     Assoc(AssocCtxt),
33 }
34
35 #[derive(Copy, Clone, Debug)]
36 pub enum BoundKind {
37     /// Trait bounds in generics bounds and type/trait alias.
38     /// E.g., `<T: Bound>`, `type A: Bound`, or `where T: Bound`.
39     Bound,
40
41     /// Trait bounds in `impl` type.
42     /// E.g., `type Foo = impl Bound1 + Bound2 + Bound3`.
43     Impl,
44
45     /// Trait bounds in trait object type.
46     /// E.g., `dyn Bound1 + Bound2 + Bound3`.
47     TraitObject,
48
49     /// Super traits of a trait.
50     /// E.g., `trait A: B`
51     SuperTraits,
52 }
53
54 #[derive(Copy, Clone, Debug)]
55 pub enum FnKind<'a> {
56     /// E.g., `fn foo()`, `fn foo(&self)`, or `extern "Abi" fn foo()`.
57     Fn(FnCtxt, Ident, &'a FnSig, &'a Visibility, &'a Generics, Option<&'a Block>),
58
59     /// E.g., `|x, y| body`.
60     Closure(&'a FnDecl, &'a Expr),
61 }
62
63 impl<'a> FnKind<'a> {
64     pub fn header(&self) -> Option<&'a FnHeader> {
65         match *self {
66             FnKind::Fn(_, _, sig, _, _, _) => Some(&sig.header),
67             FnKind::Closure(_, _) => None,
68         }
69     }
70
71     pub fn ident(&self) -> Option<&Ident> {
72         match self {
73             FnKind::Fn(_, ident, ..) => Some(ident),
74             _ => None,
75         }
76     }
77
78     pub fn decl(&self) -> &'a FnDecl {
79         match self {
80             FnKind::Fn(_, _, sig, _, _, _) => &sig.decl,
81             FnKind::Closure(decl, _) => decl,
82         }
83     }
84
85     pub fn ctxt(&self) -> Option<FnCtxt> {
86         match self {
87             FnKind::Fn(ctxt, ..) => Some(*ctxt),
88             FnKind::Closure(..) => None,
89         }
90     }
91 }
92
93 /// Each method of the `Visitor` trait is a hook to be potentially
94 /// overridden. Each method's default implementation recursively visits
95 /// the substructure of the input via the corresponding `walk` method;
96 /// e.g., the `visit_item` method by default calls `visit::walk_item`.
97 ///
98 /// If you want to ensure that your code handles every variant
99 /// explicitly, you need to override each method. (And you also need
100 /// to monitor future changes to `Visitor` in case a new method with a
101 /// new default implementation gets introduced.)
102 pub trait Visitor<'ast>: Sized {
103     fn visit_name(&mut self, _span: Span, _name: Symbol) {
104         // Nothing to do.
105     }
106     fn visit_ident(&mut self, ident: Ident) {
107         walk_ident(self, ident);
108     }
109     fn visit_foreign_item(&mut self, i: &'ast ForeignItem) {
110         walk_foreign_item(self, i)
111     }
112     fn visit_item(&mut self, i: &'ast Item) {
113         walk_item(self, i)
114     }
115     fn visit_local(&mut self, l: &'ast Local) {
116         walk_local(self, l)
117     }
118     fn visit_block(&mut self, b: &'ast Block) {
119         walk_block(self, b)
120     }
121     fn visit_stmt(&mut self, s: &'ast Stmt) {
122         walk_stmt(self, s)
123     }
124     fn visit_param(&mut self, param: &'ast Param) {
125         walk_param(self, param)
126     }
127     fn visit_arm(&mut self, a: &'ast Arm) {
128         walk_arm(self, a)
129     }
130     fn visit_pat(&mut self, p: &'ast Pat) {
131         walk_pat(self, p)
132     }
133     fn visit_anon_const(&mut self, c: &'ast AnonConst) {
134         walk_anon_const(self, c)
135     }
136     fn visit_expr(&mut self, ex: &'ast Expr) {
137         walk_expr(self, ex)
138     }
139     fn visit_expr_post(&mut self, _ex: &'ast Expr) {}
140     fn visit_ty(&mut self, t: &'ast Ty) {
141         walk_ty(self, t)
142     }
143     fn visit_generic_param(&mut self, param: &'ast GenericParam) {
144         walk_generic_param(self, param)
145     }
146     fn visit_generics(&mut self, g: &'ast Generics) {
147         walk_generics(self, g)
148     }
149     fn visit_where_predicate(&mut self, p: &'ast WherePredicate) {
150         walk_where_predicate(self, p)
151     }
152     fn visit_fn(&mut self, fk: FnKind<'ast>, s: Span, _: NodeId) {
153         walk_fn(self, fk, s)
154     }
155     fn visit_assoc_item(&mut self, i: &'ast AssocItem, ctxt: AssocCtxt) {
156         walk_assoc_item(self, i, ctxt)
157     }
158     fn visit_trait_ref(&mut self, t: &'ast TraitRef) {
159         walk_trait_ref(self, t)
160     }
161     fn visit_param_bound(&mut self, bounds: &'ast GenericBound, _ctxt: BoundKind) {
162         walk_param_bound(self, bounds)
163     }
164     fn visit_poly_trait_ref(&mut self, t: &'ast PolyTraitRef, m: &'ast TraitBoundModifier) {
165         walk_poly_trait_ref(self, t, m)
166     }
167     fn visit_variant_data(&mut self, s: &'ast VariantData) {
168         walk_struct_def(self, s)
169     }
170     fn visit_field_def(&mut self, s: &'ast FieldDef) {
171         walk_field_def(self, s)
172     }
173     fn visit_enum_def(
174         &mut self,
175         enum_definition: &'ast EnumDef,
176         generics: &'ast Generics,
177         item_id: NodeId,
178         _: Span,
179     ) {
180         walk_enum_def(self, enum_definition, generics, item_id)
181     }
182     fn visit_variant(&mut self, v: &'ast Variant) {
183         walk_variant(self, v)
184     }
185     fn visit_label(&mut self, label: &'ast Label) {
186         walk_label(self, label)
187     }
188     fn visit_lifetime(&mut self, lifetime: &'ast Lifetime) {
189         walk_lifetime(self, lifetime)
190     }
191     fn visit_mac_call(&mut self, mac: &'ast MacCall) {
192         walk_mac(self, mac)
193     }
194     fn visit_mac_def(&mut self, _mac: &'ast MacroDef, _id: NodeId) {
195         // Nothing to do
196     }
197     fn visit_path(&mut self, path: &'ast Path, _id: NodeId) {
198         walk_path(self, path)
199     }
200     fn visit_use_tree(&mut self, use_tree: &'ast UseTree, id: NodeId, _nested: bool) {
201         walk_use_tree(self, use_tree, id)
202     }
203     fn visit_path_segment(&mut self, path_span: Span, path_segment: &'ast PathSegment) {
204         walk_path_segment(self, path_span, path_segment)
205     }
206     fn visit_generic_args(&mut self, path_span: Span, generic_args: &'ast GenericArgs) {
207         walk_generic_args(self, path_span, generic_args)
208     }
209     fn visit_generic_arg(&mut self, generic_arg: &'ast GenericArg) {
210         walk_generic_arg(self, generic_arg)
211     }
212     fn visit_assoc_constraint(&mut self, constraint: &'ast AssocConstraint) {
213         walk_assoc_constraint(self, constraint)
214     }
215     fn visit_attribute(&mut self, attr: &'ast Attribute) {
216         walk_attribute(self, attr)
217     }
218     fn visit_vis(&mut self, vis: &'ast Visibility) {
219         walk_vis(self, vis)
220     }
221     fn visit_fn_ret_ty(&mut self, ret_ty: &'ast FnRetTy) {
222         walk_fn_ret_ty(self, ret_ty)
223     }
224     fn visit_fn_header(&mut self, _header: &'ast FnHeader) {
225         // Nothing to do
226     }
227     fn visit_expr_field(&mut self, f: &'ast ExprField) {
228         walk_expr_field(self, f)
229     }
230     fn visit_pat_field(&mut self, fp: &'ast PatField) {
231         walk_pat_field(self, fp)
232     }
233     fn visit_crate(&mut self, krate: &'ast Crate) {
234         walk_crate(self, krate)
235     }
236     fn visit_inline_asm(&mut self, asm: &'ast InlineAsm) {
237         walk_inline_asm(self, asm)
238     }
239     fn visit_inline_asm_sym(&mut self, sym: &'ast InlineAsmSym) {
240         walk_inline_asm_sym(self, sym)
241     }
242 }
243
244 #[macro_export]
245 macro_rules! walk_list {
246     ($visitor: expr, $method: ident, $list: expr) => {
247         for elem in $list {
248             $visitor.$method(elem)
249         }
250     };
251     ($visitor: expr, $method: ident, $list: expr, $($extra_args: expr),*) => {
252         for elem in $list {
253             $visitor.$method(elem, $($extra_args,)*)
254         }
255     }
256 }
257
258 pub fn walk_ident<'a, V: Visitor<'a>>(visitor: &mut V, ident: Ident) {
259     visitor.visit_name(ident.span, ident.name);
260 }
261
262 pub fn walk_crate<'a, V: Visitor<'a>>(visitor: &mut V, krate: &'a Crate) {
263     walk_list!(visitor, visit_item, &krate.items);
264     walk_list!(visitor, visit_attribute, &krate.attrs);
265 }
266
267 pub fn walk_local<'a, V: Visitor<'a>>(visitor: &mut V, local: &'a Local) {
268     for attr in local.attrs.iter() {
269         visitor.visit_attribute(attr);
270     }
271     visitor.visit_pat(&local.pat);
272     walk_list!(visitor, visit_ty, &local.ty);
273     if let Some((init, els)) = local.kind.init_else_opt() {
274         visitor.visit_expr(init);
275         walk_list!(visitor, visit_block, els);
276     }
277 }
278
279 pub fn walk_label<'a, V: Visitor<'a>>(visitor: &mut V, label: &'a Label) {
280     visitor.visit_ident(label.ident);
281 }
282
283 pub fn walk_lifetime<'a, V: Visitor<'a>>(visitor: &mut V, lifetime: &'a Lifetime) {
284     visitor.visit_ident(lifetime.ident);
285 }
286
287 pub fn walk_poly_trait_ref<'a, V>(
288     visitor: &mut V,
289     trait_ref: &'a PolyTraitRef,
290     _: &TraitBoundModifier,
291 ) where
292     V: Visitor<'a>,
293 {
294     walk_list!(visitor, visit_generic_param, &trait_ref.bound_generic_params);
295     visitor.visit_trait_ref(&trait_ref.trait_ref);
296 }
297
298 pub fn walk_trait_ref<'a, V: Visitor<'a>>(visitor: &mut V, trait_ref: &'a TraitRef) {
299     visitor.visit_path(&trait_ref.path, trait_ref.ref_id)
300 }
301
302 pub fn walk_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a Item) {
303     visitor.visit_vis(&item.vis);
304     visitor.visit_ident(item.ident);
305     match item.kind {
306         ItemKind::ExternCrate(orig_name) => {
307             if let Some(orig_name) = orig_name {
308                 visitor.visit_name(item.span, orig_name);
309             }
310         }
311         ItemKind::Use(ref use_tree) => visitor.visit_use_tree(use_tree, item.id, false),
312         ItemKind::Static(ref typ, _, ref expr) | ItemKind::Const(_, ref typ, ref expr) => {
313             visitor.visit_ty(typ);
314             walk_list!(visitor, visit_expr, expr);
315         }
316         ItemKind::Fn(box Fn { defaultness: _, ref generics, ref sig, ref body }) => {
317             let kind =
318                 FnKind::Fn(FnCtxt::Free, item.ident, sig, &item.vis, generics, body.as_deref());
319             visitor.visit_fn(kind, item.span, item.id)
320         }
321         ItemKind::Mod(_unsafety, ref mod_kind) => match mod_kind {
322             ModKind::Loaded(items, _inline, _inner_span) => {
323                 walk_list!(visitor, visit_item, items)
324             }
325             ModKind::Unloaded => {}
326         },
327         ItemKind::ForeignMod(ref foreign_module) => {
328             walk_list!(visitor, visit_foreign_item, &foreign_module.items);
329         }
330         ItemKind::GlobalAsm(ref asm) => walk_inline_asm(visitor, asm),
331         ItemKind::TyAlias(box TyAlias { ref generics, ref bounds, ref ty, .. }) => {
332             visitor.visit_generics(generics);
333             walk_list!(visitor, visit_param_bound, bounds, BoundKind::Bound);
334             walk_list!(visitor, visit_ty, ty);
335         }
336         ItemKind::Enum(ref enum_definition, ref generics) => {
337             visitor.visit_generics(generics);
338             visitor.visit_enum_def(enum_definition, generics, item.id, item.span)
339         }
340         ItemKind::Impl(box Impl {
341             defaultness: _,
342             unsafety: _,
343             ref generics,
344             constness: _,
345             polarity: _,
346             ref of_trait,
347             ref self_ty,
348             ref items,
349         }) => {
350             visitor.visit_generics(generics);
351             walk_list!(visitor, visit_trait_ref, of_trait);
352             visitor.visit_ty(self_ty);
353             walk_list!(visitor, visit_assoc_item, items, AssocCtxt::Impl);
354         }
355         ItemKind::Struct(ref struct_definition, ref generics)
356         | ItemKind::Union(ref struct_definition, ref generics) => {
357             visitor.visit_generics(generics);
358             visitor.visit_variant_data(struct_definition);
359         }
360         ItemKind::Trait(box Trait {
361             unsafety: _,
362             is_auto: _,
363             ref generics,
364             ref bounds,
365             ref items,
366         }) => {
367             visitor.visit_generics(generics);
368             walk_list!(visitor, visit_param_bound, bounds, BoundKind::SuperTraits);
369             walk_list!(visitor, visit_assoc_item, items, AssocCtxt::Trait);
370         }
371         ItemKind::TraitAlias(ref generics, ref bounds) => {
372             visitor.visit_generics(generics);
373             walk_list!(visitor, visit_param_bound, bounds, BoundKind::Bound);
374         }
375         ItemKind::MacCall(ref mac) => visitor.visit_mac_call(mac),
376         ItemKind::MacroDef(ref ts) => visitor.visit_mac_def(ts, item.id),
377     }
378     walk_list!(visitor, visit_attribute, &item.attrs);
379 }
380
381 pub fn walk_enum_def<'a, V: Visitor<'a>>(
382     visitor: &mut V,
383     enum_definition: &'a EnumDef,
384     _: &'a Generics,
385     _: NodeId,
386 ) {
387     walk_list!(visitor, visit_variant, &enum_definition.variants);
388 }
389
390 pub fn walk_variant<'a, V: Visitor<'a>>(visitor: &mut V, variant: &'a Variant)
391 where
392     V: Visitor<'a>,
393 {
394     visitor.visit_ident(variant.ident);
395     visitor.visit_vis(&variant.vis);
396     visitor.visit_variant_data(&variant.data);
397     walk_list!(visitor, visit_anon_const, &variant.disr_expr);
398     walk_list!(visitor, visit_attribute, &variant.attrs);
399 }
400
401 pub fn walk_expr_field<'a, V: Visitor<'a>>(visitor: &mut V, f: &'a ExprField) {
402     visitor.visit_expr(&f.expr);
403     visitor.visit_ident(f.ident);
404     walk_list!(visitor, visit_attribute, f.attrs.iter());
405 }
406
407 pub fn walk_pat_field<'a, V: Visitor<'a>>(visitor: &mut V, fp: &'a PatField) {
408     visitor.visit_ident(fp.ident);
409     visitor.visit_pat(&fp.pat);
410     walk_list!(visitor, visit_attribute, fp.attrs.iter());
411 }
412
413 pub fn walk_ty<'a, V: Visitor<'a>>(visitor: &mut V, typ: &'a Ty) {
414     match typ.kind {
415         TyKind::Slice(ref ty) | TyKind::Paren(ref ty) => visitor.visit_ty(ty),
416         TyKind::Ptr(ref mutable_type) => visitor.visit_ty(&mutable_type.ty),
417         TyKind::Rptr(ref opt_lifetime, ref mutable_type) => {
418             walk_list!(visitor, visit_lifetime, opt_lifetime);
419             visitor.visit_ty(&mutable_type.ty)
420         }
421         TyKind::Tup(ref tuple_element_types) => {
422             walk_list!(visitor, visit_ty, tuple_element_types);
423         }
424         TyKind::BareFn(ref function_declaration) => {
425             walk_list!(visitor, visit_generic_param, &function_declaration.generic_params);
426             walk_fn_decl(visitor, &function_declaration.decl);
427         }
428         TyKind::Path(ref maybe_qself, ref path) => {
429             if let Some(ref qself) = *maybe_qself {
430                 visitor.visit_ty(&qself.ty);
431             }
432             visitor.visit_path(path, typ.id);
433         }
434         TyKind::Array(ref ty, ref length) => {
435             visitor.visit_ty(ty);
436             visitor.visit_anon_const(length)
437         }
438         TyKind::TraitObject(ref bounds, ..) => {
439             walk_list!(visitor, visit_param_bound, bounds, BoundKind::TraitObject);
440         }
441         TyKind::ImplTrait(_, ref bounds) => {
442             walk_list!(visitor, visit_param_bound, bounds, BoundKind::Impl);
443         }
444         TyKind::Typeof(ref expression) => visitor.visit_anon_const(expression),
445         TyKind::Infer | TyKind::ImplicitSelf | TyKind::Err => {}
446         TyKind::MacCall(ref mac) => visitor.visit_mac_call(mac),
447         TyKind::Never | TyKind::CVarArgs => {}
448     }
449 }
450
451 pub fn walk_path<'a, V: Visitor<'a>>(visitor: &mut V, path: &'a Path) {
452     for segment in &path.segments {
453         visitor.visit_path_segment(path.span, segment);
454     }
455 }
456
457 pub fn walk_use_tree<'a, V: Visitor<'a>>(visitor: &mut V, use_tree: &'a UseTree, id: NodeId) {
458     visitor.visit_path(&use_tree.prefix, id);
459     match use_tree.kind {
460         UseTreeKind::Simple(rename, ..) => {
461             // The extra IDs are handled during HIR lowering.
462             if let Some(rename) = rename {
463                 visitor.visit_ident(rename);
464             }
465         }
466         UseTreeKind::Glob => {}
467         UseTreeKind::Nested(ref use_trees) => {
468             for &(ref nested_tree, nested_id) in use_trees {
469                 visitor.visit_use_tree(nested_tree, nested_id, true);
470             }
471         }
472     }
473 }
474
475 pub fn walk_path_segment<'a, V: Visitor<'a>>(
476     visitor: &mut V,
477     path_span: Span,
478     segment: &'a PathSegment,
479 ) {
480     visitor.visit_ident(segment.ident);
481     if let Some(ref args) = segment.args {
482         visitor.visit_generic_args(path_span, args);
483     }
484 }
485
486 pub fn walk_generic_args<'a, V>(visitor: &mut V, _path_span: Span, generic_args: &'a GenericArgs)
487 where
488     V: Visitor<'a>,
489 {
490     match *generic_args {
491         GenericArgs::AngleBracketed(ref data) => {
492             for arg in &data.args {
493                 match arg {
494                     AngleBracketedArg::Arg(a) => visitor.visit_generic_arg(a),
495                     AngleBracketedArg::Constraint(c) => visitor.visit_assoc_constraint(c),
496                 }
497             }
498         }
499         GenericArgs::Parenthesized(ref data) => {
500             walk_list!(visitor, visit_ty, &data.inputs);
501             walk_fn_ret_ty(visitor, &data.output);
502         }
503     }
504 }
505
506 pub fn walk_generic_arg<'a, V>(visitor: &mut V, generic_arg: &'a GenericArg)
507 where
508     V: Visitor<'a>,
509 {
510     match generic_arg {
511         GenericArg::Lifetime(lt) => visitor.visit_lifetime(lt),
512         GenericArg::Type(ty) => visitor.visit_ty(ty),
513         GenericArg::Const(ct) => visitor.visit_anon_const(ct),
514     }
515 }
516
517 pub fn walk_assoc_constraint<'a, V: Visitor<'a>>(visitor: &mut V, constraint: &'a AssocConstraint) {
518     visitor.visit_ident(constraint.ident);
519     if let Some(ref gen_args) = constraint.gen_args {
520         visitor.visit_generic_args(gen_args.span(), gen_args);
521     }
522     match constraint.kind {
523         AssocConstraintKind::Equality { ref term } => match term {
524             Term::Ty(ty) => visitor.visit_ty(ty),
525             Term::Const(c) => visitor.visit_anon_const(c),
526         },
527         AssocConstraintKind::Bound { ref bounds } => {
528             walk_list!(visitor, visit_param_bound, bounds, BoundKind::Bound);
529         }
530     }
531 }
532
533 pub fn walk_pat<'a, V: Visitor<'a>>(visitor: &mut V, pattern: &'a Pat) {
534     match pattern.kind {
535         PatKind::TupleStruct(ref opt_qself, ref path, ref elems) => {
536             if let Some(ref qself) = *opt_qself {
537                 visitor.visit_ty(&qself.ty);
538             }
539             visitor.visit_path(path, pattern.id);
540             walk_list!(visitor, visit_pat, elems);
541         }
542         PatKind::Path(ref opt_qself, ref path) => {
543             if let Some(ref qself) = *opt_qself {
544                 visitor.visit_ty(&qself.ty);
545             }
546             visitor.visit_path(path, pattern.id)
547         }
548         PatKind::Struct(ref opt_qself, ref path, ref fields, _) => {
549             if let Some(ref qself) = *opt_qself {
550                 visitor.visit_ty(&qself.ty);
551             }
552             visitor.visit_path(path, pattern.id);
553             walk_list!(visitor, visit_pat_field, fields);
554         }
555         PatKind::Box(ref subpattern)
556         | PatKind::Ref(ref subpattern, _)
557         | PatKind::Paren(ref subpattern) => visitor.visit_pat(subpattern),
558         PatKind::Ident(_, ident, ref optional_subpattern) => {
559             visitor.visit_ident(ident);
560             walk_list!(visitor, visit_pat, optional_subpattern);
561         }
562         PatKind::Lit(ref expression) => visitor.visit_expr(expression),
563         PatKind::Range(ref lower_bound, ref upper_bound, _) => {
564             walk_list!(visitor, visit_expr, lower_bound);
565             walk_list!(visitor, visit_expr, upper_bound);
566         }
567         PatKind::Wild | PatKind::Rest => {}
568         PatKind::Tuple(ref elems) | PatKind::Slice(ref elems) | PatKind::Or(ref elems) => {
569             walk_list!(visitor, visit_pat, elems);
570         }
571         PatKind::MacCall(ref mac) => visitor.visit_mac_call(mac),
572     }
573 }
574
575 pub fn walk_foreign_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a ForeignItem) {
576     let Item { id, span, ident, ref vis, ref attrs, ref kind, tokens: _ } = *item;
577     visitor.visit_vis(vis);
578     visitor.visit_ident(ident);
579     walk_list!(visitor, visit_attribute, attrs);
580     match kind {
581         ForeignItemKind::Static(ty, _, expr) => {
582             visitor.visit_ty(ty);
583             walk_list!(visitor, visit_expr, expr);
584         }
585         ForeignItemKind::Fn(box Fn { defaultness: _, ref generics, ref sig, ref body }) => {
586             let kind = FnKind::Fn(FnCtxt::Foreign, ident, sig, vis, generics, body.as_deref());
587             visitor.visit_fn(kind, span, id);
588         }
589         ForeignItemKind::TyAlias(box TyAlias { generics, bounds, ty, .. }) => {
590             visitor.visit_generics(generics);
591             walk_list!(visitor, visit_param_bound, bounds, BoundKind::Bound);
592             walk_list!(visitor, visit_ty, ty);
593         }
594         ForeignItemKind::MacCall(mac) => {
595             visitor.visit_mac_call(mac);
596         }
597     }
598 }
599
600 pub fn walk_param_bound<'a, V: Visitor<'a>>(visitor: &mut V, bound: &'a GenericBound) {
601     match *bound {
602         GenericBound::Trait(ref typ, ref modifier) => visitor.visit_poly_trait_ref(typ, modifier),
603         GenericBound::Outlives(ref lifetime) => visitor.visit_lifetime(lifetime),
604     }
605 }
606
607 pub fn walk_generic_param<'a, V: Visitor<'a>>(visitor: &mut V, param: &'a GenericParam) {
608     visitor.visit_ident(param.ident);
609     walk_list!(visitor, visit_attribute, param.attrs.iter());
610     walk_list!(visitor, visit_param_bound, &param.bounds, BoundKind::Bound);
611     match param.kind {
612         GenericParamKind::Lifetime => (),
613         GenericParamKind::Type { ref default } => walk_list!(visitor, visit_ty, default),
614         GenericParamKind::Const { ref ty, ref default, .. } => {
615             visitor.visit_ty(ty);
616             if let Some(default) = default {
617                 visitor.visit_anon_const(default);
618             }
619         }
620     }
621 }
622
623 pub fn walk_generics<'a, V: Visitor<'a>>(visitor: &mut V, generics: &'a Generics) {
624     walk_list!(visitor, visit_generic_param, &generics.params);
625     walk_list!(visitor, visit_where_predicate, &generics.where_clause.predicates);
626 }
627
628 pub fn walk_where_predicate<'a, V: Visitor<'a>>(visitor: &mut V, predicate: &'a WherePredicate) {
629     match *predicate {
630         WherePredicate::BoundPredicate(WhereBoundPredicate {
631             ref bounded_ty,
632             ref bounds,
633             ref bound_generic_params,
634             ..
635         }) => {
636             visitor.visit_ty(bounded_ty);
637             walk_list!(visitor, visit_param_bound, bounds, BoundKind::Bound);
638             walk_list!(visitor, visit_generic_param, bound_generic_params);
639         }
640         WherePredicate::RegionPredicate(WhereRegionPredicate {
641             ref lifetime, ref bounds, ..
642         }) => {
643             visitor.visit_lifetime(lifetime);
644             walk_list!(visitor, visit_param_bound, bounds, BoundKind::Bound);
645         }
646         WherePredicate::EqPredicate(WhereEqPredicate { ref lhs_ty, ref rhs_ty, .. }) => {
647             visitor.visit_ty(lhs_ty);
648             visitor.visit_ty(rhs_ty);
649         }
650     }
651 }
652
653 pub fn walk_fn_ret_ty<'a, V: Visitor<'a>>(visitor: &mut V, ret_ty: &'a FnRetTy) {
654     if let FnRetTy::Ty(ref output_ty) = *ret_ty {
655         visitor.visit_ty(output_ty)
656     }
657 }
658
659 pub fn walk_fn_decl<'a, V: Visitor<'a>>(visitor: &mut V, function_declaration: &'a FnDecl) {
660     for param in &function_declaration.inputs {
661         visitor.visit_param(param);
662     }
663     visitor.visit_fn_ret_ty(&function_declaration.output);
664 }
665
666 pub fn walk_fn<'a, V: Visitor<'a>>(visitor: &mut V, kind: FnKind<'a>, _span: Span) {
667     match kind {
668         FnKind::Fn(_, _, sig, _, generics, body) => {
669             visitor.visit_generics(generics);
670             visitor.visit_fn_header(&sig.header);
671             walk_fn_decl(visitor, &sig.decl);
672             walk_list!(visitor, visit_block, body);
673         }
674         FnKind::Closure(decl, body) => {
675             walk_fn_decl(visitor, decl);
676             visitor.visit_expr(body);
677         }
678     }
679 }
680
681 pub fn walk_assoc_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a AssocItem, ctxt: AssocCtxt) {
682     let Item { id, span, ident, ref vis, ref attrs, ref kind, tokens: _ } = *item;
683     visitor.visit_vis(vis);
684     visitor.visit_ident(ident);
685     walk_list!(visitor, visit_attribute, attrs);
686     match kind {
687         AssocItemKind::Const(_, ty, expr) => {
688             visitor.visit_ty(ty);
689             walk_list!(visitor, visit_expr, expr);
690         }
691         AssocItemKind::Fn(box Fn { defaultness: _, ref generics, ref sig, ref body }) => {
692             let kind = FnKind::Fn(FnCtxt::Assoc(ctxt), ident, sig, vis, generics, body.as_deref());
693             visitor.visit_fn(kind, span, id);
694         }
695         AssocItemKind::TyAlias(box TyAlias { generics, bounds, ty, .. }) => {
696             visitor.visit_generics(generics);
697             walk_list!(visitor, visit_param_bound, bounds, BoundKind::Bound);
698             walk_list!(visitor, visit_ty, ty);
699         }
700         AssocItemKind::MacCall(mac) => {
701             visitor.visit_mac_call(mac);
702         }
703     }
704 }
705
706 pub fn walk_struct_def<'a, V: Visitor<'a>>(visitor: &mut V, struct_definition: &'a VariantData) {
707     walk_list!(visitor, visit_field_def, struct_definition.fields());
708 }
709
710 pub fn walk_field_def<'a, V: Visitor<'a>>(visitor: &mut V, field: &'a FieldDef) {
711     visitor.visit_vis(&field.vis);
712     if let Some(ident) = field.ident {
713         visitor.visit_ident(ident);
714     }
715     visitor.visit_ty(&field.ty);
716     walk_list!(visitor, visit_attribute, &field.attrs);
717 }
718
719 pub fn walk_block<'a, V: Visitor<'a>>(visitor: &mut V, block: &'a Block) {
720     walk_list!(visitor, visit_stmt, &block.stmts);
721 }
722
723 pub fn walk_stmt<'a, V: Visitor<'a>>(visitor: &mut V, statement: &'a Stmt) {
724     match statement.kind {
725         StmtKind::Local(ref local) => visitor.visit_local(local),
726         StmtKind::Item(ref item) => visitor.visit_item(item),
727         StmtKind::Expr(ref expr) | StmtKind::Semi(ref expr) => visitor.visit_expr(expr),
728         StmtKind::Empty => {}
729         StmtKind::MacCall(ref mac) => {
730             let MacCallStmt { ref mac, style: _, ref attrs, tokens: _ } = **mac;
731             visitor.visit_mac_call(mac);
732             for attr in attrs.iter() {
733                 visitor.visit_attribute(attr);
734             }
735         }
736     }
737 }
738
739 pub fn walk_mac<'a, V: Visitor<'a>>(visitor: &mut V, mac: &'a MacCall) {
740     visitor.visit_path(&mac.path, DUMMY_NODE_ID);
741 }
742
743 pub fn walk_anon_const<'a, V: Visitor<'a>>(visitor: &mut V, constant: &'a AnonConst) {
744     visitor.visit_expr(&constant.value);
745 }
746
747 pub fn walk_inline_asm<'a, V: Visitor<'a>>(visitor: &mut V, asm: &'a InlineAsm) {
748     for (op, _) in &asm.operands {
749         match op {
750             InlineAsmOperand::In { expr, .. }
751             | InlineAsmOperand::Out { expr: Some(expr), .. }
752             | InlineAsmOperand::InOut { expr, .. } => visitor.visit_expr(expr),
753             InlineAsmOperand::Out { expr: None, .. } => {}
754             InlineAsmOperand::SplitInOut { in_expr, out_expr, .. } => {
755                 visitor.visit_expr(in_expr);
756                 if let Some(out_expr) = out_expr {
757                     visitor.visit_expr(out_expr);
758                 }
759             }
760             InlineAsmOperand::Const { anon_const, .. } => visitor.visit_anon_const(anon_const),
761             InlineAsmOperand::Sym { sym } => visitor.visit_inline_asm_sym(sym),
762         }
763     }
764 }
765
766 pub fn walk_inline_asm_sym<'a, V: Visitor<'a>>(visitor: &mut V, sym: &'a InlineAsmSym) {
767     if let Some(ref qself) = sym.qself {
768         visitor.visit_ty(&qself.ty);
769     }
770     visitor.visit_path(&sym.path, sym.id);
771 }
772
773 pub fn walk_expr<'a, V: Visitor<'a>>(visitor: &mut V, expression: &'a Expr) {
774     walk_list!(visitor, visit_attribute, expression.attrs.iter());
775
776     match expression.kind {
777         ExprKind::Box(ref subexpression) => visitor.visit_expr(subexpression),
778         ExprKind::Array(ref subexpressions) => {
779             walk_list!(visitor, visit_expr, subexpressions);
780         }
781         ExprKind::ConstBlock(ref anon_const) => visitor.visit_anon_const(anon_const),
782         ExprKind::Repeat(ref element, ref count) => {
783             visitor.visit_expr(element);
784             visitor.visit_anon_const(count)
785         }
786         ExprKind::Struct(ref se) => {
787             if let Some(ref qself) = se.qself {
788                 visitor.visit_ty(&qself.ty);
789             }
790             visitor.visit_path(&se.path, expression.id);
791             walk_list!(visitor, visit_expr_field, &se.fields);
792             match &se.rest {
793                 StructRest::Base(expr) => visitor.visit_expr(expr),
794                 StructRest::Rest(_span) => {}
795                 StructRest::None => {}
796             }
797         }
798         ExprKind::Tup(ref subexpressions) => {
799             walk_list!(visitor, visit_expr, subexpressions);
800         }
801         ExprKind::Call(ref callee_expression, ref arguments) => {
802             visitor.visit_expr(callee_expression);
803             walk_list!(visitor, visit_expr, arguments);
804         }
805         ExprKind::MethodCall(ref segment, ref arguments, _span) => {
806             visitor.visit_path_segment(expression.span, segment);
807             walk_list!(visitor, visit_expr, arguments);
808         }
809         ExprKind::Binary(_, ref left_expression, ref right_expression) => {
810             visitor.visit_expr(left_expression);
811             visitor.visit_expr(right_expression)
812         }
813         ExprKind::AddrOf(_, _, ref subexpression) | ExprKind::Unary(_, ref subexpression) => {
814             visitor.visit_expr(subexpression)
815         }
816         ExprKind::Cast(ref subexpression, ref typ) | ExprKind::Type(ref subexpression, ref typ) => {
817             visitor.visit_expr(subexpression);
818             visitor.visit_ty(typ)
819         }
820         ExprKind::Let(ref pat, ref expr, _) => {
821             visitor.visit_pat(pat);
822             visitor.visit_expr(expr);
823         }
824         ExprKind::If(ref head_expression, ref if_block, ref optional_else) => {
825             visitor.visit_expr(head_expression);
826             visitor.visit_block(if_block);
827             walk_list!(visitor, visit_expr, optional_else);
828         }
829         ExprKind::While(ref subexpression, ref block, ref opt_label) => {
830             walk_list!(visitor, visit_label, opt_label);
831             visitor.visit_expr(subexpression);
832             visitor.visit_block(block);
833         }
834         ExprKind::ForLoop(ref pattern, ref subexpression, ref block, ref opt_label) => {
835             walk_list!(visitor, visit_label, opt_label);
836             visitor.visit_pat(pattern);
837             visitor.visit_expr(subexpression);
838             visitor.visit_block(block);
839         }
840         ExprKind::Loop(ref block, ref opt_label) => {
841             walk_list!(visitor, visit_label, opt_label);
842             visitor.visit_block(block);
843         }
844         ExprKind::Match(ref subexpression, ref arms) => {
845             visitor.visit_expr(subexpression);
846             walk_list!(visitor, visit_arm, arms);
847         }
848         ExprKind::Closure(_, _, _, ref decl, ref body, _decl_span) => {
849             visitor.visit_fn(FnKind::Closure(decl, body), expression.span, expression.id)
850         }
851         ExprKind::Block(ref block, ref opt_label) => {
852             walk_list!(visitor, visit_label, opt_label);
853             visitor.visit_block(block);
854         }
855         ExprKind::Async(_, _, ref body) => {
856             visitor.visit_block(body);
857         }
858         ExprKind::Await(ref expr) => visitor.visit_expr(expr),
859         ExprKind::Assign(ref lhs, ref rhs, _) => {
860             visitor.visit_expr(lhs);
861             visitor.visit_expr(rhs);
862         }
863         ExprKind::AssignOp(_, ref left_expression, ref right_expression) => {
864             visitor.visit_expr(left_expression);
865             visitor.visit_expr(right_expression);
866         }
867         ExprKind::Field(ref subexpression, ident) => {
868             visitor.visit_expr(subexpression);
869             visitor.visit_ident(ident);
870         }
871         ExprKind::Index(ref main_expression, ref index_expression) => {
872             visitor.visit_expr(main_expression);
873             visitor.visit_expr(index_expression)
874         }
875         ExprKind::Range(ref start, ref end, _) => {
876             walk_list!(visitor, visit_expr, start);
877             walk_list!(visitor, visit_expr, end);
878         }
879         ExprKind::Underscore => {}
880         ExprKind::Path(ref maybe_qself, ref path) => {
881             if let Some(ref qself) = *maybe_qself {
882                 visitor.visit_ty(&qself.ty);
883             }
884             visitor.visit_path(path, expression.id)
885         }
886         ExprKind::Break(ref opt_label, ref opt_expr) => {
887             walk_list!(visitor, visit_label, opt_label);
888             walk_list!(visitor, visit_expr, opt_expr);
889         }
890         ExprKind::Continue(ref opt_label) => {
891             walk_list!(visitor, visit_label, opt_label);
892         }
893         ExprKind::Ret(ref optional_expression) => {
894             walk_list!(visitor, visit_expr, optional_expression);
895         }
896         ExprKind::Yeet(ref optional_expression) => {
897             walk_list!(visitor, visit_expr, optional_expression);
898         }
899         ExprKind::MacCall(ref mac) => visitor.visit_mac_call(mac),
900         ExprKind::Paren(ref subexpression) => visitor.visit_expr(subexpression),
901         ExprKind::InlineAsm(ref asm) => walk_inline_asm(visitor, asm),
902         ExprKind::Yield(ref optional_expression) => {
903             walk_list!(visitor, visit_expr, optional_expression);
904         }
905         ExprKind::Try(ref subexpression) => visitor.visit_expr(subexpression),
906         ExprKind::TryBlock(ref body) => visitor.visit_block(body),
907         ExprKind::Lit(_) | ExprKind::Err => {}
908     }
909
910     visitor.visit_expr_post(expression)
911 }
912
913 pub fn walk_param<'a, V: Visitor<'a>>(visitor: &mut V, param: &'a Param) {
914     walk_list!(visitor, visit_attribute, param.attrs.iter());
915     visitor.visit_pat(&param.pat);
916     visitor.visit_ty(&param.ty);
917 }
918
919 pub fn walk_arm<'a, V: Visitor<'a>>(visitor: &mut V, arm: &'a Arm) {
920     visitor.visit_pat(&arm.pat);
921     walk_list!(visitor, visit_expr, &arm.guard);
922     visitor.visit_expr(&arm.body);
923     walk_list!(visitor, visit_attribute, &arm.attrs);
924 }
925
926 pub fn walk_vis<'a, V: Visitor<'a>>(visitor: &mut V, vis: &'a Visibility) {
927     if let VisibilityKind::Restricted { ref path, id } = vis.kind {
928         visitor.visit_path(path, id);
929     }
930 }
931
932 pub fn walk_attribute<'a, V: Visitor<'a>>(visitor: &mut V, attr: &'a Attribute) {
933     match attr.kind {
934         AttrKind::Normal(ref item, ref _tokens) => walk_mac_args(visitor, &item.args),
935         AttrKind::DocComment(..) => {}
936     }
937 }
938
939 pub fn walk_mac_args<'a, V: Visitor<'a>>(visitor: &mut V, args: &'a MacArgs) {
940     match args {
941         MacArgs::Empty => {}
942         MacArgs::Delimited(_dspan, _delim, _tokens) => {}
943         // The value in `#[key = VALUE]` must be visited as an expression for backward
944         // compatibility, so that macros can be expanded in that position.
945         MacArgs::Eq(_eq_span, token) => match &token.kind {
946             token::Interpolated(nt) => match &**nt {
947                 token::NtExpr(expr) => visitor.visit_expr(expr),
948                 t => panic!("unexpected token in key-value attribute: {:?}", t),
949             },
950             t => panic!("unexpected token in key-value attribute: {:?}", t),
951         },
952     }
953 }