Skip to content

Commit 0fa5f8c

Browse files
altanozluojeda
authored andcommitted
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 <[email protected]> Link: #1149 Signed-off-by: Altan Ozlu <[email protected]> Reviewed-by: Benno Lossin <[email protected]> Reviewed-by: Trevor Gross <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Miguel Ojeda <[email protected]>
1 parent 79d04e7 commit 0fa5f8c

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

rust/kernel/static_assert.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
///
77
/// Similar to C11 [`_Static_assert`] and C++11 [`static_assert`].
88
///
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+
///
913
/// The feature may be added to Rust in the future: see [RFC 2790].
1014
///
1115
/// [`_Static_assert`]: https://en.cppreference.com/w/c/language/_Static_assert
@@ -25,10 +29,11 @@
2529
/// x + 2
2630
/// }
2731
/// static_assert!(f(40) == 42);
32+
/// static_assert!(f(40) == 42, "f(x) must add 2 to the given input.");
2833
/// ```
2934
#[macro_export]
3035
macro_rules! static_assert {
31-
($condition:expr) => {
32-
const _: () = core::assert!($condition);
36+
($condition:expr $(,$arg:literal)?) => {
37+
const _: () = core::assert!($condition $(,$arg)?);
3338
};
3439
}

0 commit comments

Comments
 (0)