Skip to content

Commit cb074a8

Browse files
committed
Allow panic!("{}", computed_str) in const fn.
1 parent 02bf87f commit cb074a8

File tree

5 files changed

+23
-1
lines changed

5 files changed

+23
-1
lines changed

core/src/panic.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,14 @@ pub macro panic_2015 {
2727
($msg:literal $(,)?) => (
2828
$crate::panicking::panic($msg)
2929
),
30+
// Use `panic_str` instead of `panic_display::<&str>` for non_fmt_panic lint.
3031
($msg:expr $(,)?) => (
3132
$crate::panicking::panic_str($msg)
3233
),
34+
// Special-case the single-argument case for const_panic.
35+
("{}", $arg:expr $(,)?) => (
36+
$crate::panicking::panic_display(&$arg)
37+
),
3338
($fmt:expr, $($arg:tt)+) => (
3439
$crate::panicking::panic_fmt($crate::const_format_args!($fmt, $($arg)+))
3540
),
@@ -44,6 +49,10 @@ pub macro panic_2021 {
4449
() => (
4550
$crate::panicking::panic("explicit panic")
4651
),
52+
// Special-case the single-argument case for const_panic.
53+
("{}", $arg:expr $(,)?) => (
54+
$crate::panicking::panic_display(&$arg)
55+
),
4756
($($t:tt)+) => (
4857
$crate::panicking::panic_fmt($crate::const_format_args!($($t)+))
4958
),

core/src/panicking.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,13 @@ pub fn panic_str(expr: &str) -> ! {
6060
panic_fmt(format_args!("{}", expr));
6161
}
6262

63+
#[inline]
64+
#[track_caller]
65+
#[cfg_attr(not(bootstrap), lang = "panic_display")] // needed for const-evaluated panics
66+
pub fn panic_display<T: fmt::Display>(x: &T) -> ! {
67+
panic_fmt(format_args!("{}", *x));
68+
}
69+
6370
#[cold]
6471
#[cfg_attr(not(feature = "panic_immediate_abort"), inline(never))]
6572
#[track_caller]

std/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,7 @@
258258
#![feature(const_trait_impl)]
259259
#![feature(container_error_extra)]
260260
#![feature(core_intrinsics)]
261+
#![feature(core_panic)]
261262
#![feature(custom_test_frameworks)]
262263
#![feature(decl_macro)]
263264
#![feature(doc_cfg)]

std/src/panic.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use crate::thread::Result;
1010

1111
#[doc(hidden)]
1212
#[unstable(feature = "edition_panic", issue = "none", reason = "use panic!() instead")]
13-
#[allow_internal_unstable(libstd_sys_internals, const_format_args)]
13+
#[allow_internal_unstable(libstd_sys_internals, const_format_args, core_panic)]
1414
#[cfg_attr(not(test), rustc_diagnostic_item = "std_panic_2015_macro")]
1515
#[rustc_macro_transparency = "semitransparent"]
1616
pub macro panic_2015 {
@@ -20,6 +20,10 @@ pub macro panic_2015 {
2020
($msg:expr $(,)?) => ({
2121
$crate::rt::begin_panic($msg)
2222
}),
23+
// Special-case the single-argument case for const_panic.
24+
("{}", $arg:expr $(,)?) => ({
25+
$crate::rt::panic_display(&$arg)
26+
}),
2327
($fmt:expr, $($arg:tt)+) => ({
2428
$crate::rt::begin_panic_fmt(&$crate::const_format_args!($fmt, $($arg)+))
2529
}),

std/src/rt.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
// Re-export some of our utilities which are expected by other crates.
1818
pub use crate::panicking::{begin_panic, begin_panic_fmt, panic_count};
19+
pub use core::panicking::panic_display;
1920

2021
// To reduce the generated code of the new `lang_start`, this function is doing
2122
// the real work.

0 commit comments

Comments
 (0)