@@ -288,45 +288,55 @@ pub use core::panic::abort_unwind;
288288
289289/// Invokes a closure, capturing the cause of an unwinding panic if one occurs.
290290///
291- /// This function will return `Ok` with the closure's result if the closure
292- /// does not panic, and will return `Err(cause)` if the closure panics. The
293- /// `cause` returned is the object with which panic was originally invoked.
291+ /// This function will return `Ok` with the closure's result if the closure does
292+ /// not panic, and will return `Err(cause)` if the closure panics. The `cause`
293+ /// returned is the object with which panic was originally invoked.
294294///
295- /// It is currently undefined behavior to unwind from Rust code into foreign
296- /// code, so this function is particularly useful when Rust is called from
297- /// another language (normally C). This can run arbitrary Rust code, capturing a
298- /// panic and allowing a graceful handling of the error.
295+ /// Rust functions that are expected to be called from foreign code that does
296+ /// not support unwinding (such as C compiled with `-fno-exceptions`) should be
297+ /// defined using `extern "C"`, which ensures that if the Rust code panics, it
298+ /// is automatically caught and the process is aborted. If this is the desired
299+ /// behavior, it is not necessary to use `catch_unwind` explicitly. This
300+ /// function should instead be used when more graceful error-handling is needed.
299301///
300302/// It is **not** recommended to use this function for a general try/catch
301303/// mechanism. The [`Result`] type is more appropriate to use for functions that
302304/// can fail on a regular basis. Additionally, this function is not guaranteed
303305/// to catch all panics, see the "Notes" section below.
304306///
305- /// The closure provided is required to adhere to the [`UnwindSafe`] trait to ensure
306- /// that all captured variables are safe to cross this boundary. The purpose of
307- /// this bound is to encode the concept of [exception safety][rfc] in the type
308- /// system. Most usage of this function should not need to worry about this
309- /// bound as programs are naturally unwind safe without `unsafe` code. If it
310- /// becomes a problem the [`AssertUnwindSafe`] wrapper struct can be used to quickly
311- /// assert that the usage here is indeed unwind safe.
307+ /// The closure provided is required to adhere to the [`UnwindSafe`] trait to
308+ /// ensure that all captured variables are safe to cross this boundary. The
309+ /// purpose of this bound is to encode the concept of [exception safety][rfc] in
310+ /// the type system. Most usage of this function should not need to worry about
311+ /// this bound as programs are naturally unwind safe without `unsafe` code. If
312+ /// it becomes a problem the [`AssertUnwindSafe`] wrapper struct can be used to
313+ /// quickly assert that the usage here is indeed unwind safe.
312314///
313315/// [rfc]: https://github.com/rust-lang/rfcs/blob/master/text/1236-stabilize-catch-panic.md
314316///
315317/// # Notes
316318///
317- /// Note that this function **might not catch all panics** in Rust . A panic in
318- /// Rust is not always implemented via unwinding, but can be implemented by
319- /// aborting the process as well. This function *only* catches unwinding panics,
320- /// not those that abort the process.
319+ /// This function **might not catch all Rust panics**. A Rust panic is not
320+ /// always implemented via unwinding, but can be implemented by aborting the
321+ /// process as well. This function *only* catches unwinding panics, not those
322+ /// that abort the process.
321323///
322- /// Note that if a custom panic hook has been set, it will be invoked before
323- /// the panic is caught, before unwinding.
324+ /// If a custom panic hook has been set, it will be invoked before the panic is
325+ /// caught, before unwinding.
324326///
325- /// Also note that unwinding into Rust code with a foreign exception (e.g.
326- /// an exception thrown from C++ code) is undefined behavior.
327+ /// Although unwinding into Rust code with a foreign exception (e.g. an
328+ /// exception thrown from C++ code, or a `panic!` in Rust code compiled or
329+ /// linked with a different runtime) via an appropriate ABI (e.g. `"C-unwind"`)
330+ /// is permitted, catching such an exception using this function will have one
331+ /// of two behaviors, and it is unspecified which will occur:
327332///
328- /// Finally, be **careful in how you drop the result of this function**.
329- /// If it is `Err`, it contains the panic payload, and dropping that may in turn panic!
333+ /// * The process aborts, after executing all destructors of `f` and the
334+ /// functions it called.
335+ /// * The function returns a `Result::Err` containing an opaque type.
336+ ///
337+ /// Finally, be **careful in how you drop the result of this function**. If it
338+ /// is `Err`, it contains the panic payload, and dropping that may in turn
339+ /// panic!
330340///
331341/// # Examples
332342///
0 commit comments