Skip to content

Commit

Permalink
Remove BasicEmitter
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonathan Turner committed Jul 14, 2016
1 parent 3c85f41 commit 8f044fa
Show file tree
Hide file tree
Showing 9 changed files with 101 additions and 89 deletions.
15 changes: 11 additions & 4 deletions src/librustc/session/mod.rs
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 @@ -577,7 +578,10 @@ unsafe fn configure_llvm(sess: &Session) {
pub fn early_error(output: config::ErrorOutputType, msg: &str) -> ! {
let mut 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()),
};
Expand All @@ -588,7 +592,10 @@ pub fn early_error(output: config::ErrorOutputType, msg: &str) -> ! {
pub fn early_warn(output: config::ErrorOutputType, msg: &str) {
let mut 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()),
};
Expand Down
17 changes: 14 additions & 3 deletions src/librustc_driver/lib.rs
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 @@ -139,7 +140,10 @@ pub fn run(args: Vec<String>) -> isize {
Some(sess) => sess.fatal(&abort_msg(err_count)),
None => {
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,
errors::Level::Fatal);
exit_on_err();
Expand Down Expand Up @@ -375,7 +379,10 @@ fn check_cfg(sopts: &config::Options,
output: ErrorOutputType) {
let mut 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()),
};
Expand Down Expand Up @@ -1046,7 +1053,11 @@ 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 mut emitter =
errors::emitter::EmitterWriter::stderr(errors::ColorConfig::Auto,
None,
None,
FormatMode::EnvironmentSelected);

// a .span_bug or .bug call has already printed what
// it wants to print.
Expand Down
136 changes: 63 additions & 73 deletions src/librustc_errors/emitter.rs
Expand Up @@ -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 {
dst: Destination,
registry: Option<registry::Registry>,
cm: Rc<CodeMapper>,
cm: Option<Rc<CodeMapper>>,

/// Is this the first error emitted thus far? If not, we emit a
/// `\n` before the top-level errors.
Expand Down Expand Up @@ -194,7 +162,7 @@ macro_rules! println_maybe_styled {
impl EmitterWriter {
pub fn stderr(color_config: ColorConfig,
registry: Option<registry::Registry>,
code_map: Rc<CodeMapper>,
code_map: Option<Rc<CodeMapper>>,
format_mode: FormatMode)
-> EmitterWriter {
if color_config.use_color() {
Expand All @@ -215,7 +183,7 @@ impl EmitterWriter {

pub fn new(dst: Box<Write + Send>,
registry: Option<registry::Registry>,
code_map: Rc<CodeMapper>,
code_map: Option<Rc<CodeMapper>>,
format_mode: FormatMode)
-> EmitterWriter {
EmitterWriter { dst: Raw(dst),
Expand Down Expand Up @@ -257,7 +225,11 @@ impl EmitterWriter {
if old_school {
let loc = match rsp.span().primary_span() {
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()
};
print_diagnostic(&mut self.dst, &loc, lvl, msg, Some(code))?
Expand All @@ -270,7 +242,11 @@ impl EmitterWriter {
if old_school {
let loc = match rsp.span().primary_span() {
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()
};
print_diagnostic(&mut self.dst, &loc, lvl, msg, code)?
Expand Down Expand Up @@ -316,7 +292,11 @@ impl EmitterWriter {
.is_some() => {
let loc = match rsp.span().primary_span() {
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()
};
let msg = "run `rustc --explain ".to_string() + &code.to_string() +
Expand All @@ -335,32 +315,34 @@ impl EmitterWriter {
use std::borrow::Borrow;

let primary_span = suggestion.msp.primary_span().unwrap();
let lines = self.cm.span_to_lines(primary_span).unwrap();
assert!(!lines.lines.is_empty());

let complete = suggestion.splice_lines(self.cm.borrow());
let line_count = cmp::min(lines.lines.len(), MAX_HIGHLIGHT_LINES);
let display_lines = &lines.lines[..line_count];

let fm = &*lines.file;
// Calculate the widest number to format evenly
let max_digits = line_num_max_digits(display_lines.last().unwrap());

// print the suggestion without any line numbers, but leave
// space for them. This helps with lining up with previous
// snippets from the actual error being reported.
let mut lines = complete.lines();
for line in lines.by_ref().take(MAX_HIGHLIGHT_LINES) {
write!(&mut self.dst, "{0}:{1:2$} {3}\n",
fm.name, "", max_digits, line)?;
}
if let Some(ref cm) = self.cm {
let lines = cm.span_to_lines(primary_span).unwrap();

assert!(!lines.lines.is_empty());

let complete = suggestion.splice_lines(cm.borrow());
let line_count = cmp::min(lines.lines.len(), MAX_HIGHLIGHT_LINES);
let display_lines = &lines.lines[..line_count];

let fm = &*lines.file;
// Calculate the widest number to format evenly
let max_digits = line_num_max_digits(display_lines.last().unwrap());

// print the suggestion without any line numbers, but leave
// space for them. This helps with lining up with previous
// snippets from the actual error being reported.
let mut lines = complete.lines();
for line in lines.by_ref().take(MAX_HIGHLIGHT_LINES) {
write!(&mut self.dst, "{0}:{1:2$} {3}\n",
fm.name, "", max_digits, line)?;
}

// if we elided some lines, add an ellipsis
if let Some(_) = lines.next() {
write!(&mut self.dst, "{0:1$} {0:2$} ...\n",
"", fm.name.len(), max_digits)?;
// if we elided some lines, add an ellipsis
if let Some(_) = lines.next() {
write!(&mut self.dst, "{0:1$} {0:2$} ...\n",
"", fm.name.len(), max_digits)?;
}
}

Ok(())
}

Expand All @@ -369,20 +351,26 @@ impl EmitterWriter {
lvl: Level)
-> 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 {
FormatMode::NewErrorFormat => false,
FormatMode::OriginalErrorFormat => true,
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(),
self.format_mode.clone());
if old_school {
let mut output_vec = vec![];

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),
self.format_mode.clone());

Expand Down Expand Up @@ -431,16 +419,18 @@ impl EmitterWriter {
fn print_macro_backtrace(&mut self,
sp: Span)
-> io::Result<()> {
for trace in self.cm.macro_backtrace(sp) {
let mut diag_string =
format!("in this expansion of {}", trace.macro_decl_name);
if let Some(def_site_span) = trace.def_site_span {
diag_string.push_str(
&format!(" (defined in {})",
self.cm.span_to_filename(def_site_span)));
if let Some(ref cm) = self.cm {
for trace in cm.macro_backtrace(sp) {
let mut diag_string =
format!("in this expansion of {}", trace.macro_decl_name);
if let Some(def_site_span) = trace.def_site_span {
diag_string.push_str(
&format!(" (defined in {})",
cm.span_to_filename(def_site_span)));
}
let snippet = cm.span_to_string(trace.call_site);
print_diagnostic(&mut self.dst, &snippet, Note, &diag_string, None)?;
}
let snippet = self.cm.span_to_string(trace.call_site);
print_diagnostic(&mut self.dst, &snippet, Note, &diag_string, None)?;
}
Ok(())
}
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_errors/lib.rs
Expand Up @@ -423,7 +423,7 @@ impl Handler {
registry: Option<registry::Registry>,
can_emit_warnings: bool,
treat_err_as_bug: bool,
cm: Rc<CodeMapper>)
cm: Option<Rc<CodeMapper>>)
-> Handler {
let emitter = Box::new(EmitterWriter::stderr(color_config, registry, cm,
snippet::FormatMode::EnvironmentSelected));
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/core.rs
Expand Up @@ -131,7 +131,7 @@ pub fn run_core(search_paths: SearchPaths,
None,
true,
false,
codemap.clone());
Some(codemap.clone()));

let dep_graph = DepGraph::new(false);
let _ignore = dep_graph.in_ignore();
Expand Down
4 changes: 2 additions & 2 deletions src/librustdoc/test.rs
Expand Up @@ -77,7 +77,7 @@ pub fn run(input: &str,
None,
true,
false,
codemap.clone());
Some(codemap.clone()));

let dep_graph = DepGraph::new(false);
let _ignore = dep_graph.in_ignore();
Expand Down Expand Up @@ -229,7 +229,7 @@ fn runtest(test: &str, cratename: &str, cfgs: Vec<String>, libs: SearchPaths,
let codemap = Rc::new(CodeMap::new());
let emitter = errors::emitter::EmitterWriter::new(box Sink(data.clone()),
None,
codemap.clone(),
Some(codemap.clone()),
errors::snippet::FormatMode::EnvironmentSelected);
let old = io::set_panic(box Sink(data.clone()));
let _bomb = Bomb(data.clone(), old.unwrap_or(box io::stdout()));
Expand Down
6 changes: 3 additions & 3 deletions src/libsyntax/codemap.rs
Expand Up @@ -1235,7 +1235,7 @@ r"blork2.rs:2:1: 2:12
let cm = Rc::new(CodeMap::new());
let mut ew = EmitterWriter::new(Box::new(Sink(data.clone())),
None,
cm.clone(),
Some(cm.clone()),
FormatMode::NewErrorFormat);
let content = "abcdefg
koksi
Expand Down Expand Up @@ -1321,7 +1321,7 @@ r"blork2.rs:2:1: 2:12
let cm = Rc::new(CodeMap::new());
let mut diag = EmitterWriter::new(Box::new(Sink(data.clone())),
None,
cm.clone(),
Some(cm.clone()),
FormatMode::NewErrorFormat);

let inp = "_____aaaaaa____bbbbbb__cccccdd_";
Expand Down Expand Up @@ -1377,7 +1377,7 @@ r"blork2.rs:2:1: 2:12
let cm = Rc::new(CodeMap::new());
let mut diag = EmitterWriter::new(Box::new(Sink(data.clone())),
None,
cm.clone(),
Some(cm.clone()),
FormatMode::NewErrorFormat);

let inp = "aaaaa\n\
Expand Down
2 changes: 1 addition & 1 deletion src/libsyntax/parse/lexer/mod.rs
Expand Up @@ -1686,7 +1686,7 @@ mod tests {
// FIXME (#22405): Replace `Box::new` with `box` here when/if possible.
let emitter = errors::emitter::EmitterWriter::new(Box::new(io::sink()),
None,
cm,
Some(cm),
errors::snippet::FormatMode::EnvironmentSelected);
errors::Handler::with_emitter(true, false, Box::new(emitter))
}
Expand Down
6 changes: 5 additions & 1 deletion src/libsyntax/parse/mod.rs
Expand Up @@ -50,7 +50,11 @@ pub struct ParseSess {
impl ParseSess {
pub fn new() -> ParseSess {
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)
}

Expand Down

0 comments on commit 8f044fa

Please sign in to comment.