diff options
| author | Mara Bos <m-ou.se@m-ou.se> | 2025-05-01 11:53:51 +0200 |
|---|---|---|
| committer | Mara Bos <m-ou.se@m-ou.se> | 2025-05-01 11:55:23 +0200 |
| commit | 36c6633b0fb422e35f78de6fb9f0df77d1f4ba23 (patch) | |
| tree | 26c102c31bea01a0c11bdc5a9b6c932b8b3a674f | |
| parent | 3e969d433d5c4f7001e14dba1c9a00a591937a4f (diff) | |
| download | rust-36c6633b0fb422e35f78de6fb9f0df77d1f4ba23.tar.gz rust-36c6633b0fb422e35f78de6fb9f0df77d1f4ba23.zip | |
Clean up "const" situation in format_args!().
Rather than marking the Argument::new_display etc. functions as non-const, this marks the Arguments::new_v1 functions as non-const.
| -rw-r--r-- | compiler/rustc_const_eval/src/check_consts/ops.rs | 2 | ||||
| -rw-r--r-- | compiler/rustc_span/src/symbol.rs | 2 | ||||
| -rw-r--r-- | library/core/src/fmt/rt.rs | 48 | ||||
| -rw-r--r-- | tests/ui/consts/const-eval/format.stderr | 8 |
4 files changed, 33 insertions, 27 deletions
diff --git a/compiler/rustc_const_eval/src/check_consts/ops.rs b/compiler/rustc_const_eval/src/check_consts/ops.rs index 7756e51c4c5..1e5b98675c4 100644 --- a/compiler/rustc_const_eval/src/check_consts/ops.rs +++ b/compiler/rustc_const_eval/src/check_consts/ops.rs @@ -352,7 +352,7 @@ fn build_error_for_const_call<'tcx>( ); err } - _ if tcx.opt_parent(callee) == tcx.get_diagnostic_item(sym::ArgumentMethods) => { + _ if tcx.opt_parent(callee) == tcx.get_diagnostic_item(sym::FmtArgumentsNew) => { ccx.dcx().create_err(errors::NonConstFmtMacroCall { span, kind: ccx.const_kind(), diff --git a/compiler/rustc_span/src/symbol.rs b/compiler/rustc_span/src/symbol.rs index ba3e6d7ca82..7a1fb36324b 100644 --- a/compiler/rustc_span/src/symbol.rs +++ b/compiler/rustc_span/src/symbol.rs @@ -174,7 +174,6 @@ symbols! { Arc, ArcWeak, Argument, - ArgumentMethods, ArrayIntoIter, AsMut, AsRef, @@ -249,6 +248,7 @@ symbols! { Error, File, FileType, + FmtArgumentsNew, Fn, FnMut, FnOnce, diff --git a/library/core/src/fmt/rt.rs b/library/core/src/fmt/rt.rs index ec5015e06d2..c2a8a39bcac 100644 --- a/library/core/src/fmt/rt.rs +++ b/library/core/src/fmt/rt.rs @@ -113,46 +113,45 @@ macro_rules! argument_new { }; } -#[rustc_diagnostic_item = "ArgumentMethods"] impl Argument<'_> { #[inline] - pub fn new_display<T: Display>(x: &T) -> Argument<'_> { + pub const fn new_display<T: Display>(x: &T) -> Argument<'_> { argument_new!(T, x, <T as Display>::fmt) } #[inline] - pub fn new_debug<T: Debug>(x: &T) -> Argument<'_> { + pub const fn new_debug<T: Debug>(x: &T) -> Argument<'_> { argument_new!(T, x, <T as Debug>::fmt) } #[inline] - pub fn new_debug_noop<T: Debug>(x: &T) -> Argument<'_> { + pub const fn new_debug_noop<T: Debug>(x: &T) -> Argument<'_> { argument_new!(T, x, |_: &T, _| Ok(())) } #[inline] - pub fn new_octal<T: Octal>(x: &T) -> Argument<'_> { + pub const fn new_octal<T: Octal>(x: &T) -> Argument<'_> { argument_new!(T, x, <T as Octal>::fmt) } #[inline] - pub fn new_lower_hex<T: LowerHex>(x: &T) -> Argument<'_> { + pub const fn new_lower_hex<T: LowerHex>(x: &T) -> Argument<'_> { argument_new!(T, x, <T as LowerHex>::fmt) } #[inline] - pub fn new_upper_hex<T: UpperHex>(x: &T) -> Argument<'_> { + pub const fn new_upper_hex<T: UpperHex>(x: &T) -> Argument<'_> { argument_new!(T, x, <T as UpperHex>::fmt) } #[inline] - pub fn new_pointer<T: Pointer>(x: &T) -> Argument<'_> { + pub const fn new_pointer<T: Pointer>(x: &T) -> Argument<'_> { argument_new!(T, x, <T as Pointer>::fmt) } #[inline] - pub fn new_binary<T: Binary>(x: &T) -> Argument<'_> { + pub const fn new_binary<T: Binary>(x: &T) -> Argument<'_> { argument_new!(T, x, <T as Binary>::fmt) } #[inline] - pub fn new_lower_exp<T: LowerExp>(x: &T) -> Argument<'_> { + pub const fn new_lower_exp<T: LowerExp>(x: &T) -> Argument<'_> { argument_new!(T, x, <T as LowerExp>::fmt) } #[inline] - pub fn new_upper_exp<T: UpperExp>(x: &T) -> Argument<'_> { + pub const fn new_upper_exp<T: UpperExp>(x: &T) -> Argument<'_> { argument_new!(T, x, <T as UpperExp>::fmt) } #[inline] @@ -203,15 +202,8 @@ impl Argument<'_> { /// let f = format_args!("{}", "a"); /// println!("{f}"); /// ``` - /// - /// This function should _not_ be const, to make sure we don't accept - /// format_args!() and panic!() with arguments in const, even when not evaluated: - /// - /// ```compile_fail,E0015 - /// const _: () = if false { panic!("a {}", "a") }; - /// ``` #[inline] - pub fn none() -> [Self; 0] { + pub const fn none() -> [Self; 0] { [] } } @@ -246,8 +238,15 @@ impl<'a> Arguments<'a> { /// When using the format_args!() macro, this function is used to generate the /// Arguments structure. + /// + /// This function should _not_ be const, to make sure we don't accept + /// format_args!() and panic!() with arguments in const, even when not evaluated: + /// + /// ```compile_fail,E0015 + /// const _: () = if false { panic!("a {}", "a") }; + /// ``` #[inline] - pub const fn new_v1<const P: usize, const A: usize>( + pub fn new_v1<const P: usize, const A: usize>( pieces: &'a [&'static str; P], args: &'a [rt::Argument<'a>; A], ) -> Arguments<'a> { @@ -262,8 +261,15 @@ impl<'a> Arguments<'a> { /// 1. The `pieces` slice must be at least as long as `fmt`. /// 2. Every `rt::Placeholder::position` value within `fmt` must be a valid index of `args`. /// 3. Every `rt::Count::Param` within `fmt` must contain a valid index of `args`. + /// + /// This function should _not_ be const, to make sure we don't accept + /// format_args!() and panic!() with arguments in const, even when not evaluated: + /// + /// ```compile_fail,E0015 + /// const _: () = if false { panic!("a {:1}", "a") }; + /// ``` #[inline] - pub const fn new_v1_formatted( + pub fn new_v1_formatted( pieces: &'a [&'static str], args: &'a [rt::Argument<'a>], fmt: &'a [rt::Placeholder], diff --git a/tests/ui/consts/const-eval/format.stderr b/tests/ui/consts/const-eval/format.stderr index 4c4cbb372a7..2f202705b7f 100644 --- a/tests/ui/consts/const-eval/format.stderr +++ b/tests/ui/consts/const-eval/format.stderr @@ -1,16 +1,16 @@ error[E0015]: cannot call non-const formatting macro in constant functions - --> $DIR/format.rs:2:13 + --> $DIR/format.rs:2:5 | LL | panic!("{:?}", 0); - | ^^^^ + | ^^^^^^^^^^^^^^^^^ | = note: calls in constant functions are limited to constant functions, tuple structs and tuple variants error[E0015]: cannot call non-const formatting macro in constant functions - --> $DIR/format.rs:7:15 + --> $DIR/format.rs:7:5 | LL | println!("{:?}", 0); - | ^^^^ + | ^^^^^^^^^^^^^^^^^^^ | = note: calls in constant functions are limited to constant functions, tuple structs and tuple variants = note: this error originates in the macro `$crate::format_args_nl` which comes from the expansion of the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info) |
