Skip to content

Commit 8c7a05a

Browse files
committed
Treat drop_in_place as nounwind with -Z panic-in-drop=abort
The AbortUnwindCalls MIR pass will eliminate any unnecessary cleanups and will prevent any unwinds from leaking out by forcing an abort.
1 parent c1bcf5c commit 8c7a05a

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

compiler/rustc_mir_transform/src/abort_unwinding_calls.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use rustc_middle::mir::*;
55
use rustc_middle::ty::layout;
66
use rustc_middle::ty::{self, TyCtxt};
77
use rustc_target::spec::abi::Abi;
8+
use rustc_target::spec::PanicStrategy;
89

910
/// A pass that runs which is targeted at ensuring that codegen guarantees about
1011
/// unwinding are upheld for compilations of panic=abort programs.
@@ -82,10 +83,11 @@ impl<'tcx> MirPass<'tcx> for AbortUnwindingCalls {
8283
};
8384
layout::fn_can_unwind(tcx, flags, sig.abi())
8485
}
85-
TerminatorKind::Drop { .. }
86-
| TerminatorKind::DropAndReplace { .. }
87-
| TerminatorKind::Assert { .. }
88-
| TerminatorKind::FalseUnwind { .. } => {
86+
TerminatorKind::Drop { .. } | TerminatorKind::DropAndReplace { .. } => {
87+
tcx.sess.opts.debugging_opts.panic_in_drop == PanicStrategy::Unwind
88+
&& layout::fn_can_unwind(tcx, CodegenFnAttrFlags::empty(), Abi::Rust)
89+
}
90+
TerminatorKind::Assert { .. } | TerminatorKind::FalseUnwind { .. } => {
8991
layout::fn_can_unwind(tcx, CodegenFnAttrFlags::empty(), Abi::Rust)
9092
}
9193
_ => continue,

compiler/rustc_typeck/src/collect.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ use rustc_session::lint;
4646
use rustc_session::parse::feature_err;
4747
use rustc_span::symbol::{kw, sym, Ident, Symbol};
4848
use rustc_span::{Span, DUMMY_SP};
49-
use rustc_target::spec::{abi, SanitizerSet};
49+
use rustc_target::spec::{abi, PanicStrategy, SanitizerSet};
5050
use rustc_trait_selection::traits::error_reporting::suggestions::NextTypeParamName;
5151
use std::iter;
5252

@@ -2683,6 +2683,13 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, id: DefId) -> CodegenFnAttrs {
26832683
codegen_fn_attrs.flags |= CodegenFnAttrFlags::TRACK_CALLER;
26842684
}
26852685

2686+
// With -Z panic-in-drop=abort, drop_in_place never unwinds.
2687+
if tcx.sess.opts.debugging_opts.panic_in_drop == PanicStrategy::Abort {
2688+
if Some(id) == tcx.lang_items().drop_in_place_fn() {
2689+
codegen_fn_attrs.flags |= CodegenFnAttrFlags::NEVER_UNWIND;
2690+
}
2691+
}
2692+
26862693
let supported_target_features = tcx.supported_target_features(LOCAL_CRATE);
26872694

26882695
let mut inline_span = None;

0 commit comments

Comments
 (0)