]> git.lizzy.rs Git - rust.git/blob - src/libcore/panicking.rs
Rollup merge of #70038 - DutchGhost:const-forget-tests, r=RalfJung
[rust.git] / src / libcore / panicking.rs
1 //! Panic support for libcore
2 //!
3 //! The core library cannot define panicking, but it does *declare* panicking. This
4 //! means that the functions inside of libcore are allowed to panic, but to be
5 //! useful an upstream crate must define panicking for libcore to use. The current
6 //! interface for panicking is:
7 //!
8 //! ```
9 //! fn panic_impl(pi: &core::panic::PanicInfo<'_>) -> !
10 //! # { loop {} }
11 //! ```
12 //!
13 //! This definition allows for panicking with any general message, but it does not
14 //! allow for failing with a `Box<Any>` value. (`PanicInfo` just contains a `&(dyn Any + Send)`,
15 //! for which we fill in a dummy value in `PanicInfo::internal_constructor`.)
16 //! The reason for this is that libcore is not allowed to allocate.
17 //!
18 //! This module contains a few other panicking functions, but these are just the
19 //! necessary lang items for the compiler. All panics are funneled through this
20 //! one function. The actual symbol is declared through the `#[panic_handler]` attribute.
21
22 // ignore-tidy-undocumented-unsafe
23
24 #![allow(dead_code, missing_docs)]
25 #![unstable(
26     feature = "core_panic",
27     reason = "internal details of the implementation of the `panic!` \
28               and related macros",
29     issue = "none"
30 )]
31
32 use crate::fmt;
33 use crate::panic::{Location, PanicInfo};
34
35 /// The underlying implementation of libcore's `panic!` macro when no formatting is used.
36 #[cold]
37 // never inline unless panic_immediate_abort to avoid code
38 // bloat at the call sites as much as possible
39 #[cfg_attr(not(feature = "panic_immediate_abort"), inline(never))]
40 #[track_caller]
41 #[lang = "panic"] // needed by codegen for panic on overflow and other `Assert` MIR terminators
42 pub fn panic(expr: &str) -> ! {
43     if cfg!(feature = "panic_immediate_abort") {
44         unsafe { super::intrinsics::abort() }
45     }
46
47     // Use Arguments::new_v1 instead of format_args!("{}", expr) to potentially
48     // reduce size overhead. The format_args! macro uses str's Display trait to
49     // write expr, which calls Formatter::pad, which must accommodate string
50     // truncation and padding (even though none is used here). Using
51     // Arguments::new_v1 may allow the compiler to omit Formatter::pad from the
52     // output binary, saving up to a few kilobytes.
53     #[cfg(not(bootstrap))]
54     panic_fmt(fmt::Arguments::new_v1(&[expr], &[]));
55     #[cfg(bootstrap)]
56     panic_fmt(fmt::Arguments::new_v1(&[expr], &[]), Location::caller());
57 }
58
59 #[cfg(not(bootstrap))]
60 #[cold]
61 #[cfg_attr(not(feature = "panic_immediate_abort"), inline(never))]
62 #[track_caller]
63 #[lang = "panic_bounds_check"] // needed by codegen for panic on OOB array/slice access
64 fn panic_bounds_check(index: usize, len: usize) -> ! {
65     if cfg!(feature = "panic_immediate_abort") {
66         unsafe { super::intrinsics::abort() }
67     }
68
69     panic!("index out of bounds: the len is {} but the index is {}", len, index)
70 }
71
72 // For bootstrap, we need a variant with the old argument order, and a corresponding
73 // `panic_fmt`.
74 #[cfg(bootstrap)]
75 #[cold]
76 #[cfg_attr(not(feature = "panic_immediate_abort"), inline(never))]
77 #[lang = "panic_bounds_check"] // needed by codegen for panic on OOB array/slice access
78 fn panic_bounds_check(location: &Location<'_>, index: usize, len: usize) -> ! {
79     if cfg!(feature = "panic_immediate_abort") {
80         unsafe { super::intrinsics::abort() }
81     }
82
83     panic_fmt(
84         format_args!("index out of bounds: the len is {} but the index is {}", len, index),
85         location,
86     )
87 }
88
89 /// The underlying implementation of libcore's `panic!` macro when formatting is used.
90 #[cold]
91 #[cfg_attr(not(feature = "panic_immediate_abort"), inline(never))]
92 #[cfg_attr(feature = "panic_immediate_abort", inline)]
93 #[cfg_attr(not(bootstrap), track_caller)]
94 pub fn panic_fmt(fmt: fmt::Arguments<'_>, #[cfg(bootstrap)] location: &Location<'_>) -> ! {
95     if cfg!(feature = "panic_immediate_abort") {
96         unsafe { super::intrinsics::abort() }
97     }
98
99     // NOTE This function never crosses the FFI boundary; it's a Rust-to-Rust call
100     // that gets resolved to the `#[panic_handler]` function.
101     extern "Rust" {
102         #[lang = "panic_impl"]
103         fn panic_impl(pi: &PanicInfo<'_>) -> !;
104     }
105
106     #[cfg(bootstrap)]
107     let pi = PanicInfo::internal_constructor(Some(&fmt), location);
108     #[cfg(not(bootstrap))]
109     let pi = PanicInfo::internal_constructor(Some(&fmt), Location::caller());
110
111     unsafe { panic_impl(&pi) }
112 }