Skip to content

Commit eb21b92

Browse files
committed
Auto merge of rust-lang#86998 - m-ou-se:const-panic-fmt-as-str, r=oli-obk
Make const panic!("..") work in Rust 2021. During const eval, this replaces calls to core::panicking::panic_fmt and std::panicking::being_panic_fmt with a call to a new const fn: core::panicking::const_panic_fmt. That function uses fmt::Arguments::as_str() to get the str and calls panic_str with that instead. panic!() invocations with formatting arguments are still not accepted, as the creation of such a fmt::Arguments cannot be done in constant functions right now. r? `@RalfJung`
2 parents 004504d + c282b7b commit eb21b92

File tree

8 files changed

+56
-11
lines changed

8 files changed

+56
-11
lines changed

core/src/fmt/mod.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,8 @@ impl<'a> Arguments<'a> {
337337
#[doc(hidden)]
338338
#[inline]
339339
#[unstable(feature = "fmt_internals", reason = "internal to format_args!", issue = "none")]
340-
pub fn new_v1(pieces: &'a [&'static str], args: &'a [ArgumentV1<'a>]) -> Arguments<'a> {
340+
#[rustc_const_unstable(feature = "const_fmt_arguments_new", issue = "none")]
341+
pub const fn new_v1(pieces: &'a [&'static str], args: &'a [ArgumentV1<'a>]) -> Arguments<'a> {
341342
Arguments { pieces, fmt: None, args }
342343
}
343344

@@ -350,7 +351,8 @@ impl<'a> Arguments<'a> {
350351
#[doc(hidden)]
351352
#[inline]
352353
#[unstable(feature = "fmt_internals", reason = "internal to format_args!", issue = "none")]
353-
pub fn new_v1_formatted(
354+
#[rustc_const_unstable(feature = "const_fmt_arguments_new", issue = "none")]
355+
pub const fn new_v1_formatted(
354356
pieces: &'a [&'static str],
355357
args: &'a [ArgumentV1<'a>],
356358
fmt: &'a [rt::v1::Argument],

core/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
#![feature(cfg_target_has_atomic)]
7474
#![feature(const_heap)]
7575
#![feature(const_alloc_layout)]
76+
#![feature(const_arguments_as_str)]
7677
#![feature(const_assert_type)]
7778
#![feature(const_discriminant)]
7879
#![feature(const_cell_into_inner)]

core/src/macros/mod.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -837,6 +837,31 @@ pub(crate) mod builtin {
837837
($fmt:expr, $($args:tt)*) => {{ /* compiler built-in */ }};
838838
}
839839

840+
/// Same as `format_args`, but can be used in some const contexts.
841+
///
842+
/// This macro is used by the panic macros for the `const_panic` feature.
843+
///
844+
/// This macro will be removed once `format_args` is allowed in const contexts.
845+
#[cfg(not(bootstrap))]
846+
#[unstable(feature = "const_format_args", issue = "none")]
847+
#[allow_internal_unstable(fmt_internals, const_fmt_arguments_new)]
848+
#[rustc_builtin_macro]
849+
#[macro_export]
850+
macro_rules! const_format_args {
851+
($fmt:expr) => {{ /* compiler built-in */ }};
852+
($fmt:expr, $($args:tt)*) => {{ /* compiler built-in */ }};
853+
}
854+
855+
/// Same as `format_args`, but can be used in some const contexts.
856+
#[cfg(bootstrap)]
857+
#[unstable(feature = "const_format_args", issue = "none")]
858+
#[macro_export]
859+
macro_rules! const_format_args {
860+
($($t:tt)*) => {
861+
$crate::format_args!($($t)*)
862+
}
863+
}
864+
840865
/// Same as `format_args`, but adds a newline in the end.
841866
#[unstable(
842867
feature = "format_args_nl",

core/src/panic.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use crate::fmt;
77

88
#[doc(hidden)]
99
#[unstable(feature = "edition_panic", issue = "none", reason = "use panic!() instead")]
10-
#[allow_internal_unstable(core_panic)]
10+
#[allow_internal_unstable(core_panic, const_format_args)]
1111
#[rustc_diagnostic_item = "core_panic_2015_macro"]
1212
#[rustc_macro_transparency = "semitransparent"]
1313
pub macro panic_2015 {
@@ -21,21 +21,21 @@ pub macro panic_2015 {
2121
$crate::panicking::panic_str($msg)
2222
),
2323
($fmt:expr, $($arg:tt)+) => (
24-
$crate::panicking::panic_fmt($crate::format_args!($fmt, $($arg)+))
24+
$crate::panicking::panic_fmt($crate::const_format_args!($fmt, $($arg)+))
2525
),
2626
}
2727

2828
#[doc(hidden)]
2929
#[unstable(feature = "edition_panic", issue = "none", reason = "use panic!() instead")]
30-
#[allow_internal_unstable(core_panic)]
30+
#[allow_internal_unstable(core_panic, const_format_args)]
3131
#[rustc_diagnostic_item = "core_panic_2021_macro"]
3232
#[rustc_macro_transparency = "semitransparent"]
3333
pub macro panic_2021 {
3434
() => (
3535
$crate::panicking::panic("explicit panic")
3636
),
3737
($($t:tt)+) => (
38-
$crate::panicking::panic_fmt($crate::format_args!($($t)+))
38+
$crate::panicking::panic_fmt($crate::const_format_args!($($t)+))
3939
),
4040
}
4141

core/src/panicking.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ fn panic_bounds_check(index: usize, len: usize) -> ! {
7474
#[cfg_attr(not(feature = "panic_immediate_abort"), inline(never))]
7575
#[cfg_attr(feature = "panic_immediate_abort", inline)]
7676
#[track_caller]
77+
#[cfg_attr(not(bootstrap), lang = "panic_fmt")] // needed for const-evaluated panics
7778
pub fn panic_fmt(fmt: fmt::Arguments<'_>) -> ! {
7879
if cfg!(feature = "panic_immediate_abort") {
7980
super::intrinsics::abort()
@@ -92,6 +93,20 @@ pub fn panic_fmt(fmt: fmt::Arguments<'_>) -> ! {
9293
unsafe { panic_impl(&pi) }
9394
}
9495

96+
/// This function is used instead of panic_fmt in const eval.
97+
#[cfg(not(bootstrap))]
98+
#[lang = "const_panic_fmt"]
99+
pub const fn const_panic_fmt(fmt: fmt::Arguments<'_>) -> ! {
100+
if let Some(msg) = fmt.as_str() {
101+
panic_str(msg);
102+
} else {
103+
// SAFETY: This is only evaluated at compile time, which reliably
104+
// handles this UB (in case this branch turns out to be reachable
105+
// somehow).
106+
unsafe { crate::hint::unreachable_unchecked() };
107+
}
108+
}
109+
95110
#[derive(Debug)]
96111
#[doc(hidden)]
97112
pub enum AssertKind {

std/src/lib.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,7 @@
247247
#![feature(const_fn_floating_point_arithmetic)]
248248
#![feature(const_fn_fn_ptr_basics)]
249249
#![cfg_attr(bootstrap, feature(const_fn_transmute))]
250+
#![feature(const_format_args)]
250251
#![feature(const_io_structs)]
251252
#![feature(const_ip)]
252253
#![feature(const_ipv4)]
@@ -555,9 +556,9 @@ pub use core::{
555556
#[stable(feature = "builtin_macro_prelude", since = "1.38.0")]
556557
#[allow(deprecated)]
557558
pub use core::{
558-
assert, assert_matches, cfg, column, compile_error, concat, concat_idents, env, file,
559-
format_args, format_args_nl, include, include_bytes, include_str, line, llvm_asm, log_syntax,
560-
module_path, option_env, stringify, trace_macros,
559+
assert, assert_matches, cfg, column, compile_error, concat, concat_idents, const_format_args,
560+
env, file, format_args, format_args_nl, include, include_bytes, include_str, line, llvm_asm,
561+
log_syntax, module_path, option_env, stringify, trace_macros,
561562
};
562563

563564
#[stable(feature = "core_primitive", since = "1.43.0")]

std/src/panic.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use crate::thread::Result;
2020

2121
#[doc(hidden)]
2222
#[unstable(feature = "edition_panic", issue = "none", reason = "use panic!() instead")]
23-
#[allow_internal_unstable(libstd_sys_internals)]
23+
#[allow_internal_unstable(libstd_sys_internals, const_format_args)]
2424
#[cfg_attr(not(test), rustc_diagnostic_item = "std_panic_2015_macro")]
2525
#[rustc_macro_transparency = "semitransparent"]
2626
pub macro panic_2015 {
@@ -31,7 +31,7 @@ pub macro panic_2015 {
3131
$crate::rt::begin_panic($msg)
3232
}),
3333
($fmt:expr, $($arg:tt)+) => ({
34-
$crate::rt::begin_panic_fmt(&$crate::format_args!($fmt, $($arg)+))
34+
$crate::rt::begin_panic_fmt(&$crate::const_format_args!($fmt, $($arg)+))
3535
}),
3636
}
3737

std/src/panicking.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,7 @@ pub fn panicking() -> bool {
448448
#[cfg_attr(not(feature = "panic_immediate_abort"), track_caller)]
449449
#[cfg_attr(not(feature = "panic_immediate_abort"), inline(never))]
450450
#[cfg_attr(feature = "panic_immediate_abort", inline)]
451+
#[cfg_attr(all(not(bootstrap), not(test)), lang = "begin_panic_fmt")]
451452
pub fn begin_panic_fmt(msg: &fmt::Arguments<'_>) -> ! {
452453
if cfg!(feature = "panic_immediate_abort") {
453454
intrinsics::abort()

0 commit comments

Comments
 (0)