Skip to content

Commit 1cc16ba

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 10a0adc commit 1cc16ba

File tree

1 file changed

+27
-21
lines changed

1 file changed

+27
-21
lines changed

core/src/fmt/rt.rs

Lines changed: 27 additions & 21 deletions
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],

0 commit comments

Comments
 (0)