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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
//! Macro definitions and the private runtime functions used in their generated
//! code.
// Items used by macro-generated code.
pub use format_args;
pub use Err;
use ;
use ;
use String;
/// Construct an [`Error`] via string formatting or another error.
///
/// Like `anyhow::format_err!` or `anyhow::anyhow!` but for
/// [`wasmtime::Error`](Error).
///
/// # String Formatting
///
/// When a string literal is the first argument, it is interpreted as a format
/// string template and the rest of the arguments are format arguments:
///
/// ```
/// # use wasmtime_internal_core::error as wasmtime;
/// use wasmtime::{format_err, Error};
///
/// let x = 42;
/// let error: Error = format_err!("x is {x}");
/// assert_eq!(error.to_string(), "x is 42");
///
/// let error: Error = format_err!("x / 2 is {}", x / 2);
/// assert_eq!(error.to_string(), "x / 2 is 21");
///
/// let error: Error = format_err!("x + 1 is {y}", y = x + 1);
/// assert_eq!(error.to_string(), "x + 1 is 43");
/// ```
///
/// # From Another Error
///
/// When a string literal is not the first argument, then it is treated as a
/// foreign error and is converted into an [`Error`]. The argument
/// must be of a type that can be passed to either [`Error::new`] or
/// [`Error::msg`].
///
/// ```
/// # fn _foo() {
/// #![cfg(feature = "std")]
/// # use wasmtime_internal_core::error as wasmtime;
/// use std::fmt;
/// use wasmtime::{format_err, Error};
///
/// #[derive(Debug)]
/// struct SomeOtherError(u32);
///
/// impl fmt::Display for SomeOtherError {
/// fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
/// write!(f, "some other error (code {})", self.0)
/// }
/// }
///
/// impl std::error::Error for SomeOtherError {}
///
/// let error: Error = format_err!(SomeOtherError(36));
/// assert!(error.is::<SomeOtherError>());
/// assert_eq!(error.to_string(), "some other error (code 36)");
/// # }
/// ```
///
/// # From an `anyhow::Error`
///
/// The `format_err!` macro can always convert an `anyhow::Error` into a
/// `wasmtime::Error`, but when the `"anyhow"` cargo feature is enabled the
/// resulting error will also return true for
/// [`error.is::<anyhow::Error>()`](Error::is) invocations.
///
/// ```
/// # fn _foo() {
/// #![cfg(feature = "anyhow")]
/// # use wasmtime_internal_core::error as wasmtime;
/// use wasmtime::format_err;
///
/// let anyhow_error: anyhow::Error = anyhow::anyhow!("aw crap");
/// let wasmtime_error: wasmtime::Error = format_err!(anyhow_error);
/// assert!(wasmtime_error.is::<anyhow::Error>());
/// # }
/// ```
/// Identical to the [`format_err!`] macro.
///
/// This is provided for API compatibility with the `anyhow` crate, but you
/// should prefer using `format_err!` instead.
/// Early exit from the current function with an error.
///
/// This helper is equivalent to `return Err(format_err!(...))`.
///
/// See the docs for the [`format_err!`] macro for details on
/// the kinds of errors that can be constructed.
///
/// Like `anyhow::bail!` but for [`wasmtime::Error`](Error).
///
/// # Example
///
/// ```
/// # use wasmtime_internal_core::error as wasmtime;
/// use wasmtime::{bail, Result};
///
/// fn error_on_none(option: Option<u32>) -> Result<u32> {
/// match option {
/// None => bail!("`error_on_none` got `None`!"),
/// Some(x) => Ok(x),
/// }
/// }
///
/// let x = error_on_none(Some(42)).unwrap();
/// assert_eq!(x, 42);
///
/// let error = error_on_none(None).unwrap_err();
/// assert_eq!(
/// error.to_string(),
/// "`error_on_none` got `None`!",
/// );
/// ```
/// Ensure that a condition holds true, or else early exit from the current
/// function with an error.
///
/// `ensure!(condition, ...)` is equivalent to the following:
///
/// ```ignore
/// if !condition {
/// return Err(format_err!(...));
/// }
/// ```
///
/// Like `anyhow::ensure!` but for [`wasmtime::Error`](Error).
///
/// # Example
///
/// ```rust
/// # use wasmtime_internal_core::error as wasmtime;
/// use wasmtime::{ensure, Result};
///
/// fn checked_div(a: u32, b: u32) -> Result<u32> {
/// ensure!(b != 0, "cannot divide by zero: {a} / {b}");
/// Ok(a / b)
/// }
///
/// let x = checked_div(6, 2).unwrap();
/// assert_eq!(x, 3);
///
/// let error = checked_div(9, 0).unwrap_err();
/// assert_eq!(
/// error.to_string(),
/// "cannot divide by zero: 9 / 0",
/// );
/// ```
/// We don't have specialization in stable Rust, so do a poor-person's
/// equivalent by hacking Rust's method name resolution and auto-deref. Given
/// that we have `n` versions of the "same" method, we do the following:
///
/// * We define `n` different traits, which each define the same trait method
/// name. The method need not have the same type across traits, but each must
/// type-check when chosen by method resolution at a particular call site.
///
/// * We implement each trait for an `i`-deep borrow of the type(s) we want to
/// specialize the `i`th implementation on, for example:
///
/// ```ignore
/// impl Specialization1 for &MyType { ... }
/// impl Specialization2 for &&OtherType { ... }
/// impl Specialization3 for &&&AnotherType { ... }
/// ```
///
/// * Call sites must have all specialization traits in scope and must borrow
/// the receiver `n` times before calling the method. Rust's method name
/// resolution will choose the method with the least number of references that
/// is well-typed. Therefore, specialization implementations for lower numbers
/// of borrows are preferred over those with higher numbers of borrows when
/// specializations overlap. For example, if both `<&&&T as
/// Specialization3>::method` and `<&T as Specialization1>::method` are
/// well-typed at the trait method call site `(&&&&&t).method()`, then
/// `Specialization1` will be prioritized over `Specialization3`.
///
/// In our specific case here of choosing an `Error` constructor, we have
/// three specializations:
///
/// 1. For `anyhow::Error`, we want to use the `Error::from_anyhow` constructor.
///
/// 2. When the type implements `core::error::Error`, we want to use the
/// `Error::new` constructor, which will preserve
/// `core::error::Error::source` chains.
///
/// 3. Otherwise, we want to use the `Error::msg` constructor.
///
/// The `*CtorTrait`s are our `n` specialization traits. Their
/// `wasmtime_error_choose_ctor` methods will return different types, each of
/// which is a dispatcher to their associated constructor. Those dispatchers
/// each have a constructor signature that is syntactically identical, but only
/// guaranteed to be well-typed based on the specialization that we did by
/// getting the dispatcher in the first place.
/// Runtime code for creating an `Error` from format arguments, handling OOM in
/// the process.