std/
panic.rs

1//! Panic support in the standard library.
2
3#![stable(feature = "std_panic", since = "1.9.0")]
4
5use crate::any::Any;
6use crate::sync::atomic::{Atomic, AtomicU8, Ordering};
7use crate::sync::{Condvar, Mutex, RwLock};
8use crate::thread::Result;
9use crate::{collections, fmt, panicking};
10
11#[stable(feature = "panic_hooks", since = "1.10.0")]
12#[deprecated(
13    since = "1.82.0",
14    note = "use `PanicHookInfo` instead",
15    suggestion = "std::panic::PanicHookInfo"
16)]
17/// A struct providing information about a panic.
18///
19/// `PanicInfo` has been renamed to [`PanicHookInfo`] to avoid confusion with
20/// [`core::panic::PanicInfo`].
21pub type PanicInfo<'a> = PanicHookInfo<'a>;
22
23/// A struct providing information about a panic.
24///
25/// `PanicHookInfo` structure is passed to a panic hook set by the [`set_hook`] function.
26///
27/// # Examples
28///
29/// ```should_panic
30/// use std::panic;
31///
32/// panic::set_hook(Box::new(|panic_info| {
33///     println!("panic occurred: {panic_info}");
34/// }));
35///
36/// panic!("critical system failure");
37/// ```
38///
39/// [`set_hook`]: ../../std/panic/fn.set_hook.html
40#[stable(feature = "panic_hook_info", since = "1.81.0")]
41#[derive(Debug)]
42pub struct PanicHookInfo<'a> {
43    payload: &'a (dyn Any + Send),
44    location: &'a Location<'a>,
45    can_unwind: bool,
46    force_no_backtrace: bool,
47}
48
49impl<'a> PanicHookInfo<'a> {
50    #[inline]
51    pub(crate) fn new(
52        location: &'a Location<'a>,
53        payload: &'a (dyn Any + Send),
54        can_unwind: bool,
55        force_no_backtrace: bool,
56    ) -> Self {
57        PanicHookInfo { payload, location, can_unwind, force_no_backtrace }
58    }
59
60    /// Returns the payload associated with the panic.
61    ///
62    /// This will commonly, but not always, be a `&'static str` or [`String`].
63    ///
64    /// A invocation of the `panic!()` macro in Rust 2021 or later will always result in a
65    /// panic payload of type `&'static str` or `String`.
66    ///
67    /// Only an invocation of [`panic_any`]
68    /// (or, in Rust 2018 and earlier, `panic!(x)` where `x` is something other than a string)
69    /// can result in a panic payload other than a `&'static str` or `String`.
70    ///
71    /// [`String`]: ../../std/string/struct.String.html
72    ///
73    /// # Examples
74    ///
75    /// ```should_panic
76    /// use std::panic;
77    ///
78    /// panic::set_hook(Box::new(|panic_info| {
79    ///     if let Some(s) = panic_info.payload().downcast_ref::<&str>() {
80    ///         println!("panic occurred: {s:?}");
81    ///     } else if let Some(s) = panic_info.payload().downcast_ref::<String>() {
82    ///         println!("panic occurred: {s:?}");
83    ///     } else {
84    ///         println!("panic occurred");
85    ///     }
86    /// }));
87    ///
88    /// panic!("Normal panic");
89    /// ```
90    #[must_use]
91    #[inline]
92    #[stable(feature = "panic_hooks", since = "1.10.0")]
93    pub fn payload(&self) -> &(dyn Any + Send) {
94        self.payload
95    }
96
97    /// Returns the payload associated with the panic, if it is a string.
98    ///
99    /// This returns the payload if it is of type `&'static str` or `String`.
100    ///
101    /// A invocation of the `panic!()` macro in Rust 2021 or later will always result in a
102    /// panic payload where `payload_as_str` returns `Some`.
103    ///
104    /// Only an invocation of [`panic_any`]
105    /// (or, in Rust 2018 and earlier, `panic!(x)` where `x` is something other than a string)
106    /// can result in a panic payload where `payload_as_str` returns `None`.
107    ///
108    /// # Example
109    ///
110    /// ```should_panic
111    /// std::panic::set_hook(Box::new(|panic_info| {
112    ///     if let Some(s) = panic_info.payload_as_str() {
113    ///         println!("panic occurred: {s:?}");
114    ///     } else {
115    ///         println!("panic occurred");
116    ///     }
117    /// }));
118    ///
119    /// panic!("Normal panic");
120    /// ```
121    #[must_use]
122    #[inline]
123    #[stable(feature = "panic_payload_as_str", since = "CURRENT_RUSTC_VERSION")]
124    pub fn payload_as_str(&self) -> Option<&str> {
125        if let Some(s) = self.payload.downcast_ref::<&str>() {
126            Some(s)
127        } else if let Some(s) = self.payload.downcast_ref::<String>() {
128            Some(s)
129        } else {
130            None
131        }
132    }
133
134    /// Returns information about the location from which the panic originated,
135    /// if available.
136    ///
137    /// This method will currently always return [`Some`], but this may change
138    /// in future versions.
139    ///
140    /// # Examples
141    ///
142    /// ```should_panic
143    /// use std::panic;
144    ///
145    /// panic::set_hook(Box::new(|panic_info| {
146    ///     if let Some(location) = panic_info.location() {
147    ///         println!("panic occurred in file '{}' at line {}",
148    ///             location.file(),
149    ///             location.line(),
150    ///         );
151    ///     } else {
152    ///         println!("panic occurred but can't get location information...");
153    ///     }
154    /// }));
155    ///
156    /// panic!("Normal panic");
157    /// ```
158    #[must_use]
159    #[inline]
160    #[stable(feature = "panic_hooks", since = "1.10.0")]
161    pub fn location(&self) -> Option<&Location<'_>> {
162        // NOTE: If this is changed to sometimes return None,
163        // deal with that case in std::panicking::default_hook and core::panicking::panic_fmt.
164        Some(&self.location)
165    }
166
167    /// Returns whether the panic handler is allowed to unwind the stack from
168    /// the point where the panic occurred.
169    ///
170    /// This is true for most kinds of panics with the exception of panics
171    /// caused by trying to unwind out of a `Drop` implementation or a function
172    /// whose ABI does not support unwinding.
173    ///
174    /// It is safe for a panic handler to unwind even when this function returns
175    /// false, however this will simply cause the panic handler to be called
176    /// again.
177    #[must_use]
178    #[inline]
179    #[unstable(feature = "panic_can_unwind", issue = "92988")]
180    pub fn can_unwind(&self) -> bool {
181        self.can_unwind
182    }
183
184    #[unstable(
185        feature = "panic_internals",
186        reason = "internal details of the implementation of the `panic!` and related macros",
187        issue = "none"
188    )]
189    #[doc(hidden)]
190    #[inline]
191    pub fn force_no_backtrace(&self) -> bool {
192        self.force_no_backtrace
193    }
194}
195
196#[stable(feature = "panic_hook_display", since = "1.26.0")]
197impl fmt::Display for PanicHookInfo<'_> {
198    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
199        formatter.write_str("panicked at ")?;
200        self.location.fmt(formatter)?;
201        if let Some(payload) = self.payload_as_str() {
202            formatter.write_str(":\n")?;
203            formatter.write_str(payload)?;
204        }
205        Ok(())
206    }
207}
208
209#[doc(hidden)]
210#[unstable(feature = "edition_panic", issue = "none", reason = "use panic!() instead")]
211#[allow_internal_unstable(libstd_sys_internals, const_format_args, panic_internals, rt)]
212#[cfg_attr(not(test), rustc_diagnostic_item = "std_panic_2015_macro")]
213#[rustc_macro_transparency = "semitransparent"]
214pub macro panic_2015 {
215    () => ({
216        $crate::rt::begin_panic("explicit panic")
217    }),
218    ($msg:expr $(,)?) => ({
219        $crate::rt::begin_panic($msg);
220    }),
221    // Special-case the single-argument case for const_panic.
222    ("{}", $arg:expr $(,)?) => ({
223        $crate::rt::panic_display(&$arg);
224    }),
225    ($fmt:expr, $($arg:tt)+) => ({
226        // Semicolon to prevent temporaries inside the formatting machinery from
227        // being considered alive in the caller after the panic_fmt call.
228        $crate::rt::panic_fmt($crate::const_format_args!($fmt, $($arg)+));
229    }),
230}
231
232#[stable(feature = "panic_hooks", since = "1.10.0")]
233pub use core::panic::Location;
234#[doc(hidden)]
235#[unstable(feature = "edition_panic", issue = "none", reason = "use panic!() instead")]
236pub use core::panic::panic_2021;
237#[stable(feature = "catch_unwind", since = "1.9.0")]
238pub use core::panic::{AssertUnwindSafe, RefUnwindSafe, UnwindSafe};
239
240#[unstable(feature = "panic_update_hook", issue = "92649")]
241pub use crate::panicking::update_hook;
242#[stable(feature = "panic_hooks", since = "1.10.0")]
243pub use crate::panicking::{set_hook, take_hook};
244
245/// Panics the current thread with the given message as the panic payload.
246///
247/// The message can be of any (`Any + Send`) type, not just strings.
248///
249/// The message is wrapped in a `Box<'static + Any + Send>`, which can be
250/// accessed later using [`PanicHookInfo::payload`].
251///
252/// See the [`panic!`] macro for more information about panicking.
253#[stable(feature = "panic_any", since = "1.51.0")]
254#[inline]
255#[track_caller]
256#[cfg_attr(not(test), rustc_diagnostic_item = "panic_any")]
257pub fn panic_any<M: 'static + Any + Send>(msg: M) -> ! {
258    crate::panicking::begin_panic(msg);
259}
260
261#[stable(feature = "catch_unwind", since = "1.9.0")]
262impl<T: ?Sized> UnwindSafe for Mutex<T> {}
263#[stable(feature = "catch_unwind", since = "1.9.0")]
264impl<T: ?Sized> UnwindSafe for RwLock<T> {}
265#[stable(feature = "catch_unwind", since = "1.9.0")]
266impl UnwindSafe for Condvar {}
267
268#[stable(feature = "unwind_safe_lock_refs", since = "1.12.0")]
269impl<T: ?Sized> RefUnwindSafe for Mutex<T> {}
270#[stable(feature = "unwind_safe_lock_refs", since = "1.12.0")]
271impl<T: ?Sized> RefUnwindSafe for RwLock<T> {}
272#[stable(feature = "unwind_safe_lock_refs", since = "1.12.0")]
273impl RefUnwindSafe for Condvar {}
274
275// https://github.com/rust-lang/rust/issues/62301
276#[stable(feature = "hashbrown", since = "1.36.0")]
277impl<K, V, S> UnwindSafe for collections::HashMap<K, V, S>
278where
279    K: UnwindSafe,
280    V: UnwindSafe,
281    S: UnwindSafe,
282{
283}
284
285#[unstable(feature = "abort_unwind", issue = "130338")]
286pub use core::panic::abort_unwind;
287
288/// Invokes a closure, capturing the cause of an unwinding panic if one occurs.
289///
290/// This function will return `Ok` with the closure's result if the closure does
291/// not panic, and will return `Err(cause)` if the closure panics. The `cause`
292/// returned is the object with which panic was originally invoked.
293///
294/// Rust functions that are expected to be called from foreign code that does
295/// not support unwinding (such as C compiled with `-fno-exceptions`) should be
296/// defined using `extern "C"`, which ensures that if the Rust code panics, it
297/// is automatically caught and the process is aborted. If this is the desired
298/// behavior, it is not necessary to use `catch_unwind` explicitly. This
299/// function should instead be used when more graceful error-handling is needed.
300///
301/// It is **not** recommended to use this function for a general try/catch
302/// mechanism. The [`Result`] type is more appropriate to use for functions that
303/// can fail on a regular basis. Additionally, this function is not guaranteed
304/// to catch all panics, see the "Notes" section below.
305///
306/// The closure provided is required to adhere to the [`UnwindSafe`] trait to
307/// ensure that all captured variables are safe to cross this boundary. The
308/// purpose of this bound is to encode the concept of [exception safety][rfc] in
309/// the type system. Most usage of this function should not need to worry about
310/// this bound as programs are naturally unwind safe without `unsafe` code. If
311/// it becomes a problem the [`AssertUnwindSafe`] wrapper struct can be used to
312/// quickly assert that the usage here is indeed unwind safe.
313///
314/// [rfc]: https://github.com/rust-lang/rfcs/blob/master/text/1236-stabilize-catch-panic.md
315///
316/// # Notes
317///
318/// This function **might not catch all Rust panics**. A Rust panic is not
319/// always implemented via unwinding, but can be implemented by aborting the
320/// process as well. This function *only* catches unwinding panics, not those
321/// that abort the process.
322///
323/// If a custom panic hook has been set, it will be invoked before the panic is
324/// caught, before unwinding.
325///
326/// Although unwinding into Rust code with a foreign exception (e.g. an
327/// exception thrown from C++ code, or a `panic!` in Rust code compiled or
328/// linked with a different runtime) via an appropriate ABI (e.g. `"C-unwind"`)
329/// is permitted, catching such an exception using this function will have one
330/// of two behaviors, and it is unspecified which will occur:
331///
332/// * The process aborts, after executing all destructors of `f` and the
333///   functions it called.
334/// * The function returns a `Result::Err` containing an opaque type.
335///
336/// Finally, be **careful in how you drop the result of this function**. If it
337/// is `Err`, it contains the panic payload, and dropping that may in turn
338/// panic!
339///
340/// # Examples
341///
342/// ```
343/// use std::panic;
344///
345/// let result = panic::catch_unwind(|| {
346///     println!("hello!");
347/// });
348/// assert!(result.is_ok());
349///
350/// let result = panic::catch_unwind(|| {
351///     panic!("oh no!");
352/// });
353/// assert!(result.is_err());
354/// ```
355#[stable(feature = "catch_unwind", since = "1.9.0")]
356pub fn catch_unwind<F: FnOnce() -> R + UnwindSafe, R>(f: F) -> Result<R> {
357    unsafe { panicking::catch_unwind(f) }
358}
359
360/// Triggers a panic without invoking the panic hook.
361///
362/// This is designed to be used in conjunction with [`catch_unwind`] to, for
363/// example, carry a panic across a layer of C code.
364///
365/// # Notes
366///
367/// Note that panics in Rust are not always implemented via unwinding, but they
368/// may be implemented by aborting the process. If this function is called when
369/// panics are implemented this way then this function will abort the process,
370/// not trigger an unwind.
371///
372/// # Examples
373///
374/// ```should_panic
375/// use std::panic;
376///
377/// let result = panic::catch_unwind(|| {
378///     if 1 != 2 {
379///         panic!("oh no!");
380///     }
381/// });
382///
383/// if let Err(err) = result {
384///     panic::resume_unwind(err);
385/// }
386/// ```
387#[stable(feature = "resume_unwind", since = "1.9.0")]
388pub fn resume_unwind(payload: Box<dyn Any + Send>) -> ! {
389    panicking::resume_unwind(payload)
390}
391
392/// Makes all future panics abort directly without running the panic hook or unwinding.
393///
394/// There is no way to undo this; the effect lasts until the process exits or
395/// execs (or the equivalent).
396///
397/// # Use after fork
398///
399/// This function is particularly useful for calling after `libc::fork`.  After `fork`, in a
400/// multithreaded program it is (on many platforms) not safe to call the allocator.  It is also
401/// generally highly undesirable for an unwind to unwind past the `fork`, because that results in
402/// the unwind propagating to code that was only ever expecting to run in the parent.
403///
404/// `panic::always_abort()` helps avoid both of these.  It directly avoids any further unwinding,
405/// and if there is a panic, the abort will occur without allocating provided that the arguments to
406/// panic can be formatted without allocating.
407///
408/// Examples
409///
410/// ```no_run
411/// #![feature(panic_always_abort)]
412/// use std::panic;
413///
414/// panic::always_abort();
415///
416/// let _ = panic::catch_unwind(|| {
417///     panic!("inside the catch");
418/// });
419///
420/// // We will have aborted already, due to the panic.
421/// unreachable!();
422/// ```
423#[unstable(feature = "panic_always_abort", issue = "84438")]
424pub fn always_abort() {
425    crate::panicking::panic_count::set_always_abort();
426}
427
428/// The configuration for whether and how the default panic hook will capture
429/// and display the backtrace.
430#[derive(Debug, Copy, Clone, PartialEq, Eq)]
431#[unstable(feature = "panic_backtrace_config", issue = "93346")]
432#[non_exhaustive]
433pub enum BacktraceStyle {
434    /// Prints a terser backtrace which ideally only contains relevant
435    /// information.
436    Short,
437    /// Prints a backtrace with all possible information.
438    Full,
439    /// Disable collecting and displaying backtraces.
440    Off,
441}
442
443impl BacktraceStyle {
444    pub(crate) fn full() -> Option<Self> {
445        if cfg!(feature = "backtrace") { Some(BacktraceStyle::Full) } else { None }
446    }
447
448    fn as_u8(self) -> u8 {
449        match self {
450            BacktraceStyle::Short => 1,
451            BacktraceStyle::Full => 2,
452            BacktraceStyle::Off => 3,
453        }
454    }
455
456    fn from_u8(s: u8) -> Option<Self> {
457        match s {
458            1 => Some(BacktraceStyle::Short),
459            2 => Some(BacktraceStyle::Full),
460            3 => Some(BacktraceStyle::Off),
461            _ => None,
462        }
463    }
464}
465
466// Tracks whether we should/can capture a backtrace, and how we should display
467// that backtrace.
468//
469// Internally stores equivalent of an Option<BacktraceStyle>.
470static SHOULD_CAPTURE: Atomic<u8> = AtomicU8::new(0);
471
472/// Configures whether the default panic hook will capture and display a
473/// backtrace.
474///
475/// The default value for this setting may be set by the `RUST_BACKTRACE`
476/// environment variable; see the details in [`get_backtrace_style`].
477#[unstable(feature = "panic_backtrace_config", issue = "93346")]
478pub fn set_backtrace_style(style: BacktraceStyle) {
479    if cfg!(feature = "backtrace") {
480        // If the `backtrace` feature of this crate is enabled, set the backtrace style.
481        SHOULD_CAPTURE.store(style.as_u8(), Ordering::Relaxed);
482    }
483}
484
485/// Checks whether the standard library's panic hook will capture and print a
486/// backtrace.
487///
488/// This function will, if a backtrace style has not been set via
489/// [`set_backtrace_style`], read the environment variable `RUST_BACKTRACE` to
490/// determine a default value for the backtrace formatting:
491///
492/// The first call to `get_backtrace_style` may read the `RUST_BACKTRACE`
493/// environment variable if `set_backtrace_style` has not been called to
494/// override the default value. After a call to `set_backtrace_style` or
495/// `get_backtrace_style`, any changes to `RUST_BACKTRACE` will have no effect.
496///
497/// `RUST_BACKTRACE` is read according to these rules:
498///
499/// * `0` for `BacktraceStyle::Off`
500/// * `full` for `BacktraceStyle::Full`
501/// * `1` for `BacktraceStyle::Short`
502/// * Other values are currently `BacktraceStyle::Short`, but this may change in
503///   the future
504///
505/// Returns `None` if backtraces aren't currently supported.
506#[unstable(feature = "panic_backtrace_config", issue = "93346")]
507pub fn get_backtrace_style() -> Option<BacktraceStyle> {
508    if !cfg!(feature = "backtrace") {
509        // If the `backtrace` feature of this crate isn't enabled quickly return
510        // `Unsupported` so this can be constant propagated all over the place
511        // to optimize away callers.
512        return None;
513    }
514
515    let current = SHOULD_CAPTURE.load(Ordering::Relaxed);
516    if let Some(style) = BacktraceStyle::from_u8(current) {
517        return Some(style);
518    }
519
520    let format = match crate::env::var_os("RUST_BACKTRACE") {
521        Some(x) if &x == "0" => BacktraceStyle::Off,
522        Some(x) if &x == "full" => BacktraceStyle::Full,
523        Some(_) => BacktraceStyle::Short,
524        None if crate::sys::FULL_BACKTRACE_DEFAULT => BacktraceStyle::Full,
525        None => BacktraceStyle::Off,
526    };
527
528    match SHOULD_CAPTURE.compare_exchange(0, format.as_u8(), Ordering::Relaxed, Ordering::Relaxed) {
529        Ok(_) => Some(format),
530        Err(new) => BacktraceStyle::from_u8(new),
531    }
532}