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 2/4] rust: build_assert: remove macro from crate root
Date: Tue, 11 Aug 2026 13:25:54 +0100 [thread overview]
Message-ID: <20260811-macro_export_scoped-v1-2-e7782b102819@garyguo.net> (raw)
In-Reply-To: <20260811-macro_export_scoped-v1-0-e7782b102819@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.
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/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
next prev parent reply other threads:[~2026-08-11 12:26 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-08-11 12:25 [PATCH 0/4] rust: add `#[macro_export_scoped]` for scoped declarative macro Gary Guo
2026-08-11 12:25 ` [PATCH 1/4] rust: macros: add `#[macro_export_scoped]` Gary Guo
2026-08-11 12:25 ` Gary Guo [this message]
2026-08-11 12:25 ` [PATCH 3/4] rust: list: convert to use `#[macro_export_scoped]` Gary Guo
2026-08-11 12:25 ` [PATCH 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=20260811-macro_export_scoped-v1-2-e7782b102819@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