#[cfg(feature = "mlir")]
pub mod ops;
#[cfg(feature = "mlir")]
pub mod types;
#[cfg(feature = "mlir")]
pub mod context;
#[cfg(feature = "mlir")]
pub mod lowering;
#[cfg(feature = "mlir")]
pub use ops::OpBuilder;
#[cfg(feature = "mlir")]
pub use types::TypeLowering;
#[cfg(feature = "mlir")]
pub use context::MlirContext;
#[cfg(feature = "mlir")]
pub use lowering::HirToMlirLowering;
use crate::ast::Span;
use std::fmt;
#[derive(Debug, Clone, PartialEq)]
pub struct MlirError {
pub message: String,
pub span: Option<Span>,
}
impl MlirError {
pub fn new(message: impl Into<String>) -> Self {
Self {
message: message.into(),
span: None,
}
}
pub fn with_span(message: impl Into<String>, span: Span) -> Self {
Self {
message: message.into(),
span: Some(span),
}
}
}
impl fmt::Display for MlirError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if let Some(span) = &self.span {
write!(
f,
"MLIR error at {}..{}: {}",
span.start, span.end, self.message
)
} else {
write!(f, "MLIR error: {}", self.message)
}
}
}
impl std::error::Error for MlirError {}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_mlir_error_new() {
let err = MlirError::new("test error");
assert_eq!(err.message, "test error");
assert!(err.span.is_none());
}
#[test]
fn test_mlir_error_with_span() {
let span = Span {
start: 5,
end: 10,
line: 1,
column: 1,
};
let err = MlirError::with_span("test error", span);
assert_eq!(err.message, "test error");
assert_eq!(err.span, Some(span));
}
#[test]
fn test_mlir_error_display() {
let err = MlirError::new("no span");
assert_eq!(err.to_string(), "MLIR error: no span");
let span = Span {
start: 5,
end: 10,
line: 1,
column: 1,
};
let err = MlirError::with_span("with span", span);
assert_eq!(err.to_string(), "MLIR error at 5..10: with span");
}
}