Skip to content

Commit 93616dd

Browse files
committed
remove ItemLikeVisitor and DeepVisitor
Signed-off-by: Miguel Guarniz <mi9uel9@gmail.com>
1 parent 0b7dd95 commit 93616dd

File tree

14 files changed

+15
-113
lines changed

14 files changed

+15
-113
lines changed

compiler/rustc_hir/src/intravisit.rs

+1-42
Original file line numberDiff line numberDiff line change
@@ -32,43 +32,12 @@
3232
//! example generator inference, and possibly also HIR borrowck.
3333
3434
use crate::hir::*;
35-
use crate::itemlikevisit::{ItemLikeVisitor, ParItemLikeVisitor};
35+
use crate::itemlikevisit::ParItemLikeVisitor;
3636
use rustc_ast::walk_list;
3737
use rustc_ast::{Attribute, Label};
3838
use rustc_span::symbol::{Ident, Symbol};
3939
use rustc_span::Span;
4040

41-
pub struct DeepVisitor<'v, V> {
42-
visitor: &'v mut V,
43-
}
44-
45-
impl<'v, V> DeepVisitor<'v, V> {
46-
pub fn new(base: &'v mut V) -> Self {
47-
DeepVisitor { visitor: base }
48-
}
49-
}
50-
51-
impl<'v, 'hir, V> ItemLikeVisitor<'hir> for DeepVisitor<'v, V>
52-
where
53-
V: Visitor<'hir>,
54-
{
55-
fn visit_item(&mut self, item: &'hir Item<'hir>) {
56-
self.visitor.visit_item(item);
57-
}
58-
59-
fn visit_trait_item(&mut self, trait_item: &'hir TraitItem<'hir>) {
60-
self.visitor.visit_trait_item(trait_item);
61-
}
62-
63-
fn visit_impl_item(&mut self, impl_item: &'hir ImplItem<'hir>) {
64-
self.visitor.visit_impl_item(impl_item);
65-
}
66-
67-
fn visit_foreign_item(&mut self, foreign_item: &'hir ForeignItem<'hir>) {
68-
self.visitor.visit_foreign_item(foreign_item);
69-
}
70-
}
71-
7241
pub trait IntoVisitor<'hir> {
7342
type Visitor: Visitor<'hir>;
7443
fn into_visitor(&self) -> Self::Visitor;
@@ -315,16 +284,6 @@ pub trait Visitor<'v>: Sized {
315284
walk_body(self, b);
316285
}
317286

318-
/// When invoking `visit_all_item_likes()`, you need to supply an
319-
/// item-like visitor. This method converts an "intra-visit"
320-
/// visitor into an item-like visitor that walks the entire tree.
321-
/// If you use this, you probably don't want to process the
322-
/// contents of nested item-like things, since the outer loop will
323-
/// visit them as well.
324-
fn as_deep_visitor(&mut self) -> DeepVisitor<'_, Self> {
325-
DeepVisitor::new(self)
326-
}
327-
328287
///////////////////////////////////////////////////////////////////////////
329288

330289
fn visit_id(&mut self, _hir_id: HirId) {

compiler/rustc_hir/src/itemlikevisit.rs

-50
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,5 @@
11
use super::{ForeignItem, ImplItem, Item, TraitItem};
22

3-
/// The "item-like visitor" defines only the top-level methods
4-
/// that can be invoked by `Crate::visit_all_item_likes()`. Whether
5-
/// this trait is the right one to implement will depend on the
6-
/// overall pattern you need. Here are the three available patterns,
7-
/// in roughly the order of desirability:
8-
///
9-
/// 1. **Shallow visit**: Get a simple callback for every item (or item-like thing) in the HIR.
10-
/// - Example: find all items with a `#[foo]` attribute on them.
11-
/// - How: Implement `ItemLikeVisitor` and call `tcx.hir().visit_all_item_likes()`.
12-
/// - Pro: Efficient; just walks the lists of item-like things, not the nodes themselves.
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.as_deep_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.
38-
///
39-
/// Note: the methods of `ItemLikeVisitor` intentionally have no
40-
/// defaults, so that as we expand the list of item-like things, we
41-
/// revisit the various visitors to see if they need to change. This
42-
/// is harder to do with `intravisit::Visitor`, so when you add a new
43-
/// `visit_nested_foo()` method, it is recommended that you search for
44-
/// existing `fn visit_nested` methods to see where changes are
45-
/// needed.
46-
pub trait ItemLikeVisitor<'hir> {
47-
fn visit_item(&mut self, item: &'hir Item<'hir>);
48-
fn visit_trait_item(&mut self, trait_item: &'hir TraitItem<'hir>);
49-
fn visit_impl_item(&mut self, impl_item: &'hir ImplItem<'hir>);
50-
fn visit_foreign_item(&mut self, foreign_item: &'hir ForeignItem<'hir>);
51-
}
52-
533
/// A parallel variant of `ItemLikeVisitor`.
544
pub trait ParItemLikeVisitor<'hir> {
555
fn visit_item(&self, item: &'hir Item<'hir>);

compiler/rustc_incremental/src/assert_dep_graph.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ pub fn assert_dep_graph(tcx: TyCtxt<'_>) {
7575
let mut visitor =
7676
IfThisChanged { tcx, if_this_changed: vec![], then_this_would_need: vec![] };
7777
visitor.process_attrs(hir::CRATE_HIR_ID);
78-
tcx.hir().visit_all_item_likes(&mut visitor.as_deep_visitor());
78+
tcx.hir().visit_all_item_likes(&mut visitor);
7979
(visitor.if_this_changed, visitor.then_this_would_need)
8080
};
8181

compiler/rustc_metadata/src/rmeta/encoder.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
452452
return;
453453
}
454454

455-
self.tcx.hir().visit_all_item_likes(&mut self.as_deep_visitor());
455+
self.tcx.hir().visit_all_item_likes(self);
456456
}
457457

458458
fn encode_def_path_table(&mut self) {

compiler/rustc_middle/src/hir/map/mod.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ use rustc_hir::def::{DefKind, Res};
99
use rustc_hir::def_id::{CrateNum, DefId, LocalDefId, CRATE_DEF_ID, LOCAL_CRATE};
1010
use rustc_hir::definitions::{DefKey, DefPath, DefPathHash};
1111
use rustc_hir::intravisit::{self, Visitor};
12-
use rustc_hir::itemlikevisit::ItemLikeVisitor;
1312
use rustc_hir::*;
1413
use rustc_index::vec::Idx;
1514
use rustc_middle::hir::nested_filter;
@@ -616,7 +615,7 @@ impl<'hir> Map<'hir> {
616615
/// visitor and then call `intravisit::walk_crate` instead.
617616
pub fn visit_all_item_likes<V>(self, visitor: &mut V)
618617
where
619-
V: itemlikevisit::ItemLikeVisitor<'hir>,
618+
V: Visitor<'hir>,
620619
{
621620
let krate = self.krate();
622621
for owner in krate.owners.iter().filter_map(|i| i.as_owner()) {
@@ -649,7 +648,7 @@ impl<'hir> Map<'hir> {
649648

650649
pub fn visit_item_likes_in_module<V>(self, module: LocalDefId, visitor: &mut V)
651650
where
652-
V: ItemLikeVisitor<'hir>,
651+
V: Visitor<'hir>,
653652
{
654653
let module = self.tcx.hir_module_items(module);
655654

compiler/rustc_mir_transform/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ fn mir_keys(tcx: TyCtxt<'_>, (): ()) -> FxIndexSet<LocalDefId> {
170170
intravisit::walk_struct_def(self, v)
171171
}
172172
}
173-
tcx.hir().visit_all_item_likes(&mut GatherCtors { tcx, set: &mut set }.as_deep_visitor());
173+
tcx.hir().visit_all_item_likes(&mut GatherCtors { tcx, set: &mut set });
174174

175175
set
176176
}

compiler/rustc_passes/src/check_attr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2384,7 +2384,7 @@ fn check_non_exported_macro_for_invalid_attrs(tcx: TyCtxt<'_>, item: &Item<'_>)
23842384

23852385
fn check_mod_attrs(tcx: TyCtxt<'_>, module_def_id: LocalDefId) {
23862386
let check_attr_visitor = &mut CheckAttrVisitor { tcx };
2387-
tcx.hir().visit_item_likes_in_module(module_def_id, &mut check_attr_visitor.as_deep_visitor());
2387+
tcx.hir().visit_item_likes_in_module(module_def_id, check_attr_visitor);
23882388
if module_def_id.is_top_level_module() {
23892389
check_attr_visitor.check_attributes(CRATE_HIR_ID, DUMMY_SP, Target::Mod, None);
23902390
check_invalid_crate_level_attr(tcx, tcx.hir().krate_attrs());

compiler/rustc_passes/src/intrinsicck.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use rustc_target::asm::{InlineAsmRegOrRegClass, InlineAsmType};
1717
use rustc_target::spec::abi::Abi::RustIntrinsic;
1818

1919
fn check_mod_intrinsics(tcx: TyCtxt<'_>, module_def_id: LocalDefId) {
20-
tcx.hir().visit_item_likes_in_module(module_def_id, &mut ItemVisitor { tcx }.as_deep_visitor());
20+
tcx.hir().visit_item_likes_in_module(module_def_id, &mut ItemVisitor { tcx });
2121
}
2222

2323
pub fn provide(providers: &mut Providers) {

compiler/rustc_passes/src/liveness.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ fn live_node_kind_to_string(lnk: LiveNodeKind, tcx: TyCtxt<'_>) -> String {
140140
}
141141

142142
fn check_mod_liveness(tcx: TyCtxt<'_>, module_def_id: LocalDefId) {
143-
tcx.hir().visit_item_likes_in_module(module_def_id, &mut IrMaps::new(tcx).as_deep_visitor());
143+
tcx.hir().visit_item_likes_in_module(module_def_id, &mut IrMaps::new(tcx));
144144
}
145145

146146
pub fn provide(providers: &mut Providers) {

compiler/rustc_passes/src/loops.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ struct CheckLoopVisitor<'a, 'hir> {
3333
fn check_mod_loops(tcx: TyCtxt<'_>, module_def_id: LocalDefId) {
3434
tcx.hir().visit_item_likes_in_module(
3535
module_def_id,
36-
&mut CheckLoopVisitor { sess: &tcx.sess, hir_map: tcx.hir(), cx: Normal }.as_deep_visitor(),
36+
&mut CheckLoopVisitor { sess: &tcx.sess, hir_map: tcx.hir(), cx: Normal },
3737
);
3838
}
3939

compiler/rustc_passes/src/naked_functions.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,7 @@ use rustc_span::Span;
1414
use rustc_target::spec::abi::Abi;
1515

1616
fn check_mod_naked_functions(tcx: TyCtxt<'_>, module_def_id: LocalDefId) {
17-
tcx.hir().visit_item_likes_in_module(
18-
module_def_id,
19-
&mut CheckNakedFunctions { tcx }.as_deep_visitor(),
20-
);
17+
tcx.hir().visit_item_likes_in_module(module_def_id, &mut CheckNakedFunctions { tcx });
2118
}
2219

2320
crate fn provide(providers: &mut Providers) {

compiler/rustc_passes/src/stability.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -661,7 +661,7 @@ fn stability_index(tcx: TyCtxt<'_>, (): ()) -> Index {
661661
/// Cross-references the feature names of unstable APIs with enabled
662662
/// features and possibly prints errors.
663663
fn check_mod_unstable_api_usage(tcx: TyCtxt<'_>, module_def_id: LocalDefId) {
664-
tcx.hir().visit_item_likes_in_module(module_def_id, &mut Checker { tcx }.as_deep_visitor());
664+
tcx.hir().visit_item_likes_in_module(module_def_id, &mut Checker { tcx });
665665
}
666666

667667
pub(crate) fn provide(providers: &mut Providers) {
@@ -837,7 +837,7 @@ pub fn check_unused_or_stable_features(tcx: TyCtxt<'_>) {
837837
let mut missing = MissingStabilityAnnotations { tcx, access_levels };
838838
missing.check_missing_stability(CRATE_DEF_ID, tcx.hir().span(CRATE_HIR_ID));
839839
tcx.hir().walk_toplevel_module(&mut missing);
840-
tcx.hir().visit_all_item_likes(&mut missing.as_deep_visitor());
840+
tcx.hir().visit_all_item_likes(&mut missing);
841841
}
842842

843843
let declared_lang_features = &tcx.features().declared_lang_features;

compiler/rustc_typeck/src/collect.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,7 @@ struct OnlySelfBounds(bool);
5959
// Main entry point
6060

6161
fn collect_mod_item_types(tcx: TyCtxt<'_>, module_def_id: LocalDefId) {
62-
tcx.hir().visit_item_likes_in_module(
63-
module_def_id,
64-
&mut CollectItemTypesVisitor { tcx }.as_deep_visitor(),
65-
);
62+
tcx.hir().visit_item_likes_in_module(module_def_id, &mut CollectItemTypesVisitor { tcx });
6663
}
6764

6865
pub fn provide(providers: &mut Providers) {

src/librustdoc/scrape_examples.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ crate fn run(
303303
// Run call-finder on all items
304304
let mut calls = FxHashMap::default();
305305
let mut finder = FindCalls { calls: &mut calls, tcx, map: tcx.hir(), cx, target_crates };
306-
tcx.hir().visit_all_item_likes(&mut finder.as_deep_visitor());
306+
tcx.hir().visit_all_item_likes(&mut finder);
307307

308308
// Sort call locations within a given file in document order
309309
for fn_calls in calls.values_mut() {

0 commit comments

Comments
 (0)