spade_typeinference/
testutil.rs

1use spade_common::location_info::WithLocation;
2use spade_common::num_ext::InfallibleToBigInt;
3use spade_hir::symbol_table::SymbolTable;
4use spade_types::KnownType;
5
6use crate::fixed_types::t_int;
7use crate::TypeVar as TVar;
8
9#[cfg(test)]
10use crate::equation::TraitList;
11#[cfg(test)]
12use spade_types::meta_types::MetaType;
13
14pub fn sized_int(size: u128, symtab: &SymbolTable) -> TVar {
15    TVar::Known(
16        ().nowhere(),
17        t_int(symtab),
18        vec![TVar::Known(
19            ().nowhere(),
20            KnownType::Integer(size.to_bigint()),
21            vec![],
22        )],
23    )
24}
25
26#[cfg(test)]
27pub fn unsized_int(id: u64, symtab: &SymbolTable) -> TVar {
28    TVar::Known(
29        ().nowhere(),
30        t_int(symtab),
31        vec![TVar::Unknown(
32            ().nowhere(),
33            id,
34            TraitList::empty(),
35            MetaType::Uint,
36        )],
37    )
38}
39
40#[macro_export]
41macro_rules! get_type {
42    ($state:ident, $e:expr) => {
43        if let Ok(t) = $state.type_of($e) {
44            t
45        } else {
46            println!("{}", format_trace_stack(&$state));
47            panic!("Failed to get type of {:?}", $e)
48        }
49    };
50}
51
52#[macro_export]
53macro_rules! ensure_same_type {
54    ($state:ident, $t1:expr, $t2:expr) => {
55        // These let bindings are required for the values returned by
56        // get_type to live long enough
57        let t1 = $t1;
58        let t2 = $t2;
59        let _t1 = t1.get_type(&$state);
60        let _t2 = t2.get_type(&$state);
61        if _t1 != _t2 {
62            println!("{}", format_trace_stack(&$state));
63            $state.print_equations();
64
65            if let (Ok(t1), Ok(t2)) = (&_t1, &_t2) {
66                println!("Types were OK and have values {}, {}", t1, t2);
67                println!("Raw: {:?}, {:?}", t1, t2);
68            } else {
69                println!("{:?}\n!=\n{:?}", _t1, _t2);
70            }
71            panic!("Types are not the same")
72        }
73    };
74}
75
76/// Shorthand macro for constructing TypeVar::Known
77#[macro_export]
78macro_rules! kvar {
79    ($base:expr $(; ( $( $params:expr ),* ) )? ) => {
80        TypeVar::Known(().nowhere(), $base, vec![ $( $($params),* )? ])
81    }
82}