Skip to content

Commit f9781fd

Browse files
committed
remove ItemLikeVisitor impls from monomorphize and rustc_typeck crates
Signed-off-by: Miguel Guarniz <[email protected]>
1 parent d2840d2 commit f9781fd

File tree

2 files changed

+97
-127
lines changed

2 files changed

+97
-127
lines changed

compiler/rustc_monomorphize/src/collector.rs

Lines changed: 55 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -181,8 +181,8 @@
181181
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
182182
use rustc_data_structures::sync::{par_iter, MTLock, MTRef, ParallelIterator};
183183
use rustc_hir as hir;
184+
use rustc_hir::def::DefKind;
184185
use rustc_hir::def_id::{DefId, DefIdMap, LocalDefId, LOCAL_CRATE};
185-
use rustc_hir::itemlikevisit::ItemLikeVisitor;
186186
use rustc_hir::lang_items::LangItem;
187187
use rustc_index::bit_set::GrowableBitSet;
188188
use rustc_middle::mir::interpret::{AllocId, ConstValue};
@@ -327,11 +327,19 @@ fn collect_roots(tcx: TyCtxt<'_>, mode: MonoItemCollectionMode) -> Vec<MonoItem<
327327

328328
debug!("collect_roots: entry_fn = {:?}", entry_fn);
329329

330-
let mut visitor = RootCollector { tcx, mode, entry_fn, output: &mut roots };
330+
let mut collector = RootCollector { tcx, mode, entry_fn, output: &mut roots };
331331

332-
tcx.hir().visit_all_item_likes(&mut visitor);
332+
let crate_items = tcx.hir_crate_items(());
333333

334-
visitor.push_extra_entry_roots();
334+
for id in crate_items.items() {
335+
collector.process_item(id);
336+
}
337+
338+
for id in crate_items.impl_items() {
339+
collector.process_impl_item(id);
340+
}
341+
342+
collector.push_extra_entry_roots();
335343
}
336344

337345
// We can only codegen items that are instantiable - items all of
@@ -1139,87 +1147,74 @@ struct RootCollector<'a, 'tcx> {
11391147
entry_fn: Option<(DefId, EntryFnType)>,
11401148
}
11411149

1142-
impl<'v> ItemLikeVisitor<'v> for RootCollector<'_, 'v> {
1143-
fn visit_item(&mut self, item: &'v hir::Item<'v>) {
1144-
match item.kind {
1145-
hir::ItemKind::ExternCrate(..)
1146-
| hir::ItemKind::Use(..)
1147-
| hir::ItemKind::Macro(..)
1148-
| hir::ItemKind::ForeignMod { .. }
1149-
| hir::ItemKind::TyAlias(..)
1150-
| hir::ItemKind::Trait(..)
1151-
| hir::ItemKind::TraitAlias(..)
1152-
| hir::ItemKind::OpaqueTy(..)
1153-
| hir::ItemKind::Mod(..) => {
1154-
// Nothing to do, just keep recursing.
1155-
}
1156-
1157-
hir::ItemKind::Impl { .. } => {
1158-
if self.mode == MonoItemCollectionMode::Eager {
1159-
create_mono_items_for_default_impls(self.tcx, item, self.output);
1160-
}
1161-
}
1162-
1163-
hir::ItemKind::Enum(_, ref generics)
1164-
| hir::ItemKind::Struct(_, ref generics)
1165-
| hir::ItemKind::Union(_, ref generics) => {
1166-
if generics.params.is_empty() {
1167-
if self.mode == MonoItemCollectionMode::Eager {
1168-
debug!(
1169-
"RootCollector: ADT drop-glue for {}",
1170-
self.tcx.def_path_str(item.def_id.to_def_id())
1171-
);
1172-
1173-
let ty = Instance::new(item.def_id.to_def_id(), InternalSubsts::empty())
1174-
.ty(self.tcx, ty::ParamEnv::reveal_all());
1175-
visit_drop_use(self.tcx, ty, true, DUMMY_SP, self.output);
1150+
impl<'v> RootCollector<'_, 'v> {
1151+
fn process_item(&mut self, id: hir::ItemId) {
1152+
match self.tcx.hir().def_kind(id.def_id) {
1153+
DefKind::Enum | DefKind::Struct | DefKind::Union => {
1154+
let item = self.tcx.hir().item(id);
1155+
match item.kind {
1156+
hir::ItemKind::Enum(_, ref generics)
1157+
| hir::ItemKind::Struct(_, ref generics)
1158+
| hir::ItemKind::Union(_, ref generics) => {
1159+
if generics.params.is_empty() {
1160+
if self.mode == MonoItemCollectionMode::Eager {
1161+
debug!(
1162+
"RootCollector: ADT drop-glue for {}",
1163+
self.tcx.def_path_str(item.def_id.to_def_id())
1164+
);
1165+
1166+
let ty =
1167+
Instance::new(item.def_id.to_def_id(), InternalSubsts::empty())
1168+
.ty(self.tcx, ty::ParamEnv::reveal_all());
1169+
visit_drop_use(self.tcx, ty, true, DUMMY_SP, self.output);
1170+
}
1171+
}
11761172
}
1173+
_ => {}
11771174
}
11781175
}
1179-
hir::ItemKind::GlobalAsm(..) => {
1176+
DefKind::GlobalAsm => {
11801177
debug!(
11811178
"RootCollector: ItemKind::GlobalAsm({})",
1182-
self.tcx.def_path_str(item.def_id.to_def_id())
1179+
self.tcx.def_path_str(id.def_id.to_def_id())
11831180
);
1184-
self.output.push(dummy_spanned(MonoItem::GlobalAsm(item.item_id())));
1181+
self.output.push(dummy_spanned(MonoItem::GlobalAsm(id)));
11851182
}
1186-
hir::ItemKind::Static(..) => {
1183+
DefKind::Static(..) => {
11871184
debug!(
11881185
"RootCollector: ItemKind::Static({})",
1189-
self.tcx.def_path_str(item.def_id.to_def_id())
1186+
self.tcx.def_path_str(id.def_id.to_def_id())
11901187
);
1191-
self.output.push(dummy_spanned(MonoItem::Static(item.def_id.to_def_id())));
1188+
self.output.push(dummy_spanned(MonoItem::Static(id.def_id.to_def_id())));
11921189
}
1193-
hir::ItemKind::Const(..) => {
1190+
DefKind::Const => {
11941191
// const items only generate mono items if they are
11951192
// actually used somewhere. Just declaring them is insufficient.
11961193

11971194
// but even just declaring them must collect the items they refer to
1198-
if let Ok(val) = self.tcx.const_eval_poly(item.def_id.to_def_id()) {
1195+
if let Ok(val) = self.tcx.const_eval_poly(id.def_id.to_def_id()) {
11991196
collect_const_value(self.tcx, val, &mut self.output);
12001197
}
12011198
}
1202-
hir::ItemKind::Fn(..) => {
1203-
self.push_if_root(item.def_id);
1199+
DefKind::Impl => {
1200+
let item = self.tcx.hir().item(id);
1201+
if self.mode == MonoItemCollectionMode::Eager {
1202+
create_mono_items_for_default_impls(self.tcx, item, self.output);
1203+
}
1204+
}
1205+
DefKind::Fn => {
1206+
self.push_if_root(id.def_id);
12041207
}
1208+
_ => {}
12051209
}
12061210
}
12071211

1208-
fn visit_trait_item(&mut self, _: &'v hir::TraitItem<'v>) {
1209-
// Even if there's a default body with no explicit generics,
1210-
// it's still generic over some `Self: Trait`, so not a root.
1211-
}
1212-
1213-
fn visit_impl_item(&mut self, ii: &'v hir::ImplItem<'v>) {
1214-
if let hir::ImplItemKind::Fn(hir::FnSig { .. }, _) = ii.kind {
1215-
self.push_if_root(ii.def_id);
1212+
fn process_impl_item(&mut self, id: hir::ImplItemId) {
1213+
if matches!(self.tcx.hir().def_kind(id.def_id), DefKind::AssocFn) {
1214+
self.push_if_root(id.def_id);
12161215
}
12171216
}
12181217

1219-
fn visit_foreign_item(&mut self, _foreign_item: &'v hir::ForeignItem<'v>) {}
1220-
}
1221-
1222-
impl<'v> RootCollector<'_, 'v> {
12231218
fn is_root(&self, def_id: LocalDefId) -> bool {
12241219
!item_requires_monomorphization(self.tcx, def_id)
12251220
&& match self.mode {

compiler/rustc_typeck/src/outlives/implicit_infer.rs

Lines changed: 42 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use rustc_data_structures::fx::FxHashMap;
2-
use rustc_hir as hir;
2+
use rustc_hir::def::DefKind;
33
use rustc_hir::def_id::DefId;
4-
use rustc_hir::itemlikevisit::ItemLikeVisitor;
54
use rustc_middle::ty::subst::{GenericArg, GenericArgKind, Subst};
65
use rustc_middle::ty::{self, Ty, TyCtxt};
76
use rustc_span::Span;
@@ -29,81 +28,57 @@ pub fn infer_predicates<'tcx>(
2928
while predicates_added {
3029
predicates_added = false;
3130

32-
let mut visitor = InferVisitor {
33-
tcx,
34-
global_inferred_outlives: &mut global_inferred_outlives,
35-
predicates_added: &mut predicates_added,
36-
explicit_map,
37-
};
38-
3931
// Visit all the crates and infer predicates
40-
tcx.hir().visit_all_item_likes(&mut visitor);
41-
}
32+
for id in tcx.hir().items() {
33+
let item_did = id.def_id;
4234

43-
global_inferred_outlives
44-
}
35+
debug!("InferVisitor::visit_item(item={:?})", item_did);
4536

46-
pub struct InferVisitor<'cx, 'tcx> {
47-
tcx: TyCtxt<'tcx>,
48-
global_inferred_outlives: &'cx mut FxHashMap<DefId, RequiredPredicates<'tcx>>,
49-
predicates_added: &'cx mut bool,
50-
explicit_map: &'cx mut ExplicitPredicatesMap<'tcx>,
51-
}
52-
53-
impl<'cx, 'tcx> ItemLikeVisitor<'tcx> for InferVisitor<'cx, 'tcx> {
54-
fn visit_item(&mut self, item: &hir::Item<'_>) {
55-
let item_did = item.def_id;
56-
57-
debug!("InferVisitor::visit_item(item={:?})", item_did);
37+
let mut item_required_predicates = RequiredPredicates::default();
38+
match tcx.hir().def_kind(item_did) {
39+
DefKind::Union | DefKind::Enum | DefKind::Struct => {
40+
let adt_def = tcx.adt_def(item_did.to_def_id());
5841

59-
let mut item_required_predicates = RequiredPredicates::default();
60-
match item.kind {
61-
hir::ItemKind::Union(..) | hir::ItemKind::Enum(..) | hir::ItemKind::Struct(..) => {
62-
let adt_def = self.tcx.adt_def(item_did.to_def_id());
63-
64-
// Iterate over all fields in item_did
65-
for field_def in adt_def.all_fields() {
66-
// Calculating the predicate requirements necessary
67-
// for item_did.
68-
//
69-
// For field of type &'a T (reference) or Adt
70-
// (struct/enum/union) there will be outlive
71-
// requirements for adt_def.
72-
let field_ty = self.tcx.type_of(field_def.did);
73-
let field_span = self.tcx.def_span(field_def.did);
74-
insert_required_predicates_to_be_wf(
75-
self.tcx,
76-
field_ty,
77-
field_span,
78-
self.global_inferred_outlives,
79-
&mut item_required_predicates,
80-
&mut self.explicit_map,
81-
);
42+
// Iterate over all fields in item_did
43+
for field_def in adt_def.all_fields() {
44+
// Calculating the predicate requirements necessary
45+
// for item_did.
46+
//
47+
// For field of type &'a T (reference) or Adt
48+
// (struct/enum/union) there will be outlive
49+
// requirements for adt_def.
50+
let field_ty = tcx.type_of(field_def.did);
51+
let field_span = tcx.def_span(field_def.did);
52+
insert_required_predicates_to_be_wf(
53+
tcx,
54+
field_ty,
55+
field_span,
56+
&mut global_inferred_outlives,
57+
&mut item_required_predicates,
58+
explicit_map,
59+
);
60+
}
8261
}
83-
}
8462

85-
_ => {}
86-
};
63+
_ => {}
64+
};
8765

88-
// If new predicates were added (`local_predicate_map` has more
89-
// predicates than the `global_inferred_outlives`), the new predicates
90-
// might result in implied predicates for their parent types.
91-
// Therefore mark `predicates_added` as true and which will ensure
92-
// we walk the crates again and re-calculate predicates for all
93-
// items.
94-
let item_predicates_len: usize =
95-
self.global_inferred_outlives.get(&item_did.to_def_id()).map_or(0, |p| p.len());
96-
if item_required_predicates.len() > item_predicates_len {
97-
*self.predicates_added = true;
98-
self.global_inferred_outlives.insert(item_did.to_def_id(), item_required_predicates);
66+
// If new predicates were added (`local_predicate_map` has more
67+
// predicates than the `global_inferred_outlives`), the new predicates
68+
// might result in implied predicates for their parent types.
69+
// Therefore mark `predicates_added` as true and which will ensure
70+
// we walk the crates again and re-calculate predicates for all
71+
// items.
72+
let item_predicates_len: usize =
73+
global_inferred_outlives.get(&item_did.to_def_id()).map_or(0, |p| p.len());
74+
if item_required_predicates.len() > item_predicates_len {
75+
predicates_added = true;
76+
global_inferred_outlives.insert(item_did.to_def_id(), item_required_predicates);
77+
}
9978
}
10079
}
10180

102-
fn visit_trait_item(&mut self, _trait_item: &'tcx hir::TraitItem<'tcx>) {}
103-
104-
fn visit_impl_item(&mut self, _impl_item: &'tcx hir::ImplItem<'tcx>) {}
105-
106-
fn visit_foreign_item(&mut self, _foreign_item: &'tcx hir::ForeignItem<'tcx>) {}
81+
global_inferred_outlives
10782
}
10883

10984
fn insert_required_predicates_to_be_wf<'tcx>(

0 commit comments

Comments
 (0)