From: "Gary Guo" <gary@garyguo.net>
To: "Eliot Courtney" <ecourtney@nvidia.com>,
"Gary Guo" <gary@garyguo.net>, "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>,
"Alexandre Courbot" <acourbot@nvidia.com>,
"Onur Özkan" <work@onurozkan.dev>
Cc: <linux-kernel@vger.kernel.org>, <rust-for-linux@vger.kernel.org>
Subject: Re: [PATCH v2 1/3] rust: const_eval: add `#[const_eval_only]` attribute
Date: Wed, 09 Sep 2026 15:36:14 +0100 [thread overview]
Message-ID: <DLAV0JX3XOKM.2OI6E3M1DLZMY@garyguo.net> (raw)
In-Reply-To: <DL8YAXFG28GT.11FRAWC7NUBM7@nvidia.com>
On Mon Sep 7, 2026 at 9:45 AM BST, Eliot Courtney wrote:
> On Fri Sep 4, 2026 at 12:21 AM JST, Gary Guo wrote:
>> We have a lot of helper const functions which are intended to be used
>> during const evaluation only and runtime calls should not be generated. Add
>> a macro to denote this explicitly. This is similar to C++'s consteval
>> keyword.
>>
>> Convert device_id.rs as an example.
>>
>> Signed-off-by: Gary Guo <gary@garyguo.net>
>> ---
>> rust/build_error.rs | 7 +++++++
>> rust/kernel/const_eval.rs | 9 +++++++++
>> rust/kernel/device_id.rs | 4 ++++
>> rust/kernel/lib.rs | 1 +
>> rust/macros/const_eval.rs | 24 ++++++++++++++++++++++++
>> rust/macros/lib.rs | 21 +++++++++++++++++++++
>> 6 files changed, 66 insertions(+)
>>
>> diff --git a/rust/build_error.rs b/rust/build_error.rs
>> index fa24eeef9929..b7ef80596f1f 100644
>> --- a/rust/build_error.rs
>> +++ b/rust/build_error.rs
>> @@ -29,3 +29,10 @@
>> pub const fn build_error(msg: &'static str) -> ! {
>> panic!("{}", msg);
>> }
>> +
>> +/// Assert that the code is in const evaluation.
>> +///
>> +/// Triggers a build error if called at runtime.
>> +#[inline(never)]
>> +#[export_name = "rust_const_eval_called_at_runtime"]
>> +pub const fn assert_in_const_eval() {}
>
> IIUC if RUST_BUILD_ASSERT_ALLOW=y then this will not cause a build error
> anything built-in. I don't expect the call will ever fail to be
> eliminated by link time here, so isn't it better to not use the same
> mechanism affected by RUST_BUILD_ASSERT_ALLOW=y here? Guess you would
> need to make a new file for this.
I could do that, but I also think it's not a big deal to also allow for
RUST_BUILD_ASSERT_ALLOW=y builds just to simplify the build system.
As long as we have people building code with RUST_BUILD_ASSERT_ALLOW=n then the
problematic use will be caught.
Best,
Gary
next prev parent reply other threads:[~2026-09-09 14:36 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-09-03 15:21 [PATCH v2 0/3] rust: const_eval: add a mechanism to do const trait calls Gary Guo
2026-09-03 15:21 ` [PATCH v2 1/3] rust: const_eval: add `#[const_eval_only]` attribute Gary Guo
2026-09-07 8:45 ` Eliot Courtney
2026-09-09 14:36 ` Gary Guo [this message]
2026-09-03 15:21 ` [PATCH v2 2/3] rust: const_eval: allow const trait method invocation in some contexts Gary Guo
2026-09-03 15:21 ` [PATCH v2 3/3] rust: str: convert `as_char_ptr` to work with `const_call!` Gary Guo
2026-09-03 15:34 ` [PATCH v2 0/3] rust: const_eval: add a mechanism to do const trait calls Miguel Ojeda
2026-09-03 15:46 ` Gary Guo
2026-09-07 9:21 ` Eliot Courtney
2026-09-09 14:32 ` Gary Guo
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=DLAV0JX3XOKM.2OI6E3M1DLZMY@garyguo.net \
--to=gary@garyguo.net \
--cc=a.hindborg@kernel.org \
--cc=acourbot@nvidia.com \
--cc=aliceryhl@google.com \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun@kernel.org \
--cc=dakr@kernel.org \
--cc=daniel.almeida@collabora.com \
--cc=ecourtney@nvidia.com \
--cc=linux-kernel@vger.kernel.org \
--cc=lossin@kernel.org \
--cc=ojeda@kernel.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=tamird@kernel.org \
--cc=tmgross@umich.edu \
--cc=work@onurozkan.dev \
/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®