Skip to content

Commit e5e4927

Browse files
Rollup merge of rust-lang#78069 - fusion-engineering-forks:core-const-panic-str, r=RalfJung
Fix const core::panic!(non_literal_str). Invocations of `core::panic!(x)` where `x` is not a string literal expand to `panic!("{}", x)`, which is not understood by the const panic logic right now. This adds `panic_str` as a lang item, and modifies the const eval implementation to hook into this item as well. This fixes the issue mentioned here: rust-lang#51999 (comment) r? `@RalfJung` `@rustbot` modify labels: +A-const-eval
2 parents b783efe + b387c24 commit e5e4927

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

core/src/macros/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ macro_rules! panic {
1010
$crate::panicking::panic($msg)
1111
);
1212
($msg:expr) => (
13-
$crate::panic!("{}", $crate::convert::identity::<&str>($msg))
13+
$crate::panicking::panic_str($msg)
1414
);
1515
($msg:expr,) => (
1616
$crate::panic!($msg)

core/src/panicking.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,13 @@ pub fn panic(expr: &'static str) -> ! {
5050
panic_fmt(fmt::Arguments::new_v1(&[expr], &[]));
5151
}
5252

53+
#[inline]
54+
#[track_caller]
55+
#[cfg_attr(not(bootstrap), lang = "panic_str")] // needed for const-evaluated panics
56+
pub fn panic_str(expr: &str) -> ! {
57+
panic_fmt(format_args!("{}", expr));
58+
}
59+
5360
#[cold]
5461
#[cfg_attr(not(feature = "panic_immediate_abort"), inline(never))]
5562
#[track_caller]

0 commit comments

Comments
 (0)