* [PATCH RESEND] rust: replace `build_assert!` with `const_assert!`
@ 2026-05-23 12:27 Mohamad Alsadhan
2026-05-26 4:33 ` kernel test robot
0 siblings, 1 reply; 2+ messages in thread
From: Mohamad Alsadhan @ 2026-05-23 12:27 UTC (permalink / raw)
To: Danilo Krummrich, Abdiel Janulgue, Daniel Almeida, Robin Murphy,
Andreas Hindborg, Miguel Ojeda, Boqun Feng, Gary Guo,
Björn Roy Baron, Benno Lossin, Alice Ryhl, Trevor Gross,
Igor Korotin, Peter Zijlstra, Ingo Molnar, Will Deacon,
Waiman Long, Tamir Duberstein
Cc: Mohamad Alsadhan, driver-core, rust-for-linux, linux-kernel
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").
These changes were identified by my PR to klint (see link), which
added a new lint that suggests to use stronger build-time assertion
forms when possible.
No functional change intended.
Link: https://github.com/Rust-for-Linux/klint/pull/14
Acked-by: Tamir Duberstein <tamird@kernel.org>
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.54.0
^ permalink raw reply [flat|nested] 2+ messages in thread* Re: [PATCH RESEND] rust: replace `build_assert!` with `const_assert!`
2026-05-23 12:27 [PATCH RESEND] rust: replace `build_assert!` with `const_assert!` Mohamad Alsadhan
@ 2026-05-26 4:33 ` kernel test robot
0 siblings, 0 replies; 2+ messages in thread
From: kernel test robot @ 2026-05-26 4:33 UTC (permalink / raw)
To: Mohamad Alsadhan, Danilo Krummrich, Abdiel Janulgue,
Daniel Almeida, Robin Murphy, Andreas Hindborg, Miguel Ojeda,
Boqun Feng, Gary Guo, Björn Roy Baron, Benno Lossin,
Alice Ryhl, Trevor Gross, Igor Korotin, Peter Zijlstra,
Ingo Molnar, Will Deacon, Waiman Long, Tamir Duberstein
Cc: oe-kbuild-all, Mohamad Alsadhan, driver-core, rust-for-linux,
linux-kernel
Hi Mohamad,
kernel test robot noticed the following build errors:
[auto build test ERROR on rust/xarray-next]
[cannot apply to rust/rust-next driver-core/driver-core-testing driver-core/driver-core-next driver-core/driver-core-linus tip/locking/core linus/master v7.1-rc5 next-20260525]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/Mohamad-Alsadhan/rust-replace-build_assert-with-const_assert/20260523-202931
base: https://github.com/Rust-for-Linux/linux xarray-next
patch link: https://lore.kernel.org/r/20260523122759.30317-1-mo%40sdhn.cc
patch subject: [PATCH RESEND] rust: replace `build_assert!` with `const_assert!`
config: x86_64-randconfig-161-20260526 (https://download.01.org/0day-ci/archive/20260526/202605261249.YsIyj5Ye-lkp@intel.com/config)
compiler: clang version 20.1.8 (https://github.com/llvm/llvm-project 87f0227cb60147a26a1eeb4fb06e3b505e9c7261)
rustc: rustc 1.88.0 (6b00bc388 2025-06-23)
smatch: v0.5.0-9185-gbcc58b9c
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20260526/202605261249.YsIyj5Ye-lkp@intel.com/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202605261249.YsIyj5Ye-lkp@intel.com/
All error/warnings (new ones prefixed by >>):
>> error[E0432]: unresolved import `crate::const_assert`
--> rust/kernel/sync/locked_by.rs:6:5
|
6 | use crate::const_assert;
| ^^^^^^^------------
| | |
| | help: a similar name exists in the module: `kunit_assert`
| no `const_assert` in the root
--
>> error[E0432]: unresolved import `crate::const_assert`
--> rust/kernel/xarray.rs:8:22
|
8 | alloc, bindings, const_assert,
| ^^^^^^^^^^^^
| |
| no `const_assert` in the root
| help: a similar name exists in the module: `kunit_assert`
--
>> error: cannot find macro `const_assert` in this scope
--> rust/kernel/list/arc.rs:254:9
|
254 | const_assert!(ID != ID2);
| ^^^^^^^^^^^^
--
>> error: cannot find macro `const_assert` in this scope
--> rust/kernel/i2c.rs:105:9
|
105 | const_assert!(
| ^^^^^^^^^^^^
--
>> error: cannot find macro `const_assert` in this scope
--> rust/kernel/dma.rs:386:9
|
386 | const_assert!(
| ^^^^^^^^^^^^
--
>> warning: unused import: `mem::size_of`
--> rust/kernel/sync/locked_by.rs:7:30
|
7 | use core::{cell::UnsafeCell, mem::size_of, ptr};
| ^^^^^^^^^^^^
|
= note: `#[warn(unused_imports)]` on by default
--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-05-26 4:34 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-05-23 12:27 [PATCH RESEND] rust: replace `build_assert!` with `const_assert!` Mohamad Alsadhan
2026-05-26 4:33 ` kernel test robot
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®