Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit f54c557

Browse files
committedMay 26, 2024
Auto merge of rust-lang#125518 - saethlin:check-arguments-new-in-const, r=joboet
Move the checks for Arguments constructors to inline const Thanks `@Skgland` for pointing out this opportunity: rust-lang#117804 (comment)
2 parents bf3ca98 + 29a1b3b commit f54c557

File tree

1 file changed

+7
-11
lines changed

1 file changed

+7
-11
lines changed
 

‎core/src/fmt/mod.rs

+7-11
Original file line numberDiff line numberDiff line change
@@ -338,23 +338,19 @@ pub struct Arguments<'a> {
338338
impl<'a> Arguments<'a> {
339339
#[inline]
340340
#[rustc_const_unstable(feature = "const_fmt_arguments_new", issue = "none")]
341-
pub const fn new_const(pieces: &'a [&'static str]) -> Self {
342-
if pieces.len() > 1 {
343-
// Since panic!() expands to panic_fmt(format_args!()), using panic! here is both a
344-
// bit silly and also significantly increases the amount of MIR generated by panics.
345-
crate::panicking::panic_nounwind("invalid args");
346-
}
341+
pub const fn new_const<const N: usize>(pieces: &'a [&'static str; N]) -> Self {
342+
const { assert!(N <= 1) };
347343
Arguments { pieces, fmt: None, args: &[] }
348344
}
349345

350346
/// When using the format_args!() macro, this function is used to generate the
351347
/// Arguments structure.
352348
#[inline]
353-
pub fn new_v1(pieces: &'a [&'static str], args: &'a [rt::Argument<'a>]) -> Arguments<'a> {
354-
if pieces.len() < args.len() || pieces.len() > args.len() + 1 {
355-
// See Arguments::new_const for why we don't use panic!.
356-
crate::panicking::panic_nounwind("invalid args");
357-
}
349+
pub fn new_v1<const P: usize, const A: usize>(
350+
pieces: &'a [&'static str; P],
351+
args: &'a [rt::Argument<'a>; A],
352+
) -> Arguments<'a> {
353+
const { assert!(P >= A && P <= A + 1, "invalid args") }
358354
Arguments { pieces, fmt: None, args }
359355
}
360356

0 commit comments

Comments
 (0)
Failed to load comments.