ruka_check/
errors.rs

1use super::*;
2
3use thiserror::Error;
4
5/// Semantic checking error produced from HIR validation.
6#[derive(Debug, Error, Clone, PartialEq, Eq)]
7pub enum CheckError {
8    /// Called function name could not be resolved.
9    #[error("unknown function `{name}`")]
10    UnknownFunction {
11        /// Function name that failed to resolve.
12        name: String,
13    },
14    /// Same extern function was declared more than once.
15    #[error(
16        "duplicate extern function declaration `{name}`; first declared at {first_decl}, redeclared at {redecl}"
17    )]
18    DuplicateExternFunction {
19        /// Extern function name.
20        name: String,
21        /// Span of the first declaration.
22        first_decl: SourceSpan,
23        /// Span of the duplicate declaration.
24        redecl: SourceSpan,
25    },
26    /// Callee expression was not an identifier.
27    #[error("call target must be a function identifier")]
28    InvalidCallTarget,
29    /// Function call argument count did not match the signature.
30    #[error("function `{function}` expected {expected} args, got {actual}")]
31    ArityMismatch {
32        /// Function name receiving the call.
33        function: String,
34        /// Number of expected arguments.
35        expected: usize,
36        /// Number of provided arguments.
37        actual: usize,
38    },
39    /// Unqualified enum variant constructor resolved to multiple enums.
40    #[error("ambiguous enum variant constructor `{variant}`; candidates: {candidates}")]
41    AmbiguousEnumVariantConstructor {
42        /// Variant name used in the call.
43        variant: String,
44        /// Rendered list of candidate enum variants.
45        candidates: String,
46    },
47    /// Call argument syntax was incompatible with the parameter mode.
48    #[error("invalid argument form for parameter `{param}` in function `{function}`: {reason}")]
49    InvalidArgForm {
50        /// Called function name.
51        function: String,
52        /// Parameter name that rejected the argument.
53        param: String,
54        /// Human-readable explanation of the mismatch.
55        reason: &'static str,
56    },
57    /// Referenced identifier was not found in scope.
58    #[error("unknown identifier `{name}`")]
59    UnknownIdentifier {
60        /// Identifier name that failed to resolve.
61        name: String,
62    },
63    /// Type expression form is not valid in runtime signatures.
64    #[error("unsupported type syntax in runtime signature")]
65    UnsupportedTypeExpr,
66    /// Function or extern return type used an ownership sigil.
67    #[error("return types must be owned and cannot use `&` or `@`")]
68    ReturnTypeOwnershipNotAllowed,
69    /// Type constructor name was not found.
70    #[error("unknown type constructor `{name}`")]
71    UnknownTypeConstructor {
72        /// Constructor name that failed to resolve.
73        name: String,
74    },
75    /// Struct declaration duplicated an existing type name.
76    #[error("duplicate struct declaration `{name}`")]
77    DuplicateStructDecl {
78        /// Struct name declared more than once.
79        name: String,
80    },
81    /// Enum declaration duplicated an existing type name.
82    #[error("duplicate enum declaration `{name}`")]
83    DuplicateEnumDecl {
84        /// Enum name declared more than once.
85        name: String,
86    },
87    /// Struct declaration repeated a generic type parameter.
88    #[error("struct `{name}` has duplicate type parameter `{param}`")]
89    DuplicateStructTypeParam {
90        /// Struct name.
91        name: String,
92        /// Duplicate type parameter name.
93        param: String,
94    },
95    /// Struct declaration repeated a field name.
96    #[error("struct `{name}` has duplicate field `{field}`")]
97    DuplicateStructField {
98        /// Struct name.
99        name: String,
100        /// Duplicate field name.
101        field: String,
102    },
103    /// Enum declaration repeated a variant name.
104    #[error("enum `{name}` has duplicate variant `{variant}`")]
105    DuplicateEnumVariant {
106        /// Enum name.
107        name: String,
108        /// Duplicate variant name.
109        variant: String,
110    },
111    /// Generic type constructor was used without arguments.
112    #[error("type constructor `{name}` requires type arguments")]
113    MissingTypeArgs {
114        /// Type constructor name.
115        name: String,
116    },
117    /// Generic type constructor was called with wrong arity.
118    #[error("type constructor `{name}` expected {expected} args, got {actual}")]
119    TypeArgCountMismatch {
120        /// Type constructor name.
121        name: String,
122        /// Expected number of type arguments.
123        expected: usize,
124        /// Provided number of type arguments.
125        actual: usize,
126    },
127    /// Inferred and expected types were not compatible.
128    #[error("type mismatch: expected `{expected}`, found `{actual}`")]
129    TypeMismatch {
130        /// Expected type.
131        expected: Ty,
132        /// Actual inferred type.
133        actual: Ty,
134    },
135    /// `if` condition did not evaluate to `Bool`.
136    #[error("`if` condition must be `Bool`, found `{actual}`")]
137    IfConditionNotBool {
138        /// Actual condition type.
139        actual: Ty,
140    },
141    /// Pointer binding `if` condition was not a pointer.
142    #[error("`if` binding requires a pointer condition, found `{actual}`")]
143    IfBindingNotPointer {
144        /// Actual condition type.
145        actual: Ty,
146    },
147    /// Mutable pointer binding lacked a named source binding.
148    #[error("mutable `if` binding requires a named pointer binding")]
149    MutableIfBindingRequiresBinding,
150    /// `while` condition did not evaluate to `Bool`.
151    #[error("`while` condition must be `Bool`, found `{actual}`")]
152    WhileConditionNotBool {
153        /// Actual condition type.
154        actual: Ty,
155    },
156    /// `match` subject was not an enum value.
157    #[error("`match` subject must be an enum value, found `{actual}`")]
158    MatchSubjectNotEnum {
159        /// Actual subject type.
160        actual: Ty,
161    },
162    /// Match arm referenced an unknown enum variant.
163    #[error("unknown variant `{variant}` on enum `{enum_name}`")]
164    UnknownEnumVariant {
165        /// Enum name used in the match.
166        enum_name: String,
167        /// Variant name that failed to resolve.
168        variant: String,
169    },
170    /// Match contained duplicate variant arms.
171    #[error("duplicate `match` arm for `{enum_name}::{variant}`")]
172    DuplicateMatchVariant {
173        /// Enum name used in the match.
174        enum_name: String,
175        /// Duplicate variant name.
176        variant: String,
177    },
178    /// Match arm bound the wrong number of payload names.
179    #[error("`{enum_name}::{variant}` pattern expected {expected} binders, found {actual}")]
180    MatchVariantArityMismatch {
181        /// Enum name used in the arm.
182        enum_name: String,
183        /// Variant name used in the arm.
184        variant: String,
185        /// Expected payload binder count.
186        expected: usize,
187        /// Actual payload binder count.
188        actual: usize,
189    },
190    /// Match arm appeared after a wildcard arm.
191    #[error("`match` arm after `_` is unreachable")]
192    UnreachableMatchArm,
193    /// Match expression did not cover all enum variants.
194    #[error("non-exhaustive `match` on `{enum_name}`; missing {missing}")]
195    NonExhaustiveMatch {
196        /// Enum name being matched.
197        enum_name: String,
198        /// Comma-separated list of missing variants.
199        missing: String,
200    },
201    /// `for` iterable expression was not an array or slice.
202    #[error("`for` iterable must be `[T; N]` or `[T]`, found `{actual}`")]
203    ForIterableNotCollection {
204        /// Actual iterable type.
205        actual: Ty,
206    },
207    /// Empty array literal needed type context but none was available.
208    #[error("`[]` requires array or slice context to infer element type")]
209    EmptyArrayNeedsContext,
210    /// Empty array literal was used with non-collection type context.
211    #[error("`[]` expected array or slice context, found `{actual}`")]
212    EmptyArrayWithNonArrayContext {
213        /// Actual contextual type.
214        actual: Ty,
215    },
216    /// Array literal contained incompatible element types.
217    #[error("array elements must be homogeneous; expected `{expected}`, found `{actual}`")]
218    HeterogeneousArray {
219        /// Expected element type.
220        expected: Ty,
221        /// Actual element type that mismatched.
222        actual: Ty,
223    },
224    /// Slice base expression was not an array or slice.
225    #[error("slice base must be `[T; N]` or `[T]`, found `{actual}`")]
226    SliceBaseNotCollection {
227        /// Actual base expression type.
228        actual: Ty,
229    },
230    /// Index base expression was not an array or slice.
231    #[error("index base must be `[T; N]` or `[T]`, found `{actual}`")]
232    IndexBaseNotCollection {
233        /// Actual base expression type.
234        actual: Ty,
235    },
236    /// Slice view value was copied into an owned context.
237    #[error("slice range expressions are read-only views and cannot be copied into owned values")]
238    CannotCopySliceView,
239    /// Prefix operator was used on a non-binding expression.
240    #[error("prefix `{op}` requires a named binding")]
241    PrefixRequiresBinding {
242        /// Prefix operator name.
243        op: &'static str,
244    },
245    /// Mutable borrow was requested from a read-only binding.
246    #[error("cannot mutably borrow read-only binding `{name}`")]
247    CannotMutBorrowReadOnlyBinding {
248        /// Binding name.
249        name: String,
250    },
251    /// Assignment attempted through a read-only binding.
252    #[error("cannot assign to read-only binding `{name}`")]
253    CannotAssignReadOnlyBinding {
254        /// Binding name.
255        name: String,
256    },
257    /// Read attempted while mutable borrow was active.
258    #[error("cannot read `{place}` while mutable borrow is active")]
259    CannotReadWhileMutBorrowed {
260        /// Place name being read.
261        place: String,
262    },
263    /// Write attempted while overlapping borrows were active.
264    #[error("cannot write `{place}` while overlapping borrows are active")]
265    CannotWriteWhileBorrowed {
266        /// Place name being written.
267        place: String,
268    },
269    /// Shared borrow attempted while mutable borrow was active.
270    #[error("cannot create shared borrow of `{place}` while mutable borrow is active")]
271    CannotBorrowSharedWhileMutBorrowed {
272        /// Place name being borrowed.
273        place: String,
274    },
275    /// Mutable borrow attempted while overlapping borrows were active.
276    #[error("cannot create mutable borrow of `{place}` while overlapping borrows are active")]
277    CannotBorrowMutWhileBorrowed {
278        /// Place name being borrowed.
279        place: String,
280    },
281    /// Move attempted from a borrowed binding.
282    #[error("cannot move from borrowed binding `{name}`")]
283    CannotMoveBorrowedBinding {
284        /// Binding name.
285        name: String,
286    },
287    /// Prefix operator was used outside call arguments.
288    #[error("prefix `{op}` is only valid in call arguments")]
289    PrefixOnlyValidInCallArgs {
290        /// Prefix operator name.
291        op: &'static str,
292    },
293    /// Unary negation operand was not an integer.
294    #[error("unary `-` requires signed numeric type, found `{actual}`")]
295    UnaryNegRequiresInt {
296        /// Actual operand type.
297        actual: Ty,
298    },
299    /// Integer operator was applied to non-integer operands.
300    #[error("operator `{op}` requires numeric operands, found `{lhs}` and `{rhs}`")]
301    IntArithmeticRequiresInt {
302        /// Operator name.
303        op: &'static str,
304        /// Left-hand operand type.
305        lhs: Ty,
306        /// Right-hand operand type.
307        rhs: Ty,
308    },
309    /// Number literal did not provide enough type information.
310    #[error("numeric literal `{literal}` is ambiguous; add a type suffix")]
311    AmbiguousNumericLiteral {
312        /// Raw literal text.
313        literal: String,
314    },
315    /// Number literal could not be parsed.
316    #[error("invalid numeric literal `{literal}`")]
317    InvalidNumericLiteral {
318        /// Raw literal text.
319        literal: String,
320    },
321    /// Number literal is out of range for the inferred type.
322    #[error("numeric literal `{literal}` is out of range for `{ty}`")]
323    NumericLiteralOutOfRange {
324        /// Raw literal text.
325        literal: String,
326        /// Inferred literal type.
327        ty: Ty,
328    },
329    /// Struct literal referenced an unknown field.
330    #[error("unknown field `{field}` on struct `{name}`")]
331    UnknownStructField {
332        /// Struct name.
333        name: String,
334        /// Unknown field name.
335        field: String,
336    },
337    /// Struct literal omitted a required field.
338    #[error("missing field `{field}` for struct `{name}` literal")]
339    MissingStructField {
340        /// Struct name.
341        name: String,
342        /// Missing field name.
343        field: String,
344    },
345    /// Struct literal repeated a field assignment.
346    #[error("duplicate field `{field}` in struct `{name}` literal")]
347    DuplicateStructLiteralField {
348        /// Struct name.
349        name: String,
350        /// Duplicate field name.
351        field: String,
352    },
353    /// Struct type arguments could not be inferred.
354    #[error("cannot infer generic arguments for struct `{name}`")]
355    StructTypeArgsNotInferred {
356        /// Struct name.
357        name: String,
358    },
359    /// Call supplied type arguments to a non-generic function.
360    #[error("function `{function}` does not take type arguments")]
361    UnexpectedCallTypeArgs {
362        /// Function name.
363        function: String,
364    },
365    /// Generic type survived past elaboration.
366    #[error("generic type `{name}` survived past elaboration")]
367    GenericTypePastElaboration {
368        /// Generic type constructor name.
369        name: String,
370    },
371    /// Field access base expression was not a struct.
372    #[error("field access requires a struct value, found `{actual}`")]
373    FieldAccessNotStruct {
374        /// Actual base expression type.
375        actual: Ty,
376    },
377    /// Pointer payload type would introduce unsupported indirection.
378    #[error("pointer payload `{ty}` would add an extra heap indirection")]
379    InvalidPointerPayload {
380        /// Rejected pointer payload type.
381        ty: Ty,
382    },
383    /// Move assignment source was not a named binding.
384    #[error("`<-` assignment requires a named source binding")]
385    InvalidMoveAssignmentSource,
386    /// Binding was used after its value had been moved.
387    #[error("use of moved binding `{name}`")]
388    UseAfterMove {
389        /// Binding name.
390        name: String,
391    },
392    /// Function body produced a value with the wrong return type.
393    #[error("function `{function}` returns `{actual}` but declared `{expected}`")]
394    ReturnTypeMismatch {
395        /// Function name.
396        function: String,
397        /// Declared return type.
398        expected: Ty,
399        /// Actual inferred return type.
400        actual: Ty,
401    },
402    /// Meta splice reached runtime semantic checking unexpectedly.
403    #[error("meta splice survived to runtime checking")]
404    UnexpectedRuntimeSplice,
405    /// One value type recursively contains itself without pointer indirection.
406    #[error(
407        "type `{name}` contains a recursive value cycle; add pointer indirection to break the cycle"
408    )]
409    RecursiveValueType {
410        /// Type name participating in the recursive value cycle.
411        name: String,
412    },
413}