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(feature = "ferrocene_certified")]
35use crate::panic::PanicInfo;
36#[cfg(not(feature = "ferrocene_certified"))]
37use crate::panic::{Location, PanicInfo};
38
39#[cfg(not(feature = "ferrocene_certified"))]
41pub(crate) type PanicFmt<'a> = fmt::Arguments<'a>;
42#[cfg(feature = "ferrocene_certified")]
43pub(crate) type PanicFmt<'a> = &'a &'static str;
44
45#[cfg(feature = "panic_immediate_abort")]
46#[cfg(not(feature = "ferrocene_certified"))]
48const _: () = assert!(cfg!(panic = "abort"), "panic_immediate_abort requires -C panic=abort");
49
50#[cfg_attr(not(feature = "panic_immediate_abort"), inline(never), cold)]
62#[cfg_attr(feature = "panic_immediate_abort", inline)]
63#[track_caller]
64#[lang = "panic_fmt"] #[rustc_do_not_const_check] #[rustc_const_stable_indirect] pub const fn panic_fmt(fmt: PanicFmt<'_>) -> ! {
68 if cfg!(feature = "panic_immediate_abort") {
69 super::intrinsics::abort()
70 }
71
72 unsafe extern "Rust" {
75 #[lang = "panic_impl"]
76 fn panic_impl(pi: &PanicInfo<'_>) -> !;
77 }
78
79 #[cfg(not(feature = "ferrocene_certified"))]
80 let pi = PanicInfo::new(
81 &fmt,
82 Location::caller(),
83 true,
84 false,
85 );
86 #[cfg(feature = "ferrocene_certified")]
87 let pi = PanicInfo::new(&fmt);
88 unsafe { panic_impl(&pi) }
90}
91
92#[cfg_attr(not(feature = "panic_immediate_abort"), inline(never), cold)]
96#[cfg_attr(feature = "panic_immediate_abort", inline)]
97#[track_caller]
98#[rustc_nounwind]
102#[rustc_const_stable_indirect] #[rustc_allow_const_fn_unstable(const_eval_select)]
104pub const fn panic_nounwind_fmt(fmt: PanicFmt<'_>, _force_no_backtrace: bool) -> ! {
105 const_eval_select!(
106 @capture { fmt: PanicFmt<'_>, _force_no_backtrace: bool } -> !:
107 if const #[track_caller] {
108 panic_fmt(fmt)
110 } else #[track_caller] {
111 if cfg!(feature = "panic_immediate_abort") {
112 super::intrinsics::abort()
113 }
114
115 unsafe extern "Rust" {
118 #[lang = "panic_impl"]
119 fn panic_impl(pi: &PanicInfo<'_>) -> !;
120 }
121
122 #[cfg(not(feature = "ferrocene_certified"))]
124 let pi = PanicInfo::new(
125 &fmt,
126 Location::caller(),
127 false,
128 _force_no_backtrace,
129 );
130 #[cfg(feature = "ferrocene_certified")]
131 let pi = PanicInfo::new(&fmt);
132
133 unsafe { panic_impl(&pi) }
135 }
136 )
137}
138
139#[cfg_attr(not(feature = "panic_immediate_abort"), inline(never), cold)]
146#[cfg_attr(feature = "panic_immediate_abort", inline)]
147#[track_caller]
148#[rustc_const_stable_indirect] #[lang = "panic"] pub const fn panic(expr: &'static str) -> ! {
151 #[cfg(not(feature = "ferrocene_certified"))]
163 panic_fmt(fmt::Arguments::new_const(&[expr]));
164 #[cfg(feature = "ferrocene_certified")]
165 panic_fmt(&expr)
166}
167
168#[cfg(not(feature = "ferrocene_certified"))]
177macro_rules! panic_const {
178 ($($lang:ident = $message:expr,)+) => {
179 $(
180 #[cfg_attr(not(feature = "panic_immediate_abort"), inline(never), cold)]
185 #[cfg_attr(feature = "panic_immediate_abort", inline)]
186 #[track_caller]
187 #[rustc_const_stable_indirect] #[lang = stringify!($lang)]
189 pub const fn $lang() -> ! {
190 panic_fmt(fmt::Arguments::new_const(&[$message]));
197 }
198 )+
199 }
200}
201
202#[cfg(not(feature = "ferrocene_certified"))]
207pub mod panic_const {
208 use super::*;
209 panic_const! {
210 panic_const_add_overflow = "attempt to add with overflow",
211 panic_const_sub_overflow = "attempt to subtract with overflow",
212 panic_const_mul_overflow = "attempt to multiply with overflow",
213 panic_const_div_overflow = "attempt to divide with overflow",
214 panic_const_rem_overflow = "attempt to calculate the remainder with overflow",
215 panic_const_neg_overflow = "attempt to negate with overflow",
216 panic_const_shr_overflow = "attempt to shift right with overflow",
217 panic_const_shl_overflow = "attempt to shift left with overflow",
218 panic_const_div_by_zero = "attempt to divide by zero",
219 panic_const_rem_by_zero = "attempt to calculate the remainder with a divisor of zero",
220 panic_const_coroutine_resumed = "coroutine resumed after completion",
221 panic_const_async_fn_resumed = "`async fn` resumed after completion",
222 panic_const_async_gen_fn_resumed = "`async gen fn` resumed after completion",
223 panic_const_gen_fn_none = "`gen fn` should just keep returning `None` after completion",
224 panic_const_coroutine_resumed_panic = "coroutine resumed after panicking",
225 panic_const_async_fn_resumed_panic = "`async fn` resumed after panicking",
226 panic_const_async_gen_fn_resumed_panic = "`async gen fn` resumed after panicking",
227 panic_const_gen_fn_none_panic = "`gen fn` should just keep returning `None` after panicking",
228 }
229 panic_const! {
232 panic_const_coroutine_resumed_drop = "coroutine resumed after async drop",
233 panic_const_async_fn_resumed_drop = "`async fn` resumed after async drop",
234 panic_const_async_gen_fn_resumed_drop = "`async gen fn` resumed after async drop",
235 panic_const_gen_fn_none_drop = "`gen fn` resumed after async drop",
236 }
237}
238
239#[cfg_attr(not(feature = "panic_immediate_abort"), inline(never), cold)]
242#[cfg_attr(feature = "panic_immediate_abort", inline)]
243#[lang = "panic_nounwind"] #[rustc_nounwind]
245#[rustc_const_stable_indirect] pub const fn panic_nounwind(expr: &'static str) -> ! {
247 #[cfg(not(feature = "ferrocene_certified"))]
248 panic_nounwind_fmt(fmt::Arguments::new_const(&[expr]), false);
249 #[cfg(feature = "ferrocene_certified")]
250 panic_nounwind_fmt(&expr, false);
251}
252
253#[cfg_attr(not(feature = "panic_immediate_abort"), inline(never), cold)]
255#[cfg_attr(feature = "panic_immediate_abort", inline)]
256#[rustc_nounwind]
257#[cfg(not(feature = "ferrocene_certified"))]
258pub fn panic_nounwind_nobacktrace(expr: &'static str) -> ! {
259 panic_nounwind_fmt(fmt::Arguments::new_const(&[expr]), true);
260}
261
262#[inline]
263#[track_caller]
264#[rustc_diagnostic_item = "unreachable_display"] #[cfg(not(feature = "ferrocene_certified"))]
266pub fn unreachable_display<T: fmt::Display>(x: &T) -> ! {
267 panic_fmt(format_args!("internal error: entered unreachable code: {}", *x));
268}
269
270#[inline]
273#[track_caller]
274#[rustc_diagnostic_item = "panic_str_2015"]
275#[rustc_const_stable_indirect] #[cfg(not(feature = "ferrocene_certified"))]
277pub const fn panic_str_2015(expr: &str) -> ! {
278 panic_display(&expr);
279}
280
281#[inline]
282#[track_caller]
283#[lang = "panic_display"] #[rustc_do_not_const_check] #[rustc_const_stable_indirect] #[cfg(not(feature = "ferrocene_certified"))]
287pub const fn panic_display<T: fmt::Display>(x: &T) -> ! {
288 panic_fmt(format_args!("{}", *x));
289}
290
291#[cfg_attr(not(feature = "panic_immediate_abort"), inline(never), cold, optimize(size))]
292#[cfg_attr(feature = "panic_immediate_abort", inline)]
293#[track_caller]
294#[lang = "panic_bounds_check"] #[cfg(not(feature = "ferrocene_certified"))]
296fn panic_bounds_check(index: usize, len: usize) -> ! {
297 if cfg!(feature = "panic_immediate_abort") {
298 super::intrinsics::abort()
299 }
300
301 panic!("index out of bounds: the len is {len} but the index is {index}")
302}
303
304#[cfg_attr(not(feature = "panic_immediate_abort"), inline(never), cold, optimize(size))]
305#[cfg_attr(feature = "panic_immediate_abort", inline)]
306#[track_caller]
307#[lang = "panic_misaligned_pointer_dereference"] #[rustc_nounwind] #[cfg(not(feature = "ferrocene_certified"))]
310fn panic_misaligned_pointer_dereference(required: usize, found: usize) -> ! {
311 if cfg!(feature = "panic_immediate_abort") {
312 super::intrinsics::abort()
313 }
314
315 panic_nounwind_fmt(
316 format_args!(
317 "misaligned pointer dereference: address must be a multiple of {required:#x} but is {found:#x}"
318 ),
319 false,
320 )
321}
322
323#[cfg_attr(not(feature = "panic_immediate_abort"), inline(never), cold, optimize(size))]
324#[cfg_attr(feature = "panic_immediate_abort", inline)]
325#[track_caller]
326#[lang = "panic_null_pointer_dereference"] #[rustc_nounwind] #[cfg(not(feature = "ferrocene_certified"))]
329fn panic_null_pointer_dereference() -> ! {
330 if cfg!(feature = "panic_immediate_abort") {
331 super::intrinsics::abort()
332 }
333
334 panic_nounwind_fmt(
335 format_args!("null pointer dereference occurred"),
336 false,
337 )
338}
339
340#[cfg_attr(not(feature = "panic_immediate_abort"), inline(never), cold, optimize(size))]
341#[cfg_attr(feature = "panic_immediate_abort", inline)]
342#[track_caller]
343#[lang = "panic_invalid_enum_construction"] #[rustc_nounwind] #[cfg(not(feature = "ferrocene_certified"))]
346fn panic_invalid_enum_construction(source: u128) -> ! {
347 if cfg!(feature = "panic_immediate_abort") {
348 super::intrinsics::abort()
349 }
350
351 panic_nounwind_fmt(
352 format_args!("trying to construct an enum from an invalid value {source:#x}"),
353 false,
354 )
355}
356
357#[cfg_attr(not(feature = "panic_immediate_abort"), inline(never), cold, optimize(size))]
365#[cfg_attr(feature = "panic_immediate_abort", inline)]
366#[lang = "panic_cannot_unwind"] #[rustc_nounwind]
368fn panic_cannot_unwind() -> ! {
369 panic_nounwind("panic in a function that cannot unwind")
371}
372
373#[cfg_attr(not(feature = "panic_immediate_abort"), inline(never), cold, optimize(size))]
381#[cfg_attr(feature = "panic_immediate_abort", inline)]
382#[lang = "panic_in_cleanup"] #[rustc_nounwind]
384#[cfg(not(feature = "ferrocene_certified"))]
385fn panic_in_cleanup() -> ! {
386 panic_nounwind_nobacktrace("panic in a destructor during cleanup")
388}
389
390#[lang = "const_panic_fmt"] #[rustc_const_stable_indirect] #[cfg(not(feature = "ferrocene_certified"))]
394pub const fn const_panic_fmt(fmt: fmt::Arguments<'_>) -> ! {
395 if let Some(msg) = fmt.as_str() {
396 panic_display(&msg);
398 } else {
399 unsafe { crate::hint::unreachable_unchecked() };
403 }
404}
405
406#[derive(Debug)]
407#[doc(hidden)]
408#[cfg(not(feature = "ferrocene_certified"))]
409pub enum AssertKind {
410 Eq,
411 Ne,
412 Match,
413}
414
415#[cfg_attr(not(feature = "panic_immediate_abort"), inline(never), cold, optimize(size))]
417#[cfg_attr(feature = "panic_immediate_abort", inline)]
418#[track_caller]
419#[doc(hidden)]
420#[cfg(not(feature = "ferrocene_certified"))]
421pub fn assert_failed<T, U>(
422 kind: AssertKind,
423 left: &T,
424 right: &U,
425 args: Option<fmt::Arguments<'_>>,
426) -> !
427where
428 T: fmt::Debug + ?Sized,
429 U: fmt::Debug + ?Sized,
430{
431 assert_failed_inner(kind, &left, &right, args)
432}
433
434#[cfg_attr(not(feature = "panic_immediate_abort"), inline(never), cold, optimize(size))]
436#[cfg_attr(feature = "panic_immediate_abort", inline)]
437#[track_caller]
438#[doc(hidden)]
439#[cfg(not(feature = "ferrocene_certified"))]
440pub fn assert_matches_failed<T: fmt::Debug + ?Sized>(
441 left: &T,
442 right: &str,
443 args: Option<fmt::Arguments<'_>>,
444) -> ! {
445 struct Pattern<'a>(&'a str);
447 impl fmt::Debug for Pattern<'_> {
448 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
449 f.write_str(self.0)
450 }
451 }
452 assert_failed_inner(AssertKind::Match, &left, &Pattern(right), args);
453}
454
455#[cfg_attr(not(feature = "panic_immediate_abort"), inline(never), cold, optimize(size))]
457#[cfg_attr(feature = "panic_immediate_abort", inline)]
458#[track_caller]
459#[cfg(not(feature = "ferrocene_certified"))]
460fn assert_failed_inner(
461 kind: AssertKind,
462 left: &dyn fmt::Debug,
463 right: &dyn fmt::Debug,
464 args: Option<fmt::Arguments<'_>>,
465) -> ! {
466 let op = match kind {
467 AssertKind::Eq => "==",
468 AssertKind::Ne => "!=",
469 AssertKind::Match => "matches",
470 };
471
472 match args {
473 Some(args) => panic!(
474 r#"assertion `left {op} right` failed: {args}
475 left: {left:?}
476 right: {right:?}"#
477 ),
478 None => panic!(
479 r#"assertion `left {op} right` failed
480 left: {left:?}
481 right: {right:?}"#
482 ),
483 }
484}