remove ItemLikeVisitor and DeepVisitor
Signed-off-by: Miguel Guarniz <mi9uel9@gmail.com>
This commit is contained in:
parent
0b7dd95475
commit
93616dd539
14 changed files with 15 additions and 113 deletions
|
@ -32,43 +32,12 @@
|
||||||
//! example generator inference, and possibly also HIR borrowck.
|
//! example generator inference, and possibly also HIR borrowck.
|
||||||
|
|
||||||
use crate::hir::*;
|
use crate::hir::*;
|
||||||
use crate::itemlikevisit::{ItemLikeVisitor, ParItemLikeVisitor};
|
use crate::itemlikevisit::ParItemLikeVisitor;
|
||||||
use rustc_ast::walk_list;
|
use rustc_ast::walk_list;
|
||||||
use rustc_ast::{Attribute, Label};
|
use rustc_ast::{Attribute, Label};
|
||||||
use rustc_span::symbol::{Ident, Symbol};
|
use rustc_span::symbol::{Ident, Symbol};
|
||||||
use rustc_span::Span;
|
use rustc_span::Span;
|
||||||
|
|
||||||
pub struct DeepVisitor<'v, V> {
|
|
||||||
visitor: &'v mut V,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'v, V> DeepVisitor<'v, V> {
|
|
||||||
pub fn new(base: &'v mut V) -> Self {
|
|
||||||
DeepVisitor { visitor: base }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'v, 'hir, V> ItemLikeVisitor<'hir> for DeepVisitor<'v, V>
|
|
||||||
where
|
|
||||||
V: Visitor<'hir>,
|
|
||||||
{
|
|
||||||
fn visit_item(&mut self, item: &'hir Item<'hir>) {
|
|
||||||
self.visitor.visit_item(item);
|
|
||||||
}
|
|
||||||
|
|
||||||
fn visit_trait_item(&mut self, trait_item: &'hir TraitItem<'hir>) {
|
|
||||||
self.visitor.visit_trait_item(trait_item);
|
|
||||||
}
|
|
||||||
|
|
||||||
fn visit_impl_item(&mut self, impl_item: &'hir ImplItem<'hir>) {
|
|
||||||
self.visitor.visit_impl_item(impl_item);
|
|
||||||
}
|
|
||||||
|
|
||||||
fn visit_foreign_item(&mut self, foreign_item: &'hir ForeignItem<'hir>) {
|
|
||||||
self.visitor.visit_foreign_item(foreign_item);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub trait IntoVisitor<'hir> {
|
pub trait IntoVisitor<'hir> {
|
||||||
type Visitor: Visitor<'hir>;
|
type Visitor: Visitor<'hir>;
|
||||||
fn into_visitor(&self) -> Self::Visitor;
|
fn into_visitor(&self) -> Self::Visitor;
|
||||||
|
@ -315,16 +284,6 @@ pub trait Visitor<'v>: Sized {
|
||||||
walk_body(self, b);
|
walk_body(self, b);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// When invoking `visit_all_item_likes()`, you need to supply an
|
|
||||||
/// item-like visitor. This method converts an "intra-visit"
|
|
||||||
/// visitor into an item-like visitor that walks the entire tree.
|
|
||||||
/// If you use this, you probably don't want to process the
|
|
||||||
/// contents of nested item-like things, since the outer loop will
|
|
||||||
/// visit them as well.
|
|
||||||
fn as_deep_visitor(&mut self) -> DeepVisitor<'_, Self> {
|
|
||||||
DeepVisitor::new(self)
|
|
||||||
}
|
|
||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
fn visit_id(&mut self, _hir_id: HirId) {
|
fn visit_id(&mut self, _hir_id: HirId) {
|
||||||
|
|
|
@ -1,55 +1,5 @@
|
||||||
use super::{ForeignItem, ImplItem, Item, TraitItem};
|
use super::{ForeignItem, ImplItem, Item, TraitItem};
|
||||||
|
|
||||||
/// The "item-like visitor" defines only the top-level methods
|
|
||||||
/// that can be invoked by `Crate::visit_all_item_likes()`. Whether
|
|
||||||
/// this trait is the right one to implement will depend on the
|
|
||||||
/// overall pattern you need. Here are the three available patterns,
|
|
||||||
/// in roughly the order of desirability:
|
|
||||||
///
|
|
||||||
/// 1. **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: Implement `ItemLikeVisitor` and call `tcx.hir().visit_all_item_likes()`.
|
|
||||||
/// - Pro: Efficient; just walks the lists of item-like things, not the nodes themselves.
|
|
||||||
/// - Con: Don't get information about nesting
|
|
||||||
/// - Con: Don't have methods for specific bits of HIR, like "on
|
|
||||||
/// every expr, do this".
|
|
||||||
/// 2. **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::Visitor` and override the `NestedFilter` type to
|
|
||||||
/// `nested_filter::OnlyBodies` (and implement `nested_visit_map`), and use
|
|
||||||
/// `tcx.hir().visit_all_item_likes(&mut visitor.as_deep_visitor())`. Within your
|
|
||||||
/// `intravisit::Visitor` impl, implement methods like `visit_expr()` (don't forget to invoke
|
|
||||||
/// `intravisit::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
|
|
||||||
/// 3. **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::Visitor` and override the `NestedFilter` type to
|
|
||||||
/// `nested_filter::All` (and implement `nested_visit_map`). Walk your crate with
|
|
||||||
/// `tcx.hir().walk_toplevel_module(visitor)` invoked on `tcx.hir().krate()`.
|
|
||||||
/// - 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.
|
|
||||||
///
|
|
||||||
/// Note: the methods of `ItemLikeVisitor` intentionally have no
|
|
||||||
/// defaults, so that as we expand the list of item-like things, we
|
|
||||||
/// revisit the various visitors to see if they need to change. This
|
|
||||||
/// is harder to do with `intravisit::Visitor`, so when you add a new
|
|
||||||
/// `visit_nested_foo()` method, it is recommended that you search for
|
|
||||||
/// existing `fn visit_nested` methods to see where changes are
|
|
||||||
/// needed.
|
|
||||||
pub trait ItemLikeVisitor<'hir> {
|
|
||||||
fn visit_item(&mut self, item: &'hir Item<'hir>);
|
|
||||||
fn visit_trait_item(&mut self, trait_item: &'hir TraitItem<'hir>);
|
|
||||||
fn visit_impl_item(&mut self, impl_item: &'hir ImplItem<'hir>);
|
|
||||||
fn visit_foreign_item(&mut self, foreign_item: &'hir ForeignItem<'hir>);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// A parallel variant of `ItemLikeVisitor`.
|
/// A parallel variant of `ItemLikeVisitor`.
|
||||||
pub trait ParItemLikeVisitor<'hir> {
|
pub trait ParItemLikeVisitor<'hir> {
|
||||||
fn visit_item(&self, item: &'hir Item<'hir>);
|
fn visit_item(&self, item: &'hir Item<'hir>);
|
||||||
|
|
|
@ -75,7 +75,7 @@ pub fn assert_dep_graph(tcx: TyCtxt<'_>) {
|
||||||
let mut visitor =
|
let mut visitor =
|
||||||
IfThisChanged { tcx, if_this_changed: vec![], then_this_would_need: vec![] };
|
IfThisChanged { tcx, if_this_changed: vec![], then_this_would_need: vec![] };
|
||||||
visitor.process_attrs(hir::CRATE_HIR_ID);
|
visitor.process_attrs(hir::CRATE_HIR_ID);
|
||||||
tcx.hir().visit_all_item_likes(&mut visitor.as_deep_visitor());
|
tcx.hir().visit_all_item_likes(&mut visitor);
|
||||||
(visitor.if_this_changed, visitor.then_this_would_need)
|
(visitor.if_this_changed, visitor.then_this_would_need)
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -452,7 +452,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
self.tcx.hir().visit_all_item_likes(&mut self.as_deep_visitor());
|
self.tcx.hir().visit_all_item_likes(self);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn encode_def_path_table(&mut self) {
|
fn encode_def_path_table(&mut self) {
|
||||||
|
|
|
@ -9,7 +9,6 @@ use rustc_hir::def::{DefKind, Res};
|
||||||
use rustc_hir::def_id::{CrateNum, DefId, LocalDefId, CRATE_DEF_ID, LOCAL_CRATE};
|
use rustc_hir::def_id::{CrateNum, DefId, LocalDefId, CRATE_DEF_ID, LOCAL_CRATE};
|
||||||
use rustc_hir::definitions::{DefKey, DefPath, DefPathHash};
|
use rustc_hir::definitions::{DefKey, DefPath, DefPathHash};
|
||||||
use rustc_hir::intravisit::{self, Visitor};
|
use rustc_hir::intravisit::{self, Visitor};
|
||||||
use rustc_hir::itemlikevisit::ItemLikeVisitor;
|
|
||||||
use rustc_hir::*;
|
use rustc_hir::*;
|
||||||
use rustc_index::vec::Idx;
|
use rustc_index::vec::Idx;
|
||||||
use rustc_middle::hir::nested_filter;
|
use rustc_middle::hir::nested_filter;
|
||||||
|
@ -616,7 +615,7 @@ impl<'hir> Map<'hir> {
|
||||||
/// visitor and then call `intravisit::walk_crate` instead.
|
/// visitor and then call `intravisit::walk_crate` instead.
|
||||||
pub fn visit_all_item_likes<V>(self, visitor: &mut V)
|
pub fn visit_all_item_likes<V>(self, visitor: &mut V)
|
||||||
where
|
where
|
||||||
V: itemlikevisit::ItemLikeVisitor<'hir>,
|
V: Visitor<'hir>,
|
||||||
{
|
{
|
||||||
let krate = self.krate();
|
let krate = self.krate();
|
||||||
for owner in krate.owners.iter().filter_map(|i| i.as_owner()) {
|
for owner in krate.owners.iter().filter_map(|i| i.as_owner()) {
|
||||||
|
@ -649,7 +648,7 @@ impl<'hir> Map<'hir> {
|
||||||
|
|
||||||
pub fn visit_item_likes_in_module<V>(self, module: LocalDefId, visitor: &mut V)
|
pub fn visit_item_likes_in_module<V>(self, module: LocalDefId, visitor: &mut V)
|
||||||
where
|
where
|
||||||
V: ItemLikeVisitor<'hir>,
|
V: Visitor<'hir>,
|
||||||
{
|
{
|
||||||
let module = self.tcx.hir_module_items(module);
|
let module = self.tcx.hir_module_items(module);
|
||||||
|
|
||||||
|
|
|
@ -170,7 +170,7 @@ fn mir_keys(tcx: TyCtxt<'_>, (): ()) -> FxIndexSet<LocalDefId> {
|
||||||
intravisit::walk_struct_def(self, v)
|
intravisit::walk_struct_def(self, v)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
tcx.hir().visit_all_item_likes(&mut GatherCtors { tcx, set: &mut set }.as_deep_visitor());
|
tcx.hir().visit_all_item_likes(&mut GatherCtors { tcx, set: &mut set });
|
||||||
|
|
||||||
set
|
set
|
||||||
}
|
}
|
||||||
|
|
|
@ -2384,7 +2384,7 @@ fn check_non_exported_macro_for_invalid_attrs(tcx: TyCtxt<'_>, item: &Item<'_>)
|
||||||
|
|
||||||
fn check_mod_attrs(tcx: TyCtxt<'_>, module_def_id: LocalDefId) {
|
fn check_mod_attrs(tcx: TyCtxt<'_>, module_def_id: LocalDefId) {
|
||||||
let check_attr_visitor = &mut CheckAttrVisitor { tcx };
|
let check_attr_visitor = &mut CheckAttrVisitor { tcx };
|
||||||
tcx.hir().visit_item_likes_in_module(module_def_id, &mut check_attr_visitor.as_deep_visitor());
|
tcx.hir().visit_item_likes_in_module(module_def_id, check_attr_visitor);
|
||||||
if module_def_id.is_top_level_module() {
|
if module_def_id.is_top_level_module() {
|
||||||
check_attr_visitor.check_attributes(CRATE_HIR_ID, DUMMY_SP, Target::Mod, None);
|
check_attr_visitor.check_attributes(CRATE_HIR_ID, DUMMY_SP, Target::Mod, None);
|
||||||
check_invalid_crate_level_attr(tcx, tcx.hir().krate_attrs());
|
check_invalid_crate_level_attr(tcx, tcx.hir().krate_attrs());
|
||||||
|
|
|
@ -17,7 +17,7 @@ use rustc_target::asm::{InlineAsmRegOrRegClass, InlineAsmType};
|
||||||
use rustc_target::spec::abi::Abi::RustIntrinsic;
|
use rustc_target::spec::abi::Abi::RustIntrinsic;
|
||||||
|
|
||||||
fn check_mod_intrinsics(tcx: TyCtxt<'_>, module_def_id: LocalDefId) {
|
fn check_mod_intrinsics(tcx: TyCtxt<'_>, module_def_id: LocalDefId) {
|
||||||
tcx.hir().visit_item_likes_in_module(module_def_id, &mut ItemVisitor { tcx }.as_deep_visitor());
|
tcx.hir().visit_item_likes_in_module(module_def_id, &mut ItemVisitor { tcx });
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn provide(providers: &mut Providers) {
|
pub fn provide(providers: &mut Providers) {
|
||||||
|
|
|
@ -140,7 +140,7 @@ fn live_node_kind_to_string(lnk: LiveNodeKind, tcx: TyCtxt<'_>) -> String {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn check_mod_liveness(tcx: TyCtxt<'_>, module_def_id: LocalDefId) {
|
fn check_mod_liveness(tcx: TyCtxt<'_>, module_def_id: LocalDefId) {
|
||||||
tcx.hir().visit_item_likes_in_module(module_def_id, &mut IrMaps::new(tcx).as_deep_visitor());
|
tcx.hir().visit_item_likes_in_module(module_def_id, &mut IrMaps::new(tcx));
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn provide(providers: &mut Providers) {
|
pub fn provide(providers: &mut Providers) {
|
||||||
|
|
|
@ -33,7 +33,7 @@ struct CheckLoopVisitor<'a, 'hir> {
|
||||||
fn check_mod_loops(tcx: TyCtxt<'_>, module_def_id: LocalDefId) {
|
fn check_mod_loops(tcx: TyCtxt<'_>, module_def_id: LocalDefId) {
|
||||||
tcx.hir().visit_item_likes_in_module(
|
tcx.hir().visit_item_likes_in_module(
|
||||||
module_def_id,
|
module_def_id,
|
||||||
&mut CheckLoopVisitor { sess: &tcx.sess, hir_map: tcx.hir(), cx: Normal }.as_deep_visitor(),
|
&mut CheckLoopVisitor { sess: &tcx.sess, hir_map: tcx.hir(), cx: Normal },
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -14,10 +14,7 @@ use rustc_span::Span;
|
||||||
use rustc_target::spec::abi::Abi;
|
use rustc_target::spec::abi::Abi;
|
||||||
|
|
||||||
fn check_mod_naked_functions(tcx: TyCtxt<'_>, module_def_id: LocalDefId) {
|
fn check_mod_naked_functions(tcx: TyCtxt<'_>, module_def_id: LocalDefId) {
|
||||||
tcx.hir().visit_item_likes_in_module(
|
tcx.hir().visit_item_likes_in_module(module_def_id, &mut CheckNakedFunctions { tcx });
|
||||||
module_def_id,
|
|
||||||
&mut CheckNakedFunctions { tcx }.as_deep_visitor(),
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
crate fn provide(providers: &mut Providers) {
|
crate fn provide(providers: &mut Providers) {
|
||||||
|
|
|
@ -661,7 +661,7 @@ fn stability_index(tcx: TyCtxt<'_>, (): ()) -> Index {
|
||||||
/// Cross-references the feature names of unstable APIs with enabled
|
/// Cross-references the feature names of unstable APIs with enabled
|
||||||
/// features and possibly prints errors.
|
/// features and possibly prints errors.
|
||||||
fn check_mod_unstable_api_usage(tcx: TyCtxt<'_>, module_def_id: LocalDefId) {
|
fn check_mod_unstable_api_usage(tcx: TyCtxt<'_>, module_def_id: LocalDefId) {
|
||||||
tcx.hir().visit_item_likes_in_module(module_def_id, &mut Checker { tcx }.as_deep_visitor());
|
tcx.hir().visit_item_likes_in_module(module_def_id, &mut Checker { tcx });
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn provide(providers: &mut Providers) {
|
pub(crate) fn provide(providers: &mut Providers) {
|
||||||
|
@ -837,7 +837,7 @@ pub fn check_unused_or_stable_features(tcx: TyCtxt<'_>) {
|
||||||
let mut missing = MissingStabilityAnnotations { tcx, access_levels };
|
let mut missing = MissingStabilityAnnotations { tcx, access_levels };
|
||||||
missing.check_missing_stability(CRATE_DEF_ID, tcx.hir().span(CRATE_HIR_ID));
|
missing.check_missing_stability(CRATE_DEF_ID, tcx.hir().span(CRATE_HIR_ID));
|
||||||
tcx.hir().walk_toplevel_module(&mut missing);
|
tcx.hir().walk_toplevel_module(&mut missing);
|
||||||
tcx.hir().visit_all_item_likes(&mut missing.as_deep_visitor());
|
tcx.hir().visit_all_item_likes(&mut missing);
|
||||||
}
|
}
|
||||||
|
|
||||||
let declared_lang_features = &tcx.features().declared_lang_features;
|
let declared_lang_features = &tcx.features().declared_lang_features;
|
||||||
|
|
|
@ -59,10 +59,7 @@ struct OnlySelfBounds(bool);
|
||||||
// Main entry point
|
// Main entry point
|
||||||
|
|
||||||
fn collect_mod_item_types(tcx: TyCtxt<'_>, module_def_id: LocalDefId) {
|
fn collect_mod_item_types(tcx: TyCtxt<'_>, module_def_id: LocalDefId) {
|
||||||
tcx.hir().visit_item_likes_in_module(
|
tcx.hir().visit_item_likes_in_module(module_def_id, &mut CollectItemTypesVisitor { tcx });
|
||||||
module_def_id,
|
|
||||||
&mut CollectItemTypesVisitor { tcx }.as_deep_visitor(),
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn provide(providers: &mut Providers) {
|
pub fn provide(providers: &mut Providers) {
|
||||||
|
|
|
@ -303,7 +303,7 @@ crate fn run(
|
||||||
// Run call-finder on all items
|
// Run call-finder on all items
|
||||||
let mut calls = FxHashMap::default();
|
let mut calls = FxHashMap::default();
|
||||||
let mut finder = FindCalls { calls: &mut calls, tcx, map: tcx.hir(), cx, target_crates };
|
let mut finder = FindCalls { calls: &mut calls, tcx, map: tcx.hir(), cx, target_crates };
|
||||||
tcx.hir().visit_all_item_likes(&mut finder.as_deep_visitor());
|
tcx.hir().visit_all_item_likes(&mut finder);
|
||||||
|
|
||||||
// Sort call locations within a given file in document order
|
// Sort call locations within a given file in document order
|
||||||
for fn_calls in calls.values_mut() {
|
for fn_calls in calls.values_mut() {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue