1#![allow(dead_code, missing_docs)]
25#![unstable(
26 feature = "panic_internals",
27 reason = "internal details of the implementation of the `panic!` and related macros",
28 issue = "none"
29)]
30
31#[cfg(not(feature = "ferrocene_certified"))]
32use crate::fmt;
33use crate::intrinsics::const_eval_select;
34#[cfg(not(feature = "ferrocene_certified"))]
35use crate::panic::{Location, PanicInfo};
36
37#[cfg(feature = "ferrocene_certified")]
39#[rustfmt::skip]
40use crate::panic::PanicInfo;
41
42#[cfg(not(feature = "ferrocene_certified"))]
44pub(crate) type PanicFmt<'a> = fmt::Arguments<'a>;
45#[cfg(feature = "ferrocene_certified")]
46pub(crate) type PanicFmt<'a> = &'a &'static str;
47
48#[cfg(feature = "panic_immediate_abort")]
49compile_error!(
50 "panic_immediate_abort is now a real panic strategy! \
51 Enable it with `panic = \"immediate-abort\"` in Cargo.toml, \
52 or with the compiler flags `-Zunstable-options -Cpanic=immediate-abort`. \
53 In both cases, you still need to build core, e.g. with `-Zbuild-std`"
54);
55
56#[cfg_attr(not(panic = "immediate-abort"), inline(never), cold)]
68#[cfg_attr(panic = "immediate-abort", inline)]
69#[track_caller]
70#[lang = "panic_fmt"] #[rustc_do_not_const_check] #[rustc_const_stable_indirect] pub const fn panic_fmt(fmt: PanicFmt<'_>) -> ! {
75 #[ferrocene::annotation(
76 "The `immediate-abort` behavior is not certified, we only support `abort`."
77 )]
78 if cfg!(panic = "immediate-abort") {
79 super::intrinsics::abort()
80 };
81
82 unsafe extern "Rust" {
85 #[lang = "panic_impl"]
86 fn panic_impl(pi: &PanicInfo<'_>) -> !;
87 }
88
89 #[cfg(not(feature = "ferrocene_certified"))]
90 let pi = PanicInfo::new(
91 &fmt,
92 Location::caller(),
93 true,
94 false,
95 );
96 #[cfg(feature = "ferrocene_certified")]
97 let pi = PanicInfo::new(&fmt);
98 unsafe { panic_impl(&pi) }
100}
101
102#[cfg_attr(not(panic = "immediate-abort"), inline(never), cold)]
106#[cfg_attr(panic = "immediate-abort", inline)]
107#[track_caller]
108#[rustc_nounwind]
112#[rustc_const_stable_indirect] #[rustc_allow_const_fn_unstable(const_eval_select)]
114#[ferrocene::annotation("Cannot be covered as it causes an unwinding panic")]
115pub const fn panic_nounwind_fmt(fmt: PanicFmt<'_>, _force_no_backtrace: bool) -> ! {
116 const_eval_select!(
117 @capture { fmt: PanicFmt<'_>, _force_no_backtrace: bool } -> !:
118 if const #[track_caller] {
119 panic_fmt(fmt)
121 } else #[track_caller] {
122 if cfg!(panic = "immediate-abort") {
123 super::intrinsics::abort()
124 }
125
126 unsafe extern "Rust" {
129 #[lang = "panic_impl"]
130 fn panic_impl(pi: &PanicInfo<'_>) -> !;
131 }
132
133 #[cfg(not(feature = "ferrocene_certified"))]
135 let pi = PanicInfo::new(
136 &fmt,
137 Location::caller(),
138 false,
139 _force_no_backtrace,
140 );
141 #[cfg(feature = "ferrocene_certified")]
142 let pi = PanicInfo::new(&fmt);
143
144 unsafe { panic_impl(&pi) }
146 }
147 )
148}
149
150#[cfg_attr(not(panic = "immediate-abort"), inline(never), cold)]
157#[cfg_attr(panic = "immediate-abort", inline)]
158#[track_caller]
159#[rustc_const_stable_indirect] #[lang = "panic"] pub const fn panic(expr: &'static str) -> ! {
162 #[cfg(not(feature = "ferrocene_certified"))]
174 panic_fmt(fmt::Arguments::new_const(&[expr]));
175 #[cfg(feature = "ferrocene_certified")]
176 panic_fmt(&expr)
177}
178
179macro_rules! panic_const {
188 ($($lang:ident = $message:expr,)+) => {
189 $(
190 #[cfg_attr(not(panic = "immediate-abort"), inline(never), cold)]
195 #[cfg_attr(panic = "immediate-abort", inline)]
196 #[track_caller]
197 #[rustc_const_stable_indirect] #[lang = stringify!($lang)]
199 #[ferrocene::annotation("Cannot be covered as this code cannot be reached during runtime.")]
200 pub const fn $lang() -> ! {
201 #[cfg(not(feature = "ferrocene_certified"))]
208 panic_fmt(fmt::Arguments::new_const(&[$message]));
209 #[cfg(feature = "ferrocene_certified")]
210 panic_fmt(&$message);
211 }
212 )+
213 }
214}
215
216pub mod panic_const {
221 use super::*;
222 panic_const! {
223 panic_const_add_overflow = "attempt to add with overflow",
224 panic_const_sub_overflow = "attempt to subtract with overflow",
225 panic_const_mul_overflow = "attempt to multiply with overflow",
226 panic_const_div_overflow = "attempt to divide with overflow",
227 panic_const_rem_overflow = "attempt to calculate the remainder with overflow",
228 panic_const_neg_overflow = "attempt to negate with overflow",
229 panic_const_shr_overflow = "attempt to shift right with overflow",
230 panic_const_shl_overflow = "attempt to shift left with overflow",
231 panic_const_div_by_zero = "attempt to divide by zero",
232 panic_const_rem_by_zero = "attempt to calculate the remainder with a divisor of zero",
233 panic_const_coroutine_resumed = "coroutine resumed after completion",
234 panic_const_async_fn_resumed = "`async fn` resumed after completion",
235 panic_const_async_gen_fn_resumed = "`async gen fn` resumed after completion",
236 panic_const_gen_fn_none = "`gen fn` should just keep returning `None` after completion",
237 panic_const_coroutine_resumed_panic = "coroutine resumed after panicking",
238 panic_const_async_fn_resumed_panic = "`async fn` resumed after panicking",
239 panic_const_async_gen_fn_resumed_panic = "`async gen fn` resumed after panicking",
240 panic_const_gen_fn_none_panic = "`gen fn` should just keep returning `None` after panicking",
241 }
242 panic_const! {
245 panic_const_coroutine_resumed_drop = "coroutine resumed after async drop",
246 panic_const_async_fn_resumed_drop = "`async fn` resumed after async drop",
247 panic_const_async_gen_fn_resumed_drop = "`async gen fn` resumed after async drop",
248 panic_const_gen_fn_none_drop = "`gen fn` resumed after async drop",
249 }
250}
251
252#[cfg_attr(not(panic = "immediate-abort"), inline(never), cold)]
255#[cfg_attr(panic = "immediate-abort", inline)]
256#[lang = "panic_nounwind"] #[rustc_nounwind]
258#[rustc_const_stable_indirect] #[ferrocene::annotation("Cannot be covered as it causes an unwinding panic")]
260pub const fn panic_nounwind(expr: &'static str) -> ! {
261 #[cfg(not(feature = "ferrocene_certified"))]
262 panic_nounwind_fmt(fmt::Arguments::new_const(&[expr]), false);
263 #[cfg(feature = "ferrocene_certified")]
264 panic_nounwind_fmt(&expr, false);
265}
266
267#[cfg_attr(not(panic = "immediate-abort"), inline(never), cold)]
269#[cfg_attr(panic = "immediate-abort", inline)]
270#[rustc_nounwind]
271#[cfg(not(feature = "ferrocene_certified"))]
272pub fn panic_nounwind_nobacktrace(expr: &'static str) -> ! {
273 panic_nounwind_fmt(fmt::Arguments::new_const(&[expr]), true);
274}
275
276#[inline]
277#[track_caller]
278#[rustc_diagnostic_item = "unreachable_display"] #[cfg(not(feature = "ferrocene_certified"))]
280pub fn unreachable_display<T: fmt::Display>(x: &T) -> ! {
281 panic_fmt(format_args!("internal error: entered unreachable code: {}", *x));
282}
283
284#[inline]
287#[track_caller]
288#[rustc_diagnostic_item = "panic_str_2015"]
289#[rustc_const_stable_indirect] #[cfg(not(feature = "ferrocene_certified"))]
291pub const fn panic_str_2015(expr: &str) -> ! {
292 panic_display(&expr);
293}
294
295#[inline]
296#[track_caller]
297#[lang = "panic_display"] #[rustc_do_not_const_check] #[rustc_const_stable_indirect] #[cfg(not(feature = "ferrocene_certified"))]
301pub const fn panic_display<T: fmt::Display>(x: &T) -> ! {
302 panic_fmt(format_args!("{}", *x));
303}
304
305#[cfg_attr(not(panic = "immediate-abort"), inline(never), cold, optimize(size))]
306#[cfg_attr(panic = "immediate-abort", inline)]
307#[track_caller]
308#[lang = "panic_bounds_check"] #[cfg_attr(feature = "ferrocene_certified", expect(unused_variables))]
310fn panic_bounds_check(index: usize, len: usize) -> ! {
311 if cfg!(panic = "immediate-abort") {
312 super::intrinsics::abort()
313 }
314 panic!("index out of bounds: the len is {len} but the index is {index}")
315}
316
317#[cfg_attr(not(panic = "immediate-abort"), inline(never), cold, optimize(size))]
318#[cfg_attr(panic = "immediate-abort", inline)]
319#[track_caller]
320#[lang = "panic_misaligned_pointer_dereference"] #[rustc_nounwind] #[cfg(not(feature = "ferrocene_certified"))]
323fn panic_misaligned_pointer_dereference(required: usize, found: usize) -> ! {
324 if cfg!(panic = "immediate-abort") {
325 super::intrinsics::abort()
326 }
327
328 panic_nounwind_fmt(
329 format_args!(
330 "misaligned pointer dereference: address must be a multiple of {required:#x} but is {found:#x}"
331 ),
332 false,
333 )
334}
335
336#[cfg_attr(not(panic = "immediate-abort"), inline(never), cold, optimize(size))]
337#[cfg_attr(panic = "immediate-abort", inline)]
338#[track_caller]
339#[lang = "panic_null_pointer_dereference"] #[rustc_nounwind] #[cfg(not(feature = "ferrocene_certified"))]
342fn panic_null_pointer_dereference() -> ! {
343 if cfg!(panic = "immediate-abort") {
344 super::intrinsics::abort()
345 }
346
347 panic_nounwind_fmt(
348 format_args!("null pointer dereference occurred"),
349 false,
350 )
351}
352
353#[cfg_attr(not(panic = "immediate-abort"), inline(never), cold, optimize(size))]
354#[cfg_attr(panic = "immediate-abort", inline)]
355#[track_caller]
356#[lang = "panic_invalid_enum_construction"] #[rustc_nounwind] #[cfg(not(feature = "ferrocene_certified"))]
359fn panic_invalid_enum_construction(source: u128) -> ! {
360 if cfg!(panic = "immediate-abort") {
361 super::intrinsics::abort()
362 }
363
364 panic_nounwind_fmt(
365 format_args!("trying to construct an enum from an invalid value {source:#x}"),
366 false,
367 )
368}
369
370#[cfg_attr(not(panic = "immediate-abort"), inline(never), cold, optimize(size))]
378#[cfg_attr(panic = "immediate-abort", inline)]
379#[lang = "panic_cannot_unwind"] #[rustc_nounwind]
381#[ferrocene::annotation("Cannot be covered as it causes an unwinding panic")]
382fn panic_cannot_unwind() -> ! {
383 panic_nounwind("panic in a function that cannot unwind")
385}
386
387#[cfg_attr(not(panic = "immediate-abort"), inline(never), cold, optimize(size))]
395#[cfg_attr(panic = "immediate-abort", inline)]
396#[lang = "panic_in_cleanup"] #[rustc_nounwind]
398#[cfg(not(feature = "ferrocene_certified"))]
399fn panic_in_cleanup() -> ! {
400 panic_nounwind_nobacktrace("panic in a destructor during cleanup")
402}
403
404#[lang = "const_panic_fmt"] #[rustc_const_stable_indirect] #[cfg(not(feature = "ferrocene_certified"))]
408pub const fn const_panic_fmt(fmt: fmt::Arguments<'_>) -> ! {
409 if let Some(msg) = fmt.as_str() {
410 panic_display(&msg);
412 } else {
413 unsafe { crate::hint::unreachable_unchecked() };
417 }
418}
419
420#[derive(Debug)]
421#[doc(hidden)]
422#[cfg(not(feature = "ferrocene_certified"))]
423pub enum AssertKind {
424 Eq,
425 Ne,
426 Match,
427}
428
429#[cfg_attr(not(panic = "immediate-abort"), inline(never), cold, optimize(size))]
431#[cfg_attr(panic = "immediate-abort", inline)]
432#[track_caller]
433#[doc(hidden)]
434#[cfg(not(feature = "ferrocene_certified"))]
435pub fn assert_failed<T, U>(
436 kind: AssertKind,
437 left: &T,
438 right: &U,
439 args: Option<fmt::Arguments<'_>>,
440) -> !
441where
442 T: fmt::Debug + ?Sized,
443 U: fmt::Debug + ?Sized,
444{
445 assert_failed_inner(kind, &left, &right, args)
446}
447
448#[cfg_attr(not(panic = "immediate-abort"), inline(never), cold, optimize(size))]
450#[cfg_attr(panic = "immediate-abort", inline)]
451#[track_caller]
452#[doc(hidden)]
453#[cfg(not(feature = "ferrocene_certified"))]
454pub fn assert_matches_failed<T: fmt::Debug + ?Sized>(
455 left: &T,
456 right: &str,
457 args: Option<fmt::Arguments<'_>>,
458) -> ! {
459 struct Pattern<'a>(&'a str);
461 impl fmt::Debug for Pattern<'_> {
462 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
463 f.write_str(self.0)
464 }
465 }
466 assert_failed_inner(AssertKind::Match, &left, &Pattern(right), args);
467}
468
469#[cfg_attr(not(panic = "immediate-abort"), inline(never), cold, optimize(size))]
471#[cfg_attr(panic = "immediate-abort", inline)]
472#[track_caller]
473#[cfg(not(feature = "ferrocene_certified"))]
474fn assert_failed_inner(
475 kind: AssertKind,
476 left: &dyn fmt::Debug,
477 right: &dyn fmt::Debug,
478 args: Option<fmt::Arguments<'_>>,
479) -> ! {
480 let op = match kind {
481 AssertKind::Eq => "==",
482 AssertKind::Ne => "!=",
483 AssertKind::Match => "matches",
484 };
485
486 match args {
487 Some(args) => panic!(
488 r#"assertion `left {op} right` failed: {args}
489 left: {left:?}
490 right: {right:?}"#
491 ),
492 None => panic!(
493 r#"assertion `left {op} right` failed
494 left: {left:?}
495 right: {right:?}"#
496 ),
497 }
498}