1#![stable(feature = "core_panic_info", since = "1.41.0")]
4
5mod location;
6mod panic_info;
7mod unwind_safe;
8
9#[stable(feature = "panic_hooks", since = "1.10.0")]
10pub use self::location::Location;
11#[stable(feature = "panic_hooks", since = "1.10.0")]
12pub use self::panic_info::PanicInfo;
13#[stable(feature = "panic_info_message", since = "1.81.0")]
14pub use self::panic_info::PanicMessage;
15#[stable(feature = "catch_unwind", since = "1.9.0")]
16pub use self::unwind_safe::{AssertUnwindSafe, RefUnwindSafe, UnwindSafe};
17use crate::any::Any;
18
19#[doc(hidden)]
20#[unstable(feature = "edition_panic", issue = "none", reason = "use panic!() instead")]
21#[allow_internal_unstable(panic_internals, const_format_args)]
22#[rustc_diagnostic_item = "core_panic_2015_macro"]
23#[rustc_macro_transparency = "semitransparent"]
24pub macro panic_2015 {
25    () => (
26        $crate::panicking::panic("explicit panic")
27    ),
28    ($msg:literal $(,)?) => (
29        $crate::panicking::panic($msg)
30    ),
31    ($msg:expr $(,)?) => ({
33        $crate::panicking::panic_str_2015($msg);
34    }),
35    ("{}", $arg:expr $(,)?) => ({
37        $crate::panicking::panic_display(&$arg);
38    }),
39    ($fmt:expr, $($arg:tt)+) => ({
40        $crate::panicking::panic_fmt($crate::const_format_args!($fmt, $($arg)+));
43    }),
44}
45
46#[doc(hidden)]
47#[unstable(feature = "edition_panic", issue = "none", reason = "use panic!() instead")]
48#[allow_internal_unstable(panic_internals, const_format_args)]
49#[rustc_diagnostic_item = "core_panic_2021_macro"]
50#[rustc_macro_transparency = "semitransparent"]
51#[cfg(feature = "panic_immediate_abort")]
52pub macro panic_2021 {
53    () => (
54        $crate::panicking::panic("explicit panic")
55    ),
56    ("{}", $arg:expr $(,)?) => ({
58        $crate::panicking::panic_display(&$arg);
59    }),
60    ($($t:tt)+) => ({
61        $crate::panicking::panic_fmt($crate::const_format_args!($($t)+));
64    }),
65}
66
67#[doc(hidden)]
68#[unstable(feature = "edition_panic", issue = "none", reason = "use panic!() instead")]
69#[allow_internal_unstable(
70    panic_internals,
71    core_intrinsics,
72    const_dispatch,
73    const_eval_select,
74    const_format_args,
75    rustc_attrs
76)]
77#[rustc_diagnostic_item = "core_panic_2021_macro"]
78#[rustc_macro_transparency = "semitransparent"]
79#[cfg(not(feature = "panic_immediate_abort"))]
80pub macro panic_2021 {
81    () => ({
82        #[cold]
85        #[track_caller]
86        #[inline(never)]
87        const fn panic_cold_explicit() -> ! {
88            $crate::panicking::panic_explicit()
89        }
90        panic_cold_explicit();
91    }),
92    ("{}", $arg:expr $(,)?) => ({
94        #[cold]
95        #[track_caller]
96        #[inline(never)]
97        #[rustc_const_panic_str] #[rustc_do_not_const_check] const fn panic_cold_display<T: $crate::fmt::Display>(arg: &T) -> ! {
100            $crate::panicking::panic_display(arg)
101        }
102        panic_cold_display(&$arg);
103    }),
104    ($($t:tt)+) => ({
105        $crate::panicking::panic_fmt($crate::const_format_args!($($t)+));
108    }),
109}
110
111#[doc(hidden)]
112#[unstable(feature = "edition_panic", issue = "none", reason = "use unreachable!() instead")]
113#[allow_internal_unstable(panic_internals)]
114#[rustc_diagnostic_item = "unreachable_2015_macro"]
115#[rustc_macro_transparency = "semitransparent"]
116pub macro unreachable_2015 {
117    () => (
118        $crate::panicking::panic("internal error: entered unreachable code")
119    ),
120    ($msg:expr $(,)?) => ({
123        $crate::panicking::unreachable_display(&$msg);
124    }),
125    ($fmt:expr, $($arg:tt)*) => (
126        $crate::panic!($crate::concat!("internal error: entered unreachable code: ", $fmt), $($arg)*)
127    ),
128}
129
130#[doc(hidden)]
131#[unstable(feature = "edition_panic", issue = "none", reason = "use unreachable!() instead")]
132#[allow_internal_unstable(panic_internals)]
133#[rustc_macro_transparency = "semitransparent"]
134pub macro unreachable_2021 {
135    () => (
136        $crate::panicking::panic("internal error: entered unreachable code")
137    ),
138    ($($t:tt)+) => (
139        $crate::panic!("internal error: entered unreachable code: {}", $crate::format_args!($($t)+))
140    ),
141}
142
143#[unstable(feature = "abort_unwind", issue = "130338")]
163#[rustc_nounwind]
164pub fn abort_unwind<F: FnOnce() -> R, R>(f: F) -> R {
165    f()
166}
167
168#[unstable(feature = "std_internals", issue = "none")]
172#[doc(hidden)]
173pub unsafe trait PanicPayload: crate::fmt::Display {
174    fn take_box(&mut self) -> *mut (dyn Any + Send);
183
184    fn get(&mut self) -> &(dyn Any + Send);
186
187    fn as_str(&mut self) -> Option<&str> {
189        None
190    }
191}
192
193#[unstable(feature = "panic_internals", issue = "none")]
202#[doc(hidden)]
203pub macro const_panic {
204    ($const_msg:literal, $runtime_msg:literal, $($arg:ident : $ty:ty = $val:expr),* $(,)?) => {{
205        #[rustc_allow_const_fn_unstable(const_eval_select)]
209        #[inline(always)] #[track_caller]
211        const fn do_panic($($arg: $ty),*) -> ! {
212            $crate::intrinsics::const_eval_select!(
213                @capture { $($arg: $ty = $arg),* } -> !:
214                #[noinline]
215                if const #[track_caller] #[inline] { $crate::panic!($const_msg)
217                } else #[track_caller] { $crate::panic!($runtime_msg)
219                }
220            )
221        }
222
223        do_panic($($val),*)
224    }},
225    ($const_msg:literal, $runtime_msg:literal, $($arg:ident : $ty:ty),* $(,)?) => {
228        $crate::panic::const_panic!(
229            $const_msg,
230            $runtime_msg,
231            $($arg: $ty = $arg),*
232        )
233    },
234}
235
236#[unstable(feature = "panic_internals", issue = "none")]
240#[doc(hidden)]
241pub macro const_assert {
242    ($condition: expr, $const_msg:literal, $runtime_msg:literal, $($arg:tt)*) => {{
243        if !$crate::intrinsics::likely($condition) {
244            $crate::panic::const_panic!($const_msg, $runtime_msg, $($arg)*)
245        }
246    }}
247}