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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
//! Hydroflow declarative macros.

/// [`assert!`] but returns a [`Result<(), String>`] instead of panicking.
#[macro_export]
macro_rules! rassert {
    ($cond:expr $(,)?) => {
        $crate::rassert!($cond, "assertion failed: `{}`", stringify!($cond))
    };
    ($cond:expr, $fmt:literal) => {
        $crate::rassert!($cond, $fmt,)
    };
    ($cond:expr, $fmt:literal, $($arg:tt)*) => {
        {
            if $cond {
                Ok(())
            }
            else {
                Err(format!($fmt, $($arg)*))
            }
        }
    };
}

/// [`assert_eq!`] but returns a [`Result<(), String>`] instead of panicking.
#[macro_export]
macro_rules! rassert_eq {
    ($a:expr, $b:expr) => {
        $crate::rassert!($a == $b,)
    };
    ($a:expr, $b:expr, $($arg:tt)*) => {
        $crate::rassert!($a == $b, $($arg)*)
    };
}

/// Asserts that the variable's type implements the given traits.
#[macro_export]
macro_rules! assert_var_impl {
    ($var:ident: $($trait:path),+ $(,)?) => {
        let _ = || {
            // Only callable when `$var` implements all traits in `$($trait)+`.
            fn assert_var_impl<T: ?Sized $(+ $trait)+>(_x: &T) {}
            assert_var_impl(& $var);
        };
    };
}

/// Tests that the given warnings are emitted by the hydroflow macro invocation.
///
/// For example usage, see `hydroflow/tests/surface_warnings.rs`.
#[macro_export]
macro_rules! hydroflow_expect_warnings {
    (
        $hf:tt,
        $( $msg:literal ),*
        $( , )?
    ) => {
        {
            let __file = std::file!();
            let __line = std::line!() as usize;
            let __hf = hydroflow::hydroflow_syntax_noemit! $hf;

            let actuals = __hf.diagnostics().expect("Expected `diagnostics()` to be set.");
            let actuals_len = actuals.len();
            let actuals = std::collections::BTreeSet::from_iter(actuals.iter().cloned().map(|mut actual| {
                actual.span.line = actual.span.line.saturating_sub(__line);
                std::borrow::Cow::<'static, str>::Owned(actual.to_string().replace(__file, "$FILE"))
            }));

            let expecteds = [
                $(
                    std::borrow::Cow::Borrowed( $msg ),
                )*
            ];
            let expecteds_len = expecteds.len();
            let expecteds = std::collections::BTreeSet::from(expecteds);

            let missing_errs = expecteds.difference(&actuals).map(|missing| {
                format!("Expected diagnostic `{}` was not emitted.", missing)
            });
            let extra_errs = actuals.difference(&expecteds).map(|extra| {
                format!("Unexpected extra diagnostic `{}` was emitted", extra)
            });
            let all_errs: Vec<_> = missing_errs.chain(extra_errs).collect();
            if !all_errs.is_empty() {
                panic!("{}", all_errs.join("\n"));
            }

            // TODO(mingwei): fix duplicates generated from multi-pass flow prop algorithm.
            // assert_eq!(actuals_len, expecteds_len, "Wrong nubmer of diagnostics, were there duplicates?");

            __hf
        }
    };
}

/// Test helper, emits and checks snapshots for the mermaid and dot graphs.
#[doc(hidden)]
#[macro_export]
macro_rules! assert_graphvis_snapshots {
    ($df:ident) => {
        {
            #[cfg(not(target_arch = "wasm32"))]
            {
                insta::with_settings!({snapshot_suffix => "graphvis_mermaid"}, {
                    insta::assert_snapshot!($df.meta_graph().unwrap().to_mermaid(&Default::default()));
                });
                insta::with_settings!({snapshot_suffix => "graphvis_dot"}, {
                    insta::assert_snapshot!($df.meta_graph().unwrap().to_dot(&Default::default()));
                });
            }
        }
    }
}

#[doc(hidden)]
#[macro_export]
#[cfg(feature = "python")]
macro_rules! __python_feature_gate {
    (
        {
            $( $ypy:tt )*
        },
        {
            $( $npy:tt )*
        }
    ) => {
        $( $ypy )*
    };
}

#[doc(hidden)]
#[macro_export]
#[cfg(not(feature = "python"))]
macro_rules! __python_feature_gate {
    (
        {
            $( $ypy:tt )*
        },
        {
            $( $npy:tt )*
        }
    ) => {
        $( $npy )*
    };
}