[PATCH] rust: replace `build_assert!` with `const_assert!`

Mohamad Alsadhan posted 1 patch 3 days, 4 hours ago
rust/kernel/dma.rs            | 4 ++--
rust/kernel/i2c.rs            | 2 +-
rust/kernel/list/arc.rs       | 2 +-
rust/kernel/sync/locked_by.rs | 8 ++++----
rust/kernel/xarray.rs         | 4 ++--
5 files changed, 10 insertions(+), 10 deletions(-)
[PATCH] rust: replace `build_assert!` with `const_assert!`
Posted by Mohamad Alsadhan 3 days, 4 hours ago
Several Rust assertions currently use the `build_assert!` macro
despite their conditions only depending on type parameters or const
generics, not runtime values.

Replace these with the more appropriate `const_assert!` macro which
should trigger earlier in the compilation pipeline. This is possible
since commit 560a7a9b9 ("rust: add `const_assert!` macro").

No functional change intended.

Signed-off-by: Mohamad Alsadhan <mo@sdhn.cc>
---
 rust/kernel/dma.rs            | 4 ++--
 rust/kernel/i2c.rs            | 2 +-
 rust/kernel/list/arc.rs       | 2 +-
 rust/kernel/sync/locked_by.rs | 8 ++++----
 rust/kernel/xarray.rs         | 4 ++--
 5 files changed, 10 insertions(+), 10 deletions(-)

diff --git a/rust/kernel/dma.rs b/rust/kernel/dma.rs
index 909d56fd5..deacfc719 100644
--- a/rust/kernel/dma.rs
+++ b/rust/kernel/dma.rs
@@ -5,7 +5,7 @@
 //! C header: [`include/linux/dma-mapping.h`](srctree/include/linux/dma-mapping.h)
 
 use crate::{
-    bindings, build_assert, device,
+    bindings, device,
     device::{Bound, Core},
     error::{to_result, Result},
     prelude::*,
@@ -401,7 +401,7 @@ pub fn alloc_attrs(
         gfp_flags: kernel::alloc::Flags,
         dma_attrs: Attrs,
     ) -> Result<CoherentAllocation<T>> {
-        build_assert!(
+        const_assert!(
             core::mem::size_of::<T>() > 0,
             "It doesn't make sense for the allocated type to be a ZST"
         );
diff --git a/rust/kernel/i2c.rs b/rust/kernel/i2c.rs
index 7b908f0c5..55b763189 100644
--- a/rust/kernel/i2c.rs
+++ b/rust/kernel/i2c.rs
@@ -110,7 +110,7 @@ unsafe fn register(
         name: &'static CStr,
         module: &'static ThisModule,
     ) -> Result {
-        build_assert!(
+        const_assert!(
             T::ACPI_ID_TABLE.is_some() || T::OF_ID_TABLE.is_some() || T::I2C_ID_TABLE.is_some(),
             "At least one of ACPI/OF/Legacy tables must be present when registering an i2c driver"
         );
diff --git a/rust/kernel/list/arc.rs b/rust/kernel/list/arc.rs
index e10824239..b57cec64f 100644
--- a/rust/kernel/list/arc.rs
+++ b/rust/kernel/list/arc.rs
@@ -252,7 +252,7 @@ pub fn pair_from_pin_unique<const ID2: u64>(
     where
         T: ListArcSafe<ID2>,
     {
-        build_assert!(ID != ID2);
+        const_assert!(ID != ID2);
 
         // SAFETY: We have a `UniqueArc`, so there is no `ListArc`.
         unsafe { <T as ListArcSafe<ID>>::on_create_list_arc_from_unique(unique.as_mut()) };
diff --git a/rust/kernel/sync/locked_by.rs b/rust/kernel/sync/locked_by.rs
index 61f100a45..5d043e661 100644
--- a/rust/kernel/sync/locked_by.rs
+++ b/rust/kernel/sync/locked_by.rs
@@ -3,7 +3,7 @@
 //! A wrapper for data protected by a lock that does not wrap it.
 
 use super::{lock::Backend, lock::Lock};
-use crate::build_assert;
+use crate::const_assert;
 use core::{cell::UnsafeCell, mem::size_of, ptr};
 
 /// Allows access to some data to be serialised by a lock that does not wrap it.
@@ -100,7 +100,7 @@ impl<T, U> LockedBy<T, U> {
     /// memory location*, the data becomes accessible again: none of this affects memory safety
     /// because in any case at most one thread (or CPU) can access the protected data at a time.
     pub fn new<B: Backend>(owner: &Lock<U, B>, data: T) -> Self {
-        build_assert!(
+        const_assert!(
             size_of::<Lock<U, B>>() > 0,
             "The lock type cannot be a ZST because it may be impossible to distinguish instances"
         );
@@ -126,7 +126,7 @@ pub fn access<'a>(&'a self, owner: &'a U) -> &'a T
     where
         T: Sync,
     {
-        build_assert!(
+        const_assert!(
             size_of::<U>() > 0,
             "`U` cannot be a ZST because `owner` wouldn't be unique"
         );
@@ -155,7 +155,7 @@ pub fn access<'a>(&'a self, owner: &'a U) -> &'a T
     /// Panics if `owner` is different from the data protected by the lock used in
     /// [`new`](LockedBy::new).
     pub fn access_mut<'a>(&'a self, owner: &'a mut U) -> &'a mut T {
-        build_assert!(
+        const_assert!(
             size_of::<U>() > 0,
             "`U` cannot be a ZST because `owner` wouldn't be unique"
         );
diff --git a/rust/kernel/xarray.rs b/rust/kernel/xarray.rs
index 46e5f4322..d4b3eac34 100644
--- a/rust/kernel/xarray.rs
+++ b/rust/kernel/xarray.rs
@@ -5,7 +5,7 @@
 //! C header: [`include/linux/xarray.h`](srctree/include/linux/xarray.h)
 
 use crate::{
-    alloc, bindings, build_assert,
+    alloc, bindings, const_assert,
     error::{Error, Result},
     ffi::c_void,
     types::{ForeignOwnable, NotThreadSafe, Opaque},
@@ -231,7 +231,7 @@ pub fn store(
         value: T,
         gfp: alloc::Flags,
     ) -> Result<Option<T>, StoreError<T>> {
-        build_assert!(
+        const_assert!(
             T::FOREIGN_ALIGN >= 4,
             "pointers stored in XArray must be 4-byte aligned"
         );
-- 
2.52.0
Re: [PATCH] rust: replace `build_assert!` with `const_assert!`
Posted by Tamir Duberstein 1 day, 14 hours ago
> Several Rust assertions currently use the `build_assert!` macro
> despite their conditions only depending on type parameters or const
> generics, not runtime values.
> 
> Replace these with the more appropriate `const_assert!` macro which
> should trigger earlier in the compilation pipeline. This is possible
> since commit 560a7a9b9 ("rust: add `const_assert!` macro").
> 
> No functional change intended.
> 
> Signed-off-by: Mohamad Alsadhan <mo@sdhn.cc>

Acked-by: Tamir Duberstein <tamird@kernel.org>

-- 
Tamir Duberstein <tamird@kernel.org>
Re: [PATCH] rust: replace `build_assert!` with `const_assert!`
Posted by Miguel Ojeda 2 days, 20 hours ago
On Mon, Mar 30, 2026 at 4:03 AM Mohamad Alsadhan <mo@sdhn.cc> wrote:
>
> Several Rust assertions currently use the `build_assert!` macro
> despite their conditions only depending on type parameters or const
> generics, not runtime values.
>
> Replace these with the more appropriate `const_assert!` macro which
> should trigger earlier in the compilation pipeline. This is possible
> since commit 560a7a9b9 ("rust: add `const_assert!` macro").
>
> No functional change intended.
>
> Signed-off-by: Mohamad Alsadhan <mo@sdhn.cc>

These are the ones identified by your Klint PR, right?

It would be nice to mention that and link to it, so I can do that on apply:

Link: https://github.com/Rust-for-Linux/klint/pull/13

...especially considering the recently added guidelines, i.e. things
like Coccinelle would apply:

  https://docs.kernel.org/process/generated-content.html

But it is also good publicity for Klint, it may get others to run it ;)

>  rust/kernel/dma.rs            | 4 ++--
>  rust/kernel/i2c.rs            | 2 +-
>  rust/kernel/sync/locked_by.rs | 8 ++++----
>  rust/kernel/xarray.rs         | 4 ++--

Let's see if we get Acked-bys; otherwise, the patch could also be
split and sent individually to subsystems.

Cc'ing the maintainers as well (`scripts/get_maintainer.pl` is used to
get the names and email of who to Cc in most cases -- please use it
when sending future patches).

Thanks!

Cheers,
Miguel