1
Fork 0

Remove emit from emitter, leaving emit_struct

This commit is contained in:
Jonathan Turner 2016-07-06 12:08:16 -04:00
parent 8f044fae36
commit 55f06883b8
5 changed files with 71 additions and 71 deletions

View file

@ -576,7 +576,7 @@ unsafe fn configure_llvm(sess: &Session) {
} }
pub fn early_error(output: config::ErrorOutputType, msg: &str) -> ! { 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) => { config::ErrorOutputType::HumanReadable(color_config) => {
Box::new(EmitterWriter::stderr(color_config, Box::new(EmitterWriter::stderr(color_config,
None, None,
@ -585,12 +585,13 @@ pub fn early_error(output: config::ErrorOutputType, msg: &str) -> ! {
} }
config::ErrorOutputType::Json => Box::new(JsonEmitter::basic()), 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); panic!(errors::FatalError);
} }
pub fn early_warn(output: config::ErrorOutputType, msg: &str) { 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) => { config::ErrorOutputType::HumanReadable(color_config) => {
Box::new(EmitterWriter::stderr(color_config, Box::new(EmitterWriter::stderr(color_config,
None, None,
@ -599,7 +600,8 @@ pub fn early_warn(output: config::ErrorOutputType, msg: &str) {
} }
config::ErrorOutputType::Json => Box::new(JsonEmitter::basic()), 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. // Err(0) means compilation was stopped, but no errors were found.

View file

@ -139,13 +139,15 @@ pub fn run(args: Vec<String>) -> isize {
match session { match session {
Some(sess) => sess.fatal(&abort_msg(err_count)), Some(sess) => sess.fatal(&abort_msg(err_count)),
None => { None => {
let mut emitter = let emitter =
errors::emitter::EmitterWriter::stderr(errors::ColorConfig::Auto, errors::emitter::EmitterWriter::stderr(errors::ColorConfig::Auto,
None, None,
None, None,
FormatMode::EnvironmentSelected); FormatMode::EnvironmentSelected);
emitter.emit(&MultiSpan::new(), &abort_msg(err_count), None, let handler = errors::Handler::with_emitter(true, false, Box::new(emitter));
errors::Level::Fatal); handler.emit(&MultiSpan::new(),
&abort_msg(err_count),
errors::Level::Fatal);
exit_on_err(); exit_on_err();
} }
} }
@ -377,7 +379,7 @@ fn handle_explain(code: &str,
fn check_cfg(sopts: &config::Options, fn check_cfg(sopts: &config::Options,
output: ErrorOutputType) { output: ErrorOutputType) {
let mut emitter: Box<Emitter> = match output { let emitter: Box<Emitter> = match output {
config::ErrorOutputType::HumanReadable(color_config) => { config::ErrorOutputType::HumanReadable(color_config) => {
Box::new(errors::emitter::EmitterWriter::stderr(color_config, Box::new(errors::emitter::EmitterWriter::stderr(color_config,
None, None,
@ -386,17 +388,17 @@ fn check_cfg(sopts: &config::Options,
} }
config::ErrorOutputType::Json => Box::new(json::JsonEmitter::basic()), config::ErrorOutputType::Json => Box::new(json::JsonEmitter::basic()),
}; };
let handler = errors::Handler::with_emitter(true, false, emitter);
let mut saw_invalid_predicate = false; let mut saw_invalid_predicate = false;
for item in sopts.cfg.iter() { for item in sopts.cfg.iter() {
match item.node { match item.node {
ast::MetaItemKind::List(ref pred, _) => { ast::MetaItemKind::List(ref pred, _) => {
saw_invalid_predicate = true; saw_invalid_predicate = true;
emitter.emit(&MultiSpan::new(), handler.emit(&MultiSpan::new(),
&format!("invalid predicate in --cfg command line argument: `{}`", &format!("invalid predicate in --cfg command line argument: `{}`",
pred), pred),
None, errors::Level::Fatal);
errors::Level::Fatal);
} }
_ => {}, _ => {},
} }
@ -1053,30 +1055,34 @@ pub fn monitor<F: FnOnce() + Send + 'static>(f: F) {
if let Err(value) = thread.unwrap().join() { if let Err(value) = thread.unwrap().join() {
// Thread panicked without emitting a fatal diagnostic // Thread panicked without emitting a fatal diagnostic
if !value.is::<errors::FatalError>() { if !value.is::<errors::FatalError>() {
let mut emitter = let emitter =
errors::emitter::EmitterWriter::stderr(errors::ColorConfig::Auto, Box::new(errors::emitter::EmitterWriter::stderr(errors::ColorConfig::Auto,
None, None,
None, None,
FormatMode::EnvironmentSelected); FormatMode::EnvironmentSelected));
let handler = errors::Handler::with_emitter(true, false, emitter);
// a .span_bug or .bug call has already printed what // a .span_bug or .bug call has already printed what
// it wants to print. // it wants to print.
if !value.is::<errors::ExplicitBug>() { 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(), let xs = ["the compiler unexpectedly panicked. this is a bug.".to_string(),
format!("we would appreciate a bug report: {}", BUG_REPORT_URL)]; format!("we would appreciate a bug report: {}", BUG_REPORT_URL)];
for note in &xs { 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") { if match env::var_os("RUST_BACKTRACE") {
Some(val) => &val != "0", Some(val) => &val != "0",
None => false, None => false,
} { } {
emitter.emit(&MultiSpan::new(), handler.emit(&MultiSpan::new(),
"run with `RUST_BACKTRACE=1` for a backtrace", "run with `RUST_BACKTRACE=1` for a backtrace",
None,
errors::Level::Note); errors::Level::Note);
} }

View file

@ -28,9 +28,6 @@ use term;
/// Emitter trait for emitting errors. Do not implement this directly: /// Emitter trait for emitting errors. Do not implement this directly:
/// implement `CoreEmitter` instead. /// implement `CoreEmitter` instead.
pub trait Emitter { pub trait Emitter {
/// Emit a standalone diagnostic message.
fn emit(&mut self, span: &MultiSpan, msg: &str, code: Option<&str>, lvl: Level);
/// Emit a structured diagnostic. /// Emit a structured diagnostic.
fn emit_struct(&mut self, db: &DiagnosticBuilder); fn emit_struct(&mut self, db: &DiagnosticBuilder);
} }
@ -46,19 +43,6 @@ pub trait CoreEmitter {
} }
impl<T: CoreEmitter> Emitter for T { impl<T: CoreEmitter> Emitter for T {
fn emit(&mut self,
msp: &MultiSpan,
msg: &str,
code: Option<&str>,
lvl: Level) {
self.emit_message(&FullSpan(msp.clone()),
msg,
code,
lvl,
true,
true);
}
fn emit_struct(&mut self, db: &DiagnosticBuilder) { fn emit_struct(&mut self, db: &DiagnosticBuilder) {
let old_school = check_old_skool(); let old_school = check_old_skool();
let db_span = FullSpan(db.span.clone()); let db_span = FullSpan(db.span.clone());

View file

@ -359,11 +359,20 @@ impl<'a> DiagnosticBuilder<'a> {
fn new(handler: &'a Handler, fn new(handler: &'a Handler,
level: Level, level: Level,
message: &str) -> DiagnosticBuilder<'a> { message: &str) -> DiagnosticBuilder<'a> {
DiagnosticBuilder::new_with_code(handler, level, None, message)
}
/// Convenience function for internal use, clients should use one of the
/// struct_* methods on Handler.
fn new_with_code(handler: &'a Handler,
level: Level,
code: Option<String>,
message: &str) -> DiagnosticBuilder<'a> {
DiagnosticBuilder { DiagnosticBuilder {
handler: handler, handler: handler,
level: level, level: level,
message: message.to_owned(), message: message.to_owned(),
code: None, code: code,
span: MultiSpan::new(), span: MultiSpan::new(),
children: vec![], children: vec![],
} }
@ -397,10 +406,10 @@ impl<'a> fmt::Debug for DiagnosticBuilder<'a> {
impl<'a> Drop for DiagnosticBuilder<'a> { impl<'a> Drop for DiagnosticBuilder<'a> {
fn drop(&mut self) { fn drop(&mut self) {
if !panicking() && !self.cancelled() { if !panicking() && !self.cancelled() {
self.handler.emit.borrow_mut().emit(&MultiSpan::new(), let mut db = DiagnosticBuilder::new(self.handler,
"Error constructed but not emitted", Bug,
None, "Error constructed but not emitted");
Bug); db.emit();
panic!(); panic!();
} }
} }
@ -588,7 +597,7 @@ impl Handler {
self.bump_err_count(); self.bump_err_count();
} }
pub fn span_note_without_error<S: Into<MultiSpan>>(&self, sp: S, msg: &str) { pub fn span_note_without_error<S: Into<MultiSpan>>(&self, sp: S, msg: &str) {
self.emit.borrow_mut().emit(&sp.into(), msg, None, Note); self.emit(&sp.into(), msg, Note);
} }
pub fn span_unimpl<S: Into<MultiSpan>>(&self, sp: S, msg: &str) -> ! { pub fn span_unimpl<S: Into<MultiSpan>>(&self, sp: S, msg: &str) -> ! {
self.span_bug(sp, &format!("unimplemented {}", msg)); self.span_bug(sp, &format!("unimplemented {}", msg));
@ -597,7 +606,10 @@ impl Handler {
if self.treat_err_as_bug { if self.treat_err_as_bug {
self.bug(msg); self.bug(msg);
} }
self.emit.borrow_mut().emit(&MultiSpan::new(), msg, None, Fatal); let mut db = DiagnosticBuilder::new(self,
Fatal,
msg);
db.emit();
self.bump_err_count(); self.bump_err_count();
FatalError FatalError
} }
@ -605,17 +617,29 @@ impl Handler {
if self.treat_err_as_bug { if self.treat_err_as_bug {
self.bug(msg); self.bug(msg);
} }
self.emit.borrow_mut().emit(&MultiSpan::new(), msg, None, Error); let mut db = DiagnosticBuilder::new(self,
Error,
msg);
db.emit();
self.bump_err_count(); self.bump_err_count();
} }
pub fn warn(&self, msg: &str) { pub fn warn(&self, msg: &str) {
self.emit.borrow_mut().emit(&MultiSpan::new(), msg, None, Warning); let mut db = DiagnosticBuilder::new(self,
Warning,
msg);
db.emit();
} }
pub fn note_without_error(&self, msg: &str) { pub fn note_without_error(&self, msg: &str) {
self.emit.borrow_mut().emit(&MultiSpan::new(), msg, None, Note); let mut db = DiagnosticBuilder::new(self,
Note,
msg);
db.emit();
} }
pub fn bug(&self, msg: &str) -> ! { pub fn bug(&self, msg: &str) -> ! {
self.emit.borrow_mut().emit(&MultiSpan::new(), msg, None, Bug); let mut db = DiagnosticBuilder::new(self,
Bug,
msg);
db.emit();
panic!(ExplicitBug); panic!(ExplicitBug);
} }
pub fn unimpl(&self, msg: &str) -> ! { pub fn unimpl(&self, msg: &str) -> ! {
@ -661,7 +685,9 @@ impl Handler {
msg: &str, msg: &str,
lvl: Level) { lvl: Level) {
if lvl == Warning && !self.can_emit_warnings { return } if lvl == Warning && !self.can_emit_warnings { return }
self.emit.borrow_mut().emit(&msp, msg, None, lvl); let mut db = DiagnosticBuilder::new(self, lvl, msg);
db.set_span(msp.clone());
db.emit();
if !self.continue_after_error.get() { self.abort_if_errors(); } if !self.continue_after_error.get() { self.abort_if_errors(); }
} }
pub fn emit_with_code(&self, pub fn emit_with_code(&self,
@ -670,7 +696,12 @@ impl Handler {
code: &str, code: &str,
lvl: Level) { lvl: Level) {
if lvl == Warning && !self.can_emit_warnings { return } if lvl == Warning && !self.can_emit_warnings { return }
self.emit.borrow_mut().emit(&msp, msg, Some(code), lvl); let mut db = DiagnosticBuilder::new_with_code(self,
lvl,
Some(code.to_owned()),
msg);
db.set_span(msp.clone());
db.emit();
if !self.continue_after_error.get() { self.abort_if_errors(); } if !self.continue_after_error.get() { self.abort_if_errors(); }
} }
} }

View file

@ -22,7 +22,7 @@
use codemap::CodeMap; use codemap::CodeMap;
use syntax_pos::{self, MacroBacktrace, Span, SpanLabel, MultiSpan}; use syntax_pos::{self, MacroBacktrace, Span, SpanLabel, MultiSpan};
use errors::registry::Registry; use errors::registry::Registry;
use errors::{Level, DiagnosticBuilder, SubDiagnostic, RenderSpan, CodeSuggestion, CodeMapper}; use errors::{DiagnosticBuilder, SubDiagnostic, RenderSpan, CodeSuggestion, CodeMapper};
use errors::emitter::Emitter; use errors::emitter::Emitter;
use std::rc::Rc; use std::rc::Rc;
@ -53,13 +53,6 @@ impl JsonEmitter {
} }
impl Emitter for JsonEmitter { impl Emitter for JsonEmitter {
fn emit(&mut self, span: &MultiSpan, msg: &str, code: Option<&str>, level: Level) {
let data = Diagnostic::new(span, msg, code, level, self);
if let Err(e) = writeln!(&mut self.dst, "{}", as_json(&data)) {
panic!("failed to print diagnostics: {:?}", e);
}
}
fn emit_struct(&mut self, db: &DiagnosticBuilder) { fn emit_struct(&mut self, db: &DiagnosticBuilder) {
let data = Diagnostic::from_diagnostic_builder(db, self); let data = Diagnostic::from_diagnostic_builder(db, self);
if let Err(e) = writeln!(&mut self.dst, "{}", as_json(&data)) { if let Err(e) = writeln!(&mut self.dst, "{}", as_json(&data)) {
@ -146,22 +139,6 @@ struct DiagnosticCode {
} }
impl<'a> Diagnostic<'a> { impl<'a> Diagnostic<'a> {
fn new(msp: &MultiSpan,
msg: &'a str,
code: Option<&str>,
level: Level,
je: &JsonEmitter)
-> Diagnostic<'a> {
Diagnostic {
message: msg,
code: DiagnosticCode::map_opt_string(code.map(|c| c.to_owned()), je),
level: level.to_str(),
spans: DiagnosticSpan::from_multispan(msp, je),
children: vec![],
rendered: None,
}
}
fn from_diagnostic_builder<'c>(db: &'c DiagnosticBuilder, fn from_diagnostic_builder<'c>(db: &'c DiagnosticBuilder,
je: &JsonEmitter) je: &JsonEmitter)
-> Diagnostic<'c> { -> Diagnostic<'c> {