From: Miguel Ojeda <ojeda@xxxxxxxxxx> The `build_error` crate provides the `build_error` function which is then used to provide the `build_error!` and the `build_assert!` macros. `build_assert!` is intended to be used when `static_assert!` cannot be used, e.g. when the condition refers to generic parameters or parameters of an inline function. Co-developed-by: Alex Gaynor <alex.gaynor@xxxxxxxxx> Signed-off-by: Alex Gaynor <alex.gaynor@xxxxxxxxx> Co-developed-by: Geoffrey Thomas <geofft@xxxxxxxxxxxxx> Signed-off-by: Geoffrey Thomas <geofft@xxxxxxxxxxxxx> Co-developed-by: Finn Behrens <me@xxxxxxxxx> Signed-off-by: Finn Behrens <me@xxxxxxxxx> Co-developed-by: Adam Bratschi-Kaye <ark.email@xxxxxxxxx> Signed-off-by: Adam Bratschi-Kaye <ark.email@xxxxxxxxx> Co-developed-by: Wedson Almeida Filho <wedsonaf@xxxxxxxxxx> Signed-off-by: Wedson Almeida Filho <wedsonaf@xxxxxxxxxx> Co-developed-by: Boqun Feng <boqun.feng@xxxxxxxxx> Signed-off-by: Boqun Feng <boqun.feng@xxxxxxxxx> Co-developed-by: Sumera Priyadarsini <sylphrenadin@xxxxxxxxx> Signed-off-by: Sumera Priyadarsini <sylphrenadin@xxxxxxxxx> Co-developed-by: Michael Ellerman <mpe@xxxxxxxxxxxxxx> Signed-off-by: Michael Ellerman <mpe@xxxxxxxxxxxxxx> Co-developed-by: Sven Van Asbroeck <thesven73@xxxxxxxxx> Signed-off-by: Sven Van Asbroeck <thesven73@xxxxxxxxx> Co-developed-by: Gary Guo <gary@xxxxxxxxxxx> Signed-off-by: Gary Guo <gary@xxxxxxxxxxx> Co-developed-by: Boris-Chengbiao Zhou <bobo1239@xxxxxx> Signed-off-by: Boris-Chengbiao Zhou <bobo1239@xxxxxx> Co-developed-by: Fox Chen <foxhlchen@xxxxxxxxx> Signed-off-by: Fox Chen <foxhlchen@xxxxxxxxx> Co-developed-by: Ayaan Zaidi <zaidi.ayaan@xxxxxxxxx> Signed-off-by: Ayaan Zaidi <zaidi.ayaan@xxxxxxxxx> Co-developed-by: Douglas Su <d0u9.su@xxxxxxxxxxx> Signed-off-by: Douglas Su <d0u9.su@xxxxxxxxxxx> Co-developed-by: Yuki Okushi <jtitor@xxxxxxxx> Signed-off-by: Yuki Okushi <jtitor@xxxxxxxx> Signed-off-by: Miguel Ojeda <ojeda@xxxxxxxxxx> --- rust/build_error.rs | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 rust/build_error.rs diff --git a/rust/build_error.rs b/rust/build_error.rs new file mode 100644 index 00000000000..d47fa8393cb --- /dev/null +++ b/rust/build_error.rs @@ -0,0 +1,33 @@ +// SPDX-License-Identifier: GPL-2.0 + +//! Build-time error. +//! +//! This crate provides a function `build_error`, which will panic in +//! compile-time if executed in const context, and will cause a build error +//! if not executed at compile time and the optimizer does not optimise away the +//! call. +//! +//! It is used by `build_assert!` in the kernel crate, allowing checking of +//! conditions that could be checked statically, but could not be enforced in +//! Rust yet (e.g. perform some checks in const functions, but those +//! functions could still be called in the runtime). + +#![no_std] +#![feature(const_panic, core_panic)] + +/// Panics if executed in const context, or triggers a build error if not. +#[inline(never)] +#[cold] +#[no_mangle] +#[track_caller] +pub const fn build_error(msg: &'static str) -> ! { + // Could also be `panic!(msg)` to avoid using unstable feature `core_panic`, + // but it is not allowed in Rust 2021, while `panic!("{}", msg)` could not + // yet be used in const context. + core::panicking::panic(msg); +} + +#[cfg(CONFIG_RUST_BUILD_ASSERT_WARN)] +#[link_section = ".gnu.warning.build_error"] +#[used] +static BUILD_ERROR_WARNING: [u8; 45] = *b"call to build_error present after compilation"; -- 2.32.0