WIP factor out RudimentaryEmitter

This commit is contained in:
Niko Matsakis 2016-04-20 15:52:52 -04:00
parent 489a6c95bf
commit 41a652e094
2 changed files with 38 additions and 30 deletions

View File

@ -19,9 +19,9 @@ use llvm::SMDiagnosticRef;
use {CrateTranslation, ModuleTranslation};
use util::common::time;
use util::common::path2cstr;
use syntax::codemap::{self, MultiSpan};
use syntax::codemap::MultiSpan;
use syntax::errors::{self, Handler, Level};
use syntax::errors::emitter::Emitter;
use syntax::errors::emitter::RudimentaryEmitter;
use std::collections::HashMap;
use std::ffi::{CStr, CString};
@ -100,24 +100,17 @@ impl SharedEmitter {
}
}
impl Emitter for SharedEmitter {
fn emit(&mut self,
sp: &codemap::MultiSpan,
msg: &str,
code: Option<&str>,
lvl: Level) {
assert!(sp.primary_span().is_none(), "SharedEmitter doesn't support spans");
impl RudimentaryEmitter for SharedEmitter {
fn emit_rudimentary(&mut self,
msg: &str,
code: Option<&str>,
lvl: Level) {
self.buffer.lock().unwrap().push(Diagnostic {
msg: msg.to_string(),
code: code.map(|s| s.to_string()),
lvl: lvl,
});
}
fn emit_struct(&mut self, _db: &errors::DiagnosticBuilder) {
bug!("SharedEmitter doesn't support emit_struct");
}
}

View File

@ -25,12 +25,38 @@ use std::rc::Rc;
use term;
pub trait Emitter {
/// Emit a standalone diagnostic message.
fn emit(&mut self, span: &MultiSpan, msg: &str, code: Option<&str>, lvl: Level);
/// Emit a structured diagnostic.
fn emit_struct(&mut self, db: &DiagnosticBuilder);
}
/// A core trait that can only handle very simple messages: those
/// without spans or any real structure. Used only in specific contexts.
pub trait RudimentaryEmitter {
fn emit_rudimentary(&mut self, msg: &str, code: Option<&str>, lvl: Level);
}
impl<T: RudimentaryEmitter> Emitter for T {
fn emit(&mut self,
msp: &MultiSpan,
msg: &str,
code: Option<&str>,
lvl: Level) {
assert!(msp.primary_span().is_none(), "Rudimenatry emitters can't handle spans");
self.emit_rudimentary(msg, code, lvl);
}
fn emit_struct(&mut self, db: &DiagnosticBuilder) {
self.emit(&db.span, &db.message, db.code.as_ref().map(|s| &**s), db.level);
for child in &db.children {
assert!(child.render_span.is_none(), "Rudimentary emitters can't handle render spans");
self.emit(&child.span, &child.message, None, child.level);
}
}
}
/// maximum number of lines we will print for each error; arbitrary.
pub const MAX_HIGHLIGHT_LINES: usize = 6;
@ -57,26 +83,15 @@ pub struct BasicEmitter {
dst: Destination,
}
impl Emitter for BasicEmitter {
fn emit(&mut self,
msp: &MultiSpan,
msg: &str,
code: Option<&str>,
lvl: Level) {
assert!(msp.primary_span().is_none(), "BasicEmitter can't handle spans");
impl RudimentaryEmitter for BasicEmitter {
fn emit_rudimentary(&mut self,
msg: &str,
code: Option<&str>,
lvl: Level) {
if let Err(e) = print_diagnostic(&mut self.dst, "", lvl, msg, code) {
panic!("failed to print diagnostics: {:?}", e);
}
}
fn emit_struct(&mut self, db: &DiagnosticBuilder) {
self.emit(&db.span, &db.message, db.code.as_ref().map(|s| &**s), db.level);
for child in &db.children {
assert!(child.render_span.is_none(), "BasicEmitter can't handle spans");
self.emit(&child.span, &child.message, None, child.level);
}
}
}
impl BasicEmitter {