Skip to content

Commit abfe5ee

Browse files
nbdd0121ojeda
authored andcommitted
rust: move static_assert into build_assert
Conceptually, `static_assert` is also a build-time assertion that occurs earlier in the pipeline. Consolidate the implementation so that we can use this as the canonical place to add more useful build-time assertions. Reviewed-by: Yury Norov <ynorov@nvidia.com> Signed-off-by: Gary Guo <gary@garyguo.net> Reviewed-by: Alice Ryhl <aliceryhl@google.com> Reviewed-by: Danilo Krummrich <dakr@kernel.org> Link: https://patch.msgid.link/20260319121653.2975748-2-gary@kernel.org [ Used kernel vertical style. - Miguel ] Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
1 parent 4f13c93 commit abfe5ee

4 files changed

Lines changed: 42 additions & 46 deletions

File tree

rust/kernel/build_assert.rs

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,46 @@
11
// SPDX-License-Identifier: GPL-2.0
22

3-
//! Build-time assert.
3+
//! Various assertions that happen during build-time.
44
55
#[doc(hidden)]
66
pub use build_error::build_error;
77

8+
/// Static assert (i.e. compile-time assert).
9+
///
10+
/// Similar to C11 [`_Static_assert`] and C++11 [`static_assert`].
11+
///
12+
/// An optional panic message can be supplied after the expression.
13+
/// Currently only a string literal without formatting is supported
14+
/// due to constness limitations of the [`assert!`] macro.
15+
///
16+
/// The feature may be added to Rust in the future: see [RFC 2790].
17+
///
18+
/// [`_Static_assert`]: https://en.cppreference.com/w/c/language/_Static_assert
19+
/// [`static_assert`]: https://en.cppreference.com/w/cpp/language/static_assert
20+
/// [RFC 2790]: https://github.com/rust-lang/rfcs/issues/2790
21+
///
22+
/// # Examples
23+
///
24+
/// ```
25+
/// static_assert!(42 > 24);
26+
/// static_assert!(core::mem::size_of::<u8>() == 1);
27+
///
28+
/// const X: &[u8] = b"bar";
29+
/// static_assert!(X[1] == b'a');
30+
///
31+
/// const fn f(x: i32) -> i32 {
32+
/// x + 2
33+
/// }
34+
/// static_assert!(f(40) == 42);
35+
/// static_assert!(f(40) == 42, "f(x) must add 2 to the given input.");
36+
/// ```
37+
#[macro_export]
38+
macro_rules! static_assert {
39+
($condition:expr $(,$arg:literal)?) => {
40+
const _: () = ::core::assert!($condition $(,$arg)?);
41+
};
42+
}
43+
844
/// Fails the build if the code path calling `build_error!` can possibly be executed.
945
///
1046
/// If the macro is executed in const context, `build_error!` will panic.
@@ -74,8 +110,6 @@ macro_rules! build_error {
74110
/// assert!(n > 1); // Run-time check
75111
/// }
76112
/// ```
77-
///
78-
/// [`static_assert!`]: crate::static_assert!
79113
#[macro_export]
80114
macro_rules! build_assert {
81115
($cond:expr $(,)?) => {{

rust/kernel/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,6 @@ pub mod sizes;
144144
pub mod slice;
145145
#[cfg(CONFIG_SOC_BUS)]
146146
pub mod soc;
147-
mod static_assert;
148147
#[doc(hidden)]
149148
pub mod std_vendor;
150149
pub mod str;

rust/kernel/prelude.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,11 @@ pub use macros::{export, fmt, kunit_tests, module, vtable};
2929

3030
pub use pin_init::{init, pin_data, pin_init, pinned_drop, InPlaceWrite, Init, PinInit, Zeroable};
3131

32-
pub use super::{build_assert, build_error};
32+
pub use super::{
33+
build_assert,
34+
build_error,
35+
static_assert, //
36+
};
3337

3438
// `super::std_vendor` is hidden, which makes the macro inline for some reason.
3539
#[doc(no_inline)]
@@ -39,8 +43,6 @@ pub use super::{pr_alert, pr_crit, pr_debug, pr_emerg, pr_err, pr_info, pr_notic
3943

4044
pub use super::{try_init, try_pin_init};
4145

42-
pub use super::static_assert;
43-
4446
pub use super::error::{code::*, Error, Result};
4547

4648
pub use super::{str::CStrExt as _, ThisModule};

rust/kernel/static_assert.rs

Lines changed: 0 additions & 39 deletions
This file was deleted.

0 commit comments

Comments
 (0)