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