Skip to content

Commit

Permalink
Structured diagnostics
Browse files Browse the repository at this point in the history
  • Loading branch information
nrc committed Dec 30, 2015
1 parent c1035b3 commit 253a1ce
Show file tree
Hide file tree
Showing 3 changed files with 303 additions and 59 deletions.
79 changes: 50 additions & 29 deletions src/librustc/session/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use util::nodemap::{NodeMap, FnvHashMap};

use syntax::ast::{NodeId, NodeIdAssigner, Name};
use syntax::codemap::Span;
use syntax::errors;
use syntax::errors::{self, DiagnosticBuilder};
use syntax::errors::emitter::{Emitter, BasicEmitter};
use syntax::diagnostics;
use syntax::feature_gate;
Expand Down Expand Up @@ -80,6 +80,55 @@ pub struct Session {
}

impl Session {
pub fn struct_span_warn<'a, 'b>(&'a self,
sp: Span,
msg: &'b str)
-> Box<DiagnosticBuilder<'a, 'b>> {
self.diagnostic().struct_span_warn(sp, msg)
}
pub fn struct_span_warn_with_code<'a, 'b>(&'a self,
sp: Span,
msg: &'b str,
code: &str)
-> Box<DiagnosticBuilder<'a, 'b>> {
self.diagnostic().struct_span_warn_with_code(sp, msg, code)
}
pub fn struct_warn<'a, 'b>(&'a self, msg: &'b str) -> Box<DiagnosticBuilder<'a, 'b>> {
self.diagnostic().struct_warn(msg)
}
pub fn struct_span_err<'a, 'b>(&'a self,
sp: Span,
msg: &'b str)
-> Box<DiagnosticBuilder<'a, 'b>> {
self.diagnostic().struct_span_err(sp, msg)
}
pub fn struct_span_err_with_code<'a, 'b>(&'a self,
sp: Span,
msg: &'b str,
code: &str)
-> Box<DiagnosticBuilder<'a, 'b>> {
self.diagnostic().struct_span_err_with_code(sp, msg, code)
}
pub fn struct_err<'a, 'b>(&'a self, msg: &'b str) -> Box<DiagnosticBuilder<'a, 'b>> {
self.diagnostic().struct_err(msg)
}
pub fn struct_span_fatal<'a, 'b>(&'a self,
sp: Span,
msg: &'b str)
-> Box<DiagnosticBuilder<'a, 'b>> {
self.diagnostic().struct_span_fatal(sp, msg)
}
pub fn struct_span_fatal_with_code<'a, 'b>(&'a self,
sp: Span,
msg: &'b str,
code: &str)
-> Box<DiagnosticBuilder<'a, 'b>> {
self.diagnostic().struct_span_fatal_with_code(sp, msg, code)
}
pub fn struct_fatal<'a, 'b>(&'a self, msg: &'b str) -> Box<DiagnosticBuilder<'a, 'b>> {
self.diagnostic().struct_fatal(msg)
}

pub fn span_fatal(&self, sp: Span, msg: &str) -> ! {
panic!(self.diagnostic().span_fatal(sp, msg))
}
Expand Down Expand Up @@ -144,34 +193,6 @@ impl Session {
None => self.warn(msg),
}
}
pub fn span_note(&self, sp: Span, msg: &str) {
self.diagnostic().span_note(sp, msg)
}
pub fn span_end_note(&self, sp: Span, msg: &str) {
self.diagnostic().span_end_note(sp, msg)
}

/// Prints out a message with a suggested edit of the code.
///
/// See `errors::RenderSpan::Suggestion` for more information.
pub fn span_suggestion(&self, sp: Span, msg: &str, suggestion: String) {
self.diagnostic().span_suggestion(sp, msg, suggestion)
}
pub fn span_help(&self, sp: Span, msg: &str) {
self.diagnostic().span_help(sp, msg)
}
pub fn fileline_note(&self, sp: Span, msg: &str) {
self.diagnostic().fileline_note(sp, msg)
}
pub fn fileline_help(&self, sp: Span, msg: &str) {
self.diagnostic().fileline_help(sp, msg)
}
pub fn note(&self, msg: &str) {
self.diagnostic().note(msg)
}
pub fn help(&self, msg: &str) {
self.diagnostic().help(msg)
}
pub fn opt_span_bug(&self, opt_sp: Option<Span>, msg: &str) -> ! {
match opt_sp {
Some(sp) => self.span_bug(sp, msg),
Expand Down
18 changes: 14 additions & 4 deletions src/libsyntax/errors/emitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use self::Destination::*;
use codemap::{self, COMMAND_LINE_SP, COMMAND_LINE_EXPN, Pos, Span};
use diagnostics;

use errors::{Level, RenderSpan};
use errors::{Level, RenderSpan, DiagnosticBuilder};
use errors::RenderSpan::*;
use errors::Level::*;

Expand All @@ -27,6 +27,17 @@ use term;
pub trait Emitter {
fn emit(&mut self, span: Option<Span>, msg: &str, code: Option<&str>, lvl: Level);
fn custom_emit(&mut self, sp: RenderSpan, msg: &str, lvl: Level);

// Emit a structured diagnostic.
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 {
match child.render_span {
Some(ref sp) => self.custom_emit(sp.clone(), &child.message, child.level),
None => self.emit(child.span, &child.message, None, child.level),
}
}
}
}

/// maximum number of lines we will print for each error; arbitrary.
Expand Down Expand Up @@ -111,9 +122,8 @@ impl Emitter for EmitterWriter {
sp: RenderSpan,
msg: &str,
lvl: Level) {
match self.emit_(sp, msg, None, lvl) {
Ok(()) => {}
Err(e) => panic!("failed to print diagnostics: {:?}", e),
if let Err(e) = self.emit_(sp, msg, None, lvl) {
panic!("failed to print diagnostics: {:?}", e);
}
}
}
Expand Down
Loading

0 comments on commit 253a1ce

Please sign in to comment.