Skip to content

Commit 4695c21

Browse files
authored
Rollup merge of #93489 - Amanieu:panic_no_unwind, r=nagisa
Mark the panic_no_unwind lang item as nounwind This has 2 effects: - It helps LLVM when inlining since it doesn't need to generate landing pads for `panic_no_unwind`. - It makes it sound for a panic handler to unwind even if `PanicInfo::can_unwind` returns true. This will simply cause another panic once the unwind tries to go past the `panic_no_unwind` lang item. Eventually this will cause a stack overflow, which is safe.
2 parents 9f4559c + f738669 commit 4695c21

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

compiler/rustc_typeck/src/collect.rs

+7
Original file line numberDiff line numberDiff line change
@@ -2778,6 +2778,13 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, id: DefId) -> CodegenFnAttrs {
27782778
}
27792779
}
27802780

2781+
// The panic_no_unwind function called by TerminatorKind::Abort will never
2782+
// unwind. If the panic handler that it invokes unwind then it will simply
2783+
// call the panic handler again.
2784+
if Some(id) == tcx.lang_items().panic_no_unwind() {
2785+
codegen_fn_attrs.flags |= CodegenFnAttrFlags::NEVER_UNWIND;
2786+
}
2787+
27812788
let supported_target_features = tcx.supported_target_features(LOCAL_CRATE);
27822789

27832790
let mut inline_span = None;

library/core/src/panic/panic_info.rs

+4
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,10 @@ impl<'a> PanicInfo<'a> {
136136
/// This is true for most kinds of panics with the exception of panics
137137
/// caused by trying to unwind out of a `Drop` implementation or a function
138138
/// whose ABI does not support unwinding.
139+
///
140+
/// It is safe for a panic handler to unwind even when this function returns
141+
/// true, however this will simply cause the panic handler to be called
142+
/// again.
139143
#[must_use]
140144
#[unstable(feature = "panic_can_unwind", issue = "92988")]
141145
pub fn can_unwind(&self) -> bool {

0 commit comments

Comments
 (0)