Skip to content

Commit 1599d22

Browse files
committed
Added session.fileline_note() method and support infrastucture for it.
Add way to print notes with just file:linenum prefix (preserving integration with source lookup for e.g. vi and emacs) but don't repeat the other span info.
1 parent 3ccbffa commit 1599d22

File tree

2 files changed

+45
-8
lines changed

2 files changed

+45
-8
lines changed

src/librustc/driver/session.rs

+3
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,9 @@ impl Session {
232232
pub fn span_end_note(&self, sp: Span, msg: &str) {
233233
self.diagnostic().span_end_note(sp, msg)
234234
}
235+
pub fn fileline_note(&self, sp: Span, msg: &str) {
236+
self.diagnostic().fileline_note(sp, msg)
237+
}
235238
pub fn note(&self, msg: &str) {
236239
self.diagnostic().handler().note(msg)
237240
}

src/libsyntax/diagnostic.rs

+42-8
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,37 @@ use term;
2020
// maximum number of lines we will print for each error; arbitrary.
2121
static MAX_LINES: uint = 6u;
2222

23+
#[deriving(Clone)]
24+
pub enum RenderSpan {
25+
/// A FullSpan renders with both with an initial line for the
26+
/// message, prefixed by file:linenum, followed by a summary of
27+
/// the source code covered by the span.
28+
FullSpan(Span),
29+
30+
/// A FileLine renders with just a line for the message prefixed
31+
/// by file:linenum.
32+
FileLine(Span),
33+
}
34+
35+
impl RenderSpan {
36+
fn span(self) -> Span {
37+
match self {
38+
FullSpan(s) | FileLine(s) => s
39+
}
40+
}
41+
fn is_full_span(&self) -> bool {
42+
match self {
43+
&FullSpan(..) => true,
44+
&FileLine(..) => false,
45+
}
46+
}
47+
}
48+
2349
pub trait Emitter {
2450
fn emit(&mut self, cmsp: Option<(&codemap::CodeMap, Span)>,
2551
msg: &str, lvl: Level);
2652
fn custom_emit(&mut self, cm: &codemap::CodeMap,
27-
sp: Span, msg: &str, lvl: Level);
53+
sp: RenderSpan, msg: &str, lvl: Level);
2854
}
2955

3056
/// This structure is used to signify that a task has failed with a fatal error
@@ -60,7 +86,10 @@ impl SpanHandler {
6086
self.handler.emit(Some((&self.cm, sp)), msg, Note);
6187
}
6288
pub fn span_end_note(&self, sp: Span, msg: &str) {
63-
self.handler.custom_emit(&self.cm, sp, msg, Note);
89+
self.handler.custom_emit(&self.cm, FullSpan(sp), msg, Note);
90+
}
91+
pub fn fileline_note(&self, sp: Span, msg: &str) {
92+
self.handler.custom_emit(&self.cm, FileLine(sp), msg, Note);
6493
}
6594
pub fn span_bug(&self, sp: Span, msg: &str) -> ! {
6695
self.handler.emit(Some((&self.cm, sp)), msg, Bug);
@@ -132,7 +161,7 @@ impl Handler {
132161
self.emit.borrow_mut().emit(cmsp, msg, lvl);
133162
}
134163
pub fn custom_emit(&self, cm: &codemap::CodeMap,
135-
sp: Span, msg: &str, lvl: Level) {
164+
sp: RenderSpan, msg: &str, lvl: Level) {
136165
self.emit.borrow_mut().custom_emit(cm, sp, msg, lvl);
137166
}
138167
}
@@ -258,7 +287,7 @@ impl Emitter for EmitterWriter {
258287
msg: &str,
259288
lvl: Level) {
260289
let error = match cmsp {
261-
Some((cm, sp)) => emit(self, cm, sp, msg, lvl, false),
290+
Some((cm, sp)) => emit(self, cm, FullSpan(sp), msg, lvl, false),
262291
None => print_diagnostic(self, "", lvl, msg),
263292
};
264293

@@ -269,16 +298,17 @@ impl Emitter for EmitterWriter {
269298
}
270299

271300
fn custom_emit(&mut self, cm: &codemap::CodeMap,
272-
sp: Span, msg: &str, lvl: Level) {
301+
sp: RenderSpan, msg: &str, lvl: Level) {
273302
match emit(self, cm, sp, msg, lvl, true) {
274303
Ok(()) => {}
275304
Err(e) => fail!("failed to print diagnostics: {}", e),
276305
}
277306
}
278307
}
279308

280-
fn emit(dst: &mut EmitterWriter, cm: &codemap::CodeMap, sp: Span,
309+
fn emit(dst: &mut EmitterWriter, cm: &codemap::CodeMap, rsp: RenderSpan,
281310
msg: &str, lvl: Level, custom: bool) -> io::IoResult<()> {
311+
let sp = rsp.span();
282312
let ss = cm.span_to_str(sp);
283313
let lines = cm.span_to_lines(sp);
284314
if custom {
@@ -288,10 +318,14 @@ fn emit(dst: &mut EmitterWriter, cm: &codemap::CodeMap, sp: Span,
288318
let span_end = Span { lo: sp.hi, hi: sp.hi, expn_info: sp.expn_info};
289319
let ses = cm.span_to_str(span_end);
290320
try!(print_diagnostic(dst, ses, lvl, msg));
291-
try!(custom_highlight_lines(dst, cm, sp, lvl, lines));
321+
if rsp.is_full_span() {
322+
try!(custom_highlight_lines(dst, cm, sp, lvl, lines));
323+
}
292324
} else {
293325
try!(print_diagnostic(dst, ss, lvl, msg));
294-
try!(highlight_lines(dst, cm, sp, lvl, lines));
326+
if rsp.is_full_span() {
327+
try!(highlight_lines(dst, cm, sp, lvl, lines));
328+
}
295329
}
296330
print_macro_backtrace(dst, cm, sp)
297331
}

0 commit comments

Comments
 (0)