about summary refs log tree commit diff
diff options
context:
space:
mode:
authorChristopher Durham <cad97@cad97.com>2024-09-14 01:34:05 -0400
committerGitHub <noreply@github.com>2024-09-14 01:34:05 -0400
commitde66d3aa2b4506795e9dec9503168e4d13c4f7b3 (patch)
treef0b3f683765815bd545fedb6adf26d3a912f6e75
parent23b04c0513472f3728ad482398008e077979e5c4 (diff)
downloadrust-de66d3aa2b4506795e9dec9503168e4d13c4f7b3.tar.gz
rust-de66d3aa2b4506795e9dec9503168e4d13c4f7b3.zip
add core::panic::abort_unwind
-rw-r--r--library/core/src/panic.rs30
1 files changed, 30 insertions, 0 deletions
diff --git a/library/core/src/panic.rs b/library/core/src/panic.rs
index 6c5236ed99c..3919bbd7958 100644
--- a/library/core/src/panic.rs
+++ b/library/core/src/panic.rs
@@ -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.