[PATCH 1/2] rust: build_assert: add utility to require const eval
From: Gary Guo
Date: Fri Aug 28 2026 - 08:06:01 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.
Convert device_id.rs as an example.
Signed-off-by: Gary Guo <gary@xxxxxxxxxxx>
---
rust/build_error.rs | 7 +++++++
rust/kernel/build_assert.rs | 9 ++++++++-
rust/kernel/device_id.rs | 4 ++++
rust/macros/const_eval.rs | 24 ++++++++++++++++++++++++
rust/macros/lib.rs | 21 +++++++++++++++++++++
5 files changed, 64 insertions(+), 1 deletion(-)
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() {}
diff --git a/rust/kernel/build_assert.rs b/rust/kernel/build_assert.rs
index c3acb9b68a65..4cab4e1a796f 100644
--- a/rust/kernel/build_assert.rs
+++ b/rust/kernel/build_assert.rs
@@ -66,9 +66,16 @@
build_assert_macro as build_assert,
build_error,
const_assert,
- static_assert, //
+ static_assert,
+ //
};
+#[doc(inline)]
+pub use build_error::assert_in_const_eval;
+
+#[doc(inline)]
+pub use macros::const_eval;
+
#[doc(hidden)]
pub use build_error::build_error as build_error_fn;
diff --git a/rust/kernel/device_id.rs b/rust/kernel/device_id.rs
index c81fca5b4986..476f44d31b79 100644
--- a/rust/kernel/device_id.rs
+++ b/rust/kernel/device_id.rs
@@ -10,6 +10,8 @@
mem::MaybeUninit, //
};
+use crate::build_assert::const_eval;
+
/// Marker trait to indicate a Rust device ID type represents a corresponding C device ID type.
///
/// This is meant to be implemented by buses/subsystems so that they can use [`IdTable`] to
@@ -108,6 +110,7 @@ impl<T: RawDeviceId + RawDeviceIdIndex, U: 'static, const N: usize> IdArray<T, U
/// Creates a new instance of the array.
///
/// The contents are derived from the given identifiers and context information.
+ #[const_eval]
pub const fn new(ids: [(T, &'static U); N]) -> Self {
let mut raw_ids = [const { MaybeUninit::<T::RawType>::uninit() }; N];
@@ -144,6 +147,7 @@ impl<T: RawDeviceId, const N: usize> IdArray<T, (), N> {
///
/// The contents are derived from the given identifiers and context information.
/// If the device implements [`RawDeviceIdIndex`], consider using [`IdArray::new`] instead.
+ #[const_eval]
pub const fn new_without_index(ids: [T; N]) -> Self {
// SAFETY: `T` is layout-wise compatible with `T::RawType`, so is the array of them.
let raw_ids: [MaybeUninit<T::RawType>; N] = unsafe { core::mem::transmute_copy(&ids) };
diff --git a/rust/macros/const_eval.rs b/rust/macros/const_eval.rs
new file mode 100644
index 000000000000..0629f2708319
--- /dev/null
+++ b/rust/macros/const_eval.rs
@@ -0,0 +1,24 @@
+// SPDX-License-Identifier: GPL-2.0
+
+use proc_macro2::TokenStream;
+use quote::ToTokens;
+use syn::{
+ parse_quote,
+ ItemFn, //
+};
+
+pub(crate) fn const_eval(mut input: ItemFn) -> TokenStream {
+ // Prevent code generation as the function is for const evaluation only.
+ input.attrs.push(parse_quote!(
+ #[inline(always)]
+ ));
+
+ input.block.stmts.insert(
+ 0,
+ parse_quote!(
+ ::kernel::build_assert::assert_in_const_eval();
+ ),
+ );
+
+ input.into_token_stream()
+}
diff --git a/rust/macros/lib.rs b/rust/macros/lib.rs
index 24f96feaeb34..3865619139a5 100644
--- a/rust/macros/lib.rs
+++ b/rust/macros/lib.rs
@@ -15,6 +15,7 @@
#![cfg_attr(not(CONFIG_RUSTC_HAS_SPAN_FILE), feature(proc_macro_span))]
mod concat_idents;
+mod const_eval;
mod export;
mod fmt;
mod for_lt;
@@ -338,6 +339,26 @@ pub fn concat_idents(input: TokenStream) -> TokenStream {
concat_idents::concat_idents(parse_macro_input!(input)).into()
}
+/// Mark a function as usable from const evaluation only.
+///
+/// Build will fail if the function is used for runtime code.
+///
+/// # Examples
+///
+/// ```
+/// #[const_eval]
+/// const fn call_for_const_eval_only() {
+/// // This code will be executed only during const eval!
+/// }
+///
+/// const _: () = call_for_const_eval_only();
+/// ```
+#[proc_macro_attribute]
+pub fn const_eval(attr: TokenStream, input: TokenStream) -> TokenStream {
+ parse_macro_input!(attr as syn::parse::Nothing);
+ const_eval::const_eval(parse_macro_input!(input)).into()
+}
+
/// Paste identifiers together.
///
/// Within the `paste!` macro, identifiers inside `[<` and `>]` are concatenated together to form a
--
2.54.0