Skip to content
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

Include source text in JSON errors #32459

Merged
merged 1 commit into from
Mar 24, 2016
Merged
Show file tree
Hide file tree
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
40 changes: 39 additions & 1 deletion 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::{MultiSpan, CodeMap};
use codemap::{Span, MultiSpan, CodeMap};
use diagnostics::registry::Registry;
use errors::{Level, DiagnosticBuilder, SubDiagnostic, RenderSpan, CodeSuggestion};
use errors::emitter::Emitter;
Expand Down Expand Up @@ -99,6 +99,16 @@ struct DiagnosticSpan {
/// 1-based, character offset.
column_start: usize,
column_end: usize,
/// Source text from the start of line_start to the end of line_end.
text: Vec<DiagnosticSpanLine>,
}

#[derive(RustcEncodable)]
struct DiagnosticSpanLine {
text: String,
/// 1-based, character offset in self.text.
highlight_start: usize,
highlight_end: usize,
}

#[derive(RustcEncodable)]
Expand Down Expand Up @@ -180,6 +190,7 @@ impl DiagnosticSpan {
line_end: end.line,
column_start: start.col.0 + 1,
column_end: end.col.0 + 1,
text: DiagnosticSpanLine::from_span(span, je),
}
}).collect()
}
Expand All @@ -202,6 +213,7 @@ impl DiagnosticSpan {
line_end: end.line,
column_start: 0,
column_end: end.col.0 + 1,
text: DiagnosticSpanLine::from_span(span, je),
}
}).collect()
}
Expand All @@ -217,13 +229,39 @@ impl DiagnosticSpan {
line_end: end.line,
column_start: 0,
column_end: 0,
text: DiagnosticSpanLine::from_span(span, je),
}
}).collect()
}
}
}
}

impl DiagnosticSpanLine {
fn from_span(span: &Span, je: &JsonEmitter) -> Vec<DiagnosticSpanLine> {
let lines = match je.cm.span_to_lines(*span) {
Ok(lines) => lines,
Err(_) => {
debug!("unprintable span");
return Vec::new();
}
};

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
}
}

impl DiagnosticCode {
fn map_opt_string(s: Option<String>, je: &JsonEmitter) -> Option<DiagnosticCode> {
s.map(|s| {
Expand Down
4 changes: 2 additions & 2 deletions src/test/run-make/json-errors/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ all:
cp foo.rs $(TMPDIR)
cd $(TMPDIR)
-$(RUSTC) -Z unstable-options --error-format=json foo.rs 2>$(LOG)
grep -q '{"message":"unresolved name `y`","code":{"code":"E0425","explanation":"\\nAn unresolved name was used. Example of erroneous codes.*"},"level":"error","spans":\[{"file_name":"foo.rs","byte_start":496,"byte_end":497,"line_start":12,"line_end":12,"column_start":18,"column_end":19}\],"children":\[\]}' $(LOG)
grep -q '{"message":".*","code":{"code":"E0277","explanation":"\\nYou tried.*"},"level":"error","spans":\[{.*}\],"children":\[{"message":"the .*","code":null,"level":"help","spans":\[{"file_name":"foo.rs","byte_start":504,"byte_end":516,"line_start":14,"line_end":14,"column_start":0,"column_end":0}\],"children":\[\]},{"message":" <u8 as core::ops::Add>","code":null,"level":"help",' $(LOG)
grep -q '{"message":"unresolved name `y`","code":{"code":"E0425","explanation":"\\nAn unresolved name was used. Example of erroneous codes.*"},"level":"error","spans":\[{"file_name":"foo.rs","byte_start":496,"byte_end":497,"line_start":12,"line_end":12,"column_start":18,"column_end":19,"text":\[{"text":" let x = 42 + y;","highlight_start":18,"highlight_end":19}\]}\],"children":\[\]}' $(LOG)
grep -q '{"message":".*","code":{"code":"E0277","explanation":"\\nYou tried.*"},"level":"error","spans":\[{.*}\],"children":\[{"message":"the .*","code":null,"level":"help","spans":\[{"file_name":"foo.rs","byte_start":504,"byte_end":516,"line_start":14,"line_end":14,"column_start":0,"column_end":0,"text":\[{.*}\]}\],"children":\[\]},{"message":" <u8 as core::ops::Add>","code":null,"level":"help",' $(LOG)