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_fail2 {
22    () => {
23        panic!("type checker failure")
24    };
25}
26
27#[macro_export]
28macro_rules! halt_no_span2 {
29    ($($x:tt)*) => {
30        return Err(InterpreterHalt::new(format!($($x)*)).into())
31    }
32}
33
34#[macro_export]
35macro_rules! halt2 {
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
46#[macro_export]
47macro_rules! fail2 {
48    ($span: expr) => {
49        InterpreterHalt::new_spanned(String::new(), $span).into()
50
51    };
52
53    ($span: expr, $($x:tt)*) => {
54        InterpreterHalt::new_spanned(format!($($x)*), $span).into()
55    };
56}
57
58pub trait ExpectTc {
59    type T;
60    fn expect_tc(self, span: Span) -> Result<Self::T>;
61}
62
63impl<T> ExpectTc for Option<T> {
64    type T = T;
65
66    fn expect_tc(self, span: Span) -> Result<Self::T> {
67        match self {
68            Some(t) => Ok(t),
69            None => Err(InterpreterHalt::new_spanned("type failure".into(), span).into()),
70        }
71    }
72}
73
74impl<T, U: std::fmt::Debug> ExpectTc for Result<T, U> {
75    type T = T;
76
77    fn expect_tc(self, span: Span) -> Result<Self::T> {
78        self.map_err(|_e| InterpreterHalt::new_spanned("type failure".into(), span).into())
79    }
80}