Skip to content

Commit

Permalink
Overhaul Diagnostic args.
Browse files Browse the repository at this point in the history
First, introduce a typedef `DiagnosticArgMap`.

Second, make the `args` field public, and remove the `args` getter and
`replace_args` setter. These were necessary previously because the getter
had a `#[allow(rustc::potential_query_instability)]` attribute, but that
was removed in rust-lang#120931 when the args were changed from `FxHashMap` to
`FxIndexMap`. (All the other `Diagnostic` fields are public.)
  • Loading branch information
nnethercote committed Feb 20, 2024
1 parent bb59453 commit 4b8b4cc
Show file tree
Hide file tree
Showing 10 changed files with 22 additions and 30 deletions.
3 changes: 1 addition & 2 deletions compiler/rustc_codegen_llvm/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,7 @@ impl<G: EmissionGuarantee> IntoDiagnostic<'_, G> for ParseTargetMachineConfig<'_
fn into_diagnostic(self, dcx: &'_ DiagCtxt, level: Level) -> DiagnosticBuilder<'_, G> {
let diag: DiagnosticBuilder<'_, G> = self.0.into_diagnostic(dcx, level);
let (message, _) = diag.messages.first().expect("`LlvmError` with no message");
let message = dcx.eagerly_translate_to_string(message.clone(), diag.args());

let message = dcx.eagerly_translate_to_string(message.clone(), diag.args.iter());
DiagnosticBuilder::new(dcx, level, fluent::codegen_llvm_parse_target_machine_config)
.with_arg("error", message)
}
Expand Down
12 changes: 6 additions & 6 deletions compiler/rustc_codegen_ssa/src/back/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ use rustc_data_structures::sync::Lrc;
use rustc_errors::emitter::Emitter;
use rustc_errors::translation::Translate;
use rustc_errors::{
DiagCtxt, DiagnosticArgName, DiagnosticArgValue, DiagnosticBuilder, DiagnosticMessage, ErrCode,
FatalError, FluentBundle, Level, Style,
DiagCtxt, DiagnosticArgMap, DiagnosticBuilder, DiagnosticMessage, ErrCode, FatalError,
FluentBundle, Level, Style,
};
use rustc_fs_util::link_or_copy;
use rustc_hir::def_id::{CrateNum, LOCAL_CRATE};
Expand Down Expand Up @@ -1001,7 +1001,7 @@ pub struct CguMessage;

struct Diagnostic {
msgs: Vec<(DiagnosticMessage, Style)>,
args: FxIndexMap<DiagnosticArgName, DiagnosticArgValue>,
args: DiagnosticArgMap,
code: Option<ErrCode>,
lvl: Level,
}
Expand Down Expand Up @@ -1813,8 +1813,8 @@ impl Translate for SharedEmitter {

impl Emitter for SharedEmitter {
fn emit_diagnostic(&mut self, diag: rustc_errors::Diagnostic) {
let args: FxIndexMap<DiagnosticArgName, DiagnosticArgValue> =
diag.args().map(|(name, arg)| (name.clone(), arg.clone())).collect();
let args: DiagnosticArgMap =
diag.args.iter().map(|(name, arg)| (name.clone(), arg.clone())).collect();
drop(self.sender.send(SharedEmitterMessage::Diagnostic(Diagnostic {
msgs: diag.messages.clone(),
args: args.clone(),
Expand Down Expand Up @@ -1857,7 +1857,7 @@ impl SharedEmitterMain {
let dcx = sess.dcx();
let mut d = rustc_errors::Diagnostic::new_with_messages(diag.lvl, diag.msgs);
d.code = diag.code; // may be `None`, that's ok
d.replace_args(diag.args);
d.args = diag.args;
dcx.emit_diagnostic(d);
}
Ok(SharedEmitterMessage::InlineAsmError(cookie, msg, level, source)) => {
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_const_eval/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ pub trait ReportErrorExt {
let mut diag = dcx.struct_allow(DiagnosticMessage::Str(String::new().into()));
let message = self.diagnostic_message();
self.add_args(&mut diag);
let s = dcx.eagerly_translate_to_string(message, diag.args());
let s = dcx.eagerly_translate_to_string(message, diag.args.iter());
diag.cancel();
s
})
Expand Down Expand Up @@ -864,7 +864,7 @@ impl<'tcx> ReportErrorExt for InvalidProgramInfo<'tcx> {
let dummy_level = Level::Bug;
let dummy_diag: DiagnosticBuilder<'_, ()> =
e.into_diagnostic().into_diagnostic(diag.dcx, dummy_level);
for (name, val) in dummy_diag.args() {
for (name, val) in dummy_diag.args.iter() {
diag.arg(name.clone(), val.clone());
}
dummy_diag.cancel();
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_const_eval/src/interpret/eval_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ pub fn format_interp_error<'tcx>(dcx: &DiagCtxt, e: InterpErrorInfo<'tcx>) -> St
let mut diag = dcx.struct_allow("");
let msg = e.diagnostic_message();
e.add_args(&mut diag);
let s = dcx.eagerly_translate_to_string(msg, diag.args());
let s = dcx.eagerly_translate_to_string(msg, diag.args.iter());
diag.cancel();
s
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ impl Translate for AnnotateSnippetEmitter {
impl Emitter for AnnotateSnippetEmitter {
/// The entry point for the diagnostics generation
fn emit_diagnostic(&mut self, mut diag: Diagnostic) {
let fluent_args = to_fluent_args(diag.args());
let fluent_args = to_fluent_args(diag.args.iter());

let mut suggestions = diag.suggestions.unwrap_or(vec![]);
self.primary_span_formatted(&mut diag.span, &mut suggestions, &fluent_args);
Expand Down
18 changes: 6 additions & 12 deletions compiler/rustc_errors/src/diagnostic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ pub trait DecorateLint<'a, G: EmissionGuarantee> {
fn msg(&self) -> DiagnosticMessage;
}

pub type DiagnosticArgMap = FxIndexMap<DiagnosticArgName, DiagnosticArgValue>;

/// The main part of a diagnostic. Note that `DiagnosticBuilder`, which wraps
/// this type, is used for most operations, and should be used instead whenever
/// possible. This type should only be used when `DiagnosticBuilder`'s lifetime
Expand All @@ -114,7 +116,7 @@ pub struct Diagnostic {
pub span: MultiSpan,
pub children: Vec<SubDiagnostic>,
pub suggestions: Result<Vec<CodeSuggestion>, SuggestionsDisabled>,
args: FxIndexMap<DiagnosticArgName, DiagnosticArgValue>,
pub args: DiagnosticArgMap,

/// This is not used for highlighting or rendering any error message. Rather, it can be used
/// as a sort key to sort a buffer of diagnostics. By default, it is the primary span of
Expand Down Expand Up @@ -328,14 +330,6 @@ impl Diagnostic {
pub(crate) fn arg(&mut self, name: impl Into<DiagnosticArgName>, arg: impl IntoDiagnosticArg) {
self.args.insert(name.into(), arg.into_diagnostic_arg());
}

pub fn args(&self) -> impl Iterator<Item = DiagnosticArg<'_>> {
self.args.iter()
}

pub fn replace_args(&mut self, args: FxIndexMap<DiagnosticArgName, DiagnosticArgValue>) {
self.args = args;
}
}

/// `DiagnosticBuilder` impls many `&mut self -> &mut Self` methods. Each one
Expand Down Expand Up @@ -965,7 +959,7 @@ impl<'a, G: EmissionGuarantee> DiagnosticBuilder<'a, G> {
subdiagnostic: impl AddToDiagnostic,
) -> &mut Self {
subdiagnostic.add_to_diagnostic_with(self, |diag, msg| {
let args = diag.args();
let args = diag.args.iter();
let msg = diag.subdiagnostic_message_to_diagnostic_message(msg);
dcx.eagerly_translate(msg, args)
});
Expand Down Expand Up @@ -1043,7 +1037,7 @@ impl<'a, G: EmissionGuarantee> DiagnosticBuilder<'a, G> {
}

impl Diagnostic {
/// Fields used for Hash, and PartialEq trait
/// Fields used for Hash, and PartialEq trait.
fn keys(
&self,
) -> (
Expand All @@ -1063,7 +1057,7 @@ impl Diagnostic {
&self.span,
&self.children,
&self.suggestions,
self.args().collect(),
self.args.iter().collect(),
// omit self.sort_span
&self.is_lint,
// omit self.emitted_at
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_errors/src/emitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,7 @@ impl Emitter for HumanEmitter {
}

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

let mut suggestions = diag.suggestions.unwrap_or(vec![]);
self.primary_span_formatted(&mut diag.span, &mut suggestions, &fluent_args);
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_errors/src/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ struct UnusedExterns<'a, 'b, 'c> {

impl Diagnostic {
fn from_errors_diagnostic(diag: crate::Diagnostic, je: &JsonEmitter) -> Diagnostic {
let args = to_fluent_args(diag.args());
let args = to_fluent_args(diag.args.iter());
let sugg = diag.suggestions.iter().flatten().map(|sugg| {
let translated_message =
je.translate_message(&sugg.msg, &args).map_err(Report::new).unwrap();
Expand Down
5 changes: 2 additions & 3 deletions compiler/rustc_errors/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ extern crate self as rustc_errors;

pub use codes::*;
pub use diagnostic::{
AddToDiagnostic, DecorateLint, Diagnostic, DiagnosticArg, DiagnosticArgName,
AddToDiagnostic, DecorateLint, Diagnostic, DiagnosticArg, DiagnosticArgMap, DiagnosticArgName,
DiagnosticArgValue, DiagnosticStyledString, IntoDiagnosticArg, StringPart, SubDiagnostic,
SubdiagnosticMessageOp,
};
Expand Down Expand Up @@ -1486,9 +1486,8 @@ impl DiagCtxtInner {
diag: &Diagnostic,
msg: impl Into<SubdiagnosticMessage>,
) -> SubdiagnosticMessage {
let args = diag.args();
let msg = diag.subdiagnostic_message_to_diagnostic_message(msg);
self.eagerly_translate(msg, args)
self.eagerly_translate(msg, diag.args.iter())
}

fn flush_delayed(&mut self) {
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/passes/lint/check_code_block_syntax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ impl Emitter for BufferEmitter {
fn emit_diagnostic(&mut self, diag: Diagnostic) {
let mut buffer = self.buffer.borrow_mut();

let fluent_args = to_fluent_args(diag.args());
let fluent_args = to_fluent_args(diag.args.iter());
let translated_main_message = self
.translate_message(&diag.messages[0].0, &fluent_args)
.unwrap_or_else(|e| panic!("{e}"));
Expand Down

0 comments on commit 4b8b4cc

Please sign in to comment.