Skip to content

Commit 0a06d73

Browse files
Rollup merge of #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: #51999 (comment) r? `@RalfJung` `@rustbot` modify labels: +A-const-eval
2 parents 7428de1 + 7130127 commit 0a06d73

File tree

8 files changed

+60
-20
lines changed

8 files changed

+60
-20
lines changed

compiler/rustc_hir/src/lang_items.rs

+1
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,7 @@ language_item_table! {
263263
// is required to define it somewhere. Additionally, there are restrictions on crates that use
264264
// a weak lang item, but do not have it defined.
265265
Panic, sym::panic, panic_fn, Target::Fn;
266+
PanicStr, sym::panic_str, panic_str, Target::Fn;
266267
PanicBoundsCheck, sym::panic_bounds_check, panic_bounds_check_fn, Target::Fn;
267268
PanicInfo, sym::panic_info, panic_info, Target::Struct;
268269
PanicLocation, sym::panic_location, panic_location, Target::Struct;

compiler/rustc_mir/src/const_eval/machine.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,10 @@ impl<'mir, 'tcx> InterpCx<'mir, 'tcx, CompileTimeInterpreter<'mir, 'tcx>> {
7070
) -> InterpResult<'tcx> {
7171
let def_id = instance.def_id();
7272
if Some(def_id) == self.tcx.lang_items().panic_fn()
73+
|| Some(def_id) == self.tcx.lang_items().panic_str()
7374
|| Some(def_id) == self.tcx.lang_items().begin_panic_fn()
7475
{
75-
// &'static str
76+
// &str
7677
assert!(args.len() == 1);
7778

7879
let msg_place = self.deref_operand(args[0])?;

compiler/rustc_mir/src/transform/check_consts/mod.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,9 @@ impl ConstCx<'mir, 'tcx> {
7474

7575
/// Returns `true` if this `DefId` points to one of the official `panic` lang items.
7676
pub fn is_lang_panic_fn(tcx: TyCtxt<'tcx>, def_id: DefId) -> bool {
77-
Some(def_id) == tcx.lang_items().panic_fn() || Some(def_id) == tcx.lang_items().begin_panic_fn()
77+
Some(def_id) == tcx.lang_items().panic_fn()
78+
|| Some(def_id) == tcx.lang_items().panic_str()
79+
|| Some(def_id) == tcx.lang_items().begin_panic_fn()
7880
}
7981

8082
pub fn allow_internal_unstable(tcx: TyCtxt<'tcx>, def_id: DefId, feature_gate: Symbol) -> bool {

compiler/rustc_span/src/symbol.rs

+1
Original file line numberDiff line numberDiff line change
@@ -777,6 +777,7 @@ symbols! {
777777
panic_info,
778778
panic_location,
779779
panic_runtime,
780+
panic_str,
780781
panic_unwind,
781782
param_attrs,
782783
parent_trait,

library/core/src/macros/mod.rs

+1-1
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)

library/core/src/panicking.rs

+7
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]

src/test/ui/consts/const-eval/const_panic.rs

+8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#![feature(const_panic)]
22
#![crate_type = "lib"]
33

4+
const MSG: &str = "hello";
5+
46
const Z: () = std::panic!("cheese");
57
//~^ ERROR any use of this value will cause an error
68

@@ -12,6 +14,9 @@ const Y: () = std::unreachable!();
1214

1315
const X: () = std::unimplemented!();
1416
//~^ ERROR any use of this value will cause an error
17+
//
18+
const W: () = std::panic!(MSG);
19+
//~^ ERROR any use of this value will cause an error
1520

1621
const Z_CORE: () = core::panic!("cheese");
1722
//~^ ERROR any use of this value will cause an error
@@ -24,3 +29,6 @@ const Y_CORE: () = core::unreachable!();
2429

2530
const X_CORE: () = core::unimplemented!();
2631
//~^ ERROR any use of this value will cause an error
32+
33+
const W_CORE: () = core::panic!(MSG);
34+
//~^ ERROR any use of this value will cause an error
Original file line numberDiff line numberDiff line change
@@ -1,83 +1,103 @@
11
error: any use of this value will cause an error
2-
--> $DIR/const_panic.rs:4:15
2+
--> $DIR/const_panic.rs:6:15
33
|
44
LL | const Z: () = std::panic!("cheese");
55
| --------------^^^^^^^^^^^^^^^^^^^^^-
66
| |
7-
| the evaluated program panicked at 'cheese', $DIR/const_panic.rs:4:15
7+
| the evaluated program panicked at 'cheese', $DIR/const_panic.rs:6:15
88
|
99
= note: `#[deny(const_err)]` on by default
1010
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
1111

1212
error: any use of this value will cause an error
13-
--> $DIR/const_panic.rs:7:16
13+
--> $DIR/const_panic.rs:9:16
1414
|
1515
LL | const Z2: () = std::panic!();
1616
| ---------------^^^^^^^^^^^^^-
1717
| |
18-
| the evaluated program panicked at 'explicit panic', $DIR/const_panic.rs:7:16
18+
| the evaluated program panicked at 'explicit panic', $DIR/const_panic.rs:9:16
1919
|
2020
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
2121

2222
error: any use of this value will cause an error
23-
--> $DIR/const_panic.rs:10:15
23+
--> $DIR/const_panic.rs:12:15
2424
|
2525
LL | const Y: () = std::unreachable!();
2626
| --------------^^^^^^^^^^^^^^^^^^^-
2727
| |
28-
| the evaluated program panicked at 'internal error: entered unreachable code', $DIR/const_panic.rs:10:15
28+
| the evaluated program panicked at 'internal error: entered unreachable code', $DIR/const_panic.rs:12:15
2929
|
3030
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
3131

3232
error: any use of this value will cause an error
33-
--> $DIR/const_panic.rs:13:15
33+
--> $DIR/const_panic.rs:15:15
3434
|
3535
LL | const X: () = std::unimplemented!();
3636
| --------------^^^^^^^^^^^^^^^^^^^^^-
3737
| |
38-
| the evaluated program panicked at 'not implemented', $DIR/const_panic.rs:13:15
38+
| the evaluated program panicked at 'not implemented', $DIR/const_panic.rs:15:15
3939
|
4040
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
4141

4242
error: any use of this value will cause an error
43-
--> $DIR/const_panic.rs:16:20
43+
--> $DIR/const_panic.rs:18:15
44+
|
45+
LL | const W: () = std::panic!(MSG);
46+
| --------------^^^^^^^^^^^^^^^^-
47+
| |
48+
| the evaluated program panicked at 'hello', $DIR/const_panic.rs:18:15
49+
|
50+
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
51+
52+
error: any use of this value will cause an error
53+
--> $DIR/const_panic.rs:21:20
4454
|
4555
LL | const Z_CORE: () = core::panic!("cheese");
4656
| -------------------^^^^^^^^^^^^^^^^^^^^^^-
4757
| |
48-
| the evaluated program panicked at 'cheese', $DIR/const_panic.rs:16:20
58+
| the evaluated program panicked at 'cheese', $DIR/const_panic.rs:21:20
4959
|
5060
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
5161

5262
error: any use of this value will cause an error
53-
--> $DIR/const_panic.rs:19:21
63+
--> $DIR/const_panic.rs:24:21
5464
|
5565
LL | const Z2_CORE: () = core::panic!();
5666
| --------------------^^^^^^^^^^^^^^-
5767
| |
58-
| the evaluated program panicked at 'explicit panic', $DIR/const_panic.rs:19:21
68+
| the evaluated program panicked at 'explicit panic', $DIR/const_panic.rs:24:21
5969
|
6070
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
6171

6272
error: any use of this value will cause an error
63-
--> $DIR/const_panic.rs:22:20
73+
--> $DIR/const_panic.rs:27:20
6474
|
6575
LL | const Y_CORE: () = core::unreachable!();
6676
| -------------------^^^^^^^^^^^^^^^^^^^^-
6777
| |
68-
| the evaluated program panicked at 'internal error: entered unreachable code', $DIR/const_panic.rs:22:20
78+
| the evaluated program panicked at 'internal error: entered unreachable code', $DIR/const_panic.rs:27:20
6979
|
7080
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
7181

7282
error: any use of this value will cause an error
73-
--> $DIR/const_panic.rs:25:20
83+
--> $DIR/const_panic.rs:30:20
7484
|
7585
LL | const X_CORE: () = core::unimplemented!();
7686
| -------------------^^^^^^^^^^^^^^^^^^^^^^-
7787
| |
78-
| the evaluated program panicked at 'not implemented', $DIR/const_panic.rs:25:20
88+
| the evaluated program panicked at 'not implemented', $DIR/const_panic.rs:30:20
89+
|
90+
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
91+
92+
error: any use of this value will cause an error
93+
--> $DIR/const_panic.rs:33:20
94+
|
95+
LL | const W_CORE: () = core::panic!(MSG);
96+
| -------------------^^^^^^^^^^^^^^^^^-
97+
| |
98+
| the evaluated program panicked at 'hello', $DIR/const_panic.rs:33:20
7999
|
80100
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
81101

82-
error: aborting due to 8 previous errors
102+
error: aborting due to 10 previous errors
83103

0 commit comments

Comments
 (0)