Skip to content

Commit 84124ef

Browse files
authored
Unrolled build for #144027
Rollup merge of #144027 - RalfJung:clippy, r=Mark-Simulacrum clippy: make tests work in stage 1 This finally fixes #78717 :) Similar to what Miri already does, the clippy test step needs to carefully consider which compiler is used to build clippy and which compiler is linked into clippy (and thus must be used to build the test dependencies). On top of that we have some extra complications that Miri avoided by using `cargo-miri` for building its test dependencies: we need cargo to use the right rustc and the right sysroot, but rust-lang/cargo#4423 makes this quite hard to do. See the long comment in `src/tools/clippy/tests/compile-test.rs` for details. Some clippy tests tried to import rustc crates; that fundamentally requires a full bootstrap loop so it cannot work in stage 1. I had to kind of guess what those tests were doing so I don't know if my changes there make any sense. Cc ```@flip1995``` ```@Kobzol```
2 parents 3f9f20f + 192efbb commit 84124ef

14 files changed

+84
-63
lines changed

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

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -739,7 +739,6 @@ impl Step for CompiletestTest {
739739

740740
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
741741
pub struct Clippy {
742-
stage: u32,
743742
host: TargetSelection,
744743
}
745744

@@ -753,33 +752,23 @@ impl Step for Clippy {
753752
}
754753

755754
fn make_run(run: RunConfig<'_>) {
756-
// If stage is explicitly set or not lower than 2, keep it. Otherwise, make sure it's at least 2
757-
// as tests for this step don't work with a lower stage.
758-
let stage = if run.builder.config.is_explicit_stage() || run.builder.top_stage >= 2 {
759-
run.builder.top_stage
760-
} else {
761-
2
762-
};
763-
764-
run.builder.ensure(Clippy { stage, host: run.target });
755+
run.builder.ensure(Clippy { host: run.target });
765756
}
766757

767758
/// Runs `cargo test` for clippy.
768759
fn run(self, builder: &Builder<'_>) {
769-
let stage = self.stage;
760+
let stage = builder.top_stage;
770761
let host = self.host;
771-
let compiler = builder.compiler(stage, host);
772-
773-
if stage < 2 {
774-
eprintln!("WARNING: clippy tests on stage {stage} may not behave well.");
775-
eprintln!("HELP: consider using stage 2");
776-
}
762+
// We need to carefully distinguish the compiler that builds clippy, and the compiler
763+
// that is linked into the clippy being tested. `target_compiler` is the latter,
764+
// and it must also be used by clippy's test runner to build tests and their dependencies.
765+
let target_compiler = builder.compiler(stage, host);
777766

778-
let tool_result = builder.ensure(tool::Clippy { compiler, target: self.host });
779-
let compiler = tool_result.build_compiler;
767+
let tool_result = builder.ensure(tool::Clippy { compiler: target_compiler, target: host });
768+
let tool_compiler = tool_result.build_compiler;
780769
let mut cargo = tool::prepare_tool_cargo(
781770
builder,
782-
compiler,
771+
tool_compiler,
783772
Mode::ToolRustc,
784773
host,
785774
Kind::Test,
@@ -788,11 +777,17 @@ impl Step for Clippy {
788777
&[],
789778
);
790779

791-
cargo.env("RUSTC_TEST_SUITE", builder.rustc(compiler));
792-
cargo.env("RUSTC_LIB_PATH", builder.rustc_libdir(compiler));
793-
let host_libs = builder.stage_out(compiler, Mode::ToolRustc).join(builder.cargo_dir());
780+
cargo.env("RUSTC_TEST_SUITE", builder.rustc(tool_compiler));
781+
cargo.env("RUSTC_LIB_PATH", builder.rustc_libdir(tool_compiler));
782+
let host_libs = builder.stage_out(tool_compiler, Mode::ToolRustc).join(builder.cargo_dir());
794783
cargo.env("HOST_LIBS", host_libs);
795784

785+
// Build the standard library that the tests can use.
786+
builder.std(target_compiler, host);
787+
cargo.env("TEST_SYSROOT", builder.sysroot(target_compiler));
788+
cargo.env("TEST_RUSTC", builder.rustc(target_compiler));
789+
cargo.env("TEST_RUSTC_LIB", builder.rustc_libdir(target_compiler));
790+
796791
// Collect paths of tests to run
797792
'partially_test: {
798793
let paths = &builder.config.paths[..];
@@ -813,7 +808,8 @@ impl Step for Clippy {
813808
cargo.add_rustc_lib_path(builder);
814809
let cargo = prepare_cargo_test(cargo, &[], &[], host, builder);
815810

816-
let _guard = builder.msg_sysroot_tool(Kind::Test, compiler.stage, "clippy", host, host);
811+
let _guard =
812+
builder.msg_sysroot_tool(Kind::Test, tool_compiler.stage, "clippy", host, host);
817813

818814
// Clippy reports errors if it blessed the outputs
819815
if cargo.allow_failure().run(builder) {

src/tools/clippy/clippy_test_deps/Cargo.lock

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ dependencies = [
7272
"futures",
7373
"if_chain",
7474
"itertools",
75+
"libc",
7576
"parking_lot",
7677
"quote",
7778
"regex",

src/tools/clippy/clippy_test_deps/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ edition = "2021"
66
# Add dependencies here to make them available in ui tests.
77

88
[dependencies]
9+
libc = "0.2"
910
regex = "1.5.5"
1011
serde = { version = "1.0.145", features = ["derive"] }
1112
if_chain = "1.0"

src/tools/clippy/tests/compile-test.rs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,31 @@ impl TestContext {
151151
defaults.set_custom(
152152
"dependencies",
153153
DependencyBuilder {
154-
program: CommandBuilder::cargo(),
154+
program: {
155+
let mut p = CommandBuilder::cargo();
156+
// If we run in bootstrap, we need to use the right compiler for building the
157+
// tests -- not the compiler that built clippy, but the compiler that got linked
158+
// into clippy. Just invoking TEST_RUSTC does not work because LD_LIBRARY_PATH
159+
// is set in a way that makes it pick the wrong sysroot. Sadly due to
160+
// <https://github.com/rust-lang/cargo/issues/4423> we cannot use RUSTFLAGS to
161+
// set `--sysroot`, so we need to use bootstrap's rustc wrapper. That wrapper
162+
// however has some staging logic that is hurting us here, so to work around
163+
// that we set both the "real" and "staging" rustc to TEST_RUSTC, including the
164+
// associated library paths.
165+
if let Some(rustc) = option_env!("TEST_RUSTC") {
166+
let libdir = option_env!("TEST_RUSTC_LIB").unwrap();
167+
let sysroot = option_env!("TEST_SYSROOT").unwrap();
168+
p.envs.push(("RUSTC_REAL".into(), Some(rustc.into())));
169+
p.envs.push(("RUSTC_REAL_LIBDIR".into(), Some(libdir.into())));
170+
p.envs.push(("RUSTC_SNAPSHOT".into(), Some(rustc.into())));
171+
p.envs.push(("RUSTC_SNAPSHOT_LIBDIR".into(), Some(libdir.into())));
172+
p.envs.push((
173+
"RUSTC_SYSROOT".into(),
174+
Some(sysroot.into()),
175+
));
176+
}
177+
p
178+
},
155179
crate_manifest_path: Path::new("clippy_test_deps").join("Cargo.toml"),
156180
build_std: None,
157181
bless_lockfile: self.args.bless,
@@ -192,6 +216,9 @@ impl TestContext {
192216
let dep = format!("-Ldependency={}", Path::new(host_libs).join("deps").display());
193217
config.program.args.push(dep.into());
194218
}
219+
if let Some(sysroot) = option_env!("TEST_SYSROOT") {
220+
config.program.args.push(format!("--sysroot={sysroot}").into());
221+
}
195222

196223
config.program.program = profile_path.join(if cfg!(windows) {
197224
"clippy-driver.exe"

src/tools/clippy/tests/ui/auxiliary/proc_macro_derive.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ pub fn derive(_: TokenStream) -> TokenStream {
1616
let output = quote! {
1717
// Should not trigger `useless_attribute`
1818
#[allow(dead_code)]
19-
extern crate rustc_middle;
19+
extern crate core;
2020
};
2121
output
2222
}

src/tools/clippy/tests/ui/cast_alignment.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
//! Test casts for alignment issues
22
3-
#![feature(rustc_private)]
43
#![feature(core_intrinsics)]
54
#![warn(clippy::cast_ptr_alignment)]
65
#![allow(
@@ -10,8 +9,6 @@
109
clippy::borrow_as_ptr
1110
)]
1211

13-
extern crate libc;
14-
1512
fn main() {
1613
/* These should be warned against */
1714

src/tools/clippy/tests/ui/cast_alignment.stderr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: casting from `*const u8` to a more-strictly-aligned pointer (`*const u16`) (1 < 2 bytes)
2-
--> tests/ui/cast_alignment.rs:19:5
2+
--> tests/ui/cast_alignment.rs:16:5
33
|
44
LL | (&1u8 as *const u8) as *const u16;
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -8,19 +8,19 @@ LL | (&1u8 as *const u8) as *const u16;
88
= help: to override `-D warnings` add `#[allow(clippy::cast_ptr_alignment)]`
99

1010
error: casting from `*mut u8` to a more-strictly-aligned pointer (`*mut u16`) (1 < 2 bytes)
11-
--> tests/ui/cast_alignment.rs:22:5
11+
--> tests/ui/cast_alignment.rs:19:5
1212
|
1313
LL | (&mut 1u8 as *mut u8) as *mut u16;
1414
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1515

1616
error: casting from `*const u8` to a more-strictly-aligned pointer (`*const u16`) (1 < 2 bytes)
17-
--> tests/ui/cast_alignment.rs:26:5
17+
--> tests/ui/cast_alignment.rs:23:5
1818
|
1919
LL | (&1u8 as *const u8).cast::<u16>();
2020
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2121

2222
error: casting from `*mut u8` to a more-strictly-aligned pointer (`*mut u16`) (1 < 2 bytes)
23-
--> tests/ui/cast_alignment.rs:29:5
23+
--> tests/ui/cast_alignment.rs:26:5
2424
|
2525
LL | (&mut 1u8 as *mut u8).cast::<u16>();
2626
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

src/tools/clippy/tests/ui/iter_over_hash_type.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,18 @@
33
#![warn(clippy::iter_over_hash_type)]
44
use std::collections::{HashMap, HashSet};
55

6-
extern crate rustc_data_structures;
7-
86
extern crate proc_macros;
97

8+
// Ensure it also works via type aliases (this isn't really the Fx hasher but that does not matter).
9+
type FxBuildHasher = std::collections::hash_map::RandomState;
10+
type FxHashMap<K, V> = HashMap<K, V, FxBuildHasher>;
11+
type FxHashSet<K> = HashSet<K, FxBuildHasher>;
12+
1013
fn main() {
1114
let mut hash_set = HashSet::<i32>::new();
1215
let mut hash_map = HashMap::<i32, i32>::new();
13-
let mut fx_hash_map = rustc_data_structures::fx::FxHashMap::<i32, i32>::default();
14-
let mut fx_hash_set = rustc_data_structures::fx::FxHashMap::<i32, i32>::default();
16+
let mut fx_hash_map = FxHashMap::<i32, i32>::default();
17+
let mut fx_hash_set = FxHashSet::<i32>::default();
1518
let vec = Vec::<i32>::new();
1619

1720
// test hashset

src/tools/clippy/tests/ui/iter_over_hash_type.stderr

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: iteration over unordered hash-based type
2-
--> tests/ui/iter_over_hash_type.rs:18:5
2+
--> tests/ui/iter_over_hash_type.rs:21:5
33
|
44
LL | / for x in &hash_set {
55
LL | |
@@ -11,7 +11,7 @@ LL | | }
1111
= help: to override `-D warnings` add `#[allow(clippy::iter_over_hash_type)]`
1212

1313
error: iteration over unordered hash-based type
14-
--> tests/ui/iter_over_hash_type.rs:22:5
14+
--> tests/ui/iter_over_hash_type.rs:25:5
1515
|
1616
LL | / for x in hash_set.iter() {
1717
LL | |
@@ -20,7 +20,7 @@ LL | | }
2020
| |_____^
2121

2222
error: iteration over unordered hash-based type
23-
--> tests/ui/iter_over_hash_type.rs:26:5
23+
--> tests/ui/iter_over_hash_type.rs:29:5
2424
|
2525
LL | / for x in hash_set.clone() {
2626
LL | |
@@ -29,7 +29,7 @@ LL | | }
2929
| |_____^
3030

3131
error: iteration over unordered hash-based type
32-
--> tests/ui/iter_over_hash_type.rs:30:5
32+
--> tests/ui/iter_over_hash_type.rs:33:5
3333
|
3434
LL | / for x in hash_set.drain() {
3535
LL | |
@@ -38,7 +38,7 @@ LL | | }
3838
| |_____^
3939

4040
error: iteration over unordered hash-based type
41-
--> tests/ui/iter_over_hash_type.rs:36:5
41+
--> tests/ui/iter_over_hash_type.rs:39:5
4242
|
4343
LL | / for (x, y) in &hash_map {
4444
LL | |
@@ -47,7 +47,7 @@ LL | | }
4747
| |_____^
4848

4949
error: iteration over unordered hash-based type
50-
--> tests/ui/iter_over_hash_type.rs:40:5
50+
--> tests/ui/iter_over_hash_type.rs:43:5
5151
|
5252
LL | / for x in hash_map.keys() {
5353
LL | |
@@ -56,7 +56,7 @@ LL | | }
5656
| |_____^
5757

5858
error: iteration over unordered hash-based type
59-
--> tests/ui/iter_over_hash_type.rs:44:5
59+
--> tests/ui/iter_over_hash_type.rs:47:5
6060
|
6161
LL | / for x in hash_map.values() {
6262
LL | |
@@ -65,7 +65,7 @@ LL | | }
6565
| |_____^
6666

6767
error: iteration over unordered hash-based type
68-
--> tests/ui/iter_over_hash_type.rs:48:5
68+
--> tests/ui/iter_over_hash_type.rs:51:5
6969
|
7070
LL | / for x in hash_map.values_mut() {
7171
LL | |
@@ -74,7 +74,7 @@ LL | | }
7474
| |_____^
7575

7676
error: iteration over unordered hash-based type
77-
--> tests/ui/iter_over_hash_type.rs:52:5
77+
--> tests/ui/iter_over_hash_type.rs:55:5
7878
|
7979
LL | / for x in hash_map.iter() {
8080
LL | |
@@ -83,7 +83,7 @@ LL | | }
8383
| |_____^
8484

8585
error: iteration over unordered hash-based type
86-
--> tests/ui/iter_over_hash_type.rs:56:5
86+
--> tests/ui/iter_over_hash_type.rs:59:5
8787
|
8888
LL | / for x in hash_map.clone() {
8989
LL | |
@@ -92,7 +92,7 @@ LL | | }
9292
| |_____^
9393

9494
error: iteration over unordered hash-based type
95-
--> tests/ui/iter_over_hash_type.rs:60:5
95+
--> tests/ui/iter_over_hash_type.rs:63:5
9696
|
9797
LL | / for x in hash_map.drain() {
9898
LL | |
@@ -101,7 +101,7 @@ LL | | }
101101
| |_____^
102102

103103
error: iteration over unordered hash-based type
104-
--> tests/ui/iter_over_hash_type.rs:66:5
104+
--> tests/ui/iter_over_hash_type.rs:69:5
105105
|
106106
LL | / for x in fx_hash_set {
107107
LL | |
@@ -110,7 +110,7 @@ LL | | }
110110
| |_____^
111111

112112
error: iteration over unordered hash-based type
113-
--> tests/ui/iter_over_hash_type.rs:70:5
113+
--> tests/ui/iter_over_hash_type.rs:73:5
114114
|
115115
LL | / for x in fx_hash_map {
116116
LL | |

src/tools/clippy/tests/ui/strlen_on_c_strings.fixed

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
#![warn(clippy::strlen_on_c_strings)]
22
#![allow(dead_code, clippy::manual_c_str_literals)]
3-
#![feature(rustc_private)]
4-
extern crate libc;
53

64
#[allow(unused)]
75
use libc::strlen;

0 commit comments

Comments
 (0)