Skip to content

Commit ee67105

Browse files
authored
Rollup merge of rust-lang#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: rust-lang#130338 - ACP: rust-lang/libs-team#441
2 parents b66efdd + f396849 commit ee67105

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

core/src/panic.rs

+25
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,31 @@ pub macro unreachable_2021 {
140140
),
141141
}
142142

143+
/// Invokes a closure, aborting if the closure unwinds.
144+
///
145+
/// When compiled with aborting panics, this function is effectively a no-op.
146+
/// With unwinding panics, an unwind results in another call into the panic
147+
/// hook followed by a process abort.
148+
///
149+
/// # Notes
150+
///
151+
/// Instead of using this function, code should attempt to support unwinding.
152+
/// Implementing [`Drop`] allows you to restore invariants uniformly in both
153+
/// return and unwind paths.
154+
///
155+
/// If an unwind can lead to logical issues but not soundness issues, you
156+
/// should allow the unwind. Opting out of [`UnwindSafe`] indicates to your
157+
/// consumers that they need to consider correctness in the face of unwinds.
158+
///
159+
/// If an unwind would be unsound, then this function should be used in order
160+
/// to prevent unwinds. However, note that `extern "C" fn` will automatically
161+
/// convert unwinds to aborts, so using this function isn't necessary for FFI.
162+
#[unstable(feature = "abort_unwind", issue = "130338")]
163+
#[rustc_nounwind]
164+
pub fn abort_unwind<F: FnOnce() -> R, R>(f: F) -> R {
165+
f()
166+
}
167+
143168
/// An internal trait used by std to pass data from std to `panic_unwind` and
144169
/// other panic runtimes. Not intended to be stabilized any time soon, do not
145170
/// use.

std/src/panic.rs

+3
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,9 @@ where
283283
{
284284
}
285285

286+
#[unstable(feature = "abort_unwind", issue = "130338")]
287+
pub use core::panic::abort_unwind;
288+
286289
/// Invokes a closure, capturing the cause of an unwinding panic if one occurs.
287290
///
288291
/// This function will return `Ok` with the closure's result if the closure

0 commit comments

Comments
 (0)