Skip to content

Commit 7021e59

Browse files
committed
Include drops in mir_inliner_callees
1 parent 7d0ba2d commit 7021e59

File tree

4 files changed

+27
-13
lines changed

4 files changed

+27
-13
lines changed

compiler/rustc_mir_transform/src/inline/cycle.rs

+15-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexSet};
22
use rustc_data_structures::stack::ensure_sufficient_stack;
33
use rustc_hir::def_id::{DefId, LocalDefId};
4+
use rustc_hir::LangItem;
45
use rustc_middle::mir::TerminatorKind;
5-
use rustc_middle::ty::{self, GenericArgsRef, InstanceKind, TyCtxt, TypeVisitableExt};
6+
use rustc_middle::ty::{
7+
self, GenericArg, GenericArgsRef, InstanceKind, ParamEnv, TyCtxt, TypeVisitableExt,
8+
};
69
use rustc_session::Limit;
710
use rustc_span::sym;
811
use tracing::{instrument, trace};
@@ -191,6 +194,17 @@ pub(crate) fn mir_inliner_callees<'tcx>(
191194
};
192195
calls.insert(call);
193196
}
197+
// This query is called on *analysis* MIR, which doesn't have drops elaborated,
198+
// let alone any of the later runtime MIR optimizations.
199+
if let TerminatorKind::Drop { place, .. } = &terminator.kind {
200+
let dropped_ty = place.ty(&body.local_decls, tcx).ty;
201+
if dropped_ty.needs_drop(tcx, ParamEnv::reveal_all()) {
202+
let drop_in_place_fn =
203+
tcx.require_lang_item(LangItem::DropInPlace, Some(terminator.source_info.span));
204+
let args = tcx.mk_args(&[GenericArg::from(dropped_ty)]);
205+
calls.insert((drop_in_place_fn, args));
206+
}
207+
}
194208
}
195209
tcx.arena.alloc_from_iter(calls.iter().copied())
196210
}

tests/mir-opt/inline/inline_diverging.g.Inline.panic-abort.diff

+4-4
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
let mut _4: i32;
1010
let mut _5: !;
1111
let _6: !;
12-
+ scope 1 (inlined panic) {
13-
+ let mut _7: !;
12+
+ scope 1 (inlined calls_panic) {
13+
+ let _7: !;
1414
+ }
1515

1616
bb0: {
@@ -34,9 +34,9 @@
3434
bb2: {
3535
StorageDead(_3);
3636
StorageLive(_6);
37-
- _6 = panic() -> unwind unreachable;
37+
- _6 = calls_panic() -> unwind unreachable;
3838
+ StorageLive(_7);
39-
+ _7 = begin_panic::<&str>(const "explicit panic") -> unwind unreachable;
39+
+ _7 = panic_cold_explicit() -> unwind unreachable;
4040
}
4141
}
4242

tests/mir-opt/inline/inline_diverging.g.Inline.panic-unwind.diff

+4-4
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
let mut _4: i32;
1010
let mut _5: !;
1111
let _6: !;
12-
+ scope 1 (inlined panic) {
13-
+ let mut _7: !;
12+
+ scope 1 (inlined calls_panic) {
13+
+ let _7: !;
1414
+ }
1515

1616
bb0: {
@@ -34,9 +34,9 @@
3434
bb2: {
3535
StorageDead(_3);
3636
StorageLive(_6);
37-
- _6 = panic() -> unwind continue;
37+
- _6 = calls_panic() -> unwind continue;
3838
+ StorageLive(_7);
39-
+ _7 = begin_panic::<&str>(const "explicit panic") -> unwind continue;
39+
+ _7 = panic_cold_explicit() -> unwind continue;
4040
}
4141
}
4242

tests/mir-opt/inline/inline_diverging.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Tests inlining of diverging calls.
22
//
33
// EMIT_MIR_FOR_EACH_PANIC_STRATEGY
4-
//@ compile-flags: -Zinline-mir-hint-threshold=1000 -C debuginfo=full
4+
//@ compile-flags: -Zinline-mir-hint-threshold=1000 -C debuginfo=full --edition=2021
55
#![crate_type = "lib"]
66

77
// EMIT_MIR inline_diverging.f.Inline.diff
@@ -17,8 +17,8 @@ pub fn g(i: i32) -> u32 {
1717
i as u32
1818
} else {
1919
// CHECK-LABEL: fn g(
20-
// CHECK: (inlined panic)
21-
panic();
20+
// CHECK: (inlined calls_panic)
21+
calls_panic();
2222
}
2323
}
2424

@@ -38,7 +38,7 @@ pub fn call_twice<R, F: Fn() -> R>(f: F) -> (R, R) {
3838
}
3939

4040
#[inline(always)]
41-
fn panic() -> ! {
41+
fn calls_panic() -> ! {
4242
panic!();
4343
}
4444

0 commit comments

Comments
 (0)