From 425404fe59bf047cd7400fb7c1fd7942f2bd040a Mon Sep 17 00:00:00 2001 From: Hanna Kruppe Date: Sun, 4 Oct 2026 20:47:09 +0200 Subject: [PATCH] outline some panics with constant messages --- library/core/src/macros/mod.rs | 2 +- library/core/src/panic.rs | 35 ++++++++++++++++++++++++++++++---- 2 files changed, 32 insertions(+), 5 deletions(-) diff --git a/library/core/src/macros/mod.rs b/library/core/src/macros/mod.rs index ab8fc47009186..3d5a15ab6d582 100644 --- a/library/core/src/macros/mod.rs +++ b/library/core/src/macros/mod.rs @@ -802,7 +802,7 @@ macro_rules! unreachable { #[rustc_diagnostic_opaque] macro_rules! unimplemented { () => { - $crate::panicking::panic("not implemented") + $crate::panic::unimplemented_noargs() }; ($($arg:tt)+) => { $crate::panic!("not implemented: {}", $crate::format_args!($($arg)+)) diff --git a/library/core/src/panic.rs b/library/core/src/panic.rs index 4fcd5f615e993..0d3d7ef7e7130 100644 --- a/library/core/src/panic.rs +++ b/library/core/src/panic.rs @@ -22,7 +22,7 @@ pub use self::unwind_safe::{AssertUnwindSafe, RefUnwindSafe, UnwindSafe}; #[rustc_macro_transparency = "semiopaque"] pub macro panic_2015 { () => ( - $crate::panicking::panic("explicit panic") + $crate::panic::panic_noargs() ), ($msg:literal $(,)?) => ( $crate::panicking::panic($msg) @@ -49,7 +49,7 @@ pub macro panic_2015 { #[rustc_macro_transparency = "semiopaque"] pub macro panic_2021 { () => ( - $crate::panicking::panic("explicit panic") + $crate::panic::panic_noargs() ), // Special-case the single-argument case for const_panic. ("{}", $arg:expr $(,)?) => ({ @@ -62,6 +62,15 @@ pub macro panic_2021 { }), } +#[doc(hidden)] +#[unstable(feature = "panic_internals", issue = "none")] +#[cold] +#[inline(never)] +#[track_caller] +pub const fn panic_noargs() -> ! { + crate::panicking::panic("explicit panic") +} + #[doc(hidden)] #[unstable(feature = "edition_panic", issue = "none", reason = "use unreachable!() instead")] #[allow_internal_unstable(panic_internals)] @@ -69,7 +78,7 @@ pub macro panic_2021 { #[rustc_macro_transparency = "semiopaque"] pub macro unreachable_2015 { () => ( - $crate::panicking::panic("internal error: entered unreachable code") + $crate::panic::unreachable_noargs() ), // Use of `unreachable_display` for non_fmt_panic lint. // NOTE: the message ("internal error ...") is embedded directly in unreachable_display @@ -87,13 +96,31 @@ pub macro unreachable_2015 { #[rustc_macro_transparency = "semiopaque"] pub macro unreachable_2021 { () => ( - $crate::panicking::panic("internal error: entered unreachable code") + $crate::panic::unreachable_noargs() ), ($($t:tt)+) => ( $crate::panic!("internal error: entered unreachable code: {}", $crate::format_args!($($t)+)) ), } +#[doc(hidden)] +#[unstable(feature = "panic_internals", issue = "none")] +#[cold] +#[inline(never)] +#[track_caller] +pub const fn unreachable_noargs() -> ! { + crate::panicking::panic("internal error: entered unreachable code") +} + +#[doc(hidden)] +#[unstable(feature = "panic_internals", issue = "none")] +#[cold] +#[inline(never)] +#[track_caller] +pub const fn unimplemented_noargs() -> ! { + crate::panicking::panic("not implemented") +} + /// Invokes a closure, aborting if the closure unwinds. /// /// When compiled with aborting panics, this function is effectively a no-op.