From 0fa5f8c877cae959de7cf6c3dc054e23e7ebcd75 Mon Sep 17 00:00:00 2001 From: Altan Ozlu Date: Wed, 26 Mar 2025 20:25:36 +0000 Subject: 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 Link: https://github.com/Rust-for-Linux/linux/issues/1149 Signed-off-by: Altan Ozlu Reviewed-by: Benno Lossin Reviewed-by: Trevor Gross Link: https://lore.kernel.org/r/20250326202520.1176162-2-altan@ozlu.eu Signed-off-by: Miguel Ojeda --- rust/kernel/static_assert.rs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) (limited to 'rust/kernel/static_assert.rs') diff --git a/rust/kernel/static_assert.rs b/rust/kernel/static_assert.rs index 3115ee0ba8e9..d8120f838260 100644 --- a/rust/kernel/static_assert.rs +++ b/rust/kernel/static_assert.rs @@ -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)?); }; } -- cgit From de7cd3e4d6387df6a5ae8c4c32ff0479ebe0efb5 Mon Sep 17 00:00:00 2001 From: Igor Korotin Date: Mon, 19 May 2025 17:45:53 +0100 Subject: rust: use absolute paths in macros referencing core and kernel Macros and auto-generated code should use absolute paths, `::core::...` and `::kernel::...`, for core and kernel references. This prevents issues where user-defined modules named `core` or `kernel` could be picked up instead of the `core` or `kernel` crates. Thus clean some references up. Suggested-by: Benno Lossin Closes: https://github.com/Rust-for-Linux/linux/issues/1150 Signed-off-by: Igor Korotin Reviewed-by: Benno Lossin Acked-by: Greg Kroah-Hartman Link: https://lore.kernel.org/r/20250519164615.3310844-1-igor.korotin.linux@gmail.com [ Applied `rustfmt`. Reworded slightly. - Miguel ] Signed-off-by: Miguel Ojeda --- rust/kernel/static_assert.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'rust/kernel/static_assert.rs') diff --git a/rust/kernel/static_assert.rs b/rust/kernel/static_assert.rs index d8120f838260..a57ba14315a0 100644 --- a/rust/kernel/static_assert.rs +++ b/rust/kernel/static_assert.rs @@ -34,6 +34,6 @@ #[macro_export] macro_rules! static_assert { ($condition:expr $(,$arg:literal)?) => { - const _: () = core::assert!($condition $(,$arg)?); + const _: () = ::core::assert!($condition $(,$arg)?); }; } -- cgit