Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Diagnostic renaming #121489

Merged
merged 10 commits into from
Feb 28, 2024
Next Next commit
Rename Diagnostic as DiagInner.
I started by changing it to `DiagData`, but that didn't feel right.
`DiagInner` felt much better.
  • Loading branch information
nnethercote committed Feb 27, 2024
commit 6588f5b7494afdd9bff016b786daad2e96739b97
6 changes: 3 additions & 3 deletions compiler/rustc_codegen_ssa/src/back/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1829,9 +1829,9 @@ impl Translate for SharedEmitter {
}

impl Emitter for SharedEmitter {
fn emit_diagnostic(&mut self, mut diag: rustc_errors::Diagnostic) {
fn emit_diagnostic(&mut self, mut diag: rustc_errors::DiagInner) {
// Check that we aren't missing anything interesting when converting to
// the cut-down local `Diagnostic`.
// the cut-down local `DiagInner`.
assert_eq!(diag.span, MultiSpan::new());
assert_eq!(diag.suggestions, Ok(vec![]));
assert_eq!(diag.sort_span, rustc_span::DUMMY_SP);
Expand Down Expand Up @@ -1880,7 +1880,7 @@ impl SharedEmitterMain {
// Convert it back to a full `Diagnostic` and emit.
let dcx = sess.dcx();
let mut d =
rustc_errors::Diagnostic::new_with_messages(diag.level, diag.messages);
rustc_errors::DiagInner::new_with_messages(diag.level, diag.messages);
d.code = diag.code; // may be `None`, that's ok
d.children = diag
.children
Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_errors/src/annotate_snippet_emitter_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::emitter::FileWithAnnotatedLines;
use crate::snippet::Line;
use crate::translation::{to_fluent_args, Translate};
use crate::{
CodeSuggestion, Diagnostic, DiagnosticMessage, Emitter, ErrCode, FluentBundle,
CodeSuggestion, DiagInner, DiagnosticMessage, Emitter, ErrCode, FluentBundle,
LazyFallbackBundle, Level, MultiSpan, Style, SubDiagnostic,
};
use annotate_snippets::{Annotation, AnnotationType, Renderer, Slice, Snippet, SourceAnnotation};
Expand Down Expand Up @@ -44,7 +44,7 @@ impl Translate for AnnotateSnippetEmitter {

impl Emitter for AnnotateSnippetEmitter {
/// The entry point for the diagnostics generation
fn emit_diagnostic(&mut self, mut diag: Diagnostic) {
fn emit_diagnostic(&mut self, mut diag: DiagInner) {
let fluent_args = to_fluent_args(diag.args.iter());

let mut suggestions = diag.suggestions.unwrap_or(vec![]);
Expand Down Expand Up @@ -82,7 +82,7 @@ fn source_string(file: Lrc<SourceFile>, line: &Line) -> String {
file.get_line(line.line_index - 1).map(|a| a.to_string()).unwrap_or_default()
}

/// Maps `Diagnostic::Level` to `snippet::AnnotationType`
/// Maps `diagnostic::Level` to `snippet::AnnotationType`
fn annotation_type_for_level(level: Level) -> AnnotationType {
match level {
Level::Bug | Level::Fatal | Level::Error | Level::DelayedBug => AnnotationType::Error,
Expand Down
38 changes: 19 additions & 19 deletions compiler/rustc_errors/src/diagnostic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ use std::ops::{Deref, DerefMut};
use std::panic;
use std::thread::panicking;

/// Error type for `Diagnostic`'s `suggestions` field, indicating that
/// `.disable_suggestions()` was called on the `Diagnostic`.
/// Error type for `DiagInner`'s `suggestions` field, indicating that
/// `.disable_suggestions()` was called on the `DiagInner`.
#[derive(Clone, Debug, PartialEq, Eq, Hash, Encodable, Decodable)]
pub struct SuggestionsDisabled;

Expand Down Expand Up @@ -267,7 +267,7 @@ impl StringPart {
/// causes difficulties, e.g. when storing diagnostics within `DiagCtxt`.
#[must_use]
#[derive(Clone, Debug, Encodable, Decodable)]
pub struct Diagnostic {
pub struct DiagInner {
// NOTE(eddyb) this is private to disallow arbitrary after-the-fact changes,
// outside of what methods in this crate themselves allow.
pub(crate) level: Level,
Expand All @@ -291,15 +291,15 @@ pub struct Diagnostic {
pub(crate) emitted_at: DiagnosticLocation,
}

impl Diagnostic {
impl DiagInner {
#[track_caller]
pub fn new<M: Into<DiagnosticMessage>>(level: Level, message: M) -> Self {
Diagnostic::new_with_messages(level, vec![(message.into(), Style::NoStyle)])
DiagInner::new_with_messages(level, vec![(message.into(), Style::NoStyle)])
}

#[track_caller]
pub fn new_with_messages(level: Level, messages: Vec<(DiagnosticMessage, Style)>) -> Self {
Diagnostic {
DiagInner {
level,
messages,
code: None,
Expand Down Expand Up @@ -433,7 +433,7 @@ impl Diagnostic {
}
}

impl Hash for Diagnostic {
impl Hash for DiagInner {
fn hash<H>(&self, state: &mut H)
where
H: Hasher,
Expand All @@ -442,7 +442,7 @@ impl Hash for Diagnostic {
}
}

impl PartialEq for Diagnostic {
impl PartialEq for DiagInner {
fn eq(&self, other: &Self) -> bool {
self.keys() == other.keys()
}
Expand All @@ -458,7 +458,7 @@ pub struct SubDiagnostic {
}

/// Used for emitting structured error messages and other diagnostic information.
/// Wraps a `Diagnostic`, adding some useful things.
/// Wraps a `DiagInner`, adding some useful things.
/// - The `dcx` field, allowing it to (a) emit itself, and (b) do a drop check
/// that it has been emitted or cancelled.
/// - The `EmissionGuarantee`, which determines the type returned from `emit`.
Expand All @@ -480,11 +480,11 @@ pub struct DiagnosticBuilder<'a, G: EmissionGuarantee = ErrorGuaranteed> {
/// replaced with `None`. Then `drop` checks that it is `None`; if not, it
/// panics because a diagnostic was built but not used.
///
/// Why the Box? `Diagnostic` is a large type, and `DiagnosticBuilder` is
/// Why the Box? `DiagInner` is a large type, and `DiagnosticBuilder` 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).
diag: Option<Box<Diagnostic>>,
diag: Option<Box<DiagInner>>,

_marker: PhantomData<G>,
}
Expand All @@ -499,15 +499,15 @@ rustc_data_structures::static_assert_size!(
);

impl<G: EmissionGuarantee> Deref for DiagnosticBuilder<'_, G> {
type Target = Diagnostic;
type Target = DiagInner;

fn deref(&self) -> &Diagnostic {
fn deref(&self) -> &DiagInner {
self.diag.as_ref().unwrap()
}
}

impl<G: EmissionGuarantee> DerefMut for DiagnosticBuilder<'_, G> {
fn deref_mut(&mut self) -> &mut Diagnostic {
fn deref_mut(&mut self) -> &mut DiagInner {
self.diag.as_mut().unwrap()
}
}
Expand Down Expand Up @@ -565,13 +565,13 @@ impl<'a, G: EmissionGuarantee> DiagnosticBuilder<'a, G> {
#[rustc_lint_diagnostics]
#[track_caller]
pub fn new<M: Into<DiagnosticMessage>>(dcx: &'a DiagCtxt, level: Level, message: M) -> Self {
Self::new_diagnostic(dcx, Diagnostic::new(level, message))
Self::new_diagnostic(dcx, DiagInner::new(level, message))
}

/// Creates a new `DiagnosticBuilder` with an already constructed
/// diagnostic.
#[track_caller]
pub(crate) fn new_diagnostic(dcx: &'a DiagCtxt, diag: Diagnostic) -> Self {
pub(crate) fn new_diagnostic(dcx: &'a DiagCtxt, diag: DiagInner) -> Self {
debug!("Created new diagnostic");
Self { dcx, diag: Some(Box::new(diag)), _marker: PhantomData }
}
Expand Down Expand Up @@ -1238,7 +1238,7 @@ impl<'a, G: EmissionGuarantee> DiagnosticBuilder<'a, G> {
/// Takes the diagnostic. For use by methods that consume the
/// DiagnosticBuilder: `emit`, `cancel`, etc. Afterwards, `drop` is the
/// only code that will be run on `self`.
fn take_diag(&mut self) -> Diagnostic {
fn take_diag(&mut self) -> DiagInner {
Box::into_inner(self.diag.take().unwrap())
}

Expand All @@ -1257,7 +1257,7 @@ impl<'a, G: EmissionGuarantee> DiagnosticBuilder<'a, G> {
// because delayed bugs have their level changed to `Bug` when they are
// actually printed, so they produce an ICE.
//
// (Also, even though `level` isn't `pub`, the whole `Diagnostic` could
// (Also, even though `level` isn't `pub`, the whole `DiagInner` could
// be overwritten with a new one thanks to `DerefMut`. So this assert
// protects against that, too.)
assert!(
Expand Down Expand Up @@ -1325,7 +1325,7 @@ impl<G: EmissionGuarantee> Drop for DiagnosticBuilder<'_, G> {
fn drop(&mut self) {
match self.diag.take() {
Some(diag) if !panicking() => {
self.dcx.emit_diagnostic(Diagnostic::new(
self.dcx.emit_diagnostic(DiagInner::new(
Level::Bug,
DiagnosticMessage::from("the following error was constructed but not emitted"),
));
Expand Down
14 changes: 7 additions & 7 deletions compiler/rustc_errors/src/emitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use crate::snippet::{
use crate::styled_buffer::StyledBuffer;
use crate::translation::{to_fluent_args, Translate};
use crate::{
diagnostic::DiagnosticLocation, CodeSuggestion, DiagCtxt, Diagnostic, DiagnosticMessage,
diagnostic::DiagnosticLocation, CodeSuggestion, DiagCtxt, DiagInner, DiagnosticMessage,
ErrCode, FluentBundle, LazyFallbackBundle, Level, MultiSpan, SubDiagnostic,
SubstitutionHighlight, SuggestionStyle, TerminalUrl,
};
Expand Down Expand Up @@ -194,15 +194,15 @@ pub type DynEmitter = dyn Emitter + DynSend;
/// Emitter trait for emitting errors.
pub trait Emitter: Translate {
/// Emit a structured diagnostic.
fn emit_diagnostic(&mut self, diag: Diagnostic);
fn emit_diagnostic(&mut self, diag: DiagInner);

/// Emit a notification that an artifact has been output.
/// Currently only supported for the JSON format.
fn emit_artifact_notification(&mut self, _path: &Path, _artifact_type: &str) {}

/// Emit a report about future breakage.
/// Currently only supported for the JSON format.
fn emit_future_breakage_report(&mut self, _diags: Vec<Diagnostic>) {}
fn emit_future_breakage_report(&mut self, _diags: Vec<DiagInner>) {}

/// Emit list of unused externs.
/// Currently only supported for the JSON format.
Expand All @@ -229,12 +229,12 @@ pub trait Emitter: Translate {
///
/// There are a lot of conditions to this method, but in short:
///
/// * If the current `Diagnostic` has only one visible `CodeSuggestion`,
/// * If the current `DiagInner` has only one visible `CodeSuggestion`,
/// we format the `help` suggestion depending on the content of the
/// substitutions. In that case, we modify the span and clear the
/// suggestions.
///
/// * If the current `Diagnostic` has multiple suggestions,
/// * If the current `DiagInner` has multiple suggestions,
/// we leave `primary_span` and the suggestions untouched.
fn primary_span_formatted(
&mut self,
Expand Down Expand Up @@ -518,7 +518,7 @@ impl Emitter for HumanEmitter {
self.sm.as_ref()
}

fn emit_diagnostic(&mut self, mut diag: Diagnostic) {
fn emit_diagnostic(&mut self, mut diag: DiagInner) {
let fluent_args = to_fluent_args(diag.args.iter());

let mut suggestions = diag.suggestions.unwrap_or(vec![]);
Expand Down Expand Up @@ -597,7 +597,7 @@ impl Emitter for SilentEmitter {
None
}

fn emit_diagnostic(&mut self, mut diag: Diagnostic) {
fn emit_diagnostic(&mut self, mut diag: DiagInner) {
if diag.level == Level::Fatal {
diag.sub(Level::Note, self.fatal_note.clone(), MultiSpan::new());
self.fatal_dcx.emit_diagnostic(diag);
Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_errors/src/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ impl Translate for JsonEmitter {
}

impl Emitter for JsonEmitter {
fn emit_diagnostic(&mut self, diag: crate::Diagnostic) {
fn emit_diagnostic(&mut self, diag: crate::DiagInner) {
let data = Diagnostic::from_errors_diagnostic(diag, self);
let result = self.emit(EmitTyped::Diagnostic(data));
if let Err(e) = result {
Expand All @@ -192,7 +192,7 @@ impl Emitter for JsonEmitter {
}
}

fn emit_future_breakage_report(&mut self, diags: Vec<crate::Diagnostic>) {
fn emit_future_breakage_report(&mut self, diags: Vec<crate::DiagInner>) {
let data: Vec<FutureBreakageItem<'_>> = diags
.into_iter()
.map(|mut diag| {
Expand Down Expand Up @@ -340,7 +340,7 @@ struct UnusedExterns<'a, 'b, 'c> {
}

impl Diagnostic {
fn from_errors_diagnostic(diag: crate::Diagnostic, je: &JsonEmitter) -> Diagnostic {
fn from_errors_diagnostic(diag: crate::DiagInner, je: &JsonEmitter) -> Diagnostic {
let args = to_fluent_args(diag.args.iter());
let sugg = diag.suggestions.iter().flatten().map(|sugg| {
let translated_message =
Expand Down
Loading