Skip to content

JSON errors: give better spans for SpanEnd errors #32714

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 5, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 60 additions & 15 deletions src/libsyntax/errors/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
// FIXME spec the JSON output properly.


use codemap::{Span, MultiSpan, CodeMap};
use codemap::{self, Span, MultiSpan, CodeMap};
use diagnostics::registry::Registry;
use errors::{Level, DiagnosticBuilder, SubDiagnostic, RenderSpan, CodeSuggestion};
use errors::emitter::Emitter;
Expand Down Expand Up @@ -197,8 +197,8 @@ impl DiagnosticSpan {

fn from_render_span(rsp: &RenderSpan, je: &JsonEmitter) -> Vec<DiagnosticSpan> {
match *rsp {
// FIXME(#30701) handle Suggestion properly
RenderSpan::FullSpan(ref msp) |
// FIXME(#30701) handle Suggestion properly
RenderSpan::Suggestion(CodeSuggestion { ref msp, .. }) => {
DiagnosticSpan::from_multispan(msp, je)
}
Expand All @@ -207,13 +207,13 @@ impl DiagnosticSpan {
let end = je.cm.lookup_char_pos(span.hi);
DiagnosticSpan {
file_name: end.file.name.clone(),
byte_start: span.lo.0,
byte_start: span.hi.0,
byte_end: span.hi.0,
line_start: 0,
line_start: end.line,
line_end: end.line,
column_start: 0,
column_start: end.col.0 + 1,
column_end: end.col.0 + 1,
text: DiagnosticSpanLine::from_span(span, je),
text: DiagnosticSpanLine::from_span_end(span, je),
}
}).collect()
}
Expand All @@ -237,25 +237,70 @@ impl DiagnosticSpan {
}
}

impl DiagnosticSpanLine {
fn from_span(span: &Span, je: &JsonEmitter) -> Vec<DiagnosticSpanLine> {
let lines = match je.cm.span_to_lines(*span) {
macro_rules! get_lines_for_span {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this a macro?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

because of the return - if I make it a function, you still need to match in the caller, so it's not much of a saving.

($span: ident, $je: ident) => {
match $je.cm.span_to_lines(*$span) {
Ok(lines) => lines,
Err(_) => {
debug!("unprintable span");
return Vec::new();
}
};
}
}
}

impl DiagnosticSpanLine {
fn line_from_filemap(fm: &codemap::FileMap,
index: usize,
h_start: usize,
h_end: usize)
-> DiagnosticSpanLine {
DiagnosticSpanLine {
text: fm.get_line(index).unwrap().to_owned(),
highlight_start: h_start,
highlight_end: h_end,
}
}

/// Create a list of DiagnosticSpanLines from span - each line with any part
/// of `span` gets a DiagnosticSpanLine, with the highlight indicating the
/// `span` within the line.
fn from_span(span: &Span, je: &JsonEmitter) -> Vec<DiagnosticSpanLine> {
let lines = get_lines_for_span!(span, je);

let mut result = Vec::new();
let fm = &*lines.file;

for line in &lines.lines {
result.push(DiagnosticSpanLine {
text: fm.get_line(line.line_index).unwrap().to_owned(),
highlight_start: line.start_col.0 + 1,
highlight_end: line.end_col.0 + 1,
});
result.push(DiagnosticSpanLine::line_from_filemap(fm,
line.line_index,
line.start_col.0 + 1,
line.end_col.0 + 1));
}

result
}

/// Create a list of DiagnosticSpanLines from span - the result covers all
/// of `span`, but the highlight is zero-length and at the end of `span`.
fn from_span_end(span: &Span, je: &JsonEmitter) -> Vec<DiagnosticSpanLine> {
let lines = get_lines_for_span!(span, je);

let mut result = Vec::new();
let fm = &*lines.file;

for (i, line) in lines.lines.iter().enumerate() {
// Invariant - CodeMap::span_to_lines will not return extra context
// lines - the last line returned is the last line of `span`.
let highlight = if i == lines.lines.len() - 1 {
(line.end_col.0 + 1, line.end_col.0 + 1)
} else {
(0, 0)
};
result.push(DiagnosticSpanLine::line_from_filemap(fm,
line.line_index,
highlight.0,
highlight.1));
}

result
Expand Down