Switch from error_core to failure.

This commit is contained in:
Dan Gohman
2018-02-15 23:14:33 -08:00
parent e37f45667f
commit 6c9cf2bacf
11 changed files with 33 additions and 99 deletions

View File

@@ -17,19 +17,17 @@ name = "cretonne"
# Please don't add any unless they are essential to the task of creating binary
# machine code. Integration tests that need external dependencies can be
# accomodated in `tests`.
failure = { version = "0.1.1", default-features = false, features = ["derive"] }
failure_derive = { version = "0.1.1", default-features = false }
[dependencies.hashmap_core]
version = "0.1.1"
optional = true
[dependencies.error_core]
version = "0.1.0"
optional = true
[features]
# The "std" feature enables use of libstd. The "no_std" feature enables use
# of some minimal std-like replacement libraries. At least one of these two
# features to be enabled.
default = ["std"]
std = []
no_std = ["hashmap_core", "error_core"]
no_std = ["hashmap_core"]

View File

@@ -8,11 +8,12 @@
// Include the `hashmap_core` crate if no_std
#[cfg(feature = "no_std")]
extern crate hashmap_core;
#[cfg(feature = "no_std")]
extern crate error_core;
#[cfg(not(feature = "std"))]
#[macro_use]
extern crate alloc;
extern crate failure;
#[macro_use]
extern crate failure_derive;
pub use context::Context;
pub use legalizer::legalize_function;
@@ -69,7 +70,4 @@ mod std {
pub use hashmap_core::map as hash_map;
pub use alloc::BTreeSet;
}
pub mod error {
pub use error_core::Error;
}
}

View File

@@ -1,25 +1,28 @@
//! Result and error types representing the outcome of compiling a function.
use verifier;
use std::error::Error as StdError;
use std::fmt;
/// A compilation error.
///
/// When Cretonne fails to compile a function, it will return one of these error codes.
#[derive(Debug, PartialEq, Eq)]
#[derive(Fail, Debug, PartialEq, Eq)]
pub enum CtonError {
/// The input is invalid.
///
/// This error code is used by a WebAssembly translator when it encounters invalid WebAssembly
/// code. This should never happen for validated WebAssembly code.
#[fail(display = "Invalid input code")]
InvalidInput,
/// An IL verifier error.
///
/// This always represents a bug, either in the code that generated IL for Cretonne, or a bug
/// in Cretonne itself.
Verifier(verifier::Error),
#[fail(display = "Verifier error: {}", _0)]
Verifier(
#[cause]
verifier::Error
),
/// An implementation limit was exceeded.
///
@@ -27,48 +30,20 @@ pub enum CtonError {
/// limits][limits] that cause compilation to fail when they are exceeded.
///
/// [limits]: http://cretonne.readthedocs.io/en/latest/langref.html#implementation-limits
#[fail(display = "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")]
CodeTooLarge,
}
/// A Cretonne compilation result.
pub type CtonResult = Result<(), CtonError>;
impl fmt::Display for CtonError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
CtonError::Verifier(ref e) => write!(f, "Verifier error: {}", e),
CtonError::InvalidInput |
CtonError::ImplLimitExceeded |
CtonError::CodeTooLarge => f.write_str(self.description()),
}
}
}
impl StdError for CtonError {
fn description(&self) -> &str {
match *self {
CtonError::InvalidInput => "Invalid input code",
CtonError::Verifier(ref e) => &e.message,
CtonError::ImplLimitExceeded => "Implementation limit exceeded",
CtonError::CodeTooLarge => "Code for function is too large",
}
}
fn cause(&self) -> Option<&StdError> {
match *self {
CtonError::Verifier(ref e) => Some(e),
CtonError::InvalidInput |
CtonError::ImplLimitExceeded |
CtonError::CodeTooLarge => None,
}
}
}
impl From<verifier::Error> for CtonError {
fn from(e: verifier::Error) -> CtonError {
CtonError::Verifier(e)

View File

@@ -70,7 +70,6 @@ use self::flags::verify_flags;
use settings::{Flags, FlagsOrIsa};
use std::cmp::Ordering;
use std::collections::BTreeSet;
use std::error as std_error;
use std::fmt::{self, Display, Formatter, Write};
use std::result;
use std::vec::Vec;
@@ -104,7 +103,7 @@ mod liveness;
mod locations;
/// A verifier error.
#[derive(Debug, PartialEq, Eq)]
#[derive(Fail, Debug, PartialEq, Eq)]
pub struct Error {
/// The entity causing the verifier error.
pub location: AnyEntity,
@@ -118,12 +117,6 @@ impl Display for Error {
}
}
impl std_error::Error for Error {
fn description(&self) -> &str {
&self.message
}
}
/// Verifier result.
pub type Result = result::Result<(), Error>;