Skip to content

Commit 4b8b4cc

Browse files
committed
Overhaul Diagnostic args.
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.)
1 parent bb59453 commit 4b8b4cc

File tree

10 files changed

+22
-30
lines changed

10 files changed

+22
-30
lines changed

compiler/rustc_codegen_llvm/src/errors.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,7 @@ impl<G: EmissionGuarantee> IntoDiagnostic<'_, G> for ParseTargetMachineConfig<'_
103103
fn into_diagnostic(self, dcx: &'_ DiagCtxt, level: Level) -> DiagnosticBuilder<'_, G> {
104104
let diag: DiagnosticBuilder<'_, G> = self.0.into_diagnostic(dcx, level);
105105
let (message, _) = diag.messages.first().expect("`LlvmError` with no message");
106-
let message = dcx.eagerly_translate_to_string(message.clone(), diag.args());
107-
106+
let message = dcx.eagerly_translate_to_string(message.clone(), diag.args.iter());
108107
DiagnosticBuilder::new(dcx, level, fluent::codegen_llvm_parse_target_machine_config)
109108
.with_arg("error", message)
110109
}

compiler/rustc_codegen_ssa/src/back/write.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ use rustc_data_structures::sync::Lrc;
1616
use rustc_errors::emitter::Emitter;
1717
use rustc_errors::translation::Translate;
1818
use rustc_errors::{
19-
DiagCtxt, DiagnosticArgName, DiagnosticArgValue, DiagnosticBuilder, DiagnosticMessage, ErrCode,
20-
FatalError, FluentBundle, Level, Style,
19+
DiagCtxt, DiagnosticArgMap, DiagnosticBuilder, DiagnosticMessage, ErrCode, FatalError,
20+
FluentBundle, Level, Style,
2121
};
2222
use rustc_fs_util::link_or_copy;
2323
use rustc_hir::def_id::{CrateNum, LOCAL_CRATE};
@@ -1001,7 +1001,7 @@ pub struct CguMessage;
10011001

10021002
struct Diagnostic {
10031003
msgs: Vec<(DiagnosticMessage, Style)>,
1004-
args: FxIndexMap<DiagnosticArgName, DiagnosticArgValue>,
1004+
args: DiagnosticArgMap,
10051005
code: Option<ErrCode>,
10061006
lvl: Level,
10071007
}
@@ -1813,8 +1813,8 @@ impl Translate for SharedEmitter {
18131813

18141814
impl Emitter for SharedEmitter {
18151815
fn emit_diagnostic(&mut self, diag: rustc_errors::Diagnostic) {
1816-
let args: FxIndexMap<DiagnosticArgName, DiagnosticArgValue> =
1817-
diag.args().map(|(name, arg)| (name.clone(), arg.clone())).collect();
1816+
let args: DiagnosticArgMap =
1817+
diag.args.iter().map(|(name, arg)| (name.clone(), arg.clone())).collect();
18181818
drop(self.sender.send(SharedEmitterMessage::Diagnostic(Diagnostic {
18191819
msgs: diag.messages.clone(),
18201820
args: args.clone(),
@@ -1857,7 +1857,7 @@ impl SharedEmitterMain {
18571857
let dcx = sess.dcx();
18581858
let mut d = rustc_errors::Diagnostic::new_with_messages(diag.lvl, diag.msgs);
18591859
d.code = diag.code; // may be `None`, that's ok
1860-
d.replace_args(diag.args);
1860+
d.args = diag.args;
18611861
dcx.emit_diagnostic(d);
18621862
}
18631863
Ok(SharedEmitterMessage::InlineAsmError(cookie, msg, level, source)) => {

compiler/rustc_const_eval/src/errors.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,7 @@ pub trait ReportErrorExt {
437437
let mut diag = dcx.struct_allow(DiagnosticMessage::Str(String::new().into()));
438438
let message = self.diagnostic_message();
439439
self.add_args(&mut diag);
440-
let s = dcx.eagerly_translate_to_string(message, diag.args());
440+
let s = dcx.eagerly_translate_to_string(message, diag.args.iter());
441441
diag.cancel();
442442
s
443443
})
@@ -864,7 +864,7 @@ impl<'tcx> ReportErrorExt for InvalidProgramInfo<'tcx> {
864864
let dummy_level = Level::Bug;
865865
let dummy_diag: DiagnosticBuilder<'_, ()> =
866866
e.into_diagnostic().into_diagnostic(diag.dcx, dummy_level);
867-
for (name, val) in dummy_diag.args() {
867+
for (name, val) in dummy_diag.args.iter() {
868868
diag.arg(name.clone(), val.clone());
869869
}
870870
dummy_diag.cancel();

compiler/rustc_const_eval/src/interpret/eval_context.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,7 @@ pub fn format_interp_error<'tcx>(dcx: &DiagCtxt, e: InterpErrorInfo<'tcx>) -> St
446446
let mut diag = dcx.struct_allow("");
447447
let msg = e.diagnostic_message();
448448
e.add_args(&mut diag);
449-
let s = dcx.eagerly_translate_to_string(msg, diag.args());
449+
let s = dcx.eagerly_translate_to_string(msg, diag.args.iter());
450450
diag.cancel();
451451
s
452452
}

compiler/rustc_errors/src/annotate_snippet_emitter_writer.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ impl Translate for AnnotateSnippetEmitter {
4545
impl Emitter for AnnotateSnippetEmitter {
4646
/// The entry point for the diagnostics generation
4747
fn emit_diagnostic(&mut self, mut diag: Diagnostic) {
48-
let fluent_args = to_fluent_args(diag.args());
48+
let fluent_args = to_fluent_args(diag.args.iter());
4949

5050
let mut suggestions = diag.suggestions.unwrap_or(vec![]);
5151
self.primary_span_formatted(&mut diag.span, &mut suggestions, &fluent_args);

compiler/rustc_errors/src/diagnostic.rs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ pub trait DecorateLint<'a, G: EmissionGuarantee> {
9898
fn msg(&self) -> DiagnosticMessage;
9999
}
100100

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

119121
/// This is not used for highlighting or rendering any error message. Rather, it can be used
120122
/// as a sort key to sort a buffer of diagnostics. By default, it is the primary span of
@@ -328,14 +330,6 @@ impl Diagnostic {
328330
pub(crate) fn arg(&mut self, name: impl Into<DiagnosticArgName>, arg: impl IntoDiagnosticArg) {
329331
self.args.insert(name.into(), arg.into_diagnostic_arg());
330332
}
331-
332-
pub fn args(&self) -> impl Iterator<Item = DiagnosticArg<'_>> {
333-
self.args.iter()
334-
}
335-
336-
pub fn replace_args(&mut self, args: FxIndexMap<DiagnosticArgName, DiagnosticArgValue>) {
337-
self.args = args;
338-
}
339333
}
340334

341335
/// `DiagnosticBuilder` impls many `&mut self -> &mut Self` methods. Each one
@@ -965,7 +959,7 @@ impl<'a, G: EmissionGuarantee> DiagnosticBuilder<'a, G> {
965959
subdiagnostic: impl AddToDiagnostic,
966960
) -> &mut Self {
967961
subdiagnostic.add_to_diagnostic_with(self, |diag, msg| {
968-
let args = diag.args();
962+
let args = diag.args.iter();
969963
let msg = diag.subdiagnostic_message_to_diagnostic_message(msg);
970964
dcx.eagerly_translate(msg, args)
971965
});
@@ -1043,7 +1037,7 @@ impl<'a, G: EmissionGuarantee> DiagnosticBuilder<'a, G> {
10431037
}
10441038

10451039
impl Diagnostic {
1046-
/// Fields used for Hash, and PartialEq trait
1040+
/// Fields used for Hash, and PartialEq trait.
10471041
fn keys(
10481042
&self,
10491043
) -> (
@@ -1063,7 +1057,7 @@ impl Diagnostic {
10631057
&self.span,
10641058
&self.children,
10651059
&self.suggestions,
1066-
self.args().collect(),
1060+
self.args.iter().collect(),
10671061
// omit self.sort_span
10681062
&self.is_lint,
10691063
// omit self.emitted_at

compiler/rustc_errors/src/emitter.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -519,7 +519,7 @@ impl Emitter for HumanEmitter {
519519
}
520520

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

524524
let mut suggestions = diag.suggestions.unwrap_or(vec![]);
525525
self.primary_span_formatted(&mut diag.span, &mut suggestions, &fluent_args);

compiler/rustc_errors/src/json.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ struct UnusedExterns<'a, 'b, 'c> {
341341

342342
impl Diagnostic {
343343
fn from_errors_diagnostic(diag: crate::Diagnostic, je: &JsonEmitter) -> Diagnostic {
344-
let args = to_fluent_args(diag.args());
344+
let args = to_fluent_args(diag.args.iter());
345345
let sugg = diag.suggestions.iter().flatten().map(|sugg| {
346346
let translated_message =
347347
je.translate_message(&sugg.msg, &args).map_err(Report::new).unwrap();

compiler/rustc_errors/src/lib.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ extern crate self as rustc_errors;
3737

3838
pub use codes::*;
3939
pub use diagnostic::{
40-
AddToDiagnostic, DecorateLint, Diagnostic, DiagnosticArg, DiagnosticArgName,
40+
AddToDiagnostic, DecorateLint, Diagnostic, DiagnosticArg, DiagnosticArgMap, DiagnosticArgName,
4141
DiagnosticArgValue, DiagnosticStyledString, IntoDiagnosticArg, StringPart, SubDiagnostic,
4242
SubdiagnosticMessageOp,
4343
};
@@ -1486,9 +1486,8 @@ impl DiagCtxtInner {
14861486
diag: &Diagnostic,
14871487
msg: impl Into<SubdiagnosticMessage>,
14881488
) -> SubdiagnosticMessage {
1489-
let args = diag.args();
14901489
let msg = diag.subdiagnostic_message_to_diagnostic_message(msg);
1491-
self.eagerly_translate(msg, args)
1490+
self.eagerly_translate(msg, diag.args.iter())
14921491
}
14931492

14941493
fn flush_delayed(&mut self) {

src/librustdoc/passes/lint/check_code_block_syntax.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ impl Emitter for BufferEmitter {
159159
fn emit_diagnostic(&mut self, diag: Diagnostic) {
160160
let mut buffer = self.buffer.borrow_mut();
161161

162-
let fluent_args = to_fluent_args(diag.args());
162+
let fluent_args = to_fluent_args(diag.args.iter());
163163
let translated_main_message = self
164164
.translate_message(&diag.messages[0].0, &fluent_args)
165165
.unwrap_or_else(|e| panic!("{e}"));

0 commit comments

Comments
 (0)