summaryrefslogtreecommitdiffstats
path: root/mozglue/static/rust/moz_asserts/lib.rs
blob: f4b00a5ad2fb3e26ee5cfc0c874d33ac9a5a806d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */

// Implementation note: It seems tempting to use if cfg!(feature = ...) directly
// in the macro, but that doesn't work, as the feature would be evaluated
// against the dependent crate.
//
// It'd also seem tempting to export different macros depending on the features
// enabled, but that's also not great because it causes warnings when the
// features are enabled, e.g.:
//
//   nightly_panic!("foo");
//   return SAFE_VALUE;
//
// would cause an unreachable code warning for Nightly builds. So instead we
// choose an exported constant to guard the condition. (For reference, this
// is also how rust's `debug_assert!` is implemented)

/// Whether Nightly-only assertions are enabled.
pub use mozbuild::config::NIGHTLY_BUILD;

/// Whether diagnostic assertions are enabled.
pub use mozbuild::config::MOZ_DIAGNOSTIC_ASSERT_ENABLED;

/// assert! on Nightly, gets compiled out otherwise.
#[macro_export]
macro_rules! nightly_assert {
    ($($arg:tt)*) => (if $crate::NIGHTLY_BUILD { assert!($($arg)*); })
}

/// assert_eq! on Nightly, gets compiled out otherwise.
#[macro_export]
macro_rules! nightly_assert_eq {
    ($($arg:tt)*) => (if $crate::NIGHTLY_BUILD { assert_eq!($($arg)*); })
}

/// assert_ne! on Nightly, gets compiled out otherwise.
#[macro_export]
macro_rules! nightly_assert_ne {
    ($($arg:tt)*) => (if $crate::NIGHTLY_BUILD { assert_ne!($($arg)*); })
}

/// panic! on Nightly, gets compiled out otherwise.
#[macro_export]
macro_rules! nightly_panic {
    ($($arg:tt)*) => (if $crate::NIGHTLY_BUILD { panic!($($arg)*); })
}

/// unreachable! on Nightly, `std::hint::unreachable_unchecked()` otherwise.
///
/// Use carefully! Consider using nightly_panic! and handling the failure
/// gracefully if you can't prove it's really unreachable.
///
/// See https://doc.rust-lang.org/std/hint/fn.unreachable_unchecked.html#safety
/// for safety details.
#[macro_export]
macro_rules! nightly_unreachable {
    ($($arg:tt)*) => {
        if $crate::NIGHTLY_BUILD {
            unreachable!($($arg)*);
        } else {
            std::hint::unreachable_unchecked()
        }
    }
}

/// assert! when diagnostic asserts are enabled, gets compiled out otherwise.
#[macro_export]
macro_rules! diagnostic_assert {
    ($($arg:tt)*) => (if $crate::MOZ_DIAGNOSTIC_ASSERT_ENABLED { assert!($($arg)*); })
}

/// assert_eq! when diagnostic asserts are enabled, gets compiled out otherwise.
#[macro_export]
macro_rules! diagnostic_assert_eq {
    ($($arg:tt)*) => (if $crate::MOZ_DIAGNOSTIC_ASSERT_ENABLED { assert_eq!($($arg)*); })
}

/// assert_ne! when diagnostic asserts are enabled, gets compiled out otherwise.
#[macro_export]
macro_rules! diagnostic_assert_ne {
    ($($arg:tt)*) => (if $crate::MOZ_DIAGNOSTIC_ASSERT_ENABLED { assert_ne!($($arg)*); })
}

/// unreachable! when diagnostic asserts are enabled,
/// `std::hint::unreachable_unchecked()` otherwise.
///
/// Use carefully! Consider using diagnostic_panic! and handling the failure
/// gracefully if you can't prove it's really unreachable.
///
/// See https://doc.rust-lang.org/std/hint/fn.unreachable_unchecked.html#safety
/// for safety details.
#[macro_export]
macro_rules! diagnostic_unreachable {
    ($($arg:tt)*) => {
        if $crate::MOZ_DIAGNOSTIC_ASSERT_ENABLED {
            unreachable!($($arg)*);
        } else {
            std::hint::unreachable_unchecked()
        }
    }
}