forked from torvalds/linux
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The `build_error` crate provides a function `build_error` which will panic at compile-time if executed in const context and, by default, will cause a build error if not executed at compile time and the optimizer does not optimise away the call. The `CONFIG_RUST_BUILD_ASSERT_ALLOW` kernel option allows to relax the default build failure and convert it to a runtime check. If the runtime check fails, `panic!` will be called. Its functionality will be exposed to users as a couple macros in the `kernel` crate in the following patch, thus some documentation here refers to them for simplicity. Signed-off-by: Gary Guo <gary@garyguo.net> Reviewed-by: Wei Liu <wei.liu@kernel.org> [Reworded, adapted for upstream and applied latest changes] Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
- Loading branch information
Showing
5 changed files
with
76 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// SPDX-License-Identifier: GPL-2.0 | ||
|
||
//! Build-time error. | ||
//! | ||
//! This crate provides a [const function][const-functions] `build_error`, which will panic in | ||
//! compile-time if executed in [const context][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][const-functions], but those | ||
//! functions could still be called in the runtime). | ||
//! | ||
//! For details on constant evaluation in Rust, please see the [Reference][const-eval]. | ||
//! | ||
//! [const-eval]: https://doc.rust-lang.org/reference/const_eval.html | ||
//! [const-functions]: https://doc.rust-lang.org/reference/const_eval.html#const-functions | ||
//! [const-context]: https://doc.rust-lang.org/reference/const_eval.html#const-context | ||
#![no_std] | ||
|
||
/// Panics if executed in [const context][const-context], or triggers a build error if not. | ||
/// | ||
/// [const-context]: https://doc.rust-lang.org/reference/const_eval.html#const-context | ||
#[inline(never)] | ||
#[cold] | ||
#[export_name = "rust_build_error"] | ||
#[track_caller] | ||
pub const fn build_error(msg: &'static str) -> ! { | ||
panic!("{}", msg); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters