Skip to content

Commit 41a652e

Browse files
committed
WIP factor out RudimentaryEmitter
1 parent 489a6c9 commit 41a652e

File tree

2 files changed

+38
-30
lines changed

2 files changed

+38
-30
lines changed

src/librustc_trans/back/write.rs

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ use llvm::SMDiagnosticRef;
1919
use {CrateTranslation, ModuleTranslation};
2020
use util::common::time;
2121
use util::common::path2cstr;
22-
use syntax::codemap::{self, MultiSpan};
22+
use syntax::codemap::MultiSpan;
2323
use syntax::errors::{self, Handler, Level};
24-
use syntax::errors::emitter::Emitter;
24+
use syntax::errors::emitter::RudimentaryEmitter;
2525

2626
use std::collections::HashMap;
2727
use std::ffi::{CStr, CString};
@@ -100,24 +100,17 @@ impl SharedEmitter {
100100
}
101101
}
102102

103-
impl Emitter for SharedEmitter {
104-
fn emit(&mut self,
105-
sp: &codemap::MultiSpan,
106-
msg: &str,
107-
code: Option<&str>,
108-
lvl: Level) {
109-
assert!(sp.primary_span().is_none(), "SharedEmitter doesn't support spans");
110-
103+
impl RudimentaryEmitter for SharedEmitter {
104+
fn emit_rudimentary(&mut self,
105+
msg: &str,
106+
code: Option<&str>,
107+
lvl: Level) {
111108
self.buffer.lock().unwrap().push(Diagnostic {
112109
msg: msg.to_string(),
113110
code: code.map(|s| s.to_string()),
114111
lvl: lvl,
115112
});
116113
}
117-
118-
fn emit_struct(&mut self, _db: &errors::DiagnosticBuilder) {
119-
bug!("SharedEmitter doesn't support emit_struct");
120-
}
121114
}
122115

123116

src/libsyntax/errors/emitter.rs

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,38 @@ use std::rc::Rc;
2525
use term;
2626

2727
pub trait Emitter {
28+
/// Emit a standalone diagnostic message.
2829
fn emit(&mut self, span: &MultiSpan, msg: &str, code: Option<&str>, lvl: Level);
2930

3031
/// Emit a structured diagnostic.
3132
fn emit_struct(&mut self, db: &DiagnosticBuilder);
3233
}
3334

35+
/// A core trait that can only handle very simple messages: those
36+
/// without spans or any real structure. Used only in specific contexts.
37+
pub trait RudimentaryEmitter {
38+
fn emit_rudimentary(&mut self, msg: &str, code: Option<&str>, lvl: Level);
39+
}
40+
41+
impl<T: RudimentaryEmitter> Emitter for T {
42+
fn emit(&mut self,
43+
msp: &MultiSpan,
44+
msg: &str,
45+
code: Option<&str>,
46+
lvl: Level) {
47+
assert!(msp.primary_span().is_none(), "Rudimenatry emitters can't handle spans");
48+
self.emit_rudimentary(msg, code, lvl);
49+
}
50+
51+
fn emit_struct(&mut self, db: &DiagnosticBuilder) {
52+
self.emit(&db.span, &db.message, db.code.as_ref().map(|s| &**s), db.level);
53+
for child in &db.children {
54+
assert!(child.render_span.is_none(), "Rudimentary emitters can't handle render spans");
55+
self.emit(&child.span, &child.message, None, child.level);
56+
}
57+
}
58+
}
59+
3460
/// maximum number of lines we will print for each error; arbitrary.
3561
pub const MAX_HIGHLIGHT_LINES: usize = 6;
3662

@@ -57,26 +83,15 @@ pub struct BasicEmitter {
5783
dst: Destination,
5884
}
5985

60-
impl Emitter for BasicEmitter {
61-
fn emit(&mut self,
62-
msp: &MultiSpan,
63-
msg: &str,
64-
code: Option<&str>,
65-
lvl: Level) {
66-
assert!(msp.primary_span().is_none(), "BasicEmitter can't handle spans");
67-
86+
impl RudimentaryEmitter for BasicEmitter {
87+
fn emit_rudimentary(&mut self,
88+
msg: &str,
89+
code: Option<&str>,
90+
lvl: Level) {
6891
if let Err(e) = print_diagnostic(&mut self.dst, "", lvl, msg, code) {
6992
panic!("failed to print diagnostics: {:?}", e);
7093
}
7194
}
72-
73-
fn emit_struct(&mut self, db: &DiagnosticBuilder) {
74-
self.emit(&db.span, &db.message, db.code.as_ref().map(|s| &**s), db.level);
75-
for child in &db.children {
76-
assert!(child.render_span.is_none(), "BasicEmitter can't handle spans");
77-
self.emit(&child.span, &child.message, None, child.level);
78-
}
79-
}
8095
}
8196

8297
impl BasicEmitter {

0 commit comments

Comments
 (0)