All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Eliot Courtney" <ecourtney@nvidia.com>
To: "Miguel Ojeda" <miguel.ojeda.sandonis@gmail.com>,
	"Eliot Courtney" <ecourtney@nvidia.com>
Cc: "Alexandre Courbot" <acourbot@nvidia.com>,
	"Yury Norov" <yury.norov@gmail.com>,
	"Miguel Ojeda" <ojeda@kernel.org>,
	"Boqun Feng" <boqun@kernel.org>, "Gary Guo" <gary@garyguo.net>,
	"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>,
	"David Airlie" <airlied@gmail.com>,
	"Simona Vetter" <simona@ffwll.ch>,
	"John Hubbard" <jhubbard@nvidia.com>,
	"Alistair Popple" <apopple@nvidia.com>,
	"Timur Tabi" <ttabi@nvidia.com>,
	rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org,
	nova-gpu@lists.linux.dev, dri-devel@lists.freedesktop.org,
	dri-devel <dri-devel-bounces@lists.freedesktop.org>
Subject: Re: [PATCH v4 1/2] rust: num: add cv! macro to create values from constant expressions
Date: Tue, 13 Oct 2026 14:52:43 +0900	[thread overview]
Message-ID: <DM3H68S4CR5K.3K9MG80U5Y9AA@nvidia.com> (raw)
In-Reply-To: <CANiq72m0PLhT=-ODzQtX7k3jn6=XXpESDUbnHcAmR-1gFqR7uQ@mail.gmail.com>

On Sat Oct 3, 2026 at 1:59 AM JST, Miguel Ojeda wrote:
> On Thu, Sep 17, 2026 at 10:35 AM Eliot Courtney <ecourtney@nvidia.com> wrote:
>>
>> from constants without a verbose turbofish syntax.
>> For example, `const { NonZero::new(1).unwrap() }` can be written as
>
> Word wrap or newline?
>
>> +/// Creates a value from an integer constant expression, with validity checked at build time.
>
> Maybe:
>
>   ... a constant value ("cv") ...
>
> since we explain that in the commit message, but not in the actual
> documentation :)
>
> I would also say "at compile time", and possibly mention it is usable
> in const context (we use "build time" for things like `build_assert!`,
> which can be much later, e.g. link time).
>
>> +    };
>> +    // Using `FromConst<V>::VALUE` means const generic expressions can't be used, since it requires
>
> Newline between these two lines?
>
>> +/// Types that can be created from an integer constant expression validated at build time.
>
> Ditto (please see above).
>
>> +/// Implement this trait to make a type usable with [`cv!`]. Use the [`cv`] macro, not this trait
>
> The second intra-doc link should also have a `!` for consistency.
>
>> +    /// Fails the build if `V` is not a valid value for `Self`.
>
> [`Self`]
>
>> +                // CAST: the macro is only used for types up to 64 bits wide, so `MIN` and `MAX`
>
> "CAST: The"
>
>> +                // CAST: the assert above confirmed that `V` fits in `$type`.
>
> Ditto.
>
>> +                // SAFETY: the assert above confirmed that `value` can be represented within `N`
>
> Ditto.
>
> Acked-by: Miguel Ojeda <ojeda@kernel.org>
>
> One thing before that, though: are these assertions expected to fail?
>
>     const X: i64 = 1 << 31;
>     const Y: i64 = cv!(1 << 31);
>     assert_eq!(X, Y);
>
> Or:
>
>     const X: u64 = 2 << 31;
>     const Y: u64 = cv!(2 << 31);
>     assert_eq!(X, Y);
>
> Because that is quite surprising, especially since we say "with the
> target type inferred from the context" in the documentation.
>
> I hope that helps & thanks Eliot for working on this!
>
> Cheers,
> Miguel

Miguel, thanks for noticing this! I agree this is surprising and worth a
pitfalls section like Alex suggests. AFAICT this can only affect
expressions which use left shift, so at least it's not all arithmetic.
And we can hit the same thing calling regular functions, e.g.
i64::from(2 << 31), although the macro does make it feel a bit more like
it should work. Let me add a pitfalls section for now.

  parent reply	other threads:[~2026-10-13  5:53 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-09-17  8:34 [PATCH v4 0/2] rust: introduce cv! macro for safe const conversions of integer-like types Eliot Courtney
2026-09-17  8:34 ` [PATCH v4 1/2] rust: num: add cv! macro to create values from constant expressions Eliot Courtney
2026-09-17  8:50   ` Danilo Krummrich
2026-09-29 11:15   ` Alice Ryhl
2026-10-02 16:59   ` Miguel Ojeda
2026-10-08 15:02     ` Alexandre Courbot
2026-10-13  5:52     ` Eliot Courtney [this message]
2026-09-17  8:34 ` [PATCH v4 2/2] gpu: nova-core: use cv! for constant casts Eliot Courtney
2026-09-17  8:57   ` Danilo Krummrich

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=DM3H68S4CR5K.3K9MG80U5Y9AA@nvidia.com \
    --to=ecourtney@nvidia.com \
    --cc=a.hindborg@kernel.org \
    --cc=acourbot@nvidia.com \
    --cc=airlied@gmail.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=dri-devel-bounces@lists.freedesktop.org \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=gary@garyguo.net \
    --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=simona@ffwll.ch \
    --cc=tamird@kernel.org \
    --cc=tmgross@umich.edu \
    --cc=ttabi@nvidia.com \
    --cc=work@onurozkan.dev \
    --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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.