Skip to main content

rex_typesystem/
error.rs

1use crate::types::{Type, TypeVarId};
2use rex_ast::expr::Symbol;
3use rex_lexer::span::Span;
4use rex_util::OutOfGas;
5
6#[derive(Debug, thiserror::Error, PartialEq, Eq)]
7pub enum TypeError {
8    #[error("types do not unify: {0} vs {1}")]
9    Unification(String, String),
10    #[error("occurs check failed for {0} in {1}")]
11    Occurs(TypeVarId, String),
12    #[error("unknown class {0}")]
13    UnknownClass(Symbol),
14    #[error("no instance for {0} {1}")]
15    NoInstance(Symbol, String),
16    #[error("unknown type {0}")]
17    UnknownTypeName(Symbol),
18    #[error("cannot redefine reserved builtin type `{0}`")]
19    ReservedTypeName(Symbol),
20    #[error("duplicate value definition `{0}`")]
21    DuplicateValue(Symbol),
22    #[error("duplicate class definition `{0}`")]
23    DuplicateClass(Symbol),
24    #[error("class `{class}` must have at least one type parameter (got {got})")]
25    InvalidClassArity { class: Symbol, got: usize },
26    #[error("duplicate class method `{0}`")]
27    DuplicateClassMethod(Symbol),
28    #[error("unknown method `{method}` in instance of class `{class}`")]
29    UnknownInstanceMethod { class: Symbol, method: Symbol },
30    #[error("missing implementation of `{method}` for instance of class `{class}`")]
31    MissingInstanceMethod { class: Symbol, method: Symbol },
32    #[error(
33        "instance method `{method}` requires constraint {class} {typ}, but it is not in the instance context"
34    )]
35    MissingInstanceConstraint {
36        method: Symbol,
37        class: Symbol,
38        typ: String,
39    },
40    #[error("unbound variable {0}")]
41    UnknownVar(Symbol),
42    #[error("ambiguous overload for {0}")]
43    AmbiguousOverload(Symbol),
44    #[error("ambiguous type variable(s) {vars:?} in constraints: {constraints}")]
45    AmbiguousTypeVars {
46        vars: Vec<TypeVarId>,
47        constraints: String,
48    },
49    #[error(
50        "kind mismatch for class `{class}`: expected {expected} type argument(s) remaining, got {got} for {typ}"
51    )]
52    KindMismatch {
53        class: Symbol,
54        expected: usize,
55        got: usize,
56        typ: String,
57    },
58    #[error("missing type class constraint(s): {constraints}")]
59    MissingConstraints { constraints: String },
60    #[error("unsupported expression {0}")]
61    UnsupportedExpr(&'static str),
62    #[error("unknown field `{field}` on {typ}")]
63    UnknownField { field: Symbol, typ: String },
64    #[error("field `{field}` is not definitely available on {typ}")]
65    FieldNotKnown { field: Symbol, typ: String },
66    #[error("non-exhaustive match for {typ}: missing {missing:?}")]
67    NonExhaustiveMatch { typ: String, missing: Vec<Symbol> },
68    #[error("at {span}: {error}")]
69    Spanned { span: Span, error: Box<TypeError> },
70    #[error("internal error: {0}")]
71    Internal(String),
72    #[error("{0}")]
73    OutOfGas(#[from] OutOfGas),
74}
75
76impl TypeError {
77    pub fn with_span(self, span: &Span) -> TypeError {
78        match self {
79            TypeError::Spanned { .. } => self,
80            other => TypeError::Spanned {
81                span: *span,
82                error: Box::new(other),
83            },
84        }
85    }
86}
87
88#[derive(Clone, Debug, Hash, Eq, PartialEq, Ord, PartialOrd)]
89pub struct AdtConflict {
90    pub name: Symbol,
91    pub definitions: Vec<Type>,
92}
93
94#[derive(Clone, Debug, thiserror::Error, PartialEq, Eq)]
95#[error("conflicting ADT definitions: {conflicts:?}")]
96pub struct CollectAdtsError {
97    pub conflicts: Vec<AdtConflict>,
98}