Skip to content

Commit 605513a

Browse files
committed
Don't sort a Vec before computing its DepTrackingHash
Previously, we sorted the vec prior to hashing, making the hash independent of the original (command-line argument) order. However, the original vec was still always kept in the original order, so we were relying on the rest of the compiler always working with it in an 'order-independent' way. This assumption was not being upheld by the `native_libraries` query - the order of the entires in its result depends on the order of entries in `Options.libs`. This lead to an 'unstable fingerprint' ICE when the `-l` arguments were re-ordered. This PR removes the sorting logic entirely. Re-ordering command-line arguments (without adding/removing/changing any arguments) seems like a really niche use case, and correctly optimizing for it would require additional work. By always hashing arguments in their original order, we can entirely avoid a cause of 'unstable fingerprint' errors.
1 parent ff2c947 commit 605513a

File tree

4 files changed

+31
-30
lines changed

4 files changed

+31
-30
lines changed

compiler/rustc_interface/src/tests.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,8 @@ fn test_lints_tracking_hash_different_construction_order() {
252252
(String::from("d"), Level::Forbid),
253253
];
254254

255-
assert_same_hash(&v1, &v2);
255+
// The hash should be order-dependent
256+
assert_different_hash(&v1, &v2);
256257
}
257258

258259
#[test]
@@ -491,9 +492,10 @@ fn test_native_libs_tracking_hash_different_order() {
491492
},
492493
];
493494

494-
assert_same_hash(&v1, &v2);
495-
assert_same_hash(&v1, &v3);
496-
assert_same_hash(&v2, &v3);
495+
// The hash should be order-dependent
496+
assert_different_hash(&v1, &v2);
497+
assert_different_hash(&v1, &v3);
498+
assert_different_hash(&v2, &v3);
497499
}
498500

499501
#[test]

compiler/rustc_session/src/config.rs

+10-26
Original file line numberDiff line numberDiff line change
@@ -2427,22 +2427,6 @@ crate mod dep_tracking {
24272427
)+};
24282428
}
24292429

2430-
macro_rules! impl_dep_tracking_hash_for_sortable_vec_of {
2431-
($($t:ty),+ $(,)?) => {$(
2432-
impl DepTrackingHash for Vec<$t> {
2433-
fn hash(&self, hasher: &mut DefaultHasher, error_format: ErrorOutputType) {
2434-
let mut elems: Vec<&$t> = self.iter().collect();
2435-
elems.sort();
2436-
Hash::hash(&elems.len(), hasher);
2437-
for (index, elem) in elems.iter().enumerate() {
2438-
Hash::hash(&index, hasher);
2439-
DepTrackingHash::hash(*elem, hasher, error_format);
2440-
}
2441-
}
2442-
}
2443-
)+};
2444-
}
2445-
24462430
impl_dep_tracking_hash_via_hash!(
24472431
bool,
24482432
usize,
@@ -2491,16 +2475,6 @@ crate mod dep_tracking {
24912475
TrimmedDefPaths,
24922476
);
24932477

2494-
impl_dep_tracking_hash_for_sortable_vec_of!(
2495-
String,
2496-
PathBuf,
2497-
(PathBuf, PathBuf),
2498-
CrateType,
2499-
NativeLib,
2500-
(String, lint::Level),
2501-
(String, u64)
2502-
);
2503-
25042478
impl<T1, T2> DepTrackingHash for (T1, T2)
25052479
where
25062480
T1: DepTrackingHash,
@@ -2530,6 +2504,16 @@ crate mod dep_tracking {
25302504
}
25312505
}
25322506

2507+
impl<T: DepTrackingHash> DepTrackingHash for Vec<T> {
2508+
fn hash(&self, hasher: &mut DefaultHasher, error_format: ErrorOutputType) {
2509+
Hash::hash(&self.len(), hasher);
2510+
for (index, elem) in self.iter().enumerate() {
2511+
Hash::hash(&index, hasher);
2512+
DepTrackingHash::hash(elem, hasher, error_format);
2513+
}
2514+
}
2515+
}
2516+
25332517
// This is a stable hash because BTreeMap is a sorted container
25342518
crate fn stable_hash(
25352519
sub_hashes: BTreeMap<&'static str, &dyn DepTrackingHash>,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// no-prefer-dynamic
2+
//[cfail1] compile-flags: -lbar -lfoo --crate-type lib
3+
//[cfail2] compile-flags: -lfoo -lbar --crate-type lib
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// aux-build:my_lib.rs
2+
// error-pattern: error: linking with
3+
// revisions:cfail1 cfail2
4+
// compile-flags:-Z query-dep-graph
5+
6+
// Tests that re-ordering the `-l` arguments used
7+
// when compiling an external dependency does not lead to
8+
// an 'unstable fingerprint' error.
9+
10+
extern crate my_lib;
11+
12+
fn main() {}

0 commit comments

Comments
 (0)