Skip to content

Commit 8c11853

Browse files
committed
clippy: make tests work in stage 1
1 parent e27f16a commit 8c11853

File tree

9 files changed

+69
-29
lines changed

9 files changed

+69
-29
lines changed

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

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -768,18 +768,18 @@ impl Step for Clippy {
768768
fn run(self, builder: &Builder<'_>) {
769769
let stage = self.stage;
770770
let host = self.host;
771-
let compiler = builder.compiler(stage, host);
771+
let target_compiler = builder.compiler(stage, host);
772772

773773
if stage < 2 {
774774
eprintln!("WARNING: clippy tests on stage {stage} may not behave well.");
775775
eprintln!("HELP: consider using stage 2");
776776
}
777777

778-
let tool_result = builder.ensure(tool::Clippy { compiler, target: self.host });
779-
let compiler = tool_result.build_compiler;
778+
let tool_result = builder.ensure(tool::Clippy { compiler: target_compiler, target: host });
779+
let tool_compiler = tool_result.build_compiler;
780780
let mut cargo = tool::prepare_tool_cargo(
781781
builder,
782-
compiler,
782+
tool_compiler,
783783
Mode::ToolRustc,
784784
host,
785785
Kind::Test,
@@ -788,11 +788,17 @@ impl Step for Clippy {
788788
&[],
789789
);
790790

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());
791+
cargo.env("RUSTC_TEST_SUITE", builder.rustc(tool_compiler));
792+
cargo.env("RUSTC_LIB_PATH", builder.rustc_libdir(tool_compiler));
793+
let host_libs = builder.stage_out(tool_compiler, Mode::ToolRustc).join(builder.cargo_dir());
794794
cargo.env("HOST_LIBS", host_libs);
795795

796+
// Build the standard library that the tests can use.
797+
builder.std(target_compiler, host);
798+
cargo.env("TEST_SYSROOT", builder.sysroot(target_compiler));
799+
cargo.env("TEST_RUSTC", builder.rustc(target_compiler));
800+
cargo.env("TEST_RUSTC_LIB", builder.rustc_libdir(target_compiler));
801+
796802
// Collect paths of tests to run
797803
'partially_test: {
798804
let paths = &builder.config.paths[..];
@@ -813,7 +819,8 @@ impl Step for Clippy {
813819
cargo.add_rustc_lib_path(builder);
814820
let cargo = prepare_cargo_test(cargo, &[], &[], host, builder);
815821

816-
let _guard = builder.msg_sysroot_tool(Kind::Test, compiler.stage, "clippy", host, host);
822+
let _guard =
823+
builder.msg_sysroot_tool(Kind::Test, tool_compiler.stage, "clippy", host, host);
817824

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

src/tools/clippy/clippy_test_deps/Cargo.lock

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ dependencies = [
7575
"parking_lot",
7676
"quote",
7777
"regex",
78+
"rustc-hash",
7879
"serde",
7980
"syn",
8081
"tokio",
@@ -355,6 +356,12 @@ version = "0.1.25"
355356
source = "registry+https://github.com/rust-lang/crates.io-index"
356357
checksum = "989e6739f80c4ad5b13e0fd7fe89531180375b18520cc8c82080e4dc4035b84f"
357358

359+
[[package]]
360+
name = "rustc-hash"
361+
version = "2.1.1"
362+
source = "registry+https://github.com/rust-lang/crates.io-index"
363+
checksum = "357703d41365b4b27c590e3ed91eabb1b663f07c4c084095e60cbed4362dff0d"
364+
358365
[[package]]
359366
name = "scopeguard"
360367
version = "1.2.0"

src/tools/clippy/clippy_test_deps/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ futures = "0.3"
1515
parking_lot = "0.12"
1616
tokio = { version = "1", features = ["io-util"] }
1717
itertools = "0.12"
18+
rustc-hash = "2.1.1"
1819

1920
# Make sure we are not part of the rustc workspace.
2021
[workspace]

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/iter_over_hash_type.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,13 @@
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

108
fn main() {
119
let mut hash_set = HashSet::<i32>::new();
1210
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();
11+
let mut fx_hash_map = rustc_hash::FxHashMap::<i32, i32>::default();
12+
let mut fx_hash_set = rustc_hash::FxHashMap::<i32, i32>::default();
1513
let vec = Vec::<i32>::new();
1614

1715
// 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:16: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:20: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:24: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:28: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:34: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:38: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:42: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:46: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:50: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:54: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:58: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:64: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:68:5
114114
|
115115
LL | / for x in fx_hash_map {
116116
LL | |

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
#[allow(unused_imports)]
1414
#[allow(unused_extern_crates)]
1515
#[macro_use]
16-
extern crate rustc_middle;
16+
extern crate regex as regex_crate;
1717

1818
#[macro_use]
1919
extern crate proc_macro_derive;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
#[allow(unused_imports)]
1414
#[allow(unused_extern_crates)]
1515
#[macro_use]
16-
extern crate rustc_middle;
16+
extern crate regex as regex_crate;
1717

1818
#[macro_use]
1919
extern crate proc_macro_derive;

0 commit comments

Comments
 (0)