pub struct Diagnostic { /* private fields */ }Expand description
One message a stage reports about the program under compilation.
A diagnostic pairs a Severity with a human-readable message. Stages create
diagnostics and hand them to the Session, which stores them
in emission order and keeps a running count of how many were errors. The driver
owns policy — whether to keep going after an error, when to stop — while the
diagnostic is just the record of what happened.
The message is a Cow<'static, str>: a fixed message
("unexpected end of input") is stored as a borrowed &'static str with no
allocation, while a computed one (format!("unknown name {name}")) is owned
only when it is actually built. This keeps the common “constant message” path
allocation-free.
This type is deliberately small — a severity and a message, nothing else. driver-lang does not model spans, error codes, or suggestions: those belong to a dedicated diagnostics crate that a language can plug in as its own concern. The driver only needs enough to route messages and decide when to abort.
§Examples
use driver_lang::{Diagnostic, Severity};
let d = Diagnostic::error("unexpected `}`");
assert_eq!(d.severity(), Severity::Error);
assert_eq!(d.message(), "unexpected `}`");
assert!(d.is_error());Implementations§
Source§impl Diagnostic
impl Diagnostic
Sourcepub fn new(severity: Severity, message: impl Into<Cow<'static, str>>) -> Self
pub fn new(severity: Severity, message: impl Into<Cow<'static, str>>) -> Self
Create a diagnostic with an explicit Severity.
The message accepts a string literal (stored without allocation) or an
owned String (a computed message). Prefer the
error, warning, and note
shorthands when the severity is known at the call site.
§Examples
use driver_lang::{Diagnostic, Severity};
let from_literal = Diagnostic::new(Severity::Note, "defined here");
let from_owned = Diagnostic::new(Severity::Note, String::from("defined here"));
assert_eq!(from_literal, from_owned);Sourcepub fn message(&self) -> &str
pub fn message(&self) -> &str
The message text.
§Examples
use driver_lang::Diagnostic;
assert_eq!(Diagnostic::error("boom").message(), "boom");Sourcepub fn is_error(&self) -> bool
pub fn is_error(&self) -> bool
Whether this diagnostic’s severity is Error.
A shorthand for self.severity().is_error(), the same test the
Session uses to decide whether a diagnostic counts
toward its error total.
§Examples
use driver_lang::Diagnostic;
assert!(Diagnostic::error("boom").is_error());
assert!(!Diagnostic::warning("hmm").is_error());Trait Implementations§
Source§impl Clone for Diagnostic
impl Clone for Diagnostic
Source§fn clone(&self) -> Diagnostic
fn clone(&self) -> Diagnostic
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for Diagnostic
impl Debug for Diagnostic
Source§impl Display for Diagnostic
impl Display for Diagnostic
impl Eq for Diagnostic
Source§impl PartialEq for Diagnostic
impl PartialEq for Diagnostic
Source§fn eq(&self, other: &Diagnostic) -> bool
fn eq(&self, other: &Diagnostic) -> bool
self and other values to be equal, and is used by ==.