diff --git a/crates/interface/src/diagnostics/builder.rs b/crates/interface/src/diagnostics/builder.rs index a3adb863..fe993cca 100644 --- a/crates/interface/src/diagnostics/builder.rs +++ b/crates/interface/src/diagnostics/builder.rs @@ -1,6 +1,6 @@ use super::{ - BugAbort, DiagCtxt, Diagnostic, DiagnosticId, DiagnosticMessage, ErrorGuaranteed, ExplicitBug, - FatalAbort, Level, MultiSpan, Style, + BugAbort, Diag, DiagCtxt, DiagId, DiagMsg, ErrorGuaranteed, ExplicitBug, FatalAbort, Level, + MultiSpan, Style, }; use crate::Span; use solar_data_structures::Never; @@ -12,32 +12,32 @@ use std::{ panic::Location, }; -/// Trait for types that `DiagnosticBuilder::emit` can return as a "guarantee" (or "proof") token +/// Trait for types that `DiagBuilder::emit` can return as a "guarantee" (or "proof") token /// that the emission happened. pub trait EmissionGuarantee: Sized { /// This exists so that bugs and fatal errors can both result in `!` (an abort) when emitted, /// but have different aborting behaviour. type EmitResult; - /// Implementation of `DiagnosticBuilder::emit`, fully controlled by each `impl` of + /// Implementation of `DiagBuilder::emit`, fully controlled by each `impl` of /// `EmissionGuarantee`, to make it impossible to create a value of `Self::EmitResult` without /// actually performing the emission. #[track_caller] - fn emit_producing_guarantee(db: &mut DiagnosticBuilder<'_, Self>) -> Self::EmitResult; + fn emit_producing_guarantee(db: &mut DiagBuilder<'_, Self>) -> Self::EmitResult; } impl EmissionGuarantee for ErrorGuaranteed { type EmitResult = Self; - fn emit_producing_guarantee(db: &mut DiagnosticBuilder<'_, Self>) -> Self::EmitResult { + fn emit_producing_guarantee(db: &mut DiagBuilder<'_, Self>) -> Self::EmitResult { let guar = db.emit_producing_error_guaranteed(); // Only allow a guarantee if the `level` wasn't switched to a - // non-error - the field isn't `pub`, but the whole `Diagnostic` + // non-error - the field isn't `pub`, but the whole `Diag` // can be overwritten with a new one, thanks to `DerefMut`. assert!( db.diagnostic.is_error(), - "emitted non-error ({:?}) diagnostic from `DiagnosticBuilder`", + "emitted non-error ({:?}) diagnostic from `DiagBuilder`", db.diagnostic.level, ); @@ -48,7 +48,7 @@ impl EmissionGuarantee for ErrorGuaranteed { impl EmissionGuarantee for () { type EmitResult = Self; - fn emit_producing_guarantee(db: &mut DiagnosticBuilder<'_, Self>) -> Self::EmitResult { + fn emit_producing_guarantee(db: &mut DiagBuilder<'_, Self>) -> Self::EmitResult { db.emit_producing_nothing(); } } @@ -56,7 +56,7 @@ impl EmissionGuarantee for () { impl EmissionGuarantee for BugAbort { type EmitResult = Never; - fn emit_producing_guarantee(db: &mut DiagnosticBuilder<'_, Self>) -> Self::EmitResult { + fn emit_producing_guarantee(db: &mut DiagBuilder<'_, Self>) -> Self::EmitResult { db.emit_producing_nothing(); std::panic::panic_any(ExplicitBug); } @@ -65,7 +65,7 @@ impl EmissionGuarantee for BugAbort { impl EmissionGuarantee for FatalAbort { type EmitResult = Never; - fn emit_producing_guarantee(db: &mut DiagnosticBuilder<'_, Self>) -> Self::EmitResult { + fn emit_producing_guarantee(db: &mut DiagBuilder<'_, Self>) -> Self::EmitResult { db.emit_producing_nothing(); std::panic::panic_any(Self); } @@ -76,33 +76,33 @@ impl EmissionGuarantee for FatalAbort { /// **Note:** Incorrect usage of this type results in a panic when dropped. /// This is to ensure that all errors are either emitted or cancelled. #[must_use = "diagnostics must be emitted or cancelled"] -pub struct DiagnosticBuilder<'a, G: EmissionGuarantee> { +pub struct DiagBuilder<'a, G: EmissionGuarantee> { dcx: &'a DiagCtxt, - /// `Diagnostic` is a large type, and `DiagnosticBuilder` is often used as a + /// `Diag` is a large type, and `DiagBuilder` is often used as a /// return value, especially within the frequently-used `PResult` type. /// In theory, return value optimization (RVO) should avoid unnecessary /// copying. In practice, it does not (at the time of writing). - diagnostic: Box, + diagnostic: Box, _marker: PhantomData, } -impl Clone for DiagnosticBuilder<'_, G> { +impl Clone for DiagBuilder<'_, G> { #[inline] fn clone(&self) -> Self { Self { dcx: self.dcx, diagnostic: self.diagnostic.clone(), _marker: PhantomData } } } -impl fmt::Debug for DiagnosticBuilder<'_, G> { +impl fmt::Debug for DiagBuilder<'_, G> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { self.diagnostic.fmt(f) } } -impl Deref for DiagnosticBuilder<'_, G> { - type Target = Diagnostic; +impl Deref for DiagBuilder<'_, G> { + type Target = Diag; #[inline] fn deref(&self) -> &Self::Target { @@ -110,21 +110,21 @@ impl Deref for DiagnosticBuilder<'_, G> { } } -impl DerefMut for DiagnosticBuilder<'_, G> { +impl DerefMut for DiagBuilder<'_, G> { #[inline] fn deref_mut(&mut self) -> &mut Self::Target { &mut self.diagnostic } } -impl Drop for DiagnosticBuilder<'_, G> { +impl Drop for DiagBuilder<'_, G> { #[track_caller] fn drop(&mut self) { if std::thread::panicking() { return; } - let _ = self.dcx.emit_diagnostic(Diagnostic::new( + let _ = self.dcx.emit_diagnostic(Diag::new( Level::Bug, "the following error was constructed but not emitted", )); @@ -133,11 +133,11 @@ impl Drop for DiagnosticBuilder<'_, G> { } } -impl<'a, G: EmissionGuarantee> DiagnosticBuilder<'a, G> { - /// Creates a new `DiagnosticBuilder`. +impl<'a, G: EmissionGuarantee> DiagBuilder<'a, G> { + /// Creates a new `DiagBuilder`. #[track_caller] - pub fn new>(dcx: &'a DiagCtxt, level: Level, msg: M) -> Self { - Self { dcx, diagnostic: Box::new(Diagnostic::new(level, msg)), _marker: PhantomData } + pub fn new>(dcx: &'a DiagCtxt, level: Level, msg: M) -> Self { + Self { dcx, diagnostic: Box::new(Diag::new(level, msg)), _marker: PhantomData } } /// Returns the [`DiagCtxt`]. @@ -178,7 +178,7 @@ impl<'a, G: EmissionGuarantee> DiagnosticBuilder<'a, G> { } } -/// Forwards methods to [`Diagnostic`]. +/// Forwards methods to [`Diag`]. macro_rules! forward { ( $( @@ -188,7 +188,7 @@ macro_rules! forward { ) => { $( $(#[$attrs])* - #[doc = concat!("See [`Diagnostic::", stringify!($n), "()`].")] + #[doc = concat!("See [`Diag::", stringify!($n), "()`].")] $vis fn $n(mut self, $($name: $ty),*) -> Self { self.diagnostic.$n($($name),*); self @@ -197,27 +197,27 @@ macro_rules! forward { }; } -/// Forwarded methods to [`Diagnostic`]. -impl DiagnosticBuilder<'_, G> { +/// Forwarded methods to [`Diag`]. +impl DiagBuilder<'_, G> { forward! { pub fn span(span: impl Into); - pub fn code(code: impl Into); + pub fn code(code: impl Into); - pub fn span_label(span: Span, label: impl Into); - pub fn span_labels(spans: impl IntoIterator, label: impl Into); + pub fn span_label(span: Span, label: impl Into); + pub fn span_labels(spans: impl IntoIterator, label: impl Into); - pub fn warn(msg: impl Into); - pub fn span_warn(span: impl Into, msg: impl Into); + pub fn warn(msg: impl Into); + pub fn span_warn(span: impl Into, msg: impl Into); - pub fn note(msg: impl Into); - pub fn span_note(span: impl Into, msg: impl Into); - pub fn highlighted_note(messages: Vec<(impl Into, Style)>); - pub fn note_once(msg: impl Into); - pub fn span_note_once(span: impl Into, msg: impl Into); + pub fn note(msg: impl Into); + pub fn span_note(span: impl Into, msg: impl Into); + pub fn highlighted_note(messages: Vec<(impl Into, Style)>); + pub fn note_once(msg: impl Into); + pub fn span_note_once(span: impl Into, msg: impl Into); - pub fn help(msg: impl Into); - pub fn help_once(msg: impl Into); - pub fn highlighted_help(messages: Vec<(impl Into, Style)>); - pub fn span_help(span: impl Into, msg: impl Into); + pub fn help(msg: impl Into); + pub fn help_once(msg: impl Into); + pub fn highlighted_help(messages: Vec<(impl Into, Style)>); + pub fn span_help(span: impl Into, msg: impl Into); } } diff --git a/crates/interface/src/diagnostics/context.rs b/crates/interface/src/diagnostics/context.rs index 2adc86cf..758aac59 100644 --- a/crates/interface/src/diagnostics/context.rs +++ b/crates/interface/src/diagnostics/context.rs @@ -1,7 +1,6 @@ use super::{ - emitter::HumanEmitter, BugAbort, Diagnostic, DiagnosticBuilder, DiagnosticMessage, DynEmitter, - EmissionGuarantee, EmittedDiagnostics, ErrorGuaranteed, FatalAbort, HumanBufferEmitter, Level, - SilentEmitter, + emitter::HumanEmitter, BugAbort, Diag, DiagBuilder, DiagMsg, DynEmitter, EmissionGuarantee, + EmittedDiagnostics, ErrorGuaranteed, FatalAbort, HumanBufferEmitter, Level, SilentEmitter, }; use crate::{Result, SourceMap}; use anstream::ColorChoice; @@ -142,17 +141,17 @@ impl DiagCtxt { /// Emits the given diagnostic with this context. #[inline] - pub fn emit_diagnostic(&self, mut diagnostic: Diagnostic) -> Result<(), ErrorGuaranteed> { + pub fn emit_diagnostic(&self, mut diagnostic: Diag) -> Result<(), ErrorGuaranteed> { self.emit_diagnostic_without_consuming(&mut diagnostic) } /// Emits the given diagnostic with this context, without consuming the diagnostic. /// - /// **Note:** This function is intended to be used only internally in `DiagnosticBuilder`. + /// **Note:** This function is intended to be used only internally in `DiagBuilder`. /// Use [`emit_diagnostic`](Self::emit_diagnostic) instead. pub(super) fn emit_diagnostic_without_consuming( &self, - diagnostic: &mut Diagnostic, + diagnostic: &mut Diag, ) -> Result<(), ErrorGuaranteed> { self.inner.lock().emit_diagnostic_without_consuming(diagnostic) } @@ -196,35 +195,35 @@ impl DiagCtxt { } } -/// Diagnostic constructors. +/// Diag constructors. /// -/// Note that methods returning a [`DiagnosticBuilder`] must also marked with `#[track_caller]`. +/// Note that methods returning a [`DiagBuilder`] must also marked with `#[track_caller]`. impl DiagCtxt { /// Creates a builder at the given `level` with the given `msg`. #[track_caller] pub fn diag( &self, level: Level, - msg: impl Into, - ) -> DiagnosticBuilder<'_, G> { - DiagnosticBuilder::new(self, level, msg) + msg: impl Into, + ) -> DiagBuilder<'_, G> { + DiagBuilder::new(self, level, msg) } /// Creates a builder at the `Bug` level with the given `msg`. #[track_caller] - pub fn bug(&self, msg: impl Into) -> DiagnosticBuilder<'_, BugAbort> { + pub fn bug(&self, msg: impl Into) -> DiagBuilder<'_, BugAbort> { self.diag(Level::Bug, msg) } /// Creates a builder at the `Fatal` level with the given `msg`. #[track_caller] - pub fn fatal(&self, msg: impl Into) -> DiagnosticBuilder<'_, FatalAbort> { + pub fn fatal(&self, msg: impl Into) -> DiagBuilder<'_, FatalAbort> { self.diag(Level::Fatal, msg) } /// Creates a builder at the `Error` level with the given `msg`. #[track_caller] - pub fn err(&self, msg: impl Into) -> DiagnosticBuilder<'_, ErrorGuaranteed> { + pub fn err(&self, msg: impl Into) -> DiagBuilder<'_, ErrorGuaranteed> { self.diag(Level::Error, msg) } @@ -232,31 +231,31 @@ impl DiagCtxt { /// /// Attempting to `.emit()` the builder will only emit if `can_emit_warnings` is `true`. #[track_caller] - pub fn warn(&self, msg: impl Into) -> DiagnosticBuilder<'_, ()> { + pub fn warn(&self, msg: impl Into) -> DiagBuilder<'_, ()> { self.diag(Level::Warning, msg) } /// Creates a builder at the `Help` level with the given `msg`. #[track_caller] - pub fn help(&self, msg: impl Into) -> DiagnosticBuilder<'_, ()> { + pub fn help(&self, msg: impl Into) -> DiagBuilder<'_, ()> { self.diag(Level::Help, msg) } /// Creates a builder at the `Note` level with the given `msg`. #[track_caller] - pub fn note(&self, msg: impl Into) -> DiagnosticBuilder<'_, ()> { + pub fn note(&self, msg: impl Into) -> DiagBuilder<'_, ()> { self.diag(Level::Note, msg) } } impl DiagCtxtInner { - fn emit_diagnostic(&mut self, mut diagnostic: Diagnostic) -> Result<(), ErrorGuaranteed> { + fn emit_diagnostic(&mut self, mut diagnostic: Diag) -> Result<(), ErrorGuaranteed> { self.emit_diagnostic_without_consuming(&mut diagnostic) } fn emit_diagnostic_without_consuming( &mut self, - diagnostic: &mut Diagnostic, + diagnostic: &mut Diag, ) -> Result<(), ErrorGuaranteed> { if diagnostic.level == Level::Warning && !self.flags.can_emit_warnings { return Ok(()); @@ -325,11 +324,11 @@ impl DiagCtxtInner { match (self.deduplicated_err_count, self.deduplicated_warn_count) { (0, 0) => Ok(()), (0, w) => { - self.emitter.emit_diagnostic(&Diagnostic::new(Level::Warning, warnings(w))); + self.emitter.emit_diagnostic(&Diag::new(Level::Warning, warnings(w))); Ok(()) } - (e, 0) => self.emit_diagnostic(Diagnostic::new(Level::Error, errors(e))), - (e, w) => self.emit_diagnostic(Diagnostic::new( + (e, 0) => self.emit_diagnostic(Diag::new(Level::Error, errors(e))), + (e, w) => self.emit_diagnostic(Diag::new( Level::Error, format!("{}; {}", errors(e), warnings(w)), )), diff --git a/crates/interface/src/diagnostics/emitter/human.rs b/crates/interface/src/diagnostics/emitter/human.rs index c09aa486..cfe06bc5 100644 --- a/crates/interface/src/diagnostics/emitter/human.rs +++ b/crates/interface/src/diagnostics/emitter/human.rs @@ -1,4 +1,4 @@ -use super::{io_panic, rustc::FileWithAnnotatedLines, Diagnostic, Emitter}; +use super::{io_panic, rustc::FileWithAnnotatedLines, Diag, Emitter}; use crate::{ diagnostics::{Level, MultiSpan, Style, SubDiagnostic}, source_map::SourceFile, @@ -27,7 +27,7 @@ const DEFAULT_RENDERER: Renderer = Renderer::plain() .emphasis(anstyle::Style::new().bold()) .none(anstyle::Style::new()); -/// Diagnostic emitter that emits to an arbitrary [`io::Write`] writer in human-readable format. +/// Diag emitter that emits to an arbitrary [`io::Write`] writer in human-readable format. pub struct HumanEmitter { writer_type_id: std::any::TypeId, real_writer: *mut Writer, @@ -40,7 +40,7 @@ pub struct HumanEmitter { unsafe impl Send for HumanEmitter {} impl Emitter for HumanEmitter { - fn emit_diagnostic(&mut self, diagnostic: &Diagnostic) { + fn emit_diagnostic(&mut self, diagnostic: &Diag) { self.snippet(diagnostic, |this, snippet| { writeln!(this.writer, "{}\n", this.renderer.render(snippet))?; this.writer.flush() @@ -156,11 +156,7 @@ impl HumanEmitter { } /// Formats the given `diagnostic` into a [`Message`] suitable for use with the renderer. - fn snippet( - &mut self, - diagnostic: &Diagnostic, - f: impl FnOnce(&mut Self, Message<'_>) -> R, - ) -> R { + fn snippet(&mut self, diagnostic: &Diag, f: impl FnOnce(&mut Self, Message<'_>) -> R) -> R { // Current format (annotate-snippets 0.10.0) (comments in <...>): /* title.level[title.id]: title.label @@ -204,14 +200,14 @@ impl HumanEmitter { } } -/// Diagnostic emitter that emits diagnostics in human-readable format to a local buffer. +/// Diag emitter that emits diagnostics in human-readable format to a local buffer. pub struct HumanBufferEmitter { inner: HumanEmitter, } impl Emitter for HumanBufferEmitter { #[inline] - fn emit_diagnostic(&mut self, diagnostic: &Diagnostic) { + fn emit_diagnostic(&mut self, diagnostic: &Diag) { self.inner.emit_diagnostic(diagnostic); } @@ -282,7 +278,7 @@ struct OwnedMessage { } impl OwnedMessage { - fn from_diagnostic(diag: &Diagnostic) -> Self { + fn from_diagnostic(diag: &Diag) -> Self { Self { id: diag.id(), label: diag.label().into_owned(), level: to_as_level(diag.level) } } @@ -322,7 +318,7 @@ struct OwnedSnippet { } impl OwnedSnippet { - fn collect(sm: &SourceMap, diagnostic: &Diagnostic) -> Vec { + fn collect(sm: &SourceMap, diagnostic: &Diag) -> Vec { // Collect main diagnostic. let mut files = Self::collect_files(sm, &diagnostic.span); files.iter_mut().for_each(|file| file.set_level(diagnostic.level)); diff --git a/crates/interface/src/diagnostics/emitter/json.rs b/crates/interface/src/diagnostics/emitter/json.rs index 1c68ea52..5b905bb5 100644 --- a/crates/interface/src/diagnostics/emitter/json.rs +++ b/crates/interface/src/diagnostics/emitter/json.rs @@ -8,7 +8,7 @@ use anstream::ColorChoice; use serde::Serialize; use std::{io, sync::Arc}; -/// Diagnostic emitter that emits diagnostics as JSON. +/// Diag emitter that emits diagnostics as JSON. pub struct JsonEmitter { writer: Box, pretty: bool, @@ -18,10 +18,10 @@ pub struct JsonEmitter { } impl Emitter for JsonEmitter { - fn emit_diagnostic(&mut self, diagnostic: &crate::diagnostics::Diagnostic) { + fn emit_diagnostic(&mut self, diagnostic: &crate::diagnostics::Diag) { if self.rustc_like { let diagnostic = self.diagnostic(diagnostic); - self.emit(&EmitTyped::Diagnostic(diagnostic)) + self.emit(&EmitTyped::Diag(diagnostic)) } else { let diagnostic = self.solc_diagnostic(diagnostic); self.emit(&diagnostic) @@ -69,8 +69,8 @@ impl JsonEmitter { Emitter::source_map(self).unwrap() } - fn diagnostic(&mut self, diagnostic: &crate::diagnostics::Diagnostic) -> Diagnostic { - Diagnostic { + fn diagnostic(&mut self, diagnostic: &crate::diagnostics::Diag) -> Diag { + Diag { message: diagnostic.label().into_owned(), code: diagnostic.id().map(|code| DiagnosticCode { code, explanation: None }), level: diagnostic.level.to_str(), @@ -80,8 +80,8 @@ impl JsonEmitter { } } - fn sub_diagnostic(&self, diagnostic: &crate::diagnostics::SubDiagnostic) -> Diagnostic { - Diagnostic { + fn sub_diagnostic(&self, diagnostic: &crate::diagnostics::SubDiagnostic) -> Diag { + Diag { message: diagnostic.label().into_owned(), code: None, level: diagnostic.level.to_str(), @@ -128,7 +128,7 @@ impl JsonEmitter { } } - fn solc_diagnostic(&mut self, diagnostic: &crate::diagnostics::Diagnostic) -> SolcDiagnostic { + fn solc_diagnostic(&mut self, diagnostic: &crate::diagnostics::Diag) -> SolcDiagnostic { let primary = diagnostic.span.primary_span(); let file = primary .map(|span| { @@ -194,7 +194,7 @@ impl JsonEmitter { } } - fn emit_diagnostic_to_buffer(&mut self, diagnostic: &crate::diagnostics::Diagnostic) -> String { + fn emit_diagnostic_to_buffer(&mut self, diagnostic: &crate::diagnostics::Diag) -> String { self.human_emitter.emit_diagnostic(diagnostic); std::mem::take(self.human_emitter.buffer_mut()) } @@ -215,11 +215,11 @@ impl JsonEmitter { #[derive(Serialize)] #[serde(tag = "$message_type", rename_all = "snake_case")] enum EmitTyped { - Diagnostic(Diagnostic), + Diag(Diag), } #[derive(Serialize)] -struct Diagnostic { +struct Diag { /// The primary error message. message: String, code: Option, @@ -227,7 +227,7 @@ struct Diagnostic { level: &'static str, spans: Vec, /// Associated diagnostic messages. - children: Vec, + children: Vec, /// The message as the compiler would render it. rendered: Option, } diff --git a/crates/interface/src/diagnostics/emitter/mod.rs b/crates/interface/src/diagnostics/emitter/mod.rs index e00b5012..74e14646 100644 --- a/crates/interface/src/diagnostics/emitter/mod.rs +++ b/crates/interface/src/diagnostics/emitter/mod.rs @@ -1,4 +1,4 @@ -use super::{DiagCtxt, Diagnostic, Level}; +use super::{Diag, DiagCtxt, Level}; use crate::SourceMap; use std::{any::Any, sync::Arc}; @@ -15,10 +15,10 @@ mod rustc; /// Dynamic diagnostic emitter. See [`Emitter`]. pub type DynEmitter = dyn Emitter + Send; -/// Diagnostic emitter. +/// Diag emitter. pub trait Emitter: Any { /// Emits a diagnostic. - fn emit_diagnostic(&mut self, diagnostic: &Diagnostic); + fn emit_diagnostic(&mut self, diagnostic: &Diag); /// Returns a reference to the source map, if any. #[inline] @@ -48,7 +48,7 @@ impl DynEmitter { } } -/// Diagnostic emitter that only emits fatal diagnostics. +/// Diag emitter that only emits fatal diagnostics. pub struct SilentEmitter { fatal_dcx: DiagCtxt, note: Option, @@ -68,7 +68,7 @@ impl SilentEmitter { } impl Emitter for SilentEmitter { - fn emit_diagnostic(&mut self, diagnostic: &Diagnostic) { + fn emit_diagnostic(&mut self, diagnostic: &Diag) { if diagnostic.level != Level::Fatal { return; } @@ -81,10 +81,10 @@ impl Emitter for SilentEmitter { } } -/// Diagnostic emitter that only stores emitted diagnostics. +/// Diag emitter that only stores emitted diagnostics. #[derive(Clone, Debug)] pub struct LocalEmitter { - diagnostics: Vec, + diagnostics: Vec, } impl Default for LocalEmitter { @@ -100,18 +100,18 @@ impl LocalEmitter { } /// Returns a reference to the emitted diagnostics. - pub fn diagnostics(&self) -> &[Diagnostic] { + pub fn diagnostics(&self) -> &[Diag] { &self.diagnostics } /// Consumes the emitter and returns the emitted diagnostics. - pub fn into_diagnostics(self) -> Vec { + pub fn into_diagnostics(self) -> Vec { self.diagnostics } } impl Emitter for LocalEmitter { - fn emit_diagnostic(&mut self, diagnostic: &Diagnostic) { + fn emit_diagnostic(&mut self, diagnostic: &Diag) { self.diagnostics.push(diagnostic.clone()); } } diff --git a/crates/interface/src/diagnostics/message.rs b/crates/interface/src/diagnostics/message.rs index 2674da95..51e5b3a0 100644 --- a/crates/interface/src/diagnostics/message.rs +++ b/crates/interface/src/diagnostics/message.rs @@ -4,29 +4,29 @@ use crate::Span; use std::borrow::Cow; #[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] -pub struct DiagnosticMessage { +pub struct DiagMsg { inner: Cow<'static, str>, } -impl From<&'static str> for DiagnosticMessage { +impl From<&'static str> for DiagMsg { fn from(value: &'static str) -> Self { Self { inner: Cow::Borrowed(value) } } } -impl From for DiagnosticMessage { +impl From for DiagMsg { fn from(value: String) -> Self { Self { inner: Cow::Owned(value) } } } -impl From> for DiagnosticMessage { +impl From> for DiagMsg { fn from(value: Cow<'static, str>) -> Self { Self { inner: value } } } -impl DiagnosticMessage { +impl DiagMsg { /// Returns the message as a string. #[inline] pub fn as_str(&self) -> &str { @@ -45,7 +45,7 @@ pub struct SpanLabel { pub is_primary: bool, /// What label should we attach to this span (if any)? - pub label: Option, + pub label: Option, } /// A collection of `Span`s. @@ -58,7 +58,7 @@ pub struct SpanLabel { #[derive(Clone, Debug, PartialEq, Eq, Hash)] pub struct MultiSpan { primary_spans: Vec, - span_labels: Vec<(Span, DiagnosticMessage)>, + span_labels: Vec<(Span, DiagMsg)>, } impl MultiSpan { @@ -76,7 +76,7 @@ impl MultiSpan { Self { primary_spans: vec, span_labels: vec![] } } - pub fn push_span_label(&mut self, span: Span, label: impl Into) { + pub fn push_span_label(&mut self, span: Span, label: impl Into) { self.span_labels.push((span, label.into())); } @@ -119,7 +119,7 @@ impl MultiSpan { replacements_occurred } - pub fn pop_span_label(&mut self) -> Option<(Span, DiagnosticMessage)> { + pub fn pop_span_label(&mut self) -> Option<(Span, DiagMsg)> { self.span_labels.pop() } diff --git a/crates/interface/src/diagnostics/mod.rs b/crates/interface/src/diagnostics/mod.rs index 182c0531..197ec731 100644 --- a/crates/interface/src/diagnostics/mod.rs +++ b/crates/interface/src/diagnostics/mod.rs @@ -7,7 +7,7 @@ use anstyle::{AnsiColor, Color}; use std::{borrow::Cow, fmt, panic::Location}; mod builder; -pub use builder::{DiagnosticBuilder, EmissionGuarantee}; +pub use builder::{DiagBuilder, EmissionGuarantee}; mod context; pub use context::{DiagCtxt, DiagCtxtFlags}; @@ -20,7 +20,7 @@ pub use emitter::{ }; mod message; -pub use message::{DiagnosticMessage, MultiSpan, SpanLabel}; +pub use message::{DiagMsg, MultiSpan, SpanLabel}; /// Represents all the diagnostics emitted up to a certain point. /// @@ -81,22 +81,22 @@ pub struct ExplicitBug; /// Marker type which enables implementation of fatal diagnostics. pub struct FatalAbort; -/// Diagnostic ID. +/// Diag ID. /// /// Use [`error_code!`](crate::error_code) to create an error code diagnostic ID. /// /// # Examples /// /// ``` -/// # use solar_interface::{diagnostics::DiagnosticId, error_code}; -/// let id: DiagnosticId = error_code!(1234); +/// # use solar_interface::{diagnostics::DiagId, error_code}; +/// let id: DiagId = error_code!(1234); /// ``` #[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] -pub struct DiagnosticId { +pub struct DiagId { id: u32, } -impl DiagnosticId { +impl DiagId { /// Creates an error code diagnostic ID. /// /// Use [`error_code!`](crate::error_code) instead. @@ -118,17 +118,17 @@ impl DiagnosticId { /// # Examples /// /// ``` -/// # use solar_interface::{diagnostics::DiagnosticId, error_code}; -/// let code: DiagnosticId = error_code!(1234); +/// # use solar_interface::{diagnostics::DiagId, error_code}; +/// let code: DiagId = error_code!(1234); /// ``` #[macro_export] macro_rules! error_code { ($id:literal) => { - const { $crate::diagnostics::DiagnosticId::new_from_macro($id) } + const { $crate::diagnostics::DiagId::new_from_macro($id) } }; } -/// Diagnostic level. +/// Diag level. #[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] pub enum Level { /// For bugs in the compiler. Manifests as an ICE (internal compiler error) panic. @@ -300,7 +300,7 @@ impl Style { #[derive(Clone, Debug, PartialEq, Hash)] pub struct SubDiagnostic { pub level: Level, - pub messages: Vec<(DiagnosticMessage, Style)>, + pub messages: Vec<(DiagMsg, Style)>, pub span: MultiSpan, } @@ -314,39 +314,39 @@ impl SubDiagnostic { /// A compiler diagnostic. #[must_use] #[derive(Clone, Debug)] -pub struct Diagnostic { +pub struct Diag { pub(crate) level: Level, - pub messages: Vec<(DiagnosticMessage, Style)>, + pub messages: Vec<(DiagMsg, Style)>, pub span: MultiSpan, pub children: Vec, - pub code: Option, + pub code: Option, pub created_at: &'static Location<'static>, } -impl PartialEq for Diagnostic { +impl PartialEq for Diag { fn eq(&self, other: &Self) -> bool { self.keys() == other.keys() } } -impl std::hash::Hash for Diagnostic { +impl std::hash::Hash for Diag { fn hash(&self, state: &mut H) { self.keys().hash(state); } } -impl Diagnostic { - /// Creates a new `Diagnostic` with a single message. +impl Diag { + /// Creates a new `Diag` with a single message. #[track_caller] - pub fn new>(level: Level, msg: M) -> Self { + pub fn new>(level: Level, msg: M) -> Self { Self::new_with_messages(level, vec![(msg.into(), Style::NoStyle)]) } - /// Creates a new `Diagnostic` with multiple messages. + /// Creates a new `Diag` with multiple messages. #[track_caller] - pub fn new_with_messages(level: Level, messages: Vec<(DiagnosticMessage, Style)>) -> Self { + pub fn new_with_messages(level: Level, messages: Vec<(DiagMsg, Style)>) -> Self { Self { level, messages, @@ -373,7 +373,7 @@ impl Diagnostic { } /// Returns the messages of this diagnostic. - pub fn messages(&self) -> &[(DiagnosticMessage, Style)] { + pub fn messages(&self) -> &[(DiagMsg, Style)] { &self.messages } @@ -403,7 +403,7 @@ impl Diagnostic { } /// Setters. -impl Diagnostic { +impl Diag { /// Sets the span of this diagnostic. pub fn span(&mut self, span: impl Into) -> &mut Self { self.span = span.into(); @@ -411,7 +411,7 @@ impl Diagnostic { } /// Sets the code of this diagnostic. - pub fn code(&mut self, code: impl Into) -> &mut Self { + pub fn code(&mut self, code: impl Into) -> &mut Self { self.code = Some(code.into()); self } @@ -424,7 +424,7 @@ impl Diagnostic { /// /// This span is *not* considered a ["primary span"][`MultiSpan`]; only /// the `Span` supplied when creating the diagnostic is primary. - pub fn span_label(&mut self, span: Span, label: impl Into) -> &mut Self { + pub fn span_label(&mut self, span: Span, label: impl Into) -> &mut Self { self.span.push_span_label(span, label); self } @@ -434,7 +434,7 @@ impl Diagnostic { pub fn span_labels( &mut self, spans: impl IntoIterator, - label: impl Into, + label: impl Into, ) -> &mut Self { let label = label.into(); for span in spans { @@ -455,93 +455,75 @@ impl Diagnostic { } /// Sub-diagnostics. -impl Diagnostic { +impl Diag { /// Add a warning attached to this diagnostic. - pub fn warn(&mut self, msg: impl Into) -> &mut Self { + pub fn warn(&mut self, msg: impl Into) -> &mut Self { self.sub(Level::Warning, msg, MultiSpan::new()) } /// Prints the span with a warning above it. - /// This is like [`Diagnostic::warn()`], but it gets its own span. - pub fn span_warn( - &mut self, - span: impl Into, - msg: impl Into, - ) -> &mut Self { + /// This is like [`Diag::warn()`], but it gets its own span. + pub fn span_warn(&mut self, span: impl Into, msg: impl Into) -> &mut Self { self.sub(Level::Warning, msg, span) } /// Add a note to this diagnostic. - pub fn note(&mut self, msg: impl Into) -> &mut Self { + pub fn note(&mut self, msg: impl Into) -> &mut Self { self.sub(Level::Note, msg, MultiSpan::new()) } /// Prints the span with a note above it. - /// This is like [`Diagnostic::note()`], but it gets its own span. - pub fn span_note( - &mut self, - span: impl Into, - msg: impl Into, - ) -> &mut Self { + /// This is like [`Diag::note()`], but it gets its own span. + pub fn span_note(&mut self, span: impl Into, msg: impl Into) -> &mut Self { self.sub(Level::Note, msg, span) } - pub fn highlighted_note( - &mut self, - messages: Vec<(impl Into, Style)>, - ) -> &mut Self { + pub fn highlighted_note(&mut self, messages: Vec<(impl Into, Style)>) -> &mut Self { self.sub_with_highlights(Level::Note, messages, MultiSpan::new()) } /// Prints the span with a note above it. - /// This is like [`Diagnostic::note()`], but it gets emitted only once. - pub fn note_once(&mut self, msg: impl Into) -> &mut Self { + /// This is like [`Diag::note()`], but it gets emitted only once. + pub fn note_once(&mut self, msg: impl Into) -> &mut Self { self.sub(Level::OnceNote, msg, MultiSpan::new()) } /// Prints the span with a note above it. - /// This is like [`Diagnostic::note_once()`], but it gets its own span. + /// This is like [`Diag::note_once()`], but it gets its own span. pub fn span_note_once( &mut self, span: impl Into, - msg: impl Into, + msg: impl Into, ) -> &mut Self { self.sub(Level::OnceNote, msg, span) } /// Add a help message attached to this diagnostic. - pub fn help(&mut self, msg: impl Into) -> &mut Self { + pub fn help(&mut self, msg: impl Into) -> &mut Self { self.sub(Level::Help, msg, MultiSpan::new()) } /// Prints the span with a help above it. - /// This is like [`Diagnostic::help()`], but it gets its own span. - pub fn help_once(&mut self, msg: impl Into) -> &mut Self { + /// This is like [`Diag::help()`], but it gets its own span. + pub fn help_once(&mut self, msg: impl Into) -> &mut Self { self.sub(Level::OnceHelp, msg, MultiSpan::new()) } /// Add a help message attached to this diagnostic with a customizable highlighted message. - pub fn highlighted_help( - &mut self, - msgs: Vec<(impl Into, Style)>, - ) -> &mut Self { + pub fn highlighted_help(&mut self, msgs: Vec<(impl Into, Style)>) -> &mut Self { self.sub_with_highlights(Level::Help, msgs, MultiSpan::new()) } /// Prints the span with some help above it. - /// This is like [`Diagnostic::help()`], but it gets its own span. - pub fn span_help( - &mut self, - span: impl Into, - msg: impl Into, - ) -> &mut Self { + /// This is like [`Diag::help()`], but it gets its own span. + pub fn span_help(&mut self, span: impl Into, msg: impl Into) -> &mut Self { self.sub(Level::Help, msg, span) } fn sub( &mut self, level: Level, - msg: impl Into, + msg: impl Into, span: impl Into, ) -> &mut Self { self.children.push(SubDiagnostic { @@ -555,7 +537,7 @@ impl Diagnostic { fn sub_with_highlights( &mut self, level: Level, - messages: Vec<(impl Into, Style)>, + messages: Vec<(impl Into, Style)>, span: MultiSpan, ) -> &mut Self { let messages = messages.into_iter().map(|(m, s)| (m.into(), s)).collect(); @@ -565,7 +547,7 @@ impl Diagnostic { } // TODO: Styles? -fn flatten_messages(messages: &[(DiagnosticMessage, Style)]) -> Cow<'_, str> { +fn flatten_messages(messages: &[(DiagMsg, Style)]) -> Cow<'_, str> { match messages { [] => Cow::Borrowed(""), [(message, _)] => Cow::Borrowed(message.as_str()), diff --git a/crates/parse/src/lib.rs b/crates/parse/src/lib.rs index b78da8a7..df1a1efb 100644 --- a/crates/parse/src/lib.rs +++ b/crates/parse/src/lib.rs @@ -8,7 +8,7 @@ #[macro_use] extern crate tracing; -use solar_interface::diagnostics::{DiagnosticBuilder, ErrorGuaranteed}; +use solar_interface::diagnostics::{DiagBuilder, ErrorGuaranteed}; pub mod lexer; pub use lexer::{unescape, Cursor, Lexer}; @@ -22,7 +22,7 @@ pub use solar_ast::{self as ast, token}; pub use solar_interface as interface; /// Parser error type. -pub type PErr<'a> = DiagnosticBuilder<'a, ErrorGuaranteed>; +pub type PErr<'a> = DiagBuilder<'a, ErrorGuaranteed>; /// Parser result type. This is a shorthand for `Result>`. pub type PResult<'a, T> = Result>; diff --git a/crates/parse/src/parser/item.rs b/crates/parse/src/parser/item.rs index 5dfa534c..2c71a2d1 100644 --- a/crates/parse/src/parser/item.rs +++ b/crates/parse/src/parser/item.rs @@ -2,7 +2,7 @@ use super::{ExpectedToken, SeqSep}; use crate::{PResult, Parser}; use itertools::Itertools; use solar_ast::{token::*, *}; -use solar_interface::{diagnostics::DiagnosticMessage, error_code, kw, sym, Ident, Span}; +use solar_interface::{diagnostics::DiagMsg, error_code, kw, sym, Ident, Span}; impl<'sess, 'ast> Parser<'sess, 'ast> { /// Parses a source unit. @@ -909,7 +909,7 @@ impl<'p, 'sess, 'ast> SemverVersionParser<'p, 'sess, 'ast> { Self { p, bumps: 0, pos_inside: 0 } } - fn emit_err(&self, msg: impl Into) { + fn emit_err(&self, msg: impl Into) { self.p.dcx().err(msg).span(self.current_span()).emit(); }