Skip to content

Commit 32c1b4e

Browse files
Rollup merge of #160548 - Zalathar:coverage-mode, r=jieyouxu
bootstrap: Register `coverage-map` and `coverage-run` aliases via a separate step Using a separate step lets us remove the `default_to_suites_only` hack that was added in #159131. In the past we did have separate steps for the aliases, which were removed by #135097. A key difference now is that the new step is normal Rust code without any clunky macros. Importantly, this PR still preserves the desired skipping behaviour, e.g. `./x test --skip=tests` continues to skip the coverage tests as intended. r? jieyouxu (or bootstrap)
2 parents f1910ba + 31ce8e5 commit 32c1b4e

4 files changed

Lines changed: 67 additions & 72 deletions

File tree

src/bootstrap/src/core/build_steps/test.rs

Lines changed: 62 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ use crate::utils::helpers::{
4444
up_to_date,
4545
};
4646
use crate::utils::render_tests::{add_flags_and_try_run_tests, try_run_tests};
47-
use crate::{CLang, CodegenBackendKind, GitRepo, Mode, PathSet, TestTarget, envify, exit};
47+
use crate::{CLang, CodegenBackendKind, GitRepo, Mode, TestTarget, envify, exit};
4848

4949
mod compiletest;
5050
pub mod failed_tests;
@@ -1974,72 +1974,41 @@ impl Coverage {
19741974
const SUITE: &'static str = "coverage";
19751975
const ALL_MODES: &[CompiletestMode] =
19761976
&[CompiletestMode::CoverageMap, CompiletestMode::CoverageRun];
1977+
1978+
fn new(run: &RunConfig<'_>, mode: CompiletestMode) -> Self {
1979+
let compiler = run.builder.compiler(run.builder.top_stage, run.build_triple());
1980+
let target = run.target;
1981+
Coverage { compiler, target, mode }
1982+
}
19771983
}
19781984

19791985
impl CommandLineStep for Coverage {
19801986
type Output = ();
19811987
/// Compiletest will automatically skip the "coverage-run" tests if necessary.
19821988
const IS_HOST: bool = false;
19831989

1984-
fn should_run(mut run: ShouldRun<'_>) -> ShouldRun<'_> {
1985-
// Support various invocation styles, including:
1990+
fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
1991+
// Handle these invocation styles:
1992+
// - `./x test` (including coverage tests)
19861993
// - `./x test coverage`
1994+
// - `./x test tests/coverage`
19871995
// - `./x test tests/coverage/trivial.rs`
1988-
// - `./x test coverage-map`
1989-
// - `./x test coverage-run -- tests/coverage/trivial.rs`
1990-
run = run.suite_path(Self::PATH);
1991-
for mode in Self::ALL_MODES {
1992-
run = run.alias(mode.as_str());
1993-
}
1994-
1995-
// Allow `./x test --skip=tests` to properly skip the coverage tests,
1996-
// by not treating the `coverage-map` and `coverage-run` aliases as
1997-
// implied command-line arguments.
1998-
run = run.default_to_suites_only();
1999-
2000-
run
1996+
// - `./x test tests/coverage/trivial.rs --skip=coverage-run`
1997+
run.suite_path(Coverage::PATH)
20011998
}
20021999

20032000
fn is_default_step(_builder: &Builder<'_>) -> bool {
20042001
true
20052002
}
20062003

20072004
fn make_run(run: RunConfig<'_>) {
2008-
let compiler = run.builder.compiler(run.builder.top_stage, run.build_triple());
2009-
let target = run.target;
2010-
2011-
// List of (coverage) test modes that the coverage test suite will be
2012-
// run in. It's OK for this to contain duplicates, because the call to
2013-
// `Builder::ensure` below will take care of deduplication.
2014-
let mut modes = vec![];
2015-
2016-
// From the pathsets that were selected on the command-line (or by default),
2017-
// determine which modes to run in.
2018-
for path in &run.paths {
2019-
match path {
2020-
PathSet::Set(_) => {
2021-
for &mode in Self::ALL_MODES {
2022-
if path.assert_single_path().path == Path::new(mode.as_str()) {
2023-
modes.push(mode);
2024-
break;
2025-
}
2026-
}
2027-
}
2028-
PathSet::Suite(_) => {
2029-
modes.extend_from_slice(Self::ALL_MODES);
2030-
break;
2031-
}
2032-
}
2033-
}
2034-
2035-
// Skip any modes that were explicitly skipped/excluded on the command-line.
2005+
// Run the tests in all coverage-test modes, but skip any modes that
2006+
// were explicitly skipped on the command-line (e.g. `--skip=coverage-run`).
20362007
// FIXME(Zalathar): Integrate this into central skip handling somehow?
2037-
modes.retain(|mode| {
2038-
!run.builder.config.skip.iter().any(|skip| skip == Path::new(mode.as_str()))
2039-
});
2040-
2041-
for mode in modes {
2042-
run.builder.ensure(Coverage { compiler, target, mode });
2008+
for &mode in Coverage::ALL_MODES {
2009+
if !run.builder.config.skip.iter().any(|skip| skip == Path::new(mode.as_str())) {
2010+
run.builder.ensure(Coverage::new(&run, mode));
2011+
}
20432012
}
20442013
}
20452014

@@ -2058,6 +2027,49 @@ impl CommandLineStep for Coverage {
20582027
}
20592028
}
20602029

2030+
/// Registers the `coverage-map` and `coverage-run` aliases, which are then
2031+
/// forwarded to the [`Coverage`] step.
2032+
///
2033+
/// If the aliases were registered by [`Coverage`] directly, they would also
2034+
/// be treated as implied command-line arguments when run by default.
2035+
/// That would cause things like `./x test --skip=tests` to still run coverage
2036+
/// tests, which is undesirable.
2037+
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
2038+
pub enum CoverageModeAlias {}
2039+
2040+
impl CommandLineStep for CoverageModeAlias {
2041+
type Output = ();
2042+
2043+
fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
2044+
// Register the aliases "coverage-map" and "coverage-run", to handle
2045+
// these invocation styles:
2046+
// - `./x test coverage-map`
2047+
// - `./x test coverage-run -- tests/coverage/trivial.rs`
2048+
Coverage::ALL_MODES.iter().fold(run, |run, mode| run.alias(mode.as_str()))
2049+
}
2050+
2051+
fn is_default_step(_builder: &Builder<'_>) -> bool {
2052+
false
2053+
}
2054+
2055+
fn make_run(run: RunConfig<'_>) {
2056+
for path in &run.paths {
2057+
let single_path = &path.assert_single_path().path;
2058+
for &mode in Coverage::ALL_MODES {
2059+
if single_path == Path::new(mode.as_str()) {
2060+
// Instead of creating an intermediate `CoverageModeAlias`
2061+
// step instance, delegate straight to `Coverage`.
2062+
run.builder.ensure(Coverage::new(&run, mode));
2063+
}
2064+
}
2065+
}
2066+
}
2067+
2068+
fn run(self, _builder: &Builder<'_>) {
2069+
unreachable!("never instantiated; `make_run` creates a Coverage step instead");
2070+
}
2071+
}
2072+
20612073
test!(CoverageRunRustdoc {
20622074
path: "tests/coverage-run-rustdoc",
20632075
mode: CompiletestMode::CoverageRun,

src/bootstrap/src/core/builder/cli_paths/snapshots/x_test_coverage_map.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
source: src/bootstrap/src/core/builder/cli_paths/tests.rs
33
expression: test coverage-map
44
---
5-
[Test] test::Coverage
5+
[Test] test::CoverageModeAlias
66
targets: [aarch64-unknown-linux-gnu]
77
- Set({coverage-map})

src/bootstrap/src/core/builder/cli_paths/snapshots/x_test_coverage_run.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
source: src/bootstrap/src/core/builder/cli_paths/tests.rs
33
expression: test coverage-run
44
---
5-
[Test] test::Coverage
5+
[Test] test::CoverageModeAlias
66
targets: [aarch64-unknown-linux-gnu]
77
- Set({coverage-run})

src/bootstrap/src/core/builder/mod.rs

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -524,13 +524,11 @@ pub struct ShouldRun<'a> {
524524

525525
// use a BTreeSet to maintain sort order
526526
paths: BTreeSet<PathSet>,
527-
528-
default_to_suites_only: bool,
529527
}
530528

531529
impl<'a> ShouldRun<'a> {
532530
fn new(builder: &'a Builder<'_>, kind: Kind) -> ShouldRun<'a> {
533-
ShouldRun { builder, kind, paths: BTreeSet::new(), default_to_suites_only: false }
531+
ShouldRun { builder, kind, paths: BTreeSet::new() }
534532
}
535533

536534
/// The corresponding step should run if the bootstrap command-line selects
@@ -643,26 +641,10 @@ impl<'a> ShouldRun<'a> {
643641
sets
644642
}
645643

646-
/// When generating pathsets for a step that is being run "by default"
647-
/// (i.e. when running bootstrap without an explicit command-line path),
648-
/// discard any paths that were not registered as test suites.
649-
///
650-
/// This is basically a hack to make path-based skipping work properly for
651-
/// coverage tests, since otherwise the `coverage-map` and `coverage-run`
652-
/// aliases would prevent `./x test --skip=tests` from skipping them.
653-
pub(crate) fn default_to_suites_only(mut self) -> Self {
654-
self.default_to_suites_only = true;
655-
self
656-
}
657-
658644
/// When the corresponding step is run "by default" (without explicit command-line paths),
659645
/// act as though the user had explicitly specified these paths.
660646
fn default_pathsets(&self) -> Vec<PathSet> {
661-
let mut default_pathsets = self.paths.iter().cloned().collect::<Vec<_>>();
662-
if self.default_to_suites_only {
663-
default_pathsets.retain(|p| matches!(p, PathSet::Suite(_)));
664-
}
665-
default_pathsets
647+
self.paths.iter().cloned().collect::<Vec<_>>()
666648
}
667649
}
668650

@@ -898,6 +880,7 @@ impl<'a> Builder<'a> {
898880
test::Ui,
899881
test::Crashes,
900882
test::Coverage,
883+
test::CoverageModeAlias,
901884
test::MirOpt,
902885
test::CodegenLlvm,
903886
test::CodegenUnits,

0 commit comments

Comments
 (0)