rust-for-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] rust: static_assert: add optional message
@ 2025-03-24 18:45 Altan Ozlu
  2025-03-24 19:06 ` Miguel Ojeda
  2025-03-24 21:01 ` [PATCH v2] " Altan Ozlu
  0 siblings, 2 replies; 8+ messages in thread
From: Altan Ozlu @ 2025-03-24 18:45 UTC (permalink / raw)
  To: Miguel Ojeda, Alex Gaynor
  Cc: Boqun Feng, Gary Guo, Björn Roy Baron, Benno Lossin,
	Andreas Hindborg, Alice Ryhl, Trevor Gross, Danilo Krummrich,
	rust-for-linux, Altan Ozlu

Add optional panic message for `static_assert!` macro.

It doesn't support argument formatting, due to `const_format_args`
being experimental. It will fail to compile if a message with argument
is given.

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>
---
 rust/kernel/static_assert.rs | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)

diff --git a/rust/kernel/static_assert.rs b/rust/kernel/static_assert.rs
index 3115ee0ba8e9..27771dc2988a 100644
--- a/rust/kernel/static_assert.rs
+++ b/rust/kernel/static_assert.rs
@@ -6,6 +6,9 @@
 ///
 /// Similar to C11 [`_Static_assert`] and C++11 [`static_assert`].
 ///
+/// It has optional argument for custom panic messages.
+/// Panic message doesn't support formatting.
+///
 /// The feature may be added to Rust in the future: see [RFC 2790].
 ///
 /// [`_Static_assert`]: https://en.cppreference.com/w/c/language/_Static_assert
@@ -24,11 +27,11 @@
 /// const fn f(x: i32) -> i32 {
 ///     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:tt)?) => {
+        const _: () = core::assert!($condition $(,$arg)?);
     };
 }

base-commit: a0b539ad369fe434fe488faf92d4ae770a27a90f
-- 
2.48.1



^ permalink raw reply related	[flat|nested] 8+ messages in thread
* Re: [PATCH] rust: static_assert: add optional message
@ 2025-03-24 19:24 Benno Lossin
  2025-03-24 21:49 ` Altan Ozlu
  0 siblings, 1 reply; 8+ messages in thread
From: Benno Lossin @ 2025-03-24 19:24 UTC (permalink / raw)
  To: Altan Ozlu, Miguel Ojeda, Alex Gaynor
  Cc: Boqun Feng, Gary Guo, Björn Roy Baron, Andreas Hindborg,
	Alice Ryhl, Trevor Gross, Danilo Krummrich, rust-for-linux

On Mon Mar 24, 2025 at 7:45 PM CET, Altan Ozlu wrote:
> Add optional panic message for `static_assert!` macro.
>
> It doesn't support argument formatting, due to `const_format_args`
> being experimental. It will fail to compile if a message with argument
> is given.
>
> 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>
> ---
>  rust/kernel/static_assert.rs | 9 ++++++---
>  1 file changed, 6 insertions(+), 3 deletions(-)
>
> diff --git a/rust/kernel/static_assert.rs b/rust/kernel/static_assert.rs
> index 3115ee0ba8e9..27771dc2988a 100644
> --- a/rust/kernel/static_assert.rs
> +++ b/rust/kernel/static_assert.rs
> @@ -6,6 +6,9 @@
>  ///
>  /// Similar to C11 [`_Static_assert`] and C++11 [`static_assert`].
>  ///
> +/// It has optional argument for custom panic messages.
> +/// Panic message doesn't support formatting.
> +///
>  /// The feature may be added to Rust in the future: see [RFC 2790].
>  ///
>  /// [`_Static_assert`]: https://en.cppreference.com/w/c/language/_Static_assert
> @@ -24,11 +27,11 @@
>  /// const fn f(x: i32) -> i32 {
>  ///     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:tt)?) => {

Shouldn't `$arg` be a `literal` fragment?

---
Cheers,
Benno

> +        const _: () = core::assert!($condition $(,$arg)?);
>      };
>  }
>
> base-commit: a0b539ad369fe434fe488faf92d4ae770a27a90f



^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2025-03-24 22:07 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-03-24 18:45 [PATCH] rust: static_assert: add optional message Altan Ozlu
2025-03-24 19:06 ` Miguel Ojeda
2025-03-24 21:11   ` Miguel Ojeda
2025-03-24 21:44   ` Altan Ozlu
2025-03-24 22:07     ` Miguel Ojeda
2025-03-24 21:01 ` [PATCH v2] " Altan Ozlu
  -- strict thread matches above, loose matches on Subject: below --
2025-03-24 19:24 [PATCH] " Benno Lossin
2025-03-24 21:49 ` Altan Ozlu

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).