Skip to content

Commit 7ed6068

Browse files
authored
Auto merge of #34789 - jonathandturner:simplify_liberror, r=alexcrichton
Simplify librustc_errors This is part 2 of the error crate refactor, starting with #34403. In this refactor, I focused on slimming down the error crate to fewer moving parts. As such, I've removed quite a few parts and replaced the with simpler, straight-line code. Specifically, this PR: * Removes BasicEmitter * Remove emit from emitter, leaving emit_struct * Renames emit_struct to emit * Removes CoreEmitter and focuses on a single Emitter * Implements the latest changes to error format RFC (#1644) * Removes (now-unused) code in emitter.rs and snippet.rs * Moves more tests to the UI tester, removing some duplicate tests in the process There is probably more that could be done with some additional refactoring, but this felt like it was getting to a good state. r? @alexcrichton cc: @Manishearth (as there may be breaking changes in stuff I removed/changed)
2 parents 34d7f7e + c7158a1 commit 7ed6068

36 files changed

+1550
-2118
lines changed

src/librustc/infer/error_reporting.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ use syntax::ast;
9494
use syntax::parse::token;
9595
use syntax::ptr::P;
9696
use syntax_pos::{self, Pos, Span};
97-
use errors::{DiagnosticBuilder, check_old_skool};
97+
use errors::{DiagnosticBuilder, check_old_school};
9898

9999
impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
100100
pub fn note_and_explain_region(self,
@@ -485,7 +485,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
485485
"{}",
486486
trace.origin);
487487

488-
if !is_simple_error || check_old_skool() {
488+
if !is_simple_error || check_old_school() {
489489
err.note_expected_found(&"type", &expected, &found);
490490
}
491491

src/librustc/session/mod.rs

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ use mir::transform as mir_pass;
2222

2323
use syntax::ast::{NodeId, Name};
2424
use errors::{self, DiagnosticBuilder};
25-
use errors::emitter::{Emitter, BasicEmitter, EmitterWriter};
25+
use errors::emitter::{Emitter, EmitterWriter};
26+
use errors::snippet::FormatMode;
2627
use syntax::json::JsonEmitter;
2728
use syntax::feature_gate;
2829
use syntax::parse;
@@ -439,7 +440,7 @@ pub fn build_session_with_codemap(sopts: config::Options,
439440
config::ErrorOutputType::HumanReadable(color_config) => {
440441
Box::new(EmitterWriter::stderr(color_config,
441442
Some(registry),
442-
codemap.clone(),
443+
Some(codemap.clone()),
443444
errors::snippet::FormatMode::EnvironmentSelected))
444445
}
445446
config::ErrorOutputType::Json => {
@@ -575,24 +576,32 @@ unsafe fn configure_llvm(sess: &Session) {
575576
}
576577

577578
pub fn early_error(output: config::ErrorOutputType, msg: &str) -> ! {
578-
let mut emitter: Box<Emitter> = match output {
579+
let emitter: Box<Emitter> = match output {
579580
config::ErrorOutputType::HumanReadable(color_config) => {
580-
Box::new(BasicEmitter::stderr(color_config))
581+
Box::new(EmitterWriter::stderr(color_config,
582+
None,
583+
None,
584+
FormatMode::EnvironmentSelected))
581585
}
582586
config::ErrorOutputType::Json => Box::new(JsonEmitter::basic()),
583587
};
584-
emitter.emit(&MultiSpan::new(), msg, None, errors::Level::Fatal);
588+
let handler = errors::Handler::with_emitter(true, false, emitter);
589+
handler.emit(&MultiSpan::new(), msg, errors::Level::Fatal);
585590
panic!(errors::FatalError);
586591
}
587592

588593
pub fn early_warn(output: config::ErrorOutputType, msg: &str) {
589-
let mut emitter: Box<Emitter> = match output {
594+
let emitter: Box<Emitter> = match output {
590595
config::ErrorOutputType::HumanReadable(color_config) => {
591-
Box::new(BasicEmitter::stderr(color_config))
596+
Box::new(EmitterWriter::stderr(color_config,
597+
None,
598+
None,
599+
FormatMode::EnvironmentSelected))
592600
}
593601
config::ErrorOutputType::Json => Box::new(JsonEmitter::basic()),
594602
};
595-
emitter.emit(&MultiSpan::new(), msg, None, errors::Level::Warning);
603+
let handler = errors::Handler::with_emitter(true, false, emitter);
604+
handler.emit(&MultiSpan::new(), msg, errors::Level::Warning);
596605
}
597606

598607
// Err(0) means compilation was stopped, but no errors were found.

src/librustc_driver/lib.rs

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ use syntax::feature_gate::{GatedCfg, UnstableFeatures};
100100
use syntax::parse::{self, PResult};
101101
use syntax_pos::MultiSpan;
102102
use errors::emitter::Emitter;
103+
use errors::snippet::FormatMode;
103104

104105
#[cfg(test)]
105106
pub mod test;
@@ -138,10 +139,15 @@ pub fn run(args: Vec<String>) -> isize {
138139
match session {
139140
Some(sess) => sess.fatal(&abort_msg(err_count)),
140141
None => {
141-
let mut emitter =
142-
errors::emitter::BasicEmitter::stderr(errors::ColorConfig::Auto);
143-
emitter.emit(&MultiSpan::new(), &abort_msg(err_count), None,
144-
errors::Level::Fatal);
142+
let emitter =
143+
errors::emitter::EmitterWriter::stderr(errors::ColorConfig::Auto,
144+
None,
145+
None,
146+
FormatMode::EnvironmentSelected);
147+
let handler = errors::Handler::with_emitter(true, false, Box::new(emitter));
148+
handler.emit(&MultiSpan::new(),
149+
&abort_msg(err_count),
150+
errors::Level::Fatal);
145151
exit_on_err();
146152
}
147153
}
@@ -373,23 +379,26 @@ fn handle_explain(code: &str,
373379

374380
fn check_cfg(sopts: &config::Options,
375381
output: ErrorOutputType) {
376-
let mut emitter: Box<Emitter> = match output {
382+
let emitter: Box<Emitter> = match output {
377383
config::ErrorOutputType::HumanReadable(color_config) => {
378-
Box::new(errors::emitter::BasicEmitter::stderr(color_config))
384+
Box::new(errors::emitter::EmitterWriter::stderr(color_config,
385+
None,
386+
None,
387+
FormatMode::EnvironmentSelected))
379388
}
380389
config::ErrorOutputType::Json => Box::new(json::JsonEmitter::basic()),
381390
};
391+
let handler = errors::Handler::with_emitter(true, false, emitter);
382392

383393
let mut saw_invalid_predicate = false;
384394
for item in sopts.cfg.iter() {
385395
match item.node {
386396
ast::MetaItemKind::List(ref pred, _) => {
387397
saw_invalid_predicate = true;
388-
emitter.emit(&MultiSpan::new(),
398+
handler.emit(&MultiSpan::new(),
389399
&format!("invalid predicate in --cfg command line argument: `{}`",
390400
pred),
391-
None,
392-
errors::Level::Fatal);
401+
errors::Level::Fatal);
393402
}
394403
_ => {},
395404
}
@@ -1046,26 +1055,34 @@ pub fn monitor<F: FnOnce() + Send + 'static>(f: F) {
10461055
if let Err(value) = thread.unwrap().join() {
10471056
// Thread panicked without emitting a fatal diagnostic
10481057
if !value.is::<errors::FatalError>() {
1049-
let mut emitter = errors::emitter::BasicEmitter::stderr(errors::ColorConfig::Auto);
1058+
let emitter =
1059+
Box::new(errors::emitter::EmitterWriter::stderr(errors::ColorConfig::Auto,
1060+
None,
1061+
None,
1062+
FormatMode::EnvironmentSelected));
1063+
let handler = errors::Handler::with_emitter(true, false, emitter);
10501064

10511065
// a .span_bug or .bug call has already printed what
10521066
// it wants to print.
10531067
if !value.is::<errors::ExplicitBug>() {
1054-
emitter.emit(&MultiSpan::new(), "unexpected panic", None, errors::Level::Bug);
1068+
handler.emit(&MultiSpan::new(),
1069+
"unexpected panic",
1070+
errors::Level::Bug);
10551071
}
10561072

10571073
let xs = ["the compiler unexpectedly panicked. this is a bug.".to_string(),
10581074
format!("we would appreciate a bug report: {}", BUG_REPORT_URL)];
10591075
for note in &xs {
1060-
emitter.emit(&MultiSpan::new(), &note[..], None, errors::Level::Note)
1076+
handler.emit(&MultiSpan::new(),
1077+
&note[..],
1078+
errors::Level::Note);
10611079
}
10621080
if match env::var_os("RUST_BACKTRACE") {
10631081
Some(val) => &val != "0",
10641082
None => false,
10651083
} {
1066-
emitter.emit(&MultiSpan::new(),
1084+
handler.emit(&MultiSpan::new(),
10671085
"run with `RUST_BACKTRACE=1` for a backtrace",
1068-
None,
10691086
errors::Level::Note);
10701087
}
10711088

src/librustc_driver/test.rs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ use syntax::ast;
3333
use syntax::abi::Abi;
3434
use syntax::codemap::CodeMap;
3535
use errors;
36-
use errors::emitter::{CoreEmitter, Emitter};
37-
use errors::{Level, RenderSpan};
36+
use errors::emitter::Emitter;
37+
use errors::{Level, DiagnosticBuilder};
3838
use syntax::parse::token;
3939
use syntax::feature_gate::UnstableFeatures;
4040
use syntax_pos::DUMMY_SP;
@@ -76,15 +76,12 @@ fn remove_message(e: &mut ExpectErrorEmitter, msg: &str, lvl: Level) {
7676
}
7777
}
7878

79-
impl CoreEmitter for ExpectErrorEmitter {
80-
fn emit_message(&mut self,
81-
_sp: &RenderSpan,
82-
msg: &str,
83-
_: Option<&str>,
84-
lvl: Level,
85-
_is_header: bool,
86-
_show_snippet: bool) {
87-
remove_message(self, msg, lvl);
79+
impl Emitter for ExpectErrorEmitter {
80+
fn emit(&mut self, db: &DiagnosticBuilder) {
81+
remove_message(self, &db.message, db.level);
82+
for child in &db.children {
83+
remove_message(self, &child.message, child.level);
84+
}
8885
}
8986
}
9087

0 commit comments

Comments
 (0)