Skip to content

Commit 38c6593

Browse files
committed
Rollup merge of #32459 - nrc:json-err-text, r=nikomatsakis
Include source text in JSON errors
2 parents a3b9b42 + 9757516 commit 38c6593

File tree

2 files changed

+41
-3
lines changed

2 files changed

+41
-3
lines changed

src/libsyntax/errors/json.rs

+39-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
// FIXME spec the JSON output properly.
2121

2222

23-
use codemap::{MultiSpan, CodeMap};
23+
use codemap::{Span, MultiSpan, CodeMap};
2424
use diagnostics::registry::Registry;
2525
use errors::{Level, DiagnosticBuilder, SubDiagnostic, RenderSpan, CodeSuggestion};
2626
use errors::emitter::Emitter;
@@ -99,6 +99,16 @@ struct DiagnosticSpan {
9999
/// 1-based, character offset.
100100
column_start: usize,
101101
column_end: usize,
102+
/// Source text from the start of line_start to the end of line_end.
103+
text: Vec<DiagnosticSpanLine>,
104+
}
105+
106+
#[derive(RustcEncodable)]
107+
struct DiagnosticSpanLine {
108+
text: String,
109+
/// 1-based, character offset in self.text.
110+
highlight_start: usize,
111+
highlight_end: usize,
102112
}
103113

104114
#[derive(RustcEncodable)]
@@ -180,6 +190,7 @@ impl DiagnosticSpan {
180190
line_end: end.line,
181191
column_start: start.col.0 + 1,
182192
column_end: end.col.0 + 1,
193+
text: DiagnosticSpanLine::from_span(span, je),
183194
}
184195
}).collect()
185196
}
@@ -202,6 +213,7 @@ impl DiagnosticSpan {
202213
line_end: end.line,
203214
column_start: 0,
204215
column_end: end.col.0 + 1,
216+
text: DiagnosticSpanLine::from_span(span, je),
205217
}
206218
}).collect()
207219
}
@@ -217,13 +229,39 @@ impl DiagnosticSpan {
217229
line_end: end.line,
218230
column_start: 0,
219231
column_end: 0,
232+
text: DiagnosticSpanLine::from_span(span, je),
220233
}
221234
}).collect()
222235
}
223236
}
224237
}
225238
}
226239

240+
impl DiagnosticSpanLine {
241+
fn from_span(span: &Span, je: &JsonEmitter) -> Vec<DiagnosticSpanLine> {
242+
let lines = match je.cm.span_to_lines(*span) {
243+
Ok(lines) => lines,
244+
Err(_) => {
245+
debug!("unprintable span");
246+
return Vec::new();
247+
}
248+
};
249+
250+
let mut result = Vec::new();
251+
let fm = &*lines.file;
252+
253+
for line in &lines.lines {
254+
result.push(DiagnosticSpanLine {
255+
text: fm.get_line(line.line_index).unwrap().to_owned(),
256+
highlight_start: line.start_col.0 + 1,
257+
highlight_end: line.end_col.0 + 1,
258+
});
259+
}
260+
261+
result
262+
}
263+
}
264+
227265
impl DiagnosticCode {
228266
fn map_opt_string(s: Option<String>, je: &JsonEmitter) -> Option<DiagnosticCode> {
229267
s.map(|s| {

src/test/run-make/json-errors/Makefile

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ all:
66
cp foo.rs $(TMPDIR)
77
cd $(TMPDIR)
88
-$(RUSTC) -Z unstable-options --error-format=json foo.rs 2>$(LOG)
9-
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)
10-
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)
9+
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)
10+
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)

0 commit comments

Comments
 (0)