Skip to content

Commit 3d6f4c8

Browse files
committed
remove ItemLikeVisitor impls and add fast paths using DefKind
Signed-off-by: Miguel Guarniz <mi9uel9@gmail.com>
1 parent 0d01ee9 commit 3d6f4c8

File tree

2 files changed

+26
-41
lines changed

2 files changed

+26
-41
lines changed

compiler/rustc_metadata/src/rmeta/encoder.rs

Lines changed: 21 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1783,14 +1783,31 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
17831783
debug!("EncodeContext::encode_traits_and_impls()");
17841784
empty_proc_macro!(self);
17851785
let tcx = self.tcx;
1786-
let mut visitor = ImplsVisitor { tcx, impls: FxHashMap::default() };
1786+
let mut fx_hash_map: FxHashMap<DefId, Vec<(DefIndex, Option<SimplifiedType>)>> =
1787+
FxHashMap::default();
17871788

17881789
for id in tcx.hir().items() {
1789-
let item = tcx.hir().item(id);
1790-
visitor.visit_item(item);
1790+
match tcx.hir().def_kind(id.def_id) {
1791+
DefKind::Impl => {
1792+
let item = tcx.hir().item(id);
1793+
if let Some(trait_ref) = tcx.impl_trait_ref(item.def_id.to_def_id()) {
1794+
let simplified_self_ty = fast_reject::simplify_type(
1795+
self.tcx,
1796+
trait_ref.self_ty(),
1797+
TreatParams::AsPlaceholders,
1798+
);
1799+
1800+
fx_hash_map
1801+
.entry(trait_ref.def_id)
1802+
.or_default()
1803+
.push((item.def_id.local_def_index, simplified_self_ty));
1804+
}
1805+
}
1806+
_ => continue,
1807+
}
17911808
}
17921809

1793-
let mut all_impls: Vec<_> = visitor.impls.into_iter().collect();
1810+
let mut all_impls: Vec<_> = fx_hash_map.into_iter().collect();
17941811

17951812
// Bring everything into deterministic order for hashing
17961813
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> {
20532070
}
20542071
}
20552072

2056-
struct ImplsVisitor<'tcx> {
2057-
tcx: TyCtxt<'tcx>,
2058-
impls: FxHashMap<DefId, Vec<(DefIndex, Option<SimplifiedType>)>>,
2059-
}
2060-
2061-
impl<'tcx, 'v> ItemLikeVisitor<'v> for ImplsVisitor<'tcx> {
2062-
fn visit_item(&mut self, item: &hir::Item<'_>) {
2063-
match item.kind {
2064-
hir::ItemKind::Impl(..) => {
2065-
if let Some(trait_ref) = self.tcx.impl_trait_ref(item.def_id.to_def_id()) {
2066-
let simplified_self_ty = fast_reject::simplify_type(
2067-
self.tcx,
2068-
trait_ref.self_ty(),
2069-
TreatParams::AsPlaceholders,
2070-
);
2071-
2072-
self.impls
2073-
.entry(trait_ref.def_id)
2074-
.or_default()
2075-
.push((item.def_id.local_def_index, simplified_self_ty));
2076-
}
2077-
}
2078-
_ => {}
2079-
}
2080-
}
2081-
2082-
fn visit_trait_item(&mut self, _trait_item: &'v hir::TraitItem<'v>) {}
2083-
2084-
fn visit_impl_item(&mut self, _impl_item: &'v hir::ImplItem<'v>) {
2085-
// handled in `visit_item` above
2086-
}
2087-
2088-
fn visit_foreign_item(&mut self, _foreign_item: &'v hir::ForeignItem<'v>) {}
2089-
}
2090-
20912073
/// Used to prefetch queries which will be needed later by metadata encoding.
20922074
/// Only a subset of the queries are actually prefetched to keep this code smaller.
20932075
fn prefetch_mir(tcx: TyCtxt<'_>) {

compiler/rustc_middle/src/ty/print/pretty.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ use rustc_hir as hir;
99
use rustc_hir::def::{self, CtorKind, DefKind, Namespace};
1010
use rustc_hir::def_id::{DefId, DefIdSet, CRATE_DEF_INDEX, LOCAL_CRATE};
1111
use rustc_hir::definitions::{DefPathData, DefPathDataName, DisambiguatedDefPathData};
12-
use rustc_hir::ItemKind;
1312
use rustc_session::config::TrimmedDefPaths;
1413
use rustc_session::cstore::{ExternCrate, ExternCrateSource};
1514
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
26832682
// Iterate all local crate items no matter where they are defined.
26842683
let hir = tcx.hir();
26852684
for id in hir.items() {
2685+
if matches!(hir.def_kind(id.def_id), DefKind::Use) {
2686+
continue;
2687+
}
2688+
26862689
let item = hir.item(id);
2687-
if item.ident.name.as_str().is_empty() || matches!(item.kind, ItemKind::Use(_, _)) {
2690+
if item.ident.name.as_str().is_empty() {
26882691
continue;
26892692
}
26902693

0 commit comments

Comments
 (0)