Migrate from failure to thiserror and anyhow (#436)

* Migrate from failure to thiserror and anyhow

The failure crate invents its own traits that don't use
std::error::Error (because failure predates certain features added to
Error); this prevents using ? on an error from failure in a function
using Error. The thiserror and anyhow crates integrate with the standard
Error trait instead.

This change does not attempt to semantically change or refactor the
approach to error-handling in any portion of the code, to ensure that
the change remains straightforward to review. Modules using specific
differentiated error types move from failure_derive and derive(Fail) to
thiserror and derive(Error). Modules boxing all errors opaquely move
from failure::Error to anyhow. Modules using String as an error type
continue to do so. Code using unwrap or expect continues to do so.

Drop Display implementations when thiserror can easily derive an
identical instance.

Drop manual traversal of iter_causes; anyhow's Debug instance prints the
chain of causes by default.

Use anyhow's type alias anyhow::Result<T> in place of
std::result::Result<T, anyhow::Error> whenever possible.

* wasm2obj: Simplify error handling using existing messages

handle_module in wasm2obj manually maps
cranelift_codegen::isa::LookupError values to strings, but LookupError
values already have strings that say almost exactly the same thing.
Rely on the strings from cranelift.

* wasmtime: Rely on question-mark-in-main

The main() wrapper around rmain() completely matches the behavior of
question-mark-in-main (print error to stderr and return 1), so switch to
question-mark-in-main.

* Update to walrus 0.13 and wasm-webidl-bindings 0.6

Both crates switched from failure to anyhow; updating lets us avoid a
translation from failure to anyhow within wasmtime-interface-types.
This commit is contained in:
Josh Triplett
2019-11-04 20:43:25 -08:00
committed by Dan Gohman
parent 0108622c7d
commit 56ce6e9c9f
53 changed files with 238 additions and 326 deletions

View File

@@ -11,16 +11,16 @@ readme = "README.md"
edition = "2018"
[dependencies]
cranelift-codegen = { version = "0.46.1", features = ["enable-serde"] }
cranelift-entity = { version = "0.46.1", features = ["enable-serde"] }
cranelift-wasm = { version = "0.46.1", features = ["enable-serde"] }
cranelift-frontend = "0.46.1"
cranelift-codegen = { version = "0.47", features = ["enable-serde"] }
cranelift-entity = { version = "0.47", features = ["enable-serde"] }
cranelift-wasm = { version = "0.47", features = ["enable-serde"] }
cranelift-frontend = { version = "0.47" }
wasmtime-environ = { path = "../wasmtime-environ", default-features = false }
wasmtime-runtime = { path = "../wasmtime-runtime", default-features = false }
wasmtime-debug = { path = "../wasmtime-debug", default-features = false }
region = "2.0.0"
failure = { version = "0.1.3", default-features = false }
failure_derive = { version = "0.1.3", default-features = false }
thiserror = "1.0.4"
target-lexicon = { version = "0.8.1", default-features = false }
hashbrown = { version = "0.6.0", optional = true }
wasmparser = { version = "0.39.2", default-features = false }

View File

@@ -7,6 +7,7 @@ use alloc::vec::Vec;
use core::cmp::max;
use core::{fmt, mem, ptr, slice};
use cranelift_codegen::ir;
use thiserror::Error;
use wasmtime_runtime::{wasmtime_call_trampoline, Export, InstanceHandle, VMInvokeArgument};
/// A runtime value.
@@ -110,22 +111,22 @@ pub enum ActionOutcome {
/// An error detected while invoking a wasm function or reading a wasm global.
/// Note that at this level, traps are not reported errors, but are rather
/// returned through `ActionOutcome`.
#[derive(Fail, Debug)]
#[derive(Error, Debug)]
pub enum ActionError {
/// An internal implementation error occurred.
#[fail(display = "{}", _0)]
Setup(SetupError),
#[error("{0}")]
Setup(#[from] SetupError),
/// No field with the specified name was present.
#[fail(display = "Unknown field: {}", _0)]
#[error("Unknown field: {0}")]
Field(String),
/// The field was present but was the wrong kind (eg. function, table, global, or memory).
#[fail(display = "Kind error: {}", _0)]
#[error("Kind error: {0}")]
Kind(String),
/// The field was present but was the wrong type (eg. i32, i64, f32, or f64).
#[fail(display = "Type error: {}", _0)]
#[error("Type error: {0}")]
Type(String),
}

View File

@@ -8,38 +8,26 @@ use alloc::boxed::Box;
use alloc::rc::Rc;
use alloc::string::{String, ToString};
use core::cell::RefCell;
use core::{fmt, str};
use cranelift_codegen::isa::TargetIsa;
use thiserror::Error;
use wasmparser::{validate, OperatorValidatorConfig, ValidatingParserConfig};
/// Indicates an unknown instance was specified.
#[derive(Fail, Debug)]
#[derive(Error, Debug)]
#[error("no instance {instance_name} present")]
pub struct UnknownInstance {
instance_name: String,
}
impl fmt::Display for UnknownInstance {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "no instance {} present", self.instance_name)
}
}
/// Error message used by `WastContext`.
#[derive(Fail, Debug)]
#[derive(Error, Debug)]
pub enum ContextError {
/// An unknown instance name was used.
Instance(UnknownInstance),
#[error("{0}")]
Instance(#[from] UnknownInstance),
/// An error occured while performing an action.
Action(ActionError),
}
impl fmt::Display for ContextError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match *self {
ContextError::Instance(ref error) => error.fmt(f),
ContextError::Action(ref error) => error.fmt(f),
}
}
#[error("{0}")]
Action(#[from] ActionError),
}
/// The collection of features configurable during compilation

View File

@@ -16,6 +16,7 @@ use cranelift_entity::{BoxedSlice, PrimaryMap};
use cranelift_wasm::{DefinedFuncIndex, SignatureIndex};
#[cfg(feature = "std")]
use std::io::Write;
use thiserror::Error;
use wasmtime_debug::read_debuginfo;
use wasmtime_environ::{
CompileError, DataInitializer, DataInitializerLocation, Module, ModuleEnvironment,
@@ -27,23 +28,23 @@ use wasmtime_runtime::{
/// An error condition while setting up a wasm instance, be it validation,
/// compilation, or instantiation.
#[derive(Fail, Debug)]
#[derive(Error, Debug)]
pub enum SetupError {
/// The module did not pass validation.
#[fail(display = "Validation error: {}", _0)]
#[error("Validation error: {0}")]
Validate(String),
/// A wasm translation error occured.
#[fail(display = "WebAssembly compilation error: {}", _0)]
Compile(CompileError),
#[error("WebAssembly compilation error: {0}")]
Compile(#[from] CompileError),
/// Some runtime resource was unavailable or insufficient, or the start function
/// trapped.
#[fail(display = "Instantiation error: {}", _0)]
Instantiate(InstantiationError),
#[error("Instantiation error: {0}")]
Instantiate(#[from] InstantiationError),
/// Debug information generation error occured.
#[fail(display = "Debug information error: {}", _0)]
#[error("Debug information error: {0}")]
DebugInfo(failure::Error),
}

View File

@@ -30,9 +30,6 @@ use hashbrown::{hash_map, HashMap, HashSet};
#[cfg(feature = "std")]
use std::collections::{hash_map, HashMap, HashSet};
#[macro_use]
extern crate failure_derive;
mod action;
mod code_memory;
mod compiler;