Re: [PATCH 2/2] rust: add `const_assert!` macro
From: Gary Guo
Date: Fri Feb 06 2026 - 17:29:23 EST
On Fri Feb 6, 2026 at 10:21 PM GMT, John Hubbard wrote:
> On 2/6/26 9:12 AM, Gary Guo wrote:
>> From: Gary Guo <gary@xxxxxxxxxxx>
>>
>> The macro is a more powerful version of `static_assert!` for use inside
>> function contexts. This is powered by inline consts, so enable the feature
>> for old compiler versions that does not have it stably.
>
> This is nice. A nit below about documentation.
>
>>
>> The `build_assert!` doc is refined to recommend it where possible.
>
> Another piece of the puzzle...see below to continue the treasure
> hunt.
>
>>
>> While it is possible already to write `const { assert!(...) }`, this
>> provides a short hand that is more uniform with other assertions. It also
>> formats nicer with rustfmt where it will not be formatted into multiple
>> lines.
>>
>> Two users that would route via the Rust tree are converted.
>>
>> Signed-off-by: Gary Guo <gary@xxxxxxxxxxx>
>> ---
>> rust/kernel/build_assert.rs | 55 +++++++++++++++++++++++++++++++++----
>> rust/kernel/num/bounded.rs | 24 ++++++----------
>> rust/kernel/prelude.rs | 2 +-
>> rust/kernel/ptr.rs | 18 ++++++------
>> scripts/Makefile.build | 3 +-
>> 5 files changed, 71 insertions(+), 31 deletions(-)
>>
>> diff --git a/rust/kernel/build_assert.rs b/rust/kernel/build_assert.rs
>> index d464494d430a..e40f0227e1ef 100644
>> --- a/rust/kernel/build_assert.rs
>> +++ b/rust/kernel/build_assert.rs
>> @@ -41,6 +41,45 @@ macro_rules! static_assert {
>> };
>> }
>>
>> +/// Assertion during constant evaluation.
>> +///
>> +/// This is a more powerful version of `static_assert` that can refer to generics inside functions
>> +/// or implementation blocks. However, it also have a limitation where it can only appear in places
>> +/// where statements can appear; for example, you cannot use it as an item in the module.
>> +///
>> +/// [`static_assert!`] should be preferred where possible.
>
> Over what? There are 3 different assertion types. If it is actually
> possible to list a clear order of preference, then let's list all
> 3 in order, rather than having a scattering around of "A is
> better than B", "C is sometimes worse than A", and then the
> reader has to play treasure hunt to work it out. haha :)
I am trying to avoid listing all assertion macros in all assertions macros,
that's just going to be duplications.
How about remove `#[doc(hidden)]` on `mod build_assert` and enumerate all
assertions in the module documentation and give them rankings there?
Best,
Gary