diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 09:22:09 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 09:22:09 +0000 |
commit | 43a97878ce14b72f0981164f87f2e35e14151312 (patch) | |
tree | 620249daf56c0258faa40cbdcf9cfba06de2a846 /mozglue/static/rust/moz_asserts/lib.rs | |
parent | Initial commit. (diff) | |
download | firefox-upstream.tar.xz firefox-upstream.zip |
Adding upstream version 110.0.1.upstream/110.0.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'mozglue/static/rust/moz_asserts/lib.rs')
-rw-r--r-- | mozglue/static/rust/moz_asserts/lib.rs | 103 |
1 files changed, 103 insertions, 0 deletions
diff --git a/mozglue/static/rust/moz_asserts/lib.rs b/mozglue/static/rust/moz_asserts/lib.rs new file mode 100644 index 0000000000..f4b00a5ad2 --- /dev/null +++ b/mozglue/static/rust/moz_asserts/lib.rs @@ -0,0 +1,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() + } + } +} |