mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH v3 0/2] rust: const_eval: add `#[const_only]` attribute
@ 2026-12-03 18:24 Gary Guo
  2026-12-03 18:24 ` [PATCH v3 1/2] rust: generalize `build_error` to `const_eval_select` Gary Guo
  2026-12-03 18:24 ` [PATCH v3 2/2] rust: const_eval: add `#[const_only]` attribute Gary Guo
  0 siblings, 2 replies; 3+ messages in thread
From: Gary Guo @ 2026-12-03 18:24 UTC (permalink / raw)
  To: Eliot Courtney, Miguel Ojeda, Boqun Feng, Björn Roy Baron,
	Benno Lossin, Andreas Hindborg, Alice Ryhl, Trevor Gross,
	Danilo Krummrich, Daniel Almeida, Tamir Duberstein,
	Alexandre Courbot, Onur Özkan
  Cc: linux-kernel, rust-for-linux, Gary Guo

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@garyguo.net>
---
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@garyguo.net

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@garyguo.net

---
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@garyguo.net>


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-12-03 18:25 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-12-03 18:24 [PATCH v3 0/2] rust: const_eval: add `#[const_only]` attribute Gary Guo
2026-12-03 18:24 ` [PATCH v3 1/2] rust: generalize `build_error` to `const_eval_select` Gary Guo
2026-12-03 18:24 ` [PATCH v3 2/2] rust: const_eval: add `#[const_only]` attribute Gary Guo

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®