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
31use crate::fmt;
32use crate::intrinsics::const_eval_select;
33use crate::panic::{Location, PanicInfo};
34
35#[cfg(feature = "panic_immediate_abort")]
36const _: () = assert!(cfg!(panic = "abort"), "panic_immediate_abort requires -C panic=abort");
37
38#[cfg_attr(not(feature = "panic_immediate_abort"), inline(never), cold)]
50#[cfg_attr(feature = "panic_immediate_abort", inline)]
51#[track_caller]
52#[lang = "panic_fmt"] #[rustc_do_not_const_check] #[rustc_const_stable_indirect] pub const fn panic_fmt(fmt: fmt::Arguments<'_>) -> ! {
56 if cfg!(feature = "panic_immediate_abort") {
57 super::intrinsics::abort()
58 }
59
60 unsafe extern "Rust" {
63 #[lang = "panic_impl"]
64 fn panic_impl(pi: &PanicInfo<'_>) -> !;
65 }
66
67 let pi = PanicInfo::new(
68 &fmt,
69 Location::caller(),
70 true,
71 false,
72 );
73
74 unsafe { panic_impl(&pi) }
76}
77
78#[cfg_attr(not(feature = "panic_immediate_abort"), inline(never), cold)]
82#[cfg_attr(feature = "panic_immediate_abort", inline)]
83#[track_caller]
84#[rustc_nounwind]
88#[rustc_const_stable_indirect] #[rustc_allow_const_fn_unstable(const_eval_select)]
90pub const fn panic_nounwind_fmt(fmt: fmt::Arguments<'_>, force_no_backtrace: bool) -> ! {
91 const_eval_select!(
92 @capture { fmt: fmt::Arguments<'_>, force_no_backtrace: bool } -> !:
93 if const #[track_caller] {
94 panic_fmt(fmt)
96 } else #[track_caller] {
97 if cfg!(feature = "panic_immediate_abort") {
98 super::intrinsics::abort()
99 }
100
101 unsafe extern "Rust" {
104 #[lang = "panic_impl"]
105 fn panic_impl(pi: &PanicInfo<'_>) -> !;
106 }
107
108 let pi = PanicInfo::new(
110 &fmt,
111 Location::caller(),
112 false,
113 force_no_backtrace,
114 );
115
116 unsafe { panic_impl(&pi) }
118 }
119 )
120}
121
122#[cfg_attr(not(feature = "panic_immediate_abort"), inline(never), cold)]
129#[cfg_attr(feature = "panic_immediate_abort", inline)]
130#[track_caller]
131#[rustc_const_stable_indirect] #[lang = "panic"] pub const fn panic(expr: &'static str) -> ! {
134 panic_fmt(fmt::Arguments::new_const(&[expr]));
146}
147
148macro_rules! panic_const {
157 ($($lang:ident = $message:expr,)+) => {
158 $(
159 #[cfg_attr(not(feature = "panic_immediate_abort"), inline(never), cold)]
164 #[cfg_attr(feature = "panic_immediate_abort", inline)]
165 #[track_caller]
166 #[rustc_const_stable_indirect] #[lang = stringify!($lang)]
168 pub const fn $lang() -> ! {
169 panic_fmt(fmt::Arguments::new_const(&[$message]));
176 }
177 )+
178 }
179}
180
181pub mod panic_const {
186 use super::*;
187 panic_const! {
188 panic_const_add_overflow = "attempt to add with overflow",
189 panic_const_sub_overflow = "attempt to subtract with overflow",
190 panic_const_mul_overflow = "attempt to multiply with overflow",
191 panic_const_div_overflow = "attempt to divide with overflow",
192 panic_const_rem_overflow = "attempt to calculate the remainder with overflow",
193 panic_const_neg_overflow = "attempt to negate with overflow",
194 panic_const_shr_overflow = "attempt to shift right with overflow",
195 panic_const_shl_overflow = "attempt to shift left with overflow",
196 panic_const_div_by_zero = "attempt to divide by zero",
197 panic_const_rem_by_zero = "attempt to calculate the remainder with a divisor of zero",
198 panic_const_coroutine_resumed = "coroutine resumed after completion",
199 panic_const_async_fn_resumed = "`async fn` resumed after completion",
200 panic_const_async_gen_fn_resumed = "`async gen fn` resumed after completion",
201 panic_const_gen_fn_none = "`gen fn` should just keep returning `None` after completion",
202 panic_const_coroutine_resumed_panic = "coroutine resumed after panicking",
203 panic_const_async_fn_resumed_panic = "`async fn` resumed after panicking",
204 panic_const_async_gen_fn_resumed_panic = "`async gen fn` resumed after panicking",
205 panic_const_gen_fn_none_panic = "`gen fn` should just keep returning `None` after panicking",
206 }
207 panic_const! {
210 panic_const_coroutine_resumed_drop = "coroutine resumed after async drop",
211 panic_const_async_fn_resumed_drop = "`async fn` resumed after async drop",
212 panic_const_async_gen_fn_resumed_drop = "`async gen fn` resumed after async drop",
213 panic_const_gen_fn_none_drop = "`gen fn` resumed after async drop",
214 }
215}
216
217#[cfg_attr(not(feature = "panic_immediate_abort"), inline(never), cold)]
220#[cfg_attr(feature = "panic_immediate_abort", inline)]
221#[lang = "panic_nounwind"] #[rustc_nounwind]
223#[rustc_const_stable_indirect] pub const fn panic_nounwind(expr: &'static str) -> ! {
225 panic_nounwind_fmt(fmt::Arguments::new_const(&[expr]), false);
226}
227
228#[cfg_attr(not(feature = "panic_immediate_abort"), inline(never), cold)]
230#[cfg_attr(feature = "panic_immediate_abort", inline)]
231#[rustc_nounwind]
232pub fn panic_nounwind_nobacktrace(expr: &'static str) -> ! {
233 panic_nounwind_fmt(fmt::Arguments::new_const(&[expr]), true);
234}
235
236#[track_caller]
237#[cfg_attr(not(feature = "panic_immediate_abort"), inline(never), cold)]
238#[cfg_attr(feature = "panic_immediate_abort", inline)]
239#[rustc_const_stable_indirect] pub const fn panic_explicit() -> ! {
241 panic_display(&"explicit panic");
242}
243
244#[inline]
245#[track_caller]
246#[rustc_diagnostic_item = "unreachable_display"] pub fn unreachable_display<T: fmt::Display>(x: &T) -> ! {
248 panic_fmt(format_args!("internal error: entered unreachable code: {}", *x));
249}
250
251#[inline]
254#[track_caller]
255#[rustc_diagnostic_item = "panic_str_2015"]
256#[rustc_const_stable_indirect] pub const fn panic_str_2015(expr: &str) -> ! {
258 panic_display(&expr);
259}
260
261#[inline]
262#[track_caller]
263#[rustc_do_not_const_check] #[rustc_const_panic_str]
266#[rustc_const_stable_indirect] pub const fn panic_display<T: fmt::Display>(x: &T) -> ! {
268 panic_fmt(format_args!("{}", *x));
269}
270
271#[cfg_attr(not(feature = "panic_immediate_abort"), inline(never), cold, optimize(size))]
272#[cfg_attr(feature = "panic_immediate_abort", inline)]
273#[track_caller]
274#[lang = "panic_bounds_check"] fn panic_bounds_check(index: usize, len: usize) -> ! {
276 if cfg!(feature = "panic_immediate_abort") {
277 super::intrinsics::abort()
278 }
279
280 panic!("index out of bounds: the len is {len} but the index is {index}")
281}
282
283#[cfg_attr(not(feature = "panic_immediate_abort"), inline(never), cold, optimize(size))]
284#[cfg_attr(feature = "panic_immediate_abort", inline)]
285#[track_caller]
286#[lang = "panic_misaligned_pointer_dereference"] #[rustc_nounwind] fn panic_misaligned_pointer_dereference(required: usize, found: usize) -> ! {
289 if cfg!(feature = "panic_immediate_abort") {
290 super::intrinsics::abort()
291 }
292
293 panic_nounwind_fmt(
294 format_args!(
295 "misaligned pointer dereference: address must be a multiple of {required:#x} but is {found:#x}"
296 ),
297 false,
298 )
299}
300
301#[cfg_attr(not(feature = "panic_immediate_abort"), inline(never), cold, optimize(size))]
302#[cfg_attr(feature = "panic_immediate_abort", inline)]
303#[track_caller]
304#[lang = "panic_null_pointer_dereference"] #[rustc_nounwind] fn panic_null_pointer_dereference() -> ! {
307 if cfg!(feature = "panic_immediate_abort") {
308 super::intrinsics::abort()
309 }
310
311 panic_nounwind_fmt(
312 format_args!("null pointer dereference occurred"),
313 false,
314 )
315}
316
317#[cfg_attr(not(feature = "panic_immediate_abort"), inline(never), cold, optimize(size))]
318#[cfg_attr(feature = "panic_immediate_abort", inline)]
319#[track_caller]
320#[lang = "panic_invalid_enum_construction"] #[rustc_nounwind] fn panic_invalid_enum_construction(source: u128) -> ! {
323 if cfg!(feature = "panic_immediate_abort") {
324 super::intrinsics::abort()
325 }
326
327 panic_nounwind_fmt(
328 format_args!("trying to construct an enum from an invalid value {source:#x}"),
329 false,
330 )
331}
332
333#[cfg_attr(not(feature = "panic_immediate_abort"), inline(never), cold, optimize(size))]
341#[cfg_attr(feature = "panic_immediate_abort", inline)]
342#[lang = "panic_cannot_unwind"] #[rustc_nounwind]
344fn panic_cannot_unwind() -> ! {
345 panic_nounwind("panic in a function that cannot unwind")
347}
348
349#[cfg_attr(not(feature = "panic_immediate_abort"), inline(never), cold, optimize(size))]
357#[cfg_attr(feature = "panic_immediate_abort", inline)]
358#[lang = "panic_in_cleanup"] #[rustc_nounwind]
360fn panic_in_cleanup() -> ! {
361 panic_nounwind_nobacktrace("panic in a destructor during cleanup")
363}
364
365#[lang = "const_panic_fmt"] #[rustc_const_stable_indirect] pub const fn const_panic_fmt(fmt: fmt::Arguments<'_>) -> ! {
369 if let Some(msg) = fmt.as_str() {
370 panic_display(&msg);
372 } else {
373 unsafe { crate::hint::unreachable_unchecked() };
377 }
378}
379
380#[derive(Debug)]
381#[doc(hidden)]
382pub enum AssertKind {
383 Eq,
384 Ne,
385 Match,
386}
387
388#[cfg_attr(not(feature = "panic_immediate_abort"), inline(never), cold, optimize(size))]
390#[cfg_attr(feature = "panic_immediate_abort", inline)]
391#[track_caller]
392#[doc(hidden)]
393pub fn assert_failed<T, U>(
394 kind: AssertKind,
395 left: &T,
396 right: &U,
397 args: Option<fmt::Arguments<'_>>,
398) -> !
399where
400 T: fmt::Debug + ?Sized,
401 U: fmt::Debug + ?Sized,
402{
403 assert_failed_inner(kind, &left, &right, args)
404}
405
406#[cfg_attr(not(feature = "panic_immediate_abort"), inline(never), cold, optimize(size))]
408#[cfg_attr(feature = "panic_immediate_abort", inline)]
409#[track_caller]
410#[doc(hidden)]
411pub fn assert_matches_failed<T: fmt::Debug + ?Sized>(
412 left: &T,
413 right: &str,
414 args: Option<fmt::Arguments<'_>>,
415) -> ! {
416 struct Pattern<'a>(&'a str);
418 impl fmt::Debug for Pattern<'_> {
419 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
420 f.write_str(self.0)
421 }
422 }
423 assert_failed_inner(AssertKind::Match, &left, &Pattern(right), args);
424}
425
426#[cfg_attr(not(feature = "panic_immediate_abort"), inline(never), cold, optimize(size))]
428#[cfg_attr(feature = "panic_immediate_abort", inline)]
429#[track_caller]
430fn assert_failed_inner(
431 kind: AssertKind,
432 left: &dyn fmt::Debug,
433 right: &dyn fmt::Debug,
434 args: Option<fmt::Arguments<'_>>,
435) -> ! {
436 let op = match kind {
437 AssertKind::Eq => "==",
438 AssertKind::Ne => "!=",
439 AssertKind::Match => "matches",
440 };
441
442 match args {
443 Some(args) => panic!(
444 r#"assertion `left {op} right` failed: {args}
445 left: {left:?}
446 right: {right:?}"#
447 ),
448 None => panic!(
449 r#"assertion `left {op} right` failed
450 left: {left:?}
451 right: {right:?}"#
452 ),
453 }
454}