[PATCH v3 1/2] rust: generalize `build_error` to `const_eval_select`
From: Gary Guo
Date: Thu Dec 03 2026 - 13:25:28 EST
Generalize the `build_error` crate to be a general-purpose
`const_eval_select` instead, which will be the home for everything that we
want to behave differently depending on whether it's in const-eval or
runtime.
For this to work, the build is reworked so rmeta/obj generation are split
with different cfgs. The object file generated is always linked; in the
absence of CONFIG_RUST_BUILD_ASSERT_ALLOW, `rust_build_error` will simply
be absent from the object file.
Signed-off-by: Gary Guo <gary@xxxxxxxxxxx>
---
rust/Makefile | 64 ++++++++++++++++++++++++++-------------
rust/build_error.rs | 31 -------------------
rust/const_eval_select.rs | 32 ++++++++++++++++++++
rust/exports.c | 6 +---
rust/kernel/build_assert.rs | 2 +-
scripts/generate_rust_analyzer.py | 8 ++---
6 files changed, 81 insertions(+), 62 deletions(-)
diff --git a/rust/Makefile b/rust/Makefile
index 80028738e51d..d938b9e9e0ad 100644
--- a/rust/Makefile
+++ b/rust/Makefile
@@ -28,11 +28,9 @@ always-$(CONFIG_RUST) += exports_bindings_generated.h exports_kernel_generated.h
always-$(CONFIG_RUST) += uapi/uapi_generated.rs
obj-$(CONFIG_RUST) += uapi.o
-ifdef CONFIG_RUST_BUILD_ASSERT_ALLOW
-obj-$(CONFIG_RUST) += build_error.o
-else
-always-$(CONFIG_RUST) += build_error.o
-endif
+obj-$(CONFIG_RUST) += const_eval_select.o
+always-$(CONFIG_RUST) += libconst_eval_select.rmeta
+always-$(CONFIG_RUST) += exports_const_eval_select_generated.h
obj-$(CONFIG_RUST) += exports.o
@@ -280,7 +278,7 @@ rustdoc-pin_init: $(src)/pin-init/src/lib.rs rustdoc-pin_init_internal \
rustdoc-kernel: private is-kernel-object := y
rustdoc-kernel: private rustc_target_flags = --extern ffi --extern pin_init \
- --extern build_error --extern macros \
+ --extern const_eval_select --extern macros \
--extern bindings --extern uapi \
--extern zerocopy --extern zerocopy_derive
rustdoc-kernel: $(src)/kernel/lib.rs rustdoc-core rustdoc-ffi rustdoc-macros \
@@ -307,7 +305,8 @@ rusttestlib-zerocopy: private rustc_target_flags = $(zerocopy-flags)
rusttestlib-zerocopy: $(src)/zerocopy/src/lib.rs FORCE
+$(call if_changed,rustc_test_library)
-rusttestlib-build_error: $(src)/build_error.rs FORCE
+rusttestlib-const_eval_select: private rustc_target_flags = --cfg const_eval
+rusttestlib-const_eval_select: $(src)/const_eval_select.rs FORCE
+$(call if_changed,rustc_test_library)
rusttestlib-ffi: $(src)/ffi.rs FORCE
@@ -352,12 +351,12 @@ rusttestlib-pin_init: $(src)/pin-init/src/lib.rs rusttestlib-macros \
+$(call if_changed,rustc_test_library)
rusttestlib-kernel: private rustc_target_flags = --extern ffi \
- --extern build_error --extern macros --extern pin_init \
+ --extern const_eval_select --extern macros --extern pin_init \
--extern bindings --extern uapi \
--extern zerocopy=$(objtree)/$(obj)/test/libzerocopy.rlib \
--extern zerocopy_derive=$(objtree)/$(obj)/test/$(libzerocopy_derive_name)
rusttestlib-kernel: $(src)/kernel/lib.rs rusttestlib-bindings rusttestlib-uapi \
- rusttestlib-build_error rusttestlib-pin_init $(obj)/$(libmacros_name) \
+ rusttestlib-const_eval_select rusttestlib-pin_init $(obj)/$(libmacros_name) \
$(obj)/bindings.o rusttestlib-zerocopy rusttestlib-zerocopy_derive FORCE
+$(call if_changed,rustc_test_library)
@@ -390,7 +389,7 @@ quiet_cmd_rustdoc_test_kernel = RUSTDOC TK $<
OBJTREE=$(abspath $(objtree)) \
$(RUSTDOC) --test $(filter-out --remap-path-scope=%,$(rust_flags)) \
-L$(objtree)/$(obj) --extern ffi --extern pin_init \
- --extern kernel --extern build_error --extern macros \
+ --extern kernel --extern const_eval_select --extern macros \
--extern bindings --extern uapi \
--extern zerocopy --extern zerocopy_derive \
--no-run --crate-name kernel -Zunstable-options \
@@ -577,6 +576,9 @@ $(obj)/exports_helpers_generated.h: $(obj)/helpers/helpers.o FORCE
$(obj)/exports_bindings_generated.h: $(obj)/bindings.o FORCE
$(call if_changed,exports)
+$(obj)/exports_const_eval_select_generated.h: $(obj)/const_eval_select.o FORCE
+ $(call if_changed,exports)
+
$(obj)/exports_kernel_generated.h: $(obj)/kernel.o FORCE
$(call if_changed,exports)
@@ -636,18 +638,22 @@ $(obj)/$(libpin_init_internal_name): $(src)/pin-init/internal/src/lib.rs \
# `rustc` requires `-Zunstable-options` to use custom target specifications
# since Rust 1.95.0 (https://github.com/rust-lang/rust/pull/151534).
-quiet_cmd_rustc_library = $(if $(skip_clippy),RUSTC,$(RUSTC_OR_CLIPPY_QUIET)) L $@
- cmd_rustc_library = \
+rustc_library_common = \
$(rustc_target_envs) \
OBJTREE=$(abspath $(objtree)) \
$(if $(skip_clippy),$(RUSTC),$(RUSTC_OR_CLIPPY)) \
$(filter-out $(skip_flags),$(rust_flags)) $(rustc_target_flags) \
- --emit=dep-info=$(depfile) --emit=$(if $(link_helper),llvm-bc=$(patsubst %.o,%.bc,$@),obj=$@) \
- --emit=metadata=$(dir $@)$(patsubst %.o,lib%.rmeta,$(notdir $@)) \
+ --emit=dep-info=$(depfile) \
--crate-type rlib -L$(objtree)/$(obj) \
- --crate-name $(patsubst %.o,%,$(notdir $@)) $< \
--sysroot=/dev/null \
- -Zunstable-options \
+ -Zunstable-options
+
+quiet_cmd_rustc_library = $(if $(skip_clippy),RUSTC,$(RUSTC_OR_CLIPPY_QUIET)) L $@
+ cmd_rustc_library = \
+ $(rustc_library_common) \
+ --emit=$(if $(link_helper),llvm-bc=$(patsubst %.o,%.bc,$@),obj=$@) \
+ $(if $(skip_rmeta),,--emit=metadata=$(dir $@)$(patsubst %.o,lib%.rmeta,$(notdir $@))) \
+ --crate-name $(patsubst %.o,%,$(notdir $@)) $< \
$(if $(link_helper),;$(LLVM_LINK) --internalize --suppress-warnings $(patsubst %.o,%.bc,$@) \
$(obj)/helpers/helpers$(if $(part-of-module),_module).bc -o $(patsubst %.o,%.m.bc,$@); \
$(CC) $(CLANG_FLAGS) $(filter-out $(CC_FLAGS_LTO),$(KBUILD_CFLAGS) $(modkern_cflags)) \
@@ -656,6 +662,15 @@ quiet_cmd_rustc_library = $(if $(skip_clippy),RUSTC,$(RUSTC_OR_CLIPPY_QUIET)) L
$(if $(rustc_objcopy),;$(OBJCOPY) $(rustc_objcopy) $@) \
$(cmd_objtool)
+# `--emit=obj=/dev/null` is needed to avoid rustc complaining about
+# "missing optimized MIR for an item" on dependent crates.
+quiet_cmd_rustc_rmeta = $(if $(skip_clippy),RUSTC,$(RUSTC_OR_CLIPPY_QUIET)) M $@
+ cmd_rustc_rmeta = \
+ $(rustc_library_common) \
+ --emit=obj=/dev/null \
+ --emit=metadata=$@ \
+ --crate-name $(patsubst lib%.rmeta,%,$(notdir $@)) $<
+
rust-analyzer:
$(Q)MAKEFLAGS= $(srctree)/scripts/generate_rust_analyzer.py \
--cfgs='core=$(core-cfgs)' $(core-edition) \
@@ -666,6 +681,7 @@ rust-analyzer:
--cfgs='zerocopy_derive=$(zerocopy_derive-cfgs)' \
--cfgs='pin_init_internal=$(pin_init_internal-cfgs)' \
--cfgs='pin_init=$(pin_init-cfgs)' \
+ --cfgs='const_eval_select=const_eval' \
--envs='zerocopy=$(zerocopy-envs)' \
$(realpath $(srctree)) $(realpath $(objtree)) \
$(rustc_sysroot) $(RUST_LIB_SRC) $(if $(KBUILD_EXTMOD),$(srcroot)) \
@@ -755,11 +771,17 @@ $(obj)/pin_init.o: $(src)/pin-init/src/lib.rs $(obj)/compiler_builtins.o \
$(obj)/$(libpin_init_internal_name) $(obj)/$(libmacros_name) FORCE
+$(call if_changed_rule,rustc_library)
-# Even if normally `build_error` is not a kernel object, it should still be
+# Even though `libconst_eval_select.rmeta` is not a kernel object, it should still be
# treated as such so that we pass the same flags. Otherwise, for instance,
# `rustc` will complain about missing sanitizer flags causing an ABI mismatch.
-$(obj)/build_error.o: private is-kernel-object := y
-$(obj)/build_error.o: $(src)/build_error.rs $(obj)/compiler_builtins.o FORCE
+$(obj)/libconst_eval_select.rmeta: private is-kernel-object := y
+$(obj)/libconst_eval_select.rmeta: private rustc_target_flags = --cfg=const_eval
+$(obj)/libconst_eval_select.rmeta: $(src)/const_eval_select.rs $(obj)/compiler_builtins.o FORCE
+ +$(call if_changed_dep,rustc_rmeta)
+
+$(obj)/const_eval_select.o: private skip_rmeta = 1
+$(obj)/const_eval_select.o: $(src)/const_eval_select.rs $(obj)/compiler_builtins.o \
+ $(obj)/libconst_eval_select.rmeta FORCE
+$(call if_changed_rule,rustc_library)
$(obj)/ffi.o: private skip_gendwarfksyms = 1
@@ -783,9 +805,9 @@ $(obj)/uapi.o: $(src)/uapi/lib.rs \
+$(call if_changed_rule,rustc_library)
$(obj)/kernel.o: private rustc_target_flags = --extern ffi --extern pin_init \
- --extern build_error --extern macros --extern bindings --extern uapi \
+ --extern const_eval_select --extern macros --extern bindings --extern uapi \
--extern zerocopy --extern zerocopy_derive
-$(obj)/kernel.o: $(src)/kernel/lib.rs $(obj)/build_error.o $(obj)/pin_init.o \
+$(obj)/kernel.o: $(src)/kernel/lib.rs $(obj)/const_eval_select.o $(obj)/pin_init.o \
$(obj)/$(libmacros_name) $(obj)/bindings.o $(obj)/uapi.o \
$(obj)/zerocopy.o $(obj)/$(libzerocopy_derive_name) FORCE
+$(call if_changed_rule,rustc_library)
diff --git a/rust/build_error.rs b/rust/build_error.rs
deleted file mode 100644
index fa24eeef9929..000000000000
--- a/rust/build_error.rs
+++ /dev/null
@@ -1,31 +0,0 @@
-// SPDX-License-Identifier: GPL-2.0
-
-//! Build-time error.
-//!
-//! This crate provides a [const function][const-functions] `build_error`, which will panic in
-//! compile-time if executed in [const context][const-context], and will cause a build error
-//! if not executed at compile time and the optimizer does not optimise away the call.
-//!
-//! It is used by `build_assert!` in the kernel crate, allowing checking of
-//! conditions that could be checked statically, but could not be enforced in
-//! Rust yet (e.g. perform some checks in [const functions][const-functions], but those
-//! functions could still be called in the runtime).
-//!
-//! For details on constant evaluation in Rust, please see the [Reference][const-eval].
-//!
-//! [const-eval]: https://doc.rust-lang.org/reference/const_eval.html
-//! [const-functions]: https://doc.rust-lang.org/reference/const_eval.html#const-functions
-//! [const-context]: https://doc.rust-lang.org/reference/const_eval.html#const-context
-
-#![no_std]
-
-/// Panics if executed in [const context][const-context], or triggers a build error if not.
-///
-/// [const-context]: https://doc.rust-lang.org/reference/const_eval.html#const-context
-#[inline(never)]
-#[cold]
-#[export_name = "rust_build_error"]
-#[track_caller]
-pub const fn build_error(msg: &'static str) -> ! {
- panic!("{}", msg);
-}
diff --git a/rust/const_eval_select.rs b/rust/const_eval_select.rs
new file mode 100644
index 000000000000..fb0670a8d925
--- /dev/null
+++ b/rust/const_eval_select.rs
@@ -0,0 +1,32 @@
+// SPDX-License-Identifier: GPL-2.0
+
+//! Functions that behave differently between const evaluation and runtime.
+//!
+//! This crate serves the same purpose as the `const_eval_select` Rust compiler intrinsic, but
+//! implemented using linking tricks.
+//!
+//! For details on constant evaluation in Rust, please see the [Reference][const-eval].
+//!
+//! [const-eval]: https://doc.rust-lang.org/reference/const_eval.html
+
+#![no_std]
+
+/// Panics if executed in [const context][const-context], or triggers a build error if not.
+///
+/// This will trigger a build error if the optimizer does not optimise away the call.
+///
+/// It is used by `build_assert!` in the kernel crate, allowing checking of
+/// conditions that could be checked statically, but could not be enforced in
+/// Rust yet (e.g. perform some checks in [const functions][const-functions], but those
+/// functions could still be called in the runtime).
+///
+/// [const-context]: https://doc.rust-lang.org/reference/const_eval.html#const-context
+/// [const-functions]: https://doc.rust-lang.org/reference/const_eval.html#const-functions
+#[cfg(any(const_eval, CONFIG_RUST_BUILD_ASSERT_ALLOW))]
+#[inline(never)]
+#[cold]
+#[export_name = "rust_build_error"]
+#[track_caller]
+pub const fn build_error(msg: &'static str) -> ! {
+ panic!("{}", msg);
+}
diff --git a/rust/exports.c b/rust/exports.c
index 1b52460b0f4e..47aacc1f817b 100644
--- a/rust/exports.c
+++ b/rust/exports.c
@@ -17,13 +17,9 @@
#include "exports_core_generated.h"
#include "exports_bindings_generated.h"
+#include "exports_const_eval_select_generated.h"
#include "exports_kernel_generated.h"
#ifndef CONFIG_RUST_INLINE_HELPERS
#include "exports_helpers_generated.h"
#endif
-
-// For modules using `rust/build_error.rs`.
-#ifdef CONFIG_RUST_BUILD_ASSERT_ALLOW
-EXPORT_SYMBOL_RUST_GPL(rust_build_error);
-#endif
diff --git a/rust/kernel/build_assert.rs b/rust/kernel/build_assert.rs
index c3acb9b68a65..f7f44e381cd8 100644
--- a/rust/kernel/build_assert.rs
+++ b/rust/kernel/build_assert.rs
@@ -70,7 +70,7 @@
};
#[doc(hidden)]
-pub use build_error::build_error as build_error_fn;
+pub use const_eval_select::build_error as build_error_fn;
/// Static assert (i.e. compile-time assert).
///
diff --git a/scripts/generate_rust_analyzer.py b/scripts/generate_rust_analyzer.py
index dc1219736f77..ef7180a797ec 100755
--- a/scripts/generate_rust_analyzer.py
+++ b/scripts/generate_rust_analyzer.py
@@ -258,9 +258,9 @@ def generate_crates(
[std, proc_macro, proc_macro2, quote, syn],
)
- build_error = append_crate(
- "build_error",
- srctree / "rust" / "build_error.rs",
+ const_eval_select = append_crate(
+ "const_eval_select",
+ srctree / "rust" / "const_eval_select.rs",
[core, compiler_builtins],
)
@@ -316,7 +316,7 @@ def generate_crates(
bindings = append_crate_with_generated("bindings", [core, ffi, pin_init])
uapi = append_crate_with_generated("uapi", [core, ffi, pin_init])
kernel = append_crate_with_generated(
- "kernel", [core, macros, build_error, pin_init, ffi, bindings, uapi, zerocopy, zerocopy_derive]
+ "kernel", [core, macros, const_eval_select, pin_init, ffi, bindings, uapi, zerocopy, zerocopy_derive]
)
scripts = srctree / "scripts"
--
2.56.1