Skip to content

Commit f86b830

Browse files
committed
Make some compiletest errors/warnings/help more visually obvious
1 parent 6e83458 commit f86b830

File tree

6 files changed

+81
-38
lines changed

6 files changed

+81
-38
lines changed

src/tools/compiletest/src/common.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use serde::de::{Deserialize, Deserializer, Error as _};
1111

1212
pub use self::Mode::*;
1313
use crate::executor::{ColorConfig, OutputFormat};
14+
use crate::fatal;
1415
use crate::util::{Utf8PathBufExt, add_dylib_path};
1516

1617
macro_rules! string_enum {
@@ -783,11 +784,13 @@ fn rustc_output(config: &Config, args: &[&str], envs: HashMap<String, String>) -
783784

784785
let output = match command.output() {
785786
Ok(output) => output,
786-
Err(e) => panic!("error: failed to run {command:?}: {e}"),
787+
Err(e) => {
788+
fatal!("failed to run {command:?}: {e}");
789+
}
787790
};
788791
if !output.status.success() {
789-
panic!(
790-
"error: failed to run {command:?}\n--- stdout\n{}\n--- stderr\n{}",
792+
fatal!(
793+
"failed to run {command:?}\n--- stdout\n{}\n--- stderr\n{}",
791794
String::from_utf8(output.stdout).unwrap(),
792795
String::from_utf8(output.stderr).unwrap(),
793796
);
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
//! Collection of diagnostics helpers for `compiletest` *itself*.
2+
3+
#[macro_export]
4+
macro_rules! fatal {
5+
($($arg:tt)*) => {
6+
let status = ::colored::Colorize::bright_red("FATAL: ");
7+
let status = ::colored::Colorize::bold(status);
8+
eprint!("{status}");
9+
eprintln!($($arg)*);
10+
// Avoid producing panic message/backtrace.
11+
std::panic::resume_unwind(Box::new(()))
12+
};
13+
}
14+
15+
#[macro_export]
16+
macro_rules! error {
17+
($($arg:tt)*) => {
18+
let status = ::colored::Colorize::red("ERROR: ");
19+
let status = ::colored::Colorize::bold(status);
20+
eprint!("{status}");
21+
eprintln!($($arg)*);
22+
};
23+
}
24+
25+
#[macro_export]
26+
macro_rules! warning {
27+
($($arg:tt)*) => {
28+
let status = ::colored::Colorize::yellow("WARNING: ");
29+
let status = ::colored::Colorize::bold(status);
30+
eprint!("{status}");
31+
eprintln!($($arg)*);
32+
};
33+
}
34+
35+
#[macro_export]
36+
macro_rules! help {
37+
($($arg:tt)*) => {
38+
let status = ::colored::Colorize::cyan("HELP: ");
39+
let status = ::colored::Colorize::bold(status);
40+
eprint!("{status}");
41+
eprintln!($($arg)*);
42+
};
43+
}

src/tools/compiletest/src/header.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use crate::errors::ErrorKind;
1515
use crate::executor::{CollectedTestDesc, ShouldPanic};
1616
use crate::header::auxiliary::{AuxProps, parse_and_update_aux};
1717
use crate::header::needs::CachedNeedsConditions;
18+
use crate::help;
1819
use crate::util::static_regex;
1920

2021
pub(crate) mod auxiliary;
@@ -920,9 +921,9 @@ fn iter_header(
920921
if !is_known_directive {
921922
*poisoned = true;
922923

923-
eprintln!(
924-
"error: detected unknown compiletest test directive `{}` in {}:{}",
925-
directive_line.raw_directive, testfile, line_number,
924+
error!(
925+
"{testfile}:{line_number}: detected unknown compiletest test directive `{}`",
926+
directive_line.raw_directive,
926927
);
927928

928929
return;
@@ -931,11 +932,11 @@ fn iter_header(
931932
if let Some(trailing_directive) = &trailing_directive {
932933
*poisoned = true;
933934

934-
eprintln!(
935-
"error: detected trailing compiletest test directive `{}` in {}:{}\n \
936-
help: put the trailing directive in it's own line: `//@ {}`",
937-
trailing_directive, testfile, line_number, trailing_directive,
935+
error!(
936+
"{testfile}:{line_number}: detected trailing compiletest test directive `{}`",
937+
trailing_directive,
938938
);
939+
help!("put the trailing directive in it's own line: `//@ {}`", trailing_directive);
939940

940941
return;
941942
}
@@ -1031,10 +1032,9 @@ impl Config {
10311032
};
10321033

10331034
let Some((regex, replacement)) = parse_normalize_rule(raw_value) else {
1034-
panic!(
1035-
"couldn't parse custom normalization rule: `{raw_directive}`\n\
1036-
help: expected syntax is: `{directive_name}: \"REGEX\" -> \"REPLACEMENT\"`"
1037-
);
1035+
error!("couldn't parse custom normalization rule: `{raw_directive}`");
1036+
help!("expected syntax is: `{directive_name}: \"REGEX\" -> \"REPLACEMENT\"`");
1037+
panic!("invalid normalization rule detected");
10381038
};
10391039
Some(NormalizeRule { kind, regex, replacement })
10401040
}
@@ -1406,7 +1406,7 @@ pub(crate) fn make_test_description<R: Read>(
14061406
ignore_message = Some(reason.into());
14071407
}
14081408
IgnoreDecision::Error { message } => {
1409-
eprintln!("error: {}:{line_number}: {message}", path);
1409+
error!("{path}:{line_number}: {message}");
14101410
*poisoned = true;
14111411
return;
14121412
}

src/tools/compiletest/src/lib.rs

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ mod tests;
1111
pub mod common;
1212
pub mod compute_diff;
1313
mod debuggers;
14+
pub mod diagnostics;
1415
pub mod errors;
1516
mod executor;
1617
pub mod header;
@@ -33,7 +34,7 @@ use build_helper::git::{get_git_modified_files, get_git_untracked_files};
3334
use camino::{Utf8Path, Utf8PathBuf};
3435
use getopts::Options;
3536
use rayon::iter::{ParallelBridge, ParallelIterator};
36-
use tracing::*;
37+
use tracing::debug;
3738
use walkdir::WalkDir;
3839

3940
use self::header::{EarlyProps, make_test_description};
@@ -651,10 +652,7 @@ pub(crate) fn collect_and_make_tests(config: Arc<Config>) -> Vec<CollectedTest>
651652
let common_inputs_stamp = common_inputs_stamp(&config);
652653
let modified_tests =
653654
modified_tests(&config, &config.src_test_suite_root).unwrap_or_else(|err| {
654-
panic!(
655-
"modified_tests got error from dir: {}, error: {}",
656-
config.src_test_suite_root, err
657-
)
655+
fatal!("modified_tests: {}: {err}", config.src_test_suite_root);
658656
});
659657
let cache = HeadersCache::load(&config);
660658

@@ -1108,22 +1106,17 @@ fn check_for_overlapping_test_paths(found_path_stems: &HashSet<Utf8PathBuf>) {
11081106

11091107
pub fn early_config_check(config: &Config) {
11101108
if !config.has_html_tidy && config.mode == Mode::Rustdoc {
1111-
eprintln!("warning: `tidy` (html-tidy.org) is not installed; diffs will not be generated");
1109+
warning!("`tidy` (html-tidy.org) is not installed; diffs will not be generated");
11121110
}
11131111

11141112
if !config.profiler_runtime && config.mode == Mode::CoverageRun {
11151113
let actioned = if config.bless { "blessed" } else { "checked" };
1116-
eprintln!(
1117-
r#"
1118-
WARNING: profiler runtime is not available, so `.coverage` files won't be {actioned}
1119-
help: try setting `profiler = true` in the `[build]` section of `bootstrap.toml`"#
1120-
);
1114+
warning!("profiler runtime is not available, so `.coverage` files won't be {actioned}");
1115+
help!("try setting `profiler = true` in the `[build]` section of `bootstrap.toml`");
11211116
}
11221117

11231118
// `RUST_TEST_NOCAPTURE` is a libtest env var, but we don't callout to libtest.
11241119
if env::var("RUST_TEST_NOCAPTURE").is_ok() {
1125-
eprintln!(
1126-
"WARNING: RUST_TEST_NOCAPTURE is not supported. Use the `--no-capture` flag instead."
1127-
);
1120+
warning!("`RUST_TEST_NOCAPTURE` is not supported; use the `--no-capture` flag instead");
11281121
}
11291122
}

src/tools/compiletest/src/runtest.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use crate::errors::{Error, ErrorKind, load_errors};
2727
use crate::header::TestProps;
2828
use crate::read2::{Truncated, read2_abbreviated};
2929
use crate::util::{Utf8PathBufExt, add_dylib_path, logv, static_regex};
30-
use crate::{ColorConfig, json, stamp_file_path};
30+
use crate::{ColorConfig, help, json, stamp_file_path, warning};
3131

3232
mod debugger;
3333

@@ -485,12 +485,15 @@ impl<'test> TestCx<'test> {
485485
.windows(2)
486486
.any(|args| args == cfg_arg || args[0] == arg || args[1] == arg)
487487
{
488-
panic!(
489-
"error: redundant cfg argument `{normalized_revision}` is already created by the revision"
488+
error!(
489+
"redundant cfg argument `{normalized_revision}` is already created by the \
490+
revision"
490491
);
492+
panic!("redundant cfg argument");
491493
}
492494
if self.config.builtin_cfg_names().contains(&normalized_revision) {
493-
panic!("error: revision `{normalized_revision}` collides with a builtin cfg");
495+
error!("revision `{normalized_revision}` collides with a built-in cfg");
496+
panic!("revision collides with built-in cfg");
494497
}
495498
cmd.args(cfg_arg);
496499
}
@@ -2167,9 +2170,10 @@ impl<'test> TestCx<'test> {
21672170
println!("{}", String::from_utf8_lossy(&output.stdout));
21682171
eprintln!("{}", String::from_utf8_lossy(&output.stderr));
21692172
} else {
2170-
eprintln!("warning: no pager configured, falling back to unified diff");
2171-
eprintln!(
2172-
"help: try configuring a git pager (e.g. `delta`) with `git config --global core.pager delta`"
2173+
warning!("no pager configured, falling back to unified diff");
2174+
help!(
2175+
"try configuring a git pager (e.g. `delta`) with \
2176+
`git config --global core.pager delta`"
21732177
);
21742178
let mut out = io::stdout();
21752179
let mut diff = BufReader::new(File::open(&diff_filename).unwrap());

src/tools/compiletest/src/runtest/codegen_units.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use std::collections::HashSet;
22

33
use super::{Emit, TestCx, WillExecute};
4-
use crate::errors;
54
use crate::util::static_regex;
5+
use crate::{errors, fatal};
66

77
impl TestCx<'_> {
88
pub(super) fn run_codegen_units_test(&self) {
@@ -100,7 +100,7 @@ impl TestCx<'_> {
100100
}
101101

102102
if !(missing.is_empty() && unexpected.is_empty() && wrong_cgus.is_empty()) {
103-
panic!();
103+
fatal!("!(missing.is_empty() && unexpected.is_empty() && wrong_cgus.is_empty())");
104104
}
105105

106106
#[derive(Clone, Eq, PartialEq)]

0 commit comments

Comments
 (0)