Skip to content

Commit 21ffe45

Browse files
authored
Rollup merge of #92528 - tmiasko:combine-commutative, r=michaelwoerister
Make `Fingerprint::combine_commutative` associative The previous implementation swapped lower and upper 64-bits of a result of modular addition, so the function was non-associative. r? `@Aaron1011`
2 parents dca1e7a + 1d64b59 commit 21ffe45

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

compiler/rustc_data_structures/src/fingerprint.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ use rustc_serialize::{Decodable, Encodable};
33
use std::convert::TryInto;
44
use std::hash::{Hash, Hasher};
55

6+
#[cfg(test)]
7+
mod tests;
8+
69
#[derive(Eq, PartialEq, Ord, PartialOrd, Debug, Clone, Copy)]
710
#[repr(C)]
811
pub struct Fingerprint(u64, u64);
@@ -54,7 +57,7 @@ impl Fingerprint {
5457

5558
let c = a.wrapping_add(b);
5659

57-
Fingerprint((c >> 64) as u64, c as u64)
60+
Fingerprint(c as u64, (c >> 64) as u64)
5861
}
5962

6063
pub fn to_hex(&self) -> String {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
use super::*;
2+
3+
// Check that `combine_commutative` is order independent.
4+
#[test]
5+
fn combine_commutative_is_order_independent() {
6+
let a = Fingerprint::new(0xf6622fb349898b06, 0x70be9377b2f9c610);
7+
let b = Fingerprint::new(0xa9562bf5a2a5303c, 0x67d9b6c82034f13d);
8+
let c = Fingerprint::new(0x0d013a27811dbbc3, 0x9a3f7b3d9142ec43);
9+
let permutations = [(a, b, c), (a, c, b), (b, a, c), (b, c, a), (c, a, b), (c, b, a)];
10+
let f = a.combine_commutative(b).combine_commutative(c);
11+
for p in &permutations {
12+
assert_eq!(f, p.0.combine_commutative(p.1).combine_commutative(p.2));
13+
}
14+
}

0 commit comments

Comments
 (0)