1
Fork 0

Remove BasicEmitter

This commit is contained in:
Jonathan Turner 2016-07-05 15:24:23 -04:00
parent 3c85f414e9
commit 8f044fae36
9 changed files with 99 additions and 87 deletions

View file

@ -22,7 +22,8 @@ use mir::transform as mir_pass;
use syntax::ast::{NodeId, Name}; use syntax::ast::{NodeId, Name};
use errors::{self, DiagnosticBuilder}; 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::json::JsonEmitter;
use syntax::feature_gate; use syntax::feature_gate;
use syntax::parse; use syntax::parse;
@ -439,7 +440,7 @@ pub fn build_session_with_codemap(sopts: config::Options,
config::ErrorOutputType::HumanReadable(color_config) => { config::ErrorOutputType::HumanReadable(color_config) => {
Box::new(EmitterWriter::stderr(color_config, Box::new(EmitterWriter::stderr(color_config,
Some(registry), Some(registry),
codemap.clone(), Some(codemap.clone()),
errors::snippet::FormatMode::EnvironmentSelected)) errors::snippet::FormatMode::EnvironmentSelected))
} }
config::ErrorOutputType::Json => { config::ErrorOutputType::Json => {
@ -577,7 +578,10 @@ 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 mut emitter: Box<Emitter> = match output {
config::ErrorOutputType::HumanReadable(color_config) => { 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()), config::ErrorOutputType::Json => Box::new(JsonEmitter::basic()),
}; };
@ -588,7 +592,10 @@ pub fn early_error(output: config::ErrorOutputType, msg: &str) -> ! {
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 mut emitter: Box<Emitter> = match output {
config::ErrorOutputType::HumanReadable(color_config) => { 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()), config::ErrorOutputType::Json => Box::new(JsonEmitter::basic()),
}; };

View file

@ -100,6 +100,7 @@ use syntax::feature_gate::{GatedCfg, UnstableFeatures};
use syntax::parse::{self, PResult}; use syntax::parse::{self, PResult};
use syntax_pos::MultiSpan; use syntax_pos::MultiSpan;
use errors::emitter::Emitter; use errors::emitter::Emitter;
use errors::snippet::FormatMode;
#[cfg(test)] #[cfg(test)]
pub mod test; pub mod test;
@ -139,7 +140,10 @@ pub fn run(args: Vec<String>) -> isize {
Some(sess) => sess.fatal(&abort_msg(err_count)), Some(sess) => sess.fatal(&abort_msg(err_count)),
None => { None => {
let mut emitter = let mut emitter =
errors::emitter::BasicEmitter::stderr(errors::ColorConfig::Auto); errors::emitter::EmitterWriter::stderr(errors::ColorConfig::Auto,
None,
None,
FormatMode::EnvironmentSelected);
emitter.emit(&MultiSpan::new(), &abort_msg(err_count), None, emitter.emit(&MultiSpan::new(), &abort_msg(err_count), None,
errors::Level::Fatal); errors::Level::Fatal);
exit_on_err(); exit_on_err();
@ -375,7 +379,10 @@ fn check_cfg(sopts: &config::Options,
output: ErrorOutputType) { output: ErrorOutputType) {
let mut emitter: Box<Emitter> = match output { let mut emitter: Box<Emitter> = match output {
config::ErrorOutputType::HumanReadable(color_config) => { 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()), config::ErrorOutputType::Json => Box::new(json::JsonEmitter::basic()),
}; };
@ -1046,7 +1053,11 @@ 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 = errors::emitter::BasicEmitter::stderr(errors::ColorConfig::Auto); let mut emitter =
errors::emitter::EmitterWriter::stderr(errors::ColorConfig::Auto,
None,
None,
FormatMode::EnvironmentSelected);
// 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.

View file

@ -117,42 +117,10 @@ impl ColorConfig {
} }
} }
/// A basic emitter for when we don't have access to a codemap or registry. Used
/// for reporting very early errors, etc.
pub struct BasicEmitter {
dst: Destination,
}
impl CoreEmitter for BasicEmitter {
fn emit_message(&mut self,
_rsp: &RenderSpan,
msg: &str,
code: Option<&str>,
lvl: Level,
_is_header: bool,
_show_snippet: bool) {
// we ignore the span as we have no access to a codemap at this point
if let Err(e) = print_diagnostic(&mut self.dst, "", lvl, msg, code) {
panic!("failed to print diagnostics: {:?}", e);
}
}
}
impl BasicEmitter {
pub fn stderr(color_config: ColorConfig) -> BasicEmitter {
if color_config.use_color() {
let dst = Destination::from_stderr();
BasicEmitter { dst: dst }
} else {
BasicEmitter { dst: Raw(Box::new(io::stderr())) }
}
}
}
pub struct EmitterWriter { pub struct EmitterWriter {
dst: Destination, dst: Destination,
registry: Option<registry::Registry>, registry: Option<registry::Registry>,
cm: Rc<CodeMapper>, cm: Option<Rc<CodeMapper>>,
/// Is this the first error emitted thus far? If not, we emit a /// Is this the first error emitted thus far? If not, we emit a
/// `\n` before the top-level errors. /// `\n` before the top-level errors.
@ -194,7 +162,7 @@ macro_rules! println_maybe_styled {
impl EmitterWriter { impl EmitterWriter {
pub fn stderr(color_config: ColorConfig, pub fn stderr(color_config: ColorConfig,
registry: Option<registry::Registry>, registry: Option<registry::Registry>,
code_map: Rc<CodeMapper>, code_map: Option<Rc<CodeMapper>>,
format_mode: FormatMode) format_mode: FormatMode)
-> EmitterWriter { -> EmitterWriter {
if color_config.use_color() { if color_config.use_color() {
@ -215,7 +183,7 @@ impl EmitterWriter {
pub fn new(dst: Box<Write + Send>, pub fn new(dst: Box<Write + Send>,
registry: Option<registry::Registry>, registry: Option<registry::Registry>,
code_map: Rc<CodeMapper>, code_map: Option<Rc<CodeMapper>>,
format_mode: FormatMode) format_mode: FormatMode)
-> EmitterWriter { -> EmitterWriter {
EmitterWriter { dst: Raw(dst), EmitterWriter { dst: Raw(dst),
@ -257,7 +225,11 @@ impl EmitterWriter {
if old_school { if old_school {
let loc = match rsp.span().primary_span() { let loc = match rsp.span().primary_span() {
Some(COMMAND_LINE_SP) | Some(DUMMY_SP) => "".to_string(), Some(COMMAND_LINE_SP) | Some(DUMMY_SP) => "".to_string(),
Some(ps) => self.cm.span_to_string(ps), Some(ps) => if let Some(ref cm) = self.cm {
cm.span_to_string(ps)
} else {
"".to_string()
},
None => "".to_string() None => "".to_string()
}; };
print_diagnostic(&mut self.dst, &loc, lvl, msg, Some(code))? print_diagnostic(&mut self.dst, &loc, lvl, msg, Some(code))?
@ -270,7 +242,11 @@ impl EmitterWriter {
if old_school { if old_school {
let loc = match rsp.span().primary_span() { let loc = match rsp.span().primary_span() {
Some(COMMAND_LINE_SP) | Some(DUMMY_SP) => "".to_string(), Some(COMMAND_LINE_SP) | Some(DUMMY_SP) => "".to_string(),
Some(ps) => self.cm.span_to_string(ps), Some(ps) => if let Some(ref cm) = self.cm {
cm.span_to_string(ps)
} else {
"".to_string()
},
None => "".to_string() None => "".to_string()
}; };
print_diagnostic(&mut self.dst, &loc, lvl, msg, code)? print_diagnostic(&mut self.dst, &loc, lvl, msg, code)?
@ -316,7 +292,11 @@ impl EmitterWriter {
.is_some() => { .is_some() => {
let loc = match rsp.span().primary_span() { let loc = match rsp.span().primary_span() {
Some(COMMAND_LINE_SP) | Some(DUMMY_SP) => "".to_string(), Some(COMMAND_LINE_SP) | Some(DUMMY_SP) => "".to_string(),
Some(ps) => self.cm.span_to_string(ps), Some(ps) => if let Some(ref cm) = self.cm {
cm.span_to_string(ps)
} else {
"".to_string()
},
None => "".to_string() None => "".to_string()
}; };
let msg = "run `rustc --explain ".to_string() + &code.to_string() + let msg = "run `rustc --explain ".to_string() + &code.to_string() +
@ -335,10 +315,12 @@ impl EmitterWriter {
use std::borrow::Borrow; use std::borrow::Borrow;
let primary_span = suggestion.msp.primary_span().unwrap(); let primary_span = suggestion.msp.primary_span().unwrap();
let lines = self.cm.span_to_lines(primary_span).unwrap(); if let Some(ref cm) = self.cm {
let lines = cm.span_to_lines(primary_span).unwrap();
assert!(!lines.lines.is_empty()); assert!(!lines.lines.is_empty());
let complete = suggestion.splice_lines(self.cm.borrow()); let complete = suggestion.splice_lines(cm.borrow());
let line_count = cmp::min(lines.lines.len(), MAX_HIGHLIGHT_LINES); let line_count = cmp::min(lines.lines.len(), MAX_HIGHLIGHT_LINES);
let display_lines = &lines.lines[..line_count]; let display_lines = &lines.lines[..line_count];
@ -360,7 +342,7 @@ impl EmitterWriter {
write!(&mut self.dst, "{0:1$} {0:2$} ...\n", write!(&mut self.dst, "{0:1$} {0:2$} ...\n",
"", fm.name.len(), max_digits)?; "", fm.name.len(), max_digits)?;
} }
}
Ok(()) Ok(())
} }
@ -369,20 +351,26 @@ impl EmitterWriter {
lvl: Level) lvl: Level)
-> io::Result<()> -> io::Result<()>
{ {
// Check to see if we have any lines to highlight, exit early if not
match self.cm {
None => return Ok(()),
_ => ()
}
let old_school = match self.format_mode { let old_school = match self.format_mode {
FormatMode::NewErrorFormat => false, FormatMode::NewErrorFormat => false,
FormatMode::OriginalErrorFormat => true, FormatMode::OriginalErrorFormat => true,
FormatMode::EnvironmentSelected => check_old_skool() FormatMode::EnvironmentSelected => check_old_skool()
}; };
let mut snippet_data = SnippetData::new(self.cm.clone(), let mut snippet_data = SnippetData::new(self.cm.as_ref().unwrap().clone(),
msp.primary_span(), msp.primary_span(),
self.format_mode.clone()); self.format_mode.clone());
if old_school { if old_school {
let mut output_vec = vec![]; let mut output_vec = vec![];
for span_label in msp.span_labels() { for span_label in msp.span_labels() {
let mut snippet_data = SnippetData::new(self.cm.clone(), let mut snippet_data = SnippetData::new(self.cm.as_ref().unwrap().clone(),
Some(span_label.span), Some(span_label.span),
self.format_mode.clone()); self.format_mode.clone());
@ -431,17 +419,19 @@ impl EmitterWriter {
fn print_macro_backtrace(&mut self, fn print_macro_backtrace(&mut self,
sp: Span) sp: Span)
-> io::Result<()> { -> io::Result<()> {
for trace in self.cm.macro_backtrace(sp) { if let Some(ref cm) = self.cm {
for trace in cm.macro_backtrace(sp) {
let mut diag_string = let mut diag_string =
format!("in this expansion of {}", trace.macro_decl_name); format!("in this expansion of {}", trace.macro_decl_name);
if let Some(def_site_span) = trace.def_site_span { if let Some(def_site_span) = trace.def_site_span {
diag_string.push_str( diag_string.push_str(
&format!(" (defined in {})", &format!(" (defined in {})",
self.cm.span_to_filename(def_site_span))); cm.span_to_filename(def_site_span)));
} }
let snippet = self.cm.span_to_string(trace.call_site); let snippet = cm.span_to_string(trace.call_site);
print_diagnostic(&mut self.dst, &snippet, Note, &diag_string, None)?; print_diagnostic(&mut self.dst, &snippet, Note, &diag_string, None)?;
} }
}
Ok(()) Ok(())
} }
} }

View file

@ -423,7 +423,7 @@ impl Handler {
registry: Option<registry::Registry>, registry: Option<registry::Registry>,
can_emit_warnings: bool, can_emit_warnings: bool,
treat_err_as_bug: bool, treat_err_as_bug: bool,
cm: Rc<CodeMapper>) cm: Option<Rc<CodeMapper>>)
-> Handler { -> Handler {
let emitter = Box::new(EmitterWriter::stderr(color_config, registry, cm, let emitter = Box::new(EmitterWriter::stderr(color_config, registry, cm,
snippet::FormatMode::EnvironmentSelected)); snippet::FormatMode::EnvironmentSelected));

View file

@ -131,7 +131,7 @@ pub fn run_core(search_paths: SearchPaths,
None, None,
true, true,
false, false,
codemap.clone()); Some(codemap.clone()));
let dep_graph = DepGraph::new(false); let dep_graph = DepGraph::new(false);
let _ignore = dep_graph.in_ignore(); let _ignore = dep_graph.in_ignore();

View file

@ -77,7 +77,7 @@ pub fn run(input: &str,
None, None,
true, true,
false, false,
codemap.clone()); Some(codemap.clone()));
let dep_graph = DepGraph::new(false); let dep_graph = DepGraph::new(false);
let _ignore = dep_graph.in_ignore(); let _ignore = dep_graph.in_ignore();
@ -229,7 +229,7 @@ fn runtest(test: &str, cratename: &str, cfgs: Vec<String>, libs: SearchPaths,
let codemap = Rc::new(CodeMap::new()); let codemap = Rc::new(CodeMap::new());
let emitter = errors::emitter::EmitterWriter::new(box Sink(data.clone()), let emitter = errors::emitter::EmitterWriter::new(box Sink(data.clone()),
None, None,
codemap.clone(), Some(codemap.clone()),
errors::snippet::FormatMode::EnvironmentSelected); errors::snippet::FormatMode::EnvironmentSelected);
let old = io::set_panic(box Sink(data.clone())); let old = io::set_panic(box Sink(data.clone()));
let _bomb = Bomb(data.clone(), old.unwrap_or(box io::stdout())); let _bomb = Bomb(data.clone(), old.unwrap_or(box io::stdout()));

View file

@ -1235,7 +1235,7 @@ r"blork2.rs:2:1: 2:12
let cm = Rc::new(CodeMap::new()); let cm = Rc::new(CodeMap::new());
let mut ew = EmitterWriter::new(Box::new(Sink(data.clone())), let mut ew = EmitterWriter::new(Box::new(Sink(data.clone())),
None, None,
cm.clone(), Some(cm.clone()),
FormatMode::NewErrorFormat); FormatMode::NewErrorFormat);
let content = "abcdefg let content = "abcdefg
koksi koksi
@ -1321,7 +1321,7 @@ r"blork2.rs:2:1: 2:12
let cm = Rc::new(CodeMap::new()); let cm = Rc::new(CodeMap::new());
let mut diag = EmitterWriter::new(Box::new(Sink(data.clone())), let mut diag = EmitterWriter::new(Box::new(Sink(data.clone())),
None, None,
cm.clone(), Some(cm.clone()),
FormatMode::NewErrorFormat); FormatMode::NewErrorFormat);
let inp = "_____aaaaaa____bbbbbb__cccccdd_"; let inp = "_____aaaaaa____bbbbbb__cccccdd_";
@ -1377,7 +1377,7 @@ r"blork2.rs:2:1: 2:12
let cm = Rc::new(CodeMap::new()); let cm = Rc::new(CodeMap::new());
let mut diag = EmitterWriter::new(Box::new(Sink(data.clone())), let mut diag = EmitterWriter::new(Box::new(Sink(data.clone())),
None, None,
cm.clone(), Some(cm.clone()),
FormatMode::NewErrorFormat); FormatMode::NewErrorFormat);
let inp = "aaaaa\n\ let inp = "aaaaa\n\

View file

@ -1686,7 +1686,7 @@ mod tests {
// FIXME (#22405): Replace `Box::new` with `box` here when/if possible. // FIXME (#22405): Replace `Box::new` with `box` here when/if possible.
let emitter = errors::emitter::EmitterWriter::new(Box::new(io::sink()), let emitter = errors::emitter::EmitterWriter::new(Box::new(io::sink()),
None, None,
cm, Some(cm),
errors::snippet::FormatMode::EnvironmentSelected); errors::snippet::FormatMode::EnvironmentSelected);
errors::Handler::with_emitter(true, false, Box::new(emitter)) errors::Handler::with_emitter(true, false, Box::new(emitter))
} }

View file

@ -50,7 +50,11 @@ pub struct ParseSess {
impl ParseSess { impl ParseSess {
pub fn new() -> ParseSess { pub fn new() -> ParseSess {
let cm = Rc::new(CodeMap::new()); let cm = Rc::new(CodeMap::new());
let handler = Handler::with_tty_emitter(ColorConfig::Auto, None, true, false, cm.clone()); let handler = Handler::with_tty_emitter(ColorConfig::Auto,
None,
true,
false,
Some(cm.clone()));
ParseSess::with_span_handler(handler, cm) ParseSess::with_span_handler(handler, cm)
} }