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
use std::{
    error,
    fmt::{self, Display, Formatter},
};

/// A Melior error.
#[derive(Debug, Eq, PartialEq)]
pub enum Error {
    BlockArgumentExpected(String),
    BlockArgumentPosition(String, usize),
    FunctionExpected(String),
    FunctionInputPosition(String, usize),
    FunctionResultPosition(String, usize),
    InvokeFunction,
    MemRefExpected(String),
    OperationResultExpected(String),
    OperationResultPosition(String, usize),
    ParsePassPipeline(String),
    RunPass,
    TupleExpected(String),
    TupleFieldPosition(String, usize),
    NamedAttributeParse(String),
}

impl Display for Error {
    fn fmt(&self, formatter: &mut Formatter) -> fmt::Result {
        match self {
            Self::BlockArgumentExpected(value) => {
                write!(formatter, "block argument expected: {value}")
            }
            Self::BlockArgumentPosition(block, position) => {
                write!(
                    formatter,
                    "block argument position {position} out of range: {block}"
                )
            }
            Self::FunctionExpected(r#type) => write!(formatter, "function expected: {type}"),
            Self::FunctionInputPosition(r#type, position) => write!(
                formatter,
                "function input position {position} out of range: {type}"
            ),
            Self::FunctionResultPosition(r#type, position) => write!(
                formatter,
                "function result position {position} out of range: {type}"
            ),
            Self::InvokeFunction => write!(formatter, "failed to invoke JIT-compiled function"),
            Self::MemRefExpected(r#type) => write!(formatter, "mem-ref expected: {type}"),
            Self::OperationResultExpected(value) => {
                write!(formatter, "operation result expected: {value}")
            }
            Self::OperationResultPosition(operation, position) => {
                write!(
                    formatter,
                    "operation result position {position} out of range: {operation}"
                )
            }
            Self::ParsePassPipeline(error) => {
                write!(formatter, "failed to parse pass pipeline: {error}")
            }
            Self::RunPass => write!(formatter, "failed to run pass"),
            Self::TupleExpected(r#type) => write!(formatter, "tuple expected: {type}"),
            Self::TupleFieldPosition(r#type, position) => {
                write!(
                    formatter,
                    "tuple field position {position} out of range: {type}"
                )
            }
            Self::NamedAttributeParse(attribute) => {
                write!(formatter, "error parsing attribute: {attribute}")
            }
        }
    }
}

impl error::Error for Error {}