Rollup merge of #130339 - CAD97:unwind-choice, r=dtolnay · qinheping/verify-rust-std@ee67105 (original) (raw)

Original file line number Diff line number Diff line change
@@ -140,6 +140,31 @@ pub macro unreachable_2021 {
140 140 ),
141 141 }
142 142
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 +
143 168 /// An internal trait used by std to pass data from std to `panic_unwind` and
144 169 /// other panic runtimes. Not intended to be stabilized any time soon, do not
145 170 /// use.