Skip to content

Commit 686897f

Browse files
authored
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 e3e899b + e89991b commit 686897f

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

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
}

0 commit comments

Comments
 (0)