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_subset"))]
32use crate::fmt;
33use crate::intrinsics::const_eval_select;
34#[cfg(feature = "ferrocene_certified_runtime")]
35use crate::marker::PhantomData;
36use crate::panic::{Location, PanicInfo};
37
38#[cfg(not(feature = "ferrocene_certified_runtime"))]
40pub(crate) type PanicArguments<'a> = fmt::Arguments<'a>;
41#[cfg(feature = "ferrocene_certified_runtime")]
42#[allow(missing_debug_implementations)]
43pub struct PanicArguments<'a> {
44 pub(crate) inner: &'static str,
45 _marker: PhantomData<&'a ()>,
46}
47
48#[cfg(feature = "ferrocene_certified_runtime")]
49impl<'a> PanicArguments<'a> {
50 pub const fn from_str(inner: &'static str) -> Self {
51 Self { inner, _marker: PhantomData }
52 }
53
54 pub(crate) const fn as_str(&self) -> Option<&'static str> {
55 Some(self.inner)
56 }
57}
58
59#[cfg(feature = "panic_immediate_abort")]
60compile_error!(
61 "panic_immediate_abort is now a real panic strategy! \
62 Enable it with `panic = \"immediate-abort\"` in Cargo.toml, \
63 or with the compiler flags `-Zunstable-options -Cpanic=immediate-abort`. \
64 In both cases, you still need to build core, e.g. with `-Zbuild-std`"
65);
66
67#[cfg_attr(not(panic = "immediate-abort"), inline(never), cold)]
79#[cfg_attr(panic = "immediate-abort", inline)]
80#[track_caller]
81#[lang = "panic_fmt"] #[rustc_do_not_const_check] #[rustc_const_stable_indirect] pub const fn panic_fmt(fmt: PanicArguments<'_>) -> ! {
86 #[ferrocene::annotation(
87 "The `immediate-abort` behavior is not certified, we only support `abort`."
88 )]
89 if cfg!(panic = "immediate-abort") {
90 super::intrinsics::abort()
91 };
92
93 unsafe extern "Rust" {
96 #[lang = "panic_impl"]
97 fn panic_impl(pi: &PanicInfo<'_>) -> !;
98 }
99
100 let pi = PanicInfo::new(
101 &fmt,
102 Location::caller(),
103 true,
104 false,
105 );
106 unsafe { panic_impl(&pi) }
108}
109
110#[cfg_attr(not(panic = "immediate-abort"), inline(never), cold)]
114#[cfg_attr(panic = "immediate-abort", inline)]
115#[track_caller]
116#[rustc_nounwind]
120#[rustc_const_stable_indirect] #[rustc_allow_const_fn_unstable(const_eval_select)]
122#[ferrocene::annotation("Cannot be covered as it causes an unwinding panic")]
123pub const fn panic_nounwind_fmt(fmt: PanicArguments<'_>, _force_no_backtrace: bool) -> ! {
124 const_eval_select!(
125 @capture { fmt: PanicArguments<'_>, _force_no_backtrace: bool } -> !:
126 if const #[track_caller] {
127 panic_fmt(fmt)
129 } else #[track_caller] {
130 if cfg!(panic = "immediate-abort") {
131 super::intrinsics::abort()
132 }
133
134 unsafe extern "Rust" {
137 #[lang = "panic_impl"]
138 fn panic_impl(pi: &PanicInfo<'_>) -> !;
139 }
140
141 let pi = PanicInfo::new(
143 &fmt,
144 Location::caller(),
145 false,
146 _force_no_backtrace,
147 );
148
149 unsafe { panic_impl(&pi) }
151 }
152 )
153}
154
155#[cfg_attr(not(panic = "immediate-abort"), inline(never), cold)]
162#[cfg_attr(panic = "immediate-abort", inline)]
163#[track_caller]
164#[rustc_const_stable_indirect] #[lang = "panic"] pub const fn panic(expr: &'static str) -> ! {
167 panic_fmt(PanicArguments::from_str(expr));
179}
180
181macro_rules! panic_const {
190 ($($lang:ident = $message:expr,)+) => {
191 $(
192 #[cfg_attr(not(panic = "immediate-abort"), inline(never), cold)]
197 #[cfg_attr(panic = "immediate-abort", inline)]
198 #[track_caller]
199 #[rustc_const_stable_indirect] #[lang = stringify!($lang)]
201 #[ferrocene::annotation("Cannot be covered as this code cannot be reached during runtime.")]
202 pub const fn $lang() -> ! {
203 panic_fmt(PanicArguments::from_str($message));
205 }
206 )+
207 }
208}
209
210pub mod panic_const {
215 use super::*;
216 panic_const! {
217 panic_const_add_overflow = "attempt to add with overflow",
218 panic_const_sub_overflow = "attempt to subtract with overflow",
219 panic_const_mul_overflow = "attempt to multiply with overflow",
220 panic_const_div_overflow = "attempt to divide with overflow",
221 panic_const_rem_overflow = "attempt to calculate the remainder with overflow",
222 panic_const_neg_overflow = "attempt to negate with overflow",
223 panic_const_shr_overflow = "attempt to shift right with overflow",
224 panic_const_shl_overflow = "attempt to shift left with overflow",
225 panic_const_div_by_zero = "attempt to divide by zero",
226 panic_const_rem_by_zero = "attempt to calculate the remainder with a divisor of zero",
227 panic_const_coroutine_resumed = "coroutine resumed after completion",
228 panic_const_async_fn_resumed = "`async fn` resumed after completion",
229 panic_const_async_gen_fn_resumed = "`async gen fn` resumed after completion",
230 panic_const_gen_fn_none = "`gen fn` should just keep returning `None` after completion",
231 panic_const_coroutine_resumed_panic = "coroutine resumed after panicking",
232 panic_const_async_fn_resumed_panic = "`async fn` resumed after panicking",
233 panic_const_async_gen_fn_resumed_panic = "`async gen fn` resumed after panicking",
234 panic_const_gen_fn_none_panic = "`gen fn` should just keep returning `None` after panicking",
235 }
236 panic_const! {
239 panic_const_coroutine_resumed_drop = "coroutine resumed after async drop",
240 panic_const_async_fn_resumed_drop = "`async fn` resumed after async drop",
241 panic_const_async_gen_fn_resumed_drop = "`async gen fn` resumed after async drop",
242 panic_const_gen_fn_none_drop = "`gen fn` resumed after async drop",
243 }
244}
245
246#[cfg_attr(not(panic = "immediate-abort"), inline(never), cold)]
249#[cfg_attr(panic = "immediate-abort", inline)]
250#[lang = "panic_nounwind"] #[rustc_nounwind]
252#[rustc_const_stable_indirect] #[ferrocene::annotation("Cannot be covered as it causes an unwinding panic")]
254pub const fn panic_nounwind(expr: &'static str) -> ! {
255 panic_nounwind_fmt(PanicArguments::from_str(expr), false);
256}
257
258#[cfg_attr(not(panic = "immediate-abort"), inline(never), cold)]
260#[cfg_attr(panic = "immediate-abort", inline)]
261#[rustc_nounwind]
262pub fn panic_nounwind_nobacktrace(expr: &'static str) -> ! {
263 panic_nounwind_fmt(PanicArguments::from_str(expr), true);
264}
265
266#[inline]
267#[track_caller]
268#[rustc_diagnostic_item = "unreachable_display"] #[cfg(not(feature = "ferrocene_certified_runtime"))]
270pub fn unreachable_display<T: fmt::Display>(x: &T) -> ! {
271 panic_fmt(format_args!("internal error: entered unreachable code: {}", *x));
272}
273
274#[inline]
277#[track_caller]
278#[rustc_diagnostic_item = "panic_str_2015"]
279#[rustc_const_stable_indirect] #[cfg(not(feature = "ferrocene_certified_runtime"))]
281pub const fn panic_str_2015(expr: &str) -> ! {
282 panic_display(&expr);
283}
284
285#[cfg(feature = "ferrocene_certified_runtime")]
286#[inline]
287#[track_caller]
288#[lang = "panic_display"] #[rustc_do_not_const_check] #[rustc_const_stable_indirect] pub const fn panic_display(msg: &&'static str) -> ! {
292 panic_fmt(PanicArguments::from_str(*msg));
293}
294
295#[cfg(not(feature = "ferrocene_certified_runtime"))]
296#[inline]
297#[track_caller]
298#[lang = "panic_display"] #[rustc_do_not_const_check] #[rustc_const_stable_indirect] pub const fn panic_display<T: fmt::Display>(x: &T) -> ! {
302 panic_fmt(format_args!("{}", *x));
303}
304
305#[cfg_attr(feature = "ferrocene_certified_runtime", expect(unused_variables))]
306#[cfg_attr(not(panic = "immediate-abort"), inline(never), cold, optimize(size))]
307#[cfg_attr(panic = "immediate-abort", inline)]
308#[track_caller]
309#[lang = "panic_bounds_check"] fn 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(feature = "ferrocene_certified_runtime", expect(unused_variables))]
318#[cfg_attr(not(panic = "immediate-abort"), inline(never), cold, optimize(size))]
319#[cfg_attr(panic = "immediate-abort", inline)]
320#[track_caller]
321#[lang = "panic_misaligned_pointer_dereference"] #[rustc_nounwind] fn panic_misaligned_pointer_dereference(required: usize, found: usize) -> ! {
324 if cfg!(panic = "immediate-abort") {
325 super::intrinsics::abort()
326 }
327
328 #[cfg(not(feature = "ferrocene_certified_runtime"))]
329 panic_nounwind_fmt(
330 format_args!(
331 "misaligned pointer dereference: address must be a multiple of {required:#x} but is {found:#x}"
332 ),
333 false,
334 );
335 #[cfg(feature = "ferrocene_certified_runtime")]
336 panic_nounwind_fmt(
337 PanicArguments::from_str("misaligned pointer dereference"),
338 false,
339 );
340}
341
342#[cfg_attr(not(panic = "immediate-abort"), inline(never), cold, optimize(size))]
343#[cfg_attr(panic = "immediate-abort", inline)]
344#[track_caller]
345#[lang = "panic_null_pointer_dereference"] #[rustc_nounwind] fn panic_null_pointer_dereference() -> ! {
348 if cfg!(panic = "immediate-abort") {
349 super::intrinsics::abort()
350 }
351
352 panic_nounwind_fmt(
353 PanicArguments::from_str("null pointer dereference occurred"),
354 false,
355 )
356}
357
358#[cfg_attr(feature = "ferrocene_certified_runtime", expect(unused_variables))]
359#[cfg_attr(not(panic = "immediate-abort"), inline(never), cold, optimize(size))]
360#[cfg_attr(panic = "immediate-abort", inline)]
361#[track_caller]
362#[lang = "panic_invalid_enum_construction"] #[rustc_nounwind] #[cfg(not(feature = "ferrocene_subset"))]
365fn panic_invalid_enum_construction(source: u128) -> ! {
366 if cfg!(panic = "immediate-abort") {
367 super::intrinsics::abort()
368 }
369
370 #[cfg(not(feature = "ferrocene_certified_runtime"))]
371 panic_nounwind_fmt(
372 format_args!("trying to construct an enum from an invalid value {source:#x}"),
373 false,
374 );
375 #[cfg(feature = "ferrocene_certified_runtime")]
376 panic_nounwind_fmt(
377 PanicArguments::from_str("trying to construct an enum from an invalid value"),
378 false,
379 );
380}
381
382#[cfg_attr(not(panic = "immediate-abort"), inline(never), cold, optimize(size))]
390#[cfg_attr(panic = "immediate-abort", inline)]
391#[lang = "panic_cannot_unwind"] #[rustc_nounwind]
393#[ferrocene::annotation("Cannot be covered as it causes an unwinding panic")]
394fn panic_cannot_unwind() -> ! {
395 panic_nounwind("panic in a function that cannot unwind")
397}
398
399#[cfg_attr(not(panic = "immediate-abort"), inline(never), cold, optimize(size))]
407#[cfg_attr(panic = "immediate-abort", inline)]
408#[lang = "panic_in_cleanup"] #[rustc_nounwind]
410fn panic_in_cleanup() -> ! {
411 panic_nounwind_nobacktrace("panic in a destructor during cleanup")
413}
414
415#[lang = "const_panic_fmt"] #[rustc_const_stable_indirect] #[cfg(not(feature = "ferrocene_subset"))]
419pub const fn const_panic_fmt(fmt: PanicArguments<'_>) -> ! {
420 if let Some(msg) = fmt.as_str() {
421 panic_display(&msg);
423 } else {
424 unsafe { crate::hint::unreachable_unchecked() };
428 }
429}
430
431#[derive(Debug)]
432#[doc(hidden)]
433#[cfg(not(feature = "ferrocene_subset"))]
434pub enum AssertKind {
435 Eq,
436 Ne,
437 Match,
438}
439
440#[cfg_attr(not(panic = "immediate-abort"), inline(never), cold, optimize(size))]
442#[cfg_attr(panic = "immediate-abort", inline)]
443#[track_caller]
444#[doc(hidden)]
445#[cfg(not(feature = "ferrocene_subset"))]
446pub fn assert_failed<T, U>(
447 kind: AssertKind,
448 left: &T,
449 right: &U,
450 args: Option<fmt::Arguments<'_>>,
451) -> !
452where
453 T: fmt::Debug + ?Sized,
454 U: fmt::Debug + ?Sized,
455{
456 assert_failed_inner(kind, &left, &right, args)
457}
458
459#[cfg_attr(not(panic = "immediate-abort"), inline(never), cold, optimize(size))]
461#[cfg_attr(panic = "immediate-abort", inline)]
462#[track_caller]
463#[doc(hidden)]
464#[cfg(not(feature = "ferrocene_subset"))]
465pub fn assert_matches_failed<T: fmt::Debug + ?Sized>(
466 left: &T,
467 right: &str,
468 args: Option<fmt::Arguments<'_>>,
469) -> ! {
470 struct Pattern<'a>(&'a str);
472 impl fmt::Debug for Pattern<'_> {
473 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
474 f.write_str(self.0)
475 }
476 }
477 assert_failed_inner(AssertKind::Match, &left, &Pattern(right), args);
478}
479
480#[cfg_attr(feature = "ferrocene_certified_runtime", expect(unused_variables))]
482#[cfg_attr(not(panic = "immediate-abort"), inline(never), cold, optimize(size))]
483#[cfg_attr(panic = "immediate-abort", inline)]
484#[track_caller]
485#[cfg(not(feature = "ferrocene_subset"))]
486fn assert_failed_inner(
487 kind: AssertKind,
488 left: &dyn fmt::Debug,
489 right: &dyn fmt::Debug,
490 args: Option<fmt::Arguments<'_>>,
491) -> ! {
492 let op = match kind {
493 AssertKind::Eq => "==",
494 AssertKind::Ne => "!=",
495 AssertKind::Match => "matches",
496 };
497
498 match args {
499 Some(args) => panic!(
500 r#"assertion `left {op} right` failed: {args}
501 left: {left:?}
502 right: {right:?}"#
503 ),
504 None => panic!(
505 r#"assertion `left {op} right` failed
506 left: {left:?}
507 right: {right:?}"#
508 ),
509 }
510}