]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_hir/src/intravisit.rs
531d9f14040217c86c2580c1fe08ae187a561665
[rust.git] / compiler / rustc_hir / src / intravisit.rs
1 //! HIR walker for walking the contents of nodes.
2 //!
3 //! Here are the three available patterns for the visitor strategy,
4 //! in roughly the order of desirability:
5 //!
6 //! 1. **Shallow visit**: Get a simple callback for every item (or item-like thing) in the HIR.
7 //!    - Example: find all items with a `#[foo]` attribute on them.
8 //!    - How: Use the `hir_crate_items` or `hir_module_items` query to traverse over item-like ids
9 //!       (ItemId, TraitItemId, etc.) and use tcx.def_kind and `tcx.hir().item*(id)` to filter and
10 //!       access actual item-like thing, respectively.
11 //!    - Pro: Efficient; just walks the lists of item ids and gives users control whether to access
12 //!       the hir_owners themselves or not.
13 //!    - Con: Don't get information about nesting
14 //!    - Con: Don't have methods for specific bits of HIR, like "on
15 //!      every expr, do this".
16 //! 2. **Deep visit**: Want to scan for specific kinds of HIR nodes within
17 //!    an item, but don't care about how item-like things are nested
18 //!    within one another.
19 //!    - Example: Examine each expression to look for its type and do some check or other.
20 //!    - How: Implement `intravisit::Visitor` and override the `NestedFilter` type to
21 //!      `nested_filter::OnlyBodies` (and implement `nested_visit_map`), and use
22 //!      `tcx.hir().visit_all_item_likes_in_crate(&mut visitor)`. Within your
23 //!      `intravisit::Visitor` impl, implement methods like `visit_expr()` (don't forget to invoke
24 //!      `intravisit::walk_expr()` to keep walking the subparts).
25 //!    - Pro: Visitor methods for any kind of HIR node, not just item-like things.
26 //!    - Pro: Integrates well into dependency tracking.
27 //!    - Con: Don't get information about nesting between items
28 //! 3. **Nested visit**: Want to visit the whole HIR and you care about the nesting between
29 //!    item-like things.
30 //!    - Example: Lifetime resolution, which wants to bring lifetimes declared on the
31 //!      impl into scope while visiting the impl-items, and then back out again.
32 //!    - How: Implement `intravisit::Visitor` and override the `NestedFilter` type to
33 //!      `nested_filter::All` (and implement `nested_visit_map`). Walk your crate with
34 //!      `tcx.hir().walk_toplevel_module(visitor)` invoked on `tcx.hir().krate()`.
35 //!    - Pro: Visitor methods for any kind of HIR node, not just item-like things.
36 //!    - Pro: Preserves nesting information
37 //!    - Con: Does not integrate well into dependency tracking.
38 //!
39 //! If you have decided to use this visitor, here are some general
40 //! notes on how to do so:
41 //!
42 //! Each overridden visit method has full control over what
43 //! happens with its node, it can do its own traversal of the node's children,
44 //! call `intravisit::walk_*` to apply the default traversal algorithm, or prevent
45 //! deeper traversal by doing nothing.
46 //!
47 //! When visiting the HIR, the contents of nested items are NOT visited
48 //! by default. This is different from the AST visitor, which does a deep walk.
49 //! Hence this module is called `intravisit`; see the method `visit_nested_item`
50 //! for more details.
51 //!
52 //! Note: it is an important invariant that the default visitor walks
53 //! the body of a function in "execution order" - more concretely, if
54 //! we consider the reverse post-order (RPO) of the CFG implied by the HIR,
55 //! then a pre-order traversal of the HIR is consistent with the CFG RPO
56 //! on the *initial CFG point* of each HIR node, while a post-order traversal
57 //! of the HIR is consistent with the CFG RPO on each *final CFG point* of
58 //! each CFG node.
59 //!
60 //! One thing that follows is that if HIR node A always starts/ends executing
61 //! before HIR node B, then A appears in traversal pre/postorder before B,
62 //! respectively. (This follows from RPO respecting CFG domination).
63 //!
64 //! This order consistency is required in a few places in rustc, for
65 //! example generator inference, and possibly also HIR borrowck.
66
67 use crate::hir::*;
68 use rustc_ast::walk_list;
69 use rustc_ast::{Attribute, Label};
70 use rustc_span::symbol::{Ident, Symbol};
71 use rustc_span::Span;
72
73 pub trait IntoVisitor<'hir> {
74     type Visitor: Visitor<'hir>;
75     fn into_visitor(&self) -> Self::Visitor;
76 }
77
78 #[derive(Copy, Clone, Debug)]
79 pub enum FnKind<'a> {
80     /// `#[xxx] pub async/const/extern "Abi" fn foo()`
81     ItemFn(Ident, &'a Generics<'a>, FnHeader),
82
83     /// `fn foo(&self)`
84     Method(Ident, &'a FnSig<'a>),
85
86     /// `|x, y| {}`
87     Closure,
88 }
89
90 impl<'a> FnKind<'a> {
91     pub fn header(&self) -> Option<&FnHeader> {
92         match *self {
93             FnKind::ItemFn(_, _, ref header) => Some(header),
94             FnKind::Method(_, ref sig) => Some(&sig.header),
95             FnKind::Closure => None,
96         }
97     }
98
99     pub fn constness(self) -> Constness {
100         self.header().map_or(Constness::NotConst, |header| header.constness)
101     }
102
103     pub fn asyncness(self) -> IsAsync {
104         self.header().map_or(IsAsync::NotAsync, |header| header.asyncness)
105     }
106 }
107
108 /// An abstract representation of the HIR `rustc_middle::hir::map::Map`.
109 pub trait Map<'hir> {
110     /// Retrieves the `Node` corresponding to `id`, returning `None` if cannot be found.
111     fn find(&self, hir_id: HirId) -> Option<Node<'hir>>;
112     fn body(&self, id: BodyId) -> &'hir Body<'hir>;
113     fn item(&self, id: ItemId) -> &'hir Item<'hir>;
114     fn trait_item(&self, id: TraitItemId) -> &'hir TraitItem<'hir>;
115     fn impl_item(&self, id: ImplItemId) -> &'hir ImplItem<'hir>;
116     fn foreign_item(&self, id: ForeignItemId) -> &'hir ForeignItem<'hir>;
117 }
118
119 // Used when no map is actually available, forcing manual implementation of nested visitors.
120 impl<'hir> Map<'hir> for ! {
121     fn find(&self, _: HirId) -> Option<Node<'hir>> {
122         *self;
123     }
124     fn body(&self, _: BodyId) -> &'hir Body<'hir> {
125         *self;
126     }
127     fn item(&self, _: ItemId) -> &'hir Item<'hir> {
128         *self;
129     }
130     fn trait_item(&self, _: TraitItemId) -> &'hir TraitItem<'hir> {
131         *self;
132     }
133     fn impl_item(&self, _: ImplItemId) -> &'hir ImplItem<'hir> {
134         *self;
135     }
136     fn foreign_item(&self, _: ForeignItemId) -> &'hir ForeignItem<'hir> {
137         *self;
138     }
139 }
140
141 pub mod nested_filter {
142     use super::Map;
143
144     /// Specifies what nested things a visitor wants to visit. By "nested
145     /// things", we are referring to bits of HIR that are not directly embedded
146     /// within one another but rather indirectly, through a table in the crate.
147     /// This is done to control dependencies during incremental compilation: the
148     /// non-inline bits of HIR can be tracked and hashed separately.
149     ///
150     /// The most common choice is `OnlyBodies`, which will cause the visitor to
151     /// visit fn bodies for fns that it encounters, and closure bodies, but
152     /// skip over nested item-like things.
153     ///
154     /// See the comments on `ItemLikeVisitor` for more details on the overall
155     /// visit strategy.
156     pub trait NestedFilter<'hir> {
157         type Map: Map<'hir>;
158
159         /// Whether the visitor visits nested "item-like" things.
160         /// E.g., item, impl-item.
161         const INTER: bool;
162         /// Whether the visitor visits "intra item-like" things.
163         /// E.g., function body, closure, `AnonConst`
164         const INTRA: bool;
165     }
166
167     /// Do not visit any nested things. When you add a new
168     /// "non-nested" thing, you will want to audit such uses to see if
169     /// they remain valid.
170     ///
171     /// Use this if you are only walking some particular kind of tree
172     /// (i.e., a type, or fn signature) and you don't want to thread a
173     /// HIR map around.
174     pub struct None(());
175     impl NestedFilter<'_> for None {
176         type Map = !;
177         const INTER: bool = false;
178         const INTRA: bool = false;
179     }
180 }
181
182 use nested_filter::NestedFilter;
183
184 /// Each method of the Visitor trait is a hook to be potentially
185 /// overridden. Each method's default implementation recursively visits
186 /// the substructure of the input via the corresponding `walk` method;
187 /// e.g., the `visit_mod` method by default calls `intravisit::walk_mod`.
188 ///
189 /// Note that this visitor does NOT visit nested items by default
190 /// (this is why the module is called `intravisit`, to distinguish it
191 /// from the AST's `visit` module, which acts differently). If you
192 /// simply want to visit all items in the crate in some order, you
193 /// should call `tcx.hir().visit_all_item_likes_in_crate`. Otherwise, see the comment
194 /// on `visit_nested_item` for details on how to visit nested items.
195 ///
196 /// If you want to ensure that your code handles every variant
197 /// explicitly, you need to override each method. (And you also need
198 /// to monitor future changes to `Visitor` in case a new method with a
199 /// new default implementation gets introduced.)
200 pub trait Visitor<'v>: Sized {
201     // this type should not be overridden, it exists for convenient usage as `Self::Map`
202     type Map: Map<'v> = <Self::NestedFilter as NestedFilter<'v>>::Map;
203
204     ///////////////////////////////////////////////////////////////////////////
205     // Nested items.
206
207     /// Override this type to control which nested HIR are visited; see
208     /// [`NestedFilter`] for details. If you override this type, you
209     /// must also override [`nested_visit_map`](Self::nested_visit_map).
210     ///
211     /// **If for some reason you want the nested behavior, but don't
212     /// have a `Map` at your disposal:** then override the
213     /// `visit_nested_XXX` methods. If a new `visit_nested_XXX` variant is
214     /// added in the future, it will cause a panic which can be detected
215     /// and fixed appropriately.
216     type NestedFilter: NestedFilter<'v> = nested_filter::None;
217
218     /// If `type NestedFilter` is set to visit nested items, this method
219     /// must also be overridden to provide a map to retrieve nested items.
220     fn nested_visit_map(&mut self) -> Self::Map {
221         panic!(
222             "nested_visit_map must be implemented or consider using \
223             `type NestedFilter = nested_filter::None` (the default)"
224         );
225     }
226
227     /// Invoked when a nested item is encountered. By default, when
228     /// `Self::NestedFilter` is `nested_filter::None`, this method does
229     /// nothing. **You probably don't want to override this method** --
230     /// instead, override [`Self::NestedFilter`] or use the "shallow" or
231     /// "deep" visit patterns described on
232     /// `itemlikevisit::ItemLikeVisitor`. The only reason to override
233     /// this method is if you want a nested pattern but cannot supply a
234     /// [`Map`]; see `nested_visit_map` for advice.
235     fn visit_nested_item(&mut self, id: ItemId) {
236         if Self::NestedFilter::INTER {
237             let item = self.nested_visit_map().item(id);
238             self.visit_item(item);
239         }
240     }
241
242     /// Like `visit_nested_item()`, but for trait items. See
243     /// `visit_nested_item()` for advice on when to override this
244     /// method.
245     fn visit_nested_trait_item(&mut self, id: TraitItemId) {
246         if Self::NestedFilter::INTER {
247             let item = self.nested_visit_map().trait_item(id);
248             self.visit_trait_item(item);
249         }
250     }
251
252     /// Like `visit_nested_item()`, but for impl items. See
253     /// `visit_nested_item()` for advice on when to override this
254     /// method.
255     fn visit_nested_impl_item(&mut self, id: ImplItemId) {
256         if Self::NestedFilter::INTER {
257             let item = self.nested_visit_map().impl_item(id);
258             self.visit_impl_item(item);
259         }
260     }
261
262     /// Like `visit_nested_item()`, but for foreign items. See
263     /// `visit_nested_item()` for advice on when to override this
264     /// method.
265     fn visit_nested_foreign_item(&mut self, id: ForeignItemId) {
266         if Self::NestedFilter::INTER {
267             let item = self.nested_visit_map().foreign_item(id);
268             self.visit_foreign_item(item);
269         }
270     }
271
272     /// Invoked to visit the body of a function, method or closure. Like
273     /// `visit_nested_item`, does nothing by default unless you override
274     /// `Self::NestedFilter`.
275     fn visit_nested_body(&mut self, id: BodyId) {
276         if Self::NestedFilter::INTRA {
277             let body = self.nested_visit_map().body(id);
278             self.visit_body(body);
279         }
280     }
281
282     fn visit_param(&mut self, param: &'v Param<'v>) {
283         walk_param(self, param)
284     }
285
286     /// Visits the top-level item and (optionally) nested items / impl items. See
287     /// `visit_nested_item` for details.
288     fn visit_item(&mut self, i: &'v Item<'v>) {
289         walk_item(self, i)
290     }
291
292     fn visit_body(&mut self, b: &'v Body<'v>) {
293         walk_body(self, b);
294     }
295
296     ///////////////////////////////////////////////////////////////////////////
297
298     fn visit_id(&mut self, _hir_id: HirId) {
299         // Nothing to do.
300     }
301     fn visit_name(&mut self, _span: Span, _name: Symbol) {
302         // Nothing to do.
303     }
304     fn visit_ident(&mut self, ident: Ident) {
305         walk_ident(self, ident)
306     }
307     fn visit_mod(&mut self, m: &'v Mod<'v>, _s: Span, n: HirId) {
308         walk_mod(self, m, n)
309     }
310     fn visit_foreign_item(&mut self, i: &'v ForeignItem<'v>) {
311         walk_foreign_item(self, i)
312     }
313     fn visit_local(&mut self, l: &'v Local<'v>) {
314         walk_local(self, l)
315     }
316     fn visit_block(&mut self, b: &'v Block<'v>) {
317         walk_block(self, b)
318     }
319     fn visit_stmt(&mut self, s: &'v Stmt<'v>) {
320         walk_stmt(self, s)
321     }
322     fn visit_arm(&mut self, a: &'v Arm<'v>) {
323         walk_arm(self, a)
324     }
325     fn visit_pat(&mut self, p: &'v Pat<'v>) {
326         walk_pat(self, p)
327     }
328     fn visit_array_length(&mut self, len: &'v ArrayLen) {
329         walk_array_len(self, len)
330     }
331     fn visit_anon_const(&mut self, c: &'v AnonConst) {
332         walk_anon_const(self, c)
333     }
334     fn visit_expr(&mut self, ex: &'v Expr<'v>) {
335         walk_expr(self, ex)
336     }
337     fn visit_let_expr(&mut self, lex: &'v Let<'v>) {
338         walk_let_expr(self, lex)
339     }
340     fn visit_ty(&mut self, t: &'v Ty<'v>) {
341         walk_ty(self, t)
342     }
343     fn visit_generic_param(&mut self, p: &'v GenericParam<'v>) {
344         walk_generic_param(self, p)
345     }
346     fn visit_const_param_default(&mut self, _param: HirId, ct: &'v AnonConst) {
347         walk_const_param_default(self, ct)
348     }
349     fn visit_generics(&mut self, g: &'v Generics<'v>) {
350         walk_generics(self, g)
351     }
352     fn visit_where_predicate(&mut self, predicate: &'v WherePredicate<'v>) {
353         walk_where_predicate(self, predicate)
354     }
355     fn visit_fn_decl(&mut self, fd: &'v FnDecl<'v>) {
356         walk_fn_decl(self, fd)
357     }
358     fn visit_fn(&mut self, fk: FnKind<'v>, fd: &'v FnDecl<'v>, b: BodyId, s: Span, id: HirId) {
359         walk_fn(self, fk, fd, b, s, id)
360     }
361     fn visit_use(&mut self, path: &'v Path<'v>, hir_id: HirId) {
362         walk_use(self, path, hir_id)
363     }
364     fn visit_trait_item(&mut self, ti: &'v TraitItem<'v>) {
365         walk_trait_item(self, ti)
366     }
367     fn visit_trait_item_ref(&mut self, ii: &'v TraitItemRef) {
368         walk_trait_item_ref(self, ii)
369     }
370     fn visit_impl_item(&mut self, ii: &'v ImplItem<'v>) {
371         walk_impl_item(self, ii)
372     }
373     fn visit_foreign_item_ref(&mut self, ii: &'v ForeignItemRef) {
374         walk_foreign_item_ref(self, ii)
375     }
376     fn visit_impl_item_ref(&mut self, ii: &'v ImplItemRef) {
377         walk_impl_item_ref(self, ii)
378     }
379     fn visit_trait_ref(&mut self, t: &'v TraitRef<'v>) {
380         walk_trait_ref(self, t)
381     }
382     fn visit_param_bound(&mut self, bounds: &'v GenericBound<'v>) {
383         walk_param_bound(self, bounds)
384     }
385     fn visit_poly_trait_ref(&mut self, t: &'v PolyTraitRef<'v>, m: TraitBoundModifier) {
386         walk_poly_trait_ref(self, t, m)
387     }
388     fn visit_variant_data(
389         &mut self,
390         s: &'v VariantData<'v>,
391         _: Symbol,
392         _: &'v Generics<'v>,
393         _parent_id: HirId,
394         _: Span,
395     ) {
396         walk_struct_def(self, s)
397     }
398     fn visit_field_def(&mut self, s: &'v FieldDef<'v>) {
399         walk_field_def(self, s)
400     }
401     fn visit_enum_def(
402         &mut self,
403         enum_definition: &'v EnumDef<'v>,
404         generics: &'v Generics<'v>,
405         item_id: HirId,
406         _: Span,
407     ) {
408         walk_enum_def(self, enum_definition, generics, item_id)
409     }
410     fn visit_variant(&mut self, v: &'v Variant<'v>, g: &'v Generics<'v>, item_id: HirId) {
411         walk_variant(self, v, g, item_id)
412     }
413     fn visit_label(&mut self, label: &'v Label) {
414         walk_label(self, label)
415     }
416     fn visit_infer(&mut self, inf: &'v InferArg) {
417         walk_inf(self, inf);
418     }
419     fn visit_generic_arg(&mut self, generic_arg: &'v GenericArg<'v>) {
420         match generic_arg {
421             GenericArg::Lifetime(lt) => self.visit_lifetime(lt),
422             GenericArg::Type(ty) => self.visit_ty(ty),
423             GenericArg::Const(ct) => self.visit_anon_const(&ct.value),
424             GenericArg::Infer(inf) => self.visit_infer(inf),
425         }
426     }
427     fn visit_lifetime(&mut self, lifetime: &'v Lifetime) {
428         walk_lifetime(self, lifetime)
429     }
430     fn visit_qpath(&mut self, qpath: &'v QPath<'v>, id: HirId, span: Span) {
431         walk_qpath(self, qpath, id, span)
432     }
433     fn visit_path(&mut self, path: &'v Path<'v>, _id: HirId) {
434         walk_path(self, path)
435     }
436     fn visit_path_segment(&mut self, path_span: Span, path_segment: &'v PathSegment<'v>) {
437         walk_path_segment(self, path_span, path_segment)
438     }
439     fn visit_generic_args(&mut self, path_span: Span, generic_args: &'v GenericArgs<'v>) {
440         walk_generic_args(self, path_span, generic_args)
441     }
442     fn visit_assoc_type_binding(&mut self, type_binding: &'v TypeBinding<'v>) {
443         walk_assoc_type_binding(self, type_binding)
444     }
445     fn visit_attribute(&mut self, _attr: &'v Attribute) {}
446     fn visit_associated_item_kind(&mut self, kind: &'v AssocItemKind) {
447         walk_associated_item_kind(self, kind);
448     }
449     fn visit_defaultness(&mut self, defaultness: &'v Defaultness) {
450         walk_defaultness(self, defaultness);
451     }
452     fn visit_inline_asm(&mut self, asm: &'v InlineAsm<'v>, id: HirId) {
453         walk_inline_asm(self, asm, id);
454     }
455 }
456
457 pub fn walk_mod<'v, V: Visitor<'v>>(visitor: &mut V, module: &'v Mod<'v>, mod_hir_id: HirId) {
458     visitor.visit_id(mod_hir_id);
459     for &item_id in module.item_ids {
460         visitor.visit_nested_item(item_id);
461     }
462 }
463
464 pub fn walk_body<'v, V: Visitor<'v>>(visitor: &mut V, body: &'v Body<'v>) {
465     walk_list!(visitor, visit_param, body.params);
466     visitor.visit_expr(&body.value);
467 }
468
469 pub fn walk_local<'v, V: Visitor<'v>>(visitor: &mut V, local: &'v Local<'v>) {
470     // Intentionally visiting the expr first - the initialization expr
471     // dominates the local's definition.
472     walk_list!(visitor, visit_expr, &local.init);
473     visitor.visit_id(local.hir_id);
474     visitor.visit_pat(&local.pat);
475     walk_list!(visitor, visit_ty, &local.ty);
476 }
477
478 pub fn walk_ident<'v, V: Visitor<'v>>(visitor: &mut V, ident: Ident) {
479     visitor.visit_name(ident.span, ident.name);
480 }
481
482 pub fn walk_label<'v, V: Visitor<'v>>(visitor: &mut V, label: &'v Label) {
483     visitor.visit_ident(label.ident);
484 }
485
486 pub fn walk_lifetime<'v, V: Visitor<'v>>(visitor: &mut V, lifetime: &'v Lifetime) {
487     visitor.visit_id(lifetime.hir_id);
488     match lifetime.name {
489         LifetimeName::Param(_, ParamName::Plain(ident)) => {
490             visitor.visit_ident(ident);
491         }
492         LifetimeName::Param(_, ParamName::Fresh)
493         | LifetimeName::Param(_, ParamName::Error)
494         | LifetimeName::Static
495         | LifetimeName::Error
496         | LifetimeName::Implicit
497         | LifetimeName::ImplicitObjectLifetimeDefault
498         | LifetimeName::Underscore => {}
499     }
500 }
501
502 pub fn walk_poly_trait_ref<'v, V: Visitor<'v>>(
503     visitor: &mut V,
504     trait_ref: &'v PolyTraitRef<'v>,
505     _modifier: TraitBoundModifier,
506 ) {
507     walk_list!(visitor, visit_generic_param, trait_ref.bound_generic_params);
508     visitor.visit_trait_ref(&trait_ref.trait_ref);
509 }
510
511 pub fn walk_trait_ref<'v, V: Visitor<'v>>(visitor: &mut V, trait_ref: &'v TraitRef<'v>) {
512     visitor.visit_id(trait_ref.hir_ref_id);
513     visitor.visit_path(&trait_ref.path, trait_ref.hir_ref_id)
514 }
515
516 pub fn walk_param<'v, V: Visitor<'v>>(visitor: &mut V, param: &'v Param<'v>) {
517     visitor.visit_id(param.hir_id);
518     visitor.visit_pat(&param.pat);
519 }
520
521 pub fn walk_item<'v, V: Visitor<'v>>(visitor: &mut V, item: &'v Item<'v>) {
522     visitor.visit_ident(item.ident);
523     match item.kind {
524         ItemKind::ExternCrate(orig_name) => {
525             visitor.visit_id(item.hir_id());
526             if let Some(orig_name) = orig_name {
527                 visitor.visit_name(item.span, orig_name);
528             }
529         }
530         ItemKind::Use(ref path, _) => {
531             visitor.visit_use(path, item.hir_id());
532         }
533         ItemKind::Static(ref typ, _, body) | ItemKind::Const(ref typ, body) => {
534             visitor.visit_id(item.hir_id());
535             visitor.visit_ty(typ);
536             visitor.visit_nested_body(body);
537         }
538         ItemKind::Fn(ref sig, ref generics, body_id) => visitor.visit_fn(
539             FnKind::ItemFn(item.ident, generics, sig.header),
540             &sig.decl,
541             body_id,
542             item.span,
543             item.hir_id(),
544         ),
545         ItemKind::Macro(..) => {
546             visitor.visit_id(item.hir_id());
547         }
548         ItemKind::Mod(ref module) => {
549             // `visit_mod()` takes care of visiting the `Item`'s `HirId`.
550             visitor.visit_mod(module, item.span, item.hir_id())
551         }
552         ItemKind::ForeignMod { abi: _, items } => {
553             visitor.visit_id(item.hir_id());
554             walk_list!(visitor, visit_foreign_item_ref, items);
555         }
556         ItemKind::GlobalAsm(asm) => {
557             visitor.visit_id(item.hir_id());
558             visitor.visit_inline_asm(asm, item.hir_id());
559         }
560         ItemKind::TyAlias(ref ty, ref generics) => {
561             visitor.visit_id(item.hir_id());
562             visitor.visit_ty(ty);
563             visitor.visit_generics(generics)
564         }
565         ItemKind::OpaqueTy(OpaqueTy { ref generics, bounds, .. }) => {
566             visitor.visit_id(item.hir_id());
567             walk_generics(visitor, generics);
568             walk_list!(visitor, visit_param_bound, bounds);
569         }
570         ItemKind::Enum(ref enum_definition, ref generics) => {
571             visitor.visit_generics(generics);
572             // `visit_enum_def()` takes care of visiting the `Item`'s `HirId`.
573             visitor.visit_enum_def(enum_definition, generics, item.hir_id(), item.span)
574         }
575         ItemKind::Impl(Impl {
576             unsafety: _,
577             defaultness: _,
578             polarity: _,
579             constness: _,
580             defaultness_span: _,
581             ref generics,
582             ref of_trait,
583             ref self_ty,
584             items,
585         }) => {
586             visitor.visit_id(item.hir_id());
587             visitor.visit_generics(generics);
588             walk_list!(visitor, visit_trait_ref, of_trait);
589             visitor.visit_ty(self_ty);
590             walk_list!(visitor, visit_impl_item_ref, *items);
591         }
592         ItemKind::Struct(ref struct_definition, ref generics)
593         | ItemKind::Union(ref struct_definition, ref generics) => {
594             visitor.visit_generics(generics);
595             visitor.visit_id(item.hir_id());
596             visitor.visit_variant_data(
597                 struct_definition,
598                 item.ident.name,
599                 generics,
600                 item.hir_id(),
601                 item.span,
602             );
603         }
604         ItemKind::Trait(.., ref generics, bounds, trait_item_refs) => {
605             visitor.visit_id(item.hir_id());
606             visitor.visit_generics(generics);
607             walk_list!(visitor, visit_param_bound, bounds);
608             walk_list!(visitor, visit_trait_item_ref, trait_item_refs);
609         }
610         ItemKind::TraitAlias(ref generics, bounds) => {
611             visitor.visit_id(item.hir_id());
612             visitor.visit_generics(generics);
613             walk_list!(visitor, visit_param_bound, bounds);
614         }
615     }
616 }
617
618 pub fn walk_inline_asm<'v, V: Visitor<'v>>(visitor: &mut V, asm: &'v InlineAsm<'v>, id: HirId) {
619     for (op, op_sp) in asm.operands {
620         match op {
621             InlineAsmOperand::In { expr, .. } | InlineAsmOperand::InOut { expr, .. } => {
622                 visitor.visit_expr(expr)
623             }
624             InlineAsmOperand::Out { expr, .. } => {
625                 if let Some(expr) = expr {
626                     visitor.visit_expr(expr);
627                 }
628             }
629             InlineAsmOperand::SplitInOut { in_expr, out_expr, .. } => {
630                 visitor.visit_expr(in_expr);
631                 if let Some(out_expr) = out_expr {
632                     visitor.visit_expr(out_expr);
633                 }
634             }
635             InlineAsmOperand::Const { anon_const, .. }
636             | InlineAsmOperand::SymFn { anon_const, .. } => visitor.visit_anon_const(anon_const),
637             InlineAsmOperand::SymStatic { path, .. } => visitor.visit_qpath(path, id, *op_sp),
638         }
639     }
640 }
641
642 pub fn walk_use<'v, V: Visitor<'v>>(visitor: &mut V, path: &'v Path<'v>, hir_id: HirId) {
643     visitor.visit_id(hir_id);
644     visitor.visit_path(path, hir_id);
645 }
646
647 pub fn walk_enum_def<'v, V: Visitor<'v>>(
648     visitor: &mut V,
649     enum_definition: &'v EnumDef<'v>,
650     generics: &'v Generics<'v>,
651     item_id: HirId,
652 ) {
653     visitor.visit_id(item_id);
654     walk_list!(visitor, visit_variant, enum_definition.variants, generics, item_id);
655 }
656
657 pub fn walk_variant<'v, V: Visitor<'v>>(
658     visitor: &mut V,
659     variant: &'v Variant<'v>,
660     generics: &'v Generics<'v>,
661     parent_item_id: HirId,
662 ) {
663     visitor.visit_ident(variant.ident);
664     visitor.visit_id(variant.id);
665     visitor.visit_variant_data(
666         &variant.data,
667         variant.ident.name,
668         generics,
669         parent_item_id,
670         variant.span,
671     );
672     walk_list!(visitor, visit_anon_const, &variant.disr_expr);
673 }
674
675 pub fn walk_ty<'v, V: Visitor<'v>>(visitor: &mut V, typ: &'v Ty<'v>) {
676     visitor.visit_id(typ.hir_id);
677
678     match typ.kind {
679         TyKind::Slice(ref ty) => visitor.visit_ty(ty),
680         TyKind::Ptr(ref mutable_type) => visitor.visit_ty(&mutable_type.ty),
681         TyKind::Rptr(ref lifetime, ref mutable_type) => {
682             visitor.visit_lifetime(lifetime);
683             visitor.visit_ty(&mutable_type.ty)
684         }
685         TyKind::Never => {}
686         TyKind::Tup(tuple_element_types) => {
687             walk_list!(visitor, visit_ty, tuple_element_types);
688         }
689         TyKind::BareFn(ref function_declaration) => {
690             walk_list!(visitor, visit_generic_param, function_declaration.generic_params);
691             visitor.visit_fn_decl(&function_declaration.decl);
692         }
693         TyKind::Path(ref qpath) => {
694             visitor.visit_qpath(qpath, typ.hir_id, typ.span);
695         }
696         TyKind::OpaqueDef(item_id, lifetimes) => {
697             visitor.visit_nested_item(item_id);
698             walk_list!(visitor, visit_generic_arg, lifetimes);
699         }
700         TyKind::Array(ref ty, ref length) => {
701             visitor.visit_ty(ty);
702             visitor.visit_array_length(length)
703         }
704         TyKind::TraitObject(bounds, ref lifetime, _syntax) => {
705             for bound in bounds {
706                 visitor.visit_poly_trait_ref(bound, TraitBoundModifier::None);
707             }
708             visitor.visit_lifetime(lifetime);
709         }
710         TyKind::Typeof(ref expression) => visitor.visit_anon_const(expression),
711         TyKind::Infer | TyKind::Err => {}
712     }
713 }
714
715 pub fn walk_inf<'v, V: Visitor<'v>>(visitor: &mut V, inf: &'v InferArg) {
716     visitor.visit_id(inf.hir_id);
717 }
718
719 pub fn walk_qpath<'v, V: Visitor<'v>>(
720     visitor: &mut V,
721     qpath: &'v QPath<'v>,
722     id: HirId,
723     span: Span,
724 ) {
725     match *qpath {
726         QPath::Resolved(ref maybe_qself, ref path) => {
727             walk_list!(visitor, visit_ty, maybe_qself);
728             visitor.visit_path(path, id)
729         }
730         QPath::TypeRelative(ref qself, ref segment) => {
731             visitor.visit_ty(qself);
732             visitor.visit_path_segment(span, segment);
733         }
734         QPath::LangItem(..) => {}
735     }
736 }
737
738 pub fn walk_path<'v, V: Visitor<'v>>(visitor: &mut V, path: &'v Path<'v>) {
739     for segment in path.segments {
740         visitor.visit_path_segment(path.span, segment);
741     }
742 }
743
744 pub fn walk_path_segment<'v, V: Visitor<'v>>(
745     visitor: &mut V,
746     path_span: Span,
747     segment: &'v PathSegment<'v>,
748 ) {
749     visitor.visit_ident(segment.ident);
750     walk_list!(visitor, visit_id, segment.hir_id);
751     if let Some(ref args) = segment.args {
752         visitor.visit_generic_args(path_span, args);
753     }
754 }
755
756 pub fn walk_generic_args<'v, V: Visitor<'v>>(
757     visitor: &mut V,
758     _path_span: Span,
759     generic_args: &'v GenericArgs<'v>,
760 ) {
761     walk_list!(visitor, visit_generic_arg, generic_args.args);
762     walk_list!(visitor, visit_assoc_type_binding, generic_args.bindings);
763 }
764
765 pub fn walk_assoc_type_binding<'v, V: Visitor<'v>>(
766     visitor: &mut V,
767     type_binding: &'v TypeBinding<'v>,
768 ) {
769     visitor.visit_id(type_binding.hir_id);
770     visitor.visit_ident(type_binding.ident);
771     visitor.visit_generic_args(type_binding.span, type_binding.gen_args);
772     match type_binding.kind {
773         TypeBindingKind::Equality { ref term } => match term {
774             Term::Ty(ref ty) => visitor.visit_ty(ty),
775             Term::Const(ref c) => visitor.visit_anon_const(c),
776         },
777         TypeBindingKind::Constraint { bounds } => walk_list!(visitor, visit_param_bound, bounds),
778     }
779 }
780
781 pub fn walk_pat<'v, V: Visitor<'v>>(visitor: &mut V, pattern: &'v Pat<'v>) {
782     visitor.visit_id(pattern.hir_id);
783     match pattern.kind {
784         PatKind::TupleStruct(ref qpath, children, _) => {
785             visitor.visit_qpath(qpath, pattern.hir_id, pattern.span);
786             walk_list!(visitor, visit_pat, children);
787         }
788         PatKind::Path(ref qpath) => {
789             visitor.visit_qpath(qpath, pattern.hir_id, pattern.span);
790         }
791         PatKind::Struct(ref qpath, fields, _) => {
792             visitor.visit_qpath(qpath, pattern.hir_id, pattern.span);
793             for field in fields {
794                 visitor.visit_id(field.hir_id);
795                 visitor.visit_ident(field.ident);
796                 visitor.visit_pat(&field.pat)
797             }
798         }
799         PatKind::Or(pats) => walk_list!(visitor, visit_pat, pats),
800         PatKind::Tuple(tuple_elements, _) => {
801             walk_list!(visitor, visit_pat, tuple_elements);
802         }
803         PatKind::Box(ref subpattern) | PatKind::Ref(ref subpattern, _) => {
804             visitor.visit_pat(subpattern)
805         }
806         PatKind::Binding(_, _hir_id, ident, ref optional_subpattern) => {
807             visitor.visit_ident(ident);
808             walk_list!(visitor, visit_pat, optional_subpattern);
809         }
810         PatKind::Lit(ref expression) => visitor.visit_expr(expression),
811         PatKind::Range(ref lower_bound, ref upper_bound, _) => {
812             walk_list!(visitor, visit_expr, lower_bound);
813             walk_list!(visitor, visit_expr, upper_bound);
814         }
815         PatKind::Wild => (),
816         PatKind::Slice(prepatterns, ref slice_pattern, postpatterns) => {
817             walk_list!(visitor, visit_pat, prepatterns);
818             walk_list!(visitor, visit_pat, slice_pattern);
819             walk_list!(visitor, visit_pat, postpatterns);
820         }
821     }
822 }
823
824 pub fn walk_foreign_item<'v, V: Visitor<'v>>(visitor: &mut V, foreign_item: &'v ForeignItem<'v>) {
825     visitor.visit_id(foreign_item.hir_id());
826     visitor.visit_ident(foreign_item.ident);
827
828     match foreign_item.kind {
829         ForeignItemKind::Fn(ref function_declaration, param_names, ref generics) => {
830             visitor.visit_generics(generics);
831             visitor.visit_fn_decl(function_declaration);
832             for &param_name in param_names {
833                 visitor.visit_ident(param_name);
834             }
835         }
836         ForeignItemKind::Static(ref typ, _) => visitor.visit_ty(typ),
837         ForeignItemKind::Type => (),
838     }
839 }
840
841 pub fn walk_param_bound<'v, V: Visitor<'v>>(visitor: &mut V, bound: &'v GenericBound<'v>) {
842     match *bound {
843         GenericBound::Trait(ref typ, modifier) => {
844             visitor.visit_poly_trait_ref(typ, modifier);
845         }
846         GenericBound::LangItemTrait(_, span, hir_id, args) => {
847             visitor.visit_id(hir_id);
848             visitor.visit_generic_args(span, args);
849         }
850         GenericBound::Outlives(ref lifetime) => visitor.visit_lifetime(lifetime),
851     }
852 }
853
854 pub fn walk_generic_param<'v, V: Visitor<'v>>(visitor: &mut V, param: &'v GenericParam<'v>) {
855     visitor.visit_id(param.hir_id);
856     match param.name {
857         ParamName::Plain(ident) => visitor.visit_ident(ident),
858         ParamName::Error | ParamName::Fresh => {}
859     }
860     match param.kind {
861         GenericParamKind::Lifetime { .. } => {}
862         GenericParamKind::Type { ref default, .. } => walk_list!(visitor, visit_ty, default),
863         GenericParamKind::Const { ref ty, ref default } => {
864             visitor.visit_ty(ty);
865             if let Some(ref default) = default {
866                 visitor.visit_const_param_default(param.hir_id, default);
867             }
868         }
869     }
870 }
871
872 pub fn walk_const_param_default<'v, V: Visitor<'v>>(visitor: &mut V, ct: &'v AnonConst) {
873     visitor.visit_anon_const(ct)
874 }
875
876 pub fn walk_generics<'v, V: Visitor<'v>>(visitor: &mut V, generics: &'v Generics<'v>) {
877     walk_list!(visitor, visit_generic_param, generics.params);
878     walk_list!(visitor, visit_where_predicate, generics.predicates);
879 }
880
881 pub fn walk_where_predicate<'v, V: Visitor<'v>>(
882     visitor: &mut V,
883     predicate: &'v WherePredicate<'v>,
884 ) {
885     match *predicate {
886         WherePredicate::BoundPredicate(WhereBoundPredicate {
887             ref bounded_ty,
888             bounds,
889             bound_generic_params,
890             ..
891         }) => {
892             visitor.visit_ty(bounded_ty);
893             walk_list!(visitor, visit_param_bound, bounds);
894             walk_list!(visitor, visit_generic_param, bound_generic_params);
895         }
896         WherePredicate::RegionPredicate(WhereRegionPredicate { ref lifetime, bounds, .. }) => {
897             visitor.visit_lifetime(lifetime);
898             walk_list!(visitor, visit_param_bound, bounds);
899         }
900         WherePredicate::EqPredicate(WhereEqPredicate {
901             hir_id, ref lhs_ty, ref rhs_ty, ..
902         }) => {
903             visitor.visit_id(hir_id);
904             visitor.visit_ty(lhs_ty);
905             visitor.visit_ty(rhs_ty);
906         }
907     }
908 }
909
910 pub fn walk_fn_ret_ty<'v, V: Visitor<'v>>(visitor: &mut V, ret_ty: &'v FnRetTy<'v>) {
911     if let FnRetTy::Return(ref output_ty) = *ret_ty {
912         visitor.visit_ty(output_ty)
913     }
914 }
915
916 pub fn walk_fn_decl<'v, V: Visitor<'v>>(visitor: &mut V, function_declaration: &'v FnDecl<'v>) {
917     for ty in function_declaration.inputs {
918         visitor.visit_ty(ty)
919     }
920     walk_fn_ret_ty(visitor, &function_declaration.output)
921 }
922
923 pub fn walk_fn_kind<'v, V: Visitor<'v>>(visitor: &mut V, function_kind: FnKind<'v>) {
924     match function_kind {
925         FnKind::ItemFn(_, generics, ..) => {
926             visitor.visit_generics(generics);
927         }
928         FnKind::Method(..) | FnKind::Closure => {}
929     }
930 }
931
932 pub fn walk_fn<'v, V: Visitor<'v>>(
933     visitor: &mut V,
934     function_kind: FnKind<'v>,
935     function_declaration: &'v FnDecl<'v>,
936     body_id: BodyId,
937     _span: Span,
938     id: HirId,
939 ) {
940     visitor.visit_id(id);
941     visitor.visit_fn_decl(function_declaration);
942     walk_fn_kind(visitor, function_kind);
943     visitor.visit_nested_body(body_id)
944 }
945
946 pub fn walk_trait_item<'v, V: Visitor<'v>>(visitor: &mut V, trait_item: &'v TraitItem<'v>) {
947     visitor.visit_ident(trait_item.ident);
948     visitor.visit_generics(&trait_item.generics);
949     match trait_item.kind {
950         TraitItemKind::Const(ref ty, default) => {
951             visitor.visit_id(trait_item.hir_id());
952             visitor.visit_ty(ty);
953             walk_list!(visitor, visit_nested_body, default);
954         }
955         TraitItemKind::Fn(ref sig, TraitFn::Required(param_names)) => {
956             visitor.visit_id(trait_item.hir_id());
957             visitor.visit_fn_decl(&sig.decl);
958             for &param_name in param_names {
959                 visitor.visit_ident(param_name);
960             }
961         }
962         TraitItemKind::Fn(ref sig, TraitFn::Provided(body_id)) => {
963             visitor.visit_fn(
964                 FnKind::Method(trait_item.ident, sig),
965                 &sig.decl,
966                 body_id,
967                 trait_item.span,
968                 trait_item.hir_id(),
969             );
970         }
971         TraitItemKind::Type(bounds, ref default) => {
972             visitor.visit_id(trait_item.hir_id());
973             walk_list!(visitor, visit_param_bound, bounds);
974             walk_list!(visitor, visit_ty, default);
975         }
976     }
977 }
978
979 pub fn walk_trait_item_ref<'v, V: Visitor<'v>>(visitor: &mut V, trait_item_ref: &'v TraitItemRef) {
980     // N.B., deliberately force a compilation error if/when new fields are added.
981     let TraitItemRef { id, ident, ref kind, span: _, ref defaultness } = *trait_item_ref;
982     visitor.visit_nested_trait_item(id);
983     visitor.visit_ident(ident);
984     visitor.visit_associated_item_kind(kind);
985     visitor.visit_defaultness(defaultness);
986 }
987
988 pub fn walk_impl_item<'v, V: Visitor<'v>>(visitor: &mut V, impl_item: &'v ImplItem<'v>) {
989     // N.B., deliberately force a compilation error if/when new fields are added.
990     let ImplItem { def_id: _, ident, ref generics, ref kind, span: _, vis_span: _ } = *impl_item;
991
992     visitor.visit_ident(ident);
993     visitor.visit_generics(generics);
994     match *kind {
995         ImplItemKind::Const(ref ty, body) => {
996             visitor.visit_id(impl_item.hir_id());
997             visitor.visit_ty(ty);
998             visitor.visit_nested_body(body);
999         }
1000         ImplItemKind::Fn(ref sig, body_id) => {
1001             visitor.visit_fn(
1002                 FnKind::Method(impl_item.ident, sig),
1003                 &sig.decl,
1004                 body_id,
1005                 impl_item.span,
1006                 impl_item.hir_id(),
1007             );
1008         }
1009         ImplItemKind::TyAlias(ref ty) => {
1010             visitor.visit_id(impl_item.hir_id());
1011             visitor.visit_ty(ty);
1012         }
1013     }
1014 }
1015
1016 pub fn walk_foreign_item_ref<'v, V: Visitor<'v>>(
1017     visitor: &mut V,
1018     foreign_item_ref: &'v ForeignItemRef,
1019 ) {
1020     // N.B., deliberately force a compilation error if/when new fields are added.
1021     let ForeignItemRef { id, ident, span: _ } = *foreign_item_ref;
1022     visitor.visit_nested_foreign_item(id);
1023     visitor.visit_ident(ident);
1024 }
1025
1026 pub fn walk_impl_item_ref<'v, V: Visitor<'v>>(visitor: &mut V, impl_item_ref: &'v ImplItemRef) {
1027     // N.B., deliberately force a compilation error if/when new fields are added.
1028     let ImplItemRef { id, ident, ref kind, span: _, ref defaultness, trait_item_def_id: _ } =
1029         *impl_item_ref;
1030     visitor.visit_nested_impl_item(id);
1031     visitor.visit_ident(ident);
1032     visitor.visit_associated_item_kind(kind);
1033     visitor.visit_defaultness(defaultness);
1034 }
1035
1036 pub fn walk_struct_def<'v, V: Visitor<'v>>(
1037     visitor: &mut V,
1038     struct_definition: &'v VariantData<'v>,
1039 ) {
1040     walk_list!(visitor, visit_id, struct_definition.ctor_hir_id());
1041     walk_list!(visitor, visit_field_def, struct_definition.fields());
1042 }
1043
1044 pub fn walk_field_def<'v, V: Visitor<'v>>(visitor: &mut V, field: &'v FieldDef<'v>) {
1045     visitor.visit_id(field.hir_id);
1046     visitor.visit_ident(field.ident);
1047     visitor.visit_ty(&field.ty);
1048 }
1049
1050 pub fn walk_block<'v, V: Visitor<'v>>(visitor: &mut V, block: &'v Block<'v>) {
1051     visitor.visit_id(block.hir_id);
1052     walk_list!(visitor, visit_stmt, block.stmts);
1053     walk_list!(visitor, visit_expr, &block.expr);
1054 }
1055
1056 pub fn walk_stmt<'v, V: Visitor<'v>>(visitor: &mut V, statement: &'v Stmt<'v>) {
1057     visitor.visit_id(statement.hir_id);
1058     match statement.kind {
1059         StmtKind::Local(ref local) => visitor.visit_local(local),
1060         StmtKind::Item(item) => visitor.visit_nested_item(item),
1061         StmtKind::Expr(ref expression) | StmtKind::Semi(ref expression) => {
1062             visitor.visit_expr(expression)
1063         }
1064     }
1065 }
1066
1067 pub fn walk_array_len<'v, V: Visitor<'v>>(visitor: &mut V, len: &'v ArrayLen) {
1068     match len {
1069         &ArrayLen::Infer(hir_id, _span) => visitor.visit_id(hir_id),
1070         ArrayLen::Body(c) => visitor.visit_anon_const(c),
1071     }
1072 }
1073
1074 pub fn walk_anon_const<'v, V: Visitor<'v>>(visitor: &mut V, constant: &'v AnonConst) {
1075     visitor.visit_id(constant.hir_id);
1076     visitor.visit_nested_body(constant.body);
1077 }
1078
1079 pub fn walk_let_expr<'v, V: Visitor<'v>>(visitor: &mut V, let_expr: &'v Let<'v>) {
1080     // match the visit order in walk_local
1081     visitor.visit_expr(let_expr.init);
1082     visitor.visit_id(let_expr.hir_id);
1083     visitor.visit_pat(let_expr.pat);
1084     walk_list!(visitor, visit_ty, let_expr.ty);
1085 }
1086
1087 pub fn walk_expr<'v, V: Visitor<'v>>(visitor: &mut V, expression: &'v Expr<'v>) {
1088     visitor.visit_id(expression.hir_id);
1089     match expression.kind {
1090         ExprKind::Box(ref subexpression) => visitor.visit_expr(subexpression),
1091         ExprKind::Array(subexpressions) => {
1092             walk_list!(visitor, visit_expr, subexpressions);
1093         }
1094         ExprKind::ConstBlock(ref anon_const) => visitor.visit_anon_const(anon_const),
1095         ExprKind::Repeat(ref element, ref count) => {
1096             visitor.visit_expr(element);
1097             visitor.visit_array_length(count)
1098         }
1099         ExprKind::Struct(ref qpath, fields, ref optional_base) => {
1100             visitor.visit_qpath(qpath, expression.hir_id, expression.span);
1101             for field in fields {
1102                 visitor.visit_id(field.hir_id);
1103                 visitor.visit_ident(field.ident);
1104                 visitor.visit_expr(&field.expr)
1105             }
1106             walk_list!(visitor, visit_expr, optional_base);
1107         }
1108         ExprKind::Tup(subexpressions) => {
1109             walk_list!(visitor, visit_expr, subexpressions);
1110         }
1111         ExprKind::Call(ref callee_expression, arguments) => {
1112             visitor.visit_expr(callee_expression);
1113             walk_list!(visitor, visit_expr, arguments);
1114         }
1115         ExprKind::MethodCall(ref segment, arguments, _) => {
1116             visitor.visit_path_segment(expression.span, segment);
1117             walk_list!(visitor, visit_expr, arguments);
1118         }
1119         ExprKind::Binary(_, ref left_expression, ref right_expression) => {
1120             visitor.visit_expr(left_expression);
1121             visitor.visit_expr(right_expression)
1122         }
1123         ExprKind::AddrOf(_, _, ref subexpression) | ExprKind::Unary(_, ref subexpression) => {
1124             visitor.visit_expr(subexpression)
1125         }
1126         ExprKind::Cast(ref subexpression, ref typ) | ExprKind::Type(ref subexpression, ref typ) => {
1127             visitor.visit_expr(subexpression);
1128             visitor.visit_ty(typ)
1129         }
1130         ExprKind::DropTemps(ref subexpression) => {
1131             visitor.visit_expr(subexpression);
1132         }
1133         ExprKind::Let(ref let_expr) => visitor.visit_let_expr(let_expr),
1134         ExprKind::If(ref cond, ref then, ref else_opt) => {
1135             visitor.visit_expr(cond);
1136             visitor.visit_expr(then);
1137             walk_list!(visitor, visit_expr, else_opt);
1138         }
1139         ExprKind::Loop(ref block, ref opt_label, _, _) => {
1140             walk_list!(visitor, visit_label, opt_label);
1141             visitor.visit_block(block);
1142         }
1143         ExprKind::Match(ref subexpression, arms, _) => {
1144             visitor.visit_expr(subexpression);
1145             walk_list!(visitor, visit_arm, arms);
1146         }
1147         ExprKind::Closure {
1148             bound_generic_params,
1149             ref fn_decl,
1150             body,
1151             capture_clause: _,
1152             fn_decl_span: _,
1153             movability: _,
1154         } => {
1155             walk_list!(visitor, visit_generic_param, bound_generic_params);
1156             visitor.visit_fn(FnKind::Closure, fn_decl, body, expression.span, expression.hir_id)
1157         }
1158         ExprKind::Block(ref block, ref opt_label) => {
1159             walk_list!(visitor, visit_label, opt_label);
1160             visitor.visit_block(block);
1161         }
1162         ExprKind::Assign(ref lhs, ref rhs, _) => {
1163             visitor.visit_expr(rhs);
1164             visitor.visit_expr(lhs)
1165         }
1166         ExprKind::AssignOp(_, ref left_expression, ref right_expression) => {
1167             visitor.visit_expr(right_expression);
1168             visitor.visit_expr(left_expression);
1169         }
1170         ExprKind::Field(ref subexpression, ident) => {
1171             visitor.visit_expr(subexpression);
1172             visitor.visit_ident(ident);
1173         }
1174         ExprKind::Index(ref main_expression, ref index_expression) => {
1175             visitor.visit_expr(main_expression);
1176             visitor.visit_expr(index_expression)
1177         }
1178         ExprKind::Path(ref qpath) => {
1179             visitor.visit_qpath(qpath, expression.hir_id, expression.span);
1180         }
1181         ExprKind::Break(ref destination, ref opt_expr) => {
1182             walk_list!(visitor, visit_label, &destination.label);
1183             walk_list!(visitor, visit_expr, opt_expr);
1184         }
1185         ExprKind::Continue(ref destination) => {
1186             walk_list!(visitor, visit_label, &destination.label);
1187         }
1188         ExprKind::Ret(ref optional_expression) => {
1189             walk_list!(visitor, visit_expr, optional_expression);
1190         }
1191         ExprKind::InlineAsm(ref asm) => {
1192             visitor.visit_inline_asm(asm, expression.hir_id);
1193         }
1194         ExprKind::Yield(ref subexpression, _) => {
1195             visitor.visit_expr(subexpression);
1196         }
1197         ExprKind::Lit(_) | ExprKind::Err => {}
1198     }
1199 }
1200
1201 pub fn walk_arm<'v, V: Visitor<'v>>(visitor: &mut V, arm: &'v Arm<'v>) {
1202     visitor.visit_id(arm.hir_id);
1203     visitor.visit_pat(&arm.pat);
1204     if let Some(ref g) = arm.guard {
1205         match g {
1206             Guard::If(ref e) => visitor.visit_expr(e),
1207             Guard::IfLet(ref l) => {
1208                 visitor.visit_let_expr(l);
1209             }
1210         }
1211     }
1212     visitor.visit_expr(&arm.body);
1213 }
1214
1215 pub fn walk_associated_item_kind<'v, V: Visitor<'v>>(_: &mut V, _: &'v AssocItemKind) {
1216     // No visitable content here: this fn exists so you can call it if
1217     // the right thing to do, should content be added in the future,
1218     // would be to walk it.
1219 }
1220
1221 pub fn walk_defaultness<'v, V: Visitor<'v>>(_: &mut V, _: &'v Defaultness) {
1222     // No visitable content here: this fn exists so you can call it if
1223     // the right thing to do, should content be added in the future,
1224     // would be to walk it.
1225 }