NVIDIA GPU driver infrastructure
 help / color / mirror / Atom feed
From: "Gary Guo" <gary@garyguo.net>
To: "Alexandre Courbot" <acourbot@nvidia.com>, "Gary Guo" <gary@garyguo.net>
Cc: "Miguel Ojeda" <miguel.ojeda.sandonis@gmail.com>,
	"Yury Norov" <yury.norov@gmail.com>,
	"Miguel Ojeda" <ojeda@kernel.org>,
	"Boqun Feng" <boqun@kernel.org>,
	"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
	"Benno Lossin" <lossin@kernel.org>,
	"Andreas Hindborg" <a.hindborg@kernel.org>,
	"Alice Ryhl" <aliceryhl@google.com>,
	"Trevor Gross" <tmgross@umich.edu>,
	"Danilo Krummrich" <dakr@kernel.org>,
	"Daniel Almeida" <daniel.almeida@collabora.com>,
	"Tamir Duberstein" <tamird@kernel.org>,
	"Onur Özkan" <work@onurozkan.dev>,
	"John Hubbard" <jhubbard@nvidia.com>,
	"Alistair Popple" <apopple@nvidia.com>,
	"Timur Tabi" <ttabi@nvidia.com>,
	"Eliot Courtney" <ecourtney@nvidia.com>,
	"Zhi Wang" <zhiw@nvidia.com>,
	rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org,
	nova-gpu@lists.linux.dev
Subject: Re: [PATCH 1/2] rust: num: casts: replace const type narrowing methods with a macro
Date: Wed, 26 Aug 2026 13:06:07 +0100	[thread overview]
Message-ID: <DKYV1ZJKV57K.2XCTQQBGP18VH@garyguo.net> (raw)
In-Reply-To: <DKYTNXT3VX96.1L07CIR9SASUX@nvidia.com>

On Wed Aug 26, 2026 at 12:00 PM BST, Alexandre Courbot wrote:
> On Tue Aug 25, 2026 at 11:39 PM JST, Gary Guo wrote:
>> 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.
>
> I can see a use for a lint that warns about `as` expressions without a
> CAST comment, yes. But the warning should be unconditional imho -
> otherwise an `as` without a CAST comment could be intended as
> non-truncating, or it could just be an omission from the author of the
> code, and there is no good way to tell.

I think a "no comment means no truncation is going to happen" enforced by
linter is the point? I'd use "unsafe" as the parallel analogy. You don't see if
a function is unsafe or not, you use the absence of "unsafe" block as a
guarantee that there's no unsafe code within a block.

>
> `const_as!` basically provides everything we need to document intent and
> verify the behavior of non-truncating casts, and I think it is useful to
> keep as much as possible at the compiler level. KLint is not part of a
> regular build, and enabling it involves some effort that not everybody
> will go through. `const_as!` emits an error exactly when we need it to,
> without any extra tool, and is both simple in its implementation and its
> use.

Enabling it is quite easy actually, just that it is more involved to package if
one is not using rustup nor nix. Once it's installed the only thing needed is a
RUSTC=klint in make command line.

The bare installation does not however allow you to name the lints so everything
is at the default level and you cannot suppress it. The required feature
`register_tool` is being worked on and I intend to upstream klint support some
time this year.

Also, as long as we have CI systems to do a build, I think it's not necessary
for everyone to run it themselves.

>
> I am also not sure whether KLint could cover something like this, that
> requires post-monomorphization analysis:
>
>     fn f<const N: usize>() -> u16 {
>         N as u16
>     }

So for clippy (and likely klint if it implements the feature), it would be done
pre-mono so it would require a CAST comment without checking.

>
> Generally speaking, I believe a sane policy is to delegate tasks to the
> lowest layer that can do the job. Here the compiler is clearly capable.

That's why I want to get the task done by clippy/klint instead. Because it *is*
the compiler, which is lower layer than a macro.

Best,
Gary

  reply	other threads:[~2026-08-26 12:06 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-08-25  2:44 [PATCH 0/2] rust: num: casts: replace const type narrowing methods with a macro Alexandre Courbot
2026-08-25  2:44 ` [PATCH 1/2] " Alexandre Courbot
2026-08-25  7:18   ` Eliot Courtney
2026-08-26 11:16     ` Alexandre Courbot
2026-08-26 13:27       ` Alexandre Courbot
2026-08-25  8:25   ` Miguel Ojeda
2026-08-25 12:01     ` Gary Guo
2026-08-25 14:26       ` Alexandre Courbot
2026-08-25 14:39         ` Gary Guo
2026-08-26 11:00           ` Alexandre Courbot
2026-08-26 12:06             ` Gary Guo [this message]
2026-08-25 13:54     ` Alexandre Courbot
2026-08-25 14:02     ` Danilo Krummrich
2026-08-25 14:11       ` Gary Guo
2026-08-25 14:30       ` Alexandre Courbot
2026-08-25 12:04   ` Gary Guo
2026-08-25  2:44 ` [PATCH 2/2] gpu: nova-core: use kernel lossless integer conversion module Alexandre Courbot
2026-08-25  5:27   ` Eliot Courtney
2026-08-27  1:36     ` Alexandre Courbot

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=DKYV1ZJKV57K.2XCTQQBGP18VH@garyguo.net \
    --to=gary@garyguo.net \
    --cc=a.hindborg@kernel.org \
    --cc=acourbot@nvidia.com \
    --cc=aliceryhl@google.com \
    --cc=apopple@nvidia.com \
    --cc=bjorn3_gh@protonmail.com \
    --cc=boqun@kernel.org \
    --cc=dakr@kernel.org \
    --cc=daniel.almeida@collabora.com \
    --cc=ecourtney@nvidia.com \
    --cc=jhubbard@nvidia.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lossin@kernel.org \
    --cc=miguel.ojeda.sandonis@gmail.com \
    --cc=nova-gpu@lists.linux.dev \
    --cc=ojeda@kernel.org \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=tamird@kernel.org \
    --cc=tmgross@umich.edu \
    --cc=ttabi@nvidia.com \
    --cc=work@onurozkan.dev \
    --cc=yury.norov@gmail.com \
    --cc=zhiw@nvidia.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