From ca165249248de59eca66492dd72b796a1f522630 Mon Sep 17 00:00:00 2001 From: Noah Goldstein Date: Tue, 23 Jul 2024 01:52:11 +0800 Subject: [PATCH 01/12] panic: Use local functions in `panic!` whenever possible This is basically extending the idea implemented in `panic_2021!`. By creating a cold/noinline function that wraps the setup for the call to the internal panic function moves more of the code-size cost of `panic!` to the cold path. For example: https://godbolt.org/z/T1ndrcq4d --- library/core/src/panic.rs | 102 +++++++++++++++++++++++++------------- library/std/src/panic.rs | 29 ++++++++++- 2 files changed, 94 insertions(+), 37 deletions(-) diff --git a/library/core/src/panic.rs b/library/core/src/panic.rs index 37c338dd9b778..8c8d2058b421f 100644 --- a/library/core/src/panic.rs +++ b/library/core/src/panic.rs @@ -23,19 +23,40 @@ pub use self::unwind_safe::{AssertUnwindSafe, RefUnwindSafe, UnwindSafe}; #[rustc_diagnostic_item = "core_panic_2015_macro"] #[rustc_macro_transparency = "semitransparent"] pub macro panic_2015 { - () => ( - $crate::panicking::panic("explicit panic") - ), - ($msg:literal $(,)?) => ( - $crate::panicking::panic($msg) - ), + () => ({ + #[cold] + #[track_caller] + #[inline(never)] + const fn panic_cold_explicit() -> ! { + $crate::panicking::panic("explicit panic"); + } + panic_cold_explicit(); + }), + // Special-case for string literal. + ($msg:literal $(,)?) => ({ + #[cold] + #[track_caller] + #[inline(never)] + const fn panic_cold_literal() -> ! { + $crate::panicking::panic($msg); + } + panic_cold_literal(); + }), // Use `panic_str_2015` instead of `panic_display::<&str>` for non_fmt_panic lint. ($msg:expr $(,)?) => ({ $crate::panicking::panic_str_2015($msg); }), // Special-case the single-argument case for const_panic. ("{}", $arg:expr $(,)?) => ({ - $crate::panicking::panic_display(&$arg); + #[cold] + #[track_caller] + #[inline(never)] + #[rustc_const_panic_str] // enforce a &&str argument in const-check and hook this by const-eval + #[rustc_do_not_const_check] // hooked by const-eval + const fn panic_cold_display(arg: &T) -> ! { + $crate::panicking::panic_display(arg) + } + panic_cold_display(&$arg); }), ($fmt:expr, $($arg:tt)+) => ({ // Semicolon to prevent temporaries inside the formatting machinery from @@ -44,27 +65,6 @@ pub macro panic_2015 { }), } -#[doc(hidden)] -#[unstable(feature = "edition_panic", issue = "none", reason = "use panic!() instead")] -#[allow_internal_unstable(panic_internals, const_format_args)] -#[rustc_diagnostic_item = "core_panic_2021_macro"] -#[rustc_macro_transparency = "semitransparent"] -#[cfg(feature = "panic_immediate_abort")] -pub macro panic_2021 { - () => ( - $crate::panicking::panic("explicit panic") - ), - // Special-case the single-argument case for const_panic. - ("{}", $arg:expr $(,)?) => ({ - $crate::panicking::panic_display(&$arg); - }), - ($($t:tt)+) => ({ - // Semicolon to prevent temporaries inside the formatting machinery from - // being considered alive in the caller after the panic_fmt call. - $crate::panicking::panic_fmt($crate::const_format_args!($($t)+)); - }), -} - #[doc(hidden)] #[unstable(feature = "edition_panic", issue = "none", reason = "use panic!() instead")] #[allow_internal_unstable( @@ -77,7 +77,6 @@ pub macro panic_2021 { )] #[rustc_diagnostic_item = "core_panic_2021_macro"] #[rustc_macro_transparency = "semitransparent"] -#[cfg(not(feature = "panic_immediate_abort"))] pub macro panic_2021 { () => ({ // Create a function so that the argument for `track_caller` @@ -115,9 +114,24 @@ pub macro panic_2021 { #[rustc_diagnostic_item = "unreachable_2015_macro"] #[rustc_macro_transparency = "semitransparent"] pub macro unreachable_2015 { - () => ( - $crate::panicking::panic("internal error: entered unreachable code") - ), + () => ({ + #[cold] + #[track_caller] + #[inline(never)] + const fn panic_cold_explicit() -> ! { + $crate::panicking::panic("internal error: entered unreachable code"); + } + panic_cold_explicit(); + }), + ($msg:literal $(,)?) => ({ + #[cold] + #[track_caller] + #[inline(never)] + const fn panic_cold_literal() -> ! { + $crate::panicking::panic($crate::concat!("internal error: entered unreachable code: ", $msg)); + } + panic_cold_literal(); + }), // Use of `unreachable_display` for non_fmt_panic lint. // NOTE: the message ("internal error ...") is embedded directly in unreachable_display ($msg:expr $(,)?) => ({ @@ -133,9 +147,27 @@ pub macro unreachable_2015 { #[allow_internal_unstable(panic_internals)] #[rustc_macro_transparency = "semitransparent"] pub macro unreachable_2021 { - () => ( - $crate::panicking::panic("internal error: entered unreachable code") - ), + () => ({ + #[cold] + #[track_caller] + #[inline(never)] + const fn panic_cold_explicit() -> ! { + $crate::panicking::panic("internal error: entered unreachable code"); + } + panic_cold_explicit(); + }), + // Special-case the single-argument case for const_panic. + ("{}", $arg:expr $(,)?) => ({ + #[cold] + #[track_caller] + #[inline(never)] + #[rustc_const_panic_str] // enforce a &&str argument in const-check and hook this by const-eval + #[rustc_do_not_const_check] // hooked by const-eval + const fn panic_cold_display(arg: &T) -> ! { + $crate::panicking::panic($crate::format_args!("internal error: entered unreachable code: {}", *arg)); + } + panic_cold_display(&$arg); + }), ($($t:tt)+) => ( $crate::panic!("internal error: entered unreachable code: {}", $crate::format_args!($($t)+)) ), diff --git a/library/std/src/panic.rs b/library/std/src/panic.rs index c5d1a893ee809..afc0988cc3b97 100644 --- a/library/std/src/panic.rs +++ b/library/std/src/panic.rs @@ -217,14 +217,39 @@ impl fmt::Display for PanicHookInfo<'_> { #[rustc_macro_transparency = "semitransparent"] pub macro panic_2015 { () => ({ - $crate::rt::begin_panic("explicit panic") + #[cold] + #[track_caller] + #[inline(never)] + const fn panic_cold_explicit() -> ! { + f + $crate::rt::begin_panic("explicit panic"); + } + panic_cold_explicit(); + }), + // Special-case for string literal. + ($msg:literal $(,)?) => ({ + #[cold] + #[track_caller] + #[inline(never)] + const fn panic_cold_literal() -> ! { + $crate::rt::begin_panic($msg); + } + panic_cold_literal(); }), ($msg:expr $(,)?) => ({ $crate::rt::begin_panic($msg); }), // Special-case the single-argument case for const_panic. ("{}", $arg:expr $(,)?) => ({ - $crate::rt::panic_display(&$arg); + #[cold] + #[track_caller] + #[inline(never)] + #[rustc_const_panic_str] // enforce a &&str argument in const-check and hook this by const-eval + #[rustc_do_not_const_check] // hooked by const-eval + const fn panic_cold_display(arg: &T) -> ! { + $crate::panicking::panic_display(arg) + } + panic_cold_display(&$arg); }), ($fmt:expr, $($arg:tt)+) => ({ // Semicolon to prevent temporaries inside the formatting machinery from From cc107fcfc10676bd7f86d87ae4f78c4625e79bb1 Mon Sep 17 00:00:00 2001 From: Noah Goldstein Date: Tue, 23 Jul 2024 15:13:38 +0800 Subject: [PATCH 02/12] Comments + Fixup to std::panic_2015 --- library/core/src/panic.rs | 22 +++++++++++++++++++++- library/std/src/panic.rs | 8 ++++++-- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/library/core/src/panic.rs b/library/core/src/panic.rs index 8c8d2058b421f..3d41c10a25135 100644 --- a/library/core/src/panic.rs +++ b/library/core/src/panic.rs @@ -23,6 +23,11 @@ pub use self::unwind_safe::{AssertUnwindSafe, RefUnwindSafe, UnwindSafe}; #[rustc_diagnostic_item = "core_panic_2015_macro"] #[rustc_macro_transparency = "semitransparent"] pub macro panic_2015 { + // For all cases where it is possible, wrap the actual call to the + // internal panic implementation function with a local no-inline + // cold function. This moves the codegen for setting up the + // arguments to the panic implementation function to the + // presumably cold panic path. () => ({ #[cold] #[track_caller] @@ -78,6 +83,11 @@ pub macro panic_2015 { #[rustc_diagnostic_item = "core_panic_2021_macro"] #[rustc_macro_transparency = "semitransparent"] pub macro panic_2021 { + // For all cases where it is possible, wrap the actual call to the + // internal panic implementation function with a local no-inline + // cold function. This moves the codegen for setting up the + // arguments to the panic implementation function to the + // presumably cold panic path. () => ({ // Create a function so that the argument for `track_caller` // can be moved inside if possible. @@ -114,6 +124,11 @@ pub macro panic_2021 { #[rustc_diagnostic_item = "unreachable_2015_macro"] #[rustc_macro_transparency = "semitransparent"] pub macro unreachable_2015 { + // For all cases where it is possible, wrap the actual call to the + // internal panic implementation function with a local no-inline + // cold function. This moves the codegen for setting up the + // arguments to the panic implementation function to the + // presumably cold panic path. () => ({ #[cold] #[track_caller] @@ -147,6 +162,11 @@ pub macro unreachable_2015 { #[allow_internal_unstable(panic_internals)] #[rustc_macro_transparency = "semitransparent"] pub macro unreachable_2021 { + // For all cases where it is possible, wrap the actual call to the + // internal panic implementation function with a local no-inline + // cold function. This moves the codegen for setting up the + // arguments to the panic implementation function to the + // presumably cold panic path. () => ({ #[cold] #[track_caller] @@ -164,7 +184,7 @@ pub macro unreachable_2021 { #[rustc_const_panic_str] // enforce a &&str argument in const-check and hook this by const-eval #[rustc_do_not_const_check] // hooked by const-eval const fn panic_cold_display(arg: &T) -> ! { - $crate::panicking::panic($crate::format_args!("internal error: entered unreachable code: {}", *arg)); + $crate::panicking::panic_fmt("internal error: entered unreachable code: {}", *arg); } panic_cold_display(&$arg); }), diff --git a/library/std/src/panic.rs b/library/std/src/panic.rs index afc0988cc3b97..01f2d2fd42042 100644 --- a/library/std/src/panic.rs +++ b/library/std/src/panic.rs @@ -216,12 +216,16 @@ impl fmt::Display for PanicHookInfo<'_> { #[cfg_attr(not(test), rustc_diagnostic_item = "std_panic_2015_macro")] #[rustc_macro_transparency = "semitransparent"] pub macro panic_2015 { + // For all cases where it is possible, wrap the actual call to the + // internal panic implementation function with a local no-inline + // cold function. This moves the codegen for setting up the + // arguments to the panic implementation function to the + // presumably cold panic path. () => ({ #[cold] #[track_caller] #[inline(never)] const fn panic_cold_explicit() -> ! { - f $crate::rt::begin_panic("explicit panic"); } panic_cold_explicit(); @@ -247,7 +251,7 @@ pub macro panic_2015 { #[rustc_const_panic_str] // enforce a &&str argument in const-check and hook this by const-eval #[rustc_do_not_const_check] // hooked by const-eval const fn panic_cold_display(arg: &T) -> ! { - $crate::panicking::panic_display(arg) + $crate::rt::panic_display(arg) } panic_cold_display(&$arg); }), From 11caff49a259da73d4847db33e738d881e24644c Mon Sep 17 00:00:00 2001 From: Noah Goldstein Date: Tue, 23 Jul 2024 15:39:03 +0800 Subject: [PATCH 03/12] Remove internal feature from std::panic --- library/std/src/panic.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/library/std/src/panic.rs b/library/std/src/panic.rs index 01f2d2fd42042..430f3893c198f 100644 --- a/library/std/src/panic.rs +++ b/library/std/src/panic.rs @@ -248,8 +248,6 @@ pub macro panic_2015 { #[cold] #[track_caller] #[inline(never)] - #[rustc_const_panic_str] // enforce a &&str argument in const-check and hook this by const-eval - #[rustc_do_not_const_check] // hooked by const-eval const fn panic_cold_display(arg: &T) -> ! { $crate::rt::panic_display(arg) } From f2f77f240f0f10d207ff21a6d3b076a6f4188a66 Mon Sep 17 00:00:00 2001 From: Noah Goldstein Date: Tue, 23 Jul 2024 16:11:55 +0800 Subject: [PATCH 04/12] make {} special case non-const in std::panic_2015 --- library/std/src/panic.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/std/src/panic.rs b/library/std/src/panic.rs index 430f3893c198f..57d612a25fb0b 100644 --- a/library/std/src/panic.rs +++ b/library/std/src/panic.rs @@ -248,7 +248,7 @@ pub macro panic_2015 { #[cold] #[track_caller] #[inline(never)] - const fn panic_cold_display(arg: &T) -> ! { + fn panic_cold_display(arg: &T) -> ! { $crate::rt::panic_display(arg) } panic_cold_display(&$arg); From a59b918a5045761a244f08d5691c55fcb0f31485 Mon Sep 17 00:00:00 2001 From: Noah Goldstein Date: Tue, 23 Jul 2024 17:03:57 +0800 Subject: [PATCH 05/12] fix core::unreachable_2021! --- library/core/src/panic.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/library/core/src/panic.rs b/library/core/src/panic.rs index 3d41c10a25135..ed1bf78e488e2 100644 --- a/library/core/src/panic.rs +++ b/library/core/src/panic.rs @@ -184,12 +184,12 @@ pub macro unreachable_2021 { #[rustc_const_panic_str] // enforce a &&str argument in const-check and hook this by const-eval #[rustc_do_not_const_check] // hooked by const-eval const fn panic_cold_display(arg: &T) -> ! { - $crate::panicking::panic_fmt("internal error: entered unreachable code: {}", *arg); + $crate::panicking::panic_fmt($crate::::format_args!("internal error: entered unreachable code: {}", *arg)); } panic_cold_display(&$arg); }), ($($t:tt)+) => ( - $crate::panic!("internal error: entered unreachable code: {}", $crate::format_args!($($t)+)) + $crate::panic!("internal error: entered unreachable code: {}", $crate::const_format_args!($($t)+)) ), } From af6260fe83e8e11382898c628e0fbd628d1ecaca Mon Sep 17 00:00:00 2001 From: Noah Goldstein Date: Tue, 23 Jul 2024 17:21:00 +0800 Subject: [PATCH 06/12] Allow const_format_args in core::unreachable_2021! --- library/core/src/panic.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/core/src/panic.rs b/library/core/src/panic.rs index ed1bf78e488e2..8fb7d344f5568 100644 --- a/library/core/src/panic.rs +++ b/library/core/src/panic.rs @@ -159,7 +159,7 @@ pub macro unreachable_2015 { #[doc(hidden)] #[unstable(feature = "edition_panic", issue = "none", reason = "use unreachable!() instead")] -#[allow_internal_unstable(panic_internals)] +#[allow_internal_unstable(panic_internals, const_format_args)] #[rustc_macro_transparency = "semitransparent"] pub macro unreachable_2021 { // For all cases where it is possible, wrap the actual call to the From 6a4f2264206271d1d010811359c2a3599fb4a1f7 Mon Sep 17 00:00:00 2001 From: Noah Goldstein Date: Wed, 24 Jul 2024 20:24:06 +0800 Subject: [PATCH 07/12] Fixup tests --- library/core/src/panic.rs | 56 ++++++++++++------- library/core/src/panicking.rs | 6 +- library/std/src/panic.rs | 27 +++++---- .../issue_101867.main.built.after.mir | 8 +-- ..._simplification.hello.GVN.panic-abort.diff | 4 +- ...simplification.hello.GVN.panic-unwind.diff | 4 +- .../gvn.wrap_unwrap.GVN.panic-abort.diff | 5 +- .../gvn.wrap_unwrap.GVN.panic-unwind.diff | 5 +- ...inline_diverging.g.Inline.panic-abort.diff | 4 +- ...nline_diverging.g.Inline.panic-unwind.diff | 4 +- ...implifyComparisonIntegral.panic-abort.diff | 4 +- ...mplifyComparisonIntegral.panic-unwind.diff | 4 +- ...fg-pre-optimizations.after.panic-abort.mir | 9 +-- ...g-pre-optimizations.after.panic-unwind.mir | 11 ++-- .../extra_arguments.stderr | 2 +- tests/ui/block-result/issue-5500.rs | 4 +- tests/ui/block-result/issue-5500.stderr | 9 +-- .../diverging-tuple-parts-39485.stderr | 15 ++--- tests/ui/never_type/issue-5500-1.rs | 14 ++--- ...e-47429-short-backtraces.legacy.run.stderr | 3 +- ...issue-47429-short-backtraces.v0.run.stderr | 3 +- .../panics/runtime-switch.legacy.run.stderr | 3 +- tests/ui/panics/runtime-switch.v0.run.stderr | 3 +- tests/ui/proc-macro/quote-debug.stdout | 10 +++- tests/ui/unpretty/let-else-hir.stdout | 9 ++- 25 files changed, 130 insertions(+), 96 deletions(-) diff --git a/library/core/src/panic.rs b/library/core/src/panic.rs index 8fb7d344f5568..49c6dadfdb11f 100644 --- a/library/core/src/panic.rs +++ b/library/core/src/panic.rs @@ -19,7 +19,14 @@ pub use self::unwind_safe::{AssertUnwindSafe, RefUnwindSafe, UnwindSafe}; #[doc(hidden)] #[unstable(feature = "edition_panic", issue = "none", reason = "use panic!() instead")] -#[allow_internal_unstable(panic_internals, const_format_args)] +#[allow_internal_unstable( + panic_internals, + core_intrinsics, + const_dispatch, + const_eval_select, + const_format_args, + rustc_attrs +)] #[rustc_diagnostic_item = "core_panic_2015_macro"] #[rustc_macro_transparency = "semitransparent"] pub macro panic_2015 { @@ -88,6 +95,8 @@ pub macro panic_2021 { // cold function. This moves the codegen for setting up the // arguments to the panic implementation function to the // presumably cold panic path. + // TODO: It would be nice to handle literals here, but there are + // some issues handling embedded format arguments. () => ({ // Create a function so that the argument for `track_caller` // can be moved inside if possible. @@ -120,7 +129,14 @@ pub macro panic_2021 { #[doc(hidden)] #[unstable(feature = "edition_panic", issue = "none", reason = "use unreachable!() instead")] -#[allow_internal_unstable(panic_internals)] +#[allow_internal_unstable( + panic_internals, + core_intrinsics, + const_dispatch, + const_eval_select, + const_format_args, + rustc_attrs +)] #[rustc_diagnostic_item = "unreachable_2015_macro"] #[rustc_macro_transparency = "semitransparent"] pub macro unreachable_2015 { @@ -133,19 +149,19 @@ pub macro unreachable_2015 { #[cold] #[track_caller] #[inline(never)] - const fn panic_cold_explicit() -> ! { + const fn unreachable_cold_explicit() -> ! { $crate::panicking::panic("internal error: entered unreachable code"); } - panic_cold_explicit(); + unreachable_cold_explicit(); }), ($msg:literal $(,)?) => ({ #[cold] #[track_caller] #[inline(never)] - const fn panic_cold_literal() -> ! { - $crate::panicking::panic($crate::concat!("internal error: entered unreachable code: ", $msg)); + const fn unreachable_cold_literal() -> ! { + $crate::panicking::unreachable_display(&$msg); } - panic_cold_literal(); + unreachable_cold_literal(); }), // Use of `unreachable_display` for non_fmt_panic lint. // NOTE: the message ("internal error ...") is embedded directly in unreachable_display @@ -159,22 +175,24 @@ pub macro unreachable_2015 { #[doc(hidden)] #[unstable(feature = "edition_panic", issue = "none", reason = "use unreachable!() instead")] -#[allow_internal_unstable(panic_internals, const_format_args)] +#[allow_internal_unstable( + panic_internals, + core_intrinsics, + const_dispatch, + const_eval_select, + const_format_args, + rustc_attrs +)] #[rustc_macro_transparency = "semitransparent"] pub macro unreachable_2021 { - // For all cases where it is possible, wrap the actual call to the - // internal panic implementation function with a local no-inline - // cold function. This moves the codegen for setting up the - // arguments to the panic implementation function to the - // presumably cold panic path. () => ({ #[cold] #[track_caller] #[inline(never)] - const fn panic_cold_explicit() -> ! { + const fn unreachable_cold_explicit() -> ! { $crate::panicking::panic("internal error: entered unreachable code"); } - panic_cold_explicit(); + unreachable_cold_explicit(); }), // Special-case the single-argument case for const_panic. ("{}", $arg:expr $(,)?) => ({ @@ -183,13 +201,13 @@ pub macro unreachable_2021 { #[inline(never)] #[rustc_const_panic_str] // enforce a &&str argument in const-check and hook this by const-eval #[rustc_do_not_const_check] // hooked by const-eval - const fn panic_cold_display(arg: &T) -> ! { - $crate::panicking::panic_fmt($crate::::format_args!("internal error: entered unreachable code: {}", *arg)); + const fn unreachable_cold_display(arg: &T) -> ! { + $crate::panicking::unreachable_display(arg) } - panic_cold_display(&$arg); + unreachable_cold_display(&$arg); }), ($($t:tt)+) => ( - $crate::panic!("internal error: entered unreachable code: {}", $crate::const_format_args!($($t)+)) + $crate::panic!("internal error: entered unreachable code: {}", $crate::format_args!($($t)+)) ), } diff --git a/library/core/src/panicking.rs b/library/core/src/panicking.rs index 97fb1d6b7323f..e62040cf5edac 100644 --- a/library/core/src/panicking.rs +++ b/library/core/src/panicking.rs @@ -239,8 +239,12 @@ pub const fn panic_explicit() -> ! { #[inline] #[track_caller] +#[rustc_do_not_const_check] // hooked by const-eval +// enforce a &&str argument in const-check and hook this by const-eval +#[rustc_const_panic_str] +#[rustc_const_unstable(feature = "panic_internals", issue = "none")] #[rustc_diagnostic_item = "unreachable_display"] // needed for `non-fmt-panics` lint -pub fn unreachable_display(x: &T) -> ! { +pub const fn unreachable_display(x: &T) -> ! { panic_fmt(format_args!("internal error: entered unreachable code: {}", *x)); } diff --git a/library/std/src/panic.rs b/library/std/src/panic.rs index 57d612a25fb0b..6875642cabe5e 100644 --- a/library/std/src/panic.rs +++ b/library/std/src/panic.rs @@ -212,7 +212,15 @@ impl fmt::Display for PanicHookInfo<'_> { #[doc(hidden)] #[unstable(feature = "edition_panic", issue = "none", reason = "use panic!() instead")] -#[allow_internal_unstable(libstd_sys_internals, const_format_args, panic_internals, rt)] +#[allow_internal_unstable( + libstd_sys_internals, + const_format_args, + panic_internals, + rt, + const_dispatch, + const_eval_select, + rustc_attrs +)] #[cfg_attr(not(test), rustc_diagnostic_item = "std_panic_2015_macro")] #[rustc_macro_transparency = "semitransparent"] pub macro panic_2015 { @@ -221,6 +229,9 @@ pub macro panic_2015 { // cold function. This moves the codegen for setting up the // arguments to the panic implementation function to the // presumably cold panic path. + // TODO: It would be nice to handle literals here specially with a + // wrapper function, but unfortunately it results in unclear error + // messages when using panic(). () => ({ #[cold] #[track_caller] @@ -230,16 +241,6 @@ pub macro panic_2015 { } panic_cold_explicit(); }), - // Special-case for string literal. - ($msg:literal $(,)?) => ({ - #[cold] - #[track_caller] - #[inline(never)] - const fn panic_cold_literal() -> ! { - $crate::rt::begin_panic($msg); - } - panic_cold_literal(); - }), ($msg:expr $(,)?) => ({ $crate::rt::begin_panic($msg); }), @@ -248,7 +249,9 @@ pub macro panic_2015 { #[cold] #[track_caller] #[inline(never)] - fn panic_cold_display(arg: &T) -> ! { + #[rustc_const_panic_str] // enforce a &&str argument in const-check and hook this by const-eval + #[rustc_do_not_const_check] // hooked by const-eval + const fn panic_cold_display(arg: &T) -> ! { $crate::rt::panic_display(arg) } panic_cold_display(&$arg); diff --git a/tests/mir-opt/building/issue_101867.main.built.after.mir b/tests/mir-opt/building/issue_101867.main.built.after.mir index 5c50b3db5cad6..28d24cdaf9f12 100644 --- a/tests/mir-opt/building/issue_101867.main.built.after.mir +++ b/tests/mir-opt/building/issue_101867.main.built.after.mir @@ -8,8 +8,8 @@ fn main() -> () { let mut _0: (); let _1: std::option::Option as UserTypeProjection { base: UserType(0), projs: [] }; let mut _2: !; - let _3: (); - let mut _4: !; + let _3: !; + let _4: !; let mut _6: isize; scope 1 { debug x => _1; @@ -31,14 +31,12 @@ fn main() -> () { } bb1: { - StorageLive(_3); StorageLive(_4); - _4 = begin_panic::<&str>(const "explicit panic") -> bb8; + _4 = panic_cold_explicit() -> bb8; } bb2: { StorageDead(_4); - StorageDead(_3); unreachable; } diff --git a/tests/mir-opt/const_prop/control_flow_simplification.hello.GVN.panic-abort.diff b/tests/mir-opt/const_prop/control_flow_simplification.hello.GVN.panic-abort.diff index 886f9a68dd9d1..495aabb1115ea 100644 --- a/tests/mir-opt/const_prop/control_flow_simplification.hello.GVN.panic-abort.diff +++ b/tests/mir-opt/const_prop/control_flow_simplification.hello.GVN.panic-abort.diff @@ -4,7 +4,7 @@ fn hello() -> () { let mut _0: (); let mut _1: bool; - let mut _2: !; + let _2: !; bb0: { StorageLive(_1); @@ -14,7 +14,7 @@ } bb1: { - _2 = begin_panic::<&str>(const "explicit panic") -> unwind unreachable; + _2 = panic_cold_explicit() -> unwind unreachable; } bb2: { diff --git a/tests/mir-opt/const_prop/control_flow_simplification.hello.GVN.panic-unwind.diff b/tests/mir-opt/const_prop/control_flow_simplification.hello.GVN.panic-unwind.diff index cc53b213397d8..ece29268d18e5 100644 --- a/tests/mir-opt/const_prop/control_flow_simplification.hello.GVN.panic-unwind.diff +++ b/tests/mir-opt/const_prop/control_flow_simplification.hello.GVN.panic-unwind.diff @@ -4,7 +4,7 @@ fn hello() -> () { let mut _0: (); let mut _1: bool; - let mut _2: !; + let _2: !; bb0: { StorageLive(_1); @@ -14,7 +14,7 @@ } bb1: { - _2 = begin_panic::<&str>(const "explicit panic") -> unwind continue; + _2 = panic_cold_explicit() -> unwind continue; } bb2: { diff --git a/tests/mir-opt/gvn.wrap_unwrap.GVN.panic-abort.diff b/tests/mir-opt/gvn.wrap_unwrap.GVN.panic-abort.diff index a5c29c191ad5c..0fcef28fb83c2 100644 --- a/tests/mir-opt/gvn.wrap_unwrap.GVN.panic-abort.diff +++ b/tests/mir-opt/gvn.wrap_unwrap.GVN.panic-abort.diff @@ -9,6 +9,7 @@ let mut _4: isize; let _5: T; let mut _6: !; + let _7: !; scope 1 { debug y => _5; } @@ -31,8 +32,8 @@ } bb2: { - StorageLive(_6); - _6 = begin_panic::<&str>(const "explicit panic") -> unwind unreachable; + StorageLive(_7); + _7 = panic_cold_explicit() -> unwind unreachable; } bb3: { diff --git a/tests/mir-opt/gvn.wrap_unwrap.GVN.panic-unwind.diff b/tests/mir-opt/gvn.wrap_unwrap.GVN.panic-unwind.diff index 6f2e52482716d..f1f43dfd56f10 100644 --- a/tests/mir-opt/gvn.wrap_unwrap.GVN.panic-unwind.diff +++ b/tests/mir-opt/gvn.wrap_unwrap.GVN.panic-unwind.diff @@ -9,6 +9,7 @@ let mut _4: isize; let _5: T; let mut _6: !; + let _7: !; scope 1 { debug y => _5; } @@ -31,8 +32,8 @@ } bb2: { - StorageLive(_6); - _6 = begin_panic::<&str>(const "explicit panic") -> unwind continue; + StorageLive(_7); + _7 = panic_cold_explicit() -> unwind continue; } bb3: { diff --git a/tests/mir-opt/inline/inline_diverging.g.Inline.panic-abort.diff b/tests/mir-opt/inline/inline_diverging.g.Inline.panic-abort.diff index d675695eb1047..2053303098542 100644 --- a/tests/mir-opt/inline/inline_diverging.g.Inline.panic-abort.diff +++ b/tests/mir-opt/inline/inline_diverging.g.Inline.panic-abort.diff @@ -10,7 +10,7 @@ let mut _5: !; let _6: !; + scope 1 (inlined panic) { -+ let mut _7: !; ++ let _7: !; + } bb0: { @@ -36,7 +36,7 @@ StorageLive(_6); - _6 = panic() -> unwind unreachable; + StorageLive(_7); -+ _7 = begin_panic::<&str>(const "explicit panic") -> unwind unreachable; ++ _7 = panic_cold_explicit() -> unwind unreachable; } } diff --git a/tests/mir-opt/inline/inline_diverging.g.Inline.panic-unwind.diff b/tests/mir-opt/inline/inline_diverging.g.Inline.panic-unwind.diff index 1142616115fb7..5ba88fef2d91a 100644 --- a/tests/mir-opt/inline/inline_diverging.g.Inline.panic-unwind.diff +++ b/tests/mir-opt/inline/inline_diverging.g.Inline.panic-unwind.diff @@ -10,7 +10,7 @@ let mut _5: !; let _6: !; + scope 1 (inlined panic) { -+ let mut _7: !; ++ let _7: !; + } bb0: { @@ -36,7 +36,7 @@ StorageLive(_6); - _6 = panic() -> unwind continue; + StorageLive(_7); -+ _7 = begin_panic::<&str>(const "explicit panic") -> unwind continue; ++ _7 = panic_cold_explicit() -> unwind continue; } } diff --git a/tests/mir-opt/issue_76432.test.SimplifyComparisonIntegral.panic-abort.diff b/tests/mir-opt/issue_76432.test.SimplifyComparisonIntegral.panic-abort.diff index 1f88339b5861b..8abe09374eeea 100644 --- a/tests/mir-opt/issue_76432.test.SimplifyComparisonIntegral.panic-abort.diff +++ b/tests/mir-opt/issue_76432.test.SimplifyComparisonIntegral.panic-abort.diff @@ -9,7 +9,7 @@ let _4: [T; 3]; let mut _5: usize; let mut _6: bool; - let mut _10: !; + let _10: !; scope 1 { debug v => _2; let _7: &T; @@ -35,7 +35,7 @@ } bb1: { - _10 = core::panicking::panic(const "internal error: entered unreachable code") -> unwind unreachable; + _10 = unreachable_cold_explicit() -> unwind unreachable; } bb2: { diff --git a/tests/mir-opt/issue_76432.test.SimplifyComparisonIntegral.panic-unwind.diff b/tests/mir-opt/issue_76432.test.SimplifyComparisonIntegral.panic-unwind.diff index 19a581ba3f096..e5252812c3bd0 100644 --- a/tests/mir-opt/issue_76432.test.SimplifyComparisonIntegral.panic-unwind.diff +++ b/tests/mir-opt/issue_76432.test.SimplifyComparisonIntegral.panic-unwind.diff @@ -9,7 +9,7 @@ let _4: [T; 3]; let mut _5: usize; let mut _6: bool; - let mut _10: !; + let _10: !; scope 1 { debug v => _2; let _7: &T; @@ -35,7 +35,7 @@ } bb1: { - _10 = core::panicking::panic(const "internal error: entered unreachable code") -> unwind continue; + _10 = unreachable_cold_explicit() -> unwind continue; } bb2: { diff --git a/tests/mir-opt/no_drop_for_inactive_variant.unwrap.SimplifyCfg-pre-optimizations.after.panic-abort.mir b/tests/mir-opt/no_drop_for_inactive_variant.unwrap.SimplifyCfg-pre-optimizations.after.panic-abort.mir index fa6c6ce8e5750..7425e1afd0e4c 100644 --- a/tests/mir-opt/no_drop_for_inactive_variant.unwrap.SimplifyCfg-pre-optimizations.after.panic-abort.mir +++ b/tests/mir-opt/no_drop_for_inactive_variant.unwrap.SimplifyCfg-pre-optimizations.after.panic-abort.mir @@ -6,9 +6,10 @@ fn unwrap(_1: Option) -> T { let mut _2: isize; let _3: T; let mut _4: !; - let mut _5: isize; + let _5: !; let mut _6: isize; let mut _7: isize; + let mut _8: isize; scope 1 { debug x => _3; } @@ -23,8 +24,8 @@ fn unwrap(_1: Option) -> T { } bb2: { - StorageLive(_4); - _4 = begin_panic::<&str>(const "explicit panic") -> unwind unreachable; + StorageLive(_5); + _5 = panic_cold_explicit() -> unwind unreachable; } bb3: { @@ -32,7 +33,7 @@ fn unwrap(_1: Option) -> T { _3 = move ((_1 as Some).0: T); _0 = move _3; StorageDead(_3); - _5 = discriminant(_1); + _6 = discriminant(_1); return; } } diff --git a/tests/mir-opt/no_drop_for_inactive_variant.unwrap.SimplifyCfg-pre-optimizations.after.panic-unwind.mir b/tests/mir-opt/no_drop_for_inactive_variant.unwrap.SimplifyCfg-pre-optimizations.after.panic-unwind.mir index 54fec3c0f9847..ba58487705c00 100644 --- a/tests/mir-opt/no_drop_for_inactive_variant.unwrap.SimplifyCfg-pre-optimizations.after.panic-unwind.mir +++ b/tests/mir-opt/no_drop_for_inactive_variant.unwrap.SimplifyCfg-pre-optimizations.after.panic-unwind.mir @@ -6,9 +6,10 @@ fn unwrap(_1: Option) -> T { let mut _2: isize; let _3: T; let mut _4: !; - let mut _5: isize; + let _5: !; let mut _6: isize; let mut _7: isize; + let mut _8: isize; scope 1 { debug x => _3; } @@ -23,8 +24,8 @@ fn unwrap(_1: Option) -> T { } bb2: { - StorageLive(_4); - _4 = begin_panic::<&str>(const "explicit panic") -> bb4; + StorageLive(_5); + _5 = panic_cold_explicit() -> bb4; } bb3: { @@ -32,12 +33,12 @@ fn unwrap(_1: Option) -> T { _3 = move ((_1 as Some).0: T); _0 = move _3; StorageDead(_3); - _5 = discriminant(_1); + _6 = discriminant(_1); return; } bb4 (cleanup): { - _7 = discriminant(_1); + _8 = discriminant(_1); resume; } } diff --git a/tests/ui/argument-suggestions/extra_arguments.stderr b/tests/ui/argument-suggestions/extra_arguments.stderr index 8c95cc86a273a..7ed9ef471e69d 100644 --- a/tests/ui/argument-suggestions/extra_arguments.stderr +++ b/tests/ui/argument-suggestions/extra_arguments.stderr @@ -329,7 +329,7 @@ error[E0061]: this function takes 1 argument but 2 arguments were supplied --> $DIR/extra_arguments.rs:53:3 | LL | one_arg(1, panic!()); - | ^^^^^^^ -------- unexpected argument #2 + | ^^^^^^^ -------- unexpected argument #2 of type `!` | note: function defined here --> $DIR/extra_arguments.rs:2:4 diff --git a/tests/ui/block-result/issue-5500.rs b/tests/ui/block-result/issue-5500.rs index de7fd39a20caa..f42482bb9ff90 100644 --- a/tests/ui/block-result/issue-5500.rs +++ b/tests/ui/block-result/issue-5500.rs @@ -2,6 +2,6 @@ fn main() { &panic!() //~^ ERROR mismatched types //~| expected unit type `()` - //~| found reference `&_` - //~| expected `()`, found `&_` + //~| found reference `&!` + //~| expected `()`, found `&!` } diff --git a/tests/ui/block-result/issue-5500.stderr b/tests/ui/block-result/issue-5500.stderr index 71ca8ffe2a8e3..5466da5f736b7 100644 --- a/tests/ui/block-result/issue-5500.stderr +++ b/tests/ui/block-result/issue-5500.stderr @@ -4,15 +4,10 @@ error[E0308]: mismatched types LL | fn main() { | - expected `()` because of default return type LL | &panic!() - | ^^^^^^^^^ expected `()`, found `&_` + | ^^^^^^^^^ expected `()`, found `&!` | = note: expected unit type `()` - found reference `&_` -help: consider removing the borrow - | -LL - &panic!() -LL + panic!() - | + found reference `&!` error: aborting due to 1 previous error diff --git a/tests/ui/never_type/diverging-tuple-parts-39485.stderr b/tests/ui/never_type/diverging-tuple-parts-39485.stderr index ded13e2707f5f..cb98f63771e38 100644 --- a/tests/ui/never_type/diverging-tuple-parts-39485.stderr +++ b/tests/ui/never_type/diverging-tuple-parts-39485.stderr @@ -1,20 +1,13 @@ error[E0308]: mismatched types --> $DIR/diverging-tuple-parts-39485.rs:8:5 | +LL | fn g() { + | - help: try adding a return type: `-> &!` LL | &panic!() - | ^^^^^^^^^ expected `()`, found `&_` + | ^^^^^^^^^ expected `()`, found `&!` | = note: expected unit type `()` - found reference `&_` -help: a return type might be missing here - | -LL | fn g() -> _ { - | ++++ -help: consider removing the borrow - | -LL - &panic!() -LL + panic!() - | + found reference `&!` error[E0308]: mismatched types --> $DIR/diverging-tuple-parts-39485.rs:12:5 diff --git a/tests/ui/never_type/issue-5500-1.rs b/tests/ui/never_type/issue-5500-1.rs index 65617e36c6d6d..a459ebc963c6f 100644 --- a/tests/ui/never_type/issue-5500-1.rs +++ b/tests/ui/never_type/issue-5500-1.rs @@ -1,15 +1,15 @@ -// MIR doesn't generate an error because the assignment isn't reachable. This -// is OK because the test is here to check that the compiler doesn't ICE (cf. -// #5500). - -//@ check-pass +// the test is here to check that the compiler doesn't ICE (cf. #5500). struct TrieMapIterator<'a> { - node: &'a usize + node: &'a usize, } fn main() { let a = 5; - let _iter = TrieMapIterator{node: &a}; + let _iter = TrieMapIterator { node: &a }; _iter.node = &panic!() + //~^ ERROR mismatched types + //~| expected `&usize`, found `&!` + //~| expected reference `&usize` + //~| found reference `&!` } diff --git a/tests/ui/panics/issue-47429-short-backtraces.legacy.run.stderr b/tests/ui/panics/issue-47429-short-backtraces.legacy.run.stderr index dce91ce59e3a1..0e77066df7a8a 100644 --- a/tests/ui/panics/issue-47429-short-backtraces.legacy.run.stderr +++ b/tests/ui/panics/issue-47429-short-backtraces.legacy.run.stderr @@ -2,5 +2,6 @@ thread 'main' panicked at $DIR/issue-47429-short-backtraces.rs:23:5: explicit panic stack backtrace: 0: std::panicking::begin_panic - 1: issue_47429_short_backtraces::main + 1: issue_47429_short_backtraces::main::panic_cold_explicit + 2: issue_47429_short_backtraces::main note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace. diff --git a/tests/ui/panics/issue-47429-short-backtraces.v0.run.stderr b/tests/ui/panics/issue-47429-short-backtraces.v0.run.stderr index f458c7acb39fd..2ec0ed8e055a5 100644 --- a/tests/ui/panics/issue-47429-short-backtraces.v0.run.stderr +++ b/tests/ui/panics/issue-47429-short-backtraces.v0.run.stderr @@ -2,5 +2,6 @@ thread 'main' panicked at $DIR/issue-47429-short-backtraces.rs:23:5: explicit panic stack backtrace: 0: std::panicking::begin_panic::<&str> - 1: issue_47429_short_backtraces::main + 1: issue_47429_short_backtraces::main::panic_cold_explicit + 2: issue_47429_short_backtraces::main note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace. diff --git a/tests/ui/panics/runtime-switch.legacy.run.stderr b/tests/ui/panics/runtime-switch.legacy.run.stderr index bd05b6cc00fb1..cd127a2321c9a 100644 --- a/tests/ui/panics/runtime-switch.legacy.run.stderr +++ b/tests/ui/panics/runtime-switch.legacy.run.stderr @@ -2,5 +2,6 @@ thread 'main' panicked at $DIR/runtime-switch.rs:26:5: explicit panic stack backtrace: 0: std::panicking::begin_panic - 1: runtime_switch::main + 1: runtime_switch::main::panic_cold_explicit + 2: runtime_switch::main note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace. diff --git a/tests/ui/panics/runtime-switch.v0.run.stderr b/tests/ui/panics/runtime-switch.v0.run.stderr index 2078c356d5cd7..dbbe8d869ca45 100644 --- a/tests/ui/panics/runtime-switch.v0.run.stderr +++ b/tests/ui/panics/runtime-switch.v0.run.stderr @@ -2,5 +2,6 @@ thread 'main' panicked at $DIR/runtime-switch.rs:26:5: explicit panic stack backtrace: 0: std::panicking::begin_panic::<&str> - 1: runtime_switch::main + 1: runtime_switch::main::panic_cold_explicit + 2: runtime_switch::main note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace. diff --git a/tests/ui/proc-macro/quote-debug.stdout b/tests/ui/proc-macro/quote-debug.stdout index d84b4e051e872..d71adfa6b837e 100644 --- a/tests/ui/proc-macro/quote-debug.stdout +++ b/tests/ui/proc-macro/quote-debug.stdout @@ -33,7 +33,15 @@ fn main() { lit.set_span(crate::Span::recover_proc_macro_span(2)); lit } else { - ::core::panicking::panic("internal error: entered unreachable code") + { + #[cold] + #[track_caller] + #[inline(never)] + const fn unreachable_cold_explicit() -> ! { + ::core::panicking::panic("internal error: entered unreachable code"); + } + unreachable_cold_explicit(); + } } })), crate::TokenStream::from(crate::TokenTree::Punct(crate::Punct::new(';', diff --git a/tests/ui/unpretty/let-else-hir.stdout b/tests/ui/unpretty/let-else-hir.stdout index a2ffa5de5673c..827cb20f20fc8 100644 --- a/tests/ui/unpretty/let-else-hir.stdout +++ b/tests/ui/unpretty/let-else-hir.stdout @@ -12,7 +12,14 @@ fn foo(x: let Some(_) = x else { - { ::std::rt::begin_panic("explicit panic") } + { + #[cold] + #[track_caller] + #[inline(never)] + const fn panic_cold_explicit() + -> ! { ::std::rt::begin_panic("explicit panic"); } + panic_cold_explicit(); + } }; } fn main() { } From 6cbc3eb774cab4ca7dd197001ba14fde3b0765fa Mon Sep 17 00:00:00 2001 From: Noah Goldstein Date: Wed, 24 Jul 2024 20:27:27 +0800 Subject: [PATCH 08/12] Drop TODO --- library/core/src/panic.rs | 2 +- library/std/src/panic.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/library/core/src/panic.rs b/library/core/src/panic.rs index 49c6dadfdb11f..fe08802f2107e 100644 --- a/library/core/src/panic.rs +++ b/library/core/src/panic.rs @@ -95,7 +95,7 @@ pub macro panic_2021 { // cold function. This moves the codegen for setting up the // arguments to the panic implementation function to the // presumably cold panic path. - // TODO: It would be nice to handle literals here, but there are + // It would be nice to handle literals here, but there are // some issues handling embedded format arguments. () => ({ // Create a function so that the argument for `track_caller` diff --git a/library/std/src/panic.rs b/library/std/src/panic.rs index 6875642cabe5e..c0d05aff4fea2 100644 --- a/library/std/src/panic.rs +++ b/library/std/src/panic.rs @@ -229,7 +229,7 @@ pub macro panic_2015 { // cold function. This moves the codegen for setting up the // arguments to the panic implementation function to the // presumably cold panic path. - // TODO: It would be nice to handle literals here specially with a + // It would be nice to handle literals here specially with a // wrapper function, but unfortunately it results in unclear error // messages when using panic(). () => ({ From 7219ce031dc8dc6f35324bef092a2c8776eccba7 Mon Sep 17 00:00:00 2001 From: Noah Goldstein Date: Wed, 24 Jul 2024 21:43:39 +0800 Subject: [PATCH 09/12] Fixup clippy test --- .../tests/ui/missing_const_for_thread_local.stderr | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/tools/clippy/tests/ui/missing_const_for_thread_local.stderr b/src/tools/clippy/tests/ui/missing_const_for_thread_local.stderr index c143a37454f5b..27c3115ba9602 100644 --- a/src/tools/clippy/tests/ui/missing_const_for_thread_local.stderr +++ b/src/tools/clippy/tests/ui/missing_const_for_thread_local.stderr @@ -37,5 +37,13 @@ error: initializer for `thread_local` value can be made `const` LL | static PEEL_ME_MANY: i32 = { let x = 1; x * x }; | ^^^^^^^^^^^^^^^^^^^^ help: replace with: `const { { let x = 1; x * x } }` -error: aborting due to 6 previous errors +error: initializer for `thread_local` value can be made `const` + --> tests/ui/missing_const_for_thread_local.rs:64:55 + | +LL | static STATE_12637_UNREACHABLE: Cell = unreachable!(); + | ^^^^^^^^^^^^^^ + | + = note: this error originates in the macro `$crate::panic::unreachable_2021` which comes from the expansion of the macro `unreachable` (in Nightly builds, run with -Z macro-backtrace for more info) + +error: aborting due to 7 previous errors From 555a033a9bd78971e5010759d45cdb7751ca1dcc Mon Sep 17 00:00:00 2001 From: Noah Goldstein Date: Wed, 24 Jul 2024 22:45:10 +0800 Subject: [PATCH 10/12] Move explicit panic/unreachable into panicking.rs --- library/core/src/panic.rs | 34 +++---------------- library/core/src/panicking.rs | 14 ++++++++ .../gvn.wrap_unwrap.GVN.panic-abort.diff | 2 +- .../gvn.wrap_unwrap.GVN.panic-unwind.diff | 2 +- ...implifyComparisonIntegral.panic-abort.diff | 2 +- ...mplifyComparisonIntegral.panic-unwind.diff | 2 +- tests/ui/proc-macro/quote-debug.stdout | 10 +----- 7 files changed, 23 insertions(+), 43 deletions(-) diff --git a/library/core/src/panic.rs b/library/core/src/panic.rs index fe08802f2107e..b195917dbac1c 100644 --- a/library/core/src/panic.rs +++ b/library/core/src/panic.rs @@ -36,13 +36,7 @@ pub macro panic_2015 { // arguments to the panic implementation function to the // presumably cold panic path. () => ({ - #[cold] - #[track_caller] - #[inline(never)] - const fn panic_cold_explicit() -> ! { - $crate::panicking::panic("explicit panic"); - } - panic_cold_explicit(); + $crate::panicking::panic_cold_explicit(); }), // Special-case for string literal. ($msg:literal $(,)?) => ({ @@ -98,15 +92,7 @@ pub macro panic_2021 { // It would be nice to handle literals here, but there are // some issues handling embedded format arguments. () => ({ - // Create a function so that the argument for `track_caller` - // can be moved inside if possible. - #[cold] - #[track_caller] - #[inline(never)] - const fn panic_cold_explicit() -> ! { - $crate::panicking::panic_explicit() - } - panic_cold_explicit(); + $crate::panicking::panic_cold_explicit(); }), // Special-case the single-argument case for const_panic. ("{}", $arg:expr $(,)?) => ({ @@ -146,13 +132,7 @@ pub macro unreachable_2015 { // arguments to the panic implementation function to the // presumably cold panic path. () => ({ - #[cold] - #[track_caller] - #[inline(never)] - const fn unreachable_cold_explicit() -> ! { - $crate::panicking::panic("internal error: entered unreachable code"); - } - unreachable_cold_explicit(); + $crate::panicking::unreachable_cold_explicit(); }), ($msg:literal $(,)?) => ({ #[cold] @@ -186,13 +166,7 @@ pub macro unreachable_2015 { #[rustc_macro_transparency = "semitransparent"] pub macro unreachable_2021 { () => ({ - #[cold] - #[track_caller] - #[inline(never)] - const fn unreachable_cold_explicit() -> ! { - $crate::panicking::panic("internal error: entered unreachable code"); - } - unreachable_cold_explicit(); + $crate::panicking::unreachable_cold_explicit(); }), // Special-case the single-argument case for const_panic. ("{}", $arg:expr $(,)?) => ({ diff --git a/library/core/src/panicking.rs b/library/core/src/panicking.rs index e62040cf5edac..21047d01c990f 100644 --- a/library/core/src/panicking.rs +++ b/library/core/src/panicking.rs @@ -258,6 +258,20 @@ pub const fn panic_str_2015(expr: &str) -> ! { panic_display(&expr); } +#[cold] +#[track_caller] +#[inline(never)] +pub const fn panic_cold_explicit() -> ! { + panic("explicit panic"); +} + +#[cold] +#[track_caller] +#[inline(never)] +pub const fn unreachable_cold_explicit() -> ! { + panic("internal error: entered unreachable code"); +} + #[inline] #[track_caller] #[rustc_do_not_const_check] // hooked by const-eval diff --git a/tests/mir-opt/gvn.wrap_unwrap.GVN.panic-abort.diff b/tests/mir-opt/gvn.wrap_unwrap.GVN.panic-abort.diff index 0fcef28fb83c2..4028a3f51a6a5 100644 --- a/tests/mir-opt/gvn.wrap_unwrap.GVN.panic-abort.diff +++ b/tests/mir-opt/gvn.wrap_unwrap.GVN.panic-abort.diff @@ -33,7 +33,7 @@ bb2: { StorageLive(_7); - _7 = panic_cold_explicit() -> unwind unreachable; + _7 = wrap_unwrap::panic_cold_explicit() -> unwind unreachable; } bb3: { diff --git a/tests/mir-opt/gvn.wrap_unwrap.GVN.panic-unwind.diff b/tests/mir-opt/gvn.wrap_unwrap.GVN.panic-unwind.diff index f1f43dfd56f10..135ece0e2c2e9 100644 --- a/tests/mir-opt/gvn.wrap_unwrap.GVN.panic-unwind.diff +++ b/tests/mir-opt/gvn.wrap_unwrap.GVN.panic-unwind.diff @@ -33,7 +33,7 @@ bb2: { StorageLive(_7); - _7 = panic_cold_explicit() -> unwind continue; + _7 = wrap_unwrap::panic_cold_explicit() -> unwind continue; } bb3: { diff --git a/tests/mir-opt/issue_76432.test.SimplifyComparisonIntegral.panic-abort.diff b/tests/mir-opt/issue_76432.test.SimplifyComparisonIntegral.panic-abort.diff index 8abe09374eeea..0692ece348fc6 100644 --- a/tests/mir-opt/issue_76432.test.SimplifyComparisonIntegral.panic-abort.diff +++ b/tests/mir-opt/issue_76432.test.SimplifyComparisonIntegral.panic-abort.diff @@ -35,7 +35,7 @@ } bb1: { - _10 = unreachable_cold_explicit() -> unwind unreachable; + _10 = core::panicking::unreachable_cold_explicit() -> unwind unreachable; } bb2: { diff --git a/tests/mir-opt/issue_76432.test.SimplifyComparisonIntegral.panic-unwind.diff b/tests/mir-opt/issue_76432.test.SimplifyComparisonIntegral.panic-unwind.diff index e5252812c3bd0..eb45329a6cfdc 100644 --- a/tests/mir-opt/issue_76432.test.SimplifyComparisonIntegral.panic-unwind.diff +++ b/tests/mir-opt/issue_76432.test.SimplifyComparisonIntegral.panic-unwind.diff @@ -35,7 +35,7 @@ } bb1: { - _10 = unreachable_cold_explicit() -> unwind continue; + _10 = core::panicking::unreachable_cold_explicit() -> unwind continue; } bb2: { diff --git a/tests/ui/proc-macro/quote-debug.stdout b/tests/ui/proc-macro/quote-debug.stdout index d71adfa6b837e..e868619ad107e 100644 --- a/tests/ui/proc-macro/quote-debug.stdout +++ b/tests/ui/proc-macro/quote-debug.stdout @@ -33,15 +33,7 @@ fn main() { lit.set_span(crate::Span::recover_proc_macro_span(2)); lit } else { - { - #[cold] - #[track_caller] - #[inline(never)] - const fn unreachable_cold_explicit() -> ! { - ::core::panicking::panic("internal error: entered unreachable code"); - } - unreachable_cold_explicit(); - } + { ::core::panicking::unreachable_cold_explicit(); } } })), crate::TokenStream::from(crate::TokenTree::Punct(crate::Punct::new(';', From 12e23c48da25cbf241d0366350981aa08f97e8c6 Mon Sep 17 00:00:00 2001 From: Noah Goldstein Date: Thu, 25 Jul 2024 01:50:43 +0800 Subject: [PATCH 11/12] Fix clippy tests --- library/core/src/panic.rs | 12 ++++++------ library/core/src/panicking.rs | 2 ++ .../tests/ui/missing_const_for_thread_local.stderr | 10 +--------- 3 files changed, 9 insertions(+), 15 deletions(-) diff --git a/library/core/src/panic.rs b/library/core/src/panic.rs index b195917dbac1c..fd70114423f6e 100644 --- a/library/core/src/panic.rs +++ b/library/core/src/panic.rs @@ -131,9 +131,9 @@ pub macro unreachable_2015 { // cold function. This moves the codegen for setting up the // arguments to the panic implementation function to the // presumably cold panic path. - () => ({ - $crate::panicking::unreachable_cold_explicit(); - }), + () => ( + $crate::panicking::unreachable_cold_explicit() + ), ($msg:literal $(,)?) => ({ #[cold] #[track_caller] @@ -165,9 +165,9 @@ pub macro unreachable_2015 { )] #[rustc_macro_transparency = "semitransparent"] pub macro unreachable_2021 { - () => ({ - $crate::panicking::unreachable_cold_explicit(); - }), + () => ( + $crate::panicking::unreachable_cold_explicit() + ), // Special-case the single-argument case for const_panic. ("{}", $arg:expr $(,)?) => ({ #[cold] diff --git a/library/core/src/panicking.rs b/library/core/src/panicking.rs index 21047d01c990f..b5ac0da07c2b5 100644 --- a/library/core/src/panicking.rs +++ b/library/core/src/panicking.rs @@ -261,6 +261,7 @@ pub const fn panic_str_2015(expr: &str) -> ! { #[cold] #[track_caller] #[inline(never)] +#[rustc_const_unstable(feature = "panic_internals", issue = "none")] pub const fn panic_cold_explicit() -> ! { panic("explicit panic"); } @@ -268,6 +269,7 @@ pub const fn panic_cold_explicit() -> ! { #[cold] #[track_caller] #[inline(never)] +#[rustc_const_unstable(feature = "panic_internals", issue = "none")] pub const fn unreachable_cold_explicit() -> ! { panic("internal error: entered unreachable code"); } diff --git a/src/tools/clippy/tests/ui/missing_const_for_thread_local.stderr b/src/tools/clippy/tests/ui/missing_const_for_thread_local.stderr index 27c3115ba9602..c143a37454f5b 100644 --- a/src/tools/clippy/tests/ui/missing_const_for_thread_local.stderr +++ b/src/tools/clippy/tests/ui/missing_const_for_thread_local.stderr @@ -37,13 +37,5 @@ error: initializer for `thread_local` value can be made `const` LL | static PEEL_ME_MANY: i32 = { let x = 1; x * x }; | ^^^^^^^^^^^^^^^^^^^^ help: replace with: `const { { let x = 1; x * x } }` -error: initializer for `thread_local` value can be made `const` - --> tests/ui/missing_const_for_thread_local.rs:64:55 - | -LL | static STATE_12637_UNREACHABLE: Cell = unreachable!(); - | ^^^^^^^^^^^^^^ - | - = note: this error originates in the macro `$crate::panic::unreachable_2021` which comes from the expansion of the macro `unreachable` (in Nightly builds, run with -Z macro-backtrace for more info) - -error: aborting due to 7 previous errors +error: aborting due to 6 previous errors From b8d3c62fc1560bfa90c3bb775f052aaceb85d32e Mon Sep 17 00:00:00 2001 From: Noah Goldstein Date: Thu, 25 Jul 2024 14:54:18 +0800 Subject: [PATCH 12/12] Fixup tests --- ...est.SimplifyComparisonIntegral.panic-abort.diff | 2 +- ...st.SimplifyComparisonIntegral.panic-unwind.diff | 2 +- tests/ui/never_type/issue-5500-1.stderr | 14 ++++++++++++++ tests/ui/proc-macro/quote-debug.stdout | 4 +--- 4 files changed, 17 insertions(+), 5 deletions(-) create mode 100644 tests/ui/never_type/issue-5500-1.stderr diff --git a/tests/mir-opt/issue_76432.test.SimplifyComparisonIntegral.panic-abort.diff b/tests/mir-opt/issue_76432.test.SimplifyComparisonIntegral.panic-abort.diff index 0692ece348fc6..db9fa7cf5bb89 100644 --- a/tests/mir-opt/issue_76432.test.SimplifyComparisonIntegral.panic-abort.diff +++ b/tests/mir-opt/issue_76432.test.SimplifyComparisonIntegral.panic-abort.diff @@ -9,7 +9,7 @@ let _4: [T; 3]; let mut _5: usize; let mut _6: bool; - let _10: !; + let mut _10: !; scope 1 { debug v => _2; let _7: &T; diff --git a/tests/mir-opt/issue_76432.test.SimplifyComparisonIntegral.panic-unwind.diff b/tests/mir-opt/issue_76432.test.SimplifyComparisonIntegral.panic-unwind.diff index eb45329a6cfdc..3bcba95ed41c2 100644 --- a/tests/mir-opt/issue_76432.test.SimplifyComparisonIntegral.panic-unwind.diff +++ b/tests/mir-opt/issue_76432.test.SimplifyComparisonIntegral.panic-unwind.diff @@ -9,7 +9,7 @@ let _4: [T; 3]; let mut _5: usize; let mut _6: bool; - let _10: !; + let mut _10: !; scope 1 { debug v => _2; let _7: &T; diff --git a/tests/ui/never_type/issue-5500-1.stderr b/tests/ui/never_type/issue-5500-1.stderr new file mode 100644 index 0000000000000..783c19f1b6adf --- /dev/null +++ b/tests/ui/never_type/issue-5500-1.stderr @@ -0,0 +1,14 @@ +error[E0308]: mismatched types + --> $DIR/issue-5500-1.rs:10:18 + | +LL | _iter.node = &panic!() + | ---------- ^^^^^^^^^ expected `&usize`, found `&!` + | | + | expected due to the type of this binding + | + = note: expected reference `&usize` + found reference `&!` + +error: aborting due to 1 previous error + +For more information about this error, try `rustc --explain E0308`. diff --git a/tests/ui/proc-macro/quote-debug.stdout b/tests/ui/proc-macro/quote-debug.stdout index e868619ad107e..22d4f21712f57 100644 --- a/tests/ui/proc-macro/quote-debug.stdout +++ b/tests/ui/proc-macro/quote-debug.stdout @@ -32,9 +32,7 @@ fn main() { (iter.next(), iter.next()) { lit.set_span(crate::Span::recover_proc_macro_span(2)); lit - } else { - { ::core::panicking::unreachable_cold_explicit(); } - } + } else { ::core::panicking::unreachable_cold_explicit() } })), crate::TokenStream::from(crate::TokenTree::Punct(crate::Punct::new(';', crate::Spacing::Alone)))].iter().cloned().collect::()