Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit eb62779

Browse files
Rollup merge of rust-lang#88954 - nbdd0121:panic3, r=oli-obk
Allow `panic!("{}", computed_str)` in const fn. Special-case `panic!("{}", arg)` and translate it to `panic_display(&arg)`. `panic_display` will behave like `panic_any` in cosnt eval and behave like `panic!(format_args!("{}", arg))` in runtime. This should bring Rust 2015 and 2021 to feature parity in terms of `const_panic`; and hopefully would unblock the stabilisation of rust-lang#51999. `@rustbot` modify labels: +T-compiler +T-libs +A-const-eval +A-const-fn r? `@oli-obk`
2 parents 723d279 + 11c0e58 commit eb62779

File tree

14 files changed

+129
-40
lines changed

14 files changed

+129
-40
lines changed

compiler/rustc_const_eval/src/const_eval/machine.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,17 @@ impl<'mir, 'tcx> InterpCx<'mir, 'tcx, CompileTimeInterpreter<'mir, 'tcx>> {
3636
let def_id = instance.def_id();
3737
if Some(def_id) == self.tcx.lang_items().panic_fn()
3838
|| Some(def_id) == self.tcx.lang_items().panic_str()
39+
|| Some(def_id) == self.tcx.lang_items().panic_display()
3940
|| Some(def_id) == self.tcx.lang_items().begin_panic_fn()
4041
{
41-
// &str
42+
// &str or &&str
4243
assert!(args.len() == 1);
4344

44-
let msg_place = self.deref_operand(&args[0])?;
45+
let mut msg_place = self.deref_operand(&args[0])?;
46+
while msg_place.layout.ty.is_ref() {
47+
msg_place = self.deref_operand(&msg_place.into())?;
48+
}
49+
4550
let msg = Symbol::intern(self.read_str(&msg_place)?);
4651
let span = self.find_closest_untracked_caller_location();
4752
let (file, line, col) = self.location_triple_for_span(span);

compiler/rustc_const_eval/src/transform/check_consts/check.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -888,6 +888,10 @@ impl Visitor<'tcx> for Checker<'mir, 'tcx> {
888888
if is_lang_panic_fn(tcx, callee) {
889889
self.check_op(ops::Panic);
890890

891+
// `begin_panic` and `panic_display` are generic functions that accept
892+
// types other than str. Check to enforce that only str can be used in
893+
// const-eval.
894+
891895
// const-eval of the `begin_panic` fn assumes the argument is `&str`
892896
if Some(callee) == tcx.lang_items().begin_panic_fn() {
893897
match args[0].ty(&self.ccx.body.local_decls, tcx).kind() {
@@ -896,6 +900,15 @@ impl Visitor<'tcx> for Checker<'mir, 'tcx> {
896900
}
897901
}
898902

903+
// const-eval of the `panic_display` fn assumes the argument is `&&str`
904+
if Some(callee) == tcx.lang_items().panic_display() {
905+
match args[0].ty(&self.ccx.body.local_decls, tcx).kind() {
906+
ty::Ref(_, ty, _) if matches!(ty.kind(), ty::Ref(_, ty, _) if ty.is_str()) =>
907+
{}
908+
_ => self.check_op(ops::PanicNonStr),
909+
}
910+
}
911+
899912
return;
900913
}
901914

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ pub fn is_lang_panic_fn(tcx: TyCtxt<'tcx>, def_id: DefId) -> bool {
7979
// Keep in sync with what that function handles!
8080
Some(def_id) == tcx.lang_items().panic_fn()
8181
|| Some(def_id) == tcx.lang_items().panic_str()
82+
|| Some(def_id) == tcx.lang_items().panic_display()
8283
|| Some(def_id) == tcx.lang_items().begin_panic_fn()
8384
|| Some(def_id) == tcx.lang_items().panic_fmt()
8485
|| Some(def_id) == tcx.lang_items().begin_panic_fmt()

compiler/rustc_hir/src/lang_items.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,7 @@ language_item_table! {
283283
// a weak lang item, but do not have it defined.
284284
Panic, sym::panic, panic_fn, Target::Fn, GenericRequirement::None;
285285
PanicFmt, sym::panic_fmt, panic_fmt, Target::Fn, GenericRequirement::None;
286+
PanicDisplay, sym::panic_display, panic_display, Target::Fn, GenericRequirement::None;
286287
PanicStr, sym::panic_str, panic_str, Target::Fn, GenericRequirement::None;
287288
ConstPanicFmt, sym::const_panic_fmt, const_panic_fmt, Target::Fn, GenericRequirement::None;
288289
PanicBoundsCheck, sym::panic_bounds_check, panic_bounds_check_fn, Target::Fn, GenericRequirement::None;

compiler/rustc_span/src/symbol.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -923,6 +923,7 @@ symbols! {
923923
panic_2021,
924924
panic_abort,
925925
panic_bounds_check,
926+
panic_display,
926927
panic_fmt,
927928
panic_handler,
928929
panic_impl,

library/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
),

library/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]

library/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)]

library/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
}),

library/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)