Skip to content

Commit 1308d99

Browse files
authored
Rollup merge of #50312 - Pazzaz:master, r=GuillaumeGomez
Add more links in panic docs Fixes #48695 by adding a link to `AssertUnwindSafe`. Also added some other links in the module's docs to make things clearer.
2 parents b88c152 + 368fe37 commit 1308d99

File tree

2 files changed

+40
-19
lines changed

2 files changed

+40
-19
lines changed

src/libstd/panic.rs

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,14 @@ pub use core::panic::{PanicInfo, Location};
3131
/// A marker trait which represents "panic safe" types in Rust.
3232
///
3333
/// This trait is implemented by default for many types and behaves similarly in
34-
/// terms of inference of implementation to the `Send` and `Sync` traits. The
35-
/// purpose of this trait is to encode what types are safe to cross a `catch_unwind`
34+
/// terms of inference of implementation to the [`Send`] and [`Sync`] traits. The
35+
/// purpose of this trait is to encode what types are safe to cross a [`catch_unwind`]
3636
/// boundary with no fear of unwind safety.
3737
///
38+
/// [`Send`]: ../marker/trait.Send.html
39+
/// [`Sync`]: ../marker/trait.Sync.html
40+
/// [`catch_unwind`]: ./fn.catch_unwind.html
41+
///
3842
/// ## What is unwind safety?
3943
///
4044
/// In Rust a function can "return" early if it either panics or calls a
@@ -95,12 +99,13 @@ pub use core::panic::{PanicInfo, Location};
9599
///
96100
/// ## When should `UnwindSafe` be used?
97101
///
98-
/// Is not intended that most types or functions need to worry about this trait.
99-
/// It is only used as a bound on the `catch_unwind` function and as mentioned above,
100-
/// the lack of `unsafe` means it is mostly an advisory. The `AssertUnwindSafe`
101-
/// wrapper struct in this module can be used to force this trait to be
102-
/// implemented for any closed over variables passed to the `catch_unwind` function
103-
/// (more on this below).
102+
/// It is not intended that most types or functions need to worry about this trait.
103+
/// It is only used as a bound on the `catch_unwind` function and as mentioned
104+
/// above, the lack of `unsafe` means it is mostly an advisory. The
105+
/// [`AssertUnwindSafe`] wrapper struct can be used to force this trait to be
106+
/// implemented for any closed over variables passed to `catch_unwind`.
107+
///
108+
/// [`AssertUnwindSafe`]: ./struct.AssertUnwindSafe.html
104109
#[stable(feature = "catch_unwind", since = "1.9.0")]
105110
#[rustc_on_unimplemented = "the type {Self} may not be safely transferred \
106111
across an unwind boundary"]
@@ -109,11 +114,14 @@ pub auto trait UnwindSafe {}
109114
/// A marker trait representing types where a shared reference is considered
110115
/// unwind safe.
111116
///
112-
/// This trait is namely not implemented by `UnsafeCell`, the root of all
117+
/// This trait is namely not implemented by [`UnsafeCell`], the root of all
113118
/// interior mutability.
114119
///
115120
/// This is a "helper marker trait" used to provide impl blocks for the
116-
/// `UnwindSafe` trait, for more information see that documentation.
121+
/// [`UnwindSafe`] trait, for more information see that documentation.
122+
///
123+
/// [`UnsafeCell`]: ../cell/struct.UnsafeCell.html
124+
/// [`UnwindSafe`]: ./trait.UnwindSafe.html
117125
#[stable(feature = "catch_unwind", since = "1.9.0")]
118126
#[rustc_on_unimplemented = "the type {Self} may contain interior mutability \
119127
and a reference may not be safely transferrable \
@@ -122,14 +130,15 @@ pub auto trait RefUnwindSafe {}
122130

123131
/// A simple wrapper around a type to assert that it is unwind safe.
124132
///
125-
/// When using `catch_unwind` it may be the case that some of the closed over
133+
/// When using [`catch_unwind`] it may be the case that some of the closed over
126134
/// variables are not unwind safe. For example if `&mut T` is captured the
127135
/// compiler will generate a warning indicating that it is not unwind safe. It
128136
/// may not be the case, however, that this is actually a problem due to the
129-
/// specific usage of `catch_unwind` if unwind safety is specifically taken into
137+
/// specific usage of [`catch_unwind`] if unwind safety is specifically taken into
130138
/// account. This wrapper struct is useful for a quick and lightweight
131139
/// annotation that a variable is indeed unwind safe.
132140
///
141+
/// [`catch_unwind`]: ./fn.catch_unwind.html
133142
/// # Examples
134143
///
135144
/// One way to use `AssertUnwindSafe` is to assert that the entire closure
@@ -318,18 +327,22 @@ impl<T: fmt::Debug> fmt::Debug for AssertUnwindSafe<T> {
318327
/// panic and allowing a graceful handling of the error.
319328
///
320329
/// It is **not** recommended to use this function for a general try/catch
321-
/// mechanism. The `Result` type is more appropriate to use for functions that
330+
/// mechanism. The [`Result`] type is more appropriate to use for functions that
322331
/// can fail on a regular basis. Additionally, this function is not guaranteed
323332
/// to catch all panics, see the "Notes" section below.
324333
///
325-
/// The closure provided is required to adhere to the `UnwindSafe` trait to ensure
334+
/// [`Result`]: ../result/enum.Result.html
335+
///
336+
/// The closure provided is required to adhere to the [`UnwindSafe`] trait to ensure
326337
/// that all captured variables are safe to cross this boundary. The purpose of
327338
/// this bound is to encode the concept of [exception safety][rfc] in the type
328339
/// system. Most usage of this function should not need to worry about this
329340
/// bound as programs are naturally unwind safe without `unsafe` code. If it
330-
/// becomes a problem the associated `AssertUnwindSafe` wrapper type in this
331-
/// module can be used to quickly assert that the usage here is indeed unwind
332-
/// safe.
341+
/// becomes a problem the [`AssertUnwindSafe`] wrapper struct can be used to quickly
342+
/// assert that the usage here is indeed unwind safe.
343+
///
344+
/// [`AssertUnwindSafe`]: ./struct.AssertUnwindSafe.html
345+
/// [`UnwindSafe`]: ./trait.UnwindSafe.html
333346
///
334347
/// [rfc]: https://github.com/rust-lang/rfcs/blob/master/text/1236-stabilize-catch-panic.md
335348
///
@@ -364,9 +377,11 @@ pub fn catch_unwind<F: FnOnce() -> R + UnwindSafe, R>(f: F) -> Result<R> {
364377

365378
/// Triggers a panic without invoking the panic hook.
366379
///
367-
/// This is designed to be used in conjunction with `catch_unwind` to, for
380+
/// This is designed to be used in conjunction with [`catch_unwind`] to, for
368381
/// example, carry a panic across a layer of C code.
369382
///
383+
/// [`catch_unwind`]: ./fn.catch_unwind.html
384+
///
370385
/// # Notes
371386
///
372387
/// Note that panics in Rust are not always implemented via unwinding, but they

src/libstd/panicking.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,9 @@ static mut HOOK: Hook = Hook::Default;
7676
/// is invoked. As such, the hook will run with both the aborting and unwinding
7777
/// runtimes. The default hook prints a message to standard error and generates
7878
/// a backtrace if requested, but this behavior can be customized with the
79-
/// `set_hook` and `take_hook` functions.
79+
/// `set_hook` and [`take_hook`] functions.
80+
///
81+
/// [`take_hook`]: ./fn.take_hook.html
8082
///
8183
/// The hook is provided with a `PanicInfo` struct which contains information
8284
/// about the origin of the panic, including the payload passed to `panic!` and
@@ -121,6 +123,10 @@ pub fn set_hook(hook: Box<Fn(&PanicInfo) + 'static + Sync + Send>) {
121123

122124
/// Unregisters the current panic hook, returning it.
123125
///
126+
/// *See also the function [`set_hook`].*
127+
///
128+
/// [`set_hook`]: ./fn.set_hook.html
129+
///
124130
/// If no custom hook is registered, the default hook will be returned.
125131
///
126132
/// # Panics

0 commit comments

Comments
 (0)