rust: static_assert: add optional message
PATCH 1/1 rust: static_assert: add optional message
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.
--- rust/kernel/static_assert.rs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-)
rust/kernel/static_assert.rs +6 -3
@@ -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
Signature · 1 line
2.48.1
Quoted 6 lines · Altan Ozlu
Add optional panic message for
static_assert!macro.It doesn't support argument formatting, due to
const_format_argsbeing 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).
Quoted 1 line
+/// It has optional argument for custom panic messages.
"It supports an optional..."
Quoted 1 line
+/// Panic message doesn't support formatting.
"The panic..."
rust/kernel/static_assert.rs line 30
Quoted 2 lines
rust/kernel/static_assert.rs +1
-1-/// 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
Quoted 2 lines · Miguel Ojeda
I wondered if we should use
*instead of+nevertheless, so that
s/instead of +/instead of ?/
Cheers,
Miguel
Altan Ozlu
AppliedQuoted 7 lines · Miguel Ojeda
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 ?
Quoted 9 lines
Quoted 2 lines
+/// It has optional argument for custom panic messages.
"It supports an optional..."
Quoted 1 line
+/// Panic message doesn't support formatting.
Quoted 7 lines
"The panic..."
rust/kernel/static_assert.rs line 30
Quoted 2 lines
rust/kernel/static_assert.rs +1
-1-/// 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.
Signature · 2 lines
Kind regards, Altan
Quoted 4 lines · Altan Ozlu
const_format_argsmight 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
PATCH v2 1/1 rust: static_assert: add optional message
In reply to Altan OzluAdd 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.
--- rust/kernel/static_assert.rs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)
rust/kernel/static_assert.rs +6 -2
@@ -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
Signature · 1 line
2.48.1