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