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