1
Fork 0

remove ItemLikeVisitor impls from monomorphize and rustc_typeck crates

Signed-off-by: Miguel Guarniz <mi9uel9@gmail.com>
This commit is contained in:
Miguel Guarniz 2022-04-07 19:59:02 -04:00
parent d2840d237c
commit f9781fd071
2 changed files with 97 additions and 127 deletions

View file

@ -181,8 +181,8 @@
use rustc_data_structures::fx::{FxHashMap, FxHashSet}; use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use rustc_data_structures::sync::{par_iter, MTLock, MTRef, ParallelIterator}; use rustc_data_structures::sync::{par_iter, MTLock, MTRef, ParallelIterator};
use rustc_hir as hir; use rustc_hir as hir;
use rustc_hir::def::DefKind;
use rustc_hir::def_id::{DefId, DefIdMap, LocalDefId, LOCAL_CRATE}; use rustc_hir::def_id::{DefId, DefIdMap, LocalDefId, LOCAL_CRATE};
use rustc_hir::itemlikevisit::ItemLikeVisitor;
use rustc_hir::lang_items::LangItem; use rustc_hir::lang_items::LangItem;
use rustc_index::bit_set::GrowableBitSet; use rustc_index::bit_set::GrowableBitSet;
use rustc_middle::mir::interpret::{AllocId, ConstValue}; use rustc_middle::mir::interpret::{AllocId, ConstValue};
@ -327,11 +327,19 @@ fn collect_roots(tcx: TyCtxt<'_>, mode: MonoItemCollectionMode) -> Vec<MonoItem<
debug!("collect_roots: entry_fn = {:?}", entry_fn); debug!("collect_roots: entry_fn = {:?}", entry_fn);
let mut visitor = RootCollector { tcx, mode, entry_fn, output: &mut roots }; let mut collector = RootCollector { tcx, mode, entry_fn, output: &mut roots };
tcx.hir().visit_all_item_likes(&mut visitor); let crate_items = tcx.hir_crate_items(());
visitor.push_extra_entry_roots(); for id in crate_items.items() {
collector.process_item(id);
}
for id in crate_items.impl_items() {
collector.process_impl_item(id);
}
collector.push_extra_entry_roots();
} }
// We can only codegen items that are instantiable - items all of // We can only codegen items that are instantiable - items all of
@ -1139,87 +1147,74 @@ struct RootCollector<'a, 'tcx> {
entry_fn: Option<(DefId, EntryFnType)>, entry_fn: Option<(DefId, EntryFnType)>,
} }
impl<'v> ItemLikeVisitor<'v> for RootCollector<'_, 'v> { impl<'v> RootCollector<'_, 'v> {
fn visit_item(&mut self, item: &'v hir::Item<'v>) { fn process_item(&mut self, id: hir::ItemId) {
match item.kind { match self.tcx.hir().def_kind(id.def_id) {
hir::ItemKind::ExternCrate(..) DefKind::Enum | DefKind::Struct | DefKind::Union => {
| hir::ItemKind::Use(..) let item = self.tcx.hir().item(id);
| hir::ItemKind::Macro(..) match item.kind {
| hir::ItemKind::ForeignMod { .. } hir::ItemKind::Enum(_, ref generics)
| hir::ItemKind::TyAlias(..) | hir::ItemKind::Struct(_, ref generics)
| hir::ItemKind::Trait(..) | hir::ItemKind::Union(_, ref generics) => {
| hir::ItemKind::TraitAlias(..) if generics.params.is_empty() {
| hir::ItemKind::OpaqueTy(..) if self.mode == MonoItemCollectionMode::Eager {
| hir::ItemKind::Mod(..) => { debug!(
// Nothing to do, just keep recursing. "RootCollector: ADT drop-glue for {}",
} self.tcx.def_path_str(item.def_id.to_def_id())
);
hir::ItemKind::Impl { .. } => { let ty =
if self.mode == MonoItemCollectionMode::Eager { Instance::new(item.def_id.to_def_id(), InternalSubsts::empty())
create_mono_items_for_default_impls(self.tcx, item, self.output); .ty(self.tcx, ty::ParamEnv::reveal_all());
} visit_drop_use(self.tcx, ty, true, DUMMY_SP, self.output);
} }
}
hir::ItemKind::Enum(_, ref generics)
| hir::ItemKind::Struct(_, ref generics)
| hir::ItemKind::Union(_, ref generics) => {
if generics.params.is_empty() {
if self.mode == MonoItemCollectionMode::Eager {
debug!(
"RootCollector: ADT drop-glue for {}",
self.tcx.def_path_str(item.def_id.to_def_id())
);
let ty = Instance::new(item.def_id.to_def_id(), InternalSubsts::empty())
.ty(self.tcx, ty::ParamEnv::reveal_all());
visit_drop_use(self.tcx, ty, true, DUMMY_SP, self.output);
} }
_ => {}
} }
} }
hir::ItemKind::GlobalAsm(..) => { DefKind::GlobalAsm => {
debug!( debug!(
"RootCollector: ItemKind::GlobalAsm({})", "RootCollector: ItemKind::GlobalAsm({})",
self.tcx.def_path_str(item.def_id.to_def_id()) self.tcx.def_path_str(id.def_id.to_def_id())
); );
self.output.push(dummy_spanned(MonoItem::GlobalAsm(item.item_id()))); self.output.push(dummy_spanned(MonoItem::GlobalAsm(id)));
} }
hir::ItemKind::Static(..) => { DefKind::Static(..) => {
debug!( debug!(
"RootCollector: ItemKind::Static({})", "RootCollector: ItemKind::Static({})",
self.tcx.def_path_str(item.def_id.to_def_id()) self.tcx.def_path_str(id.def_id.to_def_id())
); );
self.output.push(dummy_spanned(MonoItem::Static(item.def_id.to_def_id()))); self.output.push(dummy_spanned(MonoItem::Static(id.def_id.to_def_id())));
} }
hir::ItemKind::Const(..) => { DefKind::Const => {
// const items only generate mono items if they are // const items only generate mono items if they are
// actually used somewhere. Just declaring them is insufficient. // actually used somewhere. Just declaring them is insufficient.
// but even just declaring them must collect the items they refer to // but even just declaring them must collect the items they refer to
if let Ok(val) = self.tcx.const_eval_poly(item.def_id.to_def_id()) { if let Ok(val) = self.tcx.const_eval_poly(id.def_id.to_def_id()) {
collect_const_value(self.tcx, val, &mut self.output); collect_const_value(self.tcx, val, &mut self.output);
} }
} }
hir::ItemKind::Fn(..) => { DefKind::Impl => {
self.push_if_root(item.def_id); let item = self.tcx.hir().item(id);
if self.mode == MonoItemCollectionMode::Eager {
create_mono_items_for_default_impls(self.tcx, item, self.output);
}
} }
DefKind::Fn => {
self.push_if_root(id.def_id);
}
_ => {}
} }
} }
fn visit_trait_item(&mut self, _: &'v hir::TraitItem<'v>) { fn process_impl_item(&mut self, id: hir::ImplItemId) {
// Even if there's a default body with no explicit generics, if matches!(self.tcx.hir().def_kind(id.def_id), DefKind::AssocFn) {
// it's still generic over some `Self: Trait`, so not a root. self.push_if_root(id.def_id);
}
fn visit_impl_item(&mut self, ii: &'v hir::ImplItem<'v>) {
if let hir::ImplItemKind::Fn(hir::FnSig { .. }, _) = ii.kind {
self.push_if_root(ii.def_id);
} }
} }
fn visit_foreign_item(&mut self, _foreign_item: &'v hir::ForeignItem<'v>) {}
}
impl<'v> RootCollector<'_, 'v> {
fn is_root(&self, def_id: LocalDefId) -> bool { fn is_root(&self, def_id: LocalDefId) -> bool {
!item_requires_monomorphization(self.tcx, def_id) !item_requires_monomorphization(self.tcx, def_id)
&& match self.mode { && match self.mode {

View file

@ -1,7 +1,6 @@
use rustc_data_structures::fx::FxHashMap; use rustc_data_structures::fx::FxHashMap;
use rustc_hir as hir; use rustc_hir::def::DefKind;
use rustc_hir::def_id::DefId; use rustc_hir::def_id::DefId;
use rustc_hir::itemlikevisit::ItemLikeVisitor;
use rustc_middle::ty::subst::{GenericArg, GenericArgKind, Subst}; use rustc_middle::ty::subst::{GenericArg, GenericArgKind, Subst};
use rustc_middle::ty::{self, Ty, TyCtxt}; use rustc_middle::ty::{self, Ty, TyCtxt};
use rustc_span::Span; use rustc_span::Span;
@ -29,81 +28,57 @@ pub fn infer_predicates<'tcx>(
while predicates_added { while predicates_added {
predicates_added = false; predicates_added = false;
let mut visitor = InferVisitor {
tcx,
global_inferred_outlives: &mut global_inferred_outlives,
predicates_added: &mut predicates_added,
explicit_map,
};
// Visit all the crates and infer predicates // Visit all the crates and infer predicates
tcx.hir().visit_all_item_likes(&mut visitor); for id in tcx.hir().items() {
} let item_did = id.def_id;
global_inferred_outlives debug!("InferVisitor::visit_item(item={:?})", item_did);
}
pub struct InferVisitor<'cx, 'tcx> { let mut item_required_predicates = RequiredPredicates::default();
tcx: TyCtxt<'tcx>, match tcx.hir().def_kind(item_did) {
global_inferred_outlives: &'cx mut FxHashMap<DefId, RequiredPredicates<'tcx>>, DefKind::Union | DefKind::Enum | DefKind::Struct => {
predicates_added: &'cx mut bool, let adt_def = tcx.adt_def(item_did.to_def_id());
explicit_map: &'cx mut ExplicitPredicatesMap<'tcx>,
}
impl<'cx, 'tcx> ItemLikeVisitor<'tcx> for InferVisitor<'cx, 'tcx> { // Iterate over all fields in item_did
fn visit_item(&mut self, item: &hir::Item<'_>) { for field_def in adt_def.all_fields() {
let item_did = item.def_id; // Calculating the predicate requirements necessary
// for item_did.
debug!("InferVisitor::visit_item(item={:?})", item_did); //
// For field of type &'a T (reference) or Adt
let mut item_required_predicates = RequiredPredicates::default(); // (struct/enum/union) there will be outlive
match item.kind { // requirements for adt_def.
hir::ItemKind::Union(..) | hir::ItemKind::Enum(..) | hir::ItemKind::Struct(..) => { let field_ty = tcx.type_of(field_def.did);
let adt_def = self.tcx.adt_def(item_did.to_def_id()); let field_span = tcx.def_span(field_def.did);
insert_required_predicates_to_be_wf(
// Iterate over all fields in item_did tcx,
for field_def in adt_def.all_fields() { field_ty,
// Calculating the predicate requirements necessary field_span,
// for item_did. &mut global_inferred_outlives,
// &mut item_required_predicates,
// For field of type &'a T (reference) or Adt explicit_map,
// (struct/enum/union) there will be outlive );
// requirements for adt_def. }
let field_ty = self.tcx.type_of(field_def.did);
let field_span = self.tcx.def_span(field_def.did);
insert_required_predicates_to_be_wf(
self.tcx,
field_ty,
field_span,
self.global_inferred_outlives,
&mut item_required_predicates,
&mut self.explicit_map,
);
} }
_ => {}
};
// If new predicates were added (`local_predicate_map` has more
// predicates than the `global_inferred_outlives`), the new predicates
// might result in implied predicates for their parent types.
// Therefore mark `predicates_added` as true and which will ensure
// we walk the crates again and re-calculate predicates for all
// items.
let item_predicates_len: usize =
global_inferred_outlives.get(&item_did.to_def_id()).map_or(0, |p| p.len());
if item_required_predicates.len() > item_predicates_len {
predicates_added = true;
global_inferred_outlives.insert(item_did.to_def_id(), item_required_predicates);
} }
_ => {}
};
// If new predicates were added (`local_predicate_map` has more
// predicates than the `global_inferred_outlives`), the new predicates
// might result in implied predicates for their parent types.
// Therefore mark `predicates_added` as true and which will ensure
// we walk the crates again and re-calculate predicates for all
// items.
let item_predicates_len: usize =
self.global_inferred_outlives.get(&item_did.to_def_id()).map_or(0, |p| p.len());
if item_required_predicates.len() > item_predicates_len {
*self.predicates_added = true;
self.global_inferred_outlives.insert(item_did.to_def_id(), item_required_predicates);
} }
} }
fn visit_trait_item(&mut self, _trait_item: &'tcx hir::TraitItem<'tcx>) {} global_inferred_outlives
fn visit_impl_item(&mut self, _impl_item: &'tcx hir::ImplItem<'tcx>) {}
fn visit_foreign_item(&mut self, _foreign_item: &'tcx hir::ForeignItem<'tcx>) {}
} }
fn insert_required_predicates_to_be_wf<'tcx>( fn insert_required_predicates_to_be_wf<'tcx>(