Expand description
HIR walker for walking the contents of nodes.
Here are the three available patterns for the visitor strategy, in roughly the order of desirability:
- Shallow visit: Get a simple callback for every item (or item-like thing) in the HIR.
- Example: find all items with a #[foo]attribute on them.
- How: Use the hir_crate_itemsorhir_module_itemsquery to traverse over item-like ids (ItemId, TraitItemId, etc.) and use tcx.def_kind andtcx.hir_item*(id)to filter and access actual item-like thing, respectively.
- Pro: Efficient; just walks the lists of item ids and gives users control whether to access the hir_owners themselves or not.
- Con: Don’t get information about nesting
- Con: Don’t have methods for specific bits of HIR, like “on every expr, do this”.
 
- Example: find all items with a 
- Deep visit: Want to scan for specific kinds of HIR nodes within
an item, but don’t care about how item-like things are nested
within one another.
- Example: Examine each expression to look for its type and do some check or other.
- How: Implement intravisit::Visitorand override theNestedFiltertype tonested_filter::OnlyBodies(and implementmaybe_tcx), and usetcx.hir_visit_all_item_likes_in_crate(&mut visitor). Within yourintravisit::Visitorimpl, implement methods likevisit_expr()(don’t forget to invokeintravisit::walk_expr()to keep walking the subparts).
- Pro: Visitor methods for any kind of HIR node, not just item-like things.
- Pro: Integrates well into dependency tracking.
- Con: Don’t get information about nesting between items
 
- Nested visit: Want to visit the whole HIR and you care about the nesting between
item-like things.
- Example: Lifetime resolution, which wants to bring lifetimes declared on the impl into scope while visiting the impl-items, and then back out again.
- How: Implement intravisit::Visitorand override theNestedFiltertype tonested_filter::All(and implementmaybe_tcx). Walk your crate withtcx.hir_walk_toplevel_module(visitor).
- Pro: Visitor methods for any kind of HIR node, not just item-like things.
- Pro: Preserves nesting information
- Con: Does not integrate well into dependency tracking.
 
If you have decided to use this visitor, here are some general notes on how to do so:
Each overridden visit method has full control over what
happens with its node, it can do its own traversal of the node’s children,
call intravisit::walk_* to apply the default traversal algorithm, or prevent
deeper traversal by doing nothing.
When visiting the HIR, the contents of nested items are NOT visited
by default. This is different from the AST visitor, which does a deep walk.
Hence this module is called intravisit; see the method visit_nested_item
for more details.
Note: it is an important invariant that the default visitor walks the body of a function in “execution order” - more concretely, if we consider the reverse post-order (RPO) of the CFG implied by the HIR, then a pre-order traversal of the HIR is consistent with the CFG RPO on the initial CFG point of each HIR node, while a post-order traversal of the HIR is consistent with the CFG RPO on each final CFG point of each CFG node.
One thing that follows is that if HIR node A always starts/ends executing before HIR node B, then A appears in traversal pre/postorder before B, respectively. (This follows from RPO respecting CFG domination).
This order consistency is required in a few places in rustc, for example coroutine inference, and possibly also HIR borrowck.
Modules§
Enums§
- FnKind
- InferKind 
- We track whether an infer var is from a Ty,ConstArg, orGenericArgso that HIR visitors overridingVisitor::visit_infercan determine what kind of infer is being visited
Traits§
- HirTyCtxt 
- HIR things retrievable from TyCtxt, avoiding an explicit dependence onTyCtxt. The only impls are for!(where these functions are never called) andTyCtxt(inrustc_middle).
- IntoVisitor 
- Visitor
- Each method of the Visitor trait is a hook to be potentially
overridden. Each method’s default implementation recursively visits
the substructure of the input via the corresponding walkmethod; e.g., thevisit_modmethod by default callsintravisit::walk_mod.
- VisitorExt 
Functions§
- walk_anon_ const 
- walk_arm 
- walk_assoc_ item_ constraint 
- walk_block 
- walk_body 
- walk_const_ arg 
- walk_const_ param_ default 
- walk_defaultness 
- walk_enum_ def 
- walk_expr 
- walk_expr_ field 
- walk_field_ def 
- walk_fn
- walk_fn_ decl 
- walk_fn_ kind 
- walk_fn_ ret_ ty 
- walk_foreign_ item 
- walk_foreign_ item_ ref 
- walk_generic_ arg 
- walk_generic_ args 
- walk_generic_ param 
- walk_generics 
- walk_ident 
- walk_impl_ item 
- walk_impl_ item_ ref 
- walk_inf 
- walk_inline_ asm 
- walk_inline_ const 
- walk_item 
- walk_label 
- walk_lifetime 
- walk_local 
- walk_mod 
- walk_opaque_ ty 
- walk_param 
- walk_param_ bound 
- walk_pat 
- walk_pat_ expr 
- walk_pat_ field 
- walk_path 
- walk_path_ segment 
- walk_poly_ trait_ ref 
- walk_precise_ capturing_ arg 
- walk_qpath 
- walk_stmt 
- walk_struct_ def 
- walk_trait_ item 
- walk_trait_ item_ ref 
- walk_trait_ ref 
- walk_ty
- walk_ty_ pat 
- walk_unambig_ const_ arg 
- walk_unambig_ ty 
- walk_use 
- walk_variant 
- walk_where_ predicate