Skip to content

Commit ca4befb

Browse files
committed
compiletest: Don't panic when trying to print unknown JSON after failure
If we're printing process output after some other check has failed, and the output is JSON-like, we shouldn't necessarily assume that it is valid JSON produced by the compiler. If it isn't, printing it as-is is more helpful than printing a less-relevant error message.
1 parent 9275ae5 commit ca4befb

File tree

2 files changed

+19
-9
lines changed

2 files changed

+19
-9
lines changed

src/tools/compiletest/src/json.rs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,12 @@ pub fn rustfix_diagnostics_only(output: &str) -> String {
9191
.collect()
9292
}
9393

94-
pub fn extract_rendered(output: &str) -> String {
94+
pub enum OnUnknownJson {
95+
Error,
96+
Print,
97+
}
98+
99+
pub fn extract_rendered(output: &str, on_unknown_json: OnUnknownJson) -> String {
95100
output
96101
.lines()
97102
.filter_map(|line| {
@@ -125,11 +130,16 @@ pub fn extract_rendered(output: &str) -> String {
125130
// Ignore the notification.
126131
None
127132
} else {
128-
print!(
129-
"failed to decode compiler output as json: line: {}\noutput: {}",
130-
line, output
131-
);
132-
panic!()
133+
match on_unknown_json {
134+
OnUnknownJson::Error => {
135+
print!(
136+
"failed to decode compiler output as json: line: {}\noutput: {}",
137+
line, output
138+
);
139+
panic!()
140+
}
141+
OnUnknownJson::Print => Some(format!("{}\n", line)),
142+
}
133143
}
134144
} else {
135145
// preserve non-JSON lines, such as ICEs

src/tools/compiletest/src/runtest.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ use std::io::{self, BufReader};
3131
use std::iter;
3232
use std::path::{Path, PathBuf};
3333
use std::process::{Child, Command, ExitStatus, Output, Stdio};
34-
use std::str;
34+
use std::str::{self, FromStr};
3535
use std::sync::Arc;
3636

3737
use anyhow::Context;
@@ -3783,7 +3783,7 @@ impl<'test> TestCx<'test> {
37833783
} else if explicit_format {
37843784
proc_res.stderr.clone()
37853785
} else {
3786-
json::extract_rendered(&proc_res.stderr)
3786+
json::extract_rendered(&proc_res.stderr, json::OnUnknownJson::Error)
37873787
};
37883788

37893789
let normalized_stderr = self.normalize_output(&stderr, &self.props.normalize_stderr);
@@ -4564,7 +4564,7 @@ pub struct ProcRes {
45644564
impl ProcRes {
45654565
pub fn print_info(&self) {
45664566
fn render(name: &str, contents: &str) -> String {
4567-
let contents = json::extract_rendered(contents);
4567+
let contents = json::extract_rendered(contents, json::OnUnknownJson::Print);
45684568
let contents = contents.trim_end();
45694569
if contents.is_empty() {
45704570
format!("{name}: none")

0 commit comments

Comments
 (0)