Skip to main content

Diagnostic

Struct Diagnostic 

Source
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

Source

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);
Source

pub fn error(message: impl Into<Cow<'static, str>>) -> Self

Create an Error diagnostic — a hard failure that counts toward the session’s error total.

§Examples
use driver_lang::Diagnostic;

let d = Diagnostic::error("type mismatch");
assert!(d.is_error());
Source

pub fn warning(message: impl Into<Cow<'static, str>>) -> Self

Create a Warning diagnostic — recorded and shown, but it does not by itself stop the build.

§Examples
use driver_lang::Diagnostic;

let d = Diagnostic::warning("unused variable `x`");
assert!(!d.is_error());
Source

pub fn note(message: impl Into<Cow<'static, str>>) -> Self

Create a Note diagnostic — neutral context attached to the output.

§Examples
use driver_lang::Diagnostic;

let d = Diagnostic::note("`main` is defined here");
assert!(!d.is_error());
Source

pub fn severity(&self) -> Severity

The Severity of this diagnostic.

Source

pub fn message(&self) -> &str

The message text.

§Examples
use driver_lang::Diagnostic;

assert_eq!(Diagnostic::error("boom").message(), "boom");
Source

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

Source§

fn clone(&self) -> Diagnostic

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Diagnostic

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for Diagnostic

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Renders as severity: message, e.g. error: unexpected }``.

Source§

impl Eq for Diagnostic

Source§

impl PartialEq for Diagnostic

Source§

fn eq(&self, other: &Diagnostic) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Serialize for Diagnostic

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl StructuralPartialEq for Diagnostic

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.