Skip to content

Commit

Permalink
Auto merge of #34789 - jonathandturner:simplify_liberror, r=alexcrichton
Browse files Browse the repository at this point in the history
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)
  • Loading branch information
bors authored Jul 17, 2016
2 parents 34d7f7e + c7158a1 commit 7ed6068
Show file tree
Hide file tree
Showing 36 changed files with 1,550 additions and 2,118 deletions.
4 changes: 2 additions & 2 deletions src/librustc/infer/error_reporting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ use syntax::ast;
use syntax::parse::token;
use syntax::ptr::P;
use syntax_pos::{self, Pos, Span};
use errors::{DiagnosticBuilder, check_old_skool};
use errors::{DiagnosticBuilder, check_old_school};

impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
pub fn note_and_explain_region(self,
Expand Down Expand Up @@ -485,7 +485,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
"{}",
trace.origin);

if !is_simple_error || check_old_skool() {
if !is_simple_error || check_old_school() {
err.note_expected_found(&"type", &expected, &found);
}

Expand Down
25 changes: 17 additions & 8 deletions src/librustc/session/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ use mir::transform as mir_pass;

use syntax::ast::{NodeId, Name};
use errors::{self, DiagnosticBuilder};
use errors::emitter::{Emitter, BasicEmitter, EmitterWriter};
use errors::emitter::{Emitter, EmitterWriter};
use errors::snippet::FormatMode;
use syntax::json::JsonEmitter;
use syntax::feature_gate;
use syntax::parse;
Expand Down Expand Up @@ -439,7 +440,7 @@ pub fn build_session_with_codemap(sopts: config::Options,
config::ErrorOutputType::HumanReadable(color_config) => {
Box::new(EmitterWriter::stderr(color_config,
Some(registry),
codemap.clone(),
Some(codemap.clone()),
errors::snippet::FormatMode::EnvironmentSelected))
}
config::ErrorOutputType::Json => {
Expand Down Expand Up @@ -575,24 +576,32 @@ unsafe fn configure_llvm(sess: &Session) {
}

pub fn early_error(output: config::ErrorOutputType, msg: &str) -> ! {
let mut emitter: Box<Emitter> = match output {
let emitter: Box<Emitter> = match output {
config::ErrorOutputType::HumanReadable(color_config) => {
Box::new(BasicEmitter::stderr(color_config))
Box::new(EmitterWriter::stderr(color_config,
None,
None,
FormatMode::EnvironmentSelected))
}
config::ErrorOutputType::Json => Box::new(JsonEmitter::basic()),
};
emitter.emit(&MultiSpan::new(), msg, None, errors::Level::Fatal);
let handler = errors::Handler::with_emitter(true, false, emitter);
handler.emit(&MultiSpan::new(), msg, errors::Level::Fatal);
panic!(errors::FatalError);
}

pub fn early_warn(output: config::ErrorOutputType, msg: &str) {
let mut emitter: Box<Emitter> = match output {
let emitter: Box<Emitter> = match output {
config::ErrorOutputType::HumanReadable(color_config) => {
Box::new(BasicEmitter::stderr(color_config))
Box::new(EmitterWriter::stderr(color_config,
None,
None,
FormatMode::EnvironmentSelected))
}
config::ErrorOutputType::Json => Box::new(JsonEmitter::basic()),
};
emitter.emit(&MultiSpan::new(), msg, None, errors::Level::Warning);
let handler = errors::Handler::with_emitter(true, false, emitter);
handler.emit(&MultiSpan::new(), msg, errors::Level::Warning);
}

// Err(0) means compilation was stopped, but no errors were found.
Expand Down
45 changes: 31 additions & 14 deletions src/librustc_driver/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ use syntax::feature_gate::{GatedCfg, UnstableFeatures};
use syntax::parse::{self, PResult};
use syntax_pos::MultiSpan;
use errors::emitter::Emitter;
use errors::snippet::FormatMode;

#[cfg(test)]
pub mod test;
Expand Down Expand Up @@ -138,10 +139,15 @@ pub fn run(args: Vec<String>) -> isize {
match session {
Some(sess) => sess.fatal(&abort_msg(err_count)),
None => {
let mut emitter =
errors::emitter::BasicEmitter::stderr(errors::ColorConfig::Auto);
emitter.emit(&MultiSpan::new(), &abort_msg(err_count), None,
errors::Level::Fatal);
let emitter =
errors::emitter::EmitterWriter::stderr(errors::ColorConfig::Auto,
None,
None,
FormatMode::EnvironmentSelected);
let handler = errors::Handler::with_emitter(true, false, Box::new(emitter));
handler.emit(&MultiSpan::new(),
&abort_msg(err_count),
errors::Level::Fatal);
exit_on_err();
}
}
Expand Down Expand Up @@ -373,23 +379,26 @@ fn handle_explain(code: &str,

fn check_cfg(sopts: &config::Options,
output: ErrorOutputType) {
let mut emitter: Box<Emitter> = match output {
let emitter: Box<Emitter> = match output {
config::ErrorOutputType::HumanReadable(color_config) => {
Box::new(errors::emitter::BasicEmitter::stderr(color_config))
Box::new(errors::emitter::EmitterWriter::stderr(color_config,
None,
None,
FormatMode::EnvironmentSelected))
}
config::ErrorOutputType::Json => Box::new(json::JsonEmitter::basic()),
};
let handler = errors::Handler::with_emitter(true, false, emitter);

let mut saw_invalid_predicate = false;
for item in sopts.cfg.iter() {
match item.node {
ast::MetaItemKind::List(ref pred, _) => {
saw_invalid_predicate = true;
emitter.emit(&MultiSpan::new(),
handler.emit(&MultiSpan::new(),
&format!("invalid predicate in --cfg command line argument: `{}`",
pred),
None,
errors::Level::Fatal);
errors::Level::Fatal);
}
_ => {},
}
Expand Down Expand Up @@ -1046,26 +1055,34 @@ pub fn monitor<F: FnOnce() + Send + 'static>(f: F) {
if let Err(value) = thread.unwrap().join() {
// Thread panicked without emitting a fatal diagnostic
if !value.is::<errors::FatalError>() {
let mut emitter = errors::emitter::BasicEmitter::stderr(errors::ColorConfig::Auto);
let emitter =
Box::new(errors::emitter::EmitterWriter::stderr(errors::ColorConfig::Auto,
None,
None,
FormatMode::EnvironmentSelected));
let handler = errors::Handler::with_emitter(true, false, emitter);

// a .span_bug or .bug call has already printed what
// it wants to print.
if !value.is::<errors::ExplicitBug>() {
emitter.emit(&MultiSpan::new(), "unexpected panic", None, errors::Level::Bug);
handler.emit(&MultiSpan::new(),
"unexpected panic",
errors::Level::Bug);
}

let xs = ["the compiler unexpectedly panicked. this is a bug.".to_string(),
format!("we would appreciate a bug report: {}", BUG_REPORT_URL)];
for note in &xs {
emitter.emit(&MultiSpan::new(), &note[..], None, errors::Level::Note)
handler.emit(&MultiSpan::new(),
&note[..],
errors::Level::Note);
}
if match env::var_os("RUST_BACKTRACE") {
Some(val) => &val != "0",
None => false,
} {
emitter.emit(&MultiSpan::new(),
handler.emit(&MultiSpan::new(),
"run with `RUST_BACKTRACE=1` for a backtrace",
None,
errors::Level::Note);
}

Expand Down
19 changes: 8 additions & 11 deletions src/librustc_driver/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ use syntax::ast;
use syntax::abi::Abi;
use syntax::codemap::CodeMap;
use errors;
use errors::emitter::{CoreEmitter, Emitter};
use errors::{Level, RenderSpan};
use errors::emitter::Emitter;
use errors::{Level, DiagnosticBuilder};
use syntax::parse::token;
use syntax::feature_gate::UnstableFeatures;
use syntax_pos::DUMMY_SP;
Expand Down Expand Up @@ -76,15 +76,12 @@ fn remove_message(e: &mut ExpectErrorEmitter, msg: &str, lvl: Level) {
}
}

impl CoreEmitter for ExpectErrorEmitter {
fn emit_message(&mut self,
_sp: &RenderSpan,
msg: &str,
_: Option<&str>,
lvl: Level,
_is_header: bool,
_show_snippet: bool) {
remove_message(self, msg, lvl);
impl Emitter for ExpectErrorEmitter {
fn emit(&mut self, db: &DiagnosticBuilder) {
remove_message(self, &db.message, db.level);
for child in &db.children {
remove_message(self, &child.message, child.level);
}
}
}

Expand Down
Loading

0 comments on commit 7ed6068

Please sign in to comment.