rust-for-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2] rust: static_assert: add optional message
  2025-03-24 18:45 [PATCH] " Altan Ozlu
@ 2025-03-24 21:01 ` Altan Ozlu
  0 siblings, 0 replies; 2+ messages in thread
From: Altan Ozlu @ 2025-03-24 21:01 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 | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/rust/kernel/static_assert.rs b/rust/kernel/static_assert.rs
index 3115ee0ba8e9..b0569dccfb76 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 supports an optional argument for custom panic messages.
+/// The 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
@@ -25,10 +28,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
-- 
2.48.1



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

* Re: [PATCH v2] rust: static_assert: add optional message
@ 2025-03-24 22:07 Benno Lossin
  0 siblings, 0 replies; 2+ messages in thread
From: Benno Lossin @ 2025-03-24 22:07 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 10:01 PM CET, Altan Ozlu wrote:
> Add optional panic message for `static_assert!` macro.

s/Add/Add an/

> 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.

I don't think that this is the correct reason to give why it's not
supported. As you mentioned in another mail, the feature is a
compiler-internal feature and is not intended to be stabilized. Yes the
error reported by the compiler references the `const_format_args` macro,
but that's an implementation detail of `assert`, so I wouldn't put too
much weight into it.

Instead I would just say that `assert!` doesn't yet support formatting
in const contexts (maybe also add that to the documentation, so someone
will complain when that's been implemented and it flew under our radar).

> 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>

Usually when sending a new version of a patch, you don't send it in
reply to the previous version.

---
Cheers,
Benno

> ---
>  rust/kernel/static_assert.rs | 8 ++++++--
>  1 file changed, 6 insertions(+), 2 deletions(-)


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

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

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-03-24 22:07 [PATCH v2] rust: static_assert: add optional message Benno Lossin
  -- strict thread matches above, loose matches on Subject: below --
2025-03-24 18:45 [PATCH] " Altan Ozlu
2025-03-24 21:01 ` [PATCH v2] " 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).