Files
ngr/src/backend/error.rs
2023-03-28 22:16:48 -05:00

48 lines
1.7 KiB
Rust

use codespan_reporting::diagnostic::Diagnostic;
use cranelift_codegen::{CodegenError, settings::SetError, isa::LookupError};
use cranelift_module::ModuleError;
use crate::backend::runtime::RuntimeFunctionError;
use thiserror::Error;
#[derive(Debug, Error)]
pub enum BackendError {
#[error("Cranelift module error: {0}")]
Cranelift(#[from] ModuleError),
#[error("Builtin function error: {0}")]
BuiltinError(#[from] RuntimeFunctionError),
#[error("Internal variable lookup error")]
VariableLookupFailure,
#[error(transparent)]
CodegenError(#[from] CodegenError),
#[error(transparent)]
SetError(#[from] SetError),
#[error(transparent)]
LookupError(#[from] LookupError),
}
impl From<BackendError> for Diagnostic<usize> {
fn from(value: BackendError) -> Self {
match value {
BackendError::Cranelift(me) => {
Diagnostic::error().with_message(format!("Internal cranelift error: {}", me))
}
BackendError::BuiltinError(me) => {
Diagnostic::error().with_message(format!("Internal runtime function error: {}", me))
}
BackendError::VariableLookupFailure => {
Diagnostic::error().with_message("Internal variable lookup error!")
}
BackendError::CodegenError(me) => {
Diagnostic::error().with_message(format!("Internal codegen error: {}", me))
}
BackendError::SetError(me) => {
Diagnostic::error().with_message(format!("Internal backend setup error: {}", me))
}
BackendError::LookupError(me) => {
Diagnostic::error().with_message(format!("Internal error: {}", me))
}
}
}
}