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