Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Make the abort display a nicer message
Mark `panic_abort` as `no-unwind`.

Co-Authored-By: Christopher Durham <cad97@cad97.com>
Co-Authored-By: Gary Guo <gary@garyguo.net>
  • Loading branch information
3 people committed Sep 26, 2022
commit 6d7ee4b4b2ac00742c291668be97dcc9a3fd1b1c
4 changes: 3 additions & 1 deletion library/core/src/any.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,9 @@ impl<T> Drop for DropNoUnwindSameAnyTypeId<T> {

impl Drop for AbortOnDrop {
fn drop(&mut self) {
crate::intrinsics::abort();
crate::panicking::panic_abort(Some(&format_args!(
"fatal runtime error: drop of the panic payload panicked"
)))
}
}

Expand Down
24 changes: 24 additions & 0 deletions library/core/src/panicking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ fn panic_bounds_check(index: usize, len: usize) -> ! {
#[inline(never)]
#[lang = "panic_no_unwind"] // needed by codegen for panic in nounwind function
fn panic_no_unwind() -> ! {
// Could this be written in terms of:
// `panic_abort(Some(&format_args!("panic in a function that cannot unwind")))`?
if cfg!(feature = "panic_immediate_abort") {
super::intrinsics::abort()
}
Expand All @@ -109,6 +111,28 @@ fn panic_no_unwind() -> ! {
unsafe { panic_impl(&pi) }
}

/// Aborts the process, but with a properly displayed panic message.
#[cold]
#[rustc_allocator_nounwind]
pub(crate) fn panic_abort<'a>(message: Option<&'a fmt::Arguments<'a>>) -> ! {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
pub(crate) fn panic_abort<'a>(message: Option<&'a fmt::Arguments<'a>>) -> ! {
pub(crate) fn panic_abort<'a>(message: fmt::Arguments<'a>) -> ! {

(and change others accordingly)

PanicInfo's optional message is only for panic_any; for something new like nounwind panic, there's no reason to use Option here. It should just follow the signature of panic_fmt below.

if cfg!(feature = "panic_immediate_abort") {
super::intrinsics::abort()
}

// NOTE This function never crosses the FFI boundary; it's a Rust-to-Rust call
// that gets resolved to the `#[panic_handler]` function.
extern "Rust" {
#[lang = "panic_impl"]
fn panic_impl(pi: &PanicInfo<'_>) -> !;
}

// PanicInfo with the `can_unwind` flag set to false forces an abort.
let pi = PanicInfo::internal_constructor(message, Location::caller(), false);

// SAFETY: `panic_impl` is defined in safe Rust code and thus is safe to call.
unsafe { panic_impl(&pi) }
}

/// The entry point for panicking with a formatted message.
///
/// This is designed to reduce the amount of code required at the call
Expand Down