// SPDX-License-Identifier: GPL-2.012//! Build-time assert.34#[doc(hidden)]5pub use build_error::build_error;67/// Fails the build if the code path calling `build_error!` can possibly be executed.8///9/// If the macro is executed in const context, `build_error!` will panic.10/// If the compiler or optimizer cannot guarantee that `build_error!` can never11/// be called, a build error will be triggered.12///13/// # Examples14///15/// ```16/// #[inline]17/// fn foo(a: usize) -> usize {18/// a.checked_add(1).unwrap_or_else(|| build_error!("overflow"))19/// }20///21/// assert_eq!(foo(usize::MAX - 1), usize::MAX); // OK.22/// // foo(usize::MAX); // Fails to compile.23/// ```24#[macro_export]25macro_rules! build_error {26() => {{27$crate::build_assert::build_error("")28}};29($msg:expr) => {{30$crate::build_assert::build_error($msg)31}};32}3334/// Asserts that a boolean expression is `true` at compile time.35///36/// If the condition is evaluated to `false` in const context, `build_assert!`37/// will panic. If the compiler or optimizer cannot guarantee the condition will38/// be evaluated to `true`, a build error will be triggered.39///40/// [`static_assert!`] should be preferred to `build_assert!` whenever possible.41///42/// # Examples43///44/// These examples show that different types of [`assert!`] will trigger errors45/// at different stage of compilation. It is preferred to err as early as46/// possible, so [`static_assert!`] should be used whenever possible.47/// ```ignore48/// fn foo() {49/// static_assert!(1 > 1); // Compile-time error50/// build_assert!(1 > 1); // Build-time error51/// assert!(1 > 1); // Run-time error52/// }53/// ```54///55/// When the condition refers to generic parameters or parameters of an inline function,56/// [`static_assert!`] cannot be used. Use `build_assert!` in this scenario.57/// ```58/// fn foo<const N: usize>() {59/// // `static_assert!(N > 1);` is not allowed60/// build_assert!(N > 1); // Build-time check61/// assert!(N > 1); // Run-time check62/// }63///64/// #[inline]65/// fn bar(n: usize) {66/// // `static_assert!(n > 1);` is not allowed67/// build_assert!(n > 1); // Build-time check68/// assert!(n > 1); // Run-time check69/// }70/// ```71///72/// [`static_assert!`]: crate::static_assert!73#[macro_export]74macro_rules! build_assert {75($cond:expr $(,)?) => {{76if !$cond {77$crate::build_assert::build_error(concat!("assertion failed: ", stringify!($cond)));78}79}};80($cond:expr, $msg:expr) => {{81if !$cond {82$crate::build_assert::build_error($msg);83}84}};85}868788