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