about summary refs log tree commit diff
path: root/compiler/rustc_data_structures/src/stable_hasher
diff options
context:
space:
mode:
authorJakub Beránek <berykubik@gmail.com>2022-01-22 10:40:52 +0100
committerJakub Beránek <berykubik@gmail.com>2022-01-24 15:35:52 +0100
commit1ffd043caf7598484a9f38c580aca91760a571f6 (patch)
tree26a6c64357a1d1f7f9ebcb4df5b130c4bca10443 /compiler/rustc_data_structures/src/stable_hasher
parentd2dc425721554348d4ed427f7eb89cdb49efefdb (diff)
downloadrust-1ffd043caf7598484a9f38c580aca91760a571f6.tar.gz
rust-1ffd043caf7598484a9f38c580aca91760a571f6.zip
Add test stable hash uniqueness of adjacent field values
Diffstat (limited to 'compiler/rustc_data_structures/src/stable_hasher')
-rw-r--r--compiler/rustc_data_structures/src/stable_hasher/tests.rs42
1 files changed, 42 insertions, 0 deletions
diff --git a/compiler/rustc_data_structures/src/stable_hasher/tests.rs b/compiler/rustc_data_structures/src/stable_hasher/tests.rs
index 391db67d29d..31190363eb6 100644
--- a/compiler/rustc_data_structures/src/stable_hasher/tests.rs
+++ b/compiler/rustc_data_structures/src/stable_hasher/tests.rs
@@ -98,3 +98,45 @@ fn test_hash_bit_matrix() {
     assert_ne!(a, b);
     assert_ne!(hash(&a), hash(&b));
 }
+
+// Check that exchanging the value of two adjacent fields changes the hash.
+#[test]
+fn test_attribute_permutation() {
+    macro_rules! test_type {
+        ($ty: ty) => {{
+            struct Foo {
+                a: $ty,
+                b: $ty,
+            }
+
+            impl<CTX> HashStable<CTX> for Foo {
+                fn hash_stable(&self, hcx: &mut CTX, hasher: &mut StableHasher) {
+                    self.a.hash_stable(hcx, hasher);
+                    self.b.hash_stable(hcx, hasher);
+                }
+            }
+
+            #[allow(overflowing_literals)]
+            let mut item = Foo { a: 0xFF, b: 0xFF_FF };
+            let hash_a = hash(&item);
+            std::mem::swap(&mut item.a, &mut item.b);
+            let hash_b = hash(&item);
+            assert_ne!(
+                hash_a,
+                hash_b,
+                "The hash stayed the same after values were swapped for type `{}`!",
+                stringify!($ty)
+            );
+        }};
+    }
+
+    test_type!(u16);
+    test_type!(u32);
+    test_type!(u64);
+    test_type!(u128);
+
+    test_type!(i16);
+    test_type!(i32);
+    test_type!(i64);
+    test_type!(i128);
+}