leo_errors/errors/flattener/
flattener_errors.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 crate::create_messages;
18use std::fmt::{Debug, Display};
19
20/// Generates the type name of a value.
21pub fn type_name<T>(_: &T) -> &'static str {
22    std::any::type_name::<T>()
23}
24
25create_messages!(
26    /// CliError enum that represents all the errors for the `leo-lang` crate.
27    FlattenError,
28    code_mask: 3000i32,
29    code_prefix: "FLA",
30
31    /// For when a constant operation would cause an overflow.
32    @formatted
33    binary_overflow {
34        args: (left: impl Display, op: impl Display, right: impl Display, right_type: impl Display),
35        msg: format!("The const operation `{left}{} {op} {right}{right_type}` causes an overflow.", type_name(&left)),
36        help: None,
37    }
38
39    /// For when a constant operation would cause an overflow.
40    @formatted
41    unary_overflow {
42        args: (left: impl Display, op: impl Display),
43        msg: format!("The const operation `{left}{} {op}` causes an overflow.", type_name(&left)),
44        help: None,
45    }
46
47    /// For when a loop uses a negative value.
48    @formatted
49    loop_has_neg_value {
50        args: (value: impl Display),
51        msg: format!(
52            "The loop has a negative loop bound `{value}`.",
53        ),
54        help: None,
55    }
56
57    /// For when a u128 value cannot be converted into an i128.
58    @formatted
59    u128_to_i128 {
60        args: (value: impl Display),
61        msg: format!(
62            "The value `{value}` cannot be converted into an i128.",
63        ),
64        help: None,
65    }
66);