Skip to content

Commit 7e44078

Browse files
committed
update comments about visitor strategy
Signed-off-by: Miguel Guarniz <[email protected]>
1 parent 93616dd commit 7e44078

File tree

1 file changed

+35
-2
lines changed

1 file changed

+35
-2
lines changed

compiler/rustc_hir/src/intravisit.rs

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,40 @@
11
//! HIR walker for walking the contents of nodes.
22
//!
3-
//! **For an overview of the visitor strategy, see the docs on the
4-
//! `super::itemlikevisit::ItemLikeVisitor` trait.**
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 `nested_visit_map`), and use
22+
//! `tcx.hir().visit_all_item_likes(&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 `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.
538
//!
639
//! If you have decided to use this visitor, here are some general
740
//! notes on how to do so:

0 commit comments

Comments
 (0)