Diagnostics Type
Diagnostics newtype wraps codespan_reporting::diagnostic::Diagnostic and is registered as a Salsa accumulator.
Diagnostic Type Alias
SourceFile as the file ID.
Accumulation Pattern
Instead of returningResult<T, E> and short-circuiting on errors, MCC stages accumulate diagnostics and continue processing when possible.
Emitting Diagnostics
Within a tracked Salsa function, emit diagnostics using theaccumulate method:
Retrieving Diagnostics
After calling a pipeline stage, retrieve accumulated diagnostics:accumulated method:
mcc::parse::accumulated::<Diagnostics>(&db, file)mcc::lowering::lower_stage_diagnostics(&db, file)(convenience wrapper)mcc::codegen::generate_assembly::accumulated::<Diagnostics>(&db, tacky)mcc::render_program::accumulated::<Diagnostics>(&db, program, target)
DiagnosticExt Trait
accumulate method on Diagnostic that wraps it in Diagnostics and accumulates it.
Rendering Diagnostics
Usecodespan-reporting to render diagnostics to a terminal or file:
Files Type
Files type implements codespan_reporting::files::Files and maps SourceFile IDs to their contents for rendering.
Diagnostic Structure
Diagnostics consist of:- Severity:
Error,Warning,Note,Help, orBug - Code: Optional error code (e.g.,
E0001) - Message: Primary message
- Labels: Source locations with annotations
- Notes: Additional free-form text
Example: Creating a Diagnostic
Accumulator Internals
The#[salsa::accumulator] attribute generates infrastructure to collect values during query execution:
.accumulate(db), Salsa stores the diagnostic in a thread-local accumulator. After the query completes, you retrieve all accumulated values with stage::accumulated::<Diagnostics>(&db, ...).
Deref Implementation
Deref implementation allows treating &Diagnostics as &Diagnostic:
Multi-Stage Diagnostics
Collect diagnostics from multiple stages:Error Codes
MCC defines error codes in thecodes module:
Example: Complete Error Flow
Best Practices
Don't panic on errors
Don't panic on errors
Use the accumulator pattern instead of panicking or returning
Result. This allows collecting multiple errors in a single pass.Provide helpful labels
Provide helpful labels
Use
Label::primary for the main error location and Label::secondary for related context:Add notes for guidance
Add notes for guidance
Include actionable suggestions in notes:
Related
API Overview
See how diagnostics fit into the overall API
Database
Learn about Salsa accumulators
Callbacks
Handle diagnostics in the driver callbacks
codespan-reporting
External documentation for diagnostic rendering