Skip to content

Commit 1131483

Browse files
authored
Unrolled build for rust-lang#139624
Rollup merge of rust-lang#139624 - m-ou-se:unconst-format-args, r=jhpratt Don't allow flattened format_args in const. Fixes rust-lang#139136 Fixes rust-lang#139621 We allow `format_args!("a")` in const, but don't allow any format_args with arguments in const, such as `format_args!("{}", arg)`. However, we accidentally allow `format_args!("hello {}", "world")` in const, as it gets flattened to `format_args!("hello world")`. This also applies to panic in const. This wasn't supposed to happen. I added protection against this in the format args flattening code, ~~but I accidentally marked a function as const that shouldn't have been const~~ but this was removed in rust-lang#135139. This is a breaking change. The crater found no breakage, however. This breaks things like: ```rust const _: () = if false { panic!("a {}", "a") }; ``` and ```rust const F: std::fmt::Arguments<'static> = format_args!("a {}", "a"); ```
2 parents 7188f45 + 56426db commit 1131483

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
lines changed

library/core/src/fmt/rt.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -200,8 +200,15 @@ impl Argument<'_> {
200200
/// let f = format_args!("{}", "a");
201201
/// println!("{f}");
202202
/// ```
203+
///
204+
/// This function should _not_ be const, to make sure we don't accept
205+
/// format_args!() and panic!() with arguments in const, even when not evaluated:
206+
///
207+
/// ```compile_fail,E0015
208+
/// const _: () = if false { panic!("a {}", "a") };
209+
/// ```
203210
#[inline]
204-
pub const fn none() -> [Self; 0] {
211+
pub fn none() -> [Self; 0] {
205212
[]
206213
}
207214
}

tests/ui/consts/const-eval/format.rs

+5
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,9 @@ const fn print() {
99
//~| ERROR cannot call non-const function `_print` in constant functions
1010
}
1111

12+
const fn format_args() {
13+
format_args!("{}", 0);
14+
//~^ ERROR cannot call non-const formatting macro in constant functions
15+
}
16+
1217
fn main() {}

tests/ui/consts/const-eval/format.stderr

+9-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,14 @@ LL | println!("{:?}", 0);
2424
= note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
2525
= note: this error originates in the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info)
2626

27-
error: aborting due to 3 previous errors
27+
error[E0015]: cannot call non-const formatting macro in constant functions
28+
--> $DIR/format.rs:13:5
29+
|
30+
LL | format_args!("{}", 0);
31+
| ^^^^^^^^^^^^^^^^^^^^^
32+
|
33+
= note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
34+
35+
error: aborting due to 4 previous errors
2836

2937
For more information about this error, try `rustc --explain E0015`.

0 commit comments

Comments
 (0)