Re: [PATCH 1/2] rust: num: casts: replace const type narrowing methods with a macro

"Gary Guo" <[email protected]>
Newsgroups dev.linux.lists.nova-gpu,org.kernel.vger.linux-kernel,org.kernel.vger.rust-for-linux
Message-ID <[email protected]>
On Tue Aug 25, 2026 at 3:26 PM BST, Alexandre Courbot wrote:
> On Tue Aug 25, 2026 at 9:01 PM JST, Gary Guo wrote:
>> On Tue Aug 25, 2026 at 9:25 AM BST, Miguel Ojeda wrote:
>>> What I wouldn't want is a raw `as`, because the point of the saga we
>>> started a long time ago is to introduce better tools that allow us to
>>> get rid of the almighty `as` into weaker (i.e. safer) options, even if
>>> some uses of `as` may be "obviously right".
>>
>> I think that is rather a linting issue, not something that warrants extra code
>> in kernel. We have been requesting some extra clippy features and I think that
>> is the correct way to go, not add a ton of methods and macros. Yes, it wasn't
>> moving on clippy end, but I could add a feature to klint instead?
>>
>> Do you think we still need all these extra function and macros if we
>> can get clippy (or klint) to enforce CAST comments?
>>
>> I can imagine the following rules that would practically solve all the footgun
>> of `as` numerical casts without having to use awkward syntax:
>>
>> * widening casts are allowed
>> * narrowing casts is disallowed unless CAST comment exists, except where its
>>   value is constant and truncation does not happen.
>
> These rules classify casts by width, but the footguns really are about
> which values are actually being converted.
>
> In particular for value narrowing we still end up with CAST comments,
> whose existence a lint can check, but not their correctness (for
> instance, a bindgen-provided constant that changes in a breaking way).
> `const_as!` lets us drop them altogether.

The rule says "except where its value is constant and truncation does not
happen".

So I'd imagine just writing

    bindings::FOO as u32

and *NOT* have CAST comment, and a warning being generated if truncation
happens.

Best,
Gary
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.