[lkml]   [2022]   [May]   [22]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
Messages in this thread
Patch in this message
Subject[PATCH v7 09/25] rust: add `build_error` crate
From: Gary Guo <>

The `build_error` crate provides the `build_error` function which
is then used to provide the `build_error!` and the `build_assert!`

`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 <>
Signed-off-by: Alex Gaynor <>
Co-developed-by: Wedson Almeida Filho <>
Signed-off-by: Wedson Almeida Filho <>
Signed-off-by: Gary Guo <>
Co-developed-by: Miguel Ojeda <>
Signed-off-by: Miguel Ojeda <>
rust/ | 29 +++++++++++++++++++++++++++++
1 file changed, 29 insertions(+)
create mode 100644 rust/

diff --git a/rust/ b/rust/
new file mode 100644
index 000000000000..34e589149d3e
--- /dev/null
+++ b/rust/
@@ -0,0 +1,29 @@
+// 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).
+/// Panics if executed in const context, or triggers a build error if not.
+pub const fn build_error(msg: &'static str) -> ! {
+ panic!("{}", msg);
+#[link_section = ".gnu.warning.build_error"]
+static BUILD_ERROR_WARNING: [u8; 45] = *b"call to build_error present after compilation";
 \ /
  Last update: 2022-05-23 04:05    [W:0.263 / U:0.068 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site