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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
use crate::interner::ChalkIr;
use chalk_parse::ast::{Identifier, Kind};
use chalk_solve::coherence::CoherenceError;
use chalk_solve::wf::WfError;
use string_cache::DefaultAtom as Atom;

/// Wrapper type for the various errors that can occur during chalk
/// processing.
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct ChalkError {
    /// For now, we just convert the error into a string, which makes
    /// it trivially hashable etc.
    error_text: String,
}

impl From<Box<dyn std::error::Error>> for ChalkError {
    fn from(value: Box<dyn std::error::Error>) -> Self {
        ChalkError {
            error_text: value.to_string(),
        }
    }
}

impl From<WfError<ChalkIr>> for ChalkError {
    fn from(value: WfError<ChalkIr>) -> Self {
        ChalkError {
            error_text: value.to_string(),
        }
    }
}

impl From<CoherenceError<ChalkIr>> for ChalkError {
    fn from(value: CoherenceError<ChalkIr>) -> Self {
        ChalkError {
            error_text: value.to_string(),
        }
    }
}

impl From<RustIrError> for ChalkError {
    fn from(value: RustIrError) -> Self {
        ChalkError {
            error_text: value.to_string(),
        }
    }
}

impl std::fmt::Display for ChalkError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.error_text)
    }
}

impl std::error::Error for ChalkError {}

#[derive(Debug)]
pub enum RustIrError {
    InvalidParameterName(Identifier),
    InvalidTraitName(Identifier),
    NotTrait(Identifier),
    NotStruct(Identifier),
    DuplicateOrShadowedParameters,
    AutoTraitAssociatedTypes(Identifier),
    AutoTraitParameters(Identifier),
    AutoTraitWhereClauses(Identifier),
    InvalidFundamentalTypesParameters(Identifier),
    NegativeImplAssociatedValues(Identifier),
    MissingAssociatedType(Identifier),
    IncorrectNumberOfVarianceParameters {
        identifier: Identifier,
        expected: usize,
        actual: usize,
    },
    IncorrectNumberOfTypeParameters {
        identifier: Identifier,
        expected: usize,
        actual: usize,
    },
    IncorrectNumberOfAssociatedTypeParameters {
        identifier: Identifier,
        expected: usize,
        actual: usize,
    },
    IncorrectParameterKind {
        identifier: Identifier,
        expected: Kind,
        actual: Kind,
    },
    IncorrectTraitParameterKind {
        identifier: Identifier,
        expected: Kind,
        actual: Kind,
    },
    IncorrectAssociatedTypeParameterKind {
        identifier: Identifier,
        expected: Kind,
        actual: Kind,
    },
    CannotApplyTypeParameter(Identifier),
    InvalidExternAbi(Atom),
}

impl std::fmt::Display for RustIrError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            RustIrError::InvalidParameterName(name) => {
                write!(f, "invalid parameter name `{}`", name)
            }
            RustIrError::InvalidTraitName(name) => write!(f, "invalid trait name `{}`", name),
            RustIrError::NotTrait(name) => write!(
                f,
                "expected a trait, found `{}`, which is not a trait",
                name
            ),
            RustIrError::NotStruct(name) => write!(
                f,
                "expected a struct, found `{}`, which is not a struct",
                name
            ),
            RustIrError::DuplicateOrShadowedParameters => {
                write!(f, "duplicate or shadowed parameters")
            }
            RustIrError::AutoTraitAssociatedTypes(name) => {
                write!(f, "auto trait `{}` cannot define associated types", name)
            }
            RustIrError::AutoTraitParameters(name) => {
                write!(f, "auto trait `{}` cannot have parameters", name)
            }
            RustIrError::AutoTraitWhereClauses(name) => {
                write!(f, "auto trait `{}` cannot have where clauses", name)
            }
            RustIrError::InvalidFundamentalTypesParameters(name) => write!(
                f,
                "only a single parameter supported for fundamental type `{}`",
                name
            ),
            RustIrError::NegativeImplAssociatedValues(name) => write!(
                f,
                "negative impl for trait `{}` cannot define associated values",
                name
            ),
            RustIrError::MissingAssociatedType(name) => {
                write!(f, "no associated type `{}` defined in trait", name)
            }
            RustIrError::IncorrectNumberOfVarianceParameters {
                identifier,
                expected,
                actual,
            } => write!(
                f,
                "`{}` has {} type parameters, not {}, which were passed for variance",
                identifier, expected, actual
            ),
            RustIrError::IncorrectNumberOfTypeParameters {
                identifier,
                expected,
                actual,
            } => write!(
                f,
                "`{}` takes {} type parameters, not {}",
                identifier, expected, actual
            ),
            RustIrError::IncorrectNumberOfAssociatedTypeParameters {
                identifier,
                expected,
                actual,
            } => write!(
                f,
                "wrong number of parameters for associated type `{}` (expected {}, got {})",
                identifier, expected, actual
            ),
            RustIrError::IncorrectParameterKind {
                identifier,
                expected,
                actual,
            } => write!(
                f,
                "incorrect parameter kind for `{}`: expected {}, found {}",
                identifier, expected, actual
            ),
            RustIrError::IncorrectTraitParameterKind {
                identifier,
                expected,
                actual,
            } => write!(
                f,
                "incorrect parameter kind for trait `{}`: expected {}, found {}",
                identifier, expected, actual
            ),
            RustIrError::IncorrectAssociatedTypeParameterKind {
                identifier,
                expected,
                actual,
            } => write!(
                f,
                "incorrect associated type parameter kind for `{}`: expected {}, found {}",
                identifier, expected, actual
            ),
            RustIrError::CannotApplyTypeParameter(name) => {
                write!(f, "cannot apply type parameter `{}`", name)
            }
            RustIrError::InvalidExternAbi(abi) => write!(f, "invalid extern ABI `{}`", abi),
        }
    }
}

impl std::error::Error for RustIrError {}