about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMatthias Krüger <matthias.krueger@famsik.de>2024-09-15 20:55:13 +0200
committerGitHub <noreply@github.com>2024-09-15 20:55:13 +0200
commitf0fb411969c4c2e6fdc20a813956cc370e914b40 (patch)
tree735fb3c91b2d2e306c24a7c0d0465661d1e2be4f
parent0daa636b93540579573f1b92d05536b20da2252a (diff)
parent42a44a04ee1b2a99ea78e942937b4ce3d8a3067e (diff)
downloadrust-f0fb411969c4c2e6fdc20a813956cc370e914b40.tar.gz
rust-f0fb411969c4c2e6fdc20a813956cc370e914b40.zip
Rollup merge of #130339 - CAD97:unwind-choice, r=dtolnay
Add `core::panic::abort_unwind`

`abort_unwind` is like `catch_unwind` except that it aborts the process if it unwinds, using the `#[rustc_nounwind]` mechanism also used by `extern "C" fn` to abort unwinding. The docs attempt to make it clear when to (rarely) and when not to (usually) use the function.

Although usage of the function is discouraged, having it available will help to normalize the experience when abort_unwind shims are hit, as opposed to the current ecosystem where there exist multiple common patterns for converting unwinding into a process abort.

For further information and justification, see the linked ACP.

- Tracking issue: https://github.com/rust-lang/rust/issues/130338
- ACP: https://github.com/rust-lang/libs-team/issues/441
-rw-r--r--library/core/src/panic.rs25
-rw-r--r--library/std/src/panic.rs3
2 files changed, 28 insertions, 0 deletions
diff --git a/library/core/src/panic.rs b/library/core/src/panic.rs
index 6c5236ed99c..c95a000561c 100644
--- a/library/core/src/panic.rs
+++ b/library/core/src/panic.rs
@@ -140,6 +140,31 @@ 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")]
+#[rustc_nounwind]
+pub fn abort_unwind<F: FnOnce() -> R, R>(f: F) -> R {
+    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.
diff --git a/library/std/src/panic.rs b/library/std/src/panic.rs
index 6f0952c41ed..541cf42ab47 100644
--- a/library/std/src/panic.rs
+++ b/library/std/src/panic.rs
@@ -283,6 +283,9 @@ where
 {
 }
 
+#[unstable(feature = "abort_unwind", issue = "130338")]
+pub use core::panic::abort_unwind;
+
 /// Invokes a closure, capturing the cause of an unwinding panic if one occurs.
 ///
 /// This function will return `Ok` with the closure's result if the closure