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