From: "Gary Guo" <gary@garyguo.net>
To: "Alexandre Courbot" <acourbot@nvidia.com>,
"Yilin Chen" <1479826151@qq.com>
Cc: "Gary Guo" <gary@garyguo.net>, <rust-for-linux@vger.kernel.org>,
<linux-kernel@vger.kernel.org>, <yury.norov@gmail.com>,
<ojeda@kernel.org>
Subject: Re: [PATCH] rust: bitfield: require Zeroable storage for Zeroable impl
Date: Mon, 21 Sep 2026 11:05:53 +0100 [thread overview]
Message-ID: <DLKWS377R3K0.1XFDX36YZWRQU@garyguo.net> (raw)
In-Reply-To: <DLKT85RM6XFH.1FHCL3Q40L1I7@nvidia.com>
On Mon Sep 21, 2026 at 8:18 AM BST, Alexandre Courbot wrote:
> On Mon Sep 21, 2026 at 3:32 PM JST, Yilin Chen wrote:
>> Hi Gary,
>>
>> Resending my reply for visibility on the mailing list.
>>
>> You are right that a bitfield with at least one field generates a use of
>> `Bounded<$storage, ...>`, which requires the storage type to implement
>> `Integer`. However, `bitfield!` also accepts an empty field list. In that
>> case, no `Bounded` use is generated, so the `Integer` requirement is
>> absent, while the macro still generates the unconditional `Zeroable`
>> implementation.
>>
>> For example:
>>
>> use core::num::NonZeroU32;
>> use pin_init::Zeroable;
>>
>> bitfield! {
>> struct Bad(NonZeroU32) {}
>> }
>>
>> fn check_bad_zeroable() {
>> let _: Bad = <Bad as Zeroable>::zeroed();
>> }
>>
>> This currently compiles.
>
> No it doesn't.
>
> error[E0277]: the trait bound `core::num::NonZero<u32>: kernel::mem::AsRepr` is not satisfied
> 364 | / bitfield! {
> 365 | | struct Bad(NonZeroU32) {}
> 366 | | }
> | |_________^ the trait `kernel::mem::AsRepr` is not implemented for `core::num::NonZero<u32>`
On the other hand, it makes sense for `NonZero<u32>` to implement
`AsRepr<Repr = u32>`. So I think we do need better defense.
Given that fundamental `bitfield!` needs a plain integer type, I don't think we
want a `Zeroable` bound, but rather just enforce it has to be integer
primitives.
Given the recent `Integer` sealing, ad a check that $storage implements
`Integer` is probably the best check.
Best,
Gary
prev parent reply other threads:[~2026-09-21 10:05 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-09-13 15:34 Yilin Chen
2026-09-13 19:37 ` Gary Guo
2026-09-21 6:32 ` Yilin Chen
2026-09-21 7:18 ` Alexandre Courbot
2026-09-21 10:05 ` Gary Guo [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=DLKWS377R3K0.1XFDX36YZWRQU@garyguo.net \
--to=gary@garyguo.net \
--cc=1479826151@qq.com \
--cc=acourbot@nvidia.com \
--cc=linux-kernel@vger.kernel.org \
--cc=ojeda@kernel.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=yury.norov@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®