Debian Codegraph @

mozglue/static/rust/moz_asserts/lib.rs

raw
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5 // Implementation note: It seems tempting to use if cfg!(feature = ...) directly
6 // in the macro, but that doesn't work, as the feature would be evaluated
7 // against the dependent crate.
8 //
9 // It'd also seem tempting to export different macros depending on the features
10 // enabled, but that's also not great because it causes warnings when the
11 // features are enabled, e.g.:
12 //
13 // nightly_panic!("foo");
14 // return SAFE_VALUE;
15 //
16 // would cause an unreachable code warning for Nightly builds. So instead we
17 // choose an exported constant to guard the condition. (For reference, this
18 // is also how rust's `debug_assert!` is implemented)
19
20 /// Whether Nightly-only assertions are enabled.
21 pub use mozbuild::config::NIGHTLY_BUILD;
22
23 /// Whether diagnostic assertions are enabled.
24 pub use mozbuild::config::MOZ_DIAGNOSTIC_ASSERT_ENABLED;
25
26 /// assert! on Nightly, gets compiled out otherwise.
27 #[macro_export]
28 macro_rules! nightly_assert {
29 ($($arg:tt)*) => (if $crate::NIGHTLY_BUILD { assert!($($arg)*); })
30 }
31
32 /// assert_eq! on Nightly, gets compiled out otherwise.
33 #[macro_export]
34 macro_rules! nightly_assert_eq {
35 ($($arg:tt)*) => (if $crate::NIGHTLY_BUILD { assert_eq!($($arg)*); })
36 }
37
38 /// assert_ne! on Nightly, gets compiled out otherwise.
39 #[macro_export]
40 macro_rules! nightly_assert_ne {
41 ($($arg:tt)*) => (if $crate::NIGHTLY_BUILD { assert_ne!($($arg)*); })
42 }
43
44 /// panic! on Nightly, gets compiled out otherwise.
45 #[macro_export]
46 macro_rules! nightly_panic {
47 ($($arg:tt)*) => (if $crate::NIGHTLY_BUILD { panic!($($arg)*); })
48 }
49
50 /// unreachable! on Nightly, `std::hint::unreachable_unchecked()` otherwise.
51 ///
52 /// Use carefully! Consider using nightly_panic! and handling the failure
53 /// gracefully if you can't prove it's really unreachable.
54 ///
55 /// See https://doc.rust-lang.org/std/hint/fn.unreachable_unchecked.html#safety
56 /// for safety details.
57 #[macro_export]
58 macro_rules! nightly_unreachable {
59 ($($arg:tt)*) => {
60 if $crate::NIGHTLY_BUILD {
61 unreachable!($($arg)*);
62 } else {
63 std::hint::unreachable_unchecked()
64 }
65 }
66 }
67
68 /// assert! when diagnostic asserts are enabled, gets compiled out otherwise.
69 #[macro_export]
70 macro_rules! diagnostic_assert {
71 ($($arg:tt)*) => (if $crate::MOZ_DIAGNOSTIC_ASSERT_ENABLED { assert!($($arg)*); })
72 }
73
74 /// assert_eq! when diagnostic asserts are enabled, gets compiled out otherwise.
75 #[macro_export]
76 macro_rules! diagnostic_assert_eq {
77 ($($arg:tt)*) => (if $crate::MOZ_DIAGNOSTIC_ASSERT_ENABLED { assert_eq!($($arg)*); })
78 }
79
80 /// assert_ne! when diagnostic asserts are enabled, gets compiled out otherwise.
81 #[macro_export]
82 macro_rules! diagnostic_assert_ne {
83 ($($arg:tt)*) => (if $crate::MOZ_DIAGNOSTIC_ASSERT_ENABLED { assert_ne!($($arg)*); })
84 }
85
86 /// unreachable! when diagnostic asserts are enabled,
87 /// `std::hint::unreachable_unchecked()` otherwise.
88 ///
89 /// Use carefully! Consider using diagnostic_panic! and handling the failure
90 /// gracefully if you can't prove it's really unreachable.
91 ///
92 /// See https://doc.rust-lang.org/std/hint/fn.unreachable_unchecked.html#safety
93 /// for safety details.
94 #[macro_export]
95 macro_rules! diagnostic_unreachable {
96 ($($arg:tt)*) => {
97 if $crate::MOZ_DIAGNOSTIC_ASSERT_ENABLED {
98 unreachable!($($arg)*);
99 } else {
100 std::hint::unreachable_unchecked()
101 }
102 }
103 }