Skip to content

Commit a295908

Browse files
committed
compiletest: Cleanup collection of actual errors
1 parent 8fb32ab commit a295908

File tree

1 file changed

+35
-48
lines changed

1 file changed

+35
-48
lines changed

src/tools/compiletest/src/json.rs

+35-48
Original file line numberDiff line numberDiff line change
@@ -142,43 +142,34 @@ pub fn extract_rendered(output: &str) -> String {
142142
}
143143

144144
pub fn parse_output(file_name: &str, output: &str, proc_res: &ProcRes) -> Vec<Error> {
145-
output.lines().flat_map(|line| parse_line(file_name, line, output, proc_res)).collect()
146-
}
147-
148-
fn parse_line(file_name: &str, line: &str, output: &str, proc_res: &ProcRes) -> Vec<Error> {
149-
// The compiler sometimes intermingles non-JSON stuff into the
150-
// output. This hack just skips over such lines. Yuck.
151-
if line.starts_with('{') {
152-
match serde_json::from_str::<Diagnostic>(line) {
153-
Ok(diagnostic) => {
154-
let mut expected_errors = vec![];
155-
push_expected_errors(&mut expected_errors, &diagnostic, &[], file_name);
156-
expected_errors
157-
}
158-
Err(error) => {
159-
// Ignore the future compat report message - this is handled
160-
// by `extract_rendered`
161-
if serde_json::from_str::<FutureIncompatReport>(line).is_ok() {
162-
vec![]
163-
} else {
164-
proc_res.fatal(
145+
let mut errors = Vec::new();
146+
for line in output.lines() {
147+
// The compiler sometimes intermingles non-JSON stuff into the
148+
// output. This hack just skips over such lines. Yuck.
149+
if line.starts_with('{') {
150+
match serde_json::from_str::<Diagnostic>(line) {
151+
Ok(diagnostic) => push_actual_errors(&mut errors, &diagnostic, &[], file_name),
152+
Err(error) => {
153+
// Ignore the future compat report message - this is handled
154+
// by `extract_rendered`
155+
if serde_json::from_str::<FutureIncompatReport>(line).is_err() {
156+
proc_res.fatal(
165157
Some(&format!(
166-
"failed to decode compiler output as json: \
167-
`{}`\nline: {}\noutput: {}",
158+
"failed to decode compiler output as json: `{}`\nline: {}\noutput: {}",
168159
error, line, output
169160
)),
170161
|| (),
171162
);
163+
}
172164
}
173165
}
174166
}
175-
} else {
176-
vec![]
177167
}
168+
errors
178169
}
179170

180-
fn push_expected_errors(
181-
expected_errors: &mut Vec<Error>,
171+
fn push_actual_errors(
172+
errors: &mut Vec<Error>,
182173
diagnostic: &Diagnostic,
183174
default_spans: &[&DiagnosticSpan],
184175
file_name: &str,
@@ -236,10 +227,10 @@ fn push_expected_errors(
236227
}
237228
};
238229

239-
// Convert multi-line messages into multiple expected
240-
// errors. We expect to replace these with something
241-
// more structured shortly anyhow.
230+
// Convert multi-line messages into multiple errors.
231+
// We expect to replace these with something more structured anyhow.
242232
let mut message_lines = diagnostic.message.lines();
233+
let kind = ErrorKind::from_str(&diagnostic.level).ok();
243234
if let Some(first_line) = message_lines.next() {
244235
let ignore = |s| {
245236
static RE: OnceLock<Regex> = OnceLock::new();
@@ -250,27 +241,23 @@ fn push_expected_errors(
250241
};
251242

252243
if primary_spans.is_empty() && !ignore(first_line) {
253-
let msg = with_code(None, first_line);
254-
let kind = ErrorKind::from_str(&diagnostic.level).ok();
255-
expected_errors.push(Error { line_num: None, kind, msg });
244+
errors.push(Error { line_num: None, kind, msg: with_code(None, first_line) });
256245
} else {
257246
for span in primary_spans {
258-
let msg = with_code(Some(span), first_line);
259-
let kind = ErrorKind::from_str(&diagnostic.level).ok();
260-
expected_errors.push(Error { line_num: Some(span.line_start), kind, msg });
247+
errors.push(Error {
248+
line_num: Some(span.line_start),
249+
kind,
250+
msg: with_code(Some(span), first_line),
251+
});
261252
}
262253
}
263254
}
264255
for next_line in message_lines {
265256
if primary_spans.is_empty() {
266-
expected_errors.push(Error {
267-
line_num: None,
268-
kind: None,
269-
msg: with_code(None, next_line),
270-
});
257+
errors.push(Error { line_num: None, kind: None, msg: with_code(None, next_line) });
271258
} else {
272259
for span in primary_spans {
273-
expected_errors.push(Error {
260+
errors.push(Error {
274261
line_num: Some(span.line_start),
275262
kind: None,
276263
msg: with_code(Some(span), next_line),
@@ -283,7 +270,7 @@ fn push_expected_errors(
283270
for span in primary_spans {
284271
if let Some(ref suggested_replacement) = span.suggested_replacement {
285272
for (index, line) in suggested_replacement.lines().enumerate() {
286-
expected_errors.push(Error {
273+
errors.push(Error {
287274
line_num: Some(span.line_start + index),
288275
kind: Some(ErrorKind::Suggestion),
289276
msg: line.to_string(),
@@ -295,13 +282,13 @@ fn push_expected_errors(
295282
// Add notes for the backtrace
296283
for span in primary_spans {
297284
if let Some(frame) = &span.expansion {
298-
push_backtrace(expected_errors, frame, file_name);
285+
push_backtrace(errors, frame, file_name);
299286
}
300287
}
301288

302289
// Add notes for any labels that appear in the message.
303290
for span in spans_in_this_file.iter().filter(|span| span.label.is_some()) {
304-
expected_errors.push(Error {
291+
errors.push(Error {
305292
line_num: Some(span.line_start),
306293
kind: Some(ErrorKind::Note),
307294
msg: span.label.clone().unwrap(),
@@ -310,24 +297,24 @@ fn push_expected_errors(
310297

311298
// Flatten out the children.
312299
for child in &diagnostic.children {
313-
push_expected_errors(expected_errors, child, primary_spans, file_name);
300+
push_actual_errors(errors, child, primary_spans, file_name);
314301
}
315302
}
316303

317304
fn push_backtrace(
318-
expected_errors: &mut Vec<Error>,
305+
errors: &mut Vec<Error>,
319306
expansion: &DiagnosticSpanMacroExpansion,
320307
file_name: &str,
321308
) {
322309
if Path::new(&expansion.span.file_name) == Path::new(&file_name) {
323-
expected_errors.push(Error {
310+
errors.push(Error {
324311
line_num: Some(expansion.span.line_start),
325312
kind: Some(ErrorKind::Note),
326313
msg: format!("in this expansion of {}", expansion.macro_decl_name),
327314
});
328315
}
329316

330317
if let Some(previous_expansion) = &expansion.span.expansion {
331-
push_backtrace(expected_errors, previous_expansion, file_name);
318+
push_backtrace(errors, previous_expansion, file_name);
332319
}
333320
}

0 commit comments

Comments
 (0)