diff --git a/Cargo.lock b/Cargo.lock index 05b1a92bd2..e1f4a79617 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -635,7 +635,6 @@ dependencies = [ "cranelift-entity", "hashbrown", "log", - "thiserror", ] [[package]] diff --git a/cranelift/module/Cargo.toml b/cranelift/module/Cargo.toml index 6650714cf6..a3fe4a0901 100644 --- a/cranelift/module/Cargo.toml +++ b/cranelift/module/Cargo.toml @@ -15,7 +15,6 @@ cranelift-codegen = { path = "../codegen", version = "0.73.0", default-features cranelift-entity = { path = "../entity", version = "0.73.0" } hashbrown = { version = "0.9.1", optional = true } log = { version = "0.4.6", default-features = false } -thiserror = "1.0.4" anyhow = "1.0" [features] diff --git a/cranelift/module/src/module.rs b/cranelift/module/src/module.rs index 6047dda103..97db899342 100644 --- a/cranelift/module/src/module.rs +++ b/cranelift/module/src/module.rs @@ -12,7 +12,6 @@ use cranelift_codegen::entity::{entity_impl, PrimaryMap}; use cranelift_codegen::{ir, isa, CodegenError, Context}; use std::borrow::ToOwned; use std::string::String; -use thiserror::Error; /// A function identifier for use in the `Module` interface. #[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)] @@ -168,30 +167,83 @@ impl FunctionDeclaration { } /// Error messages for all `Module` methods -#[derive(Error, Debug)] +#[derive(Debug)] pub enum ModuleError { /// Indicates an identifier was used before it was declared - #[error("Undeclared identifier: {0}")] Undeclared(String), + /// Indicates an identifier was used as data/function first, but then used as the other - #[error("Incompatible declaration of identifier: {0}")] IncompatibleDeclaration(String), + /// Indicates a function identifier was declared with a /// different signature than declared previously - #[error("Function {0} signature {2:?} is incompatible with previous declaration {1:?}")] IncompatibleSignature(String, ir::Signature, ir::Signature), + /// Indicates an identifier was defined more than once - #[error("Duplicate definition of identifier: {0}")] DuplicateDefinition(String), + /// Indicates an identifier was defined, but was declared as an import - #[error("Invalid to define identifier declared as an import: {0}")] InvalidImportDefinition(String), + /// Wraps a `cranelift-codegen` error - #[error("Compilation error: {0}")] - Compilation(#[from] CodegenError), + Compilation(CodegenError), + /// Wraps a generic error from a backend - #[error("Backend error: {0}")] - Backend(#[source] anyhow::Error), + Backend(anyhow::Error), +} + +impl std::error::Error for ModuleError { + fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { + match self { + Self::Undeclared { .. } + | Self::IncompatibleDeclaration { .. } + | Self::IncompatibleSignature { .. } + | Self::DuplicateDefinition { .. } + | Self::InvalidImportDefinition { .. } => None, + Self::Compilation(source) => Some(source), + Self::Backend(source) => Some(&**source), + } + } +} + +impl std::fmt::Display for ModuleError { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + match self { + Self::Undeclared(name) => { + write!(f, "Undeclared identifier: {}", name) + } + Self::IncompatibleDeclaration(name) => { + write!(f, "Incompatible declaration of identifier: {}", name,) + } + Self::IncompatibleSignature(name, prev_sig, new_sig) => { + write!( + f, + "Function {} signature {:?} is incompatible with previous declaration {:?}", + name, new_sig, prev_sig, + ) + } + Self::DuplicateDefinition(name) => { + write!(f, "Duplicate definition of identifier: {}", name) + } + Self::InvalidImportDefinition(name) => { + write!( + f, + "Invalid to define identifier declared as an import: {}", + name, + ) + } + Self::Compilation(err) => { + write!(f, "Compilation error: {}", err) + } + Self::Backend(err) => write!(f, "Backend error: {}", err), + } + } +} + +impl std::convert::From for ModuleError { + fn from(source: CodegenError) -> Self { + Self::Compilation { 0: source } + } } /// A convenient alias for a `Result` that uses `ModuleError` as the error type.