feat: Add span information to compiler errors

Signed-off-by: Anand Krishnamoorthi <anakrish@microsoft.com>
This commit is contained in:
Anand Krishnamoorthi
2025-12-02 12:29:15 -06:00
parent e060e43a6c
commit 5aefd51cb6
10 changed files with 159 additions and 57 deletions
+52 -1
View File
@@ -4,6 +4,9 @@
use alloc::format;
use alloc::string::String;
use crate::lexer::Span;
use core::fmt;
#[derive(thiserror::Error, Debug)]
pub enum CompilerError {
#[error("Not a builtin function: {name}")]
@@ -68,4 +71,52 @@ impl From<anyhow::Error> for CompilerError {
}
}
pub type Result<T> = ::core::result::Result<T, CompilerError>;
#[derive(Debug)]
pub struct SpannedCompilerError {
pub error: CompilerError,
pub span: Option<Span>,
}
impl SpannedCompilerError {
pub fn new(error: CompilerError) -> Self {
Self { error, span: None }
}
pub fn with_span(mut self, span: &Span) -> Self {
if self.span.is_none() {
self.span = Some(span.clone());
}
self
}
}
impl fmt::Display for SpannedCompilerError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if let Some(span) = &self.span {
let msg = format!("{}", self.error);
write!(f, "{}", span.message("error", &msg))
} else {
write!(f, "{}", self.error)
}
}
}
impl From<CompilerError> for SpannedCompilerError {
fn from(error: CompilerError) -> Self {
Self::new(error)
}
}
impl core::error::Error for SpannedCompilerError {
fn source(&self) -> Option<&(dyn core::error::Error + 'static)> {
Some(&self.error)
}
}
impl CompilerError {
pub fn at(self, span: &Span) -> SpannedCompilerError {
SpannedCompilerError::from(self).with_span(span)
}
}
pub type Result<T> = ::core::result::Result<T, SpannedCompilerError>;