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; 7+ 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] 7+ messages in thread

* Re: [PATCH] rust: static_assert: add optional message
  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 21:01 ` [PATCH v2] " Altan Ozlu
  1 sibling, 2 replies; 7+ messages in thread
From: Miguel Ojeda @ 2025-03-24 19:06 UTC (permalink / raw)
  To: Altan Ozlu
  Cc: Miguel Ojeda, Alex Gaynor, Boqun Feng, Gary Guo,
	Björn Roy Baron, Benno Lossin, Andreas Hindborg, Alice Ryhl,
	Trevor Gross, Danilo Krummrich, rust-for-linux

On Mon, Mar 24, 2025 at 7:45 PM Altan Ozlu <altan@ozlu.eu> 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.

I wondered if we should use `*` instead of `+` nevertheless, so that
it eventually "automatically works", but I guess the advantages of
using `+` are that we will not forget to update the docs eventually
(since the restriction is mentioned), and also using `+` means people
do not mistakenly use it in beta/nightly (since it seems it does not
require to enable the feature explicitly, from a quick test in
Compiler Explorer).

> +/// It has optional argument for custom panic messages.

"It supports an optional..."

> +/// Panic message doesn't support formatting.

"The panic..."

> -/// static_assert!(f(40) == 42);
> +/// static_assert!(f(40) == 42, "f(x) must add 2 to the given input.");

It could be nice to showcase both the case with and without the message.

Anyway, the patch looks fine :) No need to send another version for
those nits -- let's see what others think first.

Thanks!

Cheers,
Miguel

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

* [PATCH v2] rust: static_assert: add optional message
  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:01 ` Altan Ozlu
  1 sibling, 0 replies; 7+ 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] 7+ messages in thread

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

On Mon, Mar 24, 2025 at 8:06 PM Miguel Ojeda
<miguel.ojeda.sandonis@gmail.com> wrote:
>
> I wondered if we should use `*` instead of `+` nevertheless, so that

s/instead of `+`/instead of `?`/

Cheers,
Miguel

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

* Re: [PATCH] rust: static_assert: add optional message
  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
  1 sibling, 1 reply; 7+ messages in thread
From: Altan Ozlu @ 2025-03-24 21:44 UTC (permalink / raw)
  To: miguel.ojeda.sandonis
  Cc: a.hindborg, alex.gaynor, aliceryhl, altan, benno.lossin,
	bjorn3_gh, boqun.feng, dakr, gary, ojeda, rust-for-linux, tmgross

On Monday, March 24th, 2025 at 20:06, Miguel Ojeda <miguel.ojeda.sandonis@gmail.com> wrote:

> I wondered if we should use `*` instead of `+` nevertheless, so that
> it eventually "automatically works", but I guess the advantages of
> using `+` are that we will not forget to update the docs eventually
> (since the restriction is mentioned), and also using `+` means people
> do not mistakenly use it in beta/nightly (since it seems it does not
> require to enable the feature explicitly, from a quick test in
> Compiler Explorer).

`const_format_args` might not stabilize in the future,
please see https://github.com/rust-lang/rust/issues/108595#issuecomment-1457030352
So it make sense to keep it as `?`

> 
> > +/// It has optional argument for custom panic messages.
> 
> 
> "It supports an optional..."
> 
> > +/// Panic message doesn't support formatting.
> 
> 
> "The panic..."
> 
> > -/// static_assert!(f(40) == 42);
> > +/// static_assert!(f(40) == 42, "f(x) must add 2 to the given input.");
> 
> 
> It could be nice to showcase both the case with and without the message.

I have applied changes for nits you mentioned in PATCH v2, thanks.
-- 
Kind regards,
Altan



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

* Re: [PATCH] rust: static_assert: add optional message
  2025-03-24 21:44   ` Altan Ozlu
@ 2025-03-24 22:07     ` Miguel Ojeda
  0 siblings, 0 replies; 7+ messages in thread
From: Miguel Ojeda @ 2025-03-24 22:07 UTC (permalink / raw)
  To: Altan Ozlu
  Cc: a.hindborg, alex.gaynor, aliceryhl, benno.lossin, bjorn3_gh,
	boqun.feng, dakr, gary, ojeda, rust-for-linux, tmgross

On Mon, Mar 24, 2025 at 10:44 PM Altan Ozlu <altan@ozlu.eu> wrote:
>
> `const_format_args` might not stabilize in the future,
> please see https://github.com/rust-lang/rust/issues/108595#issuecomment-1457030352
> So it make sense to keep it as `?`

That feature is not meant to be stable, but the docs of `format_args!` say:

    This macro will be removed once format_args is allowed in const contexts.

That is, it seems like the intention is to eventually allow
`format_args` to work, and I assume then `panic!`, `assert!` etc. with
formatting will eventually work.

At least, I don't see a reason Rust wouldn't want it eventually, and a
quick test in nightly seems to work for a simple case.

Cheers,
Miguel

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

* Re: [PATCH v2] rust: static_assert: add optional message
@ 2025-03-24 22:07 Benno Lossin
  0 siblings, 0 replies; 7+ 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] 7+ messages in thread

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

Thread overview: 7+ 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 22:07 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).