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