Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

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>

authored by

Gary Guo and committed by
Miguel Ojeda
abfe5ee9 4f13c934

+42 -46
+37 -3
rust/kernel/build_assert.rs
··· 1 1 // SPDX-License-Identifier: GPL-2.0 2 2 3 - //! Build-time assert. 3 + //! Various assertions that happen during build-time. 4 4 5 5 #[doc(hidden)] 6 6 pub use build_error::build_error; 7 + 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 + } 7 43 8 44 /// Fails the build if the code path calling `build_error!` can possibly be executed. 9 45 /// ··· 110 74 /// assert!(n > 1); // Run-time check 111 75 /// } 112 76 /// ``` 113 - /// 114 - /// [`static_assert!`]: crate::static_assert! 115 77 #[macro_export] 116 78 macro_rules! build_assert { 117 79 ($cond:expr $(,)?) => {{
-1
rust/kernel/lib.rs
··· 144 144 pub mod slice; 145 145 #[cfg(CONFIG_SOC_BUS)] 146 146 pub mod soc; 147 - mod static_assert; 148 147 #[doc(hidden)] 149 148 pub mod std_vendor; 150 149 pub mod str;
+5 -3
rust/kernel/prelude.rs
··· 29 29 30 30 pub use pin_init::{init, pin_data, pin_init, pinned_drop, InPlaceWrite, Init, PinInit, Zeroable}; 31 31 32 - pub use super::{build_assert, build_error}; 32 + pub use super::{ 33 + build_assert, 34 + build_error, 35 + static_assert, // 36 + }; 33 37 34 38 // `super::std_vendor` is hidden, which makes the macro inline for some reason. 35 39 #[doc(no_inline)] ··· 42 38 pub use super::{pr_alert, pr_crit, pr_debug, pr_emerg, pr_err, pr_info, pr_notice, pr_warn}; 43 39 44 40 pub use super::{try_init, try_pin_init}; 45 - 46 - pub use super::static_assert; 47 41 48 42 pub use super::error::{code::*, Error, Result}; 49 43
-39
rust/kernel/static_assert.rs
··· 1 - // SPDX-License-Identifier: GPL-2.0 2 - 3 - //! Static assert. 4 - 5 - /// Static assert (i.e. compile-time assert). 6 - /// 7 - /// Similar to C11 [`_Static_assert`] and C++11 [`static_assert`]. 8 - /// 9 - /// An optional panic message can be supplied after the expression. 10 - /// Currently only a string literal without formatting is supported 11 - /// due to constness limitations of the [`assert!`] macro. 12 - /// 13 - /// The feature may be added to Rust in the future: see [RFC 2790]. 14 - /// 15 - /// [`_Static_assert`]: https://en.cppreference.com/w/c/language/_Static_assert 16 - /// [`static_assert`]: https://en.cppreference.com/w/cpp/language/static_assert 17 - /// [RFC 2790]: https://github.com/rust-lang/rfcs/issues/2790 18 - /// 19 - /// # Examples 20 - /// 21 - /// ``` 22 - /// static_assert!(42 > 24); 23 - /// static_assert!(core::mem::size_of::<u8>() == 1); 24 - /// 25 - /// const X: &[u8] = b"bar"; 26 - /// static_assert!(X[1] == b'a'); 27 - /// 28 - /// const fn f(x: i32) -> i32 { 29 - /// x + 2 30 - /// } 31 - /// static_assert!(f(40) == 42); 32 - /// static_assert!(f(40) == 42, "f(x) must add 2 to the given input."); 33 - /// ``` 34 - #[macro_export] 35 - macro_rules! static_assert { 36 - ($condition:expr $(,$arg:literal)?) => { 37 - const _: () = ::core::assert!($condition $(,$arg)?); 38 - }; 39 - }