Skip to content

Commit 6d7ee4b

Browse files
danielhenrymantillaCAD97nbdd0121
committed
Make the abort display a nicer message
Mark `panic_abort` as `no-unwind`. Co-Authored-By: Christopher Durham <[email protected]> Co-Authored-By: Gary Guo <[email protected]>
1 parent 045d7cb commit 6d7ee4b

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

library/core/src/any.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,9 @@ impl<T> Drop for DropNoUnwindSameAnyTypeId<T> {
245245

246246
impl Drop for AbortOnDrop {
247247
fn drop(&mut self) {
248-
crate::intrinsics::abort();
248+
crate::panicking::panic_abort(Some(&format_args!(
249+
"fatal runtime error: drop of the panic payload panicked"
250+
)))
249251
}
250252
}
251253

library/core/src/panicking.rs

+24
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ fn panic_bounds_check(index: usize, len: usize) -> ! {
9090
#[inline(never)]
9191
#[lang = "panic_no_unwind"] // needed by codegen for panic in nounwind function
9292
fn panic_no_unwind() -> ! {
93+
// Could this be written in terms of:
94+
// `panic_abort(Some(&format_args!("panic in a function that cannot unwind")))`?
9395
if cfg!(feature = "panic_immediate_abort") {
9496
super::intrinsics::abort()
9597
}
@@ -109,6 +111,28 @@ fn panic_no_unwind() -> ! {
109111
unsafe { panic_impl(&pi) }
110112
}
111113

114+
/// Aborts the process, but with a properly displayed panic message.
115+
#[cold]
116+
#[rustc_allocator_nounwind]
117+
pub(crate) fn panic_abort<'a>(message: Option<&'a fmt::Arguments<'a>>) -> ! {
118+
if cfg!(feature = "panic_immediate_abort") {
119+
super::intrinsics::abort()
120+
}
121+
122+
// NOTE This function never crosses the FFI boundary; it's a Rust-to-Rust call
123+
// that gets resolved to the `#[panic_handler]` function.
124+
extern "Rust" {
125+
#[lang = "panic_impl"]
126+
fn panic_impl(pi: &PanicInfo<'_>) -> !;
127+
}
128+
129+
// PanicInfo with the `can_unwind` flag set to false forces an abort.
130+
let pi = PanicInfo::internal_constructor(message, Location::caller(), false);
131+
132+
// SAFETY: `panic_impl` is defined in safe Rust code and thus is safe to call.
133+
unsafe { panic_impl(&pi) }
134+
}
135+
112136
/// The entry point for panicking with a formatted message.
113137
///
114138
/// This is designed to reduce the amount of code required at the call

0 commit comments

Comments
 (0)