Migrate from failure to thiserror

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 crate integrates with the standard Error
trait instead.
This commit is contained in:
Josh Triplett
2019-10-30 07:30:20 -07:00
committed by Dan Gohman
parent 6de45ff8fc
commit 7e725cf880
9 changed files with 37 additions and 53 deletions

View File

@@ -65,8 +65,8 @@ use crate::settings::SetResult;
use crate::timing;
use alloc::boxed::Box;
use core::fmt;
use failure_derive::Fail;
use target_lexicon::{triple, Architecture, PointerWidth, Triple};
use thiserror::Error;
#[cfg(feature = "riscv")]
mod riscv;
@@ -124,14 +124,14 @@ pub fn lookup_by_name(name: &str) -> Result<Builder, LookupError> {
}
/// Describes reason for target lookup failure
#[derive(Fail, PartialEq, Eq, Copy, Clone, Debug)]
#[derive(Error, PartialEq, Eq, Copy, Clone, Debug)]
pub enum LookupError {
/// Support for this target was disabled in the current build.
#[fail(display = "Support for this target is disabled")]
#[error("Support for this target is disabled")]
SupportDisabled,
/// Support for this target has not yet been implemented.
#[fail(display = "Support for this target has not been implemented yet")]
#[error("Support for this target has not been implemented yet")]
Unsupported,
}

View File

@@ -1,19 +1,19 @@
//! Result and error types representing the outcome of compiling a function.
use crate::verifier::VerifierErrors;
use failure_derive::Fail;
use thiserror::Error;
/// A compilation error.
///
/// When Cranelift fails to compile a function, it will return one of these error codes.
#[derive(Fail, Debug, PartialEq, Eq)]
#[derive(Error, Debug, PartialEq, Eq)]
pub enum CodegenError {
/// A list of IR verifier errors.
///
/// This always represents a bug, either in the code that generated IR for Cranelift, or a bug
/// in Cranelift itself.
#[fail(display = "Verifier errors:\n{}", _0)]
Verifier(#[cause] VerifierErrors),
#[error("Verifier errors")]
Verifier(#[from] VerifierErrors),
/// An implementation limit was exceeded.
///
@@ -21,22 +21,16 @@ pub enum CodegenError {
/// limits][limits] that cause compilation to fail when they are exceeded.
///
/// [limits]: https://cranelift.readthedocs.io/en/latest/ir.html#implementation-limits
#[fail(display = "Implementation limit exceeded")]
#[error("Implementation limit exceeded")]
ImplLimitExceeded,
/// The code size for the function is too large.
///
/// Different target ISAs may impose a limit on the size of a compiled function. If that limit
/// is exceeded, compilation fails.
#[fail(display = "Code for function is too large")]
#[error("Code for function is too large")]
CodeTooLarge,
}
/// A convenient alias for a `Result` that uses `CodegenError` as the error type.
pub type CodegenResult<T> = Result<T, CodegenError>;
impl From<VerifierErrors> for CodegenError {
fn from(e: VerifierErrors) -> Self {
Self::Verifier(e)
}
}

View File

@@ -26,7 +26,7 @@ use alloc::boxed::Box;
use alloc::string::{String, ToString};
use core::fmt;
use core::str;
use failure_derive::Fail;
use thiserror::Error;
/// A string-based configurator for settings groups.
///
@@ -165,18 +165,18 @@ impl Configurable for Builder {
}
/// An error produced when changing a setting.
#[derive(Fail, Debug, PartialEq, Eq)]
#[derive(Error, Debug, PartialEq, Eq)]
pub enum SetError {
/// No setting by this name exists.
#[fail(display = "No existing setting named '{}'", _0)]
#[error("No existing setting named '{0}'")]
BadName(String),
/// Type mismatch for setting (e.g., setting an enum setting as a bool).
#[fail(display = "Trying to set a setting with the wrong type")]
#[error("Trying to set a setting with the wrong type")]
BadType,
/// This is not a valid value for this setting.
#[fail(display = "Unexpected value for a setting, expected {}", _0)]
#[error("Unexpected value for a setting, expected {0}")]
BadValue(String),
}

View File

@@ -77,7 +77,7 @@ use alloc::string::String;
use alloc::vec::Vec;
use core::cmp::Ordering;
use core::fmt::{self, Display, Formatter, Write};
use failure_derive::Fail;
use thiserror::Error;
pub use self::cssa::verify_cssa;
pub use self::liveness::verify_liveness;
@@ -127,7 +127,8 @@ mod liveness;
mod locations;
/// A verifier error.
#[derive(Fail, Debug, PartialEq, Eq)]
#[derive(Error, Debug, PartialEq, Eq)]
#[error("{location}: {message}")]
pub struct VerifierError {
/// The entity causing the verifier error.
pub location: AnyEntity,
@@ -135,12 +136,6 @@ pub struct VerifierError {
pub message: String,
}
impl Display for VerifierError {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
write!(f, "{}: {}", self.location, self.message)
}
}
/// Result of a step in the verification process.
///
/// Functions that return `VerifierStepResult<()>` should also take a
@@ -160,7 +155,7 @@ pub type VerifierStepResult<T> = Result<T, ()>;
pub type VerifierResult<T> = Result<T, VerifierErrors>;
/// List of verifier errors.
#[derive(Fail, Debug, Default, PartialEq, Eq)]
#[derive(Error, Debug, Default, PartialEq, Eq)]
pub struct VerifierErrors(pub Vec<VerifierError>);
impl VerifierErrors {