Skip to content

Commit 8710c76

Browse files
committed
Replace the use of the rather randomly named boolean custom to mean
"highlight end" and instead add a variant to `RenderSpan`
1 parent e326aa1 commit 8710c76

File tree

1 file changed

+37
-30
lines changed

1 file changed

+37
-30
lines changed

src/libsyntax/diagnostic.rs

Lines changed: 37 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -35,21 +35,25 @@ pub enum RenderSpan {
3535
/// the source code covered by the span.
3636
FullSpan(Span),
3737

38+
/// Similar to a FullSpan, but the cited position is the end of
39+
/// the span, instead of the start. Used, at least, for telling
40+
/// compiletest/runtest to look at the last line of the span
41+
/// (since `end_highlight_lines` displays an arrow to the end
42+
/// of the span).
43+
EndSpan(Span),
44+
3845
/// A FileLine renders with just a line for the message prefixed
3946
/// by file:linenum.
4047
FileLine(Span),
4148
}
4249

4350
impl RenderSpan {
44-
fn span(self) -> Span {
45-
match self {
46-
FullSpan(s) | FileLine(s) => s
47-
}
48-
}
49-
fn is_full_span(&self) -> bool {
50-
match self {
51-
&FullSpan(..) => true,
52-
&FileLine(..) => false,
51+
fn span(&self) -> Span {
52+
match *self {
53+
FullSpan(s) |
54+
EndSpan(s) |
55+
FileLine(s) =>
56+
s
5357
}
5458
}
5559
}
@@ -115,7 +119,7 @@ impl SpanHandler {
115119
self.handler.emit(Some((&self.cm, sp)), msg, Note);
116120
}
117121
pub fn span_end_note(&self, sp: Span, msg: &str) {
118-
self.handler.custom_emit(&self.cm, FullSpan(sp), msg, Note);
122+
self.handler.custom_emit(&self.cm, EndSpan(sp), msg, Note);
119123
}
120124
pub fn span_help(&self, sp: Span, msg: &str) {
121125
self.handler.emit(Some((&self.cm, sp)), msg, Help);
@@ -407,8 +411,8 @@ impl Emitter for EmitterWriter {
407411
let error = match cmsp {
408412
Some((cm, COMMAND_LINE_SP)) => emit(self, cm,
409413
FileLine(COMMAND_LINE_SP),
410-
msg, code, lvl, false),
411-
Some((cm, sp)) => emit(self, cm, FullSpan(sp), msg, code, lvl, false),
414+
msg, code, lvl),
415+
Some((cm, sp)) => emit(self, cm, FullSpan(sp), msg, code, lvl),
412416
None => print_diagnostic(self, "", lvl, msg, code),
413417
};
414418

@@ -420,43 +424,46 @@ impl Emitter for EmitterWriter {
420424

421425
fn custom_emit(&mut self, cm: &codemap::CodeMap,
422426
sp: RenderSpan, msg: &str, lvl: Level) {
423-
match emit(self, cm, sp, msg, None, lvl, true) {
427+
match emit(self, cm, sp, msg, None, lvl) {
424428
Ok(()) => {}
425429
Err(e) => panic!("failed to print diagnostics: {:?}", e),
426430
}
427431
}
428432
}
429433

430434
fn emit(dst: &mut EmitterWriter, cm: &codemap::CodeMap, rsp: RenderSpan,
431-
msg: &str, code: Option<&str>, lvl: Level, custom: bool) -> io::Result<()> {
435+
msg: &str, code: Option<&str>, lvl: Level) -> io::Result<()> {
432436
let sp = rsp.span();
433437

434438
// We cannot check equality directly with COMMAND_LINE_SP
435439
// since PartialEq is manually implemented to ignore the ExpnId
436440
let ss = if sp.expn_id == COMMAND_LINE_EXPN {
437441
"<command line option>".to_string()
442+
} else if let EndSpan(_) = rsp {
443+
let span_end = Span { lo: sp.hi, hi: sp.hi, expn_id: sp.expn_id};
444+
cm.span_to_string(span_end)
438445
} else {
439446
cm.span_to_string(sp)
440447
};
441-
if custom {
442-
// we want to tell compiletest/runtest to look at the last line of the
443-
// span (since `custom_highlight_lines` displays an arrow to the end of
444-
// the span)
445-
let span_end = Span { lo: sp.hi, hi: sp.hi, expn_id: sp.expn_id};
446-
let ses = cm.span_to_string(span_end);
447-
try!(print_diagnostic(dst, &ses[..], lvl, msg, code));
448-
if rsp.is_full_span() {
449-
try!(custom_highlight_lines(dst, cm, sp, lvl, cm.span_to_lines(sp)));
450-
}
451-
} else {
452-
try!(print_diagnostic(dst, &ss[..], lvl, msg, code));
453-
if rsp.is_full_span() {
448+
449+
try!(print_diagnostic(dst, &ss[..], lvl, msg, code));
450+
451+
match rsp {
452+
FullSpan(_) => {
454453
try!(highlight_lines(dst, cm, sp, lvl, cm.span_to_lines(sp)));
455454
}
455+
EndSpan(_) => {
456+
try!(end_highlight_lines(dst, cm, sp, lvl, cm.span_to_lines(sp)));
457+
}
458+
FileLine(..) => {
459+
// no source text in this case!
460+
}
456461
}
462+
457463
if sp != COMMAND_LINE_SP {
458464
try!(print_macro_backtrace(dst, cm, sp));
459465
}
466+
460467
match code {
461468
Some(code) =>
462469
match dst.registry.as_ref().and_then(|registry| registry.find_description(code)) {
@@ -575,12 +582,12 @@ fn highlight_lines(err: &mut EmitterWriter,
575582
}
576583

577584
/// Here are the differences between this and the normal `highlight_lines`:
578-
/// `custom_highlight_lines` will always put arrow on the last byte of the
585+
/// `end_highlight_lines` will always put arrow on the last byte of the
579586
/// span (instead of the first byte). Also, when the span is too long (more
580-
/// than 6 lines), `custom_highlight_lines` will print the first line, then
587+
/// than 6 lines), `end_highlight_lines` will print the first line, then
581588
/// dot dot dot, then last line, whereas `highlight_lines` prints the first
582589
/// six lines.
583-
fn custom_highlight_lines(w: &mut EmitterWriter,
590+
fn end_highlight_lines(w: &mut EmitterWriter,
584591
cm: &codemap::CodeMap,
585592
sp: Span,
586593
lvl: Level,

0 commit comments

Comments
 (0)