Skip to content

Commit

Permalink
Remove failure Reference
Browse files Browse the repository at this point in the history
Replace use of the deprecated failure crate with custom display impls
for our errors.

fixes: #43
  • Loading branch information
iwillspeak committed Aug 28, 2022
1 parent f1d8b00 commit 829ef8d
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 116 deletions.
101 changes: 0 additions & 101 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,5 @@ llvm-13 = { package = "llvm-sys", version = "130", optional = true }
docopt = "1.1"
serde = { version = "1.0", features = ["derive"] }
tempfile = "3.1"
failure = "0.1"
libc = "0.2"
indexmap = "1.5"
49 changes: 38 additions & 11 deletions src/compile/error.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,40 @@
//! Compilation error module. Contains the Result and Error types for
//! the compile module.
use failure::Fail;
use std::io;
use std::{fmt::Display, io};

/// Represents the different types of errors which can be encountered
/// when compiling.
#[derive(Fail, Debug)]
#[derive(Debug)]
pub enum CompError {
/// Generic Error String
#[fail(display = "compilation error: {}", _0)]
Generic(String),

/// Linker Failure
#[fail(display = "linker failed: {}", _0)]
Linker(#[cause] LinkerError),
Linker(LinkerError),

/// Wrapped IO Error
#[fail(display = "IO error: {}", _0)]
IO(#[cause] ::std::io::Error),
IO(io::Error),
}

impl std::error::Error for CompError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
CompError::Linker(e) => Some(e),
CompError::IO(e) => Some(e),
_ => None,
}
}
}

impl Display for CompError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
CompError::Generic(msg) => write!(f, "compilation error: {}", msg),
CompError::Linker(cause) => write!(f, "linker failed.: {}", cause),
CompError::IO(cause) => write!(f, "IO error: {}", cause),
}
}
}

/// Compilation result. Returned from each compilation stage.
Expand All @@ -27,17 +43,28 @@ pub type CompResult<T> = Result<T, CompError>;
/// Link Failure Type
///
/// Used to group together the different failure modes for the linker.
#[derive(Fail, Debug)]
#[derive(Debug)]
pub enum LinkerError {
/// The linker failed with a known exit status
#[fail(display = "linker returned exit status {}: {}", _0, _1)]
WithExitStatus(i32, String),

/// The linker failed with an unknown exit status
#[fail(display = "unknown linker error: {}", _0)]
UnknownFailure(String),
}

impl std::error::Error for LinkerError {}

impl Display for LinkerError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
LinkerError::WithExitStatus(status, msg) => {
write!(f, "linker returned exit status {}: {}", status, msg)
}
LinkerError::UnknownFailure(msg) => write!(f, "unknown linker error: {}", msg),
}
}
}

impl From<String> for CompError {
/// Convert untyped errors to generic compilation errors.
fn from(s: String) -> Self {
Expand Down
13 changes: 10 additions & 3 deletions src/low_loader/targets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
use super::llvm_sys::core::LLVMDisposeMessage;
use super::llvm_sys::target_machine::*;
use failure::Fail;
use std::ffi::{CStr, CString};
use std::fmt::Display;
use std::{fmt, ptr};

/// Compilation Target
Expand All @@ -18,10 +18,17 @@ pub struct Target {
/// Target Lookup Error
///
/// Returned if a target couldn't be resolved from the given triple.
#[derive(Fail, Debug)]
#[fail(display = "Could not find target: '{}'", _0)]
#[derive(Debug)]
pub struct TargetLookupError(String);

impl std::error::Error for TargetLookupError {}

impl Display for TargetLookupError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Could not find target: '{}'", self.0)
}
}

impl fmt::Display for Target {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
writeln!(f, "Target information for {}:", self.triple)?;
Expand Down

0 comments on commit 829ef8d

Please sign in to comment.