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