Skip to content

Commit ec30b29

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

File tree

7 files changed

+69
-34
lines changed

7 files changed

+69
-34
lines changed

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

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -768,18 +768,16 @@ 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);
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-
}
771+
// We need to carefully distinguish the compiler that builds clippy, and the compiler
772+
// that is linked into the clippy being tested. `target_compiler` is the latter,
773+
// and it must also be used by clippy's test runner to build tests and their dependencies.
774+
let target_compiler = builder.compiler(stage, host);
777775

778-
let tool_result = builder.ensure(tool::Clippy { compiler, target: self.host });
779-
let compiler = tool_result.build_compiler;
776+
let tool_result = builder.ensure(tool::Clippy { compiler: target_compiler, target: host });
777+
let tool_compiler = tool_result.build_compiler;
780778
let mut cargo = tool::prepare_tool_cargo(
781779
builder,
782-
compiler,
780+
tool_compiler,
783781
Mode::ToolRustc,
784782
host,
785783
Kind::Test,
@@ -788,11 +786,17 @@ impl Step for Clippy {
788786
&[],
789787
);
790788

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

794+
// Build the standard library that the tests can use.
795+
builder.std(target_compiler, host);
796+
cargo.env("TEST_SYSROOT", builder.sysroot(target_compiler));
797+
cargo.env("TEST_RUSTC", builder.rustc(target_compiler));
798+
cargo.env("TEST_RUSTC_LIB", builder.rustc_libdir(target_compiler));
799+
796800
// Collect paths of tests to run
797801
'partially_test: {
798802
let paths = &builder.config.paths[..];
@@ -813,7 +817,8 @@ impl Step for Clippy {
813817
cargo.add_rustc_lib_path(builder);
814818
let cargo = prepare_cargo_test(cargo, &[], &[], host, builder);
815819

816-
let _guard = builder.msg_sysroot_tool(Kind::Test, compiler.stage, "clippy", host, host);
820+
let _guard =
821+
builder.msg_sysroot_tool(Kind::Test, tool_compiler.stage, "clippy", host, host);
817822

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

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: 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 = FxHashMap::<i32, 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:20: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:24: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:28: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:32: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:38: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:42: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:46: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:50: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:54: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:58: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:62: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:68: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:72: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)