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