summaryrefslogtreecommitdiff
path: root/dhall/src/error/mod.rs
blob: 5595c53ca8f717df42488275f2545cdec05c9890 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
use std::io::Error as IOError;

use crate::semantics::core::value::Value;
use crate::semantics::phase::resolve::ImportStack;
use crate::semantics::phase::NormalizedExpr;
use crate::syntax::{BinOp, Import, Label, ParseError, Span};

pub type Result<T> = std::result::Result<T, Error>;

#[derive(Debug)]
#[non_exhaustive]
pub enum Error {
    IO(IOError),
    Parse(ParseError),
    Decode(DecodeError),
    Encode(EncodeError),
    Resolve(ImportError),
    Typecheck(TypeError),
}

#[derive(Debug)]
pub enum ImportError {
    Recursive(Import<NormalizedExpr>, Box<Error>),
    UnexpectedImport(Import<NormalizedExpr>),
    ImportCycle(ImportStack, Import<NormalizedExpr>),
}

#[derive(Debug)]
pub enum DecodeError {
    CBORError(serde_cbor::error::Error),
    WrongFormatError(String),
}

#[derive(Debug)]
pub enum EncodeError {
    CBORError(serde_cbor::error::Error),
}

/// A structured type error
#[derive(Debug)]
pub struct TypeError {
    message: TypeMessage,
}

/// The specific type error
#[derive(Debug)]
pub(crate) enum TypeMessage {
    UnboundVariable(Span),
    InvalidInputType(Value),
    InvalidOutputType(Value),
    NotAFunction(Value),
    TypeMismatch(Value, Value, Value),
    AnnotMismatch(Value, Value),
    InvalidListElement(usize, Value, Value),
    InvalidListType(Value),
    InvalidOptionalType(Value),
    InvalidPredicate(Value),
    IfBranchMismatch(Value, Value),
    IfBranchMustBeTerm(bool, Value),
    InvalidFieldType(Label, Value),
    NotARecord(Label, Value),
    MustCombineRecord(Value),
    MissingRecordField(Label, Value),
    MissingUnionField(Label, Value),
    BinOpTypeMismatch(BinOp, Value),
    InvalidTextInterpolation(Value),
    Merge1ArgMustBeRecord(Value),
    Merge2ArgMustBeUnionOrOptional(Value),
    MergeEmptyNeedsAnnotation,
    MergeHandlerMissingVariant(Label),
    MergeVariantMissingHandler(Label),
    MergeAnnotMismatch,
    MergeHandlerTypeMismatch,
    MergeHandlerReturnTypeMustNotBeDependent,
    ProjectionMustBeRecord,
    ProjectionMissingEntry,
    ProjectionDuplicateField,
    Sort,
    RecordTypeDuplicateField,
    RecordTypeMergeRequiresRecordType(Value),
    UnionTypeDuplicateField,
    EquivalenceArgumentMustBeTerm(bool, Value),
    EquivalenceTypeMismatch(Value, Value),
    AssertMismatch(Value, Value),
    AssertMustTakeEquivalence,
}

impl TypeError {
    pub(crate) fn new(message: TypeMessage) -> Self {
        TypeError { message }
    }
}

impl std::fmt::Display for TypeError {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        use TypeMessage::*;
        let msg = match &self.message {
            UnboundVariable(var) => var.error("Type error: Unbound variable"),
            InvalidInputType(v) => {
                v.span().error("Type error: Invalid function input")
            }
            InvalidOutputType(v) => {
                v.span().error("Type error: Invalid function output")
            }
            NotAFunction(v) => v.span().error("Type error: Not a function"),
            TypeMismatch(x, y, z) => {
                x.span()
                    .error("Type error: Wrong type of function argument")
                    + "\n"
                    + &z.span().error(format!(
                        "This argument has type {:?}",
                        z.get_type().unwrap()
                    ))
                    + "\n"
                    + &y.span().error(format!(
                        "But the function expected an argument of type {:?}",
                        y
                    ))
            }
            _ => format!("Type error: Unhandled error: {:?}", self.message),
        };
        write!(f, "{}", msg)
    }
}

impl std::error::Error for TypeError {}

impl std::fmt::Display for Error {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        match self {
            Error::IO(err) => write!(f, "{}", err),
            Error::Parse(err) => write!(f, "{}", err),
            Error::Decode(err) => write!(f, "{:?}", err),
            Error::Encode(err) => write!(f, "{:?}", err),
            Error::Resolve(err) => write!(f, "{:?}", err),
            Error::Typecheck(err) => write!(f, "{}", err),
        }
    }
}

impl std::error::Error for Error {}
impl From<IOError> for Error {
    fn from(err: IOError) -> Error {
        Error::IO(err)
    }
}
impl From<ParseError> for Error {
    fn from(err: ParseError) -> Error {
        Error::Parse(err)
    }
}
impl From<DecodeError> for Error {
    fn from(err: DecodeError) -> Error {
        Error::Decode(err)
    }
}
impl From<EncodeError> for Error {
    fn from(err: EncodeError) -> Error {
        Error::Encode(err)
    }
}
impl From<ImportError> for Error {
    fn from(err: ImportError) -> Error {
        Error::Resolve(err)
    }
}
impl From<TypeError> for Error {
    fn from(err: TypeError) -> Error {
        Error::Typecheck(err)
    }
}
impl From<crate::semantics::nze::nzexpr::TypeError> for Error {
    fn from(_: crate::semantics::nze::nzexpr::TypeError) -> Error {
        Error::Decode(DecodeError::WrongFormatError("type error".to_string()))
    }
}