Skip to content

Commit 55f0688

Browse files
author
Jonathan Turner
committed
Remove emit from emitter, leaving emit_struct
1 parent 8f044fa commit 55f0688

File tree

5 files changed

+71
-71
lines changed

5 files changed

+71
-71
lines changed

src/librustc/session/mod.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -576,7 +576,7 @@ unsafe fn configure_llvm(sess: &Session) {
576576
}
577577

578578
pub fn early_error(output: config::ErrorOutputType, msg: &str) -> ! {
579-
let mut emitter: Box<Emitter> = match output {
579+
let emitter: Box<Emitter> = match output {
580580
config::ErrorOutputType::HumanReadable(color_config) => {
581581
Box::new(EmitterWriter::stderr(color_config,
582582
None,
@@ -585,12 +585,13 @@ pub fn early_error(output: config::ErrorOutputType, msg: &str) -> ! {
585585
}
586586
config::ErrorOutputType::Json => Box::new(JsonEmitter::basic()),
587587
};
588-
emitter.emit(&MultiSpan::new(), msg, None, errors::Level::Fatal);
588+
let handler = errors::Handler::with_emitter(true, false, emitter);
589+
handler.emit(&MultiSpan::new(), msg, errors::Level::Fatal);
589590
panic!(errors::FatalError);
590591
}
591592

592593
pub fn early_warn(output: config::ErrorOutputType, msg: &str) {
593-
let mut emitter: Box<Emitter> = match output {
594+
let emitter: Box<Emitter> = match output {
594595
config::ErrorOutputType::HumanReadable(color_config) => {
595596
Box::new(EmitterWriter::stderr(color_config,
596597
None,
@@ -599,7 +600,8 @@ pub fn early_warn(output: config::ErrorOutputType, msg: &str) {
599600
}
600601
config::ErrorOutputType::Json => Box::new(JsonEmitter::basic()),
601602
};
602-
emitter.emit(&MultiSpan::new(), msg, None, errors::Level::Warning);
603+
let handler = errors::Handler::with_emitter(true, false, emitter);
604+
handler.emit(&MultiSpan::new(), msg, errors::Level::Warning);
603605
}
604606

605607
// Err(0) means compilation was stopped, but no errors were found.

src/librustc_driver/lib.rs

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -139,13 +139,15 @@ pub fn run(args: Vec<String>) -> isize {
139139
match session {
140140
Some(sess) => sess.fatal(&abort_msg(err_count)),
141141
None => {
142-
let mut emitter =
142+
let emitter =
143143
errors::emitter::EmitterWriter::stderr(errors::ColorConfig::Auto,
144144
None,
145145
None,
146146
FormatMode::EnvironmentSelected);
147-
emitter.emit(&MultiSpan::new(), &abort_msg(err_count), None,
148-
errors::Level::Fatal);
147+
let handler = errors::Handler::with_emitter(true, false, Box::new(emitter));
148+
handler.emit(&MultiSpan::new(),
149+
&abort_msg(err_count),
150+
errors::Level::Fatal);
149151
exit_on_err();
150152
}
151153
}
@@ -377,7 +379,7 @@ fn handle_explain(code: &str,
377379

378380
fn check_cfg(sopts: &config::Options,
379381
output: ErrorOutputType) {
380-
let mut emitter: Box<Emitter> = match output {
382+
let emitter: Box<Emitter> = match output {
381383
config::ErrorOutputType::HumanReadable(color_config) => {
382384
Box::new(errors::emitter::EmitterWriter::stderr(color_config,
383385
None,
@@ -386,17 +388,17 @@ fn check_cfg(sopts: &config::Options,
386388
}
387389
config::ErrorOutputType::Json => Box::new(json::JsonEmitter::basic()),
388390
};
391+
let handler = errors::Handler::with_emitter(true, false, emitter);
389392

390393
let mut saw_invalid_predicate = false;
391394
for item in sopts.cfg.iter() {
392395
match item.node {
393396
ast::MetaItemKind::List(ref pred, _) => {
394397
saw_invalid_predicate = true;
395-
emitter.emit(&MultiSpan::new(),
398+
handler.emit(&MultiSpan::new(),
396399
&format!("invalid predicate in --cfg command line argument: `{}`",
397400
pred),
398-
None,
399-
errors::Level::Fatal);
401+
errors::Level::Fatal);
400402
}
401403
_ => {},
402404
}
@@ -1053,30 +1055,34 @@ pub fn monitor<F: FnOnce() + Send + 'static>(f: F) {
10531055
if let Err(value) = thread.unwrap().join() {
10541056
// Thread panicked without emitting a fatal diagnostic
10551057
if !value.is::<errors::FatalError>() {
1056-
let mut emitter =
1057-
errors::emitter::EmitterWriter::stderr(errors::ColorConfig::Auto,
1058+
let emitter =
1059+
Box::new(errors::emitter::EmitterWriter::stderr(errors::ColorConfig::Auto,
10581060
None,
10591061
None,
1060-
FormatMode::EnvironmentSelected);
1062+
FormatMode::EnvironmentSelected));
1063+
let handler = errors::Handler::with_emitter(true, false, emitter);
10611064

10621065
// a .span_bug or .bug call has already printed what
10631066
// it wants to print.
10641067
if !value.is::<errors::ExplicitBug>() {
1065-
emitter.emit(&MultiSpan::new(), "unexpected panic", None, errors::Level::Bug);
1068+
handler.emit(&MultiSpan::new(),
1069+
"unexpected panic",
1070+
errors::Level::Bug);
10661071
}
10671072

10681073
let xs = ["the compiler unexpectedly panicked. this is a bug.".to_string(),
10691074
format!("we would appreciate a bug report: {}", BUG_REPORT_URL)];
10701075
for note in &xs {
1071-
emitter.emit(&MultiSpan::new(), &note[..], None, errors::Level::Note)
1076+
handler.emit(&MultiSpan::new(),
1077+
&note[..],
1078+
errors::Level::Note);
10721079
}
10731080
if match env::var_os("RUST_BACKTRACE") {
10741081
Some(val) => &val != "0",
10751082
None => false,
10761083
} {
1077-
emitter.emit(&MultiSpan::new(),
1084+
handler.emit(&MultiSpan::new(),
10781085
"run with `RUST_BACKTRACE=1` for a backtrace",
1079-
None,
10801086
errors::Level::Note);
10811087
}
10821088

src/librustc_errors/emitter.rs

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,6 @@ use term;
2828
/// Emitter trait for emitting errors. Do not implement this directly:
2929
/// implement `CoreEmitter` instead.
3030
pub trait Emitter {
31-
/// Emit a standalone diagnostic message.
32-
fn emit(&mut self, span: &MultiSpan, msg: &str, code: Option<&str>, lvl: Level);
33-
3431
/// Emit a structured diagnostic.
3532
fn emit_struct(&mut self, db: &DiagnosticBuilder);
3633
}
@@ -46,19 +43,6 @@ pub trait CoreEmitter {
4643
}
4744

4845
impl<T: CoreEmitter> Emitter for T {
49-
fn emit(&mut self,
50-
msp: &MultiSpan,
51-
msg: &str,
52-
code: Option<&str>,
53-
lvl: Level) {
54-
self.emit_message(&FullSpan(msp.clone()),
55-
msg,
56-
code,
57-
lvl,
58-
true,
59-
true);
60-
}
61-
6246
fn emit_struct(&mut self, db: &DiagnosticBuilder) {
6347
let old_school = check_old_skool();
6448
let db_span = FullSpan(db.span.clone());

src/librustc_errors/lib.rs

Lines changed: 44 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -359,11 +359,20 @@ impl<'a> DiagnosticBuilder<'a> {
359359
fn new(handler: &'a Handler,
360360
level: Level,
361361
message: &str) -> DiagnosticBuilder<'a> {
362+
DiagnosticBuilder::new_with_code(handler, level, None, message)
363+
}
364+
365+
/// Convenience function for internal use, clients should use one of the
366+
/// struct_* methods on Handler.
367+
fn new_with_code(handler: &'a Handler,
368+
level: Level,
369+
code: Option<String>,
370+
message: &str) -> DiagnosticBuilder<'a> {
362371
DiagnosticBuilder {
363372
handler: handler,
364373
level: level,
365374
message: message.to_owned(),
366-
code: None,
375+
code: code,
367376
span: MultiSpan::new(),
368377
children: vec![],
369378
}
@@ -397,10 +406,10 @@ impl<'a> fmt::Debug for DiagnosticBuilder<'a> {
397406
impl<'a> Drop for DiagnosticBuilder<'a> {
398407
fn drop(&mut self) {
399408
if !panicking() && !self.cancelled() {
400-
self.handler.emit.borrow_mut().emit(&MultiSpan::new(),
401-
"Error constructed but not emitted",
402-
None,
403-
Bug);
409+
let mut db = DiagnosticBuilder::new(self.handler,
410+
Bug,
411+
"Error constructed but not emitted");
412+
db.emit();
404413
panic!();
405414
}
406415
}
@@ -588,7 +597,7 @@ impl Handler {
588597
self.bump_err_count();
589598
}
590599
pub fn span_note_without_error<S: Into<MultiSpan>>(&self, sp: S, msg: &str) {
591-
self.emit.borrow_mut().emit(&sp.into(), msg, None, Note);
600+
self.emit(&sp.into(), msg, Note);
592601
}
593602
pub fn span_unimpl<S: Into<MultiSpan>>(&self, sp: S, msg: &str) -> ! {
594603
self.span_bug(sp, &format!("unimplemented {}", msg));
@@ -597,25 +606,40 @@ impl Handler {
597606
if self.treat_err_as_bug {
598607
self.bug(msg);
599608
}
600-
self.emit.borrow_mut().emit(&MultiSpan::new(), msg, None, Fatal);
609+
let mut db = DiagnosticBuilder::new(self,
610+
Fatal,
611+
msg);
612+
db.emit();
601613
self.bump_err_count();
602614
FatalError
603615
}
604616
pub fn err(&self, msg: &str) {
605617
if self.treat_err_as_bug {
606618
self.bug(msg);
607619
}
608-
self.emit.borrow_mut().emit(&MultiSpan::new(), msg, None, Error);
620+
let mut db = DiagnosticBuilder::new(self,
621+
Error,
622+
msg);
623+
db.emit();
609624
self.bump_err_count();
610625
}
611626
pub fn warn(&self, msg: &str) {
612-
self.emit.borrow_mut().emit(&MultiSpan::new(), msg, None, Warning);
627+
let mut db = DiagnosticBuilder::new(self,
628+
Warning,
629+
msg);
630+
db.emit();
613631
}
614632
pub fn note_without_error(&self, msg: &str) {
615-
self.emit.borrow_mut().emit(&MultiSpan::new(), msg, None, Note);
633+
let mut db = DiagnosticBuilder::new(self,
634+
Note,
635+
msg);
636+
db.emit();
616637
}
617638
pub fn bug(&self, msg: &str) -> ! {
618-
self.emit.borrow_mut().emit(&MultiSpan::new(), msg, None, Bug);
639+
let mut db = DiagnosticBuilder::new(self,
640+
Bug,
641+
msg);
642+
db.emit();
619643
panic!(ExplicitBug);
620644
}
621645
pub fn unimpl(&self, msg: &str) -> ! {
@@ -661,7 +685,9 @@ impl Handler {
661685
msg: &str,
662686
lvl: Level) {
663687
if lvl == Warning && !self.can_emit_warnings { return }
664-
self.emit.borrow_mut().emit(&msp, msg, None, lvl);
688+
let mut db = DiagnosticBuilder::new(self, lvl, msg);
689+
db.set_span(msp.clone());
690+
db.emit();
665691
if !self.continue_after_error.get() { self.abort_if_errors(); }
666692
}
667693
pub fn emit_with_code(&self,
@@ -670,7 +696,12 @@ impl Handler {
670696
code: &str,
671697
lvl: Level) {
672698
if lvl == Warning && !self.can_emit_warnings { return }
673-
self.emit.borrow_mut().emit(&msp, msg, Some(code), lvl);
699+
let mut db = DiagnosticBuilder::new_with_code(self,
700+
lvl,
701+
Some(code.to_owned()),
702+
msg);
703+
db.set_span(msp.clone());
704+
db.emit();
674705
if !self.continue_after_error.get() { self.abort_if_errors(); }
675706
}
676707
}

src/libsyntax/json.rs

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
use codemap::CodeMap;
2323
use syntax_pos::{self, MacroBacktrace, Span, SpanLabel, MultiSpan};
2424
use errors::registry::Registry;
25-
use errors::{Level, DiagnosticBuilder, SubDiagnostic, RenderSpan, CodeSuggestion, CodeMapper};
25+
use errors::{DiagnosticBuilder, SubDiagnostic, RenderSpan, CodeSuggestion, CodeMapper};
2626
use errors::emitter::Emitter;
2727

2828
use std::rc::Rc;
@@ -53,13 +53,6 @@ impl JsonEmitter {
5353
}
5454

5555
impl Emitter for JsonEmitter {
56-
fn emit(&mut self, span: &MultiSpan, msg: &str, code: Option<&str>, level: Level) {
57-
let data = Diagnostic::new(span, msg, code, level, self);
58-
if let Err(e) = writeln!(&mut self.dst, "{}", as_json(&data)) {
59-
panic!("failed to print diagnostics: {:?}", e);
60-
}
61-
}
62-
6356
fn emit_struct(&mut self, db: &DiagnosticBuilder) {
6457
let data = Diagnostic::from_diagnostic_builder(db, self);
6558
if let Err(e) = writeln!(&mut self.dst, "{}", as_json(&data)) {
@@ -146,22 +139,6 @@ struct DiagnosticCode {
146139
}
147140

148141
impl<'a> Diagnostic<'a> {
149-
fn new(msp: &MultiSpan,
150-
msg: &'a str,
151-
code: Option<&str>,
152-
level: Level,
153-
je: &JsonEmitter)
154-
-> Diagnostic<'a> {
155-
Diagnostic {
156-
message: msg,
157-
code: DiagnosticCode::map_opt_string(code.map(|c| c.to_owned()), je),
158-
level: level.to_str(),
159-
spans: DiagnosticSpan::from_multispan(msp, je),
160-
children: vec![],
161-
rendered: None,
162-
}
163-
}
164-
165142
fn from_diagnostic_builder<'c>(db: &'c DiagnosticBuilder,
166143
je: &JsonEmitter)
167144
-> Diagnostic<'c> {

0 commit comments

Comments
 (0)