Skip to content

Commit fede36f

Browse files
committed
Introduce first-class compiletest-force-stage0 override
Instead of the ad-hoc `COMPILETEST_FORCE_STAGE0` env var. However, explicitly do not test this configuration under CI, as tests are most certainly not guaranteed to pass against stage 0 (compiler, library).
1 parent 875901b commit fede36f

File tree

5 files changed

+47
-20
lines changed

5 files changed

+47
-20
lines changed

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

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ use std::ffi::{OsStr, OsString};
88
use std::path::{Path, PathBuf};
99
use std::{env, fs, iter};
1010

11+
use build_helper::exit;
12+
1113
use crate::core::build_steps::compile::{Std, run_cargo};
1214
use crate::core::build_steps::doc::DocumentationFormat;
1315
use crate::core::build_steps::gcc::{Gcc, add_cg_gcc_cargo_flags};
@@ -714,17 +716,24 @@ impl Step for CompiletestTest {
714716

715717
/// Runs `cargo test` for compiletest.
716718
fn run(self, builder: &Builder<'_>) {
719+
if builder.top_stage == 0 && !builder.config.compiletest_force_stage0 {
720+
eprintln!(
721+
"`compiletest` unit tests cannot be run against stage 0 unless `build.compiletest-force-stage0` override is specified"
722+
);
723+
exit!(1);
724+
}
725+
717726
let host = self.host;
718727
let compiler = builder.compiler(builder.top_stage, host);
719728

720-
// We need `ToolStd` for the locally-built sysroot because
721-
// compiletest uses unstable features of the `test` crate.
729+
// We need `ToolStd` for the locally-built sysroot because compiletest uses unstable
730+
// features of the `test` crate.
722731
builder.std(compiler, host);
723732
let mut cargo = tool::prepare_tool_cargo(
724733
builder,
725734
compiler,
726-
// compiletest uses libtest internals; make it use the in-tree std to make sure it never breaks
727-
// when std sources change.
735+
// compiletest uses libtest internals; make it use the in-tree std to make sure it never
736+
// breaks when std sources change.
728737
Mode::ToolStd,
729738
host,
730739
Kind::Test,
@@ -1612,12 +1621,11 @@ impl Step for Compiletest {
16121621
return;
16131622
}
16141623

1615-
if builder.top_stage == 0 && env::var("COMPILETEST_FORCE_STAGE0").is_err() {
1624+
if builder.top_stage == 0 && !builder.config.compiletest_force_stage0 {
16161625
eprintln!("\
1617-
ERROR: `--stage 0` runs compiletest on the stage0 (precompiled) compiler, not your local changes, and will almost always cause tests to fail
1618-
HELP: to test the compiler, use `--stage 1` instead
1619-
HELP: to test the standard library, use `--stage 0 library/std` instead
1620-
NOTE: if you're sure you want to do this, please open an issue as to why. In the meantime, you can override this with `COMPILETEST_FORCE_STAGE0=1`."
1626+
ERROR: `--stage 0` runs `compiletest` on the stage0 (precompiled) compiler, not your local changes, and will almost always cause tests to fail
1627+
HELP: to test the staged compiler/library, use `--stage 1` instead
1628+
NOTE: if you're sure you want to do this, please open an issue as to why. In the meantime, you can override this with `--set build.compiletest-force-stage0=true`."
16211629
);
16221630
crate::exit!(1);
16231631
}

src/bootstrap/src/core/build_steps/tool.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -496,7 +496,7 @@ macro_rules! bootstrap_tool {
496496
build_compiler: self.compiler,
497497
target: self.target,
498498
tool: $tool_name,
499-
mode: if is_unstable {
499+
mode: if is_unstable || !builder.config.compiletest_force_stage0 {
500500
// use in-tree libraries for unstable features
501501
Mode::ToolStd
502502
} else {

src/bootstrap/src/core/builder/tests.rs

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1531,16 +1531,6 @@ mod snapshot {
15311531
insta::assert_snapshot!(
15321532
ctx.config("check")
15331533
.path("compiletest")
1534-
.render_steps(), @"[check] rustc 0 <host> -> Compiletest 1 <host>");
1535-
}
1536-
1537-
#[test]
1538-
fn check_compiletest_stage1_libtest() {
1539-
let ctx = TestCtx::new();
1540-
insta::assert_snapshot!(
1541-
ctx.config("check")
1542-
.path("compiletest")
1543-
.args(&["--set", "build.compiletest-use-stage0-libtest=false"])
15441534
.render_steps(), @r"
15451535
[build] llvm <host>
15461536
[build] rustc 0 <host> -> rustc 1 <host>
@@ -1549,6 +1539,26 @@ mod snapshot {
15491539
");
15501540
}
15511541

1542+
#[test]
1543+
#[should_panic]
1544+
fn test_compiletest_stage0_no_force() {
1545+
let ctx = TestCtx::new();
1546+
ctx.config("test").path("compiletest").stage(0).run();
1547+
}
1548+
1549+
#[test]
1550+
fn test_compiletest_force_stage0() {
1551+
let ctx = TestCtx::new();
1552+
insta::assert_snapshot!(
1553+
ctx.config("test")
1554+
.path("compiletest")
1555+
.stage(0)
1556+
.args(&[
1557+
"--set", "build.compiletest-force-stage0=true"
1558+
])
1559+
.render_steps(), @"[build] rustdoc 0 <host>");
1560+
}
1561+
15521562
#[test]
15531563
fn check_codegen() {
15541564
let ctx = TestCtx::new();

src/bootstrap/src/core/config/config.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,10 @@ pub struct Config {
293293
/// `paths=["foo", "bar"]`.
294294
pub paths: Vec<PathBuf>,
295295

296+
/// Force `compiletest` to be built and tested with stage 0 (compiler, library). Note that
297+
/// `compiletest`'s own unit tests are not guaranteed to pass against stage 0 (compiler,
298+
/// library)!
299+
pub compiletest_force_stage0: bool,
296300
/// Command for visual diff display, e.g. `diff-tool --color=always`.
297301
pub compiletest_diff_tool: Option<String>,
298302

@@ -744,6 +748,7 @@ impl Config {
744748
android_ndk,
745749
optimized_compiler_builtins,
746750
jobs,
751+
compiletest_force_stage0,
747752
compiletest_diff_tool,
748753
tidy_extra_checks,
749754
ccache,
@@ -1009,7 +1014,10 @@ impl Config {
10091014

10101015
config.optimized_compiler_builtins =
10111016
optimized_compiler_builtins.unwrap_or(config.channel != "dev");
1017+
1018+
config.compiletest_force_stage0 = compiletest_force_stage0.unwrap_or(false);
10121019
config.compiletest_diff_tool = compiletest_diff_tool;
1020+
10131021
config.tidy_extra_checks = tidy_extra_checks;
10141022

10151023
let download_rustc = config.download_rustc_commit.is_some();

src/bootstrap/src/core/config/toml/build.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ define_config! {
6767
android_ndk: Option<PathBuf> = "android-ndk",
6868
optimized_compiler_builtins: Option<bool> = "optimized-compiler-builtins",
6969
jobs: Option<u32> = "jobs",
70+
compiletest_force_stage0: Option<bool> = "compiletest-force-stage0",
7071
compiletest_diff_tool: Option<String> = "compiletest-diff-tool",
7172
tidy_extra_checks: Option<String> = "tidy-extra-checks",
7273
ccache: Option<StringOrBool> = "ccache",

0 commit comments

Comments
 (0)