Skip to content

Commit c840027

Browse files
committed
Add new const_format_args!() macro and use it in panics.
1 parent 259b25b commit c840027

File tree

5 files changed

+22
-9
lines changed

5 files changed

+22
-9
lines changed

core/src/fmt/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,7 @@ impl<'a> Arguments<'a> {
337337
#[doc(hidden)]
338338
#[inline]
339339
#[unstable(feature = "fmt_internals", reason = "internal to format_args!", issue = "none")]
340+
#[rustc_const_unstable(feature = "const_fmt_arguments_new", issue = "none")]
340341
pub const fn new_v1(pieces: &'a [&'static str], args: &'a [ArgumentV1<'a>]) -> Arguments<'a> {
341342
Arguments { pieces, fmt: None, args }
342343
}
@@ -350,6 +351,7 @@ impl<'a> Arguments<'a> {
350351
#[doc(hidden)]
351352
#[inline]
352353
#[unstable(feature = "fmt_internals", reason = "internal to format_args!", issue = "none")]
354+
#[rustc_const_unstable(feature = "const_fmt_arguments_new", issue = "none")]
353355
pub const fn new_v1_formatted(
354356
pieces: &'a [&'static str],
355357
args: &'a [ArgumentV1<'a>],

core/src/macros/mod.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -837,6 +837,16 @@ 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+
#[unstable(feature = "const_format_args", issue = "none")]
842+
#[allow_internal_unstable(fmt_internals, const_fmt_arguments_new)]
843+
#[rustc_builtin_macro]
844+
#[macro_export]
845+
macro_rules! const_format_args {
846+
($fmt:expr) => {{ /* compiler built-in */ }};
847+
($fmt:expr, $($args:tt)*) => {{ /* compiler built-in */ }};
848+
}
849+
840850
/// Same as `format_args`, but adds a newline in the end.
841851
#[unstable(
842852
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

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)]
@@ -556,9 +557,9 @@ pub use core::{
556557
#[stable(feature = "builtin_macro_prelude", since = "1.38.0")]
557558
#[allow(deprecated)]
558559
pub use core::{
559-
assert, assert_matches, cfg, column, compile_error, concat, concat_idents, env, file,
560-
format_args, format_args_nl, include, include_bytes, include_str, line, llvm_asm, log_syntax,
561-
module_path, option_env, stringify, trace_macros,
560+
assert, assert_matches, cfg, column, compile_error, concat, concat_idents, const_format_args,
561+
env, file, format_args, format_args_nl, include, include_bytes, include_str, line, llvm_asm,
562+
log_syntax, module_path, option_env, stringify, trace_macros,
562563
};
563564

564565
#[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

0 commit comments

Comments
 (0)