Re: [PATCH v2 1/3] rust: const_eval: add `#[const_eval_only]` attribute
From: Gary Guo
Date: Wed Sep 09 2026 - 13:09:27 EST
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@xxxxxxxxxxx>
>> ---
>> 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