Skip to content

Commit 1063b0f

Browse files
committed
Change TestCx::error to error_prefix, which returns a string
This reduces the amount of "hidden" printing in error-reporting code, which will be helpful when overhauling compiletest's error handling and output capture.
1 parent d1d44d4 commit 1063b0f

File tree

1 file changed

+22
-14
lines changed

1 file changed

+22
-14
lines changed

src/tools/compiletest/src/runtest.rs

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -603,7 +603,10 @@ impl<'test> TestCx<'test> {
603603
);
604604
} else {
605605
for pattern in missing_patterns {
606-
self.error(&format!("error pattern '{}' not found!", pattern));
606+
println!(
607+
"\n{prefix}: error pattern '{pattern}' not found!",
608+
prefix = self.error_prefix()
609+
);
607610
}
608611
self.fatal_proc_rec("multiple error patterns not found", proc_res);
609612
}
@@ -821,10 +824,11 @@ impl<'test> TestCx<'test> {
821824
// - only known line - meh, but suggested
822825
// - others are not worth suggesting
823826
if !unexpected.is_empty() {
824-
self.error(&format!(
825-
"{} diagnostics reported in JSON output but not expected in test file",
826-
unexpected.len(),
827-
));
827+
println!(
828+
"\n{prefix}: {n} diagnostics reported in JSON output but not expected in test file",
829+
prefix = self.error_prefix(),
830+
n = unexpected.len(),
831+
);
828832
for error in &unexpected {
829833
print_error(error);
830834
let mut suggestions = Vec::new();
@@ -854,10 +858,11 @@ impl<'test> TestCx<'test> {
854858
}
855859
}
856860
if !not_found.is_empty() {
857-
self.error(&format!(
858-
"{} diagnostics expected in test file but not reported in JSON output",
859-
not_found.len()
860-
));
861+
println!(
862+
"\n{prefix}: {n} diagnostics expected in test file but not reported in JSON output",
863+
prefix = self.error_prefix(),
864+
n = not_found.len(),
865+
);
861866
for error in &not_found {
862867
print_error(error);
863868
let mut suggestions = Vec::new();
@@ -1992,16 +1997,19 @@ impl<'test> TestCx<'test> {
19921997
output_base_name(self.config, self.testpaths, self.safe_revision())
19931998
}
19941999

1995-
fn error(&self, err: &str) {
2000+
/// Prefix to print before error messages. Normally just `error`, but also
2001+
/// includes the revision name for tests that use revisions.
2002+
#[must_use]
2003+
fn error_prefix(&self) -> String {
19962004
match self.revision {
1997-
Some(rev) => println!("\nerror in revision `{}`: {}", rev, err),
1998-
None => println!("\nerror: {}", err),
2005+
Some(rev) => format!("error in revision `{rev}`"),
2006+
None => format!("error"),
19992007
}
20002008
}
20012009

20022010
#[track_caller]
20032011
fn fatal(&self, err: &str) -> ! {
2004-
self.error(err);
2012+
println!("\n{prefix}: {err}", prefix = self.error_prefix());
20052013
error!("fatal error, panic: {:?}", err);
20062014
panic!("fatal error");
20072015
}
@@ -2019,7 +2027,7 @@ impl<'test> TestCx<'test> {
20192027
proc_res: &ProcRes,
20202028
callback_before_unwind: impl FnOnce(),
20212029
) -> ! {
2022-
self.error(err);
2030+
println!("\n{prefix}: {err}", prefix = self.error_prefix());
20232031

20242032
// Some callers want to print additional notes after the main error message.
20252033
if let Some(note) = extra_note {

0 commit comments

Comments
 (0)