about summary refs log tree commit diff
path: root/library/core/src
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2024-05-26 01:10:39 +0000
committerbors <bors@rust-lang.org>2024-05-26 01:10:39 +0000
commit75e2c5dcd0ddce0fe0eb3d4a2195cd51073c729b (patch)
tree3514db09772b2f31f920e8feb3570a5802b83f02 /library/core/src
parent0a59f113629aafb6e5ee55ad04a2d451a11d8466 (diff)
parent9763222f592dad3e3fdda987491da3878f1c1410 (diff)
downloadrust-75e2c5dcd0ddce0fe0eb3d4a2195cd51073c729b.tar.gz
rust-75e2c5dcd0ddce0fe0eb3d4a2195cd51073c729b.zip
Auto merge of #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: https://github.com/rust-lang/rust/pull/117804#discussion_r1612964362
Diffstat (limited to 'library/core/src')
-rw-r--r--library/core/src/fmt/mod.rs18
1 files changed, 7 insertions, 11 deletions
diff --git a/library/core/src/fmt/mod.rs b/library/core/src/fmt/mod.rs
index 1324fb6e056..c25bc5a1b13 100644
--- a/library/core/src/fmt/mod.rs
+++ b/library/core/src/fmt/mod.rs
@@ -338,23 +338,19 @@ pub struct Arguments<'a> {
 impl<'a> Arguments<'a> {
     #[inline]
     #[rustc_const_unstable(feature = "const_fmt_arguments_new", issue = "none")]
-    pub const fn new_const(pieces: &'a [&'static str]) -> Self {
-        if pieces.len() > 1 {
-            // Since panic!() expands to panic_fmt(format_args!()), using panic! here is both a
-            // bit silly and also significantly increases the amount of MIR generated by panics.
-            crate::panicking::panic_nounwind("invalid args");
-        }
+    pub const fn new_const<const N: usize>(pieces: &'a [&'static str; N]) -> Self {
+        const { assert!(N <= 1) };
         Arguments { pieces, fmt: None, args: &[] }
     }
 
     /// When using the format_args!() macro, this function is used to generate the
     /// Arguments structure.
     #[inline]
-    pub fn new_v1(pieces: &'a [&'static str], args: &'a [rt::Argument<'a>]) -> Arguments<'a> {
-        if pieces.len() < args.len() || pieces.len() > args.len() + 1 {
-            // See Arguments::new_const for why we don't use panic!.
-            crate::panicking::panic_nounwind("invalid args");
-        }
+    pub fn new_v1<const P: usize, const A: usize>(
+        pieces: &'a [&'static str; P],
+        args: &'a [rt::Argument<'a>; A],
+    ) -> Arguments<'a> {
+        const { assert!(P >= A && P <= A + 1, "invalid args") }
         Arguments { pieces, fmt: None, args }
     }