Skip to content

Commit 66bf391

Browse files
committed
Auto merge of #64272 - Mark-Simulacrum:parallel-handler, r=estebank
Refactor librustc_errors::Handler API This should be reviewed by-commit. The last commit moves all fields into an inner struct behind a single lock; this is done to prevent possible deadlocks in a multi-threaded compiler, as well as inconsistent state observation.
2 parents b6716a1 + 4cc5aaa commit 66bf391

File tree

28 files changed

+322
-262
lines changed

28 files changed

+322
-262
lines changed

src/librustc/dep_graph/graph.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use errors::{Diagnostic, DiagnosticBuilder};
1+
use errors::Diagnostic;
22
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
33
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
44
use rustc_data_structures::indexed_vec::{Idx, IndexVec};
@@ -819,7 +819,7 @@ impl DepGraph {
819819
let handle = tcx.sess.diagnostic();
820820

821821
for diagnostic in diagnostics {
822-
DiagnosticBuilder::new_diagnostic(handle, diagnostic).emit();
822+
handle.emit_diagnostic(&diagnostic);
823823
}
824824

825825
// Mark the node as green now that diagnostics are emitted

src/librustc/infer/error_reporting/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1119,7 +1119,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
11191119
Some((expected, found)) => Some((expected, found)),
11201120
None => {
11211121
// Derived error. Cancel the emitter.
1122-
self.tcx.sess.diagnostic().cancel(diag);
1122+
diag.cancel();
11231123
return;
11241124
}
11251125
};

src/librustc/session/config.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1855,7 +1855,7 @@ pub fn rustc_optgroups() -> Vec<RustcOptGroup> {
18551855
struct NullEmitter;
18561856

18571857
impl errors::emitter::Emitter for NullEmitter {
1858-
fn emit_diagnostic(&mut self, _: &errors::DiagnosticBuilder<'_>) {}
1858+
fn emit_diagnostic(&mut self, _: &errors::Diagnostic) {}
18591859
}
18601860

18611861
// Converts strings provided as `--cfg [cfgspec]` into a `crate_cfg`.

src/librustc/session/config/tests.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ fn test_can_print_warnings() {
8787
let registry = errors::registry::Registry::new(&[]);
8888
let (sessopts, _) = build_session_options_and_crate_config(&matches);
8989
let sess = build_session(sessopts, None, registry);
90-
assert!(!sess.diagnostic().flags.can_emit_warnings);
90+
assert!(!sess.diagnostic().can_emit_warnings());
9191
});
9292

9393
syntax::with_default_globals(|| {
@@ -97,15 +97,15 @@ fn test_can_print_warnings() {
9797
let registry = errors::registry::Registry::new(&[]);
9898
let (sessopts, _) = build_session_options_and_crate_config(&matches);
9999
let sess = build_session(sessopts, None, registry);
100-
assert!(sess.diagnostic().flags.can_emit_warnings);
100+
assert!(sess.diagnostic().can_emit_warnings());
101101
});
102102

103103
syntax::with_default_globals(|| {
104104
let matches = optgroups().parse(&["-Adead_code".to_string()]).unwrap();
105105
let registry = errors::registry::Registry::new(&[]);
106106
let (sessopts, _) = build_session_options_and_crate_config(&matches);
107107
let sess = build_session(sessopts, None, registry);
108-
assert!(sess.diagnostic().flags.can_emit_warnings);
108+
assert!(sess.diagnostic().can_emit_warnings());
109109
});
110110
}
111111

src/librustc/session/mod.rs

+12-12
Original file line numberDiff line numberDiff line change
@@ -365,12 +365,6 @@ impl Session {
365365
pub fn span_note_without_error<S: Into<MultiSpan>>(&self, sp: S, msg: &str) {
366366
self.diagnostic().span_note_without_error(sp, msg)
367367
}
368-
pub fn span_unimpl<S: Into<MultiSpan>>(&self, sp: S, msg: &str) -> ! {
369-
self.diagnostic().span_unimpl(sp, msg)
370-
}
371-
pub fn unimpl(&self, msg: &str) -> ! {
372-
self.diagnostic().unimpl(msg)
373-
}
374368

375369
pub fn buffer_lint<S: Into<MultiSpan>>(
376370
&self,
@@ -1040,6 +1034,7 @@ fn default_emitter(
10401034
source_map: &Lrc<source_map::SourceMap>,
10411035
emitter_dest: Option<Box<dyn Write + Send>>,
10421036
) -> Box<dyn Emitter + sync::Send> {
1037+
let external_macro_backtrace = sopts.debugging_opts.external_macro_backtrace;
10431038
match (sopts.error_format, emitter_dest) {
10441039
(config::ErrorOutputType::HumanReadable(kind), dst) => {
10451040
let (short, color_config) = kind.unzip();
@@ -1048,6 +1043,7 @@ fn default_emitter(
10481043
let emitter = AnnotateSnippetEmitterWriter::new(
10491044
Some(source_map.clone()),
10501045
short,
1046+
external_macro_backtrace,
10511047
);
10521048
Box::new(emitter.ui_testing(sopts.debugging_opts.ui_testing))
10531049
} else {
@@ -1058,6 +1054,7 @@ fn default_emitter(
10581054
short,
10591055
sopts.debugging_opts.teach,
10601056
sopts.debugging_opts.terminal_width,
1057+
external_macro_backtrace,
10611058
),
10621059
Some(dst) => EmitterWriter::new(
10631060
dst,
@@ -1066,6 +1063,7 @@ fn default_emitter(
10661063
false, // no teach messages when writing to a buffer
10671064
false, // no colors when writing to a buffer
10681065
None, // no terminal width
1066+
external_macro_backtrace,
10691067
),
10701068
};
10711069
Box::new(emitter.ui_testing(sopts.debugging_opts.ui_testing))
@@ -1077,6 +1075,7 @@ fn default_emitter(
10771075
source_map.clone(),
10781076
pretty,
10791077
json_rendered,
1078+
external_macro_backtrace,
10801079
).ui_testing(sopts.debugging_opts.ui_testing),
10811080
),
10821081
(config::ErrorOutputType::Json { pretty, json_rendered }, Some(dst)) => Box::new(
@@ -1086,6 +1085,7 @@ fn default_emitter(
10861085
source_map.clone(),
10871086
pretty,
10881087
json_rendered,
1088+
external_macro_backtrace,
10891089
).ui_testing(sopts.debugging_opts.ui_testing),
10901090
),
10911091
}
@@ -1382,27 +1382,27 @@ pub fn early_error(output: config::ErrorOutputType, msg: &str) -> ! {
13821382
let emitter: Box<dyn Emitter + sync::Send> = match output {
13831383
config::ErrorOutputType::HumanReadable(kind) => {
13841384
let (short, color_config) = kind.unzip();
1385-
Box::new(EmitterWriter::stderr(color_config, None, short, false, None))
1385+
Box::new(EmitterWriter::stderr(color_config, None, short, false, None, false))
13861386
}
13871387
config::ErrorOutputType::Json { pretty, json_rendered } =>
1388-
Box::new(JsonEmitter::basic(pretty, json_rendered)),
1388+
Box::new(JsonEmitter::basic(pretty, json_rendered, false)),
13891389
};
13901390
let handler = errors::Handler::with_emitter(true, None, emitter);
1391-
handler.emit(&MultiSpan::new(), msg, errors::Level::Fatal);
1391+
handler.struct_fatal(msg).emit();
13921392
errors::FatalError.raise();
13931393
}
13941394

13951395
pub fn early_warn(output: config::ErrorOutputType, msg: &str) {
13961396
let emitter: Box<dyn Emitter + sync::Send> = match output {
13971397
config::ErrorOutputType::HumanReadable(kind) => {
13981398
let (short, color_config) = kind.unzip();
1399-
Box::new(EmitterWriter::stderr(color_config, None, short, false, None))
1399+
Box::new(EmitterWriter::stderr(color_config, None, short, false, None, false))
14001400
}
14011401
config::ErrorOutputType::Json { pretty, json_rendered } =>
1402-
Box::new(JsonEmitter::basic(pretty, json_rendered)),
1402+
Box::new(JsonEmitter::basic(pretty, json_rendered, false)),
14031403
};
14041404
let handler = errors::Handler::with_emitter(true, None, emitter);
1405-
handler.emit(&MultiSpan::new(), msg, errors::Level::Warning);
1405+
handler.struct_warn(msg).emit();
14061406
}
14071407

14081408
pub type CompileResult = Result<(), ErrorReported>;

src/librustc/ty/query/plumbing.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -330,14 +330,13 @@ impl<'tcx> TyCtxt<'tcx> {
330330
let mut i = 0;
331331

332332
while let Some(query) = current_query {
333-
let mut db = DiagnosticBuilder::new(icx.tcx.sess.diagnostic(),
334-
Level::FailureNote,
333+
let mut diag = Diagnostic::new(Level::FailureNote,
335334
&format!("#{} [{}] {}",
336335
i,
337336
query.info.query.name(),
338337
query.info.query.describe(icx.tcx)));
339-
db.set_span(icx.tcx.sess.source_map().def_span(query.info.span));
340-
icx.tcx.sess.diagnostic().force_print_db(db);
338+
diag.span = icx.tcx.sess.source_map().def_span(query.info.span).into();
339+
icx.tcx.sess.diagnostic().force_print_diagnostic(diag);
341340

342341
current_query = query.parent.clone();
343342
i += 1;

src/librustc_codegen_ssa/back/write.rs

+7-15
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,11 @@ use rustc::util::common::{time_depth, set_time_depth, print_time_passes_entry};
2222
use rustc::util::profiling::SelfProfiler;
2323
use rustc_fs_util::link_or_copy;
2424
use rustc_data_structures::svh::Svh;
25-
use rustc_errors::{Handler, Level, DiagnosticBuilder, FatalError, DiagnosticId};
25+
use rustc_errors::{Handler, Level, FatalError, DiagnosticId};
2626
use rustc_errors::emitter::{Emitter};
2727
use rustc_target::spec::MergeFunctions;
2828
use syntax::attr;
2929
use syntax::ext::hygiene::ExpnId;
30-
use syntax_pos::MultiSpan;
3130
use syntax_pos::symbol::{Symbol, sym};
3231
use jobserver::{Client, Acquired};
3332

@@ -1725,7 +1724,7 @@ impl SharedEmitter {
17251724
}
17261725

17271726
impl Emitter for SharedEmitter {
1728-
fn emit_diagnostic(&mut self, db: &DiagnosticBuilder<'_>) {
1727+
fn emit_diagnostic(&mut self, db: &rustc_errors::Diagnostic) {
17291728
drop(self.sender.send(SharedEmitterMessage::Diagnostic(Diagnostic {
17301729
msg: db.message(),
17311730
code: db.code.clone(),
@@ -1760,19 +1759,12 @@ impl SharedEmitterMain {
17601759
match message {
17611760
Ok(SharedEmitterMessage::Diagnostic(diag)) => {
17621761
let handler = sess.diagnostic();
1763-
match diag.code {
1764-
Some(ref code) => {
1765-
handler.emit_with_code(&MultiSpan::new(),
1766-
&diag.msg,
1767-
code.clone(),
1768-
diag.lvl);
1769-
}
1770-
None => {
1771-
handler.emit(&MultiSpan::new(),
1772-
&diag.msg,
1773-
diag.lvl);
1774-
}
1762+
let mut d = rustc_errors::Diagnostic::new(diag.lvl, &diag.msg);
1763+
if let Some(code) = diag.code {
1764+
d.code(code);
17751765
}
1766+
handler.emit_diagnostic(&d);
1767+
handler.abort_if_errors_and_should_abort();
17761768
}
17771769
Ok(SharedEmitterMessage::InlineAsmError(cookie, msg)) => {
17781770
sess.span_err(ExpnId::from_u32(cookie).expn_data().call_site, &msg)

src/librustc_driver/lib.rs

+6-7
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ use syntax::source_map::FileLoader;
6666
use syntax::feature_gate::{GatedCfg, UnstableFeatures};
6767
use syntax::parse::{self, PResult};
6868
use syntax::symbol::sym;
69-
use syntax_pos::{DUMMY_SP, MultiSpan, FileName};
69+
use syntax_pos::{DUMMY_SP, FileName};
7070

7171
pub mod pretty;
7272
mod args;
@@ -1196,15 +1196,16 @@ pub fn report_ice(info: &panic::PanicInfo<'_>, bug_report_url: &str) {
11961196
false,
11971197
false,
11981198
None,
1199+
false,
11991200
));
12001201
let handler = errors::Handler::with_emitter(true, None, emitter);
12011202

12021203
// a .span_bug or .bug call has already printed what
12031204
// it wants to print.
12041205
if !info.payload().is::<errors::ExplicitBug>() {
1205-
handler.emit(&MultiSpan::new(),
1206-
"unexpected panic",
1207-
errors::Level::Bug);
1206+
let d = errors::Diagnostic::new(errors::Level::Bug, "unexpected panic");
1207+
handler.emit_diagnostic(&d);
1208+
handler.abort_if_errors_and_should_abort();
12081209
}
12091210

12101211
let mut xs: Vec<Cow<'static, str>> = vec![
@@ -1224,9 +1225,7 @@ pub fn report_ice(info: &panic::PanicInfo<'_>, bug_report_url: &str) {
12241225
}
12251226

12261227
for note in &xs {
1227-
handler.emit(&MultiSpan::new(),
1228-
note,
1229-
errors::Level::Note);
1228+
handler.note_without_error(&note);
12301229
}
12311230

12321231
// If backtraces are enabled, also print the query stack

src/librustc_errors/annotate_snippet_emitter_writer.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
88
use syntax_pos::{SourceFile, MultiSpan, Loc};
99
use crate::{
10-
Level, CodeSuggestion, DiagnosticBuilder, Emitter,
10+
Level, CodeSuggestion, Diagnostic, Emitter,
1111
SourceMapperDyn, SubDiagnostic, DiagnosticId
1212
};
1313
use crate::emitter::FileWithAnnotatedLines;
@@ -25,19 +25,21 @@ pub struct AnnotateSnippetEmitterWriter {
2525
short_message: bool,
2626
/// If true, will normalize line numbers with `LL` to prevent noise in UI test diffs.
2727
ui_testing: bool,
28+
29+
external_macro_backtrace: bool,
2830
}
2931

3032
impl Emitter for AnnotateSnippetEmitterWriter {
3133
/// The entry point for the diagnostics generation
32-
fn emit_diagnostic(&mut self, db: &DiagnosticBuilder<'_>) {
34+
fn emit_diagnostic(&mut self, db: &Diagnostic) {
3335
let mut children = db.children.clone();
3436
let (mut primary_span, suggestions) = self.primary_span_formatted(&db);
3537

3638
self.fix_multispans_in_std_macros(&self.source_map,
3739
&mut primary_span,
3840
&mut children,
3941
&db.level,
40-
db.handler().flags.external_macro_backtrace);
42+
self.external_macro_backtrace);
4143

4244
self.emit_messages_default(&db.level,
4345
db.message(),
@@ -163,12 +165,14 @@ impl<'a> DiagnosticConverter<'a> {
163165
impl AnnotateSnippetEmitterWriter {
164166
pub fn new(
165167
source_map: Option<Lrc<SourceMapperDyn>>,
166-
short_message: bool
168+
short_message: bool,
169+
external_macro_backtrace: bool,
167170
) -> Self {
168171
Self {
169172
source_map,
170173
short_message,
171174
ui_testing: false,
175+
external_macro_backtrace,
172176
}
173177
}
174178

src/librustc_errors/diagnostic_builder.rs

+4-11
Original file line numberDiff line numberDiff line change
@@ -99,17 +99,9 @@ impl<'a> DerefMut for DiagnosticBuilder<'a> {
9999
}
100100

101101
impl<'a> DiagnosticBuilder<'a> {
102-
pub fn handler(&self) -> &'a Handler{
103-
self.0.handler
104-
}
105-
106102
/// Emit the diagnostic.
107103
pub fn emit(&mut self) {
108-
if self.cancelled() {
109-
return;
110-
}
111-
112-
self.0.handler.emit_db(&self);
104+
self.0.handler.emit_diagnostic(&self);
113105
self.cancel();
114106
}
115107

@@ -354,7 +346,7 @@ impl<'a> DiagnosticBuilder<'a> {
354346

355347
/// Convenience function for internal use, clients should use one of the
356348
/// struct_* methods on Handler.
357-
pub fn new(handler: &'a Handler, level: Level, message: &str) -> DiagnosticBuilder<'a> {
349+
crate fn new(handler: &'a Handler, level: Level, message: &str) -> DiagnosticBuilder<'a> {
358350
DiagnosticBuilder::new_with_code(handler, level, None, message)
359351
}
360352

@@ -371,7 +363,8 @@ impl<'a> DiagnosticBuilder<'a> {
371363

372364
/// Creates a new `DiagnosticBuilder` with an already constructed
373365
/// diagnostic.
374-
pub fn new_diagnostic(handler: &'a Handler, diagnostic: Diagnostic) -> DiagnosticBuilder<'a> {
366+
crate fn new_diagnostic(handler: &'a Handler, diagnostic: Diagnostic)
367+
-> DiagnosticBuilder<'a> {
375368
DiagnosticBuilder(Box::new(DiagnosticBuilderInner {
376369
handler,
377370
diagnostic,

0 commit comments

Comments
 (0)