Skip to content

Commit 8f044fa

Browse files
author
Jonathan Turner
committed
Remove BasicEmitter
1 parent 3c85f41 commit 8f044fa

File tree

9 files changed

+101
-89
lines changed

9 files changed

+101
-89
lines changed

src/librustc/session/mod.rs

Lines changed: 11 additions & 4 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 => {
@@ -577,7 +578,10 @@ unsafe fn configure_llvm(sess: &Session) {
577578
pub fn early_error(output: config::ErrorOutputType, msg: &str) -> ! {
578579
let mut 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
};
@@ -588,7 +592,10 @@ pub fn early_error(output: config::ErrorOutputType, msg: &str) -> ! {
588592
pub fn early_warn(output: config::ErrorOutputType, msg: &str) {
589593
let mut emitter: Box<Emitter> = match output {
590594
config::ErrorOutputType::HumanReadable(color_config) => {
591-
Box::new(BasicEmitter::stderr(color_config))
595+
Box::new(EmitterWriter::stderr(color_config,
596+
None,
597+
None,
598+
FormatMode::EnvironmentSelected))
592599
}
593600
config::ErrorOutputType::Json => Box::new(JsonEmitter::basic()),
594601
};

src/librustc_driver/lib.rs

Lines changed: 14 additions & 3 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;
@@ -139,7 +140,10 @@ pub fn run(args: Vec<String>) -> isize {
139140
Some(sess) => sess.fatal(&abort_msg(err_count)),
140141
None => {
141142
let mut emitter =
142-
errors::emitter::BasicEmitter::stderr(errors::ColorConfig::Auto);
143+
errors::emitter::EmitterWriter::stderr(errors::ColorConfig::Auto,
144+
None,
145+
None,
146+
FormatMode::EnvironmentSelected);
143147
emitter.emit(&MultiSpan::new(), &abort_msg(err_count), None,
144148
errors::Level::Fatal);
145149
exit_on_err();
@@ -375,7 +379,10 @@ fn check_cfg(sopts: &config::Options,
375379
output: ErrorOutputType) {
376380
let mut emitter: Box<Emitter> = match output {
377381
config::ErrorOutputType::HumanReadable(color_config) => {
378-
Box::new(errors::emitter::BasicEmitter::stderr(color_config))
382+
Box::new(errors::emitter::EmitterWriter::stderr(color_config,
383+
None,
384+
None,
385+
FormatMode::EnvironmentSelected))
379386
}
380387
config::ErrorOutputType::Json => Box::new(json::JsonEmitter::basic()),
381388
};
@@ -1046,7 +1053,11 @@ pub fn monitor<F: FnOnce() + Send + 'static>(f: F) {
10461053
if let Err(value) = thread.unwrap().join() {
10471054
// Thread panicked without emitting a fatal diagnostic
10481055
if !value.is::<errors::FatalError>() {
1049-
let mut emitter = errors::emitter::BasicEmitter::stderr(errors::ColorConfig::Auto);
1056+
let mut emitter =
1057+
errors::emitter::EmitterWriter::stderr(errors::ColorConfig::Auto,
1058+
None,
1059+
None,
1060+
FormatMode::EnvironmentSelected);
10501061

10511062
// a .span_bug or .bug call has already printed what
10521063
// it wants to print.

src/librustc_errors/emitter.rs

Lines changed: 63 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -117,42 +117,10 @@ impl ColorConfig {
117117
}
118118
}
119119

120-
/// A basic emitter for when we don't have access to a codemap or registry. Used
121-
/// for reporting very early errors, etc.
122-
pub struct BasicEmitter {
123-
dst: Destination,
124-
}
125-
126-
impl CoreEmitter for BasicEmitter {
127-
fn emit_message(&mut self,
128-
_rsp: &RenderSpan,
129-
msg: &str,
130-
code: Option<&str>,
131-
lvl: Level,
132-
_is_header: bool,
133-
_show_snippet: bool) {
134-
// we ignore the span as we have no access to a codemap at this point
135-
if let Err(e) = print_diagnostic(&mut self.dst, "", lvl, msg, code) {
136-
panic!("failed to print diagnostics: {:?}", e);
137-
}
138-
}
139-
}
140-
141-
impl BasicEmitter {
142-
pub fn stderr(color_config: ColorConfig) -> BasicEmitter {
143-
if color_config.use_color() {
144-
let dst = Destination::from_stderr();
145-
BasicEmitter { dst: dst }
146-
} else {
147-
BasicEmitter { dst: Raw(Box::new(io::stderr())) }
148-
}
149-
}
150-
}
151-
152120
pub struct EmitterWriter {
153121
dst: Destination,
154122
registry: Option<registry::Registry>,
155-
cm: Rc<CodeMapper>,
123+
cm: Option<Rc<CodeMapper>>,
156124

157125
/// Is this the first error emitted thus far? If not, we emit a
158126
/// `\n` before the top-level errors.
@@ -194,7 +162,7 @@ macro_rules! println_maybe_styled {
194162
impl EmitterWriter {
195163
pub fn stderr(color_config: ColorConfig,
196164
registry: Option<registry::Registry>,
197-
code_map: Rc<CodeMapper>,
165+
code_map: Option<Rc<CodeMapper>>,
198166
format_mode: FormatMode)
199167
-> EmitterWriter {
200168
if color_config.use_color() {
@@ -215,7 +183,7 @@ impl EmitterWriter {
215183

216184
pub fn new(dst: Box<Write + Send>,
217185
registry: Option<registry::Registry>,
218-
code_map: Rc<CodeMapper>,
186+
code_map: Option<Rc<CodeMapper>>,
219187
format_mode: FormatMode)
220188
-> EmitterWriter {
221189
EmitterWriter { dst: Raw(dst),
@@ -257,7 +225,11 @@ impl EmitterWriter {
257225
if old_school {
258226
let loc = match rsp.span().primary_span() {
259227
Some(COMMAND_LINE_SP) | Some(DUMMY_SP) => "".to_string(),
260-
Some(ps) => self.cm.span_to_string(ps),
228+
Some(ps) => if let Some(ref cm) = self.cm {
229+
cm.span_to_string(ps)
230+
} else {
231+
"".to_string()
232+
},
261233
None => "".to_string()
262234
};
263235
print_diagnostic(&mut self.dst, &loc, lvl, msg, Some(code))?
@@ -270,7 +242,11 @@ impl EmitterWriter {
270242
if old_school {
271243
let loc = match rsp.span().primary_span() {
272244
Some(COMMAND_LINE_SP) | Some(DUMMY_SP) => "".to_string(),
273-
Some(ps) => self.cm.span_to_string(ps),
245+
Some(ps) => if let Some(ref cm) = self.cm {
246+
cm.span_to_string(ps)
247+
} else {
248+
"".to_string()
249+
},
274250
None => "".to_string()
275251
};
276252
print_diagnostic(&mut self.dst, &loc, lvl, msg, code)?
@@ -316,7 +292,11 @@ impl EmitterWriter {
316292
.is_some() => {
317293
let loc = match rsp.span().primary_span() {
318294
Some(COMMAND_LINE_SP) | Some(DUMMY_SP) => "".to_string(),
319-
Some(ps) => self.cm.span_to_string(ps),
295+
Some(ps) => if let Some(ref cm) = self.cm {
296+
cm.span_to_string(ps)
297+
} else {
298+
"".to_string()
299+
},
320300
None => "".to_string()
321301
};
322302
let msg = "run `rustc --explain ".to_string() + &code.to_string() +
@@ -335,32 +315,34 @@ impl EmitterWriter {
335315
use std::borrow::Borrow;
336316

337317
let primary_span = suggestion.msp.primary_span().unwrap();
338-
let lines = self.cm.span_to_lines(primary_span).unwrap();
339-
assert!(!lines.lines.is_empty());
340-
341-
let complete = suggestion.splice_lines(self.cm.borrow());
342-
let line_count = cmp::min(lines.lines.len(), MAX_HIGHLIGHT_LINES);
343-
let display_lines = &lines.lines[..line_count];
344-
345-
let fm = &*lines.file;
346-
// Calculate the widest number to format evenly
347-
let max_digits = line_num_max_digits(display_lines.last().unwrap());
348-
349-
// print the suggestion without any line numbers, but leave
350-
// space for them. This helps with lining up with previous
351-
// snippets from the actual error being reported.
352-
let mut lines = complete.lines();
353-
for line in lines.by_ref().take(MAX_HIGHLIGHT_LINES) {
354-
write!(&mut self.dst, "{0}:{1:2$} {3}\n",
355-
fm.name, "", max_digits, line)?;
356-
}
318+
if let Some(ref cm) = self.cm {
319+
let lines = cm.span_to_lines(primary_span).unwrap();
320+
321+
assert!(!lines.lines.is_empty());
322+
323+
let complete = suggestion.splice_lines(cm.borrow());
324+
let line_count = cmp::min(lines.lines.len(), MAX_HIGHLIGHT_LINES);
325+
let display_lines = &lines.lines[..line_count];
326+
327+
let fm = &*lines.file;
328+
// Calculate the widest number to format evenly
329+
let max_digits = line_num_max_digits(display_lines.last().unwrap());
330+
331+
// print the suggestion without any line numbers, but leave
332+
// space for them. This helps with lining up with previous
333+
// snippets from the actual error being reported.
334+
let mut lines = complete.lines();
335+
for line in lines.by_ref().take(MAX_HIGHLIGHT_LINES) {
336+
write!(&mut self.dst, "{0}:{1:2$} {3}\n",
337+
fm.name, "", max_digits, line)?;
338+
}
357339

358-
// if we elided some lines, add an ellipsis
359-
if let Some(_) = lines.next() {
360-
write!(&mut self.dst, "{0:1$} {0:2$} ...\n",
361-
"", fm.name.len(), max_digits)?;
340+
// if we elided some lines, add an ellipsis
341+
if let Some(_) = lines.next() {
342+
write!(&mut self.dst, "{0:1$} {0:2$} ...\n",
343+
"", fm.name.len(), max_digits)?;
344+
}
362345
}
363-
364346
Ok(())
365347
}
366348

@@ -369,20 +351,26 @@ impl EmitterWriter {
369351
lvl: Level)
370352
-> io::Result<()>
371353
{
354+
// Check to see if we have any lines to highlight, exit early if not
355+
match self.cm {
356+
None => return Ok(()),
357+
_ => ()
358+
}
359+
372360
let old_school = match self.format_mode {
373361
FormatMode::NewErrorFormat => false,
374362
FormatMode::OriginalErrorFormat => true,
375363
FormatMode::EnvironmentSelected => check_old_skool()
376364
};
377365

378-
let mut snippet_data = SnippetData::new(self.cm.clone(),
366+
let mut snippet_data = SnippetData::new(self.cm.as_ref().unwrap().clone(),
379367
msp.primary_span(),
380368
self.format_mode.clone());
381369
if old_school {
382370
let mut output_vec = vec![];
383371

384372
for span_label in msp.span_labels() {
385-
let mut snippet_data = SnippetData::new(self.cm.clone(),
373+
let mut snippet_data = SnippetData::new(self.cm.as_ref().unwrap().clone(),
386374
Some(span_label.span),
387375
self.format_mode.clone());
388376

@@ -431,16 +419,18 @@ impl EmitterWriter {
431419
fn print_macro_backtrace(&mut self,
432420
sp: Span)
433421
-> io::Result<()> {
434-
for trace in self.cm.macro_backtrace(sp) {
435-
let mut diag_string =
436-
format!("in this expansion of {}", trace.macro_decl_name);
437-
if let Some(def_site_span) = trace.def_site_span {
438-
diag_string.push_str(
439-
&format!(" (defined in {})",
440-
self.cm.span_to_filename(def_site_span)));
422+
if let Some(ref cm) = self.cm {
423+
for trace in cm.macro_backtrace(sp) {
424+
let mut diag_string =
425+
format!("in this expansion of {}", trace.macro_decl_name);
426+
if let Some(def_site_span) = trace.def_site_span {
427+
diag_string.push_str(
428+
&format!(" (defined in {})",
429+
cm.span_to_filename(def_site_span)));
430+
}
431+
let snippet = cm.span_to_string(trace.call_site);
432+
print_diagnostic(&mut self.dst, &snippet, Note, &diag_string, None)?;
441433
}
442-
let snippet = self.cm.span_to_string(trace.call_site);
443-
print_diagnostic(&mut self.dst, &snippet, Note, &diag_string, None)?;
444434
}
445435
Ok(())
446436
}

src/librustc_errors/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,7 @@ impl Handler {
423423
registry: Option<registry::Registry>,
424424
can_emit_warnings: bool,
425425
treat_err_as_bug: bool,
426-
cm: Rc<CodeMapper>)
426+
cm: Option<Rc<CodeMapper>>)
427427
-> Handler {
428428
let emitter = Box::new(EmitterWriter::stderr(color_config, registry, cm,
429429
snippet::FormatMode::EnvironmentSelected));

src/librustdoc/core.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ pub fn run_core(search_paths: SearchPaths,
131131
None,
132132
true,
133133
false,
134-
codemap.clone());
134+
Some(codemap.clone()));
135135

136136
let dep_graph = DepGraph::new(false);
137137
let _ignore = dep_graph.in_ignore();

src/librustdoc/test.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ pub fn run(input: &str,
7777
None,
7878
true,
7979
false,
80-
codemap.clone());
80+
Some(codemap.clone()));
8181

8282
let dep_graph = DepGraph::new(false);
8383
let _ignore = dep_graph.in_ignore();
@@ -229,7 +229,7 @@ fn runtest(test: &str, cratename: &str, cfgs: Vec<String>, libs: SearchPaths,
229229
let codemap = Rc::new(CodeMap::new());
230230
let emitter = errors::emitter::EmitterWriter::new(box Sink(data.clone()),
231231
None,
232-
codemap.clone(),
232+
Some(codemap.clone()),
233233
errors::snippet::FormatMode::EnvironmentSelected);
234234
let old = io::set_panic(box Sink(data.clone()));
235235
let _bomb = Bomb(data.clone(), old.unwrap_or(box io::stdout()));

src/libsyntax/codemap.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1235,7 +1235,7 @@ r"blork2.rs:2:1: 2:12
12351235
let cm = Rc::new(CodeMap::new());
12361236
let mut ew = EmitterWriter::new(Box::new(Sink(data.clone())),
12371237
None,
1238-
cm.clone(),
1238+
Some(cm.clone()),
12391239
FormatMode::NewErrorFormat);
12401240
let content = "abcdefg
12411241
koksi
@@ -1321,7 +1321,7 @@ r"blork2.rs:2:1: 2:12
13211321
let cm = Rc::new(CodeMap::new());
13221322
let mut diag = EmitterWriter::new(Box::new(Sink(data.clone())),
13231323
None,
1324-
cm.clone(),
1324+
Some(cm.clone()),
13251325
FormatMode::NewErrorFormat);
13261326

13271327
let inp = "_____aaaaaa____bbbbbb__cccccdd_";
@@ -1377,7 +1377,7 @@ r"blork2.rs:2:1: 2:12
13771377
let cm = Rc::new(CodeMap::new());
13781378
let mut diag = EmitterWriter::new(Box::new(Sink(data.clone())),
13791379
None,
1380-
cm.clone(),
1380+
Some(cm.clone()),
13811381
FormatMode::NewErrorFormat);
13821382

13831383
let inp = "aaaaa\n\

src/libsyntax/parse/lexer/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1686,7 +1686,7 @@ mod tests {
16861686
// FIXME (#22405): Replace `Box::new` with `box` here when/if possible.
16871687
let emitter = errors::emitter::EmitterWriter::new(Box::new(io::sink()),
16881688
None,
1689-
cm,
1689+
Some(cm),
16901690
errors::snippet::FormatMode::EnvironmentSelected);
16911691
errors::Handler::with_emitter(true, false, Box::new(emitter))
16921692
}

src/libsyntax/parse/mod.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,11 @@ pub struct ParseSess {
5050
impl ParseSess {
5151
pub fn new() -> ParseSess {
5252
let cm = Rc::new(CodeMap::new());
53-
let handler = Handler::with_tty_emitter(ColorConfig::Auto, None, true, false, cm.clone());
53+
let handler = Handler::with_tty_emitter(ColorConfig::Auto,
54+
None,
55+
true,
56+
false,
57+
Some(cm.clone()));
5458
ParseSess::with_span_handler(handler, cm)
5559
}
5660

0 commit comments

Comments
 (0)