Skip to content

Commit 66282cb

Browse files
committed
add panic_fmt_nounwind for panicing without unwinding, and use it for panic_no_unwind
1 parent 2b50cd1 commit 66282cb

File tree

2 files changed

+19
-10
lines changed

2 files changed

+19
-10
lines changed

compiler/rustc_hir_analysis/src/collect.rs

-7
Original file line numberDiff line numberDiff line change
@@ -1582,13 +1582,6 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: DefId) -> CodegenFnAttrs {
15821582
codegen_fn_attrs.flags |= CodegenFnAttrFlags::TRACK_CALLER;
15831583
}
15841584

1585-
// The panic_no_unwind function called by TerminatorKind::Abort will never
1586-
// unwind. If the panic handler that it invokes unwind then it will simply
1587-
// call the panic handler again.
1588-
if Some(did.to_def_id()) == tcx.lang_items().panic_no_unwind() {
1589-
codegen_fn_attrs.flags |= CodegenFnAttrFlags::NEVER_UNWIND;
1590-
}
1591-
15921585
let supported_target_features = tcx.supported_target_features(LOCAL_CRATE);
15931586

15941587
let mut inline_span = None;

library/core/src/panicking.rs

+19-3
Original file line numberDiff line numberDiff line change
@@ -84,12 +84,27 @@ fn panic_bounds_check(index: usize, len: usize) -> ! {
8484
panic!("index out of bounds: the len is {len} but the index is {index}")
8585
}
8686

87-
// This function is called directly by the codegen backend, and must not have
88-
// any extra arguments (including those synthesized by track_caller).
87+
/// Panic because we cannot unwind out of a function.
88+
///
89+
/// This function is called directly by the codegen backend, and must not have
90+
/// any extra arguments (including those synthesized by track_caller).
8991
#[cold]
9092
#[inline(never)]
9193
#[lang = "panic_no_unwind"] // needed by codegen for panic in nounwind function
94+
#[cfg_attr(not(bootstrap), rustc_nounwind)]
95+
#[cfg_attr(bootstrap, rustc_allocator_nounwind)]
9296
fn panic_no_unwind() -> ! {
97+
panic_str_nounwind("panic in a function that cannot unwind")
98+
}
99+
100+
/// Like panic_fmt, but without unwinding and track_caller to reduce the impact on codesize.
101+
/// Also just works on `str`, as a `fmt::Arguments` needs more space to be passed.
102+
#[cold]
103+
#[cfg_attr(not(feature = "panic_immediate_abort"), inline(never))]
104+
#[cfg_attr(feature = "panic_immediate_abort", inline)]
105+
#[cfg_attr(not(bootstrap), rustc_nounwind)]
106+
#[cfg_attr(bootstrap, rustc_allocator_nounwind)]
107+
pub fn panic_str_nounwind(msg: &'static str) -> ! {
93108
if cfg!(feature = "panic_immediate_abort") {
94109
super::intrinsics::abort()
95110
}
@@ -102,7 +117,8 @@ fn panic_no_unwind() -> ! {
102117
}
103118

104119
// PanicInfo with the `can_unwind` flag set to false forces an abort.
105-
let fmt = format_args!("panic in a function that cannot unwind");
120+
let pieces = [msg];
121+
let fmt = fmt::Arguments::new_v1(&pieces, &[]);
106122
let pi = PanicInfo::internal_constructor(Some(&fmt), Location::caller(), false);
107123

108124
// SAFETY: `panic_impl` is defined in safe Rust code and thus is safe to call.

0 commit comments

Comments
 (0)