1use crate::SemanticError;
6use source_map_node::Node;
7use std::fmt;
8use std::num::{ParseFloatError, ParseIntError};
9use swamp_types::prelude::*;
10
11#[derive(Clone, Debug)]
12pub struct Error {
13 pub node: Node,
14 pub kind: ErrorKind,
15}
16#[derive(Clone, Debug)]
17pub enum ErrorKind {
18 NoAssociatedFunction(TypeRef, String),
19 MissingSubscriptMember,
20 UnusedVariablesCanNotBeMut,
21 VariableTypeMustBeBlittable(TypeRef),
22 GuardCanNotHaveMultipleWildcards,
23 WildcardMustBeLastInGuard,
24 GuardMustHaveWildcard,
25 GuardHasNoType,
26 TooManyDestructureVariables,
27 CanNotDestructure,
28 UnknownStructTypeReference,
29 DuplicateFieldName,
30 MissingFieldInStructInstantiation(Vec<String>, AnonymousStructType),
31 UnknownVariable,
32 ArrayIndexMustBeInt(TypeRef),
33 OverwriteVariableWithAnotherType,
34 NoneNeedsExpectedTypeHint,
35 ExpectedMutableLocation,
36 WrongNumberOfArguments(usize, usize),
37 CanOnlyOverwriteVariableWithMut,
38 OverwriteVariableNotAllowedHere,
39 UnknownEnumVariantType,
40 UnknownStructField,
41 UnknownEnumVariantTypeInPattern,
42 ExpectedEnumInPattern,
43 WrongEnumVariantContainer(EnumVariantType),
44 VariableIsNotMutable,
45 ArgumentIsNotMutable,
46 UnknownTypeReference,
47 SemanticError(SemanticError),
48 ExpectedOptional,
49 MapKeyTypeMismatch {
50 expected: TypeRef,
51 found: TypeRef,
52 },
53 MapValueTypeMismatch {
54 expected: TypeRef,
55 found: TypeRef,
56 },
57 IncompatibleTypes {
58 expected: TypeRef,
59 found: TypeRef,
60 },
61 UnknownMemberFunction(TypeRef),
62 ExpressionsNotAllowedInLetPattern,
63 UnknownField,
64 EnumVariantHasNoFields,
65 ExpectedTupleType,
66 TooManyTupleFields {
67 max: usize,
68 got: usize,
69 },
70 ExpectedBooleanExpression,
71 NotAnIterator,
72 IntConversionError(ParseIntError),
73 FloatConversionError(ParseFloatError),
74 BoolConversionError,
75 DuplicateFieldInStructInstantiation(String),
76 UnknownIdentifier(String),
77 NoDefaultImplemented(TypeRef),
78 UnknownConstant,
79 NotValidLocationStartingPoint,
80 CallsCanNotBePartOfChain,
81 UnwrapCanNotBePartOfChain,
82 NoneCoalesceCanNotBePartOfChain,
83 InvalidOperatorAfterOptionalChaining,
84 SelfNotCorrectType,
85 CanNotNoneCoalesce,
86 UnknownSymbol,
87 UnknownEnumType,
88 UnknownModule,
89 BreakOutsideLoop,
90 ReturnOutsideCompare,
91 EmptyMatch,
92 MatchArmsMustHaveTypes,
93 ContinueOutsideLoop,
94 ParameterIsNotMutable,
95 CouldNotCoerceTo(TypeRef),
96 UnexpectedType,
97 CanNotAttachFunctionsToType,
98 MissingMemberFunction(String, TypeRef),
99 ExpectedLambda,
100 ExpectedSlice,
101 MissingToString(TypeRef),
102 IncompatibleTypesForAssignment {
103 expected: TypeRef,
104 found: TypeRef,
105 },
106 CapacityNotEnough {
107 size_requested: usize,
108 capacity: usize,
109 },
110 ExpectedInitializerTarget {
111 destination_type: TypeRef,
112 },
113 NoInferredTypeForEmptyInitializer,
114 TooManyInitializerListElementsForStorage {
115 capacity: usize,
116 },
117 KeyVariableNotAllowedToBeMutable,
118 SelfNotCorrectMutableState,
119 NotAllowedAsReturnType(TypeRef),
120 ParameterTypeCanNotBeStorage(TypeRef),
121 OperatorProblem,
122 MatchMustHaveAtLeastOneArm,
123 NeedStorage,
124 TooManyParameters {
125 encountered: usize,
126 allowed: usize,
127 },
128 CanOnlyHaveFunctionCallAtStartOfPostfixChain,
129 CanNotSubscriptWithThatType,
130}
131
132impl fmt::Display for ErrorKind {
133 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
134 let error_message = match self {
135 Self::ExpectedTupleType => "expected tuple type",
136 Self::CanOnlyHaveFunctionCallAtStartOfPostfixChain => {
137 "function calls only allowed at start of chain"
138 }
139 Self::CanNotSubscriptWithThatType => {
140 "subscript not possible with that type"
141 }
142 Self::NoAssociatedFunction(_, _) => "no associated function",
144 Self::UnknownMemberFunction(_) => "unknown member function",
145 Self::MissingMemberFunction(_, _) => "missing member function",
146 Self::WrongNumberOfArguments(_, _) => "wrong number of arguments",
147 Self::TooManyParameters { .. } => "too many parameters",
148 Self::CallsCanNotBePartOfChain => "calls cannot be part of a chain",
149 Self::ExpectedLambda => "expected a lambda",
150
151 Self::UnknownVariable => "unknown variable",
153 Self::UnknownIdentifier(_) => "unknown identifier",
154 Self::UnknownTypeReference => "unknown type",
155 Self::UnusedVariablesCanNotBeMut => "unused variable cannot be mutable",
156 Self::VariableTypeMustBeBlittable(_) => "variable type must be blittable",
157 Self::OverwriteVariableWithAnotherType => {
158 "cannot overwrite variable with a different type"
159 }
160 Self::IncompatibleTypes { .. } => "incompatible types",
161 Self::IncompatibleTypesForAssignment { .. } => "incompatible types for assignment",
162 Self::CouldNotCoerceTo(_) => "could not coerce to type",
163 Self::UnexpectedType => "unexpected type",
164 Self::SelfNotCorrectType => "'self' is not the correct type",
165 Self::SelfNotCorrectMutableState => "'self' has incorrect mutable state",
166 Self::NotAllowedAsReturnType(_) => "not an allowed return type",
167 Self::ParameterTypeCanNotBeStorage(_) => "parameter cannot be of storage type",
168
169 Self::ExpectedMutableLocation => "expected a mutable location",
171 Self::CanOnlyOverwriteVariableWithMut => "can only overwrite mutable variables",
172 Self::OverwriteVariableNotAllowedHere => "overwriting variables is not allowed here",
173 Self::VariableIsNotMutable => "variable is not mutable",
174 Self::ArgumentIsNotMutable => "argument is not mutable",
175 Self::ParameterIsNotMutable => "parameter is not mutable",
176 Self::KeyVariableNotAllowedToBeMutable => "key variable cannot be mutable",
177
178 Self::UnknownStructTypeReference => "unknown struct type",
180 Self::DuplicateFieldName => "duplicate field name",
181 Self::MissingFieldInStructInstantiation(_, _) => {
182 "missing field in struct instantiation"
183 }
184 Self::UnknownStructField => "unknown struct field",
185 Self::UnknownField => "unknown field",
186 Self::DuplicateFieldInStructInstantiation(_) => {
187 "duplicate field in struct instantiation"
188 }
189 Self::UnknownEnumVariantType => "unknown enum variant",
190 Self::UnknownEnumVariantTypeInPattern => "unknown enum variant in pattern",
191 Self::ExpectedEnumInPattern => "expected an enum in pattern",
192 Self::WrongEnumVariantContainer(_) => "wrong enum variant container",
193 Self::EnumVariantHasNoFields => "enum variant has no fields",
194 Self::UnknownEnumType => "unknown enum type",
195
196 Self::ExpectedOptional => "expected optional",
198 Self::NoneNeedsExpectedTypeHint => "none requires a type hint",
199 Self::UnwrapCanNotBePartOfChain => "unwrap cannot be part of a chain",
200 Self::NoneCoalesceCanNotBePartOfChain => "none coalesce cannot be part of a chain",
201 Self::InvalidOperatorAfterOptionalChaining => {
202 "invalid operator after optional chaining (?)"
203 }
204 Self::CanNotNoneCoalesce => "cannot none-coalesce",
205
206 Self::GuardCanNotHaveMultipleWildcards => "guard cannot have multiple wildcards",
208 Self::WildcardMustBeLastInGuard => "wildcard must be last in guard",
209 Self::GuardMustHaveWildcard => "guard must have a wildcard",
210 Self::GuardHasNoType => "guard has no type",
211 Self::TooManyDestructureVariables => "too many destructure variables",
212 Self::CanNotDestructure => "cannot destructure",
213 Self::ExpressionsNotAllowedInLetPattern => "expressions not allowed in let patterns",
214 Self::EmptyMatch => "match statement is empty",
215 Self::MatchArmsMustHaveTypes => "match arms must have types",
216 Self::MatchMustHaveAtLeastOneArm => "match must have at least one arm",
217
218 Self::MissingSubscriptMember => "missing subscript member",
220 Self::ArrayIndexMustBeInt(_) => "array index must be an integer",
221 Self::MapKeyTypeMismatch { .. } => "map key type mismatch",
222 Self::MapValueTypeMismatch { .. } => "map value type mismatch",
223 Self::TooManyTupleFields { .. } => "too many tuple fields",
224 Self::ExpectedSlice => "expected a slice",
225 Self::CapacityNotEnough { .. } => "capacity not enough",
226 Self::ExpectedInitializerTarget { .. } => "expected initializer target",
227 Self::NoInferredTypeForEmptyInitializer => "cannot infer type for empty initializer",
228 Self::TooManyInitializerListElementsForStorage { .. } => {
229 "too many elements for storage"
230 }
231
232 Self::BreakOutsideLoop => "break outside of a loop",
234 Self::ContinueOutsideLoop => "continue outside of a loop",
235 Self::ReturnOutsideCompare => "return outside of a compare",
236
237 Self::IntConversionError(_) => "integer conversion error",
239 Self::FloatConversionError(_) => "float conversion error",
240 Self::BoolConversionError => "boolean conversion error",
241 Self::ExpectedBooleanExpression => "expected a boolean expression",
242 Self::OperatorProblem => "operator problem",
243 Self::MissingToString(_) => "missing to_string implementation",
244
245 Self::SemanticError(e) => return write!(f, "{e:?}"),
247 Self::NotAnIterator => "not an iterator",
248 Self::NoDefaultImplemented(_) => "no default implementation",
249 Self::UnknownConstant => "unknown constant",
250 Self::NotValidLocationStartingPoint => "not a valid location starting point",
251 Self::UnknownSymbol => "unknown symbol",
252 Self::UnknownModule => "unknown module",
253 Self::CanNotAttachFunctionsToType => "cannot attach functions to this type",
254 Self::NeedStorage => "storage needed",
255 };
256 f.write_str(error_message)
257 }
258}
259
260
261impl ErrorKind {
262 #[must_use] pub const fn code(&self) -> usize {
263 match self {
264 Self::NoAssociatedFunction(_, _) => 1,
265 Self::MissingSubscriptMember => 2,
266 Self::UnusedVariablesCanNotBeMut => 3,
267 Self::VariableTypeMustBeBlittable(_) => 4,
268 Self::GuardCanNotHaveMultipleWildcards => 5,
269 Self::WildcardMustBeLastInGuard => 6,
270 Self::GuardMustHaveWildcard => 7,
271 Self::GuardHasNoType => 8,
272 Self::TooManyDestructureVariables => 9,
273 Self::CanNotDestructure => 10,
274 Self::UnknownStructTypeReference => 11,
275 Self::DuplicateFieldName => 12,
276 Self::MissingFieldInStructInstantiation(_, _) => 13,
277 Self::UnknownVariable => 14,
278 Self::ArrayIndexMustBeInt(_) => 15,
279 Self::OverwriteVariableWithAnotherType => 16,
280 Self::NoneNeedsExpectedTypeHint => 17,
281 Self::ExpectedMutableLocation => 18,
282 Self::WrongNumberOfArguments(_, _) => 19,
283 Self::CanOnlyOverwriteVariableWithMut => 20,
284 Self::OverwriteVariableNotAllowedHere => 21,
285 Self::UnknownEnumVariantType => 22,
286 Self::UnknownStructField => 23,
287 Self::UnknownEnumVariantTypeInPattern => 24,
288 Self::ExpectedEnumInPattern => 25,
289 Self::WrongEnumVariantContainer(_) => 26,
290 Self::VariableIsNotMutable => 27,
291 Self::ArgumentIsNotMutable => 28,
292 Self::UnknownTypeReference => 29,
293 Self::SemanticError(_) => 30,
294 Self::ExpectedOptional => 31,
295 Self::MapKeyTypeMismatch { .. } => 32,
296 Self::MapValueTypeMismatch { .. } => 33,
297 Self::IncompatibleTypes { .. } => 34,
298 Self::UnknownMemberFunction(_) => 35,
299 Self::ExpressionsNotAllowedInLetPattern => 36,
300 Self::UnknownField => 37,
301 Self::EnumVariantHasNoFields => 38,
302 Self::ExpectedTupleType => 39,
303 Self::TooManyTupleFields { .. } => 40,
304 Self::ExpectedBooleanExpression => 41,
305 Self::NotAnIterator => 42,
306 Self::IntConversionError(_) => 43,
307 Self::FloatConversionError(_) => 44,
308 Self::BoolConversionError => 45,
309 Self::DuplicateFieldInStructInstantiation(_) => 46,
310 Self::UnknownIdentifier(_) => 47,
311 Self::NoDefaultImplemented(_) => 48,
312 Self::UnknownConstant => 49,
313 Self::NotValidLocationStartingPoint => 50,
314 Self::CallsCanNotBePartOfChain => 51,
315 Self::UnwrapCanNotBePartOfChain => 52,
316 Self::NoneCoalesceCanNotBePartOfChain => 53,
317 Self::InvalidOperatorAfterOptionalChaining => 54,
318 Self::SelfNotCorrectType => 55,
319 Self::CanNotNoneCoalesce => 56,
320 Self::UnknownSymbol => 57,
321 Self::UnknownEnumType => 58,
322 Self::UnknownModule => 59,
323 Self::BreakOutsideLoop => 60,
324 Self::ReturnOutsideCompare => 61,
325 Self::EmptyMatch => 62,
326 Self::MatchArmsMustHaveTypes => 63,
327 Self::ContinueOutsideLoop => 64,
328 Self::ParameterIsNotMutable => 65,
329 Self::CouldNotCoerceTo(_) => 66,
330 Self::UnexpectedType => 67,
331 Self::CanNotAttachFunctionsToType => 68,
332 Self::MissingMemberFunction(_, _) => 69,
333 Self::ExpectedLambda => 70,
334 Self::ExpectedSlice => 71,
335 Self::MissingToString(_) => 72,
336 Self::IncompatibleTypesForAssignment { .. } => 73,
337 Self::CapacityNotEnough { .. } => 74,
338 Self::ExpectedInitializerTarget { .. } => 75,
339 Self::NoInferredTypeForEmptyInitializer => 76,
340 Self::TooManyInitializerListElementsForStorage { .. } => 77,
341 Self::KeyVariableNotAllowedToBeMutable => 78,
342 Self::SelfNotCorrectMutableState => 79,
343 Self::NotAllowedAsReturnType(_) => 80,
344 Self::ParameterTypeCanNotBeStorage(_) => 81,
345 Self::OperatorProblem => 82,
346 Self::MatchMustHaveAtLeastOneArm => 83,
347 Self::NeedStorage => 84,
348 Self::TooManyParameters { .. } => 85,
349 Self::CanOnlyHaveFunctionCallAtStartOfPostfixChain => 86,
350 Self::CanNotSubscriptWithThatType => 87,
351 }
352 }
353}
354
355
356impl From<SemanticError> for Error {
357 fn from(value: SemanticError) -> Self {
358 Self {
359 node: Node::default(),
360 kind: ErrorKind::SemanticError(value),
361 }
362 }
363}