Skip to content

Commit 2d73a5d

Browse files
committed
Simplify codegen diagnostic handling.
To send a diagnostic from a codegen thread to the main thread, `SharedEmitter` currently converts the `rustc_errors::Diagnostic` into a one or more `rustc_codegen_ssa::Diagnostic`s, sends them, and then `SharedEmitterMain` reconstructs them at the other end into a `rustc_errors::Diagnostic`. This is a lossy operation, because of differences between the two `Diagnostic` types. Instead we can just clone the `rustc_errors::Diagnostic` and send it directly. Maybe in the past `rustc_errors::Diagnostic` wasn't `Send`? But it works now. Much simpler and nicer.
1 parent 11f32b7 commit 2d73a5d

File tree

2 files changed

+5
-37
lines changed

2 files changed

+5
-37
lines changed

compiler/rustc_codegen_ssa/src/back/write.rs

+3-35
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,7 @@ use rustc_data_structures::profiling::{SelfProfilerRef, VerboseTimingGuard};
1515
use rustc_data_structures::sync::Lrc;
1616
use rustc_errors::emitter::Emitter;
1717
use rustc_errors::translation::Translate;
18-
use rustc_errors::{
19-
DiagCtxt, DiagnosticArgName, DiagnosticArgValue, DiagnosticBuilder, DiagnosticMessage, ErrCode,
20-
FatalError, FluentBundle, Level, Style,
21-
};
18+
use rustc_errors::{DiagCtxt, Diagnostic, DiagnosticBuilder, FatalError, FluentBundle, Level};
2219
use rustc_fs_util::link_or_copy;
2320
use rustc_hir::def_id::{CrateNum, LOCAL_CRATE};
2421
use rustc_incremental::{
@@ -39,7 +36,6 @@ use rustc_target::spec::{MergeFunctions, SanitizerSet};
3936

4037
use crate::errors::ErrorCreatingRemarkDir;
4138
use std::any::Any;
42-
use std::borrow::Cow;
4339
use std::fs;
4440
use std::io;
4541
use std::marker::PhantomData;
@@ -998,13 +994,6 @@ pub(crate) enum Message<B: WriteBackendMethods> {
998994
/// process another codegen unit.
999995
pub struct CguMessage;
1000996

1001-
struct Diagnostic {
1002-
msgs: Vec<(DiagnosticMessage, Style)>,
1003-
args: FxHashMap<DiagnosticArgName, DiagnosticArgValue>,
1004-
code: Option<ErrCode>,
1005-
lvl: Level,
1006-
}
1007-
1008997
#[derive(PartialEq, Clone, Copy, Debug)]
1009998
enum MainThreadState {
1010999
/// Doing nothing.
@@ -1812,22 +1801,7 @@ impl Translate for SharedEmitter {
18121801

18131802
impl Emitter for SharedEmitter {
18141803
fn emit_diagnostic(&mut self, diag: &rustc_errors::Diagnostic) {
1815-
let args: FxHashMap<Cow<'_, str>, DiagnosticArgValue> =
1816-
diag.args().map(|(name, arg)| (name.clone(), arg.clone())).collect();
1817-
drop(self.sender.send(SharedEmitterMessage::Diagnostic(Diagnostic {
1818-
msgs: diag.messages.clone(),
1819-
args: args.clone(),
1820-
code: diag.code.clone(),
1821-
lvl: diag.level(),
1822-
})));
1823-
for child in &diag.children {
1824-
drop(self.sender.send(SharedEmitterMessage::Diagnostic(Diagnostic {
1825-
msgs: child.messages.clone(),
1826-
args: args.clone(),
1827-
code: None,
1828-
lvl: child.level,
1829-
})));
1830-
}
1804+
drop(self.sender.send(SharedEmitterMessage::Diagnostic(diag.clone())));
18311805
drop(self.sender.send(SharedEmitterMessage::AbortIfErrors));
18321806
}
18331807

@@ -1853,13 +1827,7 @@ impl SharedEmitterMain {
18531827

18541828
match message {
18551829
Ok(SharedEmitterMessage::Diagnostic(diag)) => {
1856-
let dcx = sess.dcx();
1857-
let mut d = rustc_errors::Diagnostic::new_with_messages(diag.lvl, diag.msgs);
1858-
if let Some(code) = diag.code {
1859-
d.code(code);
1860-
}
1861-
d.replace_args(diag.args);
1862-
dcx.emit_diagnostic(d);
1830+
sess.dcx().emit_diagnostic(diag);
18631831
}
18641832
Ok(SharedEmitterMessage::InlineAsmError(cookie, msg, level, source)) => {
18651833
assert!(matches!(level, Level::Error | Level::Warning | Level::Note));

tests/ui/lto/issue-11154.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
error: cannot prefer dynamic linking when performing LTO
2-
3-
note: only 'staticlib', 'bin', and 'cdylib' outputs are supported with LTO
2+
|
3+
= note: only 'staticlib', 'bin', and 'cdylib' outputs are supported with LTO
44

55
error: aborting due to 1 previous error
66

0 commit comments

Comments
 (0)