1
Fork 0

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 {CrateTranslation, ModuleTranslation};
use util::common::time; use util::common::time;
use util::common::path2cstr; use util::common::path2cstr;
use syntax::codemap::{self, MultiSpan}; use syntax::codemap::MultiSpan;
use syntax::errors::{self, Handler, Level}; use syntax::errors::{self, Handler, Level};
use syntax::errors::emitter::Emitter; use syntax::errors::emitter::RudimentaryEmitter;
use std::collections::HashMap; use std::collections::HashMap;
use std::ffi::{CStr, CString}; use std::ffi::{CStr, CString};
@ -100,24 +100,17 @@ impl SharedEmitter {
} }
} }
impl Emitter for SharedEmitter { impl RudimentaryEmitter for SharedEmitter {
fn emit(&mut self, fn emit_rudimentary(&mut self,
sp: &codemap::MultiSpan,
msg: &str, msg: &str,
code: Option<&str>, code: Option<&str>,
lvl: Level) { lvl: Level) {
assert!(sp.primary_span().is_none(), "SharedEmitter doesn't support spans");
self.buffer.lock().unwrap().push(Diagnostic { self.buffer.lock().unwrap().push(Diagnostic {
msg: msg.to_string(), msg: msg.to_string(),
code: code.map(|s| s.to_string()), code: code.map(|s| s.to_string()),
lvl: lvl, 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; use term;
pub trait Emitter { pub trait Emitter {
/// Emit a standalone diagnostic message.
fn emit(&mut self, span: &MultiSpan, msg: &str, code: Option<&str>, lvl: Level); fn emit(&mut self, span: &MultiSpan, msg: &str, code: Option<&str>, lvl: Level);
/// Emit a structured diagnostic. /// Emit a structured diagnostic.
fn emit_struct(&mut self, db: &DiagnosticBuilder); 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. /// maximum number of lines we will print for each error; arbitrary.
pub const MAX_HIGHLIGHT_LINES: usize = 6; pub const MAX_HIGHLIGHT_LINES: usize = 6;
@ -57,26 +83,15 @@ pub struct BasicEmitter {
dst: Destination, dst: Destination,
} }
impl Emitter for BasicEmitter { impl RudimentaryEmitter for BasicEmitter {
fn emit(&mut self, fn emit_rudimentary(&mut self,
msp: &MultiSpan,
msg: &str, msg: &str,
code: Option<&str>, code: Option<&str>,
lvl: Level) { lvl: Level) {
assert!(msp.primary_span().is_none(), "BasicEmitter can't handle spans");
if let Err(e) = print_diagnostic(&mut self.dst, "", lvl, msg, code) { if let Err(e) = print_diagnostic(&mut self.dst, "", lvl, msg, code) {
panic!("failed to print diagnostics: {:?}", e); 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 { impl BasicEmitter {