|
29 | 29 | use crate::fmt; |
30 | 30 | use crate::panic::{Location, PanicInfo}; |
31 | 31 |
|
| 32 | +// First we define the two main entry points that all panics go through. |
| 33 | +// In the end both are just convenience wrappers around `panic_impl`. |
| 34 | + |
| 35 | +/// The entry point for panicking with a formatted message. |
| 36 | +/// |
| 37 | +/// This is designed to reduce the amount of code required at the call |
| 38 | +/// site as much as possible (so that `panic!()` has as low an impact |
| 39 | +/// on (e.g.) the inlining of other functions as possible), by moving |
| 40 | +/// the actual formatting into this shared place. |
| 41 | +#[cold] |
| 42 | +// If panic_immediate_abort, inline the abort call, |
| 43 | +// otherwise avoid inlining because of it is cold path. |
| 44 | +#[cfg_attr(not(feature = "panic_immediate_abort"), inline(never))] |
| 45 | +#[cfg_attr(feature = "panic_immediate_abort", inline)] |
| 46 | +#[track_caller] |
| 47 | +#[lang = "panic_fmt"] // needed for const-evaluated panics |
| 48 | +#[rustc_do_not_const_check] // hooked by const-eval |
| 49 | +#[rustc_const_unstable(feature = "core_panic", issue = "none")] |
| 50 | +pub const fn panic_fmt(fmt: fmt::Arguments<'_>) -> ! { |
| 51 | + if cfg!(feature = "panic_immediate_abort") { |
| 52 | + super::intrinsics::abort() |
| 53 | + } |
| 54 | + |
| 55 | + // NOTE This function never crosses the FFI boundary; it's a Rust-to-Rust call |
| 56 | + // that gets resolved to the `#[panic_handler]` function. |
| 57 | + extern "Rust" { |
| 58 | + #[lang = "panic_impl"] |
| 59 | + fn panic_impl(pi: &PanicInfo<'_>) -> !; |
| 60 | + } |
| 61 | + |
| 62 | + let pi = PanicInfo::internal_constructor(Some(&fmt), Location::caller(), true); |
| 63 | + |
| 64 | + // SAFETY: `panic_impl` is defined in safe Rust code and thus is safe to call. |
| 65 | + unsafe { panic_impl(&pi) } |
| 66 | +} |
| 67 | + |
| 68 | +/// Like panic_fmt, but without unwinding and track_caller to reduce the impact on codesize. |
| 69 | +/// Also just works on `str`, as a `fmt::Arguments` needs more space to be passed. |
| 70 | +#[cold] |
| 71 | +#[cfg_attr(not(feature = "panic_immediate_abort"), inline(never))] |
| 72 | +#[cfg_attr(feature = "panic_immediate_abort", inline)] |
| 73 | +#[cfg_attr(not(bootstrap), rustc_nounwind)] |
| 74 | +#[cfg_attr(bootstrap, rustc_allocator_nounwind)] |
| 75 | +pub fn panic_str_nounwind(msg: &'static str) -> ! { |
| 76 | + if cfg!(feature = "panic_immediate_abort") { |
| 77 | + super::intrinsics::abort() |
| 78 | + } |
| 79 | + |
| 80 | + // NOTE This function never crosses the FFI boundary; it's a Rust-to-Rust call |
| 81 | + // that gets resolved to the `#[panic_handler]` function. |
| 82 | + extern "Rust" { |
| 83 | + #[lang = "panic_impl"] |
| 84 | + fn panic_impl(pi: &PanicInfo<'_>) -> !; |
| 85 | + } |
| 86 | + |
| 87 | + // PanicInfo with the `can_unwind` flag set to false forces an abort. |
| 88 | + let pieces = [msg]; |
| 89 | + let fmt = fmt::Arguments::new_v1(&pieces, &[]); |
| 90 | + let pi = PanicInfo::internal_constructor(Some(&fmt), Location::caller(), false); |
| 91 | + |
| 92 | + // SAFETY: `panic_impl` is defined in safe Rust code and thus is safe to call. |
| 93 | + unsafe { panic_impl(&pi) } |
| 94 | +} |
| 95 | + |
| 96 | +// Next we define a bunch of higher-level wrappers that all bottom out in the two core functions |
| 97 | +// above. |
| 98 | + |
32 | 99 | /// The underlying implementation of libcore's `panic!` macro when no formatting is used. |
33 | 100 | #[cold] |
34 | 101 | // never inline unless panic_immediate_abort to avoid code |
@@ -97,67 +164,6 @@ fn panic_no_unwind() -> ! { |
97 | 164 | panic_str_nounwind("panic in a function that cannot unwind") |
98 | 165 | } |
99 | 166 |
|
100 | | -/// Like panic_fmt, but without unwinding and track_caller to reduce the impact on codesize. |
101 | | -/// Also just works on `str`, as a `fmt::Arguments` needs more space to be passed. |
102 | | -#[cold] |
103 | | -#[cfg_attr(not(feature = "panic_immediate_abort"), inline(never))] |
104 | | -#[cfg_attr(feature = "panic_immediate_abort", inline)] |
105 | | -#[cfg_attr(not(bootstrap), rustc_nounwind)] |
106 | | -#[cfg_attr(bootstrap, rustc_allocator_nounwind)] |
107 | | -pub fn panic_str_nounwind(msg: &'static str) -> ! { |
108 | | - if cfg!(feature = "panic_immediate_abort") { |
109 | | - super::intrinsics::abort() |
110 | | - } |
111 | | - |
112 | | - // NOTE This function never crosses the FFI boundary; it's a Rust-to-Rust call |
113 | | - // that gets resolved to the `#[panic_handler]` function. |
114 | | - extern "Rust" { |
115 | | - #[lang = "panic_impl"] |
116 | | - fn panic_impl(pi: &PanicInfo<'_>) -> !; |
117 | | - } |
118 | | - |
119 | | - // PanicInfo with the `can_unwind` flag set to false forces an abort. |
120 | | - let pieces = [msg]; |
121 | | - let fmt = fmt::Arguments::new_v1(&pieces, &[]); |
122 | | - let pi = PanicInfo::internal_constructor(Some(&fmt), Location::caller(), false); |
123 | | - |
124 | | - // SAFETY: `panic_impl` is defined in safe Rust code and thus is safe to call. |
125 | | - unsafe { panic_impl(&pi) } |
126 | | -} |
127 | | - |
128 | | -/// The entry point for panicking with a formatted message. |
129 | | -/// |
130 | | -/// This is designed to reduce the amount of code required at the call |
131 | | -/// site as much as possible (so that `panic!()` has as low an impact |
132 | | -/// on (e.g.) the inlining of other functions as possible), by moving |
133 | | -/// the actual formatting into this shared place. |
134 | | -#[cold] |
135 | | -// If panic_immediate_abort, inline the abort call, |
136 | | -// otherwise avoid inlining because of it is cold path. |
137 | | -#[cfg_attr(not(feature = "panic_immediate_abort"), inline(never))] |
138 | | -#[cfg_attr(feature = "panic_immediate_abort", inline)] |
139 | | -#[track_caller] |
140 | | -#[lang = "panic_fmt"] // needed for const-evaluated panics |
141 | | -#[rustc_do_not_const_check] // hooked by const-eval |
142 | | -#[rustc_const_unstable(feature = "core_panic", issue = "none")] |
143 | | -pub const fn panic_fmt(fmt: fmt::Arguments<'_>) -> ! { |
144 | | - if cfg!(feature = "panic_immediate_abort") { |
145 | | - super::intrinsics::abort() |
146 | | - } |
147 | | - |
148 | | - // NOTE This function never crosses the FFI boundary; it's a Rust-to-Rust call |
149 | | - // that gets resolved to the `#[panic_handler]` function. |
150 | | - extern "Rust" { |
151 | | - #[lang = "panic_impl"] |
152 | | - fn panic_impl(pi: &PanicInfo<'_>) -> !; |
153 | | - } |
154 | | - |
155 | | - let pi = PanicInfo::internal_constructor(Some(&fmt), Location::caller(), true); |
156 | | - |
157 | | - // SAFETY: `panic_impl` is defined in safe Rust code and thus is safe to call. |
158 | | - unsafe { panic_impl(&pi) } |
159 | | -} |
160 | | - |
161 | 167 | /// This function is used instead of panic_fmt in const eval. |
162 | 168 | #[lang = "const_panic_fmt"] |
163 | 169 | #[rustc_const_unstable(feature = "core_panic", issue = "none")] |
|
0 commit comments