Re: [PATCH v2 1/3] rust: extract `bitfield!` macro from `register!`

John Hubbard
2026-04-15T23:18:09Z

On 4/12/26 7:29 PM, Eliot Courtney wrote:
> On Thu Apr 9, 2026 at 11:58 PM JST, Alexandre Courbot wrote:
...
> In the nova version of bitfield we had @check_field_bounds. If we put
> in the bit numbers the wrong way around, this patch gives a compile
> error like:
>
> ```
> attempt to compute `4_u32 - 7_u32`, which would overflow
> ```
>
> The original nova version looks like it used build_assert, but I think
> we can do it with const assert!, so we should be able to get a better
> build error message for this case:
>
> ```
> const _: () = assert!($hi >= $lo, "bitfield: hi bit must be >= lo bit");
> ```
>
> With just that we get an extra build error, although it still spams the
> confusing build error. We could also consider adding a function like:
>
> ```
> pub const fn bitfield_width(hi: u32, lo: u32) -> u32 {
> assert!(hi >= lo, "bitfield: hi bit must be >= lo bit");
> hi + 1 - lo
> }
> ```
>
> Using this instead gets rid of some confusing build errors since we can
> also use it in type bounds. But to get rid of all of them we would need
> to do it for the mask etc and others.
>
> WDYT?

Just an admin note: if you do any or all of the above, please let's make
it a separate patch, so that this first patch remains just a "move the
code" (almost, anyway).

thanks,
--
John Hubbard