Add new const_format_args!() macro and use it in panics.

This commit is contained in:
Mara Bos 2021-07-09 16:45:50 +02:00
parent 76cf1b8bd0
commit b64c4f9560
9 changed files with 36 additions and 11 deletions

View File

@ -72,6 +72,7 @@ pub fn register_builtin_macros(resolver: &mut dyn ResolverExpand) {
file: source_util::expand_file,
format_args_nl: format::expand_format_args_nl,
format_args: format::expand_format_args,
const_format_args: format::expand_format_args,
global_asm: asm::expand_global_asm,
include_bytes: source_util::expand_include_bytes,
include_str: source_util::expand_include_str,

View File

@ -410,6 +410,7 @@ symbols! {
const_fn_transmute,
const_fn_union,
const_fn_unsize,
const_format_args,
const_generic_defaults,
const_generics,
const_generics_defaults,

View File

@ -337,6 +337,7 @@ impl<'a> Arguments<'a> {
#[doc(hidden)]
#[inline]
#[unstable(feature = "fmt_internals", reason = "internal to format_args!", issue = "none")]
#[rustc_const_unstable(feature = "const_fmt_arguments_new", issue = "none")]
pub const fn new_v1(pieces: &'a [&'static str], args: &'a [ArgumentV1<'a>]) -> Arguments<'a> {
Arguments { pieces, fmt: None, args }
}
@ -350,6 +351,7 @@ impl<'a> Arguments<'a> {
#[doc(hidden)]
#[inline]
#[unstable(feature = "fmt_internals", reason = "internal to format_args!", issue = "none")]
#[rustc_const_unstable(feature = "const_fmt_arguments_new", issue = "none")]
pub const fn new_v1_formatted(
pieces: &'a [&'static str],
args: &'a [ArgumentV1<'a>],

View File

@ -837,6 +837,16 @@ pub(crate) mod builtin {
($fmt:expr, $($args:tt)*) => {{ /* compiler built-in */ }};
}
/// Same as `format_args`, but can be used in some const contexts.
#[unstable(feature = "const_format_args", issue = "none")]
#[allow_internal_unstable(fmt_internals, const_fmt_arguments_new)]
#[rustc_builtin_macro]
#[macro_export]
macro_rules! const_format_args {
($fmt:expr) => {{ /* compiler built-in */ }};
($fmt:expr, $($args:tt)*) => {{ /* compiler built-in */ }};
}
/// Same as `format_args`, but adds a newline in the end.
#[unstable(
feature = "format_args_nl",

View File

@ -7,7 +7,7 @@ use crate::fmt;
#[doc(hidden)]
#[unstable(feature = "edition_panic", issue = "none", reason = "use panic!() instead")]
#[allow_internal_unstable(core_panic)]
#[allow_internal_unstable(core_panic, const_format_args)]
#[rustc_diagnostic_item = "core_panic_2015_macro"]
#[rustc_macro_transparency = "semitransparent"]
pub macro panic_2015 {
@ -21,13 +21,13 @@ pub macro panic_2015 {
$crate::panicking::panic_str($msg)
),
($fmt:expr, $($arg:tt)+) => (
$crate::panicking::panic_fmt($crate::format_args!($fmt, $($arg)+))
$crate::panicking::panic_fmt($crate::const_format_args!($fmt, $($arg)+))
),
}
#[doc(hidden)]
#[unstable(feature = "edition_panic", issue = "none", reason = "use panic!() instead")]
#[allow_internal_unstable(core_panic)]
#[allow_internal_unstable(core_panic, const_format_args)]
#[rustc_diagnostic_item = "core_panic_2021_macro"]
#[rustc_macro_transparency = "semitransparent"]
pub macro panic_2021 {
@ -35,7 +35,7 @@ pub macro panic_2021 {
$crate::panicking::panic("explicit panic")
),
($($t:tt)+) => (
$crate::panicking::panic_fmt($crate::format_args!($($t)+))
$crate::panicking::panic_fmt($crate::const_format_args!($($t)+))
),
}

View File

@ -247,6 +247,7 @@
#![feature(const_fn_floating_point_arithmetic)]
#![feature(const_fn_fn_ptr_basics)]
#![cfg_attr(bootstrap, feature(const_fn_transmute))]
#![feature(const_format_args)]
#![feature(const_io_structs)]
#![feature(const_ip)]
#![feature(const_ipv4)]
@ -556,9 +557,9 @@ pub use core::{
#[stable(feature = "builtin_macro_prelude", since = "1.38.0")]
#[allow(deprecated)]
pub use core::{
assert, assert_matches, cfg, column, compile_error, concat, concat_idents, env, file,
format_args, format_args_nl, include, include_bytes, include_str, line, llvm_asm, log_syntax,
module_path, option_env, stringify, trace_macros,
assert, assert_matches, cfg, column, compile_error, concat, concat_idents, const_format_args,
env, file, format_args, format_args_nl, include, include_bytes, include_str, line, llvm_asm,
log_syntax, module_path, option_env, stringify, trace_macros,
};
#[stable(feature = "core_primitive", since = "1.43.0")]

View File

@ -20,7 +20,7 @@ use crate::thread::Result;
#[doc(hidden)]
#[unstable(feature = "edition_panic", issue = "none", reason = "use panic!() instead")]
#[allow_internal_unstable(libstd_sys_internals)]
#[allow_internal_unstable(libstd_sys_internals, const_format_args)]
#[cfg_attr(not(test), rustc_diagnostic_item = "std_panic_2015_macro")]
#[rustc_macro_transparency = "semitransparent"]
pub macro panic_2015 {
@ -31,7 +31,7 @@ pub macro panic_2015 {
$crate::rt::begin_panic($msg)
}),
($fmt:expr, $($arg:tt)+) => ({
$crate::rt::begin_panic_fmt(&$crate::format_args!($fmt, $($arg)+))
$crate::rt::begin_panic_fmt(&$crate::const_format_args!($fmt, $($arg)+))
}),
}

View File

@ -3,6 +3,7 @@ struct Value;
static settings_dir: String = format!("");
//~^ ERROR calls in statics are limited to constant functions
//~| ERROR is not yet stable as a const
fn from_string(_: String) -> Value {
Value

View File

@ -1,9 +1,18 @@
error[E0507]: cannot move out of static item `settings_dir`
--> $DIR/issue-64453.rs:13:37
--> $DIR/issue-64453.rs:14:37
|
LL | let settings_data = from_string(settings_dir);
| ^^^^^^^^^^^^ move occurs because `settings_dir` has type `String`, which does not implement the `Copy` trait
error: `Arguments::<'a>::new_v1` is not yet stable as a const fn
--> $DIR/issue-64453.rs:4:31
|
LL | static settings_dir: String = format!("");
| ^^^^^^^^^^^
|
= help: add `#![feature(const_fmt_arguments_new)]` to the crate attributes to enable
= note: this error originates in the macro `$crate::__export::format_args` (in Nightly builds, run with -Z macro-backtrace for more info)
error[E0015]: calls in statics are limited to constant functions, tuple structs and tuple variants
--> $DIR/issue-64453.rs:4:31
|
@ -12,7 +21,7 @@ LL | static settings_dir: String = format!("");
|
= note: this error originates in the macro `format` (in Nightly builds, run with -Z macro-backtrace for more info)
error: aborting due to 2 previous errors
error: aborting due to 3 previous errors
Some errors have detailed explanations: E0015, E0507.
For more information about an error, try `rustc --explain E0015`.