From: Gary Guo <gary@garyguo.net>
To: "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>,
"Breno Leitao" <leitao@debian.org>,
"Luis Chamberlain" <mcgrof@kernel.org>,
"Russ Weight" <russ.weight@linux.dev>,
"Lyude Paul" <lyude@redhat.com>
Cc: linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org,
driver-core@lists.linux.dev, Gary Guo <gary@garyguo.net>
Subject: [PATCH v2 2/4] rust: build_assert: remove macros from crate root
Date: Thu, 03 Dec 2026 15:57:44 +0000 [thread overview]
Message-ID: <20261203-macro_export_scoped-v2-2-55d1b09238e2@garyguo.net> (raw)
In-Reply-To: <20261203-macro_export_scoped-v2-0-55d1b09238e2@garyguo.net>
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@kernel.org>
Signed-off-by: Gary Guo <gary@garyguo.net>
---
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
next prev parent reply other threads:[~2026-12-03 15:58 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-03 15:57 [PATCH v2 0/4] rust: add `#[macro_export_scoped]` for scoped declarative macro Gary Guo
2026-12-03 15:57 ` [PATCH v2 1/4] rust: macros: add `#[macro_export_scoped]` Gary Guo
2026-12-03 15:57 ` Gary Guo [this message]
2026-12-03 15:57 ` [PATCH v2 3/4] rust: list: convert to use `#[macro_export_scoped]` Gary Guo
2026-12-03 15:57 ` [PATCH v2 4/4] rust: io: " 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-macro_export_scoped-v2-2-55d1b09238e2@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=driver-core@lists.linux.dev \
--cc=leitao@debian.org \
--cc=linux-kernel@vger.kernel.org \
--cc=lossin@kernel.org \
--cc=lyude@redhat.com \
--cc=mcgrof@kernel.org \
--cc=ojeda@kernel.org \
--cc=russ.weight@linux.dev \
--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®