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