Skip to contentSkip to conversation

rust: static_assert: add optional message

PATCH Applied Altan Ozlu 3 participants 7 messages Mar 26, 2025 – May 12, 2025 Rust

lore
Share & tools
lore

Altan Ozlu

author
#0 lore raw

PATCH v4 1/1 rust: static_assert: add optional message

Add an optional panic message to the static_assert! macro.

The panic message doesn't support argument formatting, because the assert! macro only supports formatting in non-const contexts.

---
Link to v3: https://lore.kernel.org/rust-for-linux/[email protected]/

---
 rust/kernel/static_assert.rs | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
rust/kernel/static_assert.rs +7 -2
index 3115ee0ba8e9..d8120f838260 100644
@@ -6,6 +6,10 @@
 ///
 /// Similar to C11 [`_Static_assert`] and C++11 [`static_assert`].
 ///
+/// An optional panic message can be supplied after the expression.
+/// Currently only a string literal without formatting is supported
+/// due to constness limitations of the [`assert!`] macro.
+///
 /// The feature may be added to Rust in the future: see [RFC 2790].
 ///
 /// [`_Static_assert`]: https://en.cppreference.com/w/c/language/_Static_assert
@@ -25,10 +29,11 @@
 ///     x + 2
 /// }
 /// static_assert!(f(40) == 42);
+/// static_assert!(f(40) == 42, "f(x) must add 2 to the given input.");
 /// ```
 #[macro_export]
 macro_rules! static_assert {
-    ($condition:expr) => {
-        const _: () = core::assert!($condition);
+    ($condition:expr $(,$arg:literal)?) => {
+        const _: () = core::assert!($condition $(,$arg)?);
     };
 }

base-commit: a0b539ad369fe434fe488faf92d4ae770a27a90f

Signature · 1 line
2.48.1
author
#1 lore raw

Hello Miguel,

Patch has been reviewed by Benno Lossin, can you check when you have time ?

Signature · 2 lines
Kind regards,
Altan
#2 lore raw
Quoted 2 lines · Altan Ozlu

Patch has been reviewed by Benno Lossin, can you check when you have time ?

If you mean applying it, I have not picked any patch for the next cycle yet.

Cheers,
Miguel

author
#3 lore raw
Quoted 2 lines

If you mean applying it, I have not picked any patch for the next cycle yet.

Yes I meant applying, I didn't knew about it. Thanks, have a nice day!

Signature · 2 lines
Kind regards,
Altan
reviewer Reviewed-by on v4 1/1
#4 lore raw
In reply to Altan Ozlu
Quoted 8 lines · Altan Ozlu

Add an optional panic message to the static_assert! macro.

The panic message doesn't support argument formatting, because the assert! macro only supports formatting in non-const contexts.

I think this could be "Closes:"

Quoted 7 lines

---
Link to v3: https://lore.kernel.org/rust-for-linux/[email protected]/

---
 rust/kernel/static_assert.rs | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
#5 lore raw
Quoted 2 lines · Trevor Gross

I think this could be "Closes:"

The documentation isn't very clear about it -- my reading is that Closes is meant for bugs, so I have always used Link for feature suggestions, but it would be reasonable to use it for that too like in other trackers.

Thanks for reviewing!

Cheers,
Miguel