Skip to content

Commit b4820a3

Browse files
committed
Delay diagnostic translation in rustc_codegen_ssa
1 parent 9a15458 commit b4820a3

File tree

2 files changed

+27
-9
lines changed

2 files changed

+27
-9
lines changed

compiler/rustc_codegen_ssa/src/back/write.rs

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,8 @@ use rustc_data_structures::profiling::TimingGuard;
1515
use rustc_data_structures::profiling::VerboseTimingGuard;
1616
use rustc_data_structures::sync::Lrc;
1717
use rustc_errors::emitter::Emitter;
18-
use rustc_errors::{
19-
translation::{to_fluent_args, Translate},
20-
DiagnosticId, FatalError, Handler, Level,
21-
};
18+
use rustc_errors::{translation::Translate, DiagnosticId, FatalError, Handler, Level};
19+
use rustc_errors::{DiagnosticMessage, Style};
2220
use rustc_fs_util::link_or_copy;
2321
use rustc_hir::def_id::{CrateNum, LOCAL_CRATE};
2422
use rustc_incremental::{
@@ -38,6 +36,7 @@ use rustc_span::{BytePos, FileName, InnerSpan, Pos, Span};
3836
use rustc_target::spec::{MergeFunctions, SanitizerSet};
3937

4038
use std::any::Any;
39+
use std::borrow::Cow;
4140
use std::fs;
4241
use std::io;
4342
use std::marker::PhantomData;
@@ -969,8 +968,11 @@ pub enum Message<B: WriteBackendMethods> {
969968
CodegenAborted,
970969
}
971970

971+
type DiagnosticArgName<'source> = Cow<'source, str>;
972+
972973
struct Diagnostic {
973-
msg: String,
974+
msg: Vec<(DiagnosticMessage, Style)>,
975+
args: FxHashMap<DiagnosticArgName<'static>, rustc_errors::DiagnosticArgValue<'static>>,
974976
code: Option<DiagnosticId>,
975977
lvl: Level,
976978
}
@@ -1743,15 +1745,18 @@ impl Translate for SharedEmitter {
17431745

17441746
impl Emitter for SharedEmitter {
17451747
fn emit_diagnostic(&mut self, diag: &rustc_errors::Diagnostic) {
1746-
let fluent_args = to_fluent_args(diag.args());
1748+
let args: FxHashMap<Cow<'_, str>, rustc_errors::DiagnosticArgValue<'_>> =
1749+
diag.args().map(|(name, arg)| (name.clone(), arg.clone())).collect();
17471750
drop(self.sender.send(SharedEmitterMessage::Diagnostic(Diagnostic {
1748-
msg: self.translate_messages(&diag.message, &fluent_args).to_string(),
1751+
msg: diag.message.clone(),
1752+
args: args.clone(),
17491753
code: diag.code.clone(),
17501754
lvl: diag.level(),
17511755
})));
17521756
for child in &diag.children {
17531757
drop(self.sender.send(SharedEmitterMessage::Diagnostic(Diagnostic {
1754-
msg: self.translate_messages(&child.message, &fluent_args).to_string(),
1758+
msg: child.message.clone(),
1759+
args: args.clone(),
17551760
code: None,
17561761
lvl: child.level,
17571762
})));
@@ -1782,10 +1787,14 @@ impl SharedEmitterMain {
17821787
match message {
17831788
Ok(SharedEmitterMessage::Diagnostic(diag)) => {
17841789
let handler = sess.diagnostic();
1785-
let mut d = rustc_errors::Diagnostic::new(diag.lvl, &diag.msg);
1790+
let mut d = rustc_errors::Diagnostic::new(diag.lvl, String::new());
1791+
d.message = diag.msg;
17861792
if let Some(code) = diag.code {
17871793
d.code(code);
17881794
}
1795+
for (name, arg) in diag.args {
1796+
d.set_arg(name, arg);
1797+
}
17891798
handler.emit_diagnostic(&mut d);
17901799
}
17911800
Ok(SharedEmitterMessage::InlineAsmError(cookie, msg, level, source)) => {

compiler/rustc_errors/src/diagnostic.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,15 @@ pub trait IntoDiagnosticArg {
4444
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static>;
4545
}
4646

47+
impl<'source> IntoDiagnosticArg for DiagnosticArgValue<'source> {
48+
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
49+
match self {
50+
DiagnosticArgValue::Str(s) => DiagnosticArgValue::Str(Cow::Owned(s.into_owned())),
51+
DiagnosticArgValue::Number(n) => DiagnosticArgValue::Number(n),
52+
}
53+
}
54+
}
55+
4756
impl<'source> Into<FluentValue<'source>> for DiagnosticArgValue<'source> {
4857
fn into(self) -> FluentValue<'source> {
4958
match self {

0 commit comments

Comments
 (0)