Move the checks for Arguments constructors to inline const · model-checking/verify-rust-std@29a1b3b (original) (raw)

Original file line number Diff line number Diff line change
@@ -338,23 +338,19 @@ pub struct Arguments<'a> {
338 338 impl<'a> Arguments<'a> {
339 339 #[inline]
340 340 #[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) };
347 343 Arguments { pieces, fmt: None, args: &[] }
348 344 }
349 345
350 346 /// When using the format_args!() macro, this function is used to generate the
351 347 /// Arguments structure.
352 348 #[inline]
353 -pub fn new_v1(pieces: &'a [&'static str], args: &'a [rt::Argument<'a>]) -> Arguments<'a> {
354 -if pieces.len() < args.len() |
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") }
358 354 Arguments { pieces, fmt: None, args }
359 355 }
360 356