Add format_errors to Diagnostics

Introduces a new method `format_errors` to the `Diagnostics` struct.
This method iterates through all diagnostics, filters for errors, and
formats them into a newline-joined string. This simplifies error
reporting in compilation results.
This commit is contained in:
2026-03-27 13:17:58 +01:00
parent 9c5506b83e
commit 325e690cd9
2 changed files with 18 additions and 28 deletions
+10
View File
@@ -51,4 +51,14 @@ impl Diagnostics {
identity: id,
});
}
/// Formats all error-level diagnostics as a newline-joined string.
pub fn format_errors(&self) -> String {
self.items
.iter()
.filter(|d| d.level == DiagnosticLevel::Error)
.map(|d| d.message.as_str())
.collect::<Vec<_>>()
.join("\n")
}
}
+8 -28
View File
@@ -25,7 +25,7 @@ use crate::ast::types::{
CommentLine, NativeFunction, NodeIdentity, Purity, SourceLocation, StaticType, Value,
};
use crate::ast::diagnostics::{DiagnosticLevel, Diagnostics};
use crate::ast::diagnostics::Diagnostics;
pub type PipelineGenerator = Box<dyn FnMut() -> bool>;
const SYSTEM_LIB_SOURCE: &str = include_str!("rtl/prelude.myc");
@@ -59,14 +59,7 @@ impl CompilationResult {
pub fn into_result(self) -> Result<TypedNode, String> {
if self.diagnostics.has_errors() {
let errors: Vec<String> = self
.diagnostics
.items
.into_iter()
.filter(|d| d.level == DiagnosticLevel::Error)
.map(|d| d.message)
.collect();
Err(errors.join("\n"))
Err(self.diagnostics.format_errors())
} else if let Some(ast) = self.ast {
Ok(ast)
} else {
@@ -198,12 +191,7 @@ impl MacroEvaluator for RuntimeMacroEvaluator {
let typed_ast = checker.check(&bound_ast, &[], &mut diag);
if diag.has_errors() {
return Err(diag
.items
.into_iter()
.map(|d| d.message)
.collect::<Vec<_>>()
.join("\n"));
return Err(diag.format_errors());
}
let exec_ast = Lowering::lower(Analyzer::analyze(&typed_ast, &self.root_purity.borrow())); // Minimal analysis for macro eval
@@ -596,12 +584,7 @@ impl Environment {
let typed_ast_opt = self.compile_pipeline(syntax_ast, &mut diagnostics);
if diagnostics.has_errors() || typed_ast_opt.is_none() {
return Err(diagnostics
.items
.into_iter()
.map(|d| d.message)
.collect::<Vec<_>>()
.join("\n"));
return Err(diagnostics.format_errors());
}
Ok(typed_ast_opt.unwrap())
@@ -839,7 +822,9 @@ impl Environment {
pub fn dump_ast(&self, source: &str) -> Result<String, String> {
self.preload_dependencies(source, None)?;
let compiled = self.compile(source).into_result()?;
let mut parser = Parser::new(source);
let syntax_ast = parser.parse_expression();
let compiled = self.compile_syntax(syntax_ast)?;
let linked = self.link(compiled);
Ok(Dumper::dump(&linked))
}
@@ -983,12 +968,7 @@ impl Environment {
let retyped_ast = checker.check_node_as_bound(func_template.ty.original.as_ref(), arg_types, &mut diag);
if diag.has_errors() {
return Err(diag
.items
.into_iter()
.map(|d| d.message)
.collect::<Vec<_>>()
.join("\n"));
return Err(diag.format_errors());
}
let analyzed = Analyzer::analyze(&retyped_ast, &root_purity.borrow());