Skip to content

Commit 3736b85

Browse files
committed
Auto merge of #135313 - compiler-errors:needs-mono, r=BoxyUwU
Eagerly mono drop for structs with lifetimes That is, use `!generics.requires_monomorphization()` rather than `generics.is_empty()` like the rest of the mono collector code.
2 parents 8c39ce5 + 27603b2 commit 3736b85

File tree

2 files changed

+48
-2
lines changed

2 files changed

+48
-2
lines changed

Diff for: compiler/rustc_monomorphize/src/collector.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -1407,7 +1407,7 @@ impl<'v> RootCollector<'_, 'v> {
14071407
match self.tcx.def_kind(id.owner_id) {
14081408
DefKind::Enum | DefKind::Struct | DefKind::Union => {
14091409
if self.strategy == MonoItemCollectionStrategy::Eager
1410-
&& self.tcx.generics_of(id.owner_id).is_empty()
1410+
&& !self.tcx.generics_of(id.owner_id).requires_monomorphization(self.tcx)
14111411
{
14121412
debug!("RootCollector: ADT drop-glue for `{id:?}`",);
14131413

@@ -1420,7 +1420,10 @@ impl<'v> RootCollector<'_, 'v> {
14201420
return;
14211421
}
14221422

1423-
let ty = self.tcx.type_of(id.owner_id.to_def_id()).no_bound_vars().unwrap();
1423+
let ty = self.tcx.erase_regions(
1424+
self.tcx.type_of(id.owner_id.to_def_id()).instantiate_identity(),
1425+
);
1426+
assert!(!ty.has_non_region_param());
14241427
visit_drop_use(self.tcx, ty, true, DUMMY_SP, self.output);
14251428
}
14261429
}
+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Ensure that we *eagerly* monomorphize drop instances for structs with lifetimes.
2+
3+
//@ compile-flags:-Zprint-mono-items=eager
4+
//@ compile-flags:--crate-type=lib
5+
6+
//~ MONO_ITEM fn std::ptr::drop_in_place::<StructWithDrop> - shim(Some(StructWithDrop))
7+
struct StructWithDrop {
8+
x: i32,
9+
}
10+
11+
impl Drop for StructWithDrop {
12+
//~ MONO_ITEM fn <StructWithDrop as std::ops::Drop>::drop
13+
fn drop(&mut self) {}
14+
}
15+
16+
struct StructNoDrop {
17+
x: i32,
18+
}
19+
20+
//~ MONO_ITEM fn std::ptr::drop_in_place::<EnumWithDrop> - shim(Some(EnumWithDrop))
21+
enum EnumWithDrop {
22+
A(i32),
23+
}
24+
25+
impl Drop for EnumWithDrop {
26+
//~ MONO_ITEM fn <EnumWithDrop as std::ops::Drop>::drop
27+
fn drop(&mut self) {}
28+
}
29+
30+
enum EnumNoDrop {
31+
A(i32),
32+
}
33+
34+
// We should be able to monomorphize drops for struct with lifetimes.
35+
impl<'a> Drop for StructWithDropAndLt<'a> {
36+
//~ MONO_ITEM fn <StructWithDropAndLt<'_> as std::ops::Drop>::drop
37+
fn drop(&mut self) {}
38+
}
39+
40+
//~ MONO_ITEM fn std::ptr::drop_in_place::<StructWithDropAndLt<'_>> - shim(Some(StructWithDropAndLt<'_>))
41+
struct StructWithDropAndLt<'a> {
42+
x: &'a i32,
43+
}

0 commit comments

Comments
 (0)