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 } 40