about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMiguel Guarniz <mi9uel9@gmail.com>2022-04-07 14:04:07 -0400
committerMiguel Guarniz <mi9uel9@gmail.com>2022-04-08 12:00:23 -0400
commit3d6f4c85adcd78c3274e4117e379c57c6a38909c (patch)
tree622e04d61dd373f4cdfa570e8ce2f898f46c1ed1
parent0d01ee95583479b5cbcffe183075c7943099192a (diff)
downloadrust-3d6f4c85adcd78c3274e4117e379c57c6a38909c.tar.gz
rust-3d6f4c85adcd78c3274e4117e379c57c6a38909c.zip
remove ItemLikeVisitor impls and add fast paths using DefKind
Signed-off-by: Miguel Guarniz <mi9uel9@gmail.com>
-rw-r--r--compiler/rustc_metadata/src/rmeta/encoder.rs60
-rw-r--r--compiler/rustc_middle/src/ty/print/pretty.rs7
2 files changed, 26 insertions, 41 deletions
diff --git a/compiler/rustc_metadata/src/rmeta/encoder.rs b/compiler/rustc_metadata/src/rmeta/encoder.rs
index 6ad7a342bdc..60a8b8cfba4 100644
--- a/compiler/rustc_metadata/src/rmeta/encoder.rs
+++ b/compiler/rustc_metadata/src/rmeta/encoder.rs
@@ -1783,14 +1783,31 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
         debug!("EncodeContext::encode_traits_and_impls()");
         empty_proc_macro!(self);
         let tcx = self.tcx;
-        let mut visitor = ImplsVisitor { tcx, impls: FxHashMap::default() };
+        let mut fx_hash_map: FxHashMap<DefId, Vec<(DefIndex, Option<SimplifiedType>)>> =
+            FxHashMap::default();
 
         for id in tcx.hir().items() {
-            let item = tcx.hir().item(id);
-            visitor.visit_item(item);
+            match tcx.hir().def_kind(id.def_id) {
+                DefKind::Impl => {
+                    let item = tcx.hir().item(id);
+                    if let Some(trait_ref) = tcx.impl_trait_ref(item.def_id.to_def_id()) {
+                        let simplified_self_ty = fast_reject::simplify_type(
+                            self.tcx,
+                            trait_ref.self_ty(),
+                            TreatParams::AsPlaceholders,
+                        );
+
+                        fx_hash_map
+                            .entry(trait_ref.def_id)
+                            .or_default()
+                            .push((item.def_id.local_def_index, simplified_self_ty));
+                    }
+                }
+                _ => continue,
+            }
         }
 
-        let mut all_impls: Vec<_> = visitor.impls.into_iter().collect();
+        let mut all_impls: Vec<_> = fx_hash_map.into_iter().collect();
 
         // Bring everything into deterministic order for hashing
         all_impls.sort_by_cached_key(|&(trait_def_id, _)| tcx.def_path_hash(trait_def_id));
@@ -2053,41 +2070,6 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
     }
 }
 
-struct ImplsVisitor<'tcx> {
-    tcx: TyCtxt<'tcx>,
-    impls: FxHashMap<DefId, Vec<(DefIndex, Option<SimplifiedType>)>>,
-}
-
-impl<'tcx, 'v> ItemLikeVisitor<'v> for ImplsVisitor<'tcx> {
-    fn visit_item(&mut self, item: &hir::Item<'_>) {
-        match item.kind {
-            hir::ItemKind::Impl(..) => {
-                if let Some(trait_ref) = self.tcx.impl_trait_ref(item.def_id.to_def_id()) {
-                    let simplified_self_ty = fast_reject::simplify_type(
-                        self.tcx,
-                        trait_ref.self_ty(),
-                        TreatParams::AsPlaceholders,
-                    );
-
-                    self.impls
-                        .entry(trait_ref.def_id)
-                        .or_default()
-                        .push((item.def_id.local_def_index, simplified_self_ty));
-                }
-            }
-            _ => {}
-        }
-    }
-
-    fn visit_trait_item(&mut self, _trait_item: &'v hir::TraitItem<'v>) {}
-
-    fn visit_impl_item(&mut self, _impl_item: &'v hir::ImplItem<'v>) {
-        // handled in `visit_item` above
-    }
-
-    fn visit_foreign_item(&mut self, _foreign_item: &'v hir::ForeignItem<'v>) {}
-}
-
 /// Used to prefetch queries which will be needed later by metadata encoding.
 /// Only a subset of the queries are actually prefetched to keep this code smaller.
 fn prefetch_mir(tcx: TyCtxt<'_>) {
diff --git a/compiler/rustc_middle/src/ty/print/pretty.rs b/compiler/rustc_middle/src/ty/print/pretty.rs
index 61f078c0e48..2ceb81b1fb3 100644
--- a/compiler/rustc_middle/src/ty/print/pretty.rs
+++ b/compiler/rustc_middle/src/ty/print/pretty.rs
@@ -9,7 +9,6 @@ use rustc_hir as hir;
 use rustc_hir::def::{self, CtorKind, DefKind, Namespace};
 use rustc_hir::def_id::{DefId, DefIdSet, CRATE_DEF_INDEX, LOCAL_CRATE};
 use rustc_hir::definitions::{DefPathData, DefPathDataName, DisambiguatedDefPathData};
-use rustc_hir::ItemKind;
 use rustc_session::config::TrimmedDefPaths;
 use rustc_session::cstore::{ExternCrate, ExternCrateSource};
 use rustc_span::symbol::{kw, Ident, Symbol};
@@ -2683,8 +2682,12 @@ fn for_each_def(tcx: TyCtxt<'_>, mut collect_fn: impl for<'b> FnMut(&'b Ident, N
     // Iterate all local crate items no matter where they are defined.
     let hir = tcx.hir();
     for id in hir.items() {
+        if matches!(hir.def_kind(id.def_id), DefKind::Use) {
+            continue;
+        }
+
         let item = hir.item(id);
-        if item.ident.name.as_str().is_empty() || matches!(item.kind, ItemKind::Use(_, _)) {
+        if item.ident.name.as_str().is_empty() {
             continue;
         }