1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
use errors::{Error, ErrorKind};
use rustc_serialize::json;
use std::str::FromStr;
use std::path::Path;
use runtest::ProcRes;
#[derive(RustcEncodable, RustcDecodable)]
struct Diagnostic {
message: String,
code: Option<DiagnosticCode>,
level: String,
spans: Vec<DiagnosticSpan>,
children: Vec<Diagnostic>,
rendered: Option<String>,
}
#[derive(RustcEncodable, RustcDecodable, Clone)]
struct DiagnosticSpan {
file_name: String,
line_start: usize,
line_end: usize,
column_start: usize,
column_end: usize,
is_primary: bool,
label: Option<String>,
expansion: Option<Box<DiagnosticSpanMacroExpansion>>,
}
#[derive(RustcEncodable, RustcDecodable, Clone)]
struct DiagnosticSpanMacroExpansion {
span: DiagnosticSpan,
macro_decl_name: String,
}
#[derive(RustcEncodable, RustcDecodable, Clone)]
struct DiagnosticCode {
code: String,
explanation: Option<String>,
}
pub fn parse_output(file_name: &str, output: &str, proc_res: &ProcRes) -> Vec<Error> {
output.lines()
.flat_map(|line| parse_line(file_name, line, output, proc_res))
.collect()
}
fn parse_line(file_name: &str, line: &str, output: &str, proc_res: &ProcRes) -> Vec<Error> {
if line.chars().next() == Some('{') {
match json::decode::<Diagnostic>(line) {
Ok(diagnostic) => {
let mut expected_errors = vec![];
push_expected_errors(&mut expected_errors, &diagnostic, &[], file_name);
expected_errors
}
Err(error) => {
proc_res.fatal(Some(&format!("failed to decode compiler output as json: \
`{}`\noutput: {}\nline: {}",
error,
line,
output)));
}
}
} else {
vec![]
}
}
fn push_expected_errors(expected_errors: &mut Vec<Error>,
diagnostic: &Diagnostic,
default_spans: &[&DiagnosticSpan],
file_name: &str) {
let spans_in_this_file: Vec<_> = diagnostic.spans
.iter()
.filter(|span| Path::new(&span.file_name) == Path::new(&file_name))
.collect();
let primary_spans: Vec<_> = spans_in_this_file.iter()
.cloned()
.filter(|span| span.is_primary)
.collect();
let primary_spans = if primary_spans.is_empty() {
default_spans
} else {
&primary_spans
};
let with_code = |span: &DiagnosticSpan, text: &str| {
match diagnostic.code {
Some(ref code) =>
format!("{}:{}: {}:{}: {} [{}]",
span.line_start, span.column_start,
span.line_end, span.column_end,
text, code.code.clone()),
None =>
format!("{}:{}: {}:{}: {}",
span.line_start, span.column_start,
span.line_end, span.column_end,
text),
}
};
let mut message_lines = diagnostic.message.lines();
if let Some(first_line) = message_lines.next() {
for span in primary_spans {
let msg = with_code(span, first_line);
let kind = ErrorKind::from_str(&diagnostic.level).ok();
expected_errors.push(Error {
line_num: span.line_start,
kind: kind,
msg: msg,
});
}
}
for next_line in message_lines {
for span in primary_spans {
expected_errors.push(Error {
line_num: span.line_start,
kind: None,
msg: with_code(span, next_line),
});
}
}
if let Some(ref rendered) = diagnostic.rendered {
let start_line = primary_spans.iter().map(|s| s.line_start).min().expect("\
every suggestion should have at least one span");
for (index, line) in rendered.lines().enumerate() {
expected_errors.push(Error {
line_num: start_line + index,
kind: Some(ErrorKind::Suggestion),
msg: line.to_string(),
});
}
}
for span in primary_spans {
for frame in &span.expansion {
push_backtrace(expected_errors, frame, file_name);
}
}
for span in spans_in_this_file.iter()
.filter(|span| span.label.is_some()) {
expected_errors.push(Error {
line_num: span.line_start,
kind: Some(ErrorKind::Note),
msg: span.label.clone().unwrap(),
});
}
for child in &diagnostic.children {
push_expected_errors(expected_errors, child, primary_spans, file_name);
}
}
fn push_backtrace(expected_errors: &mut Vec<Error>,
expansion: &DiagnosticSpanMacroExpansion,
file_name: &str) {
if Path::new(&expansion.span.file_name) == Path::new(&file_name) {
expected_errors.push(Error {
line_num: expansion.span.line_start,
kind: Some(ErrorKind::Note),
msg: format!("in this expansion of {}", expansion.macro_decl_name),
});
}
for previous_expansion in &expansion.span.expansion {
push_backtrace(expected_errors, previous_expansion, file_name);
}
}