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

@@ -92,10 +92,6 @@ called `hashmap_core` is pulled in (only in `no_std` builds). This
is mostly the same as `std::collections::HashMap`, except that it doesn't is mostly the same as `std::collections::HashMap`, except that it doesn't
have DOS protection. Just something to think about. have DOS protection. Just something to think about.
Lastly, to support `std::error`, which isn't is `std` or `alloc` for
an inexplicable reason, the `error_core` crate is also used in `no_std` builds.
You might need it, as well, when interfacing with `CtonError`.
Building the documentation Building the documentation
-------------------------- --------------------------

View File

@@ -17,19 +17,17 @@ name = "cretonne"
# Please don't add any unless they are essential to the task of creating binary # 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 # machine code. Integration tests that need external dependencies can be
# accomodated in `tests`. # 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] [dependencies.hashmap_core]
version = "0.1.1" version = "0.1.1"
optional = true optional = true
[dependencies.error_core]
version = "0.1.0"
optional = true
[features] [features]
# The "std" feature enables use of libstd. The "no_std" feature enables use # 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 # of some minimal std-like replacement libraries. At least one of these two
# features to be enabled. # features to be enabled.
default = ["std"] default = ["std"]
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 // Include the `hashmap_core` crate if no_std
#[cfg(feature = "no_std")] #[cfg(feature = "no_std")]
extern crate hashmap_core; extern crate hashmap_core;
#[cfg(feature = "no_std")]
extern crate error_core;
#[cfg(not(feature = "std"))] #[cfg(not(feature = "std"))]
#[macro_use] #[macro_use]
extern crate alloc; extern crate alloc;
extern crate failure;
#[macro_use]
extern crate failure_derive;
pub use context::Context; pub use context::Context;
pub use legalizer::legalize_function; pub use legalizer::legalize_function;
@@ -69,7 +70,4 @@ mod std {
pub use hashmap_core::map as hash_map; pub use hashmap_core::map as hash_map;
pub use alloc::BTreeSet; 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. //! Result and error types representing the outcome of compiling a function.
use verifier; use verifier;
use std::error::Error as StdError;
use std::fmt;
/// A compilation error. /// A compilation error.
/// ///
/// When Cretonne fails to compile a function, it will return one of these error codes. /// 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 { pub enum CtonError {
/// The input is invalid. /// The input is invalid.
/// ///
/// This error code is used by a WebAssembly translator when it encounters invalid WebAssembly /// This error code is used by a WebAssembly translator when it encounters invalid WebAssembly
/// code. This should never happen for validated WebAssembly code. /// code. This should never happen for validated WebAssembly code.
#[fail(display = "Invalid input code")]
InvalidInput, InvalidInput,
/// An IL verifier error. /// An IL verifier error.
/// ///
/// This always represents a bug, either in the code that generated IL for Cretonne, or a bug /// This always represents a bug, either in the code that generated IL for Cretonne, or a bug
/// in Cretonne itself. /// in Cretonne itself.
Verifier(verifier::Error), #[fail(display = "Verifier error: {}", _0)]
Verifier(
#[cause]
verifier::Error
),
/// An implementation limit was exceeded. /// An implementation limit was exceeded.
/// ///
@@ -27,48 +30,20 @@ pub enum CtonError {
/// limits][limits] that cause compilation to fail when they are exceeded. /// limits][limits] that cause compilation to fail when they are exceeded.
/// ///
/// [limits]: http://cretonne.readthedocs.io/en/latest/langref.html#implementation-limits /// [limits]: http://cretonne.readthedocs.io/en/latest/langref.html#implementation-limits
#[fail(display = "Implementation limit exceeded")]
ImplLimitExceeded, ImplLimitExceeded,
/// The code size for the function is too large. /// 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 /// Different target ISAs may impose a limit on the size of a compiled function. If that limit
/// is exceeded, compilation fails. /// is exceeded, compilation fails.
#[fail(display = "Code for function is too large")]
CodeTooLarge, CodeTooLarge,
} }
/// A Cretonne compilation result. /// A Cretonne compilation result.
pub type CtonResult = Result<(), CtonError>; 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 { impl From<verifier::Error> for CtonError {
fn from(e: verifier::Error) -> CtonError { fn from(e: verifier::Error) -> CtonError {
CtonError::Verifier(e) CtonError::Verifier(e)

View File

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

View File

@@ -12,3 +12,5 @@ name = "filecheck"
[dependencies] [dependencies]
regex = "0.2.6" regex = "0.2.6"
failure = "0.1.1"
failure_derive = "0.1.1"

View File

@@ -1,21 +1,21 @@
use std::result; use std::result;
use std::convert::From; use std::convert::From;
use std::error::Error as StdError;
use std::fmt;
use regex; use regex;
/// A result from the filecheck library. /// A result from the filecheck library.
pub type Result<T> = result::Result<T, Error>; pub type Result<T> = result::Result<T, Error>;
/// A filecheck error. /// A filecheck error.
#[derive(Debug)] #[derive(Fail, Debug)]
pub enum Error { pub enum Error {
/// A syntax error in a check line. /// A syntax error in a check line.
#[fail(display = "{}", _0)]
Syntax(String), Syntax(String),
/// A check refers to an undefined variable. /// A check refers to an undefined variable.
/// ///
/// The pattern contains `$foo` where the `foo` variable has not yet been defined. /// The pattern contains `$foo` where the `foo` variable has not yet been defined.
/// Use `$$` to match a literal dollar sign. /// Use `$$` to match a literal dollar sign.
#[fail(display = "{}", _0)]
UndefVariable(String), UndefVariable(String),
/// A pattern contains a back-reference to a variable that was defined in the same pattern. /// A pattern contains a back-reference to a variable that was defined in the same pattern.
/// ///
@@ -26,40 +26,19 @@ pub enum Error {
/// check: Hello $(world=[^ ]*) /// check: Hello $(world=[^ ]*)
/// sameln: $world /// sameln: $world
/// ``` /// ```
#[fail(display = "{}", _0)]
Backref(String), Backref(String),
/// A pattern contains multiple definitions of the same variable. /// A pattern contains multiple definitions of the same variable.
#[fail(display = "{}", _0)]
DuplicateDef(String), DuplicateDef(String),
/// An error in a regular expression. /// An error in a regular expression.
/// ///
/// Use `cause()` to get the underlying `Regex` library error. /// Use `cause()` to get the underlying `Regex` library error.
Regex(regex::Error), #[fail(display = "{}", _0)]
} Regex(
#[cause]
impl StdError for Error { regex::Error
fn description(&self) -> &str { ),
use Error::*;
match *self {
Syntax(ref s) |
UndefVariable(ref s) |
Backref(ref s) |
DuplicateDef(ref s) => s,
Regex(ref err) => err.description(),
}
}
fn cause(&self) -> Option<&StdError> {
use Error::*;
match *self {
Regex(ref err) => Some(err),
_ => None,
}
}
}
impl fmt::Display for Error {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
write!(fmt, "{}", self.description())
}
} }
impl From<regex::Error> for Error { impl From<regex::Error> for Error {

View File

@@ -243,6 +243,9 @@ pub use variable::{VariableMap, Value, NO_VARIABLES};
pub use checker::{Checker, CheckerBuilder}; pub use checker::{Checker, CheckerBuilder};
extern crate regex; extern crate regex;
extern crate failure;
#[macro_use]
extern crate failure_derive;
mod error; mod error;
mod variable; mod variable;

View File

@@ -19,14 +19,10 @@ cretonne-frontend = { path = "../frontend", version = "0.1.0", default_features
version = "0.1.1" version = "0.1.1"
optional = true optional = true
[dependencies.error_core]
version = "0.1.0"
optional = true
[dev-dependencies] [dev-dependencies]
tempdir = "0.3.5" tempdir = "0.3.5"
[features] [features]
default = ["std"] default = ["std"]
std = ["cretonne/std", "cretonne-frontend/std"] std = ["cretonne/std", "cretonne-frontend/std"]
no_std = ["hashmap_core", "error_core", "cretonne/no_std", "cretonne-frontend/no_std"] no_std = ["hashmap_core", "cretonne/no_std", "cretonne-frontend/no_std"]

View File

@@ -9,7 +9,6 @@ use cretonne::ir::types::*;
use cretonne::cursor::FuncCursor; use cretonne::cursor::FuncCursor;
use cretonne::settings; use cretonne::settings;
use wasmparser; use wasmparser;
use std::error::Error;
use std::vec::Vec; use std::vec::Vec;
use std::string::String; use std::string::String;
@@ -387,7 +386,7 @@ impl<'data> ModuleEnvironment<'data> for DummyEnvironment {
let reader = wasmparser::BinaryReader::new(body_bytes); let reader = wasmparser::BinaryReader::new(body_bytes);
self.trans self.trans
.translate_from_reader(reader, &mut func, &mut func_environ) .translate_from_reader(reader, &mut func, &mut func_environ)
.map_err(|e| String::from(e.description()))?; .map_err(|e| format!("{}", e))?;
func func
}; };
self.func_bytecode_sizes.push(body_bytes.len()); self.func_bytecode_sizes.push(body_bytes.len());

View File

@@ -20,8 +20,6 @@ extern crate alloc;
#[cfg(feature = "no_std")] #[cfg(feature = "no_std")]
extern crate hashmap_core; extern crate hashmap_core;
#[cfg(feature = "no_std")]
extern crate error_core;
extern crate wasmparser; extern crate wasmparser;
extern crate cton_frontend; extern crate cton_frontend;
@@ -50,7 +48,4 @@ mod std {
pub mod collections { pub mod collections {
pub use hashmap_core::{HashMap, map as hash_map}; pub use hashmap_core::{HashMap, map as hash_map};
} }
pub mod error {
pub use error_core::Error;
}
} }