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