1#![allow(dead_code)]
25#![unstable(
26 feature = "panic_internals",
27 reason = "internal details of the implementation of the `panic!` and related macros",
28 issue = "none"
29)]
30
31use crate::fmt;
32use crate::intrinsics::const_eval_select;
33use crate::panic::{Location, PanicInfo};
34
35#[cfg(feature = "panic_immediate_abort")]
36compile_error!(
37 "panic_immediate_abort is now a real panic strategy! \
38 Enable it with `panic = \"immediate-abort\"` in Cargo.toml, \
39 or with the compiler flags `-Zunstable-options -Cpanic=immediate-abort`. \
40 In both cases, you still need to build core, e.g. with `-Zbuild-std`"
41);
42
43#[cfg_attr(not(panic = "immediate-abort"), inline(never), cold)]
55#[cfg_attr(panic = "immediate-abort", inline)]
56#[track_caller]
57#[lang = "panic_fmt"] #[rustc_do_not_const_check] #[rustc_const_stable_indirect] pub const fn panic_fmt(fmt: fmt::Arguments<'_>) -> ! {
61 if cfg!(panic = "immediate-abort") {
62 super::intrinsics::abort_immediate()
63 }
64
65 unsafe extern "Rust" {
68 #[lang = "panic_impl"]
69 fn panic_impl(pi: &PanicInfo<'_>) -> !;
70 }
71
72 let pi = PanicInfo::new(
73 &fmt,
74 Location::caller(),
75 true,
76 false,
77 );
78
79 unsafe { panic_impl(&pi) }
81}
82
83#[cfg_attr(not(panic = "immediate-abort"), inline(never), cold)]
87#[cfg_attr(panic = "immediate-abort", inline)]
88#[track_caller]
89#[rustc_nounwind]
93#[rustc_const_stable_indirect] #[rustc_allow_const_fn_unstable(const_eval_select)]
95pub const fn panic_nounwind_fmt(fmt: fmt::Arguments<'_>, force_no_backtrace: bool) -> ! {
96 const_eval_select!(
97 @capture { fmt: fmt::Arguments<'_>, force_no_backtrace: bool } -> !:
98 if const #[track_caller] {
99 panic_fmt(fmt)
101 } else #[track_caller] {
102 if cfg!(panic = "immediate-abort") {
103 super::intrinsics::abort_immediate()
104 }
105
106 unsafe extern "Rust" {
109 #[lang = "panic_impl"]
110 fn panic_impl(pi: &PanicInfo<'_>) -> !;
111 }
112
113 let pi = PanicInfo::new(
115 &fmt,
116 Location::caller(),
117 false,
118 force_no_backtrace,
119 );
120
121 unsafe { panic_impl(&pi) }
123 }
124 )
125}
126
127#[cfg_attr(not(panic = "immediate-abort"), inline(never), cold)]
134#[cfg_attr(panic = "immediate-abort", inline)]
135#[track_caller]
136#[rustc_const_stable_indirect] #[lang = "panic"] pub const fn panic(expr: &'static str) -> ! {
139 panic_fmt(fmt::Arguments::from_str(expr));
151}
152
153macro_rules! panic_const {
162 ($($lang:ident = $message:expr,)+) => {
163 $(
164 #[cfg_attr(not(panic = "immediate-abort"), inline(never), cold)]
169 #[cfg_attr(panic = "immediate-abort", inline)]
170 #[track_caller]
171 #[rustc_const_stable_indirect] #[lang = stringify!($lang)]
173 pub const fn $lang() -> ! {
174 panic_fmt(fmt::Arguments::from_str($message));
176 }
177 )+
178 }
179}
180
181#[allow(missing_docs)]
186pub mod panic_const {
187 use super::*;
188 panic_const! {
189 panic_const_add_overflow = "attempt to add with overflow",
190 panic_const_sub_overflow = "attempt to subtract with overflow",
191 panic_const_mul_overflow = "attempt to multiply with overflow",
192 panic_const_div_overflow = "attempt to divide with overflow",
193 panic_const_rem_overflow = "attempt to calculate the remainder with overflow",
194 panic_const_neg_overflow = "attempt to negate with overflow",
195 panic_const_shr_overflow = "attempt to shift right with overflow",
196 panic_const_shl_overflow = "attempt to shift left with overflow",
197 panic_const_div_by_zero = "attempt to divide by zero",
198 panic_const_rem_by_zero = "attempt to calculate the remainder with a divisor of zero",
199 panic_const_coroutine_resumed = "coroutine resumed after completion",
200 panic_const_async_fn_resumed = "`async fn` resumed after completion",
201 panic_const_async_gen_fn_resumed = "`async gen fn` resumed after completion",
202 panic_const_gen_fn_none = "`gen fn` should just keep returning `None` after completion",
203 panic_const_coroutine_resumed_panic = "coroutine resumed after panicking",
204 panic_const_async_fn_resumed_panic = "`async fn` resumed after panicking",
205 panic_const_async_gen_fn_resumed_panic = "`async gen fn` resumed after panicking",
206 panic_const_gen_fn_none_panic = "`gen fn` should just keep returning `None` after panicking",
207 }
208 panic_const! {
211 panic_const_coroutine_resumed_drop = "coroutine resumed after async drop",
212 panic_const_async_fn_resumed_drop = "`async fn` resumed after async drop",
213 panic_const_async_gen_fn_resumed_drop = "`async gen fn` resumed after async drop",
214 panic_const_gen_fn_none_drop = "`gen fn` resumed after async drop",
215 }
216}
217
218#[cfg_attr(not(panic = "immediate-abort"), inline(never), cold)]
221#[cfg_attr(panic = "immediate-abort", inline)]
222#[lang = "panic_nounwind"] #[rustc_nounwind]
224#[rustc_const_stable_indirect] pub const fn panic_nounwind(expr: &'static str) -> ! {
226 panic_nounwind_fmt(fmt::Arguments::from_str(expr), false);
227}
228
229#[cfg_attr(not(panic = "immediate-abort"), inline(never), cold)]
231#[cfg_attr(panic = "immediate-abort", inline)]
232#[rustc_nounwind]
233pub fn panic_nounwind_nobacktrace(expr: &'static str) -> ! {
234 panic_nounwind_fmt(fmt::Arguments::from_str(expr), true);
235}
236
237#[inline]
239#[track_caller]
240#[rustc_diagnostic_item = "unreachable_display"] pub fn unreachable_display<T: fmt::Display>(x: &T) -> ! {
242 panic_fmt(format_args!("internal error: entered unreachable code: {}", *x));
243}
244
245#[inline]
248#[track_caller]
249#[rustc_diagnostic_item = "panic_str_2015"]
250#[rustc_const_stable_indirect] pub const fn panic_str_2015(expr: &str) -> ! {
252 panic_display(&expr);
253}
254
255#[inline]
257#[track_caller]
258#[lang = "panic_display"] #[rustc_do_not_const_check] #[rustc_const_stable_indirect] pub const fn panic_display<T: fmt::Display>(x: &T) -> ! {
262 panic_fmt(format_args!("{}", *x));
263}
264
265#[cfg_attr(not(panic = "immediate-abort"), inline(never), cold, optimize(size))]
266#[cfg_attr(panic = "immediate-abort", inline)]
267#[track_caller]
268#[lang = "panic_bounds_check"] fn panic_bounds_check(index: usize, len: usize) -> ! {
270 if cfg!(panic = "immediate-abort") {
271 super::intrinsics::abort_immediate()
272 }
273
274 panic!("index out of bounds: the len is {len} but the index is {index}")
275}
276
277#[cfg_attr(not(panic = "immediate-abort"), inline(never), cold, optimize(size))]
278#[cfg_attr(panic = "immediate-abort", inline)]
279#[track_caller]
280#[lang = "panic_misaligned_pointer_dereference"] #[rustc_nounwind] fn panic_misaligned_pointer_dereference(required: usize, found: usize) -> ! {
283 if cfg!(panic = "immediate-abort") {
284 super::intrinsics::abort_immediate()
285 }
286
287 panic_nounwind_fmt(
288 format_args!(
289 "misaligned pointer dereference: address must be a multiple of {required:#x} but is {found:#x}"
290 ),
291 false,
292 )
293}
294
295#[cfg_attr(not(panic = "immediate-abort"), inline(never), cold, optimize(size))]
296#[cfg_attr(panic = "immediate-abort", inline)]
297#[track_caller]
298#[lang = "panic_null_pointer_dereference"] #[rustc_nounwind] fn panic_null_pointer_dereference() -> ! {
301 if cfg!(panic = "immediate-abort") {
302 super::intrinsics::abort_immediate()
303 }
304
305 panic_nounwind_fmt(
306 format_args!("null pointer dereference occurred"),
307 false,
308 )
309}
310
311#[cfg_attr(not(panic = "immediate-abort"), inline(never), cold, optimize(size))]
312#[cfg_attr(panic = "immediate-abort", inline)]
313#[track_caller]
314#[lang = "panic_null_reference_constructed"] #[rustc_nounwind] fn panic_null_reference_constructed() -> ! {
317 if cfg!(panic = "immediate-abort") {
318 super::intrinsics::abort_immediate()
319 }
320
321 panic_nounwind_fmt(format_args!("null reference produced"), false)
322}
323
324#[cfg_attr(not(panic = "immediate-abort"), inline(never), cold, optimize(size))]
325#[cfg_attr(panic = "immediate-abort", inline)]
326#[track_caller]
327#[lang = "panic_invalid_enum_construction"] #[rustc_nounwind] fn panic_invalid_enum_construction(source: u128) -> ! {
330 if cfg!(panic = "immediate-abort") {
331 super::intrinsics::abort_immediate()
332 }
333
334 panic_nounwind_fmt(
335 format_args!("trying to construct an enum from an invalid value {source:#x}"),
336 false,
337 )
338}
339
340#[cfg_attr(not(panic = "immediate-abort"), inline(never), cold, optimize(size))]
348#[cfg_attr(panic = "immediate-abort", inline)]
349#[lang = "panic_cannot_unwind"] #[rustc_nounwind]
351fn panic_cannot_unwind() -> ! {
352 panic_nounwind("panic in a function that cannot unwind")
354}
355
356#[cfg_attr(not(panic = "immediate-abort"), inline(never), cold, optimize(size))]
364#[cfg_attr(panic = "immediate-abort", inline)]
365#[lang = "panic_in_cleanup"] #[rustc_nounwind]
367fn panic_in_cleanup() -> ! {
368 panic_nounwind_nobacktrace("panic in a destructor during cleanup")
370}
371
372#[lang = "const_panic_fmt"] #[rustc_const_stable_indirect] pub const fn const_panic_fmt(fmt: fmt::Arguments<'_>) -> ! {
376 if let Some(msg) = fmt.as_str() {
377 panic_display(&msg);
379 } else {
380 unsafe { crate::hint::unreachable_unchecked() };
384 }
385}
386
387#[derive(Debug)]
388#[doc(hidden)]
389pub enum AssertKind {
390 Eq,
391 Ne,
392 Match,
393}
394
395#[cfg_attr(not(panic = "immediate-abort"), inline(never), cold, optimize(size))]
397#[cfg_attr(panic = "immediate-abort", inline)]
398#[track_caller]
399#[doc(hidden)]
400pub fn assert_failed<T, U>(
401 kind: AssertKind,
402 left: &T,
403 right: &U,
404 args: Option<fmt::Arguments<'_>>,
405) -> !
406where
407 T: fmt::Debug + ?Sized,
408 U: fmt::Debug + ?Sized,
409{
410 assert_failed_inner(kind, &left, &right, args)
411}
412
413#[cfg_attr(not(panic = "immediate-abort"), inline(never), cold, optimize(size))]
415#[cfg_attr(panic = "immediate-abort", inline)]
416#[track_caller]
417#[doc(hidden)]
418pub fn assert_matches_failed<T: fmt::Debug + ?Sized>(
419 left: &T,
420 right: &str,
421 args: Option<fmt::Arguments<'_>>,
422) -> ! {
423 struct Pattern<'a>(&'a str);
425 impl fmt::Debug for Pattern<'_> {
426 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
427 f.write_str(self.0)
428 }
429 }
430 assert_failed_inner(AssertKind::Match, &left, &Pattern(right), args);
431}
432
433#[cfg_attr(not(panic = "immediate-abort"), inline(never), cold, optimize(size))]
435#[cfg_attr(panic = "immediate-abort", inline)]
436#[track_caller]
437fn assert_failed_inner(
438 kind: AssertKind,
439 left: &dyn fmt::Debug,
440 right: &dyn fmt::Debug,
441 args: Option<fmt::Arguments<'_>>,
442) -> ! {
443 let op = match kind {
444 AssertKind::Eq => "==",
445 AssertKind::Ne => "!=",
446 AssertKind::Match => "matches",
447 };
448
449 match args {
450 Some(args) => panic!(
451 r"assertion `left {op} right` failed: {args}
452 left: {left:?}
453 right: {right:?}"
454 ),
455 None => panic!(
456 r"assertion `left {op} right` failed
457 left: {left:?}
458 right: {right:?}"
459 ),
460 }
461}
462
463#[cfg_attr(not(panic = "immediate-abort"), inline(never), cold)]
465#[cfg_attr(panic = "immediate-abort", inline)]
466#[track_caller]
467pub const fn panic_default_msg() -> ! {
468 panic("explicit panic")
469}
470
471#[cfg_attr(not(panic = "immediate-abort"), inline(never), cold)]
473#[cfg_attr(panic = "immediate-abort", inline)]
474#[track_caller]
475pub const fn unreachable_default_msg() -> ! {
476 panic("internal error: entered unreachable code")
477}
478
479#[cfg_attr(not(panic = "immediate-abort"), inline(never), cold)]
481#[cfg_attr(panic = "immediate-abort", inline)]
482#[track_caller]
483pub const fn unimplemented_default_msg() -> ! {
484 panic("not implemented")
485}
486
487#[cfg_attr(not(panic = "immediate-abort"), inline(never), cold)]
489#[cfg_attr(panic = "immediate-abort", inline)]
490#[track_caller]
491pub const fn todo_default_msg() -> ! {
492 panic("not yet implemented")
493}