mycelium_util/
macros.rs

1/// Indicates unreachable code that we are confident is *truly* unreachable.
2///
3/// This is essentially a compromise between `core::unreachable!()` and
4/// `core::hint::unreachable_unchecked()`. In debug mode builds and in tests,
5/// this expands to `unreachable!()`, causing a panic. However, in release mode
6/// non-test builds, this expands to `unreachable_unchecked`. Thus, this is a
7/// somewhat safer form of `unreachable_unchecked` that will allow cases where
8/// `unreachable_unchecked` would be invalid to be detected early.
9///
10/// Nonetheless, this must still be used with caution! If code is not adequately
11/// tested, it is entirely possible for the `unreachable_unchecked` to be
12/// reached in a scenario that was not reachable in tests.
13#[macro_export]
14macro_rules! unreachable_unchecked {
15    () => ({
16        #[cfg(any(test, debug_assertions))]
17        panic!(
18            "internal error: entered unreachable code \n\",
19            /!\\ EXTREMELY SERIOUS WARNING: in release mode, this would have been \n\
20            \x32   `unreachable_unchecked`! This could result in undefine behavior. \n\
21            \x32   Please double- or triple-check any assumptions about code which \n\
22            \x32   could lead to this being triggered."
23        );
24        #[allow(unreachable_code)] // lol
25        {
26            core::hint::unreachable_unchecked();
27        }
28    });
29    ($msg:expr) => ({
30        $crate::unreachable_unchecked!("{}", $msg)
31    });
32    ($msg:expr,) => ({
33        $crate::unreachable_unchecked!($msg)
34    });
35    ($fmt:expr, $($arg:tt)*) => ({
36        #[cfg(any(test, debug_assertions))]
37        #[allow(clippy::literal_string_with_formatting_args)]
38        panic!(
39            concat!(
40                "internal error: entered unreachable code: ",
41                $fmt,
42                "\n/!\\ EXTREMELY SERIOUS WARNING: in release mode, this would have been \n\
43                \x32   `unreachable_unchecked`! This could result in undefine behavior. \n\
44                \x32   Please double- or triple-check any assumptions about code which \n\
45                \x32   could lead to this being triggered."
46            ),
47            $($arg)*
48        );
49        #[allow(unreachable_code)] // lol
50        {
51            core::hint::unreachable_unchecked();
52        }
53    });
54}
55
56#[cfg(all(test, not(loom)))]
57macro_rules! test_info {
58    ($($arg:tt)+) => {
59        tracing::trace!($($arg)+);
60    };
61}
62
63#[cfg(all(test, loom))]
64macro_rules! test_info {
65    ($($arg:tt)+) => {
66        tracing_01::trace!($($arg)+);
67    };
68}