Skip to content

Commit 36c6633

Browse files
committed
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.
1 parent 3e969d4 commit 36c6633

File tree

4 files changed

+33
-27
lines changed

4 files changed

+33
-27
lines changed

compiler/rustc_const_eval/src/check_consts/ops.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ fn build_error_for_const_call<'tcx>(
352352
);
353353
err
354354
}
355-
_ if tcx.opt_parent(callee) == tcx.get_diagnostic_item(sym::ArgumentMethods) => {
355+
_ if tcx.opt_parent(callee) == tcx.get_diagnostic_item(sym::FmtArgumentsNew) => {
356356
ccx.dcx().create_err(errors::NonConstFmtMacroCall {
357357
span,
358358
kind: ccx.const_kind(),

compiler/rustc_span/src/symbol.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,6 @@ symbols! {
174174
Arc,
175175
ArcWeak,
176176
Argument,
177-
ArgumentMethods,
178177
ArrayIntoIter,
179178
AsMut,
180179
AsRef,
@@ -249,6 +248,7 @@ symbols! {
249248
Error,
250249
File,
251250
FileType,
251+
FmtArgumentsNew,
252252
Fn,
253253
FnMut,
254254
FnOnce,

library/core/src/fmt/rt.rs

+27-21
Original file line numberDiff line numberDiff line change
@@ -113,46 +113,45 @@ macro_rules! argument_new {
113113
};
114114
}
115115

116-
#[rustc_diagnostic_item = "ArgumentMethods"]
117116
impl Argument<'_> {
118117
#[inline]
119-
pub fn new_display<T: Display>(x: &T) -> Argument<'_> {
118+
pub const fn new_display<T: Display>(x: &T) -> Argument<'_> {
120119
argument_new!(T, x, <T as Display>::fmt)
121120
}
122121
#[inline]
123-
pub fn new_debug<T: Debug>(x: &T) -> Argument<'_> {
122+
pub const fn new_debug<T: Debug>(x: &T) -> Argument<'_> {
124123
argument_new!(T, x, <T as Debug>::fmt)
125124
}
126125
#[inline]
127-
pub fn new_debug_noop<T: Debug>(x: &T) -> Argument<'_> {
126+
pub const fn new_debug_noop<T: Debug>(x: &T) -> Argument<'_> {
128127
argument_new!(T, x, |_: &T, _| Ok(()))
129128
}
130129
#[inline]
131-
pub fn new_octal<T: Octal>(x: &T) -> Argument<'_> {
130+
pub const fn new_octal<T: Octal>(x: &T) -> Argument<'_> {
132131
argument_new!(T, x, <T as Octal>::fmt)
133132
}
134133
#[inline]
135-
pub fn new_lower_hex<T: LowerHex>(x: &T) -> Argument<'_> {
134+
pub const fn new_lower_hex<T: LowerHex>(x: &T) -> Argument<'_> {
136135
argument_new!(T, x, <T as LowerHex>::fmt)
137136
}
138137
#[inline]
139-
pub fn new_upper_hex<T: UpperHex>(x: &T) -> Argument<'_> {
138+
pub const fn new_upper_hex<T: UpperHex>(x: &T) -> Argument<'_> {
140139
argument_new!(T, x, <T as UpperHex>::fmt)
141140
}
142141
#[inline]
143-
pub fn new_pointer<T: Pointer>(x: &T) -> Argument<'_> {
142+
pub const fn new_pointer<T: Pointer>(x: &T) -> Argument<'_> {
144143
argument_new!(T, x, <T as Pointer>::fmt)
145144
}
146145
#[inline]
147-
pub fn new_binary<T: Binary>(x: &T) -> Argument<'_> {
146+
pub const fn new_binary<T: Binary>(x: &T) -> Argument<'_> {
148147
argument_new!(T, x, <T as Binary>::fmt)
149148
}
150149
#[inline]
151-
pub fn new_lower_exp<T: LowerExp>(x: &T) -> Argument<'_> {
150+
pub const fn new_lower_exp<T: LowerExp>(x: &T) -> Argument<'_> {
152151
argument_new!(T, x, <T as LowerExp>::fmt)
153152
}
154153
#[inline]
155-
pub fn new_upper_exp<T: UpperExp>(x: &T) -> Argument<'_> {
154+
pub const fn new_upper_exp<T: UpperExp>(x: &T) -> Argument<'_> {
156155
argument_new!(T, x, <T as UpperExp>::fmt)
157156
}
158157
#[inline]
@@ -203,15 +202,8 @@ impl Argument<'_> {
203202
/// let f = format_args!("{}", "a");
204203
/// println!("{f}");
205204
/// ```
206-
///
207-
/// This function should _not_ be const, to make sure we don't accept
208-
/// format_args!() and panic!() with arguments in const, even when not evaluated:
209-
///
210-
/// ```compile_fail,E0015
211-
/// const _: () = if false { panic!("a {}", "a") };
212-
/// ```
213205
#[inline]
214-
pub fn none() -> [Self; 0] {
206+
pub const fn none() -> [Self; 0] {
215207
[]
216208
}
217209
}
@@ -246,8 +238,15 @@ impl<'a> Arguments<'a> {
246238

247239
/// When using the format_args!() macro, this function is used to generate the
248240
/// Arguments structure.
241+
///
242+
/// This function should _not_ be const, to make sure we don't accept
243+
/// format_args!() and panic!() with arguments in const, even when not evaluated:
244+
///
245+
/// ```compile_fail,E0015
246+
/// const _: () = if false { panic!("a {}", "a") };
247+
/// ```
249248
#[inline]
250-
pub const fn new_v1<const P: usize, const A: usize>(
249+
pub fn new_v1<const P: usize, const A: usize>(
251250
pieces: &'a [&'static str; P],
252251
args: &'a [rt::Argument<'a>; A],
253252
) -> Arguments<'a> {
@@ -262,8 +261,15 @@ impl<'a> Arguments<'a> {
262261
/// 1. The `pieces` slice must be at least as long as `fmt`.
263262
/// 2. Every `rt::Placeholder::position` value within `fmt` must be a valid index of `args`.
264263
/// 3. Every `rt::Count::Param` within `fmt` must contain a valid index of `args`.
264+
///
265+
/// This function should _not_ be const, to make sure we don't accept
266+
/// format_args!() and panic!() with arguments in const, even when not evaluated:
267+
///
268+
/// ```compile_fail,E0015
269+
/// const _: () = if false { panic!("a {:1}", "a") };
270+
/// ```
265271
#[inline]
266-
pub const fn new_v1_formatted(
272+
pub fn new_v1_formatted(
267273
pieces: &'a [&'static str],
268274
args: &'a [rt::Argument<'a>],
269275
fmt: &'a [rt::Placeholder],

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
error[E0015]: cannot call non-const formatting macro in constant functions
2-
--> $DIR/format.rs:2:13
2+
--> $DIR/format.rs:2:5
33
|
44
LL | panic!("{:?}", 0);
5-
| ^^^^
5+
| ^^^^^^^^^^^^^^^^^
66
|
77
= note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
88

99
error[E0015]: cannot call non-const formatting macro in constant functions
10-
--> $DIR/format.rs:7:15
10+
--> $DIR/format.rs:7:5
1111
|
1212
LL | println!("{:?}", 0);
13-
| ^^^^
13+
| ^^^^^^^^^^^^^^^^^^^
1414
|
1515
= note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
1616
= 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)

0 commit comments

Comments
 (0)