[PATCH v2 2/4] rust: build_assert: remove macros from crate root
From: Gary Guo
Date: Thu Dec 03 2026 - 10:58:32 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.
Acked-by: Andreas Hindborg <a.hindborg@xxxxxxxxxx>
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/num/casts.rs | 6 +++---
rust/kernel/ptr.rs | 8 ++------
rust/kernel/sync/atomic.rs | 2 +-
rust/kernel/time.rs | 4 +++-
7 files changed, 20 insertions(+), 32 deletions(-)
diff --git a/rust/kernel/build_assert.rs b/rust/kernel/build_assert.rs
index c3acb9b68a65..484614738b1f 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 {
+#[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 dbed72844b31..4e5448858b5e 100644
--- a/rust/kernel/configfs.rs
+++ b/rust/kernel/configfs.rs
@@ -541,7 +541,7 @@ pub trait GroupOperations: Sync {
/// 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)
}
}
@@ -694,7 +694,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/num/casts.rs b/rust/kernel/num/casts.rs
index 7e6c7dec747d..ec6f85ed52c3 100644
--- a/rust/kernel/num/casts.rs
+++ b/rust/kernel/num/casts.rs
@@ -81,7 +81,7 @@ macro_rules! impl_safe_as {
/// ```
#[inline]
pub const fn [<$from _as_ $into>](value: $from) -> $into {
- $crate::static_assert!(size_of::<$into>() >= size_of::<$from>());
+ static_assert!(size_of::<$into>() >= size_of::<$from>());
value as $into
}
@@ -217,10 +217,10 @@ macro_rules! impl_const_into {
#[inline]
pub const fn [<$from _into_ $into>]<const N: $from>() -> $into {
// Make sure that the target type is smaller than the source one.
- $crate::static_assert!($from::BITS >= $into::BITS);
+ static_assert!($from::BITS >= $into::BITS);
// CAST: we statically enforced above that `$from` is larger than `$into`, so the
// `as` conversion will be lossless.
- $crate::const_assert!(N >= $into::MIN as $from && N <= $into::MAX as $from);
+ const_assert!(N >= $into::MIN as $from && N <= $into::MAX as $from);
N as $into
}
diff --git a/rust/kernel/ptr.rs b/rust/kernel/ptr.rs
index 2c12e4fd4572..ea12bc7f4f10 100644
--- a/rust/kernel/ptr.rs
+++ b/rust/kernel/ptr.rs
@@ -5,15 +5,11 @@
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,
- num::FromConst, //
+ num::FromConst,
+ 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 6d27898add42..17e60e1710c8 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;
diff --git a/rust/kernel/time.rs b/rust/kernel/time.rs
index b6a2aab12a50..e8c342ec5c61 100644
--- a/rust/kernel/time.rs
+++ b/rust/kernel/time.rs
@@ -27,6 +27,8 @@
use core::marker::PhantomData;
use core::ops;
+use crate::prelude::*;
+
pub mod delay;
pub mod hrtimer;
@@ -619,7 +621,7 @@ pub fn to_jiffies_timeout(self) -> Delta<Jiffy> {
// The quotient `(nsecs * HZ + NSEC_PER_SEC - 1) / NSEC_PER_SEC` has to fit in
// `u64`; `nsecs * HZ` does not. With `HZ <= NSEC_PER_SEC` the numerator is at
// most `(nsecs + 1) * NSEC_PER_SEC - 1`, so the quotient is at most `nsecs`.
- crate::static_assert!(HZ <= NSEC_PER_SEC as u64);
+ static_assert!(HZ <= NSEC_PER_SEC as u64);
// CAST: `i64::max()` makes the value non-negative, so the cast keeps it.
let nsecs = i64::max(self.as_nanos(), 0) as u64;
--
2.56.1