Skip to content

Commit 61ba32b

Browse files
committed
Update intravisit docs
1 parent d201c81 commit 61ba32b

File tree

2 files changed

+35
-35
lines changed

2 files changed

+35
-35
lines changed

compiler/rustc_hir/src/intravisit.rs

+30-30
Original file line numberDiff line numberDiff line change
@@ -163,10 +163,15 @@ impl<'hir> Map<'hir> for ! {
163163
pub mod nested_filter {
164164
use super::Map;
165165

166-
/// Specifies what nested things a visitor wants to visit. The most
167-
/// common choice is `OnlyBodies`, which will cause the visitor to
168-
/// visit fn bodies for fns that it encounters, but skip over nested
169-
/// item-like things.
166+
/// Specifies what nested things a visitor wants to visit. By "nested
167+
/// things", we are referring to bits of HIR that are not directly embedded
168+
/// within one another but rather indirectly, through a table in the crate.
169+
/// This is done to control dependencies during incremental compilation: the
170+
/// non-inline bits of HIR can be tracked and hashed separately.
171+
///
172+
/// The most common choice is `OnlyBodies`, which will cause the visitor to
173+
/// visit fn bodies for fns that it encounters, and closure bodies, but
174+
/// skip over nested item-like things.
170175
///
171176
/// See the comments on `ItemLikeVisitor` for more details on the overall
172177
/// visit strategy.
@@ -217,42 +222,38 @@ use nested_filter::NestedFilter;
217222
pub trait Visitor<'v>: Sized {
218223
// this type should not be overridden, it exists for convenient usage as `Self::Map`
219224
type Map: Map<'v> = <Self::NestedFilter as NestedFilter<'v>>::Map;
220-
type NestedFilter: NestedFilter<'v> = nested_filter::None;
221225

222226
///////////////////////////////////////////////////////////////////////////
223227
// Nested items.
224228

225-
/// The default versions of the `visit_nested_XXX` routines invoke
226-
/// this method to get a map to use. By selecting an enum variant,
227-
/// you control which kinds of nested HIR are visited; see
228-
/// `NestedVisitorMap` for details. By "nested HIR", we are
229-
/// referring to bits of HIR that are not directly embedded within
230-
/// one another but rather indirectly, through a table in the
231-
/// crate. This is done to control dependencies during incremental
232-
/// compilation: the non-inline bits of HIR can be tracked and
233-
/// hashed separately.
229+
/// Override this type to control which nested HIR are visited; see
230+
/// [`NestedFilter`] for details. If you override this type, you
231+
/// must also override [`nested_visit_map`](Self::nested_visit_map).
234232
///
235233
/// **If for some reason you want the nested behavior, but don't
236-
/// have a `Map` at your disposal:** then you should override the
237-
/// `visit_nested_XXX` methods, and override this method to
238-
/// `panic!()`. This way, if a new `visit_nested_XXX` variant is
239-
/// added in the future, we will see the panic in your code and
240-
/// fix it appropriately.
234+
/// have a `Map` at your disposal:** then override the
235+
/// `visit_nested_XXX` methods. If a new `visit_nested_XXX` variant is
236+
/// added in the future, it will cause a panic which can be detected
237+
/// and fixed appropriately.
238+
type NestedFilter: NestedFilter<'v> = nested_filter::None;
239+
240+
/// If `type NestedFilter` is set to visit nested items, this method
241+
/// must also be overridden to provide a map to retrieve nested items.
241242
fn nested_visit_map(&mut self) -> Self::Map {
242243
panic!(
243244
"nested_visit_map must be implemented or consider using \
244245
`type NestedFilter = nested_filter::None` (the default)"
245246
);
246247
}
247248

248-
/// Invoked when a nested item is encountered. By default does
249-
/// nothing unless you override `nested_visit_map` to return other than
250-
/// `None`, in which case it will walk the item. **You probably
251-
/// don't want to override this method** -- instead, override
252-
/// `nested_visit_map` or use the "shallow" or "deep" visit
253-
/// patterns described on `itemlikevisit::ItemLikeVisitor`. The only
254-
/// reason to override this method is if you want a nested pattern
255-
/// but cannot supply a `Map`; see `nested_visit_map` for advice.
249+
/// Invoked when a nested item is encountered. By default, when
250+
/// `Self::NestedFilter` is `nested_filter::None`, this method does
251+
/// nothing. **You probably don't want to override this method** --
252+
/// instead, override [`Self::NestedFilter`] or use the "shallow" or
253+
/// "deep" visit patterns described on
254+
/// `itemlikevisit::ItemLikeVisitor`. The only reason to override
255+
/// this method is if you want a nested pattern but cannot supply a
256+
/// [`Map`]; see `nested_visit_map` for advice.
256257
fn visit_nested_item(&mut self, id: ItemId) {
257258
if Self::NestedFilter::INTER {
258259
let item = self.nested_visit_map().item(id);
@@ -291,9 +292,8 @@ pub trait Visitor<'v>: Sized {
291292
}
292293

293294
/// Invoked to visit the body of a function, method or closure. Like
294-
/// visit_nested_item, does nothing by default unless you override
295-
/// `nested_visit_map` to return other than `None`, in which case it will walk
296-
/// the body.
295+
/// `visit_nested_item`, does nothing by default unless you override
296+
/// `Self::NestedFilter`.
297297
fn visit_nested_body(&mut self, id: BodyId) {
298298
if Self::NestedFilter::INTRA {
299299
let body = self.nested_visit_map().body(id);

compiler/rustc_hir/src/itemlikevisit.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ use super::{ForeignItem, ImplItem, Item, TraitItem};
1717
/// an item, but don't care about how item-like things are nested
1818
/// within one another.
1919
/// - Example: Examine each expression to look for its type and do some check or other.
20-
/// - How: Implement `intravisit::Visitor` and override the `nested_visit_map()` method
21-
/// to return `NestedVisitorMap::OnlyBodies` and use
20+
/// - How: Implement `intravisit::Visitor` and override the `NestedFilter` type to
21+
/// `nested_filter::OnlyBodies` (and implement `nested_visit_map`), and use
2222
/// `tcx.hir().visit_all_item_likes(&mut visitor.as_deep_visitor())`. Within your
2323
/// `intravisit::Visitor` impl, implement methods like `visit_expr()` (don't forget to invoke
2424
/// `intravisit::walk_expr()` to keep walking the subparts).
@@ -29,9 +29,9 @@ use super::{ForeignItem, ImplItem, Item, TraitItem};
2929
/// item-like things.
3030
/// - Example: Lifetime resolution, which wants to bring lifetimes declared on the
3131
/// impl into scope while visiting the impl-items, and then back out again.
32-
/// - How: Implement `intravisit::Visitor` and override the `nested_visit_map()` method
33-
/// to return `NestedVisitorMap::All`. Walk your crate with `intravisit::walk_crate()`
34-
/// invoked on `tcx.hir().krate()`.
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()`.
3535
/// - Pro: Visitor methods for any kind of HIR node, not just item-like things.
3636
/// - Pro: Preserves nesting information
3737
/// - Con: Does not integrate well into dependency tracking.

0 commit comments

Comments
 (0)