]> git.lizzy.rs Git - rust.git/blob - src/libsyntax/visit.rs
Auto merge of #69332 - nnethercote:revert-u8to64_le-changes, r=michaelwoerister
[rust.git] / src / libsyntax / 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 typ, ref generics) => {
316             visitor.visit_ty(typ);
317             visitor.visit_generics(generics)
318         }
319         ItemKind::Enum(ref enum_definition, ref generics) => {
320             visitor.visit_generics(generics);
321             visitor.visit_enum_def(enum_definition, generics, item.id, item.span)
322         }
323         ItemKind::Impl {
324             unsafety: _,
325             polarity: _,
326             defaultness: _,
327             constness: _,
328             ref generics,
329             ref of_trait,
330             ref self_ty,
331             ref items,
332         } => {
333             visitor.visit_generics(generics);
334             walk_list!(visitor, visit_trait_ref, of_trait);
335             visitor.visit_ty(self_ty);
336             walk_list!(visitor, visit_assoc_item, items, AssocCtxt::Impl);
337         }
338         ItemKind::Struct(ref struct_definition, ref generics)
339         | ItemKind::Union(ref struct_definition, ref generics) => {
340             visitor.visit_generics(generics);
341             visitor.visit_variant_data(struct_definition);
342         }
343         ItemKind::Trait(.., ref generics, ref bounds, ref items) => {
344             visitor.visit_generics(generics);
345             walk_list!(visitor, visit_param_bound, bounds);
346             walk_list!(visitor, visit_assoc_item, items, AssocCtxt::Trait);
347         }
348         ItemKind::TraitAlias(ref generics, ref bounds) => {
349             visitor.visit_generics(generics);
350             walk_list!(visitor, visit_param_bound, bounds);
351         }
352         ItemKind::Mac(ref mac) => visitor.visit_mac(mac),
353         ItemKind::MacroDef(ref ts) => visitor.visit_mac_def(ts, item.id),
354     }
355     walk_list!(visitor, visit_attribute, &item.attrs);
356 }
357
358 pub fn walk_enum_def<'a, V: Visitor<'a>>(
359     visitor: &mut V,
360     enum_definition: &'a EnumDef,
361     _: &'a Generics,
362     _: NodeId,
363 ) {
364     walk_list!(visitor, visit_variant, &enum_definition.variants);
365 }
366
367 pub fn walk_variant<'a, V: Visitor<'a>>(visitor: &mut V, variant: &'a Variant)
368 where
369     V: Visitor<'a>,
370 {
371     visitor.visit_ident(variant.ident);
372     visitor.visit_vis(&variant.vis);
373     visitor.visit_variant_data(&variant.data);
374     walk_list!(visitor, visit_anon_const, &variant.disr_expr);
375     walk_list!(visitor, visit_attribute, &variant.attrs);
376 }
377
378 pub fn walk_field<'a, V: Visitor<'a>>(visitor: &mut V, f: &'a Field) {
379     visitor.visit_expr(&f.expr);
380     visitor.visit_ident(f.ident);
381     walk_list!(visitor, visit_attribute, f.attrs.iter());
382 }
383
384 pub fn walk_field_pattern<'a, V: Visitor<'a>>(visitor: &mut V, fp: &'a FieldPat) {
385     visitor.visit_ident(fp.ident);
386     visitor.visit_pat(&fp.pat);
387     walk_list!(visitor, visit_attribute, fp.attrs.iter());
388 }
389
390 pub fn walk_ty<'a, V: Visitor<'a>>(visitor: &mut V, typ: &'a Ty) {
391     match typ.kind {
392         TyKind::Slice(ref ty) | TyKind::Paren(ref ty) => visitor.visit_ty(ty),
393         TyKind::Ptr(ref mutable_type) => visitor.visit_ty(&mutable_type.ty),
394         TyKind::Rptr(ref opt_lifetime, ref mutable_type) => {
395             walk_list!(visitor, visit_lifetime, opt_lifetime);
396             visitor.visit_ty(&mutable_type.ty)
397         }
398         TyKind::Tup(ref tuple_element_types) => {
399             walk_list!(visitor, visit_ty, tuple_element_types);
400         }
401         TyKind::BareFn(ref function_declaration) => {
402             walk_list!(visitor, visit_generic_param, &function_declaration.generic_params);
403             walk_fn_decl(visitor, &function_declaration.decl);
404         }
405         TyKind::Path(ref maybe_qself, ref path) => {
406             if let Some(ref qself) = *maybe_qself {
407                 visitor.visit_ty(&qself.ty);
408             }
409             visitor.visit_path(path, typ.id);
410         }
411         TyKind::Array(ref ty, ref length) => {
412             visitor.visit_ty(ty);
413             visitor.visit_anon_const(length)
414         }
415         TyKind::TraitObject(ref bounds, ..) | TyKind::ImplTrait(_, ref bounds) => {
416             walk_list!(visitor, visit_param_bound, bounds);
417         }
418         TyKind::Typeof(ref expression) => visitor.visit_anon_const(expression),
419         TyKind::Infer | TyKind::ImplicitSelf | TyKind::Err => {}
420         TyKind::Mac(ref mac) => visitor.visit_mac(mac),
421         TyKind::Never | TyKind::CVarArgs => {}
422     }
423 }
424
425 pub fn walk_path<'a, V: Visitor<'a>>(visitor: &mut V, path: &'a Path) {
426     for segment in &path.segments {
427         visitor.visit_path_segment(path.span, segment);
428     }
429 }
430
431 pub fn walk_use_tree<'a, V: Visitor<'a>>(visitor: &mut V, use_tree: &'a UseTree, id: NodeId) {
432     visitor.visit_path(&use_tree.prefix, id);
433     match use_tree.kind {
434         UseTreeKind::Simple(rename, ..) => {
435             // The extra IDs are handled during HIR lowering.
436             if let Some(rename) = rename {
437                 visitor.visit_ident(rename);
438             }
439         }
440         UseTreeKind::Glob => {}
441         UseTreeKind::Nested(ref use_trees) => {
442             for &(ref nested_tree, nested_id) in use_trees {
443                 visitor.visit_use_tree(nested_tree, nested_id, true);
444             }
445         }
446     }
447 }
448
449 pub fn walk_path_segment<'a, V: Visitor<'a>>(
450     visitor: &mut V,
451     path_span: Span,
452     segment: &'a PathSegment,
453 ) {
454     visitor.visit_ident(segment.ident);
455     if let Some(ref args) = segment.args {
456         visitor.visit_generic_args(path_span, args);
457     }
458 }
459
460 pub fn walk_generic_args<'a, V>(visitor: &mut V, _path_span: Span, generic_args: &'a GenericArgs)
461 where
462     V: Visitor<'a>,
463 {
464     match *generic_args {
465         GenericArgs::AngleBracketed(ref data) => {
466             walk_list!(visitor, visit_generic_arg, &data.args);
467             walk_list!(visitor, visit_assoc_ty_constraint, &data.constraints);
468         }
469         GenericArgs::Parenthesized(ref data) => {
470             walk_list!(visitor, visit_ty, &data.inputs);
471             walk_fn_ret_ty(visitor, &data.output);
472         }
473     }
474 }
475
476 pub fn walk_assoc_ty_constraint<'a, V: Visitor<'a>>(
477     visitor: &mut V,
478     constraint: &'a AssocTyConstraint,
479 ) {
480     visitor.visit_ident(constraint.ident);
481     match constraint.kind {
482         AssocTyConstraintKind::Equality { ref ty } => {
483             visitor.visit_ty(ty);
484         }
485         AssocTyConstraintKind::Bound { ref bounds } => {
486             walk_list!(visitor, visit_param_bound, bounds);
487         }
488     }
489 }
490
491 pub fn walk_pat<'a, V: Visitor<'a>>(visitor: &mut V, pattern: &'a Pat) {
492     match pattern.kind {
493         PatKind::TupleStruct(ref path, ref elems) => {
494             visitor.visit_path(path, pattern.id);
495             walk_list!(visitor, visit_pat, elems);
496         }
497         PatKind::Path(ref opt_qself, ref path) => {
498             if let Some(ref qself) = *opt_qself {
499                 visitor.visit_ty(&qself.ty);
500             }
501             visitor.visit_path(path, pattern.id)
502         }
503         PatKind::Struct(ref path, ref fields, _) => {
504             visitor.visit_path(path, pattern.id);
505             walk_list!(visitor, visit_field_pattern, fields);
506         }
507         PatKind::Box(ref subpattern)
508         | PatKind::Ref(ref subpattern, _)
509         | PatKind::Paren(ref subpattern) => visitor.visit_pat(subpattern),
510         PatKind::Ident(_, ident, ref optional_subpattern) => {
511             visitor.visit_ident(ident);
512             walk_list!(visitor, visit_pat, optional_subpattern);
513         }
514         PatKind::Lit(ref expression) => visitor.visit_expr(expression),
515         PatKind::Range(ref lower_bound, ref upper_bound, _) => {
516             walk_list!(visitor, visit_expr, lower_bound);
517             walk_list!(visitor, visit_expr, upper_bound);
518         }
519         PatKind::Wild | PatKind::Rest => {}
520         PatKind::Tuple(ref elems) | PatKind::Slice(ref elems) | PatKind::Or(ref elems) => {
521             walk_list!(visitor, visit_pat, elems);
522         }
523         PatKind::Mac(ref mac) => visitor.visit_mac(mac),
524     }
525 }
526
527 pub fn walk_foreign_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a ForeignItem) {
528     let ForeignItem { id, span, ident, vis, attrs, kind, tokens: _ } = item;
529     walk_nested_item(visitor, *id, *span, *ident, vis, attrs, kind, FnCtxt::Foreign);
530 }
531
532 pub fn walk_global_asm<'a, V: Visitor<'a>>(_: &mut V, _: &'a GlobalAsm) {
533     // Empty!
534 }
535
536 pub fn walk_param_bound<'a, V: Visitor<'a>>(visitor: &mut V, bound: &'a GenericBound) {
537     match *bound {
538         GenericBound::Trait(ref typ, ref modifier) => visitor.visit_poly_trait_ref(typ, modifier),
539         GenericBound::Outlives(ref lifetime) => visitor.visit_lifetime(lifetime),
540     }
541 }
542
543 pub fn walk_generic_param<'a, V: Visitor<'a>>(visitor: &mut V, param: &'a GenericParam) {
544     visitor.visit_ident(param.ident);
545     walk_list!(visitor, visit_attribute, param.attrs.iter());
546     walk_list!(visitor, visit_param_bound, &param.bounds);
547     match param.kind {
548         GenericParamKind::Lifetime => (),
549         GenericParamKind::Type { ref default } => walk_list!(visitor, visit_ty, default),
550         GenericParamKind::Const { ref ty, .. } => visitor.visit_ty(ty),
551     }
552 }
553
554 pub fn walk_generics<'a, V: Visitor<'a>>(visitor: &mut V, generics: &'a Generics) {
555     walk_list!(visitor, visit_generic_param, &generics.params);
556     walk_list!(visitor, visit_where_predicate, &generics.where_clause.predicates);
557 }
558
559 pub fn walk_where_predicate<'a, V: Visitor<'a>>(visitor: &mut V, predicate: &'a WherePredicate) {
560     match *predicate {
561         WherePredicate::BoundPredicate(WhereBoundPredicate {
562             ref bounded_ty,
563             ref bounds,
564             ref bound_generic_params,
565             ..
566         }) => {
567             visitor.visit_ty(bounded_ty);
568             walk_list!(visitor, visit_param_bound, bounds);
569             walk_list!(visitor, visit_generic_param, bound_generic_params);
570         }
571         WherePredicate::RegionPredicate(WhereRegionPredicate {
572             ref lifetime, ref bounds, ..
573         }) => {
574             visitor.visit_lifetime(lifetime);
575             walk_list!(visitor, visit_param_bound, bounds);
576         }
577         WherePredicate::EqPredicate(WhereEqPredicate { ref lhs_ty, ref rhs_ty, .. }) => {
578             visitor.visit_ty(lhs_ty);
579             visitor.visit_ty(rhs_ty);
580         }
581     }
582 }
583
584 pub fn walk_fn_ret_ty<'a, V: Visitor<'a>>(visitor: &mut V, ret_ty: &'a FnRetTy) {
585     if let FnRetTy::Ty(ref output_ty) = *ret_ty {
586         visitor.visit_ty(output_ty)
587     }
588 }
589
590 pub fn walk_fn_decl<'a, V: Visitor<'a>>(visitor: &mut V, function_declaration: &'a FnDecl) {
591     for param in &function_declaration.inputs {
592         visitor.visit_param(param);
593     }
594     visitor.visit_fn_ret_ty(&function_declaration.output);
595 }
596
597 pub fn walk_fn<'a, V: Visitor<'a>>(visitor: &mut V, kind: FnKind<'a>, _span: Span) {
598     match kind {
599         FnKind::Fn(_, _, sig, _, body) => {
600             visitor.visit_fn_header(&sig.header);
601             walk_fn_decl(visitor, &sig.decl);
602             walk_list!(visitor, visit_block, body);
603         }
604         FnKind::Closure(decl, body) => {
605             walk_fn_decl(visitor, decl);
606             visitor.visit_expr(body);
607         }
608     }
609 }
610
611 pub fn walk_assoc_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a AssocItem, ctxt: AssocCtxt) {
612     let AssocItem { id, span, ident, vis, attrs, kind, tokens: _, defaultness: _ } = item;
613     walk_nested_item(visitor, *id, *span, *ident, vis, attrs, kind, FnCtxt::Assoc(ctxt));
614 }
615
616 fn walk_nested_item<'a, V: Visitor<'a>>(
617     visitor: &mut V,
618     id: NodeId,
619     span: Span,
620     ident: Ident,
621     vis: &'a Visibility,
622     attrs: &'a [Attribute],
623     kind: &'a AssocItemKind,
624     ctxt: FnCtxt,
625 ) {
626     visitor.visit_vis(vis);
627     visitor.visit_ident(ident);
628     walk_list!(visitor, visit_attribute, attrs);
629     match kind {
630         AssocItemKind::Const(ty, expr) | AssocItemKind::Static(ty, _, expr) => {
631             visitor.visit_ty(ty);
632             walk_list!(visitor, visit_expr, expr);
633         }
634         AssocItemKind::Fn(sig, generics, body) => {
635             visitor.visit_generics(generics);
636             let kind = FnKind::Fn(ctxt, ident, sig, vis, body.as_deref());
637             visitor.visit_fn(kind, span, id);
638         }
639         AssocItemKind::TyAlias(generics, bounds, ty) => {
640             visitor.visit_generics(generics);
641             walk_list!(visitor, visit_param_bound, bounds);
642             walk_list!(visitor, visit_ty, ty);
643         }
644         AssocItemKind::Macro(mac) => {
645             visitor.visit_mac(mac);
646         }
647     }
648 }
649
650 pub fn walk_struct_def<'a, V: Visitor<'a>>(visitor: &mut V, struct_definition: &'a VariantData) {
651     walk_list!(visitor, visit_struct_field, struct_definition.fields());
652 }
653
654 pub fn walk_struct_field<'a, V: Visitor<'a>>(visitor: &mut V, struct_field: &'a StructField) {
655     visitor.visit_vis(&struct_field.vis);
656     if let Some(ident) = struct_field.ident {
657         visitor.visit_ident(ident);
658     }
659     visitor.visit_ty(&struct_field.ty);
660     walk_list!(visitor, visit_attribute, &struct_field.attrs);
661 }
662
663 pub fn walk_block<'a, V: Visitor<'a>>(visitor: &mut V, block: &'a Block) {
664     walk_list!(visitor, visit_stmt, &block.stmts);
665 }
666
667 pub fn walk_stmt<'a, V: Visitor<'a>>(visitor: &mut V, statement: &'a Stmt) {
668     match statement.kind {
669         StmtKind::Local(ref local) => visitor.visit_local(local),
670         StmtKind::Item(ref item) => visitor.visit_item(item),
671         StmtKind::Expr(ref expression) | StmtKind::Semi(ref expression) => {
672             visitor.visit_expr(expression)
673         }
674         StmtKind::Mac(ref mac) => {
675             let (ref mac, _, ref attrs) = **mac;
676             visitor.visit_mac(mac);
677             for attr in attrs.iter() {
678                 visitor.visit_attribute(attr);
679             }
680         }
681     }
682 }
683
684 pub fn walk_mac<'a, V: Visitor<'a>>(visitor: &mut V, mac: &'a Mac) {
685     visitor.visit_path(&mac.path, DUMMY_NODE_ID);
686 }
687
688 pub fn walk_anon_const<'a, V: Visitor<'a>>(visitor: &mut V, constant: &'a AnonConst) {
689     visitor.visit_expr(&constant.value);
690 }
691
692 pub fn walk_expr<'a, V: Visitor<'a>>(visitor: &mut V, expression: &'a Expr) {
693     walk_list!(visitor, visit_attribute, expression.attrs.iter());
694
695     match expression.kind {
696         ExprKind::Box(ref subexpression) => visitor.visit_expr(subexpression),
697         ExprKind::Array(ref subexpressions) => {
698             walk_list!(visitor, visit_expr, subexpressions);
699         }
700         ExprKind::Repeat(ref element, ref count) => {
701             visitor.visit_expr(element);
702             visitor.visit_anon_const(count)
703         }
704         ExprKind::Struct(ref path, ref fields, ref optional_base) => {
705             visitor.visit_path(path, expression.id);
706             walk_list!(visitor, visit_field, fields);
707             walk_list!(visitor, visit_expr, optional_base);
708         }
709         ExprKind::Tup(ref subexpressions) => {
710             walk_list!(visitor, visit_expr, subexpressions);
711         }
712         ExprKind::Call(ref callee_expression, ref arguments) => {
713             visitor.visit_expr(callee_expression);
714             walk_list!(visitor, visit_expr, arguments);
715         }
716         ExprKind::MethodCall(ref segment, ref arguments) => {
717             visitor.visit_path_segment(expression.span, segment);
718             walk_list!(visitor, visit_expr, arguments);
719         }
720         ExprKind::Binary(_, ref left_expression, ref right_expression) => {
721             visitor.visit_expr(left_expression);
722             visitor.visit_expr(right_expression)
723         }
724         ExprKind::AddrOf(_, _, ref subexpression) | ExprKind::Unary(_, ref subexpression) => {
725             visitor.visit_expr(subexpression)
726         }
727         ExprKind::Cast(ref subexpression, ref typ) | ExprKind::Type(ref subexpression, ref typ) => {
728             visitor.visit_expr(subexpression);
729             visitor.visit_ty(typ)
730         }
731         ExprKind::Let(ref pat, ref scrutinee) => {
732             visitor.visit_pat(pat);
733             visitor.visit_expr(scrutinee);
734         }
735         ExprKind::If(ref head_expression, ref if_block, ref optional_else) => {
736             visitor.visit_expr(head_expression);
737             visitor.visit_block(if_block);
738             walk_list!(visitor, visit_expr, optional_else);
739         }
740         ExprKind::While(ref subexpression, ref block, ref opt_label) => {
741             walk_list!(visitor, visit_label, opt_label);
742             visitor.visit_expr(subexpression);
743             visitor.visit_block(block);
744         }
745         ExprKind::ForLoop(ref pattern, ref subexpression, ref block, ref opt_label) => {
746             walk_list!(visitor, visit_label, opt_label);
747             visitor.visit_pat(pattern);
748             visitor.visit_expr(subexpression);
749             visitor.visit_block(block);
750         }
751         ExprKind::Loop(ref block, ref opt_label) => {
752             walk_list!(visitor, visit_label, opt_label);
753             visitor.visit_block(block);
754         }
755         ExprKind::Match(ref subexpression, ref arms) => {
756             visitor.visit_expr(subexpression);
757             walk_list!(visitor, visit_arm, arms);
758         }
759         ExprKind::Closure(_, _, _, ref decl, ref body, _decl_span) => {
760             visitor.visit_fn(FnKind::Closure(decl, body), expression.span, expression.id)
761         }
762         ExprKind::Block(ref block, ref opt_label) => {
763             walk_list!(visitor, visit_label, opt_label);
764             visitor.visit_block(block);
765         }
766         ExprKind::Async(_, _, ref body) => {
767             visitor.visit_block(body);
768         }
769         ExprKind::Await(ref expr) => visitor.visit_expr(expr),
770         ExprKind::Assign(ref lhs, ref rhs, _) => {
771             visitor.visit_expr(lhs);
772             visitor.visit_expr(rhs);
773         }
774         ExprKind::AssignOp(_, ref left_expression, ref right_expression) => {
775             visitor.visit_expr(left_expression);
776             visitor.visit_expr(right_expression);
777         }
778         ExprKind::Field(ref subexpression, ident) => {
779             visitor.visit_expr(subexpression);
780             visitor.visit_ident(ident);
781         }
782         ExprKind::Index(ref main_expression, ref index_expression) => {
783             visitor.visit_expr(main_expression);
784             visitor.visit_expr(index_expression)
785         }
786         ExprKind::Range(ref start, ref end, _) => {
787             walk_list!(visitor, visit_expr, start);
788             walk_list!(visitor, visit_expr, end);
789         }
790         ExprKind::Path(ref maybe_qself, ref path) => {
791             if let Some(ref qself) = *maybe_qself {
792                 visitor.visit_ty(&qself.ty);
793             }
794             visitor.visit_path(path, expression.id)
795         }
796         ExprKind::Break(ref opt_label, ref opt_expr) => {
797             walk_list!(visitor, visit_label, opt_label);
798             walk_list!(visitor, visit_expr, opt_expr);
799         }
800         ExprKind::Continue(ref opt_label) => {
801             walk_list!(visitor, visit_label, opt_label);
802         }
803         ExprKind::Ret(ref optional_expression) => {
804             walk_list!(visitor, visit_expr, optional_expression);
805         }
806         ExprKind::Mac(ref mac) => visitor.visit_mac(mac),
807         ExprKind::Paren(ref subexpression) => visitor.visit_expr(subexpression),
808         ExprKind::InlineAsm(ref ia) => {
809             for &(_, ref input) in &ia.inputs {
810                 visitor.visit_expr(input)
811             }
812             for output in &ia.outputs {
813                 visitor.visit_expr(&output.expr)
814             }
815         }
816         ExprKind::Yield(ref optional_expression) => {
817             walk_list!(visitor, visit_expr, optional_expression);
818         }
819         ExprKind::Try(ref subexpression) => visitor.visit_expr(subexpression),
820         ExprKind::TryBlock(ref body) => visitor.visit_block(body),
821         ExprKind::Lit(_) | ExprKind::Err => {}
822     }
823
824     visitor.visit_expr_post(expression)
825 }
826
827 pub fn walk_param<'a, V: Visitor<'a>>(visitor: &mut V, param: &'a Param) {
828     walk_list!(visitor, visit_attribute, param.attrs.iter());
829     visitor.visit_pat(&param.pat);
830     visitor.visit_ty(&param.ty);
831 }
832
833 pub fn walk_arm<'a, V: Visitor<'a>>(visitor: &mut V, arm: &'a Arm) {
834     visitor.visit_pat(&arm.pat);
835     walk_list!(visitor, visit_expr, &arm.guard);
836     visitor.visit_expr(&arm.body);
837     walk_list!(visitor, visit_attribute, &arm.attrs);
838 }
839
840 pub fn walk_vis<'a, V: Visitor<'a>>(visitor: &mut V, vis: &'a Visibility) {
841     if let VisibilityKind::Restricted { ref path, id } = vis.node {
842         visitor.visit_path(path, id);
843     }
844 }
845
846 pub fn walk_attribute<'a, V: Visitor<'a>>(visitor: &mut V, attr: &'a Attribute) {
847     match attr.kind {
848         AttrKind::Normal(ref item) => walk_mac_args(visitor, &item.args),
849         AttrKind::DocComment(_) => {}
850     }
851 }
852
853 pub fn walk_mac_args<'a, V: Visitor<'a>>(visitor: &mut V, args: &'a MacArgs) {
854     match args {
855         MacArgs::Empty => {}
856         MacArgs::Delimited(_dspan, _delim, tokens) => visitor.visit_tts(tokens.clone()),
857         MacArgs::Eq(_eq_span, tokens) => visitor.visit_tts(tokens.clone()),
858     }
859 }
860
861 pub fn walk_tt<'a, V: Visitor<'a>>(visitor: &mut V, tt: TokenTree) {
862     match tt {
863         TokenTree::Token(token) => visitor.visit_token(token),
864         TokenTree::Delimited(_, _, tts) => visitor.visit_tts(tts),
865     }
866 }
867
868 pub fn walk_tts<'a, V: Visitor<'a>>(visitor: &mut V, tts: TokenStream) {
869     for tt in tts.trees() {
870         visitor.visit_tt(tt);
871     }
872 }