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.)
203pub trait Visitor<'v>: Sized {
204    // This type should not be overridden, it exists for convenient usage as `Self::MaybeTyCtxt`.
205    type MaybeTyCtxt: HirTyCtxt<'v> = <Self::NestedFilter as NestedFilter<'v>>::MaybeTyCtxt;
206
207    ///////////////////////////////////////////////////////////////////////////
208    // Nested items.
209
210    /// Override this type to control which nested HIR are visited; see
211    /// [`NestedFilter`] for details. If you override this type, you
212    /// must also override [`maybe_tcx`](Self::maybe_tcx).
213    ///
214    /// **If for some reason you want the nested behavior, but don't
215    /// have a `tcx` at your disposal:** then override the
216    /// `visit_nested_XXX` methods. If a new `visit_nested_XXX` variant is
217    /// added in the future, it will cause a panic which can be detected
218    /// and fixed appropriately.
219    type NestedFilter: NestedFilter<'v> = nested_filter::None;
220
221    /// The result type of the `visit_*` methods. Can be either `()`,
222    /// or `ControlFlow<T>`.
223    type Result: VisitorResult = ();
224
225    /// If `type NestedFilter` is set to visit nested items, this method
226    /// must also be overridden to provide a map to retrieve nested items.
227    fn maybe_tcx(&mut self) -> Self::MaybeTyCtxt {
228        panic!(
229            "maybe_tcx must be implemented or consider using \
230            `type NestedFilter = nested_filter::None` (the default)"
231        );
232    }
233
234    /// Invoked when a nested item is encountered. By default, when
235    /// `Self::NestedFilter` is `nested_filter::None`, this method does
236    /// nothing. **You probably don't want to override this method** --
237    /// instead, override [`Self::NestedFilter`] or use the "shallow" or
238    /// "deep" visit patterns described at
239    /// [`rustc_hir::intravisit`]. The only reason to override
240    /// this method is if you want a nested pattern but cannot supply a
241    /// `TyCtxt`; see `maybe_tcx` for advice.
242    fn visit_nested_item(&mut self, id: ItemId) -> Self::Result {
243        if Self::NestedFilter::INTER {
244            let item = self.maybe_tcx().hir_item(id);
245            try_visit!(self.visit_item(item));
246        }
247        Self::Result::output()
248    }
249
250    /// Like `visit_nested_item()`, but for trait items. See
251    /// `visit_nested_item()` for advice on when to override this
252    /// method.
253    fn visit_nested_trait_item(&mut self, id: TraitItemId) -> Self::Result {
254        if Self::NestedFilter::INTER {
255            let item = self.maybe_tcx().hir_trait_item(id);
256            try_visit!(self.visit_trait_item(item));
257        }
258        Self::Result::output()
259    }
260
261    /// Like `visit_nested_item()`, but for impl items. See
262    /// `visit_nested_item()` for advice on when to override this
263    /// method.
264    fn visit_nested_impl_item(&mut self, id: ImplItemId) -> Self::Result {
265        if Self::NestedFilter::INTER {
266            let item = self.maybe_tcx().hir_impl_item(id);
267            try_visit!(self.visit_impl_item(item));
268        }
269        Self::Result::output()
270    }
271
272    /// Like `visit_nested_item()`, but for foreign items. See
273    /// `visit_nested_item()` for advice on when to override this
274    /// method.
275    fn visit_nested_foreign_item(&mut self, id: ForeignItemId) -> Self::Result {
276        if Self::NestedFilter::INTER {
277            let item = self.maybe_tcx().hir_foreign_item(id);
278            try_visit!(self.visit_foreign_item(item));
279        }
280        Self::Result::output()
281    }
282
283    /// Invoked to visit the body of a function, method or closure. Like
284    /// `visit_nested_item`, does nothing by default unless you override
285    /// `Self::NestedFilter`.
286    fn visit_nested_body(&mut self, id: BodyId) -> Self::Result {
287        if Self::NestedFilter::INTRA {
288            let body = self.maybe_tcx().hir_body(id);
289            try_visit!(self.visit_body(body));
290        }
291        Self::Result::output()
292    }
293
294    fn visit_param(&mut self, param: &'v Param<'v>) -> Self::Result {
295        walk_param(self, param)
296    }
297
298    /// Visits the top-level item and (optionally) nested items / impl items. See
299    /// `visit_nested_item` for details.
300    fn visit_item(&mut self, i: &'v Item<'v>) -> Self::Result {
301        walk_item(self, i)
302    }
303
304    fn visit_body(&mut self, b: &Body<'v>) -> Self::Result {
305        walk_body(self, b)
306    }
307
308    ///////////////////////////////////////////////////////////////////////////
309
310    fn visit_id(&mut self, _hir_id: HirId) -> Self::Result {
311        Self::Result::output()
312    }
313    fn visit_name(&mut self, _name: Symbol) -> Self::Result {
314        Self::Result::output()
315    }
316    fn visit_ident(&mut self, ident: Ident) -> Self::Result {
317        walk_ident(self, ident)
318    }
319    fn visit_mod(&mut self, m: &'v Mod<'v>, _s: Span, _n: HirId) -> Self::Result {
320        walk_mod(self, m)
321    }
322    fn visit_foreign_item(&mut self, i: &'v ForeignItem<'v>) -> Self::Result {
323        walk_foreign_item(self, i)
324    }
325    fn visit_local(&mut self, l: &'v LetStmt<'v>) -> Self::Result {
326        walk_local(self, l)
327    }
328    fn visit_block(&mut self, b: &'v Block<'v>) -> Self::Result {
329        walk_block(self, b)
330    }
331    fn visit_stmt(&mut self, s: &'v Stmt<'v>) -> Self::Result {
332        walk_stmt(self, s)
333    }
334    fn visit_arm(&mut self, a: &'v Arm<'v>) -> Self::Result {
335        walk_arm(self, a)
336    }
337    fn visit_pat(&mut self, p: &'v Pat<'v>) -> Self::Result {
338        walk_pat(self, p)
339    }
340    fn visit_pat_field(&mut self, f: &'v PatField<'v>) -> Self::Result {
341        walk_pat_field(self, f)
342    }
343    fn visit_pat_expr(&mut self, expr: &'v PatExpr<'v>) -> Self::Result {
344        walk_pat_expr(self, expr)
345    }
346    fn visit_lit(&mut self, _hir_id: HirId, _lit: &'v Lit, _negated: bool) -> Self::Result {
347        Self::Result::output()
348    }
349    fn visit_anon_const(&mut self, c: &'v AnonConst) -> Self::Result {
350        walk_anon_const(self, c)
351    }
352    fn visit_inline_const(&mut self, c: &'v ConstBlock) -> Self::Result {
353        walk_inline_const(self, c)
354    }
355
356    fn visit_generic_arg(&mut self, generic_arg: &'v GenericArg<'v>) -> Self::Result {
357        walk_generic_arg(self, generic_arg)
358    }
359
360    /// All types are treated as ambiguous types for the purposes of hir visiting in
361    /// order to ensure that visitors can handle infer vars without it being too error-prone.
362    ///
363    /// See the doc comments on [`Ty`] for an explanation of what it means for a type to be
364    /// ambiguous.
365    ///
366    /// The [`Visitor::visit_infer`] method should be overridden in order to handle infer vars.
367    fn visit_ty(&mut self, t: &'v Ty<'v, AmbigArg>) -> Self::Result {
368        walk_ty(self, t)
369    }
370
371    /// All consts are treated as ambiguous consts for the purposes of hir visiting in
372    /// order to ensure that visitors can handle infer vars without it being too error-prone.
373    ///
374    /// See the doc comments on [`ConstArg`] for an explanation of what it means for a const to be
375    /// ambiguous.
376    ///
377    /// The [`Visitor::visit_infer`] method should be overridden in order to handle infer vars.
378    fn visit_const_arg(&mut self, c: &'v ConstArg<'v, AmbigArg>) -> Self::Result {
379        walk_ambig_const_arg(self, c)
380    }
381
382    #[allow(unused_variables)]
383    fn visit_infer(&mut self, inf_id: HirId, inf_span: Span, kind: InferKind<'v>) -> Self::Result {
384        self.visit_id(inf_id)
385    }
386
387    fn visit_lifetime(&mut self, lifetime: &'v Lifetime) -> Self::Result {
388        walk_lifetime(self, lifetime)
389    }
390
391    fn visit_expr(&mut self, ex: &'v Expr<'v>) -> Self::Result {
392        walk_expr(self, ex)
393    }
394    fn visit_expr_field(&mut self, field: &'v ExprField<'v>) -> Self::Result {
395        walk_expr_field(self, field)
396    }
397    fn visit_pattern_type_pattern(&mut self, p: &'v TyPat<'v>) -> Self::Result {
398        walk_ty_pat(self, p)
399    }
400    fn visit_generic_param(&mut self, p: &'v GenericParam<'v>) -> Self::Result {
401        walk_generic_param(self, p)
402    }
403    fn visit_const_param_default(&mut self, _param: HirId, ct: &'v ConstArg<'v>) -> Self::Result {
404        walk_const_param_default(self, ct)
405    }
406    fn visit_generics(&mut self, g: &'v Generics<'v>) -> Self::Result {
407        walk_generics(self, g)
408    }
409    fn visit_where_predicate(&mut self, predicate: &'v WherePredicate<'v>) -> Self::Result {
410        walk_where_predicate(self, predicate)
411    }
412    fn visit_fn_ret_ty(&mut self, ret_ty: &'v FnRetTy<'v>) -> Self::Result {
413        walk_fn_ret_ty(self, ret_ty)
414    }
415    fn visit_fn_decl(&mut self, fd: &'v FnDecl<'v>) -> Self::Result {
416        walk_fn_decl(self, fd)
417    }
418    fn visit_fn(
419        &mut self,
420        fk: FnKind<'v>,
421        fd: &'v FnDecl<'v>,
422        b: BodyId,
423        _: Span,
424        id: LocalDefId,
425    ) -> Self::Result {
426        walk_fn(self, fk, fd, b, id)
427    }
428    fn visit_use(&mut self, path: &'v UsePath<'v>, hir_id: HirId) -> Self::Result {
429        walk_use(self, path, hir_id)
430    }
431    fn visit_trait_item(&mut self, ti: &'v TraitItem<'v>) -> Self::Result {
432        walk_trait_item(self, ti)
433    }
434    fn visit_trait_item_ref(&mut self, ii: &'v TraitItemRef) -> Self::Result {
435        walk_trait_item_ref(self, ii)
436    }
437    fn visit_impl_item(&mut self, ii: &'v ImplItem<'v>) -> Self::Result {
438        walk_impl_item(self, ii)
439    }
440    fn visit_foreign_item_ref(&mut self, ii: &'v ForeignItemRef) -> Self::Result {
441        walk_foreign_item_ref(self, ii)
442    }
443    fn visit_impl_item_ref(&mut self, ii: &'v ImplItemRef) -> Self::Result {
444        walk_impl_item_ref(self, ii)
445    }
446    fn visit_trait_ref(&mut self, t: &'v TraitRef<'v>) -> Self::Result {
447        walk_trait_ref(self, t)
448    }
449    fn visit_param_bound(&mut self, bounds: &'v GenericBound<'v>) -> Self::Result {
450        walk_param_bound(self, bounds)
451    }
452    fn visit_precise_capturing_arg(&mut self, arg: &'v PreciseCapturingArg<'v>) -> Self::Result {
453        walk_precise_capturing_arg(self, arg)
454    }
455    fn visit_poly_trait_ref(&mut self, t: &'v PolyTraitRef<'v>) -> Self::Result {
456        walk_poly_trait_ref(self, t)
457    }
458    fn visit_opaque_ty(&mut self, opaque: &'v OpaqueTy<'v>) -> Self::Result {
459        walk_opaque_ty(self, opaque)
460    }
461    fn visit_variant_data(&mut self, s: &'v VariantData<'v>) -> Self::Result {
462        walk_struct_def(self, s)
463    }
464    fn visit_field_def(&mut self, s: &'v FieldDef<'v>) -> Self::Result {
465        walk_field_def(self, s)
466    }
467    fn visit_enum_def(&mut self, enum_definition: &'v EnumDef<'v>) -> Self::Result {
468        walk_enum_def(self, enum_definition)
469    }
470    fn visit_variant(&mut self, v: &'v Variant<'v>) -> Self::Result {
471        walk_variant(self, v)
472    }
473    fn visit_label(&mut self, label: &'v Label) -> Self::Result {
474        walk_label(self, label)
475    }
476    // The span is that of the surrounding type/pattern/expr/whatever.
477    fn visit_qpath(&mut self, qpath: &'v QPath<'v>, id: HirId, _span: Span) -> Self::Result {
478        walk_qpath(self, qpath, id)
479    }
480    fn visit_path(&mut self, path: &Path<'v>, _id: HirId) -> Self::Result {
481        walk_path(self, path)
482    }
483    fn visit_path_segment(&mut self, path_segment: &'v PathSegment<'v>) -> Self::Result {
484        walk_path_segment(self, path_segment)
485    }
486    fn visit_generic_args(&mut self, generic_args: &'v GenericArgs<'v>) -> Self::Result {
487        walk_generic_args(self, generic_args)
488    }
489    fn visit_assoc_item_constraint(
490        &mut self,
491        constraint: &'v AssocItemConstraint<'v>,
492    ) -> Self::Result {
493        walk_assoc_item_constraint(self, constraint)
494    }
495    fn visit_attribute(&mut self, _attr: &'v Attribute) -> Self::Result {
496        Self::Result::output()
497    }
498    fn visit_associated_item_kind(&mut self, kind: &'v AssocItemKind) -> Self::Result {
499        walk_associated_item_kind(self, kind)
500    }
501    fn visit_defaultness(&mut self, defaultness: &'v Defaultness) -> Self::Result {
502        walk_defaultness(self, defaultness)
503    }
504    fn visit_inline_asm(&mut self, asm: &'v InlineAsm<'v>, id: HirId) -> Self::Result {
505        walk_inline_asm(self, asm, id)
506    }
507}
508
509pub trait VisitorExt<'v>: Visitor<'v> {
510    /// Extension trait method to visit types in unambiguous positions, this is not
511    /// directly on the [`Visitor`] trait as this method should never be overridden.
512    ///
513    /// Named `visit_ty_unambig` instead of `visit_unambig_ty` to aid in discovery
514    /// by IDes when `v.visit_ty` is written.
515    fn visit_ty_unambig(&mut self, t: &'v Ty<'v>) -> Self::Result {
516        walk_unambig_ty(self, t)
517    }
518    /// Extension trait method to visit consts in unambiguous positions, this is not
519    /// directly on the [`Visitor`] trait as this method should never be overridden.
520    ///
521    /// Named `visit_const_arg_unambig` instead of `visit_unambig_const_arg` to aid in
522    /// discovery by IDes when `v.visit_const_arg` is written.
523    fn visit_const_arg_unambig(&mut self, c: &'v ConstArg<'v>) -> Self::Result {
524        walk_const_arg(self, c)
525    }
526}
527impl<'v, V: Visitor<'v>> VisitorExt<'v> for V {}
528
529pub fn walk_param<'v, V: Visitor<'v>>(visitor: &mut V, param: &'v Param<'v>) -> V::Result {
530    try_visit!(visitor.visit_id(param.hir_id));
531    visitor.visit_pat(param.pat)
532}
533
534pub fn walk_item<'v, V: Visitor<'v>>(visitor: &mut V, item: &'v Item<'v>) -> V::Result {
535    try_visit!(visitor.visit_id(item.hir_id()));
536    match item.kind {
537        ItemKind::ExternCrate(orig_name, ident) => {
538            visit_opt!(visitor, visit_name, orig_name);
539            try_visit!(visitor.visit_ident(ident));
540        }
541        ItemKind::Use(ref path, kind) => {
542            try_visit!(visitor.visit_use(path, item.hir_id()));
543            match kind {
544                UseKind::Single(ident) => try_visit!(visitor.visit_ident(ident)),
545                UseKind::Glob | UseKind::ListStem => {}
546            }
547        }
548        ItemKind::Static(ident, ref typ, _, body) => {
549            try_visit!(visitor.visit_ident(ident));
550            try_visit!(visitor.visit_ty_unambig(typ));
551            try_visit!(visitor.visit_nested_body(body));
552        }
553        ItemKind::Const(ident, ref typ, ref generics, body) => {
554            try_visit!(visitor.visit_ident(ident));
555            try_visit!(visitor.visit_ty_unambig(typ));
556            try_visit!(visitor.visit_generics(generics));
557            try_visit!(visitor.visit_nested_body(body));
558        }
559        ItemKind::Fn { ident, sig, generics, body: body_id, .. } => {
560            try_visit!(visitor.visit_ident(ident));
561            try_visit!(visitor.visit_fn(
562                FnKind::ItemFn(ident, generics, sig.header),
563                sig.decl,
564                body_id,
565                item.span,
566                item.owner_id.def_id,
567            ));
568        }
569        ItemKind::Macro(ident, _def, _kind) => {
570            try_visit!(visitor.visit_ident(ident));
571        }
572        ItemKind::Mod(ident, ref module) => {
573            try_visit!(visitor.visit_ident(ident));
574            try_visit!(visitor.visit_mod(module, item.span, item.hir_id()));
575        }
576        ItemKind::ForeignMod { abi: _, items } => {
577            walk_list!(visitor, visit_foreign_item_ref, items);
578        }
579        ItemKind::GlobalAsm { asm: _, fake_body } => {
580            // Visit the fake body, which contains the asm statement.
581            // Therefore we should not visit the asm statement again
582            // outside of the body, or some visitors won't have their
583            // typeck results set correctly.
584            try_visit!(visitor.visit_nested_body(fake_body));
585        }
586        ItemKind::TyAlias(ident, ref ty, ref generics) => {
587            try_visit!(visitor.visit_ident(ident));
588            try_visit!(visitor.visit_ty_unambig(ty));
589            try_visit!(visitor.visit_generics(generics));
590        }
591        ItemKind::Enum(ident, ref enum_definition, ref generics) => {
592            try_visit!(visitor.visit_ident(ident));
593            try_visit!(visitor.visit_generics(generics));
594            try_visit!(visitor.visit_enum_def(enum_definition));
595        }
596        ItemKind::Impl(Impl {
597            constness: _,
598            safety: _,
599            defaultness: _,
600            polarity: _,
601            defaultness_span: _,
602            generics,
603            of_trait,
604            self_ty,
605            items,
606        }) => {
607            try_visit!(visitor.visit_generics(generics));
608            visit_opt!(visitor, visit_trait_ref, of_trait);
609            try_visit!(visitor.visit_ty_unambig(self_ty));
610            walk_list!(visitor, visit_impl_item_ref, *items);
611        }
612        ItemKind::Struct(ident, ref struct_definition, ref generics)
613        | ItemKind::Union(ident, ref struct_definition, ref generics) => {
614            try_visit!(visitor.visit_ident(ident));
615            try_visit!(visitor.visit_generics(generics));
616            try_visit!(visitor.visit_variant_data(struct_definition));
617        }
618        ItemKind::Trait(_is_auto, _safety, ident, ref generics, bounds, trait_item_refs) => {
619            try_visit!(visitor.visit_ident(ident));
620            try_visit!(visitor.visit_generics(generics));
621            walk_list!(visitor, visit_param_bound, bounds);
622            walk_list!(visitor, visit_trait_item_ref, trait_item_refs);
623        }
624        ItemKind::TraitAlias(ident, ref generics, bounds) => {
625            try_visit!(visitor.visit_ident(ident));
626            try_visit!(visitor.visit_generics(generics));
627            walk_list!(visitor, visit_param_bound, bounds);
628        }
629    }
630    V::Result::output()
631}
632
633pub fn walk_body<'v, V: Visitor<'v>>(visitor: &mut V, body: &Body<'v>) -> V::Result {
634    walk_list!(visitor, visit_param, body.params);
635    visitor.visit_expr(body.value)
636}
637
638pub fn walk_ident<'v, V: Visitor<'v>>(visitor: &mut V, ident: Ident) -> V::Result {
639    visitor.visit_name(ident.name)
640}
641
642pub fn walk_mod<'v, V: Visitor<'v>>(visitor: &mut V, module: &'v Mod<'v>) -> V::Result {
643    walk_list!(visitor, visit_nested_item, module.item_ids.iter().copied());
644    V::Result::output()
645}
646
647pub fn walk_foreign_item<'v, V: Visitor<'v>>(
648    visitor: &mut V,
649    foreign_item: &'v ForeignItem<'v>,
650) -> V::Result {
651    try_visit!(visitor.visit_id(foreign_item.hir_id()));
652    try_visit!(visitor.visit_ident(foreign_item.ident));
653
654    match foreign_item.kind {
655        ForeignItemKind::Fn(ref sig, param_idents, ref generics) => {
656            try_visit!(visitor.visit_generics(generics));
657            try_visit!(visitor.visit_fn_decl(sig.decl));
658            for ident in param_idents.iter().copied() {
659                visit_opt!(visitor, visit_ident, ident);
660            }
661        }
662        ForeignItemKind::Static(ref typ, _, _) => {
663            try_visit!(visitor.visit_ty_unambig(typ));
664        }
665        ForeignItemKind::Type => (),
666    }
667    V::Result::output()
668}
669
670pub fn walk_local<'v, V: Visitor<'v>>(visitor: &mut V, local: &'v LetStmt<'v>) -> V::Result {
671    // Intentionally visiting the expr first - the initialization expr
672    // dominates the local's definition.
673    visit_opt!(visitor, visit_expr, local.init);
674    try_visit!(visitor.visit_id(local.hir_id));
675    try_visit!(visitor.visit_pat(local.pat));
676    visit_opt!(visitor, visit_block, local.els);
677    visit_opt!(visitor, visit_ty_unambig, local.ty);
678    V::Result::output()
679}
680
681pub fn walk_block<'v, V: Visitor<'v>>(visitor: &mut V, block: &'v Block<'v>) -> V::Result {
682    try_visit!(visitor.visit_id(block.hir_id));
683    walk_list!(visitor, visit_stmt, block.stmts);
684    visit_opt!(visitor, visit_expr, block.expr);
685    V::Result::output()
686}
687
688pub fn walk_stmt<'v, V: Visitor<'v>>(visitor: &mut V, statement: &'v Stmt<'v>) -> V::Result {
689    try_visit!(visitor.visit_id(statement.hir_id));
690    match statement.kind {
691        StmtKind::Let(ref local) => visitor.visit_local(local),
692        StmtKind::Item(item) => visitor.visit_nested_item(item),
693        StmtKind::Expr(ref expression) | StmtKind::Semi(ref expression) => {
694            visitor.visit_expr(expression)
695        }
696    }
697}
698
699pub fn walk_arm<'v, V: Visitor<'v>>(visitor: &mut V, arm: &'v Arm<'v>) -> V::Result {
700    try_visit!(visitor.visit_id(arm.hir_id));
701    try_visit!(visitor.visit_pat(arm.pat));
702    visit_opt!(visitor, visit_expr, arm.guard);
703    visitor.visit_expr(arm.body)
704}
705
706pub fn walk_ty_pat<'v, V: Visitor<'v>>(visitor: &mut V, pattern: &'v TyPat<'v>) -> V::Result {
707    try_visit!(visitor.visit_id(pattern.hir_id));
708    match pattern.kind {
709        TyPatKind::Range(lower_bound, upper_bound) => {
710            try_visit!(visitor.visit_const_arg_unambig(lower_bound));
711            try_visit!(visitor.visit_const_arg_unambig(upper_bound));
712        }
713        TyPatKind::Or(patterns) => walk_list!(visitor, visit_pattern_type_pattern, patterns),
714        TyPatKind::Err(_) => (),
715    }
716    V::Result::output()
717}
718
719pub fn walk_pat<'v, V: Visitor<'v>>(visitor: &mut V, pattern: &'v Pat<'v>) -> V::Result {
720    try_visit!(visitor.visit_id(pattern.hir_id));
721    match pattern.kind {
722        PatKind::TupleStruct(ref qpath, children, _) => {
723            try_visit!(visitor.visit_qpath(qpath, pattern.hir_id, pattern.span));
724            walk_list!(visitor, visit_pat, children);
725        }
726        PatKind::Struct(ref qpath, fields, _) => {
727            try_visit!(visitor.visit_qpath(qpath, pattern.hir_id, pattern.span));
728            walk_list!(visitor, visit_pat_field, fields);
729        }
730        PatKind::Or(pats) => walk_list!(visitor, visit_pat, pats),
731        PatKind::Tuple(tuple_elements, _) => {
732            walk_list!(visitor, visit_pat, tuple_elements);
733        }
734        PatKind::Box(ref subpattern)
735        | PatKind::Deref(ref subpattern)
736        | PatKind::Ref(ref subpattern, _) => {
737            try_visit!(visitor.visit_pat(subpattern));
738        }
739        PatKind::Binding(_, _hir_id, ident, ref optional_subpattern) => {
740            try_visit!(visitor.visit_ident(ident));
741            visit_opt!(visitor, visit_pat, optional_subpattern);
742        }
743        PatKind::Expr(ref expression) => try_visit!(visitor.visit_pat_expr(expression)),
744        PatKind::Range(ref lower_bound, ref upper_bound, _) => {
745            visit_opt!(visitor, visit_pat_expr, lower_bound);
746            visit_opt!(visitor, visit_pat_expr, upper_bound);
747        }
748        PatKind::Missing | PatKind::Never | PatKind::Wild | PatKind::Err(_) => (),
749        PatKind::Slice(prepatterns, ref slice_pattern, postpatterns) => {
750            walk_list!(visitor, visit_pat, prepatterns);
751            visit_opt!(visitor, visit_pat, slice_pattern);
752            walk_list!(visitor, visit_pat, postpatterns);
753        }
754        PatKind::Guard(subpat, condition) => {
755            try_visit!(visitor.visit_pat(subpat));
756            try_visit!(visitor.visit_expr(condition));
757        }
758    }
759    V::Result::output()
760}
761
762pub fn walk_pat_field<'v, V: Visitor<'v>>(visitor: &mut V, field: &'v PatField<'v>) -> V::Result {
763    try_visit!(visitor.visit_id(field.hir_id));
764    try_visit!(visitor.visit_ident(field.ident));
765    visitor.visit_pat(field.pat)
766}
767
768pub fn walk_pat_expr<'v, V: Visitor<'v>>(visitor: &mut V, expr: &'v PatExpr<'v>) -> V::Result {
769    try_visit!(visitor.visit_id(expr.hir_id));
770    match &expr.kind {
771        PatExprKind::Lit { lit, negated } => visitor.visit_lit(expr.hir_id, lit, *negated),
772        PatExprKind::ConstBlock(c) => visitor.visit_inline_const(c),
773        PatExprKind::Path(qpath) => visitor.visit_qpath(qpath, expr.hir_id, expr.span),
774    }
775}
776
777pub fn walk_anon_const<'v, V: Visitor<'v>>(visitor: &mut V, constant: &'v AnonConst) -> V::Result {
778    try_visit!(visitor.visit_id(constant.hir_id));
779    visitor.visit_nested_body(constant.body)
780}
781
782pub fn walk_inline_const<'v, V: Visitor<'v>>(
783    visitor: &mut V,
784    constant: &'v ConstBlock,
785) -> V::Result {
786    try_visit!(visitor.visit_id(constant.hir_id));
787    visitor.visit_nested_body(constant.body)
788}
789
790pub fn walk_expr<'v, V: Visitor<'v>>(visitor: &mut V, expression: &'v Expr<'v>) -> V::Result {
791    try_visit!(visitor.visit_id(expression.hir_id));
792    match expression.kind {
793        ExprKind::Array(subexpressions) => {
794            walk_list!(visitor, visit_expr, subexpressions);
795        }
796        ExprKind::ConstBlock(ref const_block) => {
797            try_visit!(visitor.visit_inline_const(const_block))
798        }
799        ExprKind::Repeat(ref element, ref count) => {
800            try_visit!(visitor.visit_expr(element));
801            try_visit!(visitor.visit_const_arg_unambig(count));
802        }
803        ExprKind::Struct(ref qpath, fields, ref optional_base) => {
804            try_visit!(visitor.visit_qpath(qpath, expression.hir_id, expression.span));
805            walk_list!(visitor, visit_expr_field, fields);
806            match optional_base {
807                StructTailExpr::Base(base) => try_visit!(visitor.visit_expr(base)),
808                StructTailExpr::None | StructTailExpr::DefaultFields(_) => {}
809            }
810        }
811        ExprKind::Tup(subexpressions) => {
812            walk_list!(visitor, visit_expr, subexpressions);
813        }
814        ExprKind::Call(ref callee_expression, arguments) => {
815            try_visit!(visitor.visit_expr(callee_expression));
816            walk_list!(visitor, visit_expr, arguments);
817        }
818        ExprKind::MethodCall(ref segment, receiver, arguments, _) => {
819            try_visit!(visitor.visit_path_segment(segment));
820            try_visit!(visitor.visit_expr(receiver));
821            walk_list!(visitor, visit_expr, arguments);
822        }
823        ExprKind::Use(expr, _) => {
824            try_visit!(visitor.visit_expr(expr));
825        }
826        ExprKind::Binary(_, ref left_expression, ref right_expression) => {
827            try_visit!(visitor.visit_expr(left_expression));
828            try_visit!(visitor.visit_expr(right_expression));
829        }
830        ExprKind::AddrOf(_, _, ref subexpression) | ExprKind::Unary(_, ref subexpression) => {
831            try_visit!(visitor.visit_expr(subexpression));
832        }
833        ExprKind::Cast(ref subexpression, ref typ) | ExprKind::Type(ref subexpression, ref typ) => {
834            try_visit!(visitor.visit_expr(subexpression));
835            try_visit!(visitor.visit_ty_unambig(typ));
836        }
837        ExprKind::DropTemps(ref subexpression) => {
838            try_visit!(visitor.visit_expr(subexpression));
839        }
840        ExprKind::Let(LetExpr { span: _, pat, ty, init, recovered: _ }) => {
841            // match the visit order in walk_local
842            try_visit!(visitor.visit_expr(init));
843            try_visit!(visitor.visit_pat(pat));
844            visit_opt!(visitor, visit_ty_unambig, ty);
845        }
846        ExprKind::If(ref cond, ref then, ref else_opt) => {
847            try_visit!(visitor.visit_expr(cond));
848            try_visit!(visitor.visit_expr(then));
849            visit_opt!(visitor, visit_expr, else_opt);
850        }
851        ExprKind::Loop(ref block, ref opt_label, _, _) => {
852            visit_opt!(visitor, visit_label, opt_label);
853            try_visit!(visitor.visit_block(block));
854        }
855        ExprKind::Match(ref subexpression, arms, _) => {
856            try_visit!(visitor.visit_expr(subexpression));
857            walk_list!(visitor, visit_arm, arms);
858        }
859        ExprKind::Closure(&Closure {
860            def_id,
861            binder: _,
862            bound_generic_params,
863            fn_decl,
864            body,
865            capture_clause: _,
866            fn_decl_span: _,
867            fn_arg_span: _,
868            kind: _,
869            constness: _,
870        }) => {
871            walk_list!(visitor, visit_generic_param, bound_generic_params);
872            try_visit!(visitor.visit_fn(FnKind::Closure, fn_decl, body, expression.span, def_id));
873        }
874        ExprKind::Block(ref block, ref opt_label) => {
875            visit_opt!(visitor, visit_label, opt_label);
876            try_visit!(visitor.visit_block(block));
877        }
878        ExprKind::Assign(ref lhs, ref rhs, _) => {
879            try_visit!(visitor.visit_expr(rhs));
880            try_visit!(visitor.visit_expr(lhs));
881        }
882        ExprKind::AssignOp(_, ref left_expression, ref right_expression) => {
883            try_visit!(visitor.visit_expr(right_expression));
884            try_visit!(visitor.visit_expr(left_expression));
885        }
886        ExprKind::Field(ref subexpression, ident) => {
887            try_visit!(visitor.visit_expr(subexpression));
888            try_visit!(visitor.visit_ident(ident));
889        }
890        ExprKind::Index(ref main_expression, ref index_expression, _) => {
891            try_visit!(visitor.visit_expr(main_expression));
892            try_visit!(visitor.visit_expr(index_expression));
893        }
894        ExprKind::Path(ref qpath) => {
895            try_visit!(visitor.visit_qpath(qpath, expression.hir_id, expression.span));
896        }
897        ExprKind::Break(ref destination, ref opt_expr) => {
898            visit_opt!(visitor, visit_label, &destination.label);
899            visit_opt!(visitor, visit_expr, opt_expr);
900        }
901        ExprKind::Continue(ref destination) => {
902            visit_opt!(visitor, visit_label, &destination.label);
903        }
904        ExprKind::Ret(ref optional_expression) => {
905            visit_opt!(visitor, visit_expr, optional_expression);
906        }
907        ExprKind::Become(ref expr) => try_visit!(visitor.visit_expr(expr)),
908        ExprKind::InlineAsm(ref asm) => {
909            try_visit!(visitor.visit_inline_asm(asm, expression.hir_id));
910        }
911        ExprKind::OffsetOf(ref container, ref fields) => {
912            try_visit!(visitor.visit_ty_unambig(container));
913            walk_list!(visitor, visit_ident, fields.iter().copied());
914        }
915        ExprKind::Yield(ref subexpression, _) => {
916            try_visit!(visitor.visit_expr(subexpression));
917        }
918        ExprKind::UnsafeBinderCast(_kind, expr, ty) => {
919            try_visit!(visitor.visit_expr(expr));
920            visit_opt!(visitor, visit_ty_unambig, ty);
921        }
922        ExprKind::Lit(lit) => try_visit!(visitor.visit_lit(expression.hir_id, lit, false)),
923        ExprKind::Err(_) => {}
924    }
925    V::Result::output()
926}
927
928pub fn walk_expr_field<'v, V: Visitor<'v>>(visitor: &mut V, field: &'v ExprField<'v>) -> V::Result {
929    try_visit!(visitor.visit_id(field.hir_id));
930    try_visit!(visitor.visit_ident(field.ident));
931    visitor.visit_expr(field.expr)
932}
933/// We track whether an infer var is from a [`Ty`], [`ConstArg`], or [`GenericArg`] so that
934/// HIR visitors overriding [`Visitor::visit_infer`] can determine what kind of infer is being visited
935pub enum InferKind<'hir> {
936    Ty(&'hir Ty<'hir>),
937    Const(&'hir ConstArg<'hir>),
938    Ambig(&'hir InferArg),
939}
940
941pub fn walk_generic_arg<'v, V: Visitor<'v>>(
942    visitor: &mut V,
943    generic_arg: &'v GenericArg<'v>,
944) -> V::Result {
945    match generic_arg {
946        GenericArg::Lifetime(lt) => visitor.visit_lifetime(lt),
947        GenericArg::Type(ty) => visitor.visit_ty(ty),
948        GenericArg::Const(ct) => visitor.visit_const_arg(ct),
949        GenericArg::Infer(inf) => visitor.visit_infer(inf.hir_id, inf.span, InferKind::Ambig(inf)),
950    }
951}
952
953pub fn walk_unambig_ty<'v, V: Visitor<'v>>(visitor: &mut V, typ: &'v Ty<'v>) -> V::Result {
954    match typ.try_as_ambig_ty() {
955        Some(ambig_ty) => visitor.visit_ty(ambig_ty),
956        None => {
957            try_visit!(visitor.visit_id(typ.hir_id));
958            visitor.visit_infer(typ.hir_id, typ.span, InferKind::Ty(typ))
959        }
960    }
961}
962
963pub fn walk_ty<'v, V: Visitor<'v>>(visitor: &mut V, typ: &'v Ty<'v, AmbigArg>) -> V::Result {
964    try_visit!(visitor.visit_id(typ.hir_id));
965
966    match typ.kind {
967        TyKind::Slice(ref ty) => try_visit!(visitor.visit_ty_unambig(ty)),
968        TyKind::Ptr(ref mutable_type) => try_visit!(visitor.visit_ty_unambig(mutable_type.ty)),
969        TyKind::Ref(ref lifetime, ref mutable_type) => {
970            try_visit!(visitor.visit_lifetime(lifetime));
971            try_visit!(visitor.visit_ty_unambig(mutable_type.ty));
972        }
973        TyKind::Never => {}
974        TyKind::Tup(tuple_element_types) => {
975            walk_list!(visitor, visit_ty_unambig, tuple_element_types);
976        }
977        TyKind::BareFn(ref function_declaration) => {
978            walk_list!(visitor, visit_generic_param, function_declaration.generic_params);
979            try_visit!(visitor.visit_fn_decl(function_declaration.decl));
980        }
981        TyKind::UnsafeBinder(ref unsafe_binder) => {
982            walk_list!(visitor, visit_generic_param, unsafe_binder.generic_params);
983            try_visit!(visitor.visit_ty_unambig(unsafe_binder.inner_ty));
984        }
985        TyKind::Path(ref qpath) => {
986            try_visit!(visitor.visit_qpath(qpath, typ.hir_id, typ.span));
987        }
988        TyKind::OpaqueDef(opaque) => {
989            try_visit!(visitor.visit_opaque_ty(opaque));
990        }
991        TyKind::TraitAscription(bounds) => {
992            walk_list!(visitor, visit_param_bound, bounds);
993        }
994        TyKind::Array(ref ty, ref length) => {
995            try_visit!(visitor.visit_ty_unambig(ty));
996            try_visit!(visitor.visit_const_arg_unambig(length));
997        }
998        TyKind::TraitObject(bounds, ref lifetime) => {
999            for bound in bounds {
1000                try_visit!(visitor.visit_poly_trait_ref(bound));
1001            }
1002            try_visit!(visitor.visit_lifetime(lifetime));
1003        }
1004        TyKind::Typeof(ref expression) => try_visit!(visitor.visit_anon_const(expression)),
1005        TyKind::InferDelegation(..) | TyKind::Err(_) => {}
1006        TyKind::Pat(ty, pat) => {
1007            try_visit!(visitor.visit_ty_unambig(ty));
1008            try_visit!(visitor.visit_pattern_type_pattern(pat));
1009        }
1010    }
1011    V::Result::output()
1012}
1013
1014pub fn walk_const_arg<'v, V: Visitor<'v>>(
1015    visitor: &mut V,
1016    const_arg: &'v ConstArg<'v>,
1017) -> V::Result {
1018    match const_arg.try_as_ambig_ct() {
1019        Some(ambig_ct) => visitor.visit_const_arg(ambig_ct),
1020        None => {
1021            try_visit!(visitor.visit_id(const_arg.hir_id));
1022            visitor.visit_infer(const_arg.hir_id, const_arg.span(), InferKind::Const(const_arg))
1023        }
1024    }
1025}
1026
1027pub fn walk_ambig_const_arg<'v, V: Visitor<'v>>(
1028    visitor: &mut V,
1029    const_arg: &'v ConstArg<'v, AmbigArg>,
1030) -> V::Result {
1031    try_visit!(visitor.visit_id(const_arg.hir_id));
1032    match &const_arg.kind {
1033        ConstArgKind::Path(qpath) => visitor.visit_qpath(qpath, const_arg.hir_id, qpath.span()),
1034        ConstArgKind::Anon(anon) => visitor.visit_anon_const(*anon),
1035    }
1036}
1037
1038pub fn walk_generic_param<'v, V: Visitor<'v>>(
1039    visitor: &mut V,
1040    param: &'v GenericParam<'v>,
1041) -> V::Result {
1042    try_visit!(visitor.visit_id(param.hir_id));
1043    match param.name {
1044        ParamName::Plain(ident) | ParamName::Error(ident) => try_visit!(visitor.visit_ident(ident)),
1045        ParamName::Fresh => {}
1046    }
1047    match param.kind {
1048        GenericParamKind::Lifetime { .. } => {}
1049        GenericParamKind::Type { ref default, .. } => {
1050            visit_opt!(visitor, visit_ty_unambig, default)
1051        }
1052        GenericParamKind::Const { ref ty, ref default, synthetic: _ } => {
1053            try_visit!(visitor.visit_ty_unambig(ty));
1054            if let Some(default) = default {
1055                try_visit!(visitor.visit_const_param_default(param.hir_id, default));
1056            }
1057        }
1058    }
1059    V::Result::output()
1060}
1061
1062pub fn walk_const_param_default<'v, V: Visitor<'v>>(
1063    visitor: &mut V,
1064    ct: &'v ConstArg<'v>,
1065) -> V::Result {
1066    visitor.visit_const_arg_unambig(ct)
1067}
1068
1069pub fn walk_generics<'v, V: Visitor<'v>>(visitor: &mut V, generics: &'v Generics<'v>) -> V::Result {
1070    walk_list!(visitor, visit_generic_param, generics.params);
1071    walk_list!(visitor, visit_where_predicate, generics.predicates);
1072    V::Result::output()
1073}
1074
1075pub fn walk_where_predicate<'v, V: Visitor<'v>>(
1076    visitor: &mut V,
1077    predicate: &'v WherePredicate<'v>,
1078) -> V::Result {
1079    let &WherePredicate { hir_id, kind, span: _ } = predicate;
1080    try_visit!(visitor.visit_id(hir_id));
1081    match *kind {
1082        WherePredicateKind::BoundPredicate(WhereBoundPredicate {
1083            ref bounded_ty,
1084            bounds,
1085            bound_generic_params,
1086            origin: _,
1087        }) => {
1088            try_visit!(visitor.visit_ty_unambig(bounded_ty));
1089            walk_list!(visitor, visit_param_bound, bounds);
1090            walk_list!(visitor, visit_generic_param, bound_generic_params);
1091        }
1092        WherePredicateKind::RegionPredicate(WhereRegionPredicate {
1093            ref lifetime,
1094            bounds,
1095            in_where_clause: _,
1096        }) => {
1097            try_visit!(visitor.visit_lifetime(lifetime));
1098            walk_list!(visitor, visit_param_bound, bounds);
1099        }
1100        WherePredicateKind::EqPredicate(WhereEqPredicate { ref lhs_ty, ref rhs_ty }) => {
1101            try_visit!(visitor.visit_ty_unambig(lhs_ty));
1102            try_visit!(visitor.visit_ty_unambig(rhs_ty));
1103        }
1104    }
1105    V::Result::output()
1106}
1107
1108pub fn walk_fn_decl<'v, V: Visitor<'v>>(
1109    visitor: &mut V,
1110    function_declaration: &'v FnDecl<'v>,
1111) -> V::Result {
1112    walk_list!(visitor, visit_ty_unambig, function_declaration.inputs);
1113    visitor.visit_fn_ret_ty(&function_declaration.output)
1114}
1115
1116pub fn walk_fn_ret_ty<'v, V: Visitor<'v>>(visitor: &mut V, ret_ty: &'v FnRetTy<'v>) -> V::Result {
1117    if let FnRetTy::Return(output_ty) = *ret_ty {
1118        try_visit!(visitor.visit_ty_unambig(output_ty));
1119    }
1120    V::Result::output()
1121}
1122
1123pub fn walk_fn<'v, V: Visitor<'v>>(
1124    visitor: &mut V,
1125    function_kind: FnKind<'v>,
1126    function_declaration: &'v FnDecl<'v>,
1127    body_id: BodyId,
1128    _: LocalDefId,
1129) -> V::Result {
1130    try_visit!(visitor.visit_fn_decl(function_declaration));
1131    try_visit!(walk_fn_kind(visitor, function_kind));
1132    visitor.visit_nested_body(body_id)
1133}
1134
1135pub fn walk_fn_kind<'v, V: Visitor<'v>>(visitor: &mut V, function_kind: FnKind<'v>) -> V::Result {
1136    match function_kind {
1137        FnKind::ItemFn(_, generics, ..) => {
1138            try_visit!(visitor.visit_generics(generics));
1139        }
1140        FnKind::Closure | FnKind::Method(..) => {}
1141    }
1142    V::Result::output()
1143}
1144
1145pub fn walk_use<'v, V: Visitor<'v>>(
1146    visitor: &mut V,
1147    path: &'v UsePath<'v>,
1148    hir_id: HirId,
1149) -> V::Result {
1150    let UsePath { segments, ref res, span } = *path;
1151    for &res in res {
1152        try_visit!(visitor.visit_path(&Path { segments, res, span }, hir_id));
1153    }
1154    V::Result::output()
1155}
1156
1157pub fn walk_trait_item<'v, V: Visitor<'v>>(
1158    visitor: &mut V,
1159    trait_item: &'v TraitItem<'v>,
1160) -> V::Result {
1161    // N.B., deliberately force a compilation error if/when new fields are added.
1162    let TraitItem { ident, generics, ref defaultness, ref kind, span, owner_id: _ } = *trait_item;
1163    let hir_id = trait_item.hir_id();
1164    try_visit!(visitor.visit_ident(ident));
1165    try_visit!(visitor.visit_generics(&generics));
1166    try_visit!(visitor.visit_defaultness(&defaultness));
1167    try_visit!(visitor.visit_id(hir_id));
1168    match *kind {
1169        TraitItemKind::Const(ref ty, default) => {
1170            try_visit!(visitor.visit_ty_unambig(ty));
1171            visit_opt!(visitor, visit_nested_body, default);
1172        }
1173        TraitItemKind::Fn(ref sig, TraitFn::Required(param_idents)) => {
1174            try_visit!(visitor.visit_fn_decl(sig.decl));
1175            for ident in param_idents.iter().copied() {
1176                visit_opt!(visitor, visit_ident, ident);
1177            }
1178        }
1179        TraitItemKind::Fn(ref sig, TraitFn::Provided(body_id)) => {
1180            try_visit!(visitor.visit_fn(
1181                FnKind::Method(ident, sig),
1182                sig.decl,
1183                body_id,
1184                span,
1185                trait_item.owner_id.def_id,
1186            ));
1187        }
1188        TraitItemKind::Type(bounds, ref default) => {
1189            walk_list!(visitor, visit_param_bound, bounds);
1190            visit_opt!(visitor, visit_ty_unambig, default);
1191        }
1192    }
1193    V::Result::output()
1194}
1195
1196pub fn walk_trait_item_ref<'v, V: Visitor<'v>>(
1197    visitor: &mut V,
1198    trait_item_ref: &'v TraitItemRef,
1199) -> V::Result {
1200    // N.B., deliberately force a compilation error if/when new fields are added.
1201    let TraitItemRef { id, ident, ref kind, span: _ } = *trait_item_ref;
1202    try_visit!(visitor.visit_nested_trait_item(id));
1203    try_visit!(visitor.visit_ident(ident));
1204    visitor.visit_associated_item_kind(kind)
1205}
1206
1207pub fn walk_impl_item<'v, V: Visitor<'v>>(
1208    visitor: &mut V,
1209    impl_item: &'v ImplItem<'v>,
1210) -> V::Result {
1211    // N.B., deliberately force a compilation error if/when new fields are added.
1212    let ImplItem {
1213        owner_id: _,
1214        ident,
1215        ref generics,
1216        ref kind,
1217        ref defaultness,
1218        span: _,
1219        vis_span: _,
1220    } = *impl_item;
1221
1222    try_visit!(visitor.visit_ident(ident));
1223    try_visit!(visitor.visit_generics(generics));
1224    try_visit!(visitor.visit_defaultness(defaultness));
1225    try_visit!(visitor.visit_id(impl_item.hir_id()));
1226    match *kind {
1227        ImplItemKind::Const(ref ty, body) => {
1228            try_visit!(visitor.visit_ty_unambig(ty));
1229            visitor.visit_nested_body(body)
1230        }
1231        ImplItemKind::Fn(ref sig, body_id) => visitor.visit_fn(
1232            FnKind::Method(impl_item.ident, sig),
1233            sig.decl,
1234            body_id,
1235            impl_item.span,
1236            impl_item.owner_id.def_id,
1237        ),
1238        ImplItemKind::Type(ref ty) => visitor.visit_ty_unambig(ty),
1239    }
1240}
1241
1242pub fn walk_foreign_item_ref<'v, V: Visitor<'v>>(
1243    visitor: &mut V,
1244    foreign_item_ref: &'v ForeignItemRef,
1245) -> V::Result {
1246    // N.B., deliberately force a compilation error if/when new fields are added.
1247    let ForeignItemRef { id, ident, span: _ } = *foreign_item_ref;
1248    try_visit!(visitor.visit_nested_foreign_item(id));
1249    visitor.visit_ident(ident)
1250}
1251
1252pub fn walk_impl_item_ref<'v, V: Visitor<'v>>(
1253    visitor: &mut V,
1254    impl_item_ref: &'v ImplItemRef,
1255) -> V::Result {
1256    // N.B., deliberately force a compilation error if/when new fields are added.
1257    let ImplItemRef { id, ident, ref kind, span: _, trait_item_def_id: _ } = *impl_item_ref;
1258    try_visit!(visitor.visit_nested_impl_item(id));
1259    try_visit!(visitor.visit_ident(ident));
1260    visitor.visit_associated_item_kind(kind)
1261}
1262
1263pub fn walk_trait_ref<'v, V: Visitor<'v>>(
1264    visitor: &mut V,
1265    trait_ref: &'v TraitRef<'v>,
1266) -> V::Result {
1267    try_visit!(visitor.visit_id(trait_ref.hir_ref_id));
1268    visitor.visit_path(trait_ref.path, trait_ref.hir_ref_id)
1269}
1270
1271pub fn walk_param_bound<'v, V: Visitor<'v>>(
1272    visitor: &mut V,
1273    bound: &'v GenericBound<'v>,
1274) -> V::Result {
1275    match *bound {
1276        GenericBound::Trait(ref typ) => visitor.visit_poly_trait_ref(typ),
1277        GenericBound::Outlives(ref lifetime) => visitor.visit_lifetime(lifetime),
1278        GenericBound::Use(args, _) => {
1279            walk_list!(visitor, visit_precise_capturing_arg, args);
1280            V::Result::output()
1281        }
1282    }
1283}
1284
1285pub fn walk_precise_capturing_arg<'v, V: Visitor<'v>>(
1286    visitor: &mut V,
1287    arg: &'v PreciseCapturingArg<'v>,
1288) -> V::Result {
1289    match *arg {
1290        PreciseCapturingArg::Lifetime(lt) => visitor.visit_lifetime(lt),
1291        PreciseCapturingArg::Param(param) => visitor.visit_id(param.hir_id),
1292    }
1293}
1294
1295pub fn walk_poly_trait_ref<'v, V: Visitor<'v>>(
1296    visitor: &mut V,
1297    trait_ref: &'v PolyTraitRef<'v>,
1298) -> V::Result {
1299    walk_list!(visitor, visit_generic_param, trait_ref.bound_generic_params);
1300    visitor.visit_trait_ref(&trait_ref.trait_ref)
1301}
1302
1303pub fn walk_opaque_ty<'v, V: Visitor<'v>>(visitor: &mut V, opaque: &'v OpaqueTy<'v>) -> V::Result {
1304    let &OpaqueTy { hir_id, def_id: _, bounds, origin: _, span: _ } = opaque;
1305    try_visit!(visitor.visit_id(hir_id));
1306    walk_list!(visitor, visit_param_bound, bounds);
1307    V::Result::output()
1308}
1309
1310pub fn walk_struct_def<'v, V: Visitor<'v>>(
1311    visitor: &mut V,
1312    struct_definition: &'v VariantData<'v>,
1313) -> V::Result {
1314    visit_opt!(visitor, visit_id, struct_definition.ctor_hir_id());
1315    walk_list!(visitor, visit_field_def, struct_definition.fields());
1316    V::Result::output()
1317}
1318
1319pub fn walk_field_def<'v, V: Visitor<'v>>(
1320    visitor: &mut V,
1321    FieldDef { hir_id, ident, ty, default, span: _, vis_span: _, def_id: _, safety: _ }: &'v FieldDef<'v>,
1322) -> V::Result {
1323    try_visit!(visitor.visit_id(*hir_id));
1324    try_visit!(visitor.visit_ident(*ident));
1325    visit_opt!(visitor, visit_anon_const, default);
1326    visitor.visit_ty_unambig(*ty)
1327}
1328
1329pub fn walk_enum_def<'v, V: Visitor<'v>>(
1330    visitor: &mut V,
1331    enum_definition: &'v EnumDef<'v>,
1332) -> V::Result {
1333    walk_list!(visitor, visit_variant, enum_definition.variants);
1334    V::Result::output()
1335}
1336
1337pub fn walk_variant<'v, V: Visitor<'v>>(visitor: &mut V, variant: &'v Variant<'v>) -> V::Result {
1338    try_visit!(visitor.visit_ident(variant.ident));
1339    try_visit!(visitor.visit_id(variant.hir_id));
1340    try_visit!(visitor.visit_variant_data(&variant.data));
1341    visit_opt!(visitor, visit_anon_const, &variant.disr_expr);
1342    V::Result::output()
1343}
1344
1345pub fn walk_label<'v, V: Visitor<'v>>(visitor: &mut V, label: &'v Label) -> V::Result {
1346    visitor.visit_ident(label.ident)
1347}
1348
1349pub fn walk_inf<'v, V: Visitor<'v>>(visitor: &mut V, inf: &'v InferArg) -> V::Result {
1350    visitor.visit_id(inf.hir_id)
1351}
1352
1353pub fn walk_lifetime<'v, V: Visitor<'v>>(visitor: &mut V, lifetime: &'v Lifetime) -> V::Result {
1354    try_visit!(visitor.visit_id(lifetime.hir_id));
1355    visitor.visit_ident(lifetime.ident)
1356}
1357
1358pub fn walk_qpath<'v, V: Visitor<'v>>(
1359    visitor: &mut V,
1360    qpath: &'v QPath<'v>,
1361    id: HirId,
1362) -> V::Result {
1363    match *qpath {
1364        QPath::Resolved(ref maybe_qself, ref path) => {
1365            visit_opt!(visitor, visit_ty_unambig, maybe_qself);
1366            visitor.visit_path(path, id)
1367        }
1368        QPath::TypeRelative(ref qself, ref segment) => {
1369            try_visit!(visitor.visit_ty_unambig(qself));
1370            visitor.visit_path_segment(segment)
1371        }
1372        QPath::LangItem(..) => V::Result::output(),
1373    }
1374}
1375
1376pub fn walk_path<'v, V: Visitor<'v>>(visitor: &mut V, path: &Path<'v>) -> V::Result {
1377    walk_list!(visitor, visit_path_segment, path.segments);
1378    V::Result::output()
1379}
1380
1381pub fn walk_path_segment<'v, V: Visitor<'v>>(
1382    visitor: &mut V,
1383    segment: &'v PathSegment<'v>,
1384) -> V::Result {
1385    try_visit!(visitor.visit_ident(segment.ident));
1386    try_visit!(visitor.visit_id(segment.hir_id));
1387    visit_opt!(visitor, visit_generic_args, segment.args);
1388    V::Result::output()
1389}
1390
1391pub fn walk_generic_args<'v, V: Visitor<'v>>(
1392    visitor: &mut V,
1393    generic_args: &'v GenericArgs<'v>,
1394) -> V::Result {
1395    walk_list!(visitor, visit_generic_arg, generic_args.args);
1396    walk_list!(visitor, visit_assoc_item_constraint, generic_args.constraints);
1397    V::Result::output()
1398}
1399
1400pub fn walk_assoc_item_constraint<'v, V: Visitor<'v>>(
1401    visitor: &mut V,
1402    constraint: &'v AssocItemConstraint<'v>,
1403) -> V::Result {
1404    try_visit!(visitor.visit_id(constraint.hir_id));
1405    try_visit!(visitor.visit_ident(constraint.ident));
1406    try_visit!(visitor.visit_generic_args(constraint.gen_args));
1407    match constraint.kind {
1408        AssocItemConstraintKind::Equality { ref term } => match term {
1409            Term::Ty(ty) => try_visit!(visitor.visit_ty_unambig(ty)),
1410            Term::Const(c) => try_visit!(visitor.visit_const_arg_unambig(c)),
1411        },
1412        AssocItemConstraintKind::Bound { bounds } => {
1413            walk_list!(visitor, visit_param_bound, bounds)
1414        }
1415    }
1416    V::Result::output()
1417}
1418
1419pub fn walk_associated_item_kind<'v, V: Visitor<'v>>(_: &mut V, _: &'v AssocItemKind) -> V::Result {
1420    // No visitable content here: this fn exists so you can call it if
1421    // the right thing to do, should content be added in the future,
1422    // would be to walk it.
1423    V::Result::output()
1424}
1425
1426pub fn walk_defaultness<'v, V: Visitor<'v>>(_: &mut V, _: &'v Defaultness) -> V::Result {
1427    // No visitable content here: this fn exists so you can call it if
1428    // the right thing to do, should content be added in the future,
1429    // would be to walk it.
1430    V::Result::output()
1431}
1432
1433pub fn walk_inline_asm<'v, V: Visitor<'v>>(
1434    visitor: &mut V,
1435    asm: &'v InlineAsm<'v>,
1436    id: HirId,
1437) -> V::Result {
1438    for (op, op_sp) in asm.operands {
1439        match op {
1440            InlineAsmOperand::In { expr, .. } | InlineAsmOperand::InOut { expr, .. } => {
1441                try_visit!(visitor.visit_expr(expr));
1442            }
1443            InlineAsmOperand::Out { expr, .. } => {
1444                visit_opt!(visitor, visit_expr, expr);
1445            }
1446            InlineAsmOperand::SplitInOut { in_expr, out_expr, .. } => {
1447                try_visit!(visitor.visit_expr(in_expr));
1448                visit_opt!(visitor, visit_expr, out_expr);
1449            }
1450            InlineAsmOperand::Const { anon_const, .. } => {
1451                try_visit!(visitor.visit_inline_const(anon_const));
1452            }
1453            InlineAsmOperand::SymFn { expr, .. } => {
1454                try_visit!(visitor.visit_expr(expr));
1455            }
1456            InlineAsmOperand::SymStatic { path, .. } => {
1457                try_visit!(visitor.visit_qpath(path, id, *op_sp));
1458            }
1459            InlineAsmOperand::Label { block } => try_visit!(visitor.visit_block(block)),
1460        }
1461    }
1462    V::Result::output()
1463}