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 OptionalChainingOperatorCanNotBePartOfChain,
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}
129
130impl fmt::Display for ErrorKind {
131 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
132 let error_message = match self {
133 Self::ExpectedTupleType => "expected tuple type",
134 Self::NoAssociatedFunction(_, _) => "no associated function",
136 Self::UnknownMemberFunction(_) => "unknown member function",
137 Self::MissingMemberFunction(_, _) => "missing member function",
138 Self::WrongNumberOfArguments(_, _) => "wrong number of arguments",
139 Self::TooManyParameters { .. } => "too many parameters",
140 Self::CallsCanNotBePartOfChain => "calls cannot be part of a chain",
141 Self::ExpectedLambda => "expected a lambda",
142
143 Self::UnknownVariable => "unknown variable",
145 Self::UnknownIdentifier(_) => "unknown identifier",
146 Self::UnknownTypeReference => "unknown type",
147 Self::UnusedVariablesCanNotBeMut => "unused variable cannot be mutable",
148 Self::VariableTypeMustBeBlittable(_) => "variable type must be blittable",
149 Self::OverwriteVariableWithAnotherType => {
150 "cannot overwrite variable with a different type"
151 }
152 Self::IncompatibleTypes { .. } => "incompatible types",
153 Self::IncompatibleTypesForAssignment { .. } => "incompatible types for assignment",
154 Self::CouldNotCoerceTo(_) => "could not coerce to type",
155 Self::UnexpectedType => "unexpected type",
156 Self::SelfNotCorrectType => "'self' is not the correct type",
157 Self::SelfNotCorrectMutableState => "'self' has incorrect mutable state",
158 Self::NotAllowedAsReturnType(_) => "not an allowed return type",
159 Self::ParameterTypeCanNotBeStorage(_) => "parameter cannot be of storage type",
160
161 Self::ExpectedMutableLocation => "expected a mutable location",
163 Self::CanOnlyOverwriteVariableWithMut => "can only overwrite mutable variables",
164 Self::OverwriteVariableNotAllowedHere => "overwriting variables is not allowed here",
165 Self::VariableIsNotMutable => "variable is not mutable",
166 Self::ArgumentIsNotMutable => "argument is not mutable",
167 Self::ParameterIsNotMutable => "parameter is not mutable",
168 Self::KeyVariableNotAllowedToBeMutable => "key variable cannot be mutable",
169
170 Self::UnknownStructTypeReference => "unknown struct type",
172 Self::DuplicateFieldName => "duplicate field name",
173 Self::MissingFieldInStructInstantiation(_, _) => {
174 "missing field in struct instantiation"
175 }
176 Self::UnknownStructField => "unknown struct field",
177 Self::UnknownField => "unknown field",
178 Self::DuplicateFieldInStructInstantiation(_) => {
179 "duplicate field in struct instantiation"
180 }
181 Self::UnknownEnumVariantType => "unknown enum variant",
182 Self::UnknownEnumVariantTypeInPattern => "unknown enum variant in pattern",
183 Self::ExpectedEnumInPattern => "expected an enum in pattern",
184 Self::WrongEnumVariantContainer(_) => "wrong enum variant container",
185 Self::EnumVariantHasNoFields => "enum variant has no fields",
186 Self::UnknownEnumType => "unknown enum type",
187
188 Self::ExpectedOptional => "expected optional",
190 Self::NoneNeedsExpectedTypeHint => "none requires a type hint",
191 Self::UnwrapCanNotBePartOfChain => "unwrap cannot be part of a chain",
192 Self::NoneCoalesceCanNotBePartOfChain => "none coalesce cannot be part of a chain",
193 Self::OptionalChainingOperatorCanNotBePartOfChain => {
194 "optional chaining cannot be part of a chain"
195 }
196 Self::CanNotNoneCoalesce => "cannot none-coalesce",
197
198 Self::GuardCanNotHaveMultipleWildcards => "guard cannot have multiple wildcards",
200 Self::WildcardMustBeLastInGuard => "wildcard must be last in guard",
201 Self::GuardMustHaveWildcard => "guard must have a wildcard",
202 Self::GuardHasNoType => "guard has no type",
203 Self::TooManyDestructureVariables => "too many destructure variables",
204 Self::CanNotDestructure => "cannot destructure",
205 Self::ExpressionsNotAllowedInLetPattern => "expressions not allowed in let patterns",
206 Self::EmptyMatch => "match statement is empty",
207 Self::MatchArmsMustHaveTypes => "match arms must have types",
208 Self::MatchMustHaveAtLeastOneArm => "match must have at least one arm",
209
210 Self::MissingSubscriptMember => "missing subscript member",
212 Self::ArrayIndexMustBeInt(_) => "array index must be an integer",
213 Self::MapKeyTypeMismatch { .. } => "map key type mismatch",
214 Self::MapValueTypeMismatch { .. } => "map value type mismatch",
215 Self::TooManyTupleFields { .. } => "too many tuple fields",
216 Self::ExpectedSlice => "expected a slice",
217 Self::CapacityNotEnough { .. } => "capacity not enough",
218 Self::ExpectedInitializerTarget { .. } => "expected initializer target",
219 Self::NoInferredTypeForEmptyInitializer => "cannot infer type for empty initializer",
220 Self::TooManyInitializerListElementsForStorage { .. } => {
221 "too many elements for storage"
222 }
223
224 Self::BreakOutsideLoop => "break outside of a loop",
226 Self::ContinueOutsideLoop => "continue outside of a loop",
227 Self::ReturnOutsideCompare => "return outside of a compare",
228
229 Self::IntConversionError(_) => "integer conversion error",
231 Self::FloatConversionError(_) => "float conversion error",
232 Self::BoolConversionError => "boolean conversion error",
233 Self::ExpectedBooleanExpression => "expected a boolean expression",
234 Self::OperatorProblem => "operator problem",
235 Self::MissingToString(_) => "missing to_string implementation",
236
237 Self::SemanticError(e) => return write!(f, "{e:?}"),
239 Self::NotAnIterator => "not an iterator",
240 Self::NoDefaultImplemented(_) => "no default implementation",
241 Self::UnknownConstant => "unknown constant",
242 Self::NotValidLocationStartingPoint => "not a valid location starting point",
243 Self::UnknownSymbol => "unknown symbol",
244 Self::UnknownModule => "unknown module",
245 Self::CanNotAttachFunctionsToType => "cannot attach functions to this type",
246 Self::NeedStorage => "storage needed",
247 };
248 f.write_str(error_message)
249 }
250}
251
252impl From<SemanticError> for Error {
253 fn from(value: SemanticError) -> Self {
254 Self {
255 node: Node::default(),
256 kind: ErrorKind::SemanticError(value),
257 }
258 }
259}