rustc_hir/
intravisit.rs

1//! HIR walker for walking the contents of nodes.
2//!
3//! Here are the three available patterns for the visitor strategy,
4//! in roughly the order of desirability:
5//!
6//! 1. **Shallow visit**: Get a simple callback for every item (or item-like thing) in the HIR.
7//!    - Example: find all items with a `#[foo]` attribute on them.
8//!    - How: Use the `hir_crate_items` or `hir_module_items` query to traverse over item-like ids
9//!       (ItemId, TraitItemId, etc.) and use tcx.def_kind and `tcx.hir_item*(id)` to filter and
10//!       access actual item-like thing, respectively.
11//!    - Pro: Efficient; just walks the lists of item ids and gives users control whether to access
12//!       the hir_owners themselves or not.
13//!    - Con: Don't get information about nesting
14//!    - Con: Don't have methods for specific bits of HIR, like "on
15//!      every expr, do this".
16//! 2. **Deep visit**: Want to scan for specific kinds of HIR nodes within
17//!    an item, but don't care about how item-like things are nested
18//!    within one another.
19//!    - Example: Examine each expression to look for its type and do some check or other.
20//!    - How: Implement `intravisit::Visitor` and override the `NestedFilter` type to
21//!      `nested_filter::OnlyBodies` (and implement `maybe_tcx`), and use
22//!      `tcx.hir_visit_all_item_likes_in_crate(&mut visitor)`. Within your
23//!      `intravisit::Visitor` impl, implement methods like `visit_expr()` (don't forget to invoke
24//!      `intravisit::walk_expr()` to keep walking the subparts).
25//!    - Pro: Visitor methods for any kind of HIR node, not just item-like things.
26//!    - Pro: Integrates well into dependency tracking.
27//!    - Con: Don't get information about nesting between items
28//! 3. **Nested visit**: Want to visit the whole HIR and you care about the nesting between
29//!    item-like things.
30//!    - Example: Lifetime resolution, which wants to bring lifetimes declared on the
31//!      impl into scope while visiting the impl-items, and then back out again.
32//!    - How: Implement `intravisit::Visitor` and override the `NestedFilter` type to
33//!      `nested_filter::All` (and implement `maybe_tcx`). Walk your crate with
34//!      `tcx.hir_walk_toplevel_module(visitor)`.
35//!    - Pro: Visitor methods for any kind of HIR node, not just item-like things.
36//!    - Pro: Preserves nesting information
37//!    - Con: Does not integrate well into dependency tracking.
38//!
39//! If you have decided to use this visitor, here are some general
40//! notes on how to do so:
41//!
42//! Each overridden visit method has full control over what
43//! happens with its node, it can do its own traversal of the node's children,
44//! call `intravisit::walk_*` to apply the default traversal algorithm, or prevent
45//! deeper traversal by doing nothing.
46//!
47//! When visiting the HIR, the contents of nested items are NOT visited
48//! by default. This is different from the AST visitor, which does a deep walk.
49//! Hence this module is called `intravisit`; see the method `visit_nested_item`
50//! for more details.
51//!
52//! Note: it is an important invariant that the default visitor walks
53//! the body of a function in "execution order" - more concretely, if
54//! we consider the reverse post-order (RPO) of the CFG implied by the HIR,
55//! then a pre-order traversal of the HIR is consistent with the CFG RPO
56//! on the *initial CFG point* of each HIR node, while a post-order traversal
57//! of the HIR is consistent with the CFG RPO on each *final CFG point* of
58//! each CFG node.
59//!
60//! One thing that follows is that if HIR node A always starts/ends executing
61//! before HIR node B, then A appears in traversal pre/postorder before B,
62//! respectively. (This follows from RPO respecting CFG domination).
63//!
64//! This order consistency is required in a few places in rustc, for
65//! example coroutine inference, and possibly also HIR borrowck.
66
67use rustc_ast::Label;
68use rustc_ast::visit::{VisitorResult, try_visit, visit_opt, walk_list};
69use rustc_span::def_id::LocalDefId;
70use rustc_span::{Ident, Span, Symbol};
71
72use crate::hir::*;
73
74pub trait IntoVisitor<'hir> {
75    type Visitor: Visitor<'hir>;
76    fn into_visitor(&self) -> Self::Visitor;
77}
78
79#[derive(Copy, Clone, Debug)]
80pub enum FnKind<'a> {
81    /// `#[xxx] pub async/const/extern "Abi" fn foo()`
82    ItemFn(Ident, &'a Generics<'a>, FnHeader),
83
84    /// `fn foo(&self)`
85    Method(Ident, &'a FnSig<'a>),
86
87    /// `|x, y| {}`
88    Closure,
89}
90
91impl<'a> FnKind<'a> {
92    pub fn header(&self) -> Option<&FnHeader> {
93        match *self {
94            FnKind::ItemFn(_, _, ref header) => Some(header),
95            FnKind::Method(_, ref sig) => Some(&sig.header),
96            FnKind::Closure => None,
97        }
98    }
99
100    pub fn constness(self) -> Constness {
101        self.header().map_or(Constness::NotConst, |header| header.constness)
102    }
103
104    pub fn asyncness(self) -> IsAsync {
105        self.header().map_or(IsAsync::NotAsync, |header| header.asyncness)
106    }
107}
108
109/// HIR things retrievable from `TyCtxt`, avoiding an explicit dependence on
110/// `TyCtxt`. The only impls are for `!` (where these functions are never
111/// called) and `TyCtxt` (in `rustc_middle`).
112pub trait HirTyCtxt<'hir> {
113    /// Retrieves the `Node` corresponding to `id`.
114    fn hir_node(&self, hir_id: HirId) -> Node<'hir>;
115    fn hir_body(&self, id: BodyId) -> &'hir Body<'hir>;
116    fn hir_item(&self, id: ItemId) -> &'hir Item<'hir>;
117    fn hir_trait_item(&self, id: TraitItemId) -> &'hir TraitItem<'hir>;
118    fn hir_impl_item(&self, id: ImplItemId) -> &'hir ImplItem<'hir>;
119    fn hir_foreign_item(&self, id: ForeignItemId) -> &'hir ForeignItem<'hir>;
120}
121
122// Used when no tcx is actually available, forcing manual implementation of nested visitors.
123impl<'hir> HirTyCtxt<'hir> for ! {
124    fn hir_node(&self, _: HirId) -> Node<'hir> {
125        unreachable!();
126    }
127    fn hir_body(&self, _: BodyId) -> &'hir Body<'hir> {
128        unreachable!();
129    }
130    fn hir_item(&self, _: ItemId) -> &'hir Item<'hir> {
131        unreachable!();
132    }
133    fn hir_trait_item(&self, _: TraitItemId) -> &'hir TraitItem<'hir> {
134        unreachable!();
135    }
136    fn hir_impl_item(&self, _: ImplItemId) -> &'hir ImplItem<'hir> {
137        unreachable!();
138    }
139    fn hir_foreign_item(&self, _: ForeignItemId) -> &'hir ForeignItem<'hir> {
140        unreachable!();
141    }
142}
143
144pub mod nested_filter {
145    use super::HirTyCtxt;
146
147    /// Specifies what nested things a visitor wants to visit. By "nested
148    /// things", we are referring to bits of HIR that are not directly embedded
149    /// within one another but rather indirectly, through a table in the crate.
150    /// This is done to control dependencies during incremental compilation: the
151    /// non-inline bits of HIR can be tracked and hashed separately.
152    ///
153    /// The most common choice is `OnlyBodies`, which will cause the visitor to
154    /// visit fn bodies for fns that it encounters, and closure bodies, but
155    /// skip over nested item-like things.
156    ///
157    /// See the comments at [`rustc_hir::intravisit`] for more details on the overall
158    /// visit strategy.
159    pub trait NestedFilter<'hir> {
160        type MaybeTyCtxt: HirTyCtxt<'hir>;
161
162        /// Whether the visitor visits nested "item-like" things.
163        /// E.g., item, impl-item.
164        const INTER: bool;
165        /// Whether the visitor visits "intra item-like" things.
166        /// E.g., function body, closure, `AnonConst`
167        const INTRA: bool;
168    }
169
170    /// Do not visit any nested things. When you add a new
171    /// "non-nested" thing, you will want to audit such uses to see if
172    /// they remain valid.
173    ///
174    /// Use this if you are only walking some particular kind of tree
175    /// (i.e., a type, or fn signature) and you don't want to thread a
176    /// `tcx` around.
177    pub struct None(());
178    impl NestedFilter<'_> for None {
179        type MaybeTyCtxt = !;
180        const INTER: bool = false;
181        const INTRA: bool = false;
182    }
183}
184
185use nested_filter::NestedFilter;
186
187/// Each method of the Visitor trait is a hook to be potentially
188/// overridden. Each method's default implementation recursively visits
189/// the substructure of the input via the corresponding `walk` method;
190/// e.g., the `visit_mod` method by default calls `intravisit::walk_mod`.
191///
192/// Note that this visitor does NOT visit nested items by default
193/// (this is why the module is called `intravisit`, to distinguish it
194/// from the AST's `visit` module, which acts differently). If you
195/// simply want to visit all items in the crate in some order, you
196/// should call `tcx.hir_visit_all_item_likes_in_crate`. Otherwise, see the comment
197/// on `visit_nested_item` for details on how to visit nested items.
198///
199/// If you want to ensure that your code handles every variant
200/// explicitly, you need to override each method. (And you also need
201/// to monitor future changes to `Visitor` in case a new method with a
202/// new default implementation gets introduced.)
203///
204/// Every `walk_*` method uses deconstruction to access fields of structs and
205/// enums. This will result in a compile error if a field is added, which makes
206/// it more likely the appropriate visit call will be added for it.
207pub trait Visitor<'v>: Sized {
208    // This type should not be overridden, it exists for convenient usage as `Self::MaybeTyCtxt`.
209    type MaybeTyCtxt: HirTyCtxt<'v> = <Self::NestedFilter as NestedFilter<'v>>::MaybeTyCtxt;
210
211    ///////////////////////////////////////////////////////////////////////////
212    // Nested items.
213
214    /// Override this type to control which nested HIR are visited; see
215    /// [`NestedFilter`] for details. If you override this type, you
216    /// must also override [`maybe_tcx`](Self::maybe_tcx).
217    ///
218    /// **If for some reason you want the nested behavior, but don't
219    /// have a `tcx` at your disposal:** then override the
220    /// `visit_nested_XXX` methods. If a new `visit_nested_XXX` variant is
221    /// added in the future, it will cause a panic which can be detected
222    /// and fixed appropriately.
223    type NestedFilter: NestedFilter<'v> = nested_filter::None;
224
225    /// The result type of the `visit_*` methods. Can be either `()`,
226    /// or `ControlFlow<T>`.
227    type Result: VisitorResult = ();
228
229    /// If `type NestedFilter` is set to visit nested items, this method
230    /// must also be overridden to provide a map to retrieve nested items.
231    fn maybe_tcx(&mut self) -> Self::MaybeTyCtxt {
232        panic!(
233            "maybe_tcx must be implemented or consider using \
234            `type NestedFilter = nested_filter::None` (the default)"
235        );
236    }
237
238    /// Invoked when a nested item is encountered. By default, when
239    /// `Self::NestedFilter` is `nested_filter::None`, this method does
240    /// nothing. **You probably don't want to override this method** --
241    /// instead, override [`Self::NestedFilter`] or use the "shallow" or
242    /// "deep" visit patterns described at
243    /// [`rustc_hir::intravisit`]. The only reason to override
244    /// this method is if you want a nested pattern but cannot supply a
245    /// `TyCtxt`; see `maybe_tcx` for advice.
246    fn visit_nested_item(&mut self, id: ItemId) -> Self::Result {
247        if Self::NestedFilter::INTER {
248            let item = self.maybe_tcx().hir_item(id);
249            try_visit!(self.visit_item(item));
250        }
251        Self::Result::output()
252    }
253
254    /// Like `visit_nested_item()`, but for trait items. See
255    /// `visit_nested_item()` for advice on when to override this
256    /// method.
257    fn visit_nested_trait_item(&mut self, id: TraitItemId) -> Self::Result {
258        if Self::NestedFilter::INTER {
259            let item = self.maybe_tcx().hir_trait_item(id);
260            try_visit!(self.visit_trait_item(item));
261        }
262        Self::Result::output()
263    }
264
265    /// Like `visit_nested_item()`, but for impl items. See
266    /// `visit_nested_item()` for advice on when to override this
267    /// method.
268    fn visit_nested_impl_item(&mut self, id: ImplItemId) -> Self::Result {
269        if Self::NestedFilter::INTER {
270            let item = self.maybe_tcx().hir_impl_item(id);
271            try_visit!(self.visit_impl_item(item));
272        }
273        Self::Result::output()
274    }
275
276    /// Like `visit_nested_item()`, but for foreign items. See
277    /// `visit_nested_item()` for advice on when to override this
278    /// method.
279    fn visit_nested_foreign_item(&mut self, id: ForeignItemId) -> Self::Result {
280        if Self::NestedFilter::INTER {
281            let item = self.maybe_tcx().hir_foreign_item(id);
282            try_visit!(self.visit_foreign_item(item));
283        }
284        Self::Result::output()
285    }
286
287    /// Invoked to visit the body of a function, method or closure. Like
288    /// `visit_nested_item`, does nothing by default unless you override
289    /// `Self::NestedFilter`.
290    fn visit_nested_body(&mut self, id: BodyId) -> Self::Result {
291        if Self::NestedFilter::INTRA {
292            let body = self.maybe_tcx().hir_body(id);
293            try_visit!(self.visit_body(body));
294        }
295        Self::Result::output()
296    }
297
298    fn visit_param(&mut self, param: &'v Param<'v>) -> Self::Result {
299        walk_param(self, param)
300    }
301
302    /// Visits the top-level item and (optionally) nested items / impl items. See
303    /// `visit_nested_item` for details.
304    fn visit_item(&mut self, i: &'v Item<'v>) -> Self::Result {
305        walk_item(self, i)
306    }
307
308    fn visit_body(&mut self, b: &Body<'v>) -> Self::Result {
309        walk_body(self, b)
310    }
311
312    ///////////////////////////////////////////////////////////////////////////
313
314    fn visit_id(&mut self, _hir_id: HirId) -> Self::Result {
315        Self::Result::output()
316    }
317    fn visit_name(&mut self, _name: Symbol) -> Self::Result {
318        Self::Result::output()
319    }
320    fn visit_ident(&mut self, ident: Ident) -> Self::Result {
321        walk_ident(self, ident)
322    }
323    fn visit_mod(&mut self, m: &'v Mod<'v>, _s: Span, _n: HirId) -> Self::Result {
324        walk_mod(self, m)
325    }
326    fn visit_foreign_item(&mut self, i: &'v ForeignItem<'v>) -> Self::Result {
327        walk_foreign_item(self, i)
328    }
329    fn visit_local(&mut self, l: &'v LetStmt<'v>) -> Self::Result {
330        walk_local(self, l)
331    }
332    fn visit_block(&mut self, b: &'v Block<'v>) -> Self::Result {
333        walk_block(self, b)
334    }
335    fn visit_stmt(&mut self, s: &'v Stmt<'v>) -> Self::Result {
336        walk_stmt(self, s)
337    }
338    fn visit_arm(&mut self, a: &'v Arm<'v>) -> Self::Result {
339        walk_arm(self, a)
340    }
341    fn visit_pat(&mut self, p: &'v Pat<'v>) -> Self::Result {
342        walk_pat(self, p)
343    }
344    fn visit_pat_field(&mut self, f: &'v PatField<'v>) -> Self::Result {
345        walk_pat_field(self, f)
346    }
347    fn visit_pat_expr(&mut self, expr: &'v PatExpr<'v>) -> Self::Result {
348        walk_pat_expr(self, expr)
349    }
350    fn visit_lit(&mut self, _hir_id: HirId, _lit: Lit, _negated: bool) -> Self::Result {
351        Self::Result::output()
352    }
353    fn visit_anon_const(&mut self, c: &'v AnonConst) -> Self::Result {
354        walk_anon_const(self, c)
355    }
356    fn visit_inline_const(&mut self, c: &'v ConstBlock) -> Self::Result {
357        walk_inline_const(self, c)
358    }
359
360    fn visit_generic_arg(&mut self, generic_arg: &'v GenericArg<'v>) -> Self::Result {
361        walk_generic_arg(self, generic_arg)
362    }
363
364    /// All types are treated as ambiguous types for the purposes of hir visiting in
365    /// order to ensure that visitors can handle infer vars without it being too error-prone.
366    ///
367    /// See the doc comments on [`Ty`] for an explanation of what it means for a type to be
368    /// ambiguous.
369    ///
370    /// The [`Visitor::visit_infer`] method should be overridden in order to handle infer vars.
371    fn visit_ty(&mut self, t: &'v Ty<'v, AmbigArg>) -> Self::Result {
372        walk_ty(self, t)
373    }
374
375    /// All consts are treated as ambiguous consts for the purposes of hir visiting in
376    /// order to ensure that visitors can handle infer vars without it being too error-prone.
377    ///
378    /// See the doc comments on [`ConstArg`] for an explanation of what it means for a const to be
379    /// ambiguous.
380    ///
381    /// The [`Visitor::visit_infer`] method should be overridden in order to handle infer vars.
382    fn visit_const_arg(&mut self, c: &'v ConstArg<'v, AmbigArg>) -> Self::Result {
383        walk_ambig_const_arg(self, c)
384    }
385
386    #[allow(unused_variables)]
387    fn visit_infer(&mut self, inf_id: HirId, inf_span: Span, kind: InferKind<'v>) -> Self::Result {
388        self.visit_id(inf_id)
389    }
390
391    fn visit_lifetime(&mut self, lifetime: &'v Lifetime) -> Self::Result {
392        walk_lifetime(self, lifetime)
393    }
394
395    fn visit_expr(&mut self, ex: &'v Expr<'v>) -> Self::Result {
396        walk_expr(self, ex)
397    }
398    fn visit_expr_field(&mut self, field: &'v ExprField<'v>) -> Self::Result {
399        walk_expr_field(self, field)
400    }
401    fn visit_pattern_type_pattern(&mut self, p: &'v TyPat<'v>) -> Self::Result {
402        walk_ty_pat(self, p)
403    }
404    fn visit_generic_param(&mut self, p: &'v GenericParam<'v>) -> Self::Result {
405        walk_generic_param(self, p)
406    }
407    fn visit_const_param_default(&mut self, _param: HirId, ct: &'v ConstArg<'v>) -> Self::Result {
408        walk_const_param_default(self, ct)
409    }
410    fn visit_generics(&mut self, g: &'v Generics<'v>) -> Self::Result {
411        walk_generics(self, g)
412    }
413    fn visit_where_predicate(&mut self, predicate: &'v WherePredicate<'v>) -> Self::Result {
414        walk_where_predicate(self, predicate)
415    }
416    fn visit_fn_ret_ty(&mut self, ret_ty: &'v FnRetTy<'v>) -> Self::Result {
417        walk_fn_ret_ty(self, ret_ty)
418    }
419    fn visit_fn_decl(&mut self, fd: &'v FnDecl<'v>) -> Self::Result {
420        walk_fn_decl(self, fd)
421    }
422    fn visit_fn(
423        &mut self,
424        fk: FnKind<'v>,
425        fd: &'v FnDecl<'v>,
426        b: BodyId,
427        _: Span,
428        id: LocalDefId,
429    ) -> Self::Result {
430        walk_fn(self, fk, fd, b, id)
431    }
432    fn visit_use(&mut self, path: &'v UsePath<'v>, hir_id: HirId) -> Self::Result {
433        walk_use(self, path, hir_id)
434    }
435    fn visit_trait_item(&mut self, ti: &'v TraitItem<'v>) -> Self::Result {
436        walk_trait_item(self, ti)
437    }
438    fn visit_trait_item_ref(&mut self, ii: &'v TraitItemRef) -> Self::Result {
439        walk_trait_item_ref(self, ii)
440    }
441    fn visit_impl_item(&mut self, ii: &'v ImplItem<'v>) -> Self::Result {
442        walk_impl_item(self, ii)
443    }
444    fn visit_foreign_item_ref(&mut self, ii: &'v ForeignItemRef) -> Self::Result {
445        walk_foreign_item_ref(self, ii)
446    }
447    fn visit_impl_item_ref(&mut self, ii: &'v ImplItemRef) -> Self::Result {
448        walk_impl_item_ref(self, ii)
449    }
450    fn visit_trait_ref(&mut self, t: &'v TraitRef<'v>) -> Self::Result {
451        walk_trait_ref(self, t)
452    }
453    fn visit_param_bound(&mut self, bounds: &'v GenericBound<'v>) -> Self::Result {
454        walk_param_bound(self, bounds)
455    }
456    fn visit_precise_capturing_arg(&mut self, arg: &'v PreciseCapturingArg<'v>) -> Self::Result {
457        walk_precise_capturing_arg(self, arg)
458    }
459    fn visit_poly_trait_ref(&mut self, t: &'v PolyTraitRef<'v>) -> Self::Result {
460        walk_poly_trait_ref(self, t)
461    }
462    fn visit_opaque_ty(&mut self, opaque: &'v OpaqueTy<'v>) -> Self::Result {
463        walk_opaque_ty(self, opaque)
464    }
465    fn visit_variant_data(&mut self, s: &'v VariantData<'v>) -> Self::Result {
466        walk_struct_def(self, s)
467    }
468    fn visit_field_def(&mut self, s: &'v FieldDef<'v>) -> Self::Result {
469        walk_field_def(self, s)
470    }
471    fn visit_enum_def(&mut self, enum_definition: &'v EnumDef<'v>) -> Self::Result {
472        walk_enum_def(self, enum_definition)
473    }
474    fn visit_variant(&mut self, v: &'v Variant<'v>) -> Self::Result {
475        walk_variant(self, v)
476    }
477    fn visit_label(&mut self, label: &'v Label) -> Self::Result {
478        walk_label(self, label)
479    }
480    // The span is that of the surrounding type/pattern/expr/whatever.
481    fn visit_qpath(&mut self, qpath: &'v QPath<'v>, id: HirId, _span: Span) -> Self::Result {
482        walk_qpath(self, qpath, id)
483    }
484    fn visit_path(&mut self, path: &Path<'v>, _id: HirId) -> Self::Result {
485        walk_path(self, path)
486    }
487    fn visit_path_segment(&mut self, path_segment: &'v PathSegment<'v>) -> Self::Result {
488        walk_path_segment(self, path_segment)
489    }
490    fn visit_generic_args(&mut self, generic_args: &'v GenericArgs<'v>) -> Self::Result {
491        walk_generic_args(self, generic_args)
492    }
493    fn visit_assoc_item_constraint(
494        &mut self,
495        constraint: &'v AssocItemConstraint<'v>,
496    ) -> Self::Result {
497        walk_assoc_item_constraint(self, constraint)
498    }
499    fn visit_attribute(&mut self, _attr: &'v Attribute) -> Self::Result {
500        Self::Result::output()
501    }
502    fn visit_associated_item_kind(&mut self, kind: &'v AssocItemKind) -> Self::Result {
503        walk_associated_item_kind(self, kind)
504    }
505    fn visit_defaultness(&mut self, defaultness: &'v Defaultness) -> Self::Result {
506        walk_defaultness(self, defaultness)
507    }
508    fn visit_inline_asm(&mut self, asm: &'v InlineAsm<'v>, id: HirId) -> Self::Result {
509        walk_inline_asm(self, asm, id)
510    }
511}
512
513pub trait VisitorExt<'v>: Visitor<'v> {
514    /// Extension trait method to visit types in unambiguous positions, this is not
515    /// directly on the [`Visitor`] trait as this method should never be overridden.
516    ///
517    /// Named `visit_ty_unambig` instead of `visit_unambig_ty` to aid in discovery
518    /// by IDes when `v.visit_ty` is written.
519    fn visit_ty_unambig(&mut self, t: &'v Ty<'v>) -> Self::Result {
520        walk_unambig_ty(self, t)
521    }
522    /// Extension trait method to visit consts in unambiguous positions, this is not
523    /// directly on the [`Visitor`] trait as this method should never be overridden.
524    ///
525    /// Named `visit_const_arg_unambig` instead of `visit_unambig_const_arg` to aid in
526    /// discovery by IDes when `v.visit_const_arg` is written.
527    fn visit_const_arg_unambig(&mut self, c: &'v ConstArg<'v>) -> Self::Result {
528        walk_const_arg(self, c)
529    }
530}
531impl<'v, V: Visitor<'v>> VisitorExt<'v> for V {}
532
533pub fn walk_param<'v, V: Visitor<'v>>(visitor: &mut V, param: &'v Param<'v>) -> V::Result {
534    let Param { hir_id, pat, ty_span: _, span: _ } = param;
535    try_visit!(visitor.visit_id(*hir_id));
536    visitor.visit_pat(pat)
537}
538
539pub fn walk_item<'v, V: Visitor<'v>>(visitor: &mut V, item: &'v Item<'v>) -> V::Result {
540    let Item { owner_id: _, kind, span: _, vis_span: _, has_delayed_lints: _ } = item;
541    try_visit!(visitor.visit_id(item.hir_id()));
542    match *kind {
543        ItemKind::ExternCrate(orig_name, ident) => {
544            visit_opt!(visitor, visit_name, orig_name);
545            try_visit!(visitor.visit_ident(ident));
546        }
547        ItemKind::Use(ref path, kind) => {
548            try_visit!(visitor.visit_use(path, item.hir_id()));
549            match kind {
550                UseKind::Single(ident) => try_visit!(visitor.visit_ident(ident)),
551                UseKind::Glob | UseKind::ListStem => {}
552            }
553        }
554        ItemKind::Static(_, ident, ref typ, body) => {
555            try_visit!(visitor.visit_ident(ident));
556            try_visit!(visitor.visit_ty_unambig(typ));
557            try_visit!(visitor.visit_nested_body(body));
558        }
559        ItemKind::Const(ident, ref generics, ref typ, body) => {
560            try_visit!(visitor.visit_ident(ident));
561            try_visit!(visitor.visit_generics(generics));
562            try_visit!(visitor.visit_ty_unambig(typ));
563            try_visit!(visitor.visit_nested_body(body));
564        }
565        ItemKind::Fn { ident, sig, generics, body: body_id, .. } => {
566            try_visit!(visitor.visit_ident(ident));
567            try_visit!(visitor.visit_fn(
568                FnKind::ItemFn(ident, generics, sig.header),
569                sig.decl,
570                body_id,
571                item.span,
572                item.owner_id.def_id,
573            ));
574        }
575        ItemKind::Macro(ident, _def, _kind) => {
576            try_visit!(visitor.visit_ident(ident));
577        }
578        ItemKind::Mod(ident, ref module) => {
579            try_visit!(visitor.visit_ident(ident));
580            try_visit!(visitor.visit_mod(module, item.span, item.hir_id()));
581        }
582        ItemKind::ForeignMod { abi: _, items } => {
583            walk_list!(visitor, visit_foreign_item_ref, items);
584        }
585        ItemKind::GlobalAsm { asm: _, fake_body } => {
586            // Visit the fake body, which contains the asm statement.
587            // Therefore we should not visit the asm statement again
588            // outside of the body, or some visitors won't have their
589            // typeck results set correctly.
590            try_visit!(visitor.visit_nested_body(fake_body));
591        }
592        ItemKind::TyAlias(ident, ref generics, ref ty) => {
593            try_visit!(visitor.visit_ident(ident));
594            try_visit!(visitor.visit_generics(generics));
595            try_visit!(visitor.visit_ty_unambig(ty));
596        }
597        ItemKind::Enum(ident, ref generics, ref enum_definition) => {
598            try_visit!(visitor.visit_ident(ident));
599            try_visit!(visitor.visit_generics(generics));
600            try_visit!(visitor.visit_enum_def(enum_definition));
601        }
602        ItemKind::Impl(Impl {
603            constness: _,
604            safety: _,
605            defaultness: _,
606            polarity: _,
607            defaultness_span: _,
608            generics,
609            of_trait,
610            self_ty,
611            items,
612        }) => {
613            try_visit!(visitor.visit_generics(generics));
614            visit_opt!(visitor, visit_trait_ref, of_trait);
615            try_visit!(visitor.visit_ty_unambig(self_ty));
616            walk_list!(visitor, visit_impl_item_ref, *items);
617        }
618        ItemKind::Struct(ident, ref generics, ref struct_definition)
619        | ItemKind::Union(ident, ref generics, ref struct_definition) => {
620            try_visit!(visitor.visit_ident(ident));
621            try_visit!(visitor.visit_generics(generics));
622            try_visit!(visitor.visit_variant_data(struct_definition));
623        }
624        ItemKind::Trait(_is_auto, _safety, ident, ref generics, bounds, trait_item_refs) => {
625            try_visit!(visitor.visit_ident(ident));
626            try_visit!(visitor.visit_generics(generics));
627            walk_list!(visitor, visit_param_bound, bounds);
628            walk_list!(visitor, visit_trait_item_ref, trait_item_refs);
629        }
630        ItemKind::TraitAlias(ident, ref generics, bounds) => {
631            try_visit!(visitor.visit_ident(ident));
632            try_visit!(visitor.visit_generics(generics));
633            walk_list!(visitor, visit_param_bound, bounds);
634        }
635    }
636    V::Result::output()
637}
638
639pub fn walk_body<'v, V: Visitor<'v>>(visitor: &mut V, body: &Body<'v>) -> V::Result {
640    let Body { params, value } = body;
641    walk_list!(visitor, visit_param, *params);
642    visitor.visit_expr(*value)
643}
644
645pub fn walk_ident<'v, V: Visitor<'v>>(visitor: &mut V, ident: Ident) -> V::Result {
646    visitor.visit_name(ident.name)
647}
648
649pub fn walk_mod<'v, V: Visitor<'v>>(visitor: &mut V, module: &'v Mod<'v>) -> V::Result {
650    let Mod { spans: _, item_ids } = module;
651    walk_list!(visitor, visit_nested_item, item_ids.iter().copied());
652    V::Result::output()
653}
654
655pub fn walk_foreign_item<'v, V: Visitor<'v>>(
656    visitor: &mut V,
657    foreign_item: &'v ForeignItem<'v>,
658) -> V::Result {
659    let ForeignItem { ident, kind, owner_id: _, span: _, vis_span: _, has_delayed_lints: _ } =
660        foreign_item;
661    try_visit!(visitor.visit_id(foreign_item.hir_id()));
662    try_visit!(visitor.visit_ident(*ident));
663
664    match *kind {
665        ForeignItemKind::Fn(ref sig, param_idents, ref generics) => {
666            try_visit!(visitor.visit_generics(generics));
667            try_visit!(visitor.visit_fn_decl(sig.decl));
668            for ident in param_idents.iter().copied() {
669                visit_opt!(visitor, visit_ident, ident);
670            }
671        }
672        ForeignItemKind::Static(ref typ, _, _) => {
673            try_visit!(visitor.visit_ty_unambig(typ));
674        }
675        ForeignItemKind::Type => (),
676    }
677    V::Result::output()
678}
679
680pub fn walk_local<'v, V: Visitor<'v>>(visitor: &mut V, local: &'v LetStmt<'v>) -> V::Result {
681    // Intentionally visiting the expr first - the initialization expr
682    // dominates the local's definition.
683    let LetStmt { super_: _, pat, ty, init, els, hir_id, span: _, source: _ } = local;
684    visit_opt!(visitor, visit_expr, *init);
685    try_visit!(visitor.visit_id(*hir_id));
686    try_visit!(visitor.visit_pat(*pat));
687    visit_opt!(visitor, visit_block, *els);
688    visit_opt!(visitor, visit_ty_unambig, *ty);
689    V::Result::output()
690}
691
692pub fn walk_block<'v, V: Visitor<'v>>(visitor: &mut V, block: &'v Block<'v>) -> V::Result {
693    let Block { stmts, expr, hir_id, rules: _, span: _, targeted_by_break: _ } = block;
694    try_visit!(visitor.visit_id(*hir_id));
695    walk_list!(visitor, visit_stmt, *stmts);
696    visit_opt!(visitor, visit_expr, *expr);
697    V::Result::output()
698}
699
700pub fn walk_stmt<'v, V: Visitor<'v>>(visitor: &mut V, statement: &'v Stmt<'v>) -> V::Result {
701    let Stmt { kind, hir_id, span: _ } = statement;
702    try_visit!(visitor.visit_id(*hir_id));
703    match *kind {
704        StmtKind::Let(ref local) => visitor.visit_local(local),
705        StmtKind::Item(item) => visitor.visit_nested_item(item),
706        StmtKind::Expr(ref expression) | StmtKind::Semi(ref expression) => {
707            visitor.visit_expr(expression)
708        }
709    }
710}
711
712pub fn walk_arm<'v, V: Visitor<'v>>(visitor: &mut V, arm: &'v Arm<'v>) -> V::Result {
713    let Arm { hir_id, span: _, pat, guard, body } = arm;
714    try_visit!(visitor.visit_id(*hir_id));
715    try_visit!(visitor.visit_pat(*pat));
716    visit_opt!(visitor, visit_expr, *guard);
717    visitor.visit_expr(*body)
718}
719
720pub fn walk_ty_pat<'v, V: Visitor<'v>>(visitor: &mut V, pattern: &'v TyPat<'v>) -> V::Result {
721    let TyPat { kind, hir_id, span: _ } = pattern;
722    try_visit!(visitor.visit_id(*hir_id));
723    match *kind {
724        TyPatKind::Range(lower_bound, upper_bound) => {
725            try_visit!(visitor.visit_const_arg_unambig(lower_bound));
726            try_visit!(visitor.visit_const_arg_unambig(upper_bound));
727        }
728        TyPatKind::Or(patterns) => walk_list!(visitor, visit_pattern_type_pattern, patterns),
729        TyPatKind::Err(_) => (),
730    }
731    V::Result::output()
732}
733
734pub fn walk_pat<'v, V: Visitor<'v>>(visitor: &mut V, pattern: &'v Pat<'v>) -> V::Result {
735    let Pat { hir_id, kind, span, default_binding_modes: _ } = pattern;
736    try_visit!(visitor.visit_id(*hir_id));
737    match *kind {
738        PatKind::TupleStruct(ref qpath, children, _) => {
739            try_visit!(visitor.visit_qpath(qpath, *hir_id, *span));
740            walk_list!(visitor, visit_pat, children);
741        }
742        PatKind::Struct(ref qpath, fields, _) => {
743            try_visit!(visitor.visit_qpath(qpath, *hir_id, *span));
744            walk_list!(visitor, visit_pat_field, fields);
745        }
746        PatKind::Or(pats) => walk_list!(visitor, visit_pat, pats),
747        PatKind::Tuple(tuple_elements, _) => {
748            walk_list!(visitor, visit_pat, tuple_elements);
749        }
750        PatKind::Box(ref subpattern)
751        | PatKind::Deref(ref subpattern)
752        | PatKind::Ref(ref subpattern, _) => {
753            try_visit!(visitor.visit_pat(subpattern));
754        }
755        PatKind::Binding(_, _hir_id, ident, ref optional_subpattern) => {
756            try_visit!(visitor.visit_ident(ident));
757            visit_opt!(visitor, visit_pat, optional_subpattern);
758        }
759        PatKind::Expr(ref expression) => try_visit!(visitor.visit_pat_expr(expression)),
760        PatKind::Range(ref lower_bound, ref upper_bound, _) => {
761            visit_opt!(visitor, visit_pat_expr, lower_bound);
762            visit_opt!(visitor, visit_pat_expr, upper_bound);
763        }
764        PatKind::Missing | PatKind::Never | PatKind::Wild | PatKind::Err(_) => (),
765        PatKind::Slice(prepatterns, ref slice_pattern, postpatterns) => {
766            walk_list!(visitor, visit_pat, prepatterns);
767            visit_opt!(visitor, visit_pat, slice_pattern);
768            walk_list!(visitor, visit_pat, postpatterns);
769        }
770        PatKind::Guard(subpat, condition) => {
771            try_visit!(visitor.visit_pat(subpat));
772            try_visit!(visitor.visit_expr(condition));
773        }
774    }
775    V::Result::output()
776}
777
778pub fn walk_pat_field<'v, V: Visitor<'v>>(visitor: &mut V, field: &'v PatField<'v>) -> V::Result {
779    let PatField { hir_id, ident, pat, is_shorthand: _, span: _ } = field;
780    try_visit!(visitor.visit_id(*hir_id));
781    try_visit!(visitor.visit_ident(*ident));
782    visitor.visit_pat(*pat)
783}
784
785pub fn walk_pat_expr<'v, V: Visitor<'v>>(visitor: &mut V, expr: &'v PatExpr<'v>) -> V::Result {
786    let PatExpr { hir_id, span, kind } = expr;
787    try_visit!(visitor.visit_id(*hir_id));
788    match kind {
789        PatExprKind::Lit { lit, negated } => visitor.visit_lit(*hir_id, *lit, *negated),
790        PatExprKind::ConstBlock(c) => visitor.visit_inline_const(c),
791        PatExprKind::Path(qpath) => visitor.visit_qpath(qpath, *hir_id, *span),
792    }
793}
794
795pub fn walk_anon_const<'v, V: Visitor<'v>>(visitor: &mut V, constant: &'v AnonConst) -> V::Result {
796    let AnonConst { hir_id, def_id: _, body, span: _ } = constant;
797    try_visit!(visitor.visit_id(*hir_id));
798    visitor.visit_nested_body(*body)
799}
800
801pub fn walk_inline_const<'v, V: Visitor<'v>>(
802    visitor: &mut V,
803    constant: &'v ConstBlock,
804) -> V::Result {
805    let ConstBlock { hir_id, def_id: _, body } = constant;
806    try_visit!(visitor.visit_id(*hir_id));
807    visitor.visit_nested_body(*body)
808}
809
810pub fn walk_expr<'v, V: Visitor<'v>>(visitor: &mut V, expression: &'v Expr<'v>) -> V::Result {
811    let Expr { hir_id, kind, span } = expression;
812    try_visit!(visitor.visit_id(*hir_id));
813    match *kind {
814        ExprKind::Array(subexpressions) => {
815            walk_list!(visitor, visit_expr, subexpressions);
816        }
817        ExprKind::ConstBlock(ref const_block) => {
818            try_visit!(visitor.visit_inline_const(const_block))
819        }
820        ExprKind::Repeat(ref element, ref count) => {
821            try_visit!(visitor.visit_expr(element));
822            try_visit!(visitor.visit_const_arg_unambig(count));
823        }
824        ExprKind::Struct(ref qpath, fields, ref optional_base) => {
825            try_visit!(visitor.visit_qpath(qpath, *hir_id, *span));
826            walk_list!(visitor, visit_expr_field, fields);
827            match optional_base {
828                StructTailExpr::Base(base) => try_visit!(visitor.visit_expr(base)),
829                StructTailExpr::None | StructTailExpr::DefaultFields(_) => {}
830            }
831        }
832        ExprKind::Tup(subexpressions) => {
833            walk_list!(visitor, visit_expr, subexpressions);
834        }
835        ExprKind::Call(ref callee_expression, arguments) => {
836            try_visit!(visitor.visit_expr(callee_expression));
837            walk_list!(visitor, visit_expr, arguments);
838        }
839        ExprKind::MethodCall(ref segment, receiver, arguments, _) => {
840            try_visit!(visitor.visit_path_segment(segment));
841            try_visit!(visitor.visit_expr(receiver));
842            walk_list!(visitor, visit_expr, arguments);
843        }
844        ExprKind::Use(expr, _) => {
845            try_visit!(visitor.visit_expr(expr));
846        }
847        ExprKind::Binary(_, ref left_expression, ref right_expression) => {
848            try_visit!(visitor.visit_expr(left_expression));
849            try_visit!(visitor.visit_expr(right_expression));
850        }
851        ExprKind::AddrOf(_, _, ref subexpression) | ExprKind::Unary(_, ref subexpression) => {
852            try_visit!(visitor.visit_expr(subexpression));
853        }
854        ExprKind::Cast(ref subexpression, ref typ) | ExprKind::Type(ref subexpression, ref typ) => {
855            try_visit!(visitor.visit_expr(subexpression));
856            try_visit!(visitor.visit_ty_unambig(typ));
857        }
858        ExprKind::DropTemps(ref subexpression) => {
859            try_visit!(visitor.visit_expr(subexpression));
860        }
861        ExprKind::Let(LetExpr { span: _, pat, ty, init, recovered: _ }) => {
862            // match the visit order in walk_local
863            try_visit!(visitor.visit_expr(init));
864            try_visit!(visitor.visit_pat(pat));
865            visit_opt!(visitor, visit_ty_unambig, ty);
866        }
867        ExprKind::If(ref cond, ref then, ref else_opt) => {
868            try_visit!(visitor.visit_expr(cond));
869            try_visit!(visitor.visit_expr(then));
870            visit_opt!(visitor, visit_expr, else_opt);
871        }
872        ExprKind::Loop(ref block, ref opt_label, _, _) => {
873            visit_opt!(visitor, visit_label, opt_label);
874            try_visit!(visitor.visit_block(block));
875        }
876        ExprKind::Match(ref subexpression, arms, _) => {
877            try_visit!(visitor.visit_expr(subexpression));
878            walk_list!(visitor, visit_arm, arms);
879        }
880        ExprKind::Closure(&Closure {
881            def_id,
882            binder: _,
883            bound_generic_params,
884            fn_decl,
885            body,
886            capture_clause: _,
887            fn_decl_span: _,
888            fn_arg_span: _,
889            kind: _,
890            constness: _,
891        }) => {
892            walk_list!(visitor, visit_generic_param, bound_generic_params);
893            try_visit!(visitor.visit_fn(FnKind::Closure, fn_decl, body, *span, def_id));
894        }
895        ExprKind::Block(ref block, ref opt_label) => {
896            visit_opt!(visitor, visit_label, opt_label);
897            try_visit!(visitor.visit_block(block));
898        }
899        ExprKind::Assign(ref lhs, ref rhs, _) => {
900            try_visit!(visitor.visit_expr(rhs));
901            try_visit!(visitor.visit_expr(lhs));
902        }
903        ExprKind::AssignOp(_, ref left_expression, ref right_expression) => {
904            try_visit!(visitor.visit_expr(right_expression));
905            try_visit!(visitor.visit_expr(left_expression));
906        }
907        ExprKind::Field(ref subexpression, ident) => {
908            try_visit!(visitor.visit_expr(subexpression));
909            try_visit!(visitor.visit_ident(ident));
910        }
911        ExprKind::Index(ref main_expression, ref index_expression, _) => {
912            try_visit!(visitor.visit_expr(main_expression));
913            try_visit!(visitor.visit_expr(index_expression));
914        }
915        ExprKind::Path(ref qpath) => {
916            try_visit!(visitor.visit_qpath(qpath, *hir_id, *span));
917        }
918        ExprKind::Break(ref destination, ref opt_expr) => {
919            visit_opt!(visitor, visit_label, &destination.label);
920            visit_opt!(visitor, visit_expr, opt_expr);
921        }
922        ExprKind::Continue(ref destination) => {
923            visit_opt!(visitor, visit_label, &destination.label);
924        }
925        ExprKind::Ret(ref optional_expression) => {
926            visit_opt!(visitor, visit_expr, optional_expression);
927        }
928        ExprKind::Become(ref expr) => try_visit!(visitor.visit_expr(expr)),
929        ExprKind::InlineAsm(ref asm) => {
930            try_visit!(visitor.visit_inline_asm(asm, *hir_id));
931        }
932        ExprKind::OffsetOf(ref container, ref fields) => {
933            try_visit!(visitor.visit_ty_unambig(container));
934            walk_list!(visitor, visit_ident, fields.iter().copied());
935        }
936        ExprKind::Yield(ref subexpression, _) => {
937            try_visit!(visitor.visit_expr(subexpression));
938        }
939        ExprKind::UnsafeBinderCast(_kind, expr, ty) => {
940            try_visit!(visitor.visit_expr(expr));
941            visit_opt!(visitor, visit_ty_unambig, ty);
942        }
943        ExprKind::Lit(lit) => try_visit!(visitor.visit_lit(*hir_id, lit, false)),
944        ExprKind::Err(_) => {}
945    }
946    V::Result::output()
947}
948
949pub fn walk_expr_field<'v, V: Visitor<'v>>(visitor: &mut V, field: &'v ExprField<'v>) -> V::Result {
950    let ExprField { hir_id, ident, expr, span: _, is_shorthand: _ } = field;
951    try_visit!(visitor.visit_id(*hir_id));
952    try_visit!(visitor.visit_ident(*ident));
953    visitor.visit_expr(*expr)
954}
955/// We track whether an infer var is from a [`Ty`], [`ConstArg`], or [`GenericArg`] so that
956/// HIR visitors overriding [`Visitor::visit_infer`] can determine what kind of infer is being visited
957pub enum InferKind<'hir> {
958    Ty(&'hir Ty<'hir>),
959    Const(&'hir ConstArg<'hir>),
960    Ambig(&'hir InferArg),
961}
962
963pub fn walk_generic_arg<'v, V: Visitor<'v>>(
964    visitor: &mut V,
965    generic_arg: &'v GenericArg<'v>,
966) -> V::Result {
967    match generic_arg {
968        GenericArg::Lifetime(lt) => visitor.visit_lifetime(lt),
969        GenericArg::Type(ty) => visitor.visit_ty(ty),
970        GenericArg::Const(ct) => visitor.visit_const_arg(ct),
971        GenericArg::Infer(inf) => {
972            let InferArg { hir_id, span } = inf;
973            visitor.visit_infer(*hir_id, *span, InferKind::Ambig(inf))
974        }
975    }
976}
977
978pub fn walk_unambig_ty<'v, V: Visitor<'v>>(visitor: &mut V, typ: &'v Ty<'v>) -> V::Result {
979    match typ.try_as_ambig_ty() {
980        Some(ambig_ty) => visitor.visit_ty(ambig_ty),
981        None => {
982            let Ty { hir_id, span, kind: _ } = typ;
983            try_visit!(visitor.visit_id(*hir_id));
984            visitor.visit_infer(*hir_id, *span, InferKind::Ty(typ))
985        }
986    }
987}
988
989pub fn walk_ty<'v, V: Visitor<'v>>(visitor: &mut V, typ: &'v Ty<'v, AmbigArg>) -> V::Result {
990    let Ty { hir_id, span: _, kind } = typ;
991    try_visit!(visitor.visit_id(*hir_id));
992
993    match *kind {
994        TyKind::Slice(ref ty) => try_visit!(visitor.visit_ty_unambig(ty)),
995        TyKind::Ptr(ref mutable_type) => try_visit!(visitor.visit_ty_unambig(mutable_type.ty)),
996        TyKind::Ref(ref lifetime, ref mutable_type) => {
997            try_visit!(visitor.visit_lifetime(lifetime));
998            try_visit!(visitor.visit_ty_unambig(mutable_type.ty));
999        }
1000        TyKind::Never => {}
1001        TyKind::Tup(tuple_element_types) => {
1002            walk_list!(visitor, visit_ty_unambig, tuple_element_types);
1003        }
1004        TyKind::FnPtr(ref function_declaration) => {
1005            walk_list!(visitor, visit_generic_param, function_declaration.generic_params);
1006            try_visit!(visitor.visit_fn_decl(function_declaration.decl));
1007        }
1008        TyKind::UnsafeBinder(ref unsafe_binder) => {
1009            walk_list!(visitor, visit_generic_param, unsafe_binder.generic_params);
1010            try_visit!(visitor.visit_ty_unambig(unsafe_binder.inner_ty));
1011        }
1012        TyKind::Path(ref qpath) => {
1013            try_visit!(visitor.visit_qpath(qpath, typ.hir_id, typ.span));
1014        }
1015        TyKind::OpaqueDef(opaque) => {
1016            try_visit!(visitor.visit_opaque_ty(opaque));
1017        }
1018        TyKind::TraitAscription(bounds) => {
1019            walk_list!(visitor, visit_param_bound, bounds);
1020        }
1021        TyKind::Array(ref ty, ref length) => {
1022            try_visit!(visitor.visit_ty_unambig(ty));
1023            try_visit!(visitor.visit_const_arg_unambig(length));
1024        }
1025        TyKind::TraitObject(bounds, ref lifetime) => {
1026            for bound in bounds {
1027                try_visit!(visitor.visit_poly_trait_ref(bound));
1028            }
1029            try_visit!(visitor.visit_lifetime(lifetime));
1030        }
1031        TyKind::Typeof(ref expression) => try_visit!(visitor.visit_anon_const(expression)),
1032        TyKind::InferDelegation(..) | TyKind::Err(_) => {}
1033        TyKind::Pat(ty, pat) => {
1034            try_visit!(visitor.visit_ty_unambig(ty));
1035            try_visit!(visitor.visit_pattern_type_pattern(pat));
1036        }
1037    }
1038    V::Result::output()
1039}
1040
1041pub fn walk_const_arg<'v, V: Visitor<'v>>(
1042    visitor: &mut V,
1043    const_arg: &'v ConstArg<'v>,
1044) -> V::Result {
1045    match const_arg.try_as_ambig_ct() {
1046        Some(ambig_ct) => visitor.visit_const_arg(ambig_ct),
1047        None => {
1048            let ConstArg { hir_id, kind: _ } = const_arg;
1049            try_visit!(visitor.visit_id(*hir_id));
1050            visitor.visit_infer(*hir_id, const_arg.span(), InferKind::Const(const_arg))
1051        }
1052    }
1053}
1054
1055pub fn walk_ambig_const_arg<'v, V: Visitor<'v>>(
1056    visitor: &mut V,
1057    const_arg: &'v ConstArg<'v, AmbigArg>,
1058) -> V::Result {
1059    let ConstArg { hir_id, kind } = const_arg;
1060    try_visit!(visitor.visit_id(*hir_id));
1061    match kind {
1062        ConstArgKind::Path(qpath) => visitor.visit_qpath(qpath, *hir_id, qpath.span()),
1063        ConstArgKind::Anon(anon) => visitor.visit_anon_const(*anon),
1064    }
1065}
1066
1067pub fn walk_generic_param<'v, V: Visitor<'v>>(
1068    visitor: &mut V,
1069    param: &'v GenericParam<'v>,
1070) -> V::Result {
1071    let GenericParam {
1072        hir_id,
1073        def_id: _,
1074        name,
1075        span: _,
1076        pure_wrt_drop: _,
1077        kind,
1078        colon_span: _,
1079        source: _,
1080    } = param;
1081    try_visit!(visitor.visit_id(*hir_id));
1082    match *name {
1083        ParamName::Plain(ident) | ParamName::Error(ident) => try_visit!(visitor.visit_ident(ident)),
1084        ParamName::Fresh => {}
1085    }
1086    match *kind {
1087        GenericParamKind::Lifetime { .. } => {}
1088        GenericParamKind::Type { ref default, .. } => {
1089            visit_opt!(visitor, visit_ty_unambig, default)
1090        }
1091        GenericParamKind::Const { ref ty, ref default, synthetic: _ } => {
1092            try_visit!(visitor.visit_ty_unambig(ty));
1093            if let Some(default) = default {
1094                try_visit!(visitor.visit_const_param_default(*hir_id, default));
1095            }
1096        }
1097    }
1098    V::Result::output()
1099}
1100
1101pub fn walk_const_param_default<'v, V: Visitor<'v>>(
1102    visitor: &mut V,
1103    ct: &'v ConstArg<'v>,
1104) -> V::Result {
1105    visitor.visit_const_arg_unambig(ct)
1106}
1107
1108pub fn walk_generics<'v, V: Visitor<'v>>(visitor: &mut V, generics: &'v Generics<'v>) -> V::Result {
1109    let &Generics {
1110        params,
1111        predicates,
1112        has_where_clause_predicates: _,
1113        where_clause_span: _,
1114        span: _,
1115    } = generics;
1116    walk_list!(visitor, visit_generic_param, params);
1117    walk_list!(visitor, visit_where_predicate, predicates);
1118    V::Result::output()
1119}
1120
1121pub fn walk_where_predicate<'v, V: Visitor<'v>>(
1122    visitor: &mut V,
1123    predicate: &'v WherePredicate<'v>,
1124) -> V::Result {
1125    let &WherePredicate { hir_id, kind, span: _ } = predicate;
1126    try_visit!(visitor.visit_id(hir_id));
1127    match *kind {
1128        WherePredicateKind::BoundPredicate(WhereBoundPredicate {
1129            ref bounded_ty,
1130            bounds,
1131            bound_generic_params,
1132            origin: _,
1133        }) => {
1134            try_visit!(visitor.visit_ty_unambig(bounded_ty));
1135            walk_list!(visitor, visit_param_bound, bounds);
1136            walk_list!(visitor, visit_generic_param, bound_generic_params);
1137        }
1138        WherePredicateKind::RegionPredicate(WhereRegionPredicate {
1139            ref lifetime,
1140            bounds,
1141            in_where_clause: _,
1142        }) => {
1143            try_visit!(visitor.visit_lifetime(lifetime));
1144            walk_list!(visitor, visit_param_bound, bounds);
1145        }
1146        WherePredicateKind::EqPredicate(WhereEqPredicate { ref lhs_ty, ref rhs_ty }) => {
1147            try_visit!(visitor.visit_ty_unambig(lhs_ty));
1148            try_visit!(visitor.visit_ty_unambig(rhs_ty));
1149        }
1150    }
1151    V::Result::output()
1152}
1153
1154pub fn walk_fn_decl<'v, V: Visitor<'v>>(
1155    visitor: &mut V,
1156    function_declaration: &'v FnDecl<'v>,
1157) -> V::Result {
1158    let FnDecl { inputs, output, c_variadic: _, implicit_self: _, lifetime_elision_allowed: _ } =
1159        function_declaration;
1160    walk_list!(visitor, visit_ty_unambig, *inputs);
1161    visitor.visit_fn_ret_ty(output)
1162}
1163
1164pub fn walk_fn_ret_ty<'v, V: Visitor<'v>>(visitor: &mut V, ret_ty: &'v FnRetTy<'v>) -> V::Result {
1165    if let FnRetTy::Return(output_ty) = *ret_ty {
1166        try_visit!(visitor.visit_ty_unambig(output_ty));
1167    }
1168    V::Result::output()
1169}
1170
1171pub fn walk_fn<'v, V: Visitor<'v>>(
1172    visitor: &mut V,
1173    function_kind: FnKind<'v>,
1174    function_declaration: &'v FnDecl<'v>,
1175    body_id: BodyId,
1176    _: LocalDefId,
1177) -> V::Result {
1178    try_visit!(visitor.visit_fn_decl(function_declaration));
1179    try_visit!(walk_fn_kind(visitor, function_kind));
1180    visitor.visit_nested_body(body_id)
1181}
1182
1183pub fn walk_fn_kind<'v, V: Visitor<'v>>(visitor: &mut V, function_kind: FnKind<'v>) -> V::Result {
1184    match function_kind {
1185        FnKind::ItemFn(_, generics, ..) => {
1186            try_visit!(visitor.visit_generics(generics));
1187        }
1188        FnKind::Closure | FnKind::Method(..) => {}
1189    }
1190    V::Result::output()
1191}
1192
1193pub fn walk_use<'v, V: Visitor<'v>>(
1194    visitor: &mut V,
1195    path: &'v UsePath<'v>,
1196    hir_id: HirId,
1197) -> V::Result {
1198    let UsePath { segments, ref res, span } = *path;
1199    for res in res.present_items() {
1200        try_visit!(visitor.visit_path(&Path { segments, res, span }, hir_id));
1201    }
1202    V::Result::output()
1203}
1204
1205pub fn walk_trait_item<'v, V: Visitor<'v>>(
1206    visitor: &mut V,
1207    trait_item: &'v TraitItem<'v>,
1208) -> V::Result {
1209    let TraitItem {
1210        ident,
1211        generics,
1212        ref defaultness,
1213        ref kind,
1214        span,
1215        owner_id: _,
1216        has_delayed_lints: _,
1217    } = *trait_item;
1218    let hir_id = trait_item.hir_id();
1219    try_visit!(visitor.visit_ident(ident));
1220    try_visit!(visitor.visit_generics(&generics));
1221    try_visit!(visitor.visit_defaultness(&defaultness));
1222    try_visit!(visitor.visit_id(hir_id));
1223    match *kind {
1224        TraitItemKind::Const(ref ty, default) => {
1225            try_visit!(visitor.visit_ty_unambig(ty));
1226            visit_opt!(visitor, visit_nested_body, default);
1227        }
1228        TraitItemKind::Fn(ref sig, TraitFn::Required(param_idents)) => {
1229            try_visit!(visitor.visit_fn_decl(sig.decl));
1230            for ident in param_idents.iter().copied() {
1231                visit_opt!(visitor, visit_ident, ident);
1232            }
1233        }
1234        TraitItemKind::Fn(ref sig, TraitFn::Provided(body_id)) => {
1235            try_visit!(visitor.visit_fn(
1236                FnKind::Method(ident, sig),
1237                sig.decl,
1238                body_id,
1239                span,
1240                trait_item.owner_id.def_id,
1241            ));
1242        }
1243        TraitItemKind::Type(bounds, ref default) => {
1244            walk_list!(visitor, visit_param_bound, bounds);
1245            visit_opt!(visitor, visit_ty_unambig, default);
1246        }
1247    }
1248    V::Result::output()
1249}
1250
1251pub fn walk_trait_item_ref<'v, V: Visitor<'v>>(
1252    visitor: &mut V,
1253    trait_item_ref: &'v TraitItemRef,
1254) -> V::Result {
1255    let TraitItemRef { id, ident, ref kind, span: _ } = *trait_item_ref;
1256    try_visit!(visitor.visit_nested_trait_item(id));
1257    try_visit!(visitor.visit_ident(ident));
1258    visitor.visit_associated_item_kind(kind)
1259}
1260
1261pub fn walk_impl_item<'v, V: Visitor<'v>>(
1262    visitor: &mut V,
1263    impl_item: &'v ImplItem<'v>,
1264) -> V::Result {
1265    let ImplItem {
1266        owner_id: _,
1267        ident,
1268        ref generics,
1269        ref kind,
1270        ref defaultness,
1271        span: _,
1272        vis_span: _,
1273        has_delayed_lints: _,
1274    } = *impl_item;
1275
1276    try_visit!(visitor.visit_ident(ident));
1277    try_visit!(visitor.visit_generics(generics));
1278    try_visit!(visitor.visit_defaultness(defaultness));
1279    try_visit!(visitor.visit_id(impl_item.hir_id()));
1280    match *kind {
1281        ImplItemKind::Const(ref ty, body) => {
1282            try_visit!(visitor.visit_ty_unambig(ty));
1283            visitor.visit_nested_body(body)
1284        }
1285        ImplItemKind::Fn(ref sig, body_id) => visitor.visit_fn(
1286            FnKind::Method(impl_item.ident, sig),
1287            sig.decl,
1288            body_id,
1289            impl_item.span,
1290            impl_item.owner_id.def_id,
1291        ),
1292        ImplItemKind::Type(ref ty) => visitor.visit_ty_unambig(ty),
1293    }
1294}
1295
1296pub fn walk_foreign_item_ref<'v, V: Visitor<'v>>(
1297    visitor: &mut V,
1298    foreign_item_ref: &'v ForeignItemRef,
1299) -> V::Result {
1300    let ForeignItemRef { id, ident, span: _ } = *foreign_item_ref;
1301    try_visit!(visitor.visit_nested_foreign_item(id));
1302    visitor.visit_ident(ident)
1303}
1304
1305pub fn walk_impl_item_ref<'v, V: Visitor<'v>>(
1306    visitor: &mut V,
1307    impl_item_ref: &'v ImplItemRef,
1308) -> V::Result {
1309    let ImplItemRef { id, ident, ref kind, span: _, trait_item_def_id: _ } = *impl_item_ref;
1310    try_visit!(visitor.visit_nested_impl_item(id));
1311    try_visit!(visitor.visit_ident(ident));
1312    visitor.visit_associated_item_kind(kind)
1313}
1314
1315pub fn walk_trait_ref<'v, V: Visitor<'v>>(
1316    visitor: &mut V,
1317    trait_ref: &'v TraitRef<'v>,
1318) -> V::Result {
1319    let TraitRef { hir_ref_id, path } = trait_ref;
1320    try_visit!(visitor.visit_id(*hir_ref_id));
1321    visitor.visit_path(*path, *hir_ref_id)
1322}
1323
1324pub fn walk_param_bound<'v, V: Visitor<'v>>(
1325    visitor: &mut V,
1326    bound: &'v GenericBound<'v>,
1327) -> V::Result {
1328    match *bound {
1329        GenericBound::Trait(ref typ) => visitor.visit_poly_trait_ref(typ),
1330        GenericBound::Outlives(ref lifetime) => visitor.visit_lifetime(lifetime),
1331        GenericBound::Use(args, _) => {
1332            walk_list!(visitor, visit_precise_capturing_arg, args);
1333            V::Result::output()
1334        }
1335    }
1336}
1337
1338pub fn walk_precise_capturing_arg<'v, V: Visitor<'v>>(
1339    visitor: &mut V,
1340    arg: &'v PreciseCapturingArg<'v>,
1341) -> V::Result {
1342    match *arg {
1343        PreciseCapturingArg::Lifetime(lt) => visitor.visit_lifetime(lt),
1344        PreciseCapturingArg::Param(param) => {
1345            let PreciseCapturingNonLifetimeArg { hir_id, ident, res: _ } = param;
1346            try_visit!(visitor.visit_id(hir_id));
1347            visitor.visit_ident(ident)
1348        }
1349    }
1350}
1351
1352pub fn walk_poly_trait_ref<'v, V: Visitor<'v>>(
1353    visitor: &mut V,
1354    trait_ref: &'v PolyTraitRef<'v>,
1355) -> V::Result {
1356    let PolyTraitRef { bound_generic_params, modifiers: _, trait_ref, span: _ } = trait_ref;
1357    walk_list!(visitor, visit_generic_param, *bound_generic_params);
1358    visitor.visit_trait_ref(trait_ref)
1359}
1360
1361pub fn walk_opaque_ty<'v, V: Visitor<'v>>(visitor: &mut V, opaque: &'v OpaqueTy<'v>) -> V::Result {
1362    let &OpaqueTy { hir_id, def_id: _, bounds, origin: _, span: _ } = opaque;
1363    try_visit!(visitor.visit_id(hir_id));
1364    walk_list!(visitor, visit_param_bound, bounds);
1365    V::Result::output()
1366}
1367
1368pub fn walk_struct_def<'v, V: Visitor<'v>>(
1369    visitor: &mut V,
1370    struct_definition: &'v VariantData<'v>,
1371) -> V::Result {
1372    visit_opt!(visitor, visit_id, struct_definition.ctor_hir_id());
1373    walk_list!(visitor, visit_field_def, struct_definition.fields());
1374    V::Result::output()
1375}
1376
1377pub fn walk_field_def<'v, V: Visitor<'v>>(
1378    visitor: &mut V,
1379    FieldDef { hir_id, ident, ty, default, span: _, vis_span: _, def_id: _, safety: _ }: &'v FieldDef<'v>,
1380) -> V::Result {
1381    try_visit!(visitor.visit_id(*hir_id));
1382    try_visit!(visitor.visit_ident(*ident));
1383    visit_opt!(visitor, visit_anon_const, default);
1384    visitor.visit_ty_unambig(*ty)
1385}
1386
1387pub fn walk_enum_def<'v, V: Visitor<'v>>(
1388    visitor: &mut V,
1389    enum_definition: &'v EnumDef<'v>,
1390) -> V::Result {
1391    let EnumDef { variants } = enum_definition;
1392    walk_list!(visitor, visit_variant, *variants);
1393    V::Result::output()
1394}
1395
1396pub fn walk_variant<'v, V: Visitor<'v>>(visitor: &mut V, variant: &'v Variant<'v>) -> V::Result {
1397    let Variant { ident, hir_id, def_id: _, data, disr_expr, span: _ } = variant;
1398    try_visit!(visitor.visit_ident(*ident));
1399    try_visit!(visitor.visit_id(*hir_id));
1400    try_visit!(visitor.visit_variant_data(data));
1401    visit_opt!(visitor, visit_anon_const, disr_expr);
1402    V::Result::output()
1403}
1404
1405pub fn walk_label<'v, V: Visitor<'v>>(visitor: &mut V, label: &'v Label) -> V::Result {
1406    let Label { ident } = label;
1407    visitor.visit_ident(*ident)
1408}
1409
1410pub fn walk_inf<'v, V: Visitor<'v>>(visitor: &mut V, inf: &'v InferArg) -> V::Result {
1411    let InferArg { hir_id, span: _ } = inf;
1412    visitor.visit_id(*hir_id)
1413}
1414
1415pub fn walk_lifetime<'v, V: Visitor<'v>>(visitor: &mut V, lifetime: &'v Lifetime) -> V::Result {
1416    let Lifetime { hir_id, ident, kind: _, source: _, syntax: _ } = lifetime;
1417    try_visit!(visitor.visit_id(*hir_id));
1418    visitor.visit_ident(*ident)
1419}
1420
1421pub fn walk_qpath<'v, V: Visitor<'v>>(
1422    visitor: &mut V,
1423    qpath: &'v QPath<'v>,
1424    id: HirId,
1425) -> V::Result {
1426    match *qpath {
1427        QPath::Resolved(ref maybe_qself, ref path) => {
1428            visit_opt!(visitor, visit_ty_unambig, maybe_qself);
1429            visitor.visit_path(path, id)
1430        }
1431        QPath::TypeRelative(ref qself, ref segment) => {
1432            try_visit!(visitor.visit_ty_unambig(qself));
1433            visitor.visit_path_segment(segment)
1434        }
1435        QPath::LangItem(..) => V::Result::output(),
1436    }
1437}
1438
1439pub fn walk_path<'v, V: Visitor<'v>>(visitor: &mut V, path: &Path<'v>) -> V::Result {
1440    let Path { segments, span: _, res: _ } = path;
1441    walk_list!(visitor, visit_path_segment, *segments);
1442    V::Result::output()
1443}
1444
1445pub fn walk_path_segment<'v, V: Visitor<'v>>(
1446    visitor: &mut V,
1447    segment: &'v PathSegment<'v>,
1448) -> V::Result {
1449    let PathSegment { ident, hir_id, res: _, args, infer_args: _ } = segment;
1450    try_visit!(visitor.visit_ident(*ident));
1451    try_visit!(visitor.visit_id(*hir_id));
1452    visit_opt!(visitor, visit_generic_args, *args);
1453    V::Result::output()
1454}
1455
1456pub fn walk_generic_args<'v, V: Visitor<'v>>(
1457    visitor: &mut V,
1458    generic_args: &'v GenericArgs<'v>,
1459) -> V::Result {
1460    let GenericArgs { args, constraints, parenthesized: _, span_ext: _ } = generic_args;
1461    walk_list!(visitor, visit_generic_arg, *args);
1462    walk_list!(visitor, visit_assoc_item_constraint, *constraints);
1463    V::Result::output()
1464}
1465
1466pub fn walk_assoc_item_constraint<'v, V: Visitor<'v>>(
1467    visitor: &mut V,
1468    constraint: &'v AssocItemConstraint<'v>,
1469) -> V::Result {
1470    let AssocItemConstraint { hir_id, ident, gen_args, kind: _, span: _ } = constraint;
1471    try_visit!(visitor.visit_id(*hir_id));
1472    try_visit!(visitor.visit_ident(*ident));
1473    try_visit!(visitor.visit_generic_args(*gen_args));
1474    match constraint.kind {
1475        AssocItemConstraintKind::Equality { ref term } => match term {
1476            Term::Ty(ty) => try_visit!(visitor.visit_ty_unambig(ty)),
1477            Term::Const(c) => try_visit!(visitor.visit_const_arg_unambig(c)),
1478        },
1479        AssocItemConstraintKind::Bound { bounds } => {
1480            walk_list!(visitor, visit_param_bound, bounds)
1481        }
1482    }
1483    V::Result::output()
1484}
1485
1486pub fn walk_associated_item_kind<'v, V: Visitor<'v>>(_: &mut V, _: &'v AssocItemKind) -> V::Result {
1487    // No visitable content here: this fn exists so you can call it if
1488    // the right thing to do, should content be added in the future,
1489    // would be to walk it.
1490    V::Result::output()
1491}
1492
1493pub fn walk_defaultness<'v, V: Visitor<'v>>(_: &mut V, _: &'v Defaultness) -> V::Result {
1494    // No visitable content here: this fn exists so you can call it if
1495    // the right thing to do, should content be added in the future,
1496    // would be to walk it.
1497    V::Result::output()
1498}
1499
1500pub fn walk_inline_asm<'v, V: Visitor<'v>>(
1501    visitor: &mut V,
1502    asm: &'v InlineAsm<'v>,
1503    id: HirId,
1504) -> V::Result {
1505    for (op, op_sp) in asm.operands {
1506        match op {
1507            InlineAsmOperand::In { expr, .. } | InlineAsmOperand::InOut { expr, .. } => {
1508                try_visit!(visitor.visit_expr(expr));
1509            }
1510            InlineAsmOperand::Out { expr, .. } => {
1511                visit_opt!(visitor, visit_expr, expr);
1512            }
1513            InlineAsmOperand::SplitInOut { in_expr, out_expr, .. } => {
1514                try_visit!(visitor.visit_expr(in_expr));
1515                visit_opt!(visitor, visit_expr, out_expr);
1516            }
1517            InlineAsmOperand::Const { anon_const, .. } => {
1518                try_visit!(visitor.visit_inline_const(anon_const));
1519            }
1520            InlineAsmOperand::SymFn { expr, .. } => {
1521                try_visit!(visitor.visit_expr(expr));
1522            }
1523            InlineAsmOperand::SymStatic { path, .. } => {
1524                try_visit!(visitor.visit_qpath(path, id, *op_sp));
1525            }
1526            InlineAsmOperand::Label { block } => try_visit!(visitor.visit_block(block)),
1527        }
1528    }
1529    V::Result::output()
1530}