1 //! HIR walker for walking the contents of nodes.
3 //! Here are the three available patterns for the visitor strategy,
4 //! in roughly the order of desirability:
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 `nested_visit_map`), 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
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 `nested_visit_map`). Walk your crate with
34 //! `tcx.hir().walk_toplevel_module(visitor)` invoked on `tcx.hir().krate()`.
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.
39 //! If you have decided to use this visitor, here are some general
40 //! notes on how to do so:
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.
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`
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
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).
64 //! This order consistency is required in a few places in rustc, for
65 //! example generator inference, and possibly also HIR borrowck.
68 use rustc_ast::walk_list;
69 use rustc_ast::{Attribute, Label};
70 use rustc_span::symbol::{Ident, Symbol};
73 pub trait IntoVisitor<'hir> {
74 type Visitor: Visitor<'hir>;
75 fn into_visitor(&self) -> Self::Visitor;
78 #[derive(Copy, Clone, Debug)]
80 /// `#[xxx] pub async/const/extern "Abi" fn foo()`
81 ItemFn(Ident, &'a Generics<'a>, FnHeader),
84 Method(Ident, &'a FnSig<'a>),
91 pub fn header(&self) -> Option<&FnHeader> {
93 FnKind::ItemFn(_, _, ref header) => Some(header),
94 FnKind::Method(_, ref sig) => Some(&sig.header),
95 FnKind::Closure => None,
99 pub fn constness(self) -> Constness {
100 self.header().map_or(Constness::NotConst, |header| header.constness)
103 pub fn asyncness(self) -> IsAsync {
104 self.header().map_or(IsAsync::NotAsync, |header| header.asyncness)
108 /// An abstract representation of the HIR `rustc_middle::hir::map::Map`.
109 pub trait Map<'hir> {
110 /// Retrieves the `Node` corresponding to `id`, returning `None` if cannot be found.
111 fn find(&self, hir_id: HirId) -> Option<Node<'hir>>;
112 fn body(&self, id: BodyId) -> &'hir Body<'hir>;
113 fn item(&self, id: ItemId) -> &'hir Item<'hir>;
114 fn trait_item(&self, id: TraitItemId) -> &'hir TraitItem<'hir>;
115 fn impl_item(&self, id: ImplItemId) -> &'hir ImplItem<'hir>;
116 fn foreign_item(&self, id: ForeignItemId) -> &'hir ForeignItem<'hir>;
119 // Used when no map is actually available, forcing manual implementation of nested visitors.
120 impl<'hir> Map<'hir> for ! {
121 fn find(&self, _: HirId) -> Option<Node<'hir>> {
124 fn body(&self, _: BodyId) -> &'hir Body<'hir> {
127 fn item(&self, _: ItemId) -> &'hir Item<'hir> {
130 fn trait_item(&self, _: TraitItemId) -> &'hir TraitItem<'hir> {
133 fn impl_item(&self, _: ImplItemId) -> &'hir ImplItem<'hir> {
136 fn foreign_item(&self, _: ForeignItemId) -> &'hir ForeignItem<'hir> {
141 pub mod nested_filter {
144 /// Specifies what nested things a visitor wants to visit. By "nested
145 /// things", we are referring to bits of HIR that are not directly embedded
146 /// within one another but rather indirectly, through a table in the crate.
147 /// This is done to control dependencies during incremental compilation: the
148 /// non-inline bits of HIR can be tracked and hashed separately.
150 /// The most common choice is `OnlyBodies`, which will cause the visitor to
151 /// visit fn bodies for fns that it encounters, and closure bodies, but
152 /// skip over nested item-like things.
154 /// See the comments on `ItemLikeVisitor` for more details on the overall
156 pub trait NestedFilter<'hir> {
159 /// Whether the visitor visits nested "item-like" things.
160 /// E.g., item, impl-item.
162 /// Whether the visitor visits "intra item-like" things.
163 /// E.g., function body, closure, `AnonConst`
167 /// Do not visit any nested things. When you add a new
168 /// "non-nested" thing, you will want to audit such uses to see if
169 /// they remain valid.
171 /// Use this if you are only walking some particular kind of tree
172 /// (i.e., a type, or fn signature) and you don't want to thread a
175 impl NestedFilter<'_> for None {
177 const INTER: bool = false;
178 const INTRA: bool = false;
182 use nested_filter::NestedFilter;
184 /// Each method of the Visitor trait is a hook to be potentially
185 /// overridden. Each method's default implementation recursively visits
186 /// the substructure of the input via the corresponding `walk` method;
187 /// e.g., the `visit_mod` method by default calls `intravisit::walk_mod`.
189 /// Note that this visitor does NOT visit nested items by default
190 /// (this is why the module is called `intravisit`, to distinguish it
191 /// from the AST's `visit` module, which acts differently). If you
192 /// simply want to visit all items in the crate in some order, you
193 /// should call `tcx.hir().visit_all_item_likes_in_crate`. Otherwise, see the comment
194 /// on `visit_nested_item` for details on how to visit nested items.
196 /// If you want to ensure that your code handles every variant
197 /// explicitly, you need to override each method. (And you also need
198 /// to monitor future changes to `Visitor` in case a new method with a
199 /// new default implementation gets introduced.)
200 pub trait Visitor<'v>: Sized {
201 // this type should not be overridden, it exists for convenient usage as `Self::Map`
202 type Map: Map<'v> = <Self::NestedFilter as NestedFilter<'v>>::Map;
204 ///////////////////////////////////////////////////////////////////////////
207 /// Override this type to control which nested HIR are visited; see
208 /// [`NestedFilter`] for details. If you override this type, you
209 /// must also override [`nested_visit_map`](Self::nested_visit_map).
211 /// **If for some reason you want the nested behavior, but don't
212 /// have a `Map` at your disposal:** then override the
213 /// `visit_nested_XXX` methods. If a new `visit_nested_XXX` variant is
214 /// added in the future, it will cause a panic which can be detected
215 /// and fixed appropriately.
216 type NestedFilter: NestedFilter<'v> = nested_filter::None;
218 /// If `type NestedFilter` is set to visit nested items, this method
219 /// must also be overridden to provide a map to retrieve nested items.
220 fn nested_visit_map(&mut self) -> Self::Map {
222 "nested_visit_map must be implemented or consider using \
223 `type NestedFilter = nested_filter::None` (the default)"
227 /// Invoked when a nested item is encountered. By default, when
228 /// `Self::NestedFilter` is `nested_filter::None`, this method does
229 /// nothing. **You probably don't want to override this method** --
230 /// instead, override [`Self::NestedFilter`] or use the "shallow" or
231 /// "deep" visit patterns described on
232 /// `itemlikevisit::ItemLikeVisitor`. The only reason to override
233 /// this method is if you want a nested pattern but cannot supply a
234 /// [`Map`]; see `nested_visit_map` for advice.
235 fn visit_nested_item(&mut self, id: ItemId) {
236 if Self::NestedFilter::INTER {
237 let item = self.nested_visit_map().item(id);
238 self.visit_item(item);
242 /// Like `visit_nested_item()`, but for trait items. See
243 /// `visit_nested_item()` for advice on when to override this
245 fn visit_nested_trait_item(&mut self, id: TraitItemId) {
246 if Self::NestedFilter::INTER {
247 let item = self.nested_visit_map().trait_item(id);
248 self.visit_trait_item(item);
252 /// Like `visit_nested_item()`, but for impl items. See
253 /// `visit_nested_item()` for advice on when to override this
255 fn visit_nested_impl_item(&mut self, id: ImplItemId) {
256 if Self::NestedFilter::INTER {
257 let item = self.nested_visit_map().impl_item(id);
258 self.visit_impl_item(item);
262 /// Like `visit_nested_item()`, but for foreign items. See
263 /// `visit_nested_item()` for advice on when to override this
265 fn visit_nested_foreign_item(&mut self, id: ForeignItemId) {
266 if Self::NestedFilter::INTER {
267 let item = self.nested_visit_map().foreign_item(id);
268 self.visit_foreign_item(item);
272 /// Invoked to visit the body of a function, method or closure. Like
273 /// `visit_nested_item`, does nothing by default unless you override
274 /// `Self::NestedFilter`.
275 fn visit_nested_body(&mut self, id: BodyId) {
276 if Self::NestedFilter::INTRA {
277 let body = self.nested_visit_map().body(id);
278 self.visit_body(body);
282 fn visit_param(&mut self, param: &'v Param<'v>) {
283 walk_param(self, param)
286 /// Visits the top-level item and (optionally) nested items / impl items. See
287 /// `visit_nested_item` for details.
288 fn visit_item(&mut self, i: &'v Item<'v>) {
292 fn visit_body(&mut self, b: &'v Body<'v>) {
296 ///////////////////////////////////////////////////////////////////////////
298 fn visit_id(&mut self, _hir_id: HirId) {
301 fn visit_name(&mut self, _name: Symbol) {
304 fn visit_ident(&mut self, ident: Ident) {
305 walk_ident(self, ident)
307 fn visit_mod(&mut self, m: &'v Mod<'v>, _s: Span, n: HirId) {
310 fn visit_foreign_item(&mut self, i: &'v ForeignItem<'v>) {
311 walk_foreign_item(self, i)
313 fn visit_local(&mut self, l: &'v Local<'v>) {
316 fn visit_block(&mut self, b: &'v Block<'v>) {
319 fn visit_stmt(&mut self, s: &'v Stmt<'v>) {
322 fn visit_arm(&mut self, a: &'v Arm<'v>) {
325 fn visit_pat(&mut self, p: &'v Pat<'v>) {
328 fn visit_pat_field(&mut self, f: &'v PatField<'v>) {
329 walk_pat_field(self, f)
331 fn visit_array_length(&mut self, len: &'v ArrayLen) {
332 walk_array_len(self, len)
334 fn visit_anon_const(&mut self, c: &'v AnonConst) {
335 walk_anon_const(self, c)
337 fn visit_expr(&mut self, ex: &'v Expr<'v>) {
340 fn visit_let_expr(&mut self, lex: &'v Let<'v>) {
341 walk_let_expr(self, lex)
343 fn visit_expr_field(&mut self, field: &'v ExprField<'v>) {
344 walk_expr_field(self, field)
346 fn visit_ty(&mut self, t: &'v Ty<'v>) {
349 fn visit_generic_param(&mut self, p: &'v GenericParam<'v>) {
350 walk_generic_param(self, p)
352 fn visit_const_param_default(&mut self, _param: HirId, ct: &'v AnonConst) {
353 walk_const_param_default(self, ct)
355 fn visit_generics(&mut self, g: &'v Generics<'v>) {
356 walk_generics(self, g)
358 fn visit_where_predicate(&mut self, predicate: &'v WherePredicate<'v>) {
359 walk_where_predicate(self, predicate)
361 fn visit_fn_decl(&mut self, fd: &'v FnDecl<'v>) {
362 walk_fn_decl(self, fd)
364 fn visit_fn(&mut self, fk: FnKind<'v>, fd: &'v FnDecl<'v>, b: BodyId, _: Span, id: HirId) {
365 walk_fn(self, fk, fd, b, id)
367 fn visit_use(&mut self, path: &'v Path<'v>, hir_id: HirId) {
368 walk_use(self, path, hir_id)
370 fn visit_trait_item(&mut self, ti: &'v TraitItem<'v>) {
371 walk_trait_item(self, ti)
373 fn visit_trait_item_ref(&mut self, ii: &'v TraitItemRef) {
374 walk_trait_item_ref(self, ii)
376 fn visit_impl_item(&mut self, ii: &'v ImplItem<'v>) {
377 walk_impl_item(self, ii)
379 fn visit_foreign_item_ref(&mut self, ii: &'v ForeignItemRef) {
380 walk_foreign_item_ref(self, ii)
382 fn visit_impl_item_ref(&mut self, ii: &'v ImplItemRef) {
383 walk_impl_item_ref(self, ii)
385 fn visit_trait_ref(&mut self, t: &'v TraitRef<'v>) {
386 walk_trait_ref(self, t)
388 fn visit_param_bound(&mut self, bounds: &'v GenericBound<'v>) {
389 walk_param_bound(self, bounds)
391 fn visit_poly_trait_ref(&mut self, t: &'v PolyTraitRef<'v>) {
392 walk_poly_trait_ref(self, t)
394 fn visit_variant_data(&mut self, s: &'v VariantData<'v>) {
395 walk_struct_def(self, s)
397 fn visit_field_def(&mut self, s: &'v FieldDef<'v>) {
398 walk_field_def(self, s)
400 fn visit_enum_def(&mut self, enum_definition: &'v EnumDef<'v>, item_id: HirId) {
401 walk_enum_def(self, enum_definition, item_id)
403 fn visit_variant(&mut self, v: &'v Variant<'v>) {
404 walk_variant(self, v)
406 fn visit_label(&mut self, label: &'v Label) {
407 walk_label(self, label)
409 fn visit_infer(&mut self, inf: &'v InferArg) {
412 fn visit_generic_arg(&mut self, generic_arg: &'v GenericArg<'v>) {
414 GenericArg::Lifetime(lt) => self.visit_lifetime(lt),
415 GenericArg::Type(ty) => self.visit_ty(ty),
416 GenericArg::Const(ct) => self.visit_anon_const(&ct.value),
417 GenericArg::Infer(inf) => self.visit_infer(inf),
420 fn visit_lifetime(&mut self, lifetime: &'v Lifetime) {
421 walk_lifetime(self, lifetime)
423 // The span is that of the surrounding type/pattern/expr/whatever.
424 fn visit_qpath(&mut self, qpath: &'v QPath<'v>, id: HirId, _span: Span) {
425 walk_qpath(self, qpath, id)
427 fn visit_path(&mut self, path: &'v Path<'v>, _id: HirId) {
428 walk_path(self, path)
430 fn visit_path_segment(&mut self, path_segment: &'v PathSegment<'v>) {
431 walk_path_segment(self, path_segment)
433 fn visit_generic_args(&mut self, generic_args: &'v GenericArgs<'v>) {
434 walk_generic_args(self, generic_args)
436 fn visit_assoc_type_binding(&mut self, type_binding: &'v TypeBinding<'v>) {
437 walk_assoc_type_binding(self, type_binding)
439 fn visit_attribute(&mut self, _attr: &'v Attribute) {}
440 fn visit_associated_item_kind(&mut self, kind: &'v AssocItemKind) {
441 walk_associated_item_kind(self, kind);
443 fn visit_defaultness(&mut self, defaultness: &'v Defaultness) {
444 walk_defaultness(self, defaultness);
446 fn visit_inline_asm(&mut self, asm: &'v InlineAsm<'v>, id: HirId) {
447 walk_inline_asm(self, asm, id);
451 pub fn walk_mod<'v, V: Visitor<'v>>(visitor: &mut V, module: &'v Mod<'v>, mod_hir_id: HirId) {
452 visitor.visit_id(mod_hir_id);
453 for &item_id in module.item_ids {
454 visitor.visit_nested_item(item_id);
458 pub fn walk_body<'v, V: Visitor<'v>>(visitor: &mut V, body: &'v Body<'v>) {
459 walk_list!(visitor, visit_param, body.params);
460 visitor.visit_expr(&body.value);
463 pub fn walk_local<'v, V: Visitor<'v>>(visitor: &mut V, local: &'v Local<'v>) {
464 // Intentionally visiting the expr first - the initialization expr
465 // dominates the local's definition.
466 walk_list!(visitor, visit_expr, &local.init);
467 visitor.visit_id(local.hir_id);
468 visitor.visit_pat(&local.pat);
469 if let Some(els) = local.els {
470 visitor.visit_block(els);
472 walk_list!(visitor, visit_ty, &local.ty);
475 pub fn walk_ident<'v, V: Visitor<'v>>(visitor: &mut V, ident: Ident) {
476 visitor.visit_name(ident.name);
479 pub fn walk_label<'v, V: Visitor<'v>>(visitor: &mut V, label: &'v Label) {
480 visitor.visit_ident(label.ident);
483 pub fn walk_lifetime<'v, V: Visitor<'v>>(visitor: &mut V, lifetime: &'v Lifetime) {
484 visitor.visit_id(lifetime.hir_id);
485 match lifetime.name {
486 LifetimeName::Param(_, ParamName::Plain(ident)) => {
487 visitor.visit_ident(ident);
489 LifetimeName::Param(_, ParamName::Fresh)
490 | LifetimeName::Param(_, ParamName::Error)
491 | LifetimeName::Static
492 | LifetimeName::Error
493 | LifetimeName::ImplicitObjectLifetimeDefault
494 | LifetimeName::Infer => {}
498 pub fn walk_poly_trait_ref<'v, V: Visitor<'v>>(visitor: &mut V, trait_ref: &'v PolyTraitRef<'v>) {
499 walk_list!(visitor, visit_generic_param, trait_ref.bound_generic_params);
500 visitor.visit_trait_ref(&trait_ref.trait_ref);
503 pub fn walk_trait_ref<'v, V: Visitor<'v>>(visitor: &mut V, trait_ref: &'v TraitRef<'v>) {
504 visitor.visit_id(trait_ref.hir_ref_id);
505 visitor.visit_path(&trait_ref.path, trait_ref.hir_ref_id)
508 pub fn walk_param<'v, V: Visitor<'v>>(visitor: &mut V, param: &'v Param<'v>) {
509 visitor.visit_id(param.hir_id);
510 visitor.visit_pat(¶m.pat);
513 pub fn walk_item<'v, V: Visitor<'v>>(visitor: &mut V, item: &'v Item<'v>) {
514 visitor.visit_ident(item.ident);
516 ItemKind::ExternCrate(orig_name) => {
517 visitor.visit_id(item.hir_id());
518 if let Some(orig_name) = orig_name {
519 visitor.visit_name(orig_name);
522 ItemKind::Use(ref path, _) => {
523 visitor.visit_use(path, item.hir_id());
525 ItemKind::Static(ref typ, _, body) | ItemKind::Const(ref typ, body) => {
526 visitor.visit_id(item.hir_id());
527 visitor.visit_ty(typ);
528 visitor.visit_nested_body(body);
530 ItemKind::Fn(ref sig, ref generics, body_id) => visitor.visit_fn(
531 FnKind::ItemFn(item.ident, generics, sig.header),
537 ItemKind::Macro(..) => {
538 visitor.visit_id(item.hir_id());
540 ItemKind::Mod(ref module) => {
541 // `visit_mod()` takes care of visiting the `Item`'s `HirId`.
542 visitor.visit_mod(module, item.span, item.hir_id())
544 ItemKind::ForeignMod { abi: _, items } => {
545 visitor.visit_id(item.hir_id());
546 walk_list!(visitor, visit_foreign_item_ref, items);
548 ItemKind::GlobalAsm(asm) => {
549 visitor.visit_id(item.hir_id());
550 visitor.visit_inline_asm(asm, item.hir_id());
552 ItemKind::TyAlias(ref ty, ref generics) => {
553 visitor.visit_id(item.hir_id());
554 visitor.visit_ty(ty);
555 visitor.visit_generics(generics)
557 ItemKind::OpaqueTy(OpaqueTy { ref generics, bounds, .. }) => {
558 visitor.visit_id(item.hir_id());
559 walk_generics(visitor, generics);
560 walk_list!(visitor, visit_param_bound, bounds);
562 ItemKind::Enum(ref enum_definition, ref generics) => {
563 visitor.visit_generics(generics);
564 // `visit_enum_def()` takes care of visiting the `Item`'s `HirId`.
565 visitor.visit_enum_def(enum_definition, item.hir_id())
567 ItemKind::Impl(Impl {
578 visitor.visit_id(item.hir_id());
579 visitor.visit_generics(generics);
580 walk_list!(visitor, visit_trait_ref, of_trait);
581 visitor.visit_ty(self_ty);
582 walk_list!(visitor, visit_impl_item_ref, *items);
584 ItemKind::Struct(ref struct_definition, ref generics)
585 | ItemKind::Union(ref struct_definition, ref generics) => {
586 visitor.visit_generics(generics);
587 visitor.visit_id(item.hir_id());
588 visitor.visit_variant_data(struct_definition);
590 ItemKind::Trait(.., ref generics, bounds, trait_item_refs) => {
591 visitor.visit_id(item.hir_id());
592 visitor.visit_generics(generics);
593 walk_list!(visitor, visit_param_bound, bounds);
594 walk_list!(visitor, visit_trait_item_ref, trait_item_refs);
596 ItemKind::TraitAlias(ref generics, bounds) => {
597 visitor.visit_id(item.hir_id());
598 visitor.visit_generics(generics);
599 walk_list!(visitor, visit_param_bound, bounds);
604 pub fn walk_inline_asm<'v, V: Visitor<'v>>(visitor: &mut V, asm: &'v InlineAsm<'v>, id: HirId) {
605 for (op, op_sp) in asm.operands {
607 InlineAsmOperand::In { expr, .. } | InlineAsmOperand::InOut { expr, .. } => {
608 visitor.visit_expr(expr)
610 InlineAsmOperand::Out { expr, .. } => {
611 if let Some(expr) = expr {
612 visitor.visit_expr(expr);
615 InlineAsmOperand::SplitInOut { in_expr, out_expr, .. } => {
616 visitor.visit_expr(in_expr);
617 if let Some(out_expr) = out_expr {
618 visitor.visit_expr(out_expr);
621 InlineAsmOperand::Const { anon_const, .. }
622 | InlineAsmOperand::SymFn { anon_const, .. } => visitor.visit_anon_const(anon_const),
623 InlineAsmOperand::SymStatic { path, .. } => visitor.visit_qpath(path, id, *op_sp),
628 pub fn walk_use<'v, V: Visitor<'v>>(visitor: &mut V, path: &'v Path<'v>, hir_id: HirId) {
629 visitor.visit_id(hir_id);
630 visitor.visit_path(path, hir_id);
633 pub fn walk_enum_def<'v, V: Visitor<'v>>(
635 enum_definition: &'v EnumDef<'v>,
638 visitor.visit_id(item_id);
639 walk_list!(visitor, visit_variant, enum_definition.variants);
642 pub fn walk_variant<'v, V: Visitor<'v>>(visitor: &mut V, variant: &'v Variant<'v>) {
643 visitor.visit_ident(variant.ident);
644 visitor.visit_id(variant.id);
645 visitor.visit_variant_data(&variant.data);
646 walk_list!(visitor, visit_anon_const, &variant.disr_expr);
649 pub fn walk_ty<'v, V: Visitor<'v>>(visitor: &mut V, typ: &'v Ty<'v>) {
650 visitor.visit_id(typ.hir_id);
653 TyKind::Slice(ref ty) => visitor.visit_ty(ty),
654 TyKind::Ptr(ref mutable_type) => visitor.visit_ty(&mutable_type.ty),
655 TyKind::Rptr(ref lifetime, ref mutable_type) => {
656 visitor.visit_lifetime(lifetime);
657 visitor.visit_ty(&mutable_type.ty)
660 TyKind::Tup(tuple_element_types) => {
661 walk_list!(visitor, visit_ty, tuple_element_types);
663 TyKind::BareFn(ref function_declaration) => {
664 walk_list!(visitor, visit_generic_param, function_declaration.generic_params);
665 visitor.visit_fn_decl(&function_declaration.decl);
667 TyKind::Path(ref qpath) => {
668 visitor.visit_qpath(qpath, typ.hir_id, typ.span);
670 TyKind::OpaqueDef(item_id, lifetimes, _in_trait) => {
671 visitor.visit_nested_item(item_id);
672 walk_list!(visitor, visit_generic_arg, lifetimes);
674 TyKind::Array(ref ty, ref length) => {
675 visitor.visit_ty(ty);
676 visitor.visit_array_length(length)
678 TyKind::TraitObject(bounds, ref lifetime, _syntax) => {
679 for bound in bounds {
680 visitor.visit_poly_trait_ref(bound);
682 visitor.visit_lifetime(lifetime);
684 TyKind::Typeof(ref expression) => visitor.visit_anon_const(expression),
685 TyKind::Infer | TyKind::Err => {}
689 pub fn walk_inf<'v, V: Visitor<'v>>(visitor: &mut V, inf: &'v InferArg) {
690 visitor.visit_id(inf.hir_id);
693 pub fn walk_qpath<'v, V: Visitor<'v>>(visitor: &mut V, qpath: &'v QPath<'v>, id: HirId) {
695 QPath::Resolved(ref maybe_qself, ref path) => {
696 walk_list!(visitor, visit_ty, maybe_qself);
697 visitor.visit_path(path, id)
699 QPath::TypeRelative(ref qself, ref segment) => {
700 visitor.visit_ty(qself);
701 visitor.visit_path_segment(segment);
703 QPath::LangItem(..) => {}
707 pub fn walk_path<'v, V: Visitor<'v>>(visitor: &mut V, path: &'v Path<'v>) {
708 for segment in path.segments {
709 visitor.visit_path_segment(segment);
713 pub fn walk_path_segment<'v, V: Visitor<'v>>(visitor: &mut V, segment: &'v PathSegment<'v>) {
714 visitor.visit_ident(segment.ident);
715 visitor.visit_id(segment.hir_id);
716 if let Some(ref args) = segment.args {
717 visitor.visit_generic_args(args);
721 pub fn walk_generic_args<'v, V: Visitor<'v>>(visitor: &mut V, generic_args: &'v GenericArgs<'v>) {
722 walk_list!(visitor, visit_generic_arg, generic_args.args);
723 walk_list!(visitor, visit_assoc_type_binding, generic_args.bindings);
726 pub fn walk_assoc_type_binding<'v, V: Visitor<'v>>(
728 type_binding: &'v TypeBinding<'v>,
730 visitor.visit_id(type_binding.hir_id);
731 visitor.visit_ident(type_binding.ident);
732 visitor.visit_generic_args(type_binding.gen_args);
733 match type_binding.kind {
734 TypeBindingKind::Equality { ref term } => match term {
735 Term::Ty(ref ty) => visitor.visit_ty(ty),
736 Term::Const(ref c) => visitor.visit_anon_const(c),
738 TypeBindingKind::Constraint { bounds } => walk_list!(visitor, visit_param_bound, bounds),
742 pub fn walk_pat<'v, V: Visitor<'v>>(visitor: &mut V, pattern: &'v Pat<'v>) {
743 visitor.visit_id(pattern.hir_id);
745 PatKind::TupleStruct(ref qpath, children, _) => {
746 visitor.visit_qpath(qpath, pattern.hir_id, pattern.span);
747 walk_list!(visitor, visit_pat, children);
749 PatKind::Path(ref qpath) => {
750 visitor.visit_qpath(qpath, pattern.hir_id, pattern.span);
752 PatKind::Struct(ref qpath, fields, _) => {
753 visitor.visit_qpath(qpath, pattern.hir_id, pattern.span);
754 walk_list!(visitor, visit_pat_field, fields);
756 PatKind::Or(pats) => walk_list!(visitor, visit_pat, pats),
757 PatKind::Tuple(tuple_elements, _) => {
758 walk_list!(visitor, visit_pat, tuple_elements);
760 PatKind::Box(ref subpattern) | PatKind::Ref(ref subpattern, _) => {
761 visitor.visit_pat(subpattern)
763 PatKind::Binding(_, _hir_id, ident, ref optional_subpattern) => {
764 visitor.visit_ident(ident);
765 walk_list!(visitor, visit_pat, optional_subpattern);
767 PatKind::Lit(ref expression) => visitor.visit_expr(expression),
768 PatKind::Range(ref lower_bound, ref upper_bound, _) => {
769 walk_list!(visitor, visit_expr, lower_bound);
770 walk_list!(visitor, visit_expr, upper_bound);
773 PatKind::Slice(prepatterns, ref slice_pattern, postpatterns) => {
774 walk_list!(visitor, visit_pat, prepatterns);
775 walk_list!(visitor, visit_pat, slice_pattern);
776 walk_list!(visitor, visit_pat, postpatterns);
781 pub fn walk_pat_field<'v, V: Visitor<'v>>(visitor: &mut V, field: &'v PatField<'v>) {
782 visitor.visit_id(field.hir_id);
783 visitor.visit_ident(field.ident);
784 visitor.visit_pat(&field.pat)
787 pub fn walk_foreign_item<'v, V: Visitor<'v>>(visitor: &mut V, foreign_item: &'v ForeignItem<'v>) {
788 visitor.visit_id(foreign_item.hir_id());
789 visitor.visit_ident(foreign_item.ident);
791 match foreign_item.kind {
792 ForeignItemKind::Fn(ref function_declaration, param_names, ref generics) => {
793 visitor.visit_generics(generics);
794 visitor.visit_fn_decl(function_declaration);
795 for ¶m_name in param_names {
796 visitor.visit_ident(param_name);
799 ForeignItemKind::Static(ref typ, _) => visitor.visit_ty(typ),
800 ForeignItemKind::Type => (),
804 pub fn walk_param_bound<'v, V: Visitor<'v>>(visitor: &mut V, bound: &'v GenericBound<'v>) {
806 GenericBound::Trait(ref typ, _modifier) => {
807 visitor.visit_poly_trait_ref(typ);
809 GenericBound::LangItemTrait(_, _span, hir_id, args) => {
810 visitor.visit_id(hir_id);
811 visitor.visit_generic_args(args);
813 GenericBound::Outlives(ref lifetime) => visitor.visit_lifetime(lifetime),
817 pub fn walk_generic_param<'v, V: Visitor<'v>>(visitor: &mut V, param: &'v GenericParam<'v>) {
818 visitor.visit_id(param.hir_id);
820 ParamName::Plain(ident) => visitor.visit_ident(ident),
821 ParamName::Error | ParamName::Fresh => {}
824 GenericParamKind::Lifetime { .. } => {}
825 GenericParamKind::Type { ref default, .. } => walk_list!(visitor, visit_ty, default),
826 GenericParamKind::Const { ref ty, ref default } => {
827 visitor.visit_ty(ty);
828 if let Some(ref default) = default {
829 visitor.visit_const_param_default(param.hir_id, default);
835 pub fn walk_const_param_default<'v, V: Visitor<'v>>(visitor: &mut V, ct: &'v AnonConst) {
836 visitor.visit_anon_const(ct)
839 pub fn walk_generics<'v, V: Visitor<'v>>(visitor: &mut V, generics: &'v Generics<'v>) {
840 walk_list!(visitor, visit_generic_param, generics.params);
841 walk_list!(visitor, visit_where_predicate, generics.predicates);
844 pub fn walk_where_predicate<'v, V: Visitor<'v>>(
846 predicate: &'v WherePredicate<'v>,
849 WherePredicate::BoundPredicate(WhereBoundPredicate {
853 bound_generic_params,
857 visitor.visit_id(hir_id);
858 visitor.visit_ty(bounded_ty);
859 walk_list!(visitor, visit_param_bound, bounds);
860 walk_list!(visitor, visit_generic_param, bound_generic_params);
862 WherePredicate::RegionPredicate(WhereRegionPredicate {
868 visitor.visit_lifetime(lifetime);
869 walk_list!(visitor, visit_param_bound, bounds);
871 WherePredicate::EqPredicate(WhereEqPredicate { ref lhs_ty, ref rhs_ty, span: _ }) => {
872 visitor.visit_ty(lhs_ty);
873 visitor.visit_ty(rhs_ty);
878 pub fn walk_fn_ret_ty<'v, V: Visitor<'v>>(visitor: &mut V, ret_ty: &'v FnRetTy<'v>) {
879 if let FnRetTy::Return(ref output_ty) = *ret_ty {
880 visitor.visit_ty(output_ty)
884 pub fn walk_fn_decl<'v, V: Visitor<'v>>(visitor: &mut V, function_declaration: &'v FnDecl<'v>) {
885 for ty in function_declaration.inputs {
888 walk_fn_ret_ty(visitor, &function_declaration.output)
891 pub fn walk_fn_kind<'v, V: Visitor<'v>>(visitor: &mut V, function_kind: FnKind<'v>) {
892 match function_kind {
893 FnKind::ItemFn(_, generics, ..) => {
894 visitor.visit_generics(generics);
896 FnKind::Closure | FnKind::Method(..) => {}
900 pub fn walk_fn<'v, V: Visitor<'v>>(
902 function_kind: FnKind<'v>,
903 function_declaration: &'v FnDecl<'v>,
907 visitor.visit_id(id);
908 visitor.visit_fn_decl(function_declaration);
909 walk_fn_kind(visitor, function_kind);
910 visitor.visit_nested_body(body_id)
913 pub fn walk_trait_item<'v, V: Visitor<'v>>(visitor: &mut V, trait_item: &'v TraitItem<'v>) {
914 // N.B., deliberately force a compilation error if/when new fields are added.
915 let TraitItem { ident, generics, ref defaultness, ref kind, span, owner_id: _ } = *trait_item;
916 let hir_id = trait_item.hir_id();
917 visitor.visit_ident(ident);
918 visitor.visit_generics(&generics);
919 visitor.visit_defaultness(&defaultness);
921 TraitItemKind::Const(ref ty, default) => {
922 visitor.visit_id(hir_id);
923 visitor.visit_ty(ty);
924 walk_list!(visitor, visit_nested_body, default);
926 TraitItemKind::Fn(ref sig, TraitFn::Required(param_names)) => {
927 visitor.visit_id(hir_id);
928 visitor.visit_fn_decl(&sig.decl);
929 for ¶m_name in param_names {
930 visitor.visit_ident(param_name);
933 TraitItemKind::Fn(ref sig, TraitFn::Provided(body_id)) => {
934 visitor.visit_fn(FnKind::Method(ident, sig), &sig.decl, body_id, span, hir_id);
936 TraitItemKind::Type(bounds, ref default) => {
937 visitor.visit_id(hir_id);
938 walk_list!(visitor, visit_param_bound, bounds);
939 walk_list!(visitor, visit_ty, default);
944 pub fn walk_trait_item_ref<'v, V: Visitor<'v>>(visitor: &mut V, trait_item_ref: &'v TraitItemRef) {
945 // N.B., deliberately force a compilation error if/when new fields are added.
946 let TraitItemRef { id, ident, ref kind, span: _ } = *trait_item_ref;
947 visitor.visit_nested_trait_item(id);
948 visitor.visit_ident(ident);
949 visitor.visit_associated_item_kind(kind);
952 pub fn walk_impl_item<'v, V: Visitor<'v>>(visitor: &mut V, impl_item: &'v ImplItem<'v>) {
953 // N.B., deliberately force a compilation error if/when new fields are added.
964 visitor.visit_ident(ident);
965 visitor.visit_generics(generics);
966 visitor.visit_defaultness(defaultness);
968 ImplItemKind::Const(ref ty, body) => {
969 visitor.visit_id(impl_item.hir_id());
970 visitor.visit_ty(ty);
971 visitor.visit_nested_body(body);
973 ImplItemKind::Fn(ref sig, body_id) => {
975 FnKind::Method(impl_item.ident, sig),
982 ImplItemKind::Type(ref ty) => {
983 visitor.visit_id(impl_item.hir_id());
984 visitor.visit_ty(ty);
989 pub fn walk_foreign_item_ref<'v, V: Visitor<'v>>(
991 foreign_item_ref: &'v ForeignItemRef,
993 // N.B., deliberately force a compilation error if/when new fields are added.
994 let ForeignItemRef { id, ident, span: _ } = *foreign_item_ref;
995 visitor.visit_nested_foreign_item(id);
996 visitor.visit_ident(ident);
999 pub fn walk_impl_item_ref<'v, V: Visitor<'v>>(visitor: &mut V, impl_item_ref: &'v ImplItemRef) {
1000 // N.B., deliberately force a compilation error if/when new fields are added.
1001 let ImplItemRef { id, ident, ref kind, span: _, trait_item_def_id: _ } = *impl_item_ref;
1002 visitor.visit_nested_impl_item(id);
1003 visitor.visit_ident(ident);
1004 visitor.visit_associated_item_kind(kind);
1007 pub fn walk_struct_def<'v, V: Visitor<'v>>(
1009 struct_definition: &'v VariantData<'v>,
1011 walk_list!(visitor, visit_id, struct_definition.ctor_hir_id());
1012 walk_list!(visitor, visit_field_def, struct_definition.fields());
1015 pub fn walk_field_def<'v, V: Visitor<'v>>(visitor: &mut V, field: &'v FieldDef<'v>) {
1016 visitor.visit_id(field.hir_id);
1017 visitor.visit_ident(field.ident);
1018 visitor.visit_ty(&field.ty);
1021 pub fn walk_block<'v, V: Visitor<'v>>(visitor: &mut V, block: &'v Block<'v>) {
1022 visitor.visit_id(block.hir_id);
1023 walk_list!(visitor, visit_stmt, block.stmts);
1024 walk_list!(visitor, visit_expr, &block.expr);
1027 pub fn walk_stmt<'v, V: Visitor<'v>>(visitor: &mut V, statement: &'v Stmt<'v>) {
1028 visitor.visit_id(statement.hir_id);
1029 match statement.kind {
1030 StmtKind::Local(ref local) => visitor.visit_local(local),
1031 StmtKind::Item(item) => visitor.visit_nested_item(item),
1032 StmtKind::Expr(ref expression) | StmtKind::Semi(ref expression) => {
1033 visitor.visit_expr(expression)
1038 pub fn walk_array_len<'v, V: Visitor<'v>>(visitor: &mut V, len: &'v ArrayLen) {
1040 &ArrayLen::Infer(hir_id, _span) => visitor.visit_id(hir_id),
1041 ArrayLen::Body(c) => visitor.visit_anon_const(c),
1045 pub fn walk_anon_const<'v, V: Visitor<'v>>(visitor: &mut V, constant: &'v AnonConst) {
1046 visitor.visit_id(constant.hir_id);
1047 visitor.visit_nested_body(constant.body);
1050 pub fn walk_let_expr<'v, V: Visitor<'v>>(visitor: &mut V, let_expr: &'v Let<'v>) {
1051 // match the visit order in walk_local
1052 visitor.visit_expr(let_expr.init);
1053 visitor.visit_id(let_expr.hir_id);
1054 visitor.visit_pat(let_expr.pat);
1055 walk_list!(visitor, visit_ty, let_expr.ty);
1058 pub fn walk_expr_field<'v, V: Visitor<'v>>(visitor: &mut V, field: &'v ExprField<'v>) {
1059 visitor.visit_id(field.hir_id);
1060 visitor.visit_ident(field.ident);
1061 visitor.visit_expr(&field.expr)
1064 pub fn walk_expr<'v, V: Visitor<'v>>(visitor: &mut V, expression: &'v Expr<'v>) {
1065 visitor.visit_id(expression.hir_id);
1066 match expression.kind {
1067 ExprKind::Box(ref subexpression) => visitor.visit_expr(subexpression),
1068 ExprKind::Array(subexpressions) => {
1069 walk_list!(visitor, visit_expr, subexpressions);
1071 ExprKind::ConstBlock(ref anon_const) => visitor.visit_anon_const(anon_const),
1072 ExprKind::Repeat(ref element, ref count) => {
1073 visitor.visit_expr(element);
1074 visitor.visit_array_length(count)
1076 ExprKind::Struct(ref qpath, fields, ref optional_base) => {
1077 visitor.visit_qpath(qpath, expression.hir_id, expression.span);
1078 walk_list!(visitor, visit_expr_field, fields);
1079 walk_list!(visitor, visit_expr, optional_base);
1081 ExprKind::Tup(subexpressions) => {
1082 walk_list!(visitor, visit_expr, subexpressions);
1084 ExprKind::Call(ref callee_expression, arguments) => {
1085 visitor.visit_expr(callee_expression);
1086 walk_list!(visitor, visit_expr, arguments);
1088 ExprKind::MethodCall(ref segment, receiver, arguments, _) => {
1089 visitor.visit_path_segment(segment);
1090 visitor.visit_expr(receiver);
1091 walk_list!(visitor, visit_expr, arguments);
1093 ExprKind::Binary(_, ref left_expression, ref right_expression) => {
1094 visitor.visit_expr(left_expression);
1095 visitor.visit_expr(right_expression)
1097 ExprKind::AddrOf(_, _, ref subexpression) | ExprKind::Unary(_, ref subexpression) => {
1098 visitor.visit_expr(subexpression)
1100 ExprKind::Cast(ref subexpression, ref typ) | ExprKind::Type(ref subexpression, ref typ) => {
1101 visitor.visit_expr(subexpression);
1102 visitor.visit_ty(typ)
1104 ExprKind::DropTemps(ref subexpression) => {
1105 visitor.visit_expr(subexpression);
1107 ExprKind::Let(ref let_expr) => visitor.visit_let_expr(let_expr),
1108 ExprKind::If(ref cond, ref then, ref else_opt) => {
1109 visitor.visit_expr(cond);
1110 visitor.visit_expr(then);
1111 walk_list!(visitor, visit_expr, else_opt);
1113 ExprKind::Loop(ref block, ref opt_label, _, _) => {
1114 walk_list!(visitor, visit_label, opt_label);
1115 visitor.visit_block(block);
1117 ExprKind::Match(ref subexpression, arms, _) => {
1118 visitor.visit_expr(subexpression);
1119 walk_list!(visitor, visit_arm, arms);
1121 ExprKind::Closure(&Closure {
1123 bound_generic_params,
1130 walk_list!(visitor, visit_generic_param, bound_generic_params);
1131 visitor.visit_fn(FnKind::Closure, fn_decl, body, expression.span, expression.hir_id)
1133 ExprKind::Block(ref block, ref opt_label) => {
1134 walk_list!(visitor, visit_label, opt_label);
1135 visitor.visit_block(block);
1137 ExprKind::Assign(ref lhs, ref rhs, _) => {
1138 visitor.visit_expr(rhs);
1139 visitor.visit_expr(lhs)
1141 ExprKind::AssignOp(_, ref left_expression, ref right_expression) => {
1142 visitor.visit_expr(right_expression);
1143 visitor.visit_expr(left_expression);
1145 ExprKind::Field(ref subexpression, ident) => {
1146 visitor.visit_expr(subexpression);
1147 visitor.visit_ident(ident);
1149 ExprKind::Index(ref main_expression, ref index_expression) => {
1150 visitor.visit_expr(main_expression);
1151 visitor.visit_expr(index_expression)
1153 ExprKind::Path(ref qpath) => {
1154 visitor.visit_qpath(qpath, expression.hir_id, expression.span);
1156 ExprKind::Break(ref destination, ref opt_expr) => {
1157 walk_list!(visitor, visit_label, &destination.label);
1158 walk_list!(visitor, visit_expr, opt_expr);
1160 ExprKind::Continue(ref destination) => {
1161 walk_list!(visitor, visit_label, &destination.label);
1163 ExprKind::Ret(ref optional_expression) => {
1164 walk_list!(visitor, visit_expr, optional_expression);
1166 ExprKind::InlineAsm(ref asm) => {
1167 visitor.visit_inline_asm(asm, expression.hir_id);
1169 ExprKind::Yield(ref subexpression, _) => {
1170 visitor.visit_expr(subexpression);
1172 ExprKind::Lit(_) | ExprKind::Err => {}
1176 pub fn walk_arm<'v, V: Visitor<'v>>(visitor: &mut V, arm: &'v Arm<'v>) {
1177 visitor.visit_id(arm.hir_id);
1178 visitor.visit_pat(&arm.pat);
1179 if let Some(ref g) = arm.guard {
1181 Guard::If(ref e) => visitor.visit_expr(e),
1182 Guard::IfLet(ref l) => {
1183 visitor.visit_let_expr(l);
1187 visitor.visit_expr(&arm.body);
1190 pub fn walk_associated_item_kind<'v, V: Visitor<'v>>(_: &mut V, _: &'v AssocItemKind) {
1191 // No visitable content here: this fn exists so you can call it if
1192 // the right thing to do, should content be added in the future,
1193 // would be to walk it.
1196 pub fn walk_defaultness<'v, V: Visitor<'v>>(_: &mut V, _: &'v Defaultness) {
1197 // No visitable content here: this fn exists so you can call it if
1198 // the right thing to do, should content be added in the future,
1199 // would be to walk it.