xref: /openbmc/linux/rust/build_error.rs (revision 7ae9fb1b7ecbb5d85d07857943f677fd1a559b18)
1*ecaa6ddfSGary Guo // SPDX-License-Identifier: GPL-2.0
2*ecaa6ddfSGary Guo 
3*ecaa6ddfSGary Guo //! Build-time error.
4*ecaa6ddfSGary Guo //!
5*ecaa6ddfSGary Guo //! This crate provides a [const function][const-functions] `build_error`, which will panic in
6*ecaa6ddfSGary Guo //! compile-time if executed in [const context][const-context], and will cause a build error
7*ecaa6ddfSGary Guo //! if not executed at compile time and the optimizer does not optimise away the call.
8*ecaa6ddfSGary Guo //!
9*ecaa6ddfSGary Guo //! It is used by `build_assert!` in the kernel crate, allowing checking of
10*ecaa6ddfSGary Guo //! conditions that could be checked statically, but could not be enforced in
11*ecaa6ddfSGary Guo //! Rust yet (e.g. perform some checks in [const functions][const-functions], but those
12*ecaa6ddfSGary Guo //! functions could still be called in the runtime).
13*ecaa6ddfSGary Guo //!
14*ecaa6ddfSGary Guo //! For details on constant evaluation in Rust, please see the [Reference][const-eval].
15*ecaa6ddfSGary Guo //!
16*ecaa6ddfSGary Guo //! [const-eval]: https://doc.rust-lang.org/reference/const_eval.html
17*ecaa6ddfSGary Guo //! [const-functions]: https://doc.rust-lang.org/reference/const_eval.html#const-functions
18*ecaa6ddfSGary Guo //! [const-context]: https://doc.rust-lang.org/reference/const_eval.html#const-context
19*ecaa6ddfSGary Guo 
20*ecaa6ddfSGary Guo #![no_std]
21*ecaa6ddfSGary Guo 
22*ecaa6ddfSGary Guo /// Panics if executed in [const context][const-context], or triggers a build error if not.
23*ecaa6ddfSGary Guo ///
24*ecaa6ddfSGary Guo /// [const-context]: https://doc.rust-lang.org/reference/const_eval.html#const-context
25*ecaa6ddfSGary Guo #[inline(never)]
26*ecaa6ddfSGary Guo #[cold]
27*ecaa6ddfSGary Guo #[export_name = "rust_build_error"]
28*ecaa6ddfSGary Guo #[track_caller]
build_error(msg: &'static str) -> !29*ecaa6ddfSGary Guo pub const fn build_error(msg: &'static str) -> ! {
30*ecaa6ddfSGary Guo     panic!("{}", msg);
31*ecaa6ddfSGary Guo }
32