Thread (66 messages) 66 messages, 16 authors, 2021-07-23

[PATCH 08/17] rust: add `build_error` crate

STALE1899d

From: ojeda@kernel.org
Date: 2021-07-04 20:29:12
Also in: linux-doc, linux-kbuild, lkml
Subsystem: rust, the rest · Maintainers: Miguel Ojeda, Linus Torvalds

Revision v1 of 10 in this series.

Revisions (10)
  1. v1 current
  2. v1 [diff vs current]
  3. v3 [diff vs current]
  4. v4 [diff vs current]
  5. v5 [diff vs current]
  6. v6 [diff vs current]
  7. v7 [diff vs current]
  8. v8 [diff vs current]
  9. v9 [diff vs current]
  10. v10 [diff vs current]
From: Miguel Ojeda <ojeda@kernel.org>

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 <redacted>
Signed-off-by: Alex Gaynor <redacted>
Co-developed-by: Geoffrey Thomas <redacted>
Signed-off-by: Geoffrey Thomas <redacted>
Co-developed-by: Finn Behrens <redacted>
Signed-off-by: Finn Behrens <redacted>
Co-developed-by: Adam Bratschi-Kaye <redacted>
Signed-off-by: Adam Bratschi-Kaye <redacted>
Co-developed-by: Wedson Almeida Filho <redacted>
Signed-off-by: Wedson Almeida Filho <redacted>
Co-developed-by: Boqun Feng <redacted>
Signed-off-by: Boqun Feng <redacted>
Co-developed-by: Sumera Priyadarsini <redacted>
Signed-off-by: Sumera Priyadarsini <redacted>
Co-developed-by: Michael Ellerman <mpe@ellerman.id.au>
Signed-off-by: Michael Ellerman <mpe@ellerman.id.au>
Co-developed-by: Sven Van Asbroeck <redacted>
Signed-off-by: Sven Van Asbroeck <redacted>
Co-developed-by: Gary Guo <gary@garyguo.net>
Signed-off-by: Gary Guo <gary@garyguo.net>
Co-developed-by: Boris-Chengbiao Zhou <redacted>
Signed-off-by: Boris-Chengbiao Zhou <redacted>
Co-developed-by: Fox Chen <redacted>
Signed-off-by: Fox Chen <redacted>
Co-developed-by: Ayaan Zaidi <redacted>
Signed-off-by: Ayaan Zaidi <redacted>
Co-developed-by: Douglas Su <redacted>
Signed-off-by: Douglas Su <redacted>
Co-developed-by: Yuki Okushi <redacted>
Signed-off-by: Yuki Okushi <redacted>
Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
---
 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
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help