rust-for-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v3] rust: static_assert: add optional message
@ 2025-03-25 21:10 Altan Ozlu
  2025-03-25 22:20 ` Benno Lossin
  0 siblings, 1 reply; 2+ messages in thread
From: Altan Ozlu @ 2025-03-25 21:10 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 an optional panic message for `static_assert!` macro.

It doesn't support argument formatting, because currently `assert!`
doesn't allow formatting in const contextes.

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>

---
Link to v2: https://lore.kernel.org/rust-for-linux/20250324210005.990827-2-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 v3] rust: static_assert: add optional message
  2025-03-25 21:10 [PATCH v3] rust: static_assert: add optional message Altan Ozlu
@ 2025-03-25 22:20 ` Benno Lossin
  0 siblings, 0 replies; 2+ messages in thread
From: Benno Lossin @ 2025-03-25 22:20 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 Tue Mar 25, 2025 at 10:10 PM CET, Altan Ozlu wrote:
> Add an optional panic message for `static_assert!` macro.

s/for/to the/

> It doesn't support argument formatting, because currently `assert!`
> doesn't allow formatting in const contextes.

I would make it even more clear:

    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>
>
> ---
> Link to v2: https://lore.kernel.org/rust-for-linux/20250324210005.990827-2-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.

Similarly here I think this fits better:

    You can supply an optional panic message after the expression. Currently only a string literal
    without formatting is supported due to constness limitations of the [`assert!`] macro.

---
Cheers,
Benno

> +///
>  /// The feature may be added to Rust in the future: see [RFC 2790].
>  ///
>  /// [`_Static_assert`]: https://en.cppreference.com/w/c/language/_Static_assert


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

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

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-03-25 21:10 [PATCH v3] rust: static_assert: add optional message Altan Ozlu
2025-03-25 22:20 ` Benno Lossin

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