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