Skip to content

Commit 8e91327

Browse files
authored
Rollup merge of #135520 - compiler-errors:mono-impossible-drop-with-lifetimes, r=BoxyUwU
Make sure we actually use the right trivial lifetime substs when eagerly monomorphizing drop for ADTs Absolutely clueless mistake of mine. Whoops. When eagerly collecting mono items, when we encounter an ADT, we try to monomorphize its drop glue. In #135313, I made it so that this acts more like eagerly monomorphizing functions, where we allow (in this case) ADTs with lifetimes, since those can be erased by codegen. However, I did not account for the call to `instantiate_and_check_impossible_predicates`, which was still passing an empty set of args. This means that if the ADT in question had any predicates, we'd get an index out of bounds panic. This PR creates the correct set of args for the ADT. Fixes #135515. I assume that this manifests in that issue because of `-Clink-dead-code` or something.
2 parents f35ff74 + c89ee08 commit 8e91327

File tree

3 files changed

+32
-5
lines changed

3 files changed

+32
-5
lines changed

compiler/rustc_monomorphize/src/collector.rs

+18-4
Original file line numberDiff line numberDiff line change
@@ -1410,19 +1410,33 @@ impl<'v> RootCollector<'_, 'v> {
14101410
&& !self.tcx.generics_of(id.owner_id).requires_monomorphization(self.tcx)
14111411
{
14121412
debug!("RootCollector: ADT drop-glue for `{id:?}`",);
1413+
let id_args =
1414+
ty::GenericArgs::for_item(self.tcx, id.owner_id.to_def_id(), |param, _| {
1415+
match param.kind {
1416+
GenericParamDefKind::Lifetime => {
1417+
self.tcx.lifetimes.re_erased.into()
1418+
}
1419+
GenericParamDefKind::Type { .. }
1420+
| GenericParamDefKind::Const { .. } => {
1421+
unreachable!(
1422+
"`own_requires_monomorphization` check means that \
1423+
we should have no type/const params"
1424+
)
1425+
}
1426+
}
1427+
});
14131428

14141429
// This type is impossible to instantiate, so we should not try to
14151430
// generate a `drop_in_place` instance for it.
14161431
if self.tcx.instantiate_and_check_impossible_predicates((
14171432
id.owner_id.to_def_id(),
1418-
ty::List::empty(),
1433+
id_args,
14191434
)) {
14201435
return;
14211436
}
14221437

1423-
let ty = self.tcx.erase_regions(
1424-
self.tcx.type_of(id.owner_id.to_def_id()).instantiate_identity(),
1425-
);
1438+
let ty =
1439+
self.tcx.type_of(id.owner_id.to_def_id()).instantiate(self.tcx, id_args);
14261440
assert!(!ty.has_non_region_param());
14271441
visit_drop_use(self.tcx, ty, true, DUMMY_SP, self.output);
14281442
}

compiler/rustc_type_ir/src/binder.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -804,7 +804,7 @@ impl<'a, I: Interner> ArgFolder<'a, I> {
804804
#[inline(never)]
805805
fn region_param_out_of_range(&self, ebr: I::EarlyParamRegion, r: I::Region) -> ! {
806806
panic!(
807-
"const parameter `{:?}` ({:?}/{}) out of range when instantiating args={:?}",
807+
"region parameter `{:?}` ({:?}/{}) out of range when instantiating args={:?}",
808808
ebr,
809809
r,
810810
ebr.index(),

tests/codegen-units/item-collection/drop-glue-eager.rs

+13
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,16 @@ impl<'a> Drop for StructWithDropAndLt<'a> {
4141
struct StructWithDropAndLt<'a> {
4242
x: &'a i32,
4343
}
44+
45+
// Make sure we don't ICE when checking impossible predicates for the struct.
46+
// Regression test for <https://github.com/rust-lang/rust/issues/135515>.
47+
//~ MONO_ITEM fn std::ptr::drop_in_place::<StructWithLtAndPredicate<'_>> - shim(Some(StructWithLtAndPredicate<'_>))
48+
struct StructWithLtAndPredicate<'a: 'a> {
49+
x: &'a i32,
50+
}
51+
52+
// We should be able to monomorphize drops for struct with lifetimes.
53+
impl<'a> Drop for StructWithLtAndPredicate<'a> {
54+
//~ MONO_ITEM fn <StructWithLtAndPredicate<'_> as std::ops::Drop>::drop
55+
fn drop(&mut self) {}
56+
}

0 commit comments

Comments
 (0)