Expand description
§diag_lang
The diagnostics layer of a compiler front-end: it collects diagnostics — a severity, a message, and a labelled span — and renders them with source context, drawing a caret underline exactly under the characters at fault, in the style developers expect from a modern toolchain.
It owns presentation only. A lexer, parser, or type checker decides what is
wrong and builds a Diagnostic; diag-lang decides how it looks. Positions
come from span_lang and source text from source_lang: a Label
points with a Span in a SourceMap’s global position space, and the
Renderer resolves that span back to a file, a line, and a column.
§Alignment
The caret line is aligned in display columns, not bytes: a multi-byte UTF-8 character counts as one column, and a tab advances to the next tab stop. The same diagnostic always renders byte-identical output, and a span that falls outside the loaded sources renders a defined note rather than panicking.
§Quickstart
use diag_lang::{Diagnostic, Label, Renderer, Severity};
use diag_lang::{SourceMap, Span};
let mut map = SourceMap::new();
map.add("main.rs", "fn main() {\n let x = foo();\n}\n").expect("fits");
let diag = Diagnostic::new(
Severity::Error,
"cannot find value `foo` in this scope",
Label::new(Span::new(24, 27), "not found in this scope"),
);
let rendered = Renderer::new().render(&diag, &map);
assert!(rendered.contains("^^^ not found in this scope"));§Output
Renderer::render returns a String; Renderer::render_to writes into
any core::fmt::Write sink, so the same renderer feeds a terminal, a reused
buffer, or a formatter without an intermediate allocation. With the color
feature, Renderer::with_color wraps the output in ANSI styling; the plain
output is complete and identically aligned, so colour only ever adds.
§Features
std(default) — the standard library; without it the crate isno_std(it always needsalloc). Forwards tospan-lang/stdandsource-lang/std.color(default) — ANSI styling viaRenderer::with_color. Disable it, or simply never callwith_color, for plain output.
§Stability
The public API is stable as of 1.0 and follows Semantic Versioning: no
breaking changes before 2.0, additions arrive in minor releases, and the MSRV
(Rust 1.85) only rises in a minor. The full surface is catalogued in
docs/API.md.
Structs§
- Diagnostic
- One thing a compiler stage has to say about the source: a
Severity, a headline message, a primaryLabelpointing at the span it concerns, and optionally secondary labels for related locations and trailing note/help lines. - Label
- A
Spanplus the text that explains what is at that span. - Renderer
- Renders a
Diagnosticagainst aSourceMapinto aligned, caret-annotated text in the style of a modern toolchain. - Source
Map - A collection of sources laid out end to end in a single global position space.
- Span
- A half-open byte range
start..endinto a single source.
Enums§
- Severity
- The level a diagnostic reports at — how serious it is and how it is labelled.