mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Gary Guo <gary@garyguo.net>
To: "Eliot Courtney" <ecourtney@nvidia.com>,
	"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,
	 Gary Guo <gary@garyguo.net>
Subject: [PATCH v3 0/2] rust: const_eval: add `#[const_only]` attribute
Date: Thu, 03 Dec 2026 18:24:41 +0000	[thread overview]
Message-ID: <20261203-cv-v3-0-bf8bfa2593cf@garyguo.net> (raw)

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>


             reply	other threads:[~2026-12-03 18:25 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-03 18:24 Gary Guo [this message]
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

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=20261203-cv-v3-0-bf8bfa2593cf@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®