Skip to content

Commit

Permalink
add core::panic::abort_unwind
Browse files Browse the repository at this point in the history
  • Loading branch information
CAD97 authored Sep 14, 2024
1 parent 34e22e8 commit 91ebe06
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions core/src/panic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,36 @@ pub macro unreachable_2021 {
),
}

/// Invokes a closure, aborting if the closure unwinds.
///
/// When compiled with aborting panics, this function is effectively a no-op.
/// With unwinding panics, an unwind results in another call into the panic
/// hook followed by a process abort.
///
/// # Notes
///
/// Instead of using this function, code should attempt to support unwinding.
/// Implementing [`Drop`] allows you to restore invariants uniformly in both
/// return and unwind paths.
///
/// If an unwind can lead to logical issues but not soundness issues, you
/// should allow the unwind. Opting out of [`UnwindSafe`] indicates to your
/// consumers that they need to consider correctness in the face of unwinds.
///
/// If an unwind would be unsound, then this function should be used in order
/// to prevent unwinds. However, note that `extern "C" fn` will automatically
/// convert unwinds to aborts, so using this function isn't necessary for FFI.
#[unstable(feature = "abort_unwind", issue = "130338")]
pub fn abort_unwind<F: FnOnce() -> R, R>(f: F) -> R {
// This attribute adds the "unwinding out of nounwind function" guard.
#[rustc_nounwind]
fn abort_unwind_inner<F: FnOnce() -> R, R>(f: F) -> R {
f()
}

abort_unwind_inner(f)
}

/// An internal trait used by std to pass data from std to `panic_unwind` and
/// other panic runtimes. Not intended to be stabilized any time soon, do not
/// use.
Expand Down

0 comments on commit 91ebe06

Please sign in to comment.