[PATCH v3 0/2] rust: const_eval: add `#[const_only]` attribute

From: Gary Guo

Date: Thu Dec 03 2026 - 13:25:15 EST


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; however, I did not use
`consteval` as the name of the attribute as it fails to capture that
annotated functions *must only* be used in const evaluation. My preferred
name for this attribute is actually `#[const_eval_only]` but after sampling
people's opinion on the attribute, it was settled that `#[const_only]` is
the name that most people are okay with.

I refactored build_error.rs to const_eval_select.rs to make it generic
enough for any functions that we prefer different behaviour between const
eval and runtime. The rust/Makefile changes are generated by Gemini 4 Neon
and I have validated it.

Signed-off-by: Gary Guo <gary@xxxxxxxxxxx>
---
Changes in v3:
- Renamed the attribute.
- Dropped `const_call!`, keep `#[const_only]` only.
- Refactored build_error so CONFIG_RUST_BUILD_ASSERT_ALLOW has reasonable
behaviour for `assert_in_const_eval`.
- Link to v2: https://patch.msgid.link/20260903-cv-v2-0-e93b1613e40c@xxxxxxxxxxx

Changes in v2:
- Split from `cv!()` alt series, and become a general infra instead.
- Link to v1: https://patch.msgid.link/20260828-cv-v1-0-694a695ff17f@xxxxxxxxxxx

---
Gary Guo (2):
rust: generalize `build_error` to `const_eval_select`
rust: const_eval: add `#[const_only]` attribute

rust/Makefile | 64 ++++++++++++++++++++++++++-------------
rust/build_error.rs | 31 -------------------
rust/const_eval_select.rs | 44 +++++++++++++++++++++++++++
rust/exports.c | 6 +---
rust/kernel/build_assert.rs | 2 +-
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 | 23 ++++++++++++++
scripts/generate_rust_analyzer.py | 8 ++---
11 files changed, 154 insertions(+), 62 deletions(-)
---
base-commit: 82d8023ec40420ed61d7a96a173ef862741f1753
change-id: 20260828-cv-acaf3400d16c

Best regards,
--
Gary Guo <gary@xxxxxxxxxxx>