[PATCH 2/4] rust: build_assert: remove macro from crate root
From: Gary Guo
Date: Tue Aug 11 2026 - 08:28:14 EST
Convert `build_assert` to use `#[macro_export_scoped]`. This removes the
macros from crate root of `kernel`. A few remaining mentions of the macros
from crate root are removed.
Signed-off-by: Gary Guo <gary@xxxxxxxxxxx>
---
rust/kernel/build_assert.rs | 24 +++++++-----------------
rust/kernel/configfs.rs | 4 ++--
rust/kernel/firmware.rs | 4 ++--
rust/kernel/ptr.rs | 6 +-----
rust/kernel/sync/atomic.rs | 2 +-
5 files changed, 13 insertions(+), 27 deletions(-)
diff --git a/rust/kernel/build_assert.rs b/rust/kernel/build_assert.rs
index c3acb9b68a65..4774c7fb6976 100644
--- a/rust/kernel/build_assert.rs
+++ b/rust/kernel/build_assert.rs
@@ -61,17 +61,11 @@
//! undefined symbols and linker errors, it is not developer friendly to debug, so it is recommended
//! to avoid it and prefer other two assertions where possible.
-#[doc(inline)]
-pub use crate::{
- build_assert_macro as build_assert,
- build_error,
- const_assert,
- static_assert, //
-};
-
#[doc(hidden)]
pub use build_error::build_error as build_error_fn;
+use macros::macro_export_scoped;
+
/// Static assert (i.e. compile-time assert).
///
/// Similar to C11 [`_Static_assert`] and C++11 [`static_assert`].
@@ -105,8 +99,7 @@
/// static_assert!(f(40) == 42);
/// static_assert!(f(40) == 42, "f(x) must add 2 to the given input.");
/// ```
-#[macro_export]
-#[doc(hidden)]
+#[macro_export_scoped]
macro_rules! static_assert {
($condition:expr $(,$arg:literal)?) => {
const _: () = ::core::assert!($condition $(,$arg)?);
@@ -134,8 +127,7 @@ macro_rules! static_assert {
/// const_assert!(size_of::<T>() > 0, "T cannot be ZST");
/// }
/// ```
-#[macro_export]
-#[doc(hidden)]
+#[macro_export_scoped]
macro_rules! const_assert {
($condition:expr $(,$arg:literal)?) => {
const { ::core::assert!($condition $(,$arg)?) };
@@ -159,8 +151,7 @@ macro_rules! const_assert {
/// assert_eq!(foo(usize::MAX - 1), usize::MAX); // OK.
/// // foo(usize::MAX); // Fails to compile.
/// ```
-#[macro_export]
-#[doc(hidden)]
+#[macro_export_scoped]
macro_rules! build_error {
() => {{
$crate::build_assert::build_error_fn("")
@@ -203,9 +194,8 @@ macro_rules! build_error {
///
/// const _: () = const_bar(2);
/// ```
-#[macro_export]
-#[doc(hidden)]
-macro_rules! build_assert_macro {
+#[macros::macro_export_scoped]
+macro_rules! build_assert {
($cond:expr $(,)?) => {{
if !$cond {
$crate::build_assert::build_error_fn(concat!("assertion failed: ", stringify!($cond)));
diff --git a/rust/kernel/configfs.rs b/rust/kernel/configfs.rs
index 2339c6467325..8660ce01d97f 100644
--- a/rust/kernel/configfs.rs
+++ b/rust/kernel/configfs.rs
@@ -511,7 +511,7 @@ pub trait GroupOperations {
/// NOTE: "drop" in the name of this function is not related to the Rust drop term. Rather, the
/// name is inherited from the callback name in the underlying C code.
fn drop_item(&self, _child: ArcBorrow<'_, Group<Self::Child>>) {
- kernel::build_error!(kernel::error::VTABLE_DEFAULT_ERROR)
+ build_error!(kernel::error::VTABLE_DEFAULT_ERROR)
}
}
@@ -660,7 +660,7 @@ pub trait AttributeOperations<const ID: u64 = 0> {
/// Implementations should parse the value from `page` and update internal
/// state to reflect the parsed value.
fn store(_data: &Self::Data, _page: &[u8]) -> Result {
- kernel::build_error!(kernel::error::VTABLE_DEFAULT_ERROR)
+ build_error!(kernel::error::VTABLE_DEFAULT_ERROR)
}
}
diff --git a/rust/kernel/firmware.rs b/rust/kernel/firmware.rs
index a18f8b84f3e3..1abc66b84ef2 100644
--- a/rust/kernel/firmware.rs
+++ b/rust/kernel/firmware.rs
@@ -325,7 +325,7 @@ const fn push_internal(mut self, bytes: &[u8]) -> Self {
pub const fn push(self, s: &str) -> Self {
// Check whether there has been an initial call to `next_entry()`.
if N != 0 && self.n == 0 {
- crate::build_error!("Must call next_entry() before push().");
+ build_error!("Must call next_entry() before push().");
}
self.push_internal(s.as_bytes())
@@ -369,7 +369,7 @@ pub const fn new_entry(self) -> Self {
if this.n == N {
this.buf
} else {
- crate::build_error!("Length mismatch.");
+ build_error!("Length mismatch.");
}
}
}
diff --git a/rust/kernel/ptr.rs b/rust/kernel/ptr.rs
index 82acb531b17b..b6fb802c2e21 100644
--- a/rust/kernel/ptr.rs
+++ b/rust/kernel/ptr.rs
@@ -5,13 +5,9 @@
pub mod projection;
pub use crate::project_pointer as project;
-use core::mem::{
- align_of,
- size_of, //
-};
use core::num::NonZero;
-use crate::const_assert;
+use crate::prelude::*;
/// Type representing an alignment, which is always a power of two.
///
diff --git a/rust/kernel/sync/atomic.rs b/rust/kernel/sync/atomic.rs
index 9cd009d57e35..c62cb46798ff 100644
--- a/rust/kernel/sync/atomic.rs
+++ b/rust/kernel/sync/atomic.rs
@@ -25,7 +25,7 @@
pub(crate) use internal::{AtomicArithmeticOps, AtomicBasicOps, AtomicExchangeOps};
-use crate::build_error;
+use crate::build_assert::build_error;
use internal::AtomicRepr;
use ordering::OrderingType;
--
2.54.0