Skip to content

Commit 253a1ce

Browse files
committed
Structured diagnostics
1 parent c1035b3 commit 253a1ce

File tree

3 files changed

+303
-59
lines changed

3 files changed

+303
-59
lines changed

src/librustc/session/mod.rs

Lines changed: 50 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use util::nodemap::{NodeMap, FnvHashMap};
1616

1717
use syntax::ast::{NodeId, NodeIdAssigner, Name};
1818
use syntax::codemap::Span;
19-
use syntax::errors;
19+
use syntax::errors::{self, DiagnosticBuilder};
2020
use syntax::errors::emitter::{Emitter, BasicEmitter};
2121
use syntax::diagnostics;
2222
use syntax::feature_gate;
@@ -80,6 +80,55 @@ pub struct Session {
8080
}
8181

8282
impl Session {
83+
pub fn struct_span_warn<'a, 'b>(&'a self,
84+
sp: Span,
85+
msg: &'b str)
86+
-> Box<DiagnosticBuilder<'a, 'b>> {
87+
self.diagnostic().struct_span_warn(sp, msg)
88+
}
89+
pub fn struct_span_warn_with_code<'a, 'b>(&'a self,
90+
sp: Span,
91+
msg: &'b str,
92+
code: &str)
93+
-> Box<DiagnosticBuilder<'a, 'b>> {
94+
self.diagnostic().struct_span_warn_with_code(sp, msg, code)
95+
}
96+
pub fn struct_warn<'a, 'b>(&'a self, msg: &'b str) -> Box<DiagnosticBuilder<'a, 'b>> {
97+
self.diagnostic().struct_warn(msg)
98+
}
99+
pub fn struct_span_err<'a, 'b>(&'a self,
100+
sp: Span,
101+
msg: &'b str)
102+
-> Box<DiagnosticBuilder<'a, 'b>> {
103+
self.diagnostic().struct_span_err(sp, msg)
104+
}
105+
pub fn struct_span_err_with_code<'a, 'b>(&'a self,
106+
sp: Span,
107+
msg: &'b str,
108+
code: &str)
109+
-> Box<DiagnosticBuilder<'a, 'b>> {
110+
self.diagnostic().struct_span_err_with_code(sp, msg, code)
111+
}
112+
pub fn struct_err<'a, 'b>(&'a self, msg: &'b str) -> Box<DiagnosticBuilder<'a, 'b>> {
113+
self.diagnostic().struct_err(msg)
114+
}
115+
pub fn struct_span_fatal<'a, 'b>(&'a self,
116+
sp: Span,
117+
msg: &'b str)
118+
-> Box<DiagnosticBuilder<'a, 'b>> {
119+
self.diagnostic().struct_span_fatal(sp, msg)
120+
}
121+
pub fn struct_span_fatal_with_code<'a, 'b>(&'a self,
122+
sp: Span,
123+
msg: &'b str,
124+
code: &str)
125+
-> Box<DiagnosticBuilder<'a, 'b>> {
126+
self.diagnostic().struct_span_fatal_with_code(sp, msg, code)
127+
}
128+
pub fn struct_fatal<'a, 'b>(&'a self, msg: &'b str) -> Box<DiagnosticBuilder<'a, 'b>> {
129+
self.diagnostic().struct_fatal(msg)
130+
}
131+
83132
pub fn span_fatal(&self, sp: Span, msg: &str) -> ! {
84133
panic!(self.diagnostic().span_fatal(sp, msg))
85134
}
@@ -144,34 +193,6 @@ impl Session {
144193
None => self.warn(msg),
145194
}
146195
}
147-
pub fn span_note(&self, sp: Span, msg: &str) {
148-
self.diagnostic().span_note(sp, msg)
149-
}
150-
pub fn span_end_note(&self, sp: Span, msg: &str) {
151-
self.diagnostic().span_end_note(sp, msg)
152-
}
153-
154-
/// Prints out a message with a suggested edit of the code.
155-
///
156-
/// See `errors::RenderSpan::Suggestion` for more information.
157-
pub fn span_suggestion(&self, sp: Span, msg: &str, suggestion: String) {
158-
self.diagnostic().span_suggestion(sp, msg, suggestion)
159-
}
160-
pub fn span_help(&self, sp: Span, msg: &str) {
161-
self.diagnostic().span_help(sp, msg)
162-
}
163-
pub fn fileline_note(&self, sp: Span, msg: &str) {
164-
self.diagnostic().fileline_note(sp, msg)
165-
}
166-
pub fn fileline_help(&self, sp: Span, msg: &str) {
167-
self.diagnostic().fileline_help(sp, msg)
168-
}
169-
pub fn note(&self, msg: &str) {
170-
self.diagnostic().note(msg)
171-
}
172-
pub fn help(&self, msg: &str) {
173-
self.diagnostic().help(msg)
174-
}
175196
pub fn opt_span_bug(&self, opt_sp: Option<Span>, msg: &str) -> ! {
176197
match opt_sp {
177198
Some(sp) => self.span_bug(sp, msg),

src/libsyntax/errors/emitter.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use self::Destination::*;
1313
use codemap::{self, COMMAND_LINE_SP, COMMAND_LINE_EXPN, Pos, Span};
1414
use diagnostics;
1515

16-
use errors::{Level, RenderSpan};
16+
use errors::{Level, RenderSpan, DiagnosticBuilder};
1717
use errors::RenderSpan::*;
1818
use errors::Level::*;
1919

@@ -27,6 +27,17 @@ use term;
2727
pub trait Emitter {
2828
fn emit(&mut self, span: Option<Span>, msg: &str, code: Option<&str>, lvl: Level);
2929
fn custom_emit(&mut self, sp: RenderSpan, msg: &str, lvl: Level);
30+
31+
// Emit a structured diagnostic.
32+
fn emit_struct(&mut self, db: &DiagnosticBuilder) {
33+
self.emit(db.span, db.message, db.code.as_ref().map(|s| &**s), db.level);
34+
for child in &db.children {
35+
match child.render_span {
36+
Some(ref sp) => self.custom_emit(sp.clone(), &child.message, child.level),
37+
None => self.emit(child.span, &child.message, None, child.level),
38+
}
39+
}
40+
}
3041
}
3142

3243
/// maximum number of lines we will print for each error; arbitrary.
@@ -111,9 +122,8 @@ impl Emitter for EmitterWriter {
111122
sp: RenderSpan,
112123
msg: &str,
113124
lvl: Level) {
114-
match self.emit_(sp, msg, None, lvl) {
115-
Ok(()) => {}
116-
Err(e) => panic!("failed to print diagnostics: {:?}", e),
125+
if let Err(e) = self.emit_(sp, msg, None, lvl) {
126+
panic!("failed to print diagnostics: {:?}", e);
117127
}
118128
}
119129
}

0 commit comments

Comments
 (0)