]> git.proxmox.com Git - rustc.git/blame - library/core/src/panic.rs
New upstream version 1.67.1+dfsg1
[rustc.git] / library / core / src / panic.rs
CommitLineData
0531ce1d
XL
1//! Panic support in the standard library.
2
60c5eb7d 3#![stable(feature = "core_panic_info", since = "1.41.0")]
0531ce1d 4
94222f64
XL
5mod location;
6mod panic_info;
7mod unwind_safe;
8
48663c56 9use crate::any::Any;
94222f64
XL
10
11#[stable(feature = "panic_hooks", since = "1.10.0")]
12pub use self::location::Location;
13#[stable(feature = "panic_hooks", since = "1.10.0")]
14pub use self::panic_info::PanicInfo;
15#[stable(feature = "catch_unwind", since = "1.9.0")]
16pub use self::unwind_safe::{AssertUnwindSafe, RefUnwindSafe, UnwindSafe};
0531ce1d 17
5869c6ff
XL
18#[doc(hidden)]
19#[unstable(feature = "edition_panic", issue = "none", reason = "use panic!() instead")]
94222f64 20#[allow_internal_unstable(core_panic, const_format_args)]
5869c6ff
XL
21#[rustc_diagnostic_item = "core_panic_2015_macro"]
22#[rustc_macro_transparency = "semitransparent"]
23pub macro panic_2015 {
24 () => (
25 $crate::panicking::panic("explicit panic")
26 ),
27 ($msg:literal $(,)?) => (
28 $crate::panicking::panic($msg)
29 ),
c295e0f8 30 // Use `panic_str` instead of `panic_display::<&str>` for non_fmt_panic lint.
5869c6ff
XL
31 ($msg:expr $(,)?) => (
32 $crate::panicking::panic_str($msg)
33 ),
c295e0f8
XL
34 // Special-case the single-argument case for const_panic.
35 ("{}", $arg:expr $(,)?) => (
36 $crate::panicking::panic_display(&$arg)
37 ),
5869c6ff 38 ($fmt:expr, $($arg:tt)+) => (
94222f64 39 $crate::panicking::panic_fmt($crate::const_format_args!($fmt, $($arg)+))
5869c6ff
XL
40 ),
41}
42
43#[doc(hidden)]
44#[unstable(feature = "edition_panic", issue = "none", reason = "use panic!() instead")]
94222f64 45#[allow_internal_unstable(core_panic, const_format_args)]
5869c6ff
XL
46#[rustc_diagnostic_item = "core_panic_2021_macro"]
47#[rustc_macro_transparency = "semitransparent"]
48pub macro panic_2021 {
49 () => (
50 $crate::panicking::panic("explicit panic")
51 ),
c295e0f8
XL
52 // Special-case the single-argument case for const_panic.
53 ("{}", $arg:expr $(,)?) => (
54 $crate::panicking::panic_display(&$arg)
55 ),
5869c6ff 56 ($($t:tt)+) => (
94222f64 57 $crate::panicking::panic_fmt($crate::const_format_args!($($t)+))
5869c6ff
XL
58 ),
59}
60
a2a8927a
XL
61#[doc(hidden)]
62#[unstable(feature = "edition_panic", issue = "none", reason = "use unreachable!() instead")]
63#[allow_internal_unstable(core_panic)]
64#[rustc_diagnostic_item = "unreachable_2015_macro"]
65#[rustc_macro_transparency = "semitransparent"]
66pub macro unreachable_2015 {
67 () => (
68 $crate::panicking::panic("internal error: entered unreachable code")
69 ),
70 // Use of `unreachable_display` for non_fmt_panic lint.
5e7ed085 71 // NOTE: the message ("internal error ...") is embedded directly in unreachable_display
a2a8927a
XL
72 ($msg:expr $(,)?) => (
73 $crate::panicking::unreachable_display(&$msg)
74 ),
75 ($fmt:expr, $($arg:tt)*) => (
76 $crate::panic!($crate::concat!("internal error: entered unreachable code: ", $fmt), $($arg)*)
77 ),
78}
79
80#[doc(hidden)]
81#[unstable(feature = "edition_panic", issue = "none", reason = "use unreachable!() instead")]
82#[allow_internal_unstable(core_panic)]
a2a8927a
XL
83#[rustc_macro_transparency = "semitransparent"]
84pub macro unreachable_2021 {
85 () => (
86 $crate::panicking::panic("internal error: entered unreachable code")
87 ),
88 ($($t:tt)+) => (
89 $crate::panic!("internal error: entered unreachable code: {}", $crate::format_args!($($t)+))
90 ),
91}
92
83c7162d
XL
93/// An internal trait used by libstd to pass data from libstd to `panic_unwind`
94/// and other panic runtimes. Not intended to be stabilized any time soon, do
95/// not use.
dfeec247 96#[unstable(feature = "std_internals", issue = "none")]
83c7162d
XL
97#[doc(hidden)]
98pub unsafe trait BoxMeUp {
60c5eb7d
XL
99 /// Take full ownership of the contents.
100 /// The return type is actually `Box<dyn Any + Send>`, but we cannot use `Box` in libcore.
101 ///
102 /// After this method got called, only some dummy default value is left in `self`.
103 /// Calling this method twice, or calling `get` after calling this method, is an error.
104 ///
105 /// The argument is borrowed because the panic runtime (`__rust_start_panic`) only
106 /// gets a borrowed `dyn BoxMeUp`.
107 fn take_box(&mut self) -> *mut (dyn Any + Send);
108
109 /// Just borrow the contents.
8faf50e0 110 fn get(&mut self) -> &(dyn Any + Send);
83c7162d 111}