Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

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.

Suggested-by: Miguel Ojeda <ojeda@kernel.org>
Link: https://github.com/Rust-for-Linux/linux/issues/1149
Signed-off-by: Altan Ozlu <altan@ozlu.eu>
Reviewed-by: Benno Lossin <benno.lossin@proton.me>
Reviewed-by: Trevor Gross <tmgross@umich.edu>
Link: https://lore.kernel.org/r/20250326202520.1176162-2-altan@ozlu.eu
Signed-off-by: Miguel Ojeda <ojeda@kernel.org>

authored by

Altan Ozlu and committed by
Miguel Ojeda
0fa5f8c8 79d04e73

+7 -2
+7 -2
rust/kernel/static_assert.rs
··· 6 6 /// 7 7 /// Similar to C11 [`_Static_assert`] and C++11 [`static_assert`]. 8 8 /// 9 + /// An optional panic message can be supplied after the expression. 10 + /// Currently only a string literal without formatting is supported 11 + /// due to constness limitations of the [`assert!`] macro. 12 + /// 9 13 /// The feature may be added to Rust in the future: see [RFC 2790]. 10 14 /// 11 15 /// [`_Static_assert`]: https://en.cppreference.com/w/c/language/_Static_assert ··· 29 25 /// x + 2 30 26 /// } 31 27 /// static_assert!(f(40) == 42); 28 + /// static_assert!(f(40) == 42, "f(x) must add 2 to the given input."); 32 29 /// ``` 33 30 #[macro_export] 34 31 macro_rules! static_assert { 35 - ($condition:expr) => { 36 - const _: () = core::assert!($condition); 32 + ($condition:expr $(,$arg:literal)?) => { 33 + const _: () = core::assert!($condition $(,$arg)?); 37 34 }; 38 35 }