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

@@ -13,11 +13,11 @@ extern crate alloc;
use alloc::boxed::Box;
use alloc::string::ToString;
use alloc::vec::Vec;
use anyhow::{bail, format_err, Result};
use core::convert::TryFrom;
use core::slice;
use core::str;
use cranelift_codegen::ir;
use failure::{bail, format_err, Error};
use wasm_webidl_bindings::ast;
use wasmtime_jit::{ActionOutcome, Context, RuntimeValue};
use wasmtime_runtime::{Export, InstanceHandle};
@@ -59,7 +59,7 @@ impl ModuleData {
/// interface types.
///
/// Returns an error if the wasm file is malformed.
pub fn new(wasm: &[u8]) -> Result<ModuleData, Error> {
pub fn new(wasm: &[u8]) -> Result<ModuleData> {
// Perform a fast search through the module for the right custom
// section. Actually parsing out the interface types data is currently a
// pretty expensive operation so we want to only do that if we actually
@@ -109,7 +109,7 @@ impl ModuleData {
handle: &mut InstanceHandle,
export: &str,
args: &[Value],
) -> Result<Vec<Value>, Error> {
) -> Result<Vec<Value>> {
let binding = self.binding_for_export(handle, export)?;
let incoming = binding.param_bindings()?;
let outgoing = binding.result_bindings()?;
@@ -130,7 +130,7 @@ impl ModuleData {
&self,
instance: &mut InstanceHandle,
name: &str,
) -> Result<ExportBinding<'_>, Error> {
) -> Result<ExportBinding<'_>> {
if let Some(binding) = self.interface_binding_for_export(name) {
return Ok(binding);
}
@@ -170,7 +170,7 @@ impl ModuleData {
impl ExportBinding<'_> {
/// Returns the list of binding expressions used to create the parameters
/// for this binding.
pub fn param_bindings(&self) -> Result<Vec<ast::IncomingBindingExpression>, Error> {
pub fn param_bindings(&self) -> Result<Vec<ast::IncomingBindingExpression>> {
match &self.kind {
ExportBindingKind::Rich { binding, .. } => Ok(binding.params.bindings.clone()),
ExportBindingKind::Raw(sig) => sig
@@ -184,7 +184,7 @@ impl ExportBinding<'_> {
}
/// Returns the list of scalar types used for this binding
pub fn param_types(&self) -> Result<Vec<ast::WebidlScalarType>, Error> {
pub fn param_types(&self) -> Result<Vec<ast::WebidlScalarType>> {
match &self.kind {
ExportBindingKind::Rich {
binding, section, ..
@@ -217,7 +217,7 @@ impl ExportBinding<'_> {
/// Returns the list of binding expressions used to extract the return
/// values of this binding.
pub fn result_bindings(&self) -> Result<Vec<ast::OutgoingBindingExpression>, Error> {
pub fn result_bindings(&self) -> Result<Vec<ast::OutgoingBindingExpression>> {
match &self.kind {
ExportBindingKind::Rich { binding, .. } => Ok(binding.result.bindings.clone()),
ExportBindingKind::Raw(sig) => sig
@@ -230,10 +230,7 @@ impl ExportBinding<'_> {
}
}
fn default_incoming(
idx: usize,
param: &ir::AbiParam,
) -> Result<ast::IncomingBindingExpression, Error> {
fn default_incoming(idx: usize, param: &ir::AbiParam) -> Result<ast::IncomingBindingExpression> {
let get = ast::IncomingBindingExpressionGet { idx: idx as u32 };
let ty = if param.value_type == ir::types::I32 {
walrus::ValType::I32
@@ -253,10 +250,7 @@ fn default_incoming(
.into())
}
fn default_outgoing(
idx: usize,
param: &ir::AbiParam,
) -> Result<ast::OutgoingBindingExpression, Error> {
fn default_outgoing(idx: usize, param: &ir::AbiParam) -> Result<ast::OutgoingBindingExpression> {
let ty = abi2ast(param)?;
Ok(ast::OutgoingBindingExpressionAs {
ty: ty.into(),
@@ -265,7 +259,7 @@ fn default_outgoing(
.into())
}
fn abi2ast(param: &ir::AbiParam) -> Result<ast::WebidlScalarType, Error> {
fn abi2ast(param: &ir::AbiParam) -> Result<ast::WebidlScalarType> {
Ok(if param.value_type == ir::types::I32 {
ast::WebidlScalarType::Long
} else if param.value_type == ir::types::I64 {
@@ -284,7 +278,7 @@ fn translate_incoming(
handle: &mut InstanceHandle,
bindings: &[ast::IncomingBindingExpression],
args: &[Value],
) -> Result<Vec<RuntimeValue>, Error> {
) -> Result<Vec<RuntimeValue>> {
let get = |expr: &ast::IncomingBindingExpression| match expr {
ast::IncomingBindingExpression::Get(g) => args
.get(g.idx as usize)
@@ -375,7 +369,7 @@ fn translate_outgoing(
handle: &mut InstanceHandle,
bindings: &[ast::OutgoingBindingExpression],
args: &[RuntimeValue],
) -> Result<Vec<Value>, Error> {
) -> Result<Vec<Value>> {
let mut values = Vec::new();
let raw_memory = || unsafe {

View File

@@ -24,12 +24,12 @@ macro_rules! from {
}
impl TryFrom<Value> for $a {
type Error = failure::Error;
type Error = anyhow::Error;
fn try_from(val: Value) -> Result<$a, Self::Error> {
match val {
Value::$b(v) => Ok(v),
v => failure::bail!("cannot convert {:?} to {}", v, stringify!($a)),
v => anyhow::bail!("cannot convert {:?} to {}", v, stringify!($a)),
}
}
}