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