Skip to content

Commit 3440566

Browse files
committed
Auto merge of rust-lang#115670 - Zoxc:outline-panic-macro-1, r=Mark-Simulacrum
Partially outline code inside the panic! macro This outlines code inside the panic! macro in some cases. This is split out from rust-lang#115562 to exclude changes to rustc.
2 parents c9b74f4 + d236202 commit 3440566

File tree

2 files changed

+56
-1
lines changed

2 files changed

+56
-1
lines changed

core/src/panic.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ pub macro panic_2015 {
4747
#[allow_internal_unstable(core_panic, const_format_args)]
4848
#[rustc_diagnostic_item = "core_panic_2021_macro"]
4949
#[rustc_macro_transparency = "semitransparent"]
50+
#[cfg(any(bootstrap, feature = "panic_immediate_abort"))]
5051
pub macro panic_2021 {
5152
() => (
5253
$crate::panicking::panic("explicit panic")
@@ -62,6 +63,50 @@ pub macro panic_2021 {
6263
}),
6364
}
6465

66+
#[doc(hidden)]
67+
#[unstable(feature = "edition_panic", issue = "none", reason = "use panic!() instead")]
68+
#[allow_internal_unstable(
69+
core_panic,
70+
core_intrinsics,
71+
const_dispatch,
72+
const_eval_select,
73+
const_format_args,
74+
rustc_attrs
75+
)]
76+
#[rustc_diagnostic_item = "core_panic_2021_macro"]
77+
#[rustc_macro_transparency = "semitransparent"]
78+
#[cfg(not(any(bootstrap, feature = "panic_immediate_abort")))]
79+
pub macro panic_2021 {
80+
() => ({
81+
// Create a function so that the argument for `track_caller`
82+
// can be moved inside if possible.
83+
#[cold]
84+
#[track_caller]
85+
#[inline(never)]
86+
const fn panic_cold_explicit() -> ! {
87+
$crate::panicking::panic_explicit()
88+
}
89+
panic_cold_explicit();
90+
}),
91+
// Special-case the single-argument case for const_panic.
92+
("{}", $arg:expr $(,)?) => ({
93+
#[cold]
94+
#[track_caller]
95+
#[inline(never)]
96+
#[rustc_const_panic_str] // enforce a &&str argument in const-check and hook this by const-eval
97+
#[rustc_do_not_const_check] // hooked by const-eval
98+
const fn panic_cold_display<T: $crate::fmt::Display>(arg: &T) -> ! {
99+
$crate::panicking::panic_display(arg)
100+
}
101+
panic_cold_display(&$arg);
102+
}),
103+
($($t:tt)+) => ({
104+
// Semicolon to prevent temporaries inside the formatting machinery from
105+
// being considered alive in the caller after the panic_fmt call.
106+
$crate::panicking::panic_fmt($crate::const_format_args!($($t)+));
107+
}),
108+
}
109+
65110
#[doc(hidden)]
66111
#[unstable(feature = "edition_panic", issue = "none", reason = "use unreachable!() instead")]
67112
#[allow_internal_unstable(core_panic)]

core/src/panicking.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,14 @@ pub const fn panic_str(expr: &str) -> ! {
152152
panic_display(&expr);
153153
}
154154

155+
#[track_caller]
156+
#[cfg_attr(not(feature = "panic_immediate_abort"), inline(never), cold)]
157+
#[cfg_attr(feature = "panic_immediate_abort", inline)]
158+
#[rustc_const_unstable(feature = "core_panic", issue = "none")]
159+
pub const fn panic_explicit() -> ! {
160+
panic_display(&"explicit panic");
161+
}
162+
155163
#[inline]
156164
#[track_caller]
157165
#[rustc_diagnostic_item = "unreachable_display"] // needed for `non-fmt-panics` lint
@@ -161,8 +169,10 @@ pub fn unreachable_display<T: fmt::Display>(x: &T) -> ! {
161169

162170
#[inline]
163171
#[track_caller]
164-
#[lang = "panic_display"] // needed for const-evaluated panics
165172
#[rustc_do_not_const_check] // hooked by const-eval
173+
#[cfg_attr(bootstrap, lang = "panic_display")]
174+
// enforce a &&str argument in const-check and hook this by const-eval
175+
#[cfg_attr(not(bootstrap), rustc_const_panic_str)]
166176
#[rustc_const_unstable(feature = "core_panic", issue = "none")]
167177
pub const fn panic_display<T: fmt::Display>(x: &T) -> ! {
168178
panic_fmt(format_args!("{}", *x));

0 commit comments

Comments
 (0)