leo_ast/interpreter_value/
util.rs

1// Copyright (C) 2019-2025 Provable Inc.
2// This file is part of the Leo library.
3
4// The Leo library is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8
9// The Leo library is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13
14// You should have received a copy of the GNU General Public License
15// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
16
17use leo_errors::{InterpreterHalt, Result};
18use leo_span::Span;
19
20#[macro_export]
21macro_rules! tc_fail {
22    () => {
23        panic!("type checker failure")
24    };
25}
26
27#[macro_export]
28macro_rules! halt_no_span {
29    ($($x:tt)*) => {
30        return Err(InterpreterHalt::new(format!($($x)*)).into())
31    }
32}
33
34#[macro_export]
35macro_rules! halt {
36    ($span: expr) => {
37        return Err(InterpreterHalt::new_spanned(String::new(), $span).into())
38
39    };
40
41    ($span: expr, $($x:tt)*) => {
42        return Err(InterpreterHalt::new_spanned(format!($($x)*), $span).into())
43    };
44}
45
46pub trait ExpectTc {
47    type T;
48    fn expect_tc(self, span: Span) -> Result<Self::T>;
49}
50
51impl<T> ExpectTc for Option<T> {
52    type T = T;
53
54    fn expect_tc(self, span: Span) -> Result<Self::T> {
55        match self {
56            Some(t) => Ok(t),
57            None => Err(InterpreterHalt::new_spanned("type failure".into(), span).into()),
58        }
59    }
60}
61
62impl<T, U: std::fmt::Debug> ExpectTc for Result<T, U> {
63    type T = T;
64
65    fn expect_tc(self, span: Span) -> Result<Self::T> {
66        self.map_err(|_e| InterpreterHalt::new_spanned("type failure".into(), span).into())
67    }
68}