Skip to content

Commit 0d39f9d

Browse files
committed
Do not fetch HIR to monomorphize impls.
1 parent facecf6 commit 0d39f9d

File tree

1 file changed

+42
-72
lines changed

1 file changed

+42
-72
lines changed

compiler/rustc_monomorphize/src/collector.rs

+42-72
Original file line numberDiff line numberDiff line change
@@ -1191,28 +1191,14 @@ impl<'v> RootCollector<'_, 'v> {
11911191
fn process_item(&mut self, id: hir::ItemId) {
11921192
match self.tcx.def_kind(id.owner_id) {
11931193
DefKind::Enum | DefKind::Struct | DefKind::Union => {
1194-
let item = self.tcx.hir().item(id);
1195-
match item.kind {
1196-
hir::ItemKind::Enum(_, ref generics)
1197-
| hir::ItemKind::Struct(_, ref generics)
1198-
| hir::ItemKind::Union(_, ref generics) => {
1199-
if generics.params.is_empty() {
1200-
if self.mode == MonoItemCollectionMode::Eager {
1201-
debug!(
1202-
"RootCollector: ADT drop-glue for {}",
1203-
self.tcx.def_path_str(item.owner_id.to_def_id())
1204-
);
1205-
1206-
let ty = Instance::new(
1207-
item.owner_id.to_def_id(),
1208-
InternalSubsts::empty(),
1209-
)
1210-
.ty(self.tcx, ty::ParamEnv::reveal_all());
1211-
visit_drop_use(self.tcx, ty, true, DUMMY_SP, self.output);
1212-
}
1213-
}
1214-
}
1215-
_ => bug!(),
1194+
if self.tcx.generics_of(id.owner_id).count() == 0
1195+
&& self.mode == MonoItemCollectionMode::Eager
1196+
{
1197+
debug!("RootCollector: ADT drop-glue for `{id:?}`",);
1198+
1199+
let ty =
1200+
self.tcx.bound_type_of(id.owner_id.to_def_id()).no_bound_vars().unwrap();
1201+
visit_drop_use(self.tcx, ty, true, DUMMY_SP, self.output);
12161202
}
12171203
}
12181204
DefKind::GlobalAsm => {
@@ -1240,8 +1226,7 @@ impl<'v> RootCollector<'_, 'v> {
12401226
}
12411227
DefKind::Impl { .. } => {
12421228
if self.mode == MonoItemCollectionMode::Eager {
1243-
let item = self.tcx.hir().item(id);
1244-
create_mono_items_for_default_impls(self.tcx, item, self.output);
1229+
create_mono_items_for_default_impls(self.tcx, id, self.output);
12451230
}
12461231
}
12471232
DefKind::Fn => {
@@ -1326,66 +1311,51 @@ fn item_requires_monomorphization(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool {
13261311
generics.requires_monomorphization(tcx)
13271312
}
13281313

1314+
#[instrument(level = "debug", skip(tcx, output))]
13291315
fn create_mono_items_for_default_impls<'tcx>(
13301316
tcx: TyCtxt<'tcx>,
1331-
item: &'tcx hir::Item<'tcx>,
1317+
item: hir::ItemId,
13321318
output: &mut MonoItems<'tcx>,
13331319
) {
1334-
match item.kind {
1335-
hir::ItemKind::Impl(ref impl_) => {
1336-
if matches!(impl_.polarity, hir::ImplPolarity::Negative(_)) {
1337-
return;
1338-
}
1320+
let polarity = tcx.impl_polarity(item.owner_id);
1321+
if matches!(polarity, ty::ImplPolarity::Negative) {
1322+
return;
1323+
}
13391324

1340-
for param in impl_.generics.params {
1341-
match param.kind {
1342-
hir::GenericParamKind::Lifetime { .. } => {}
1343-
hir::GenericParamKind::Type { .. } | hir::GenericParamKind::Const { .. } => {
1344-
return;
1345-
}
1346-
}
1347-
}
1325+
if tcx.generics_of(item.owner_id).own_requires_monomorphization() {
1326+
return;
1327+
}
13481328

1349-
debug!(
1350-
"create_mono_items_for_default_impls(item={})",
1351-
tcx.def_path_str(item.owner_id.to_def_id())
1352-
);
1329+
let Some(trait_ref) = tcx.impl_trait_ref(item.owner_id) else {
1330+
return;
1331+
};
13531332

1354-
if let Some(trait_ref) = tcx.impl_trait_ref(item.owner_id) {
1355-
let trait_ref = trait_ref.subst_identity();
1333+
let trait_ref = trait_ref.subst_identity();
13561334

1357-
let param_env = ty::ParamEnv::reveal_all();
1358-
let trait_ref = tcx.normalize_erasing_regions(param_env, trait_ref);
1359-
let overridden_methods = tcx.impl_item_implementor_ids(item.owner_id);
1360-
for method in tcx.provided_trait_methods(trait_ref.def_id) {
1361-
if overridden_methods.contains_key(&method.def_id) {
1362-
continue;
1363-
}
1335+
let param_env = ty::ParamEnv::reveal_all();
1336+
let trait_ref = tcx.normalize_erasing_regions(param_env, trait_ref);
1337+
let overridden_methods = tcx.impl_item_implementor_ids(item.owner_id);
1338+
for method in tcx.provided_trait_methods(trait_ref.def_id) {
1339+
if overridden_methods.contains_key(&method.def_id) {
1340+
continue;
1341+
}
13641342

1365-
if tcx.generics_of(method.def_id).own_requires_monomorphization() {
1366-
continue;
1367-
}
1343+
if tcx.generics_of(method.def_id).own_requires_monomorphization() {
1344+
continue;
1345+
}
13681346

1369-
let substs =
1370-
InternalSubsts::for_item(tcx, method.def_id, |param, _| match param.kind {
1371-
GenericParamDefKind::Lifetime => tcx.lifetimes.re_erased.into(),
1372-
GenericParamDefKind::Type { .. }
1373-
| GenericParamDefKind::Const { .. } => {
1374-
trait_ref.substs[param.index as usize]
1375-
}
1376-
});
1377-
let instance =
1378-
ty::Instance::expect_resolve(tcx, param_env, method.def_id, substs);
1379-
1380-
let mono_item = create_fn_mono_item(tcx, instance, DUMMY_SP);
1381-
if mono_item.node.is_instantiable(tcx) && should_codegen_locally(tcx, &instance)
1382-
{
1383-
output.push(mono_item);
1384-
}
1385-
}
1347+
let substs = InternalSubsts::for_item(tcx, method.def_id, |param, _| match param.kind {
1348+
GenericParamDefKind::Lifetime => tcx.lifetimes.re_erased.into(),
1349+
GenericParamDefKind::Type { .. } | GenericParamDefKind::Const { .. } => {
1350+
trait_ref.substs[param.index as usize]
13861351
}
1352+
});
1353+
let instance = ty::Instance::expect_resolve(tcx, param_env, method.def_id, substs);
1354+
1355+
let mono_item = create_fn_mono_item(tcx, instance, DUMMY_SP);
1356+
if mono_item.node.is_instantiable(tcx) && should_codegen_locally(tcx, &instance) {
1357+
output.push(mono_item);
13871358
}
1388-
_ => bug!(),
13891359
}
13901360
}
13911361

0 commit comments

Comments
 (0)