Skip to main content

unitype_runtime/
error.rs

1//! What can go wrong while running a proof.
2
3use unitype_core::QualifiedName;
4use unitype_core::value::ValueError;
5
6/// Why an implementation did not produce an answer.
7///
8/// The distinction that matters here is between a proof being *wrong* and a
9/// proof being *unreachable*. An implementation that returns the wrong shape
10/// has failed to prove what it claimed, and trying a different one would be
11/// papering over that. An implementation that could not be started because the
12/// interpreter is not installed has not failed at all, and the dispatcher is
13/// free to look elsewhere.
14#[derive(thiserror::Error, Debug)]
15pub enum ExecError {
16    /// The environment could not be reached at all.
17    #[error("could not reach the `{environment}` environment: {detail}")]
18    Unreachable {
19        /// Where the implementation was to run.
20        environment: String,
21        /// What went wrong.
22        detail: String,
23    },
24    /// The implementation did not answer in time.
25    #[error("`{id}` did not answer within {millis}ms")]
26    Timeout {
27        /// The implementation that hung.
28        id: String,
29        /// How long it was given.
30        millis: u64,
31    },
32    /// The implementation used up the work it was allowed.
33    ///
34    /// The sandbox working rather than the proof being wrong: a guest with no
35    /// process to kill is stopped this way instead.
36    #[error("`{id}` used up its budget of {budget} before answering")]
37    Exhausted {
38        /// The implementation that would not stop.
39        id: String,
40        /// How much work it was allowed.
41        budget: u64,
42    },
43    /// The implementation reported a failure of its own.
44    #[error("`{id}` failed: {message}")]
45    Failed {
46        /// The implementation that failed.
47        id: String,
48        /// What it said.
49        message: String,
50    },
51    /// The two sides did not agree on how to talk.
52    #[error("`{id}` broke the protocol: {detail}")]
53    Protocol {
54        /// The implementation involved.
55        id: String,
56        /// What was wrong with what it sent.
57        detail: String,
58    },
59    /// An implementation was registered without a body to run.
60    #[error("`{0}` has no body registered in this process")]
61    NotRegistered(String),
62
63    /// A body used a name nothing bound.
64    #[error("`{0}` has nothing bound to it")]
65    Unbound(String),
66    /// A body read a field off something that has none.
67    #[error("`{value}` has no field `{field}`")]
68    NoSuchField {
69        /// What was being read.
70        value: String,
71        /// The field asked for.
72        field: String,
73    },
74    /// A body did something its types said it would not.
75    ///
76    /// The checker rules these out ahead of time, so reaching one means a body
77    /// ran without being checked.
78    #[error("{0}")]
79    Untyped(String),
80    /// A body called a signature that is not in the workspace.
81    #[error("`{0}` is not a signature in this workspace")]
82    UnknownSignature(QualifiedName),
83    /// Following calls between bodies went too deep.
84    #[error("evaluation did not terminate")]
85    TooDeep,
86    /// Arithmetic produced a number the wire cannot carry.
87    ///
88    /// A failure of the body, not of anywhere it ran: every proof given the
89    /// same numbers would overflow the same way.
90    #[error("`{op}` overflowed: the result is not a number this system can carry")]
91    Overflow {
92        /// The operation.
93        op: String,
94    },
95}
96
97impl ExecError {
98    /// Whether this says something about the environment rather than about the
99    /// proof.
100    ///
101    /// Only these are worth retrying somewhere else. A proof that returned the
102    /// wrong thing will return the wrong thing again.
103    pub fn is_environmental(&self) -> bool {
104        matches!(
105            self,
106            ExecError::Unreachable { .. } | ExecError::Timeout { .. } | ExecError::Exhausted { .. }
107        )
108    }
109}
110
111/// Why a call did not reach an implementation at all.
112#[derive(thiserror::Error, Debug)]
113pub enum DispatchError {
114    /// The workspace has no such signature.
115    #[error("`{0}` is not a signature in this workspace")]
116    UnknownSignature(QualifiedName),
117    /// Nothing has been registered that proves it.
118    #[error("nothing proves `{0}`")]
119    NoImplementation(QualifiedName),
120    /// Everything that proves it is out of reach here.
121    #[error("nothing that proves `{sig}` can run here: {}", .skipped.join("; "))]
122    NoneAvailable {
123        /// The signature being called.
124        sig: QualifiedName,
125        /// Each candidate and why it was passed over.
126        skipped: Vec<String>,
127    },
128    /// A specific implementation was asked for and cannot be used.
129    #[error("`{id}` cannot be used: {reason}")]
130    Unusable {
131        /// The implementation that was named.
132        id: String,
133        /// Why not.
134        reason: String,
135    },
136    /// The arguments were not what the signature takes.
137    #[error("argument {index} of `{sig}`: {}", .errors.iter().map(ToString::to_string).collect::<Vec<_>>().join("; "))]
138    BadArgument {
139        /// The signature being called.
140        sig: QualifiedName,
141        /// Which argument.
142        index: usize,
143        /// What was wrong with it.
144        errors: Vec<ValueError>,
145    },
146    /// The wrong number of type arguments.
147    ///
148    /// A generic proposition is a claim about every type at once, and a value
149    /// has one type, so the call has to say which type it is at before anything
150    /// can be checked against it.
151    #[error("`{sig}` takes {expected} type argument(s), but {got} were given")]
152    TypeArity {
153        /// The signature being called.
154        sig: QualifiedName,
155        /// How many it takes.
156        expected: usize,
157        /// How many were given.
158        got: usize,
159    },
160    /// A type argument that is not what its parameter is bounded by.
161    ///
162    /// `pick<T: Named>` says something about every `T` that has a name, and
163    /// nothing about one that does not, so it cannot be asked at one.
164    #[error("`{sig}` takes `{param}` only where it is a `{bound}`, and `{arg}` is not")]
165    BoundNotMet {
166        /// The signature being called.
167        sig: QualifiedName,
168        /// The generic parameter.
169        param: unitype_core::Ident,
170        /// The type it was given.
171        arg: String,
172        /// The bound it does not meet.
173        bound: String,
174    },
175    /// The wrong number of arguments.
176    #[error("`{sig}` takes {expected} argument(s), but {got} were given")]
177    Arity {
178        /// The signature being called.
179        sig: QualifiedName,
180        /// How many it takes.
181        expected: usize,
182        /// How many were given.
183        got: usize,
184    },
185    /// An implementation returned something that is not what it promised.
186    ///
187    /// This is the one failure that never falls back. The point of checking
188    /// both sides of the boundary is to catch a proof that does not prove what
189    /// it claims, and quietly asking someone else would defeat it.
190    #[error("`{id}` claims to prove `{sig}` but returned something else: {}", .errors.iter().map(ToString::to_string).collect::<Vec<_>>().join("; "))]
191    BadResult {
192        /// The implementation at fault.
193        id: String,
194        /// What it claimed to prove.
195        sig: QualifiedName,
196        /// How the result fell short.
197        errors: Vec<ValueError>,
198    },
199    /// Running it failed.
200    #[error("`{id}`: {source}")]
201    Exec {
202        /// The implementation that was run.
203        id: String,
204        /// What went wrong.
205        #[source]
206        source: ExecError,
207    },
208}
209
210#[cfg(test)]
211mod tests {
212    use super::*;
213
214    #[test]
215    fn a_failure_of_the_environment_is_worth_trying_elsewhere() {
216        assert!(
217            ExecError::Unreachable {
218                environment: "python3".into(),
219                detail: "not installed".into(),
220            }
221            .is_environmental()
222        );
223        assert!(
224            ExecError::Timeout {
225                id: "slow".into(),
226                millis: 100
227            }
228            .is_environmental()
229        );
230    }
231
232    #[test]
233    fn a_failure_of_the_proof_is_not() {
234        assert!(
235            !ExecError::Failed {
236                id: "wrong".into(),
237                message: "divided by zero".into(),
238            }
239            .is_environmental(),
240            "asking someone else will not make this proof correct"
241        );
242        assert!(
243            !ExecError::Protocol {
244                id: "confused".into(),
245                detail: "sent nothing".into(),
246            }
247            .is_environmental()
248        );
249    }
250}