Skip to content

Commit 9738031

Browse files
authored
Rollup merge of rust-lang#144027 - RalfJung:clippy, r=Mark-Simulacrum
clippy: make tests work in stage 1 This finally fixes rust-lang#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 e81009d + 50f36c0 commit 9738031

13 files changed

+64
-39
lines changed

clippy_test_deps/Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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"

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"

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
}

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

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
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

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

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 | |

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;

tests/ui/strlen_on_c_strings.rs

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)