]> git.lizzy.rs Git - rust.git/blob - tests/ui/mut_key.rs
Fix FP when using raw pointers as hashed keys
[rust.git] / tests / ui / mut_key.rs
1 use std::cell::Cell;
2 use std::collections::{HashMap, HashSet};
3 use std::hash::{Hash, Hasher};
4 use std::sync::atomic::{AtomicUsize, Ordering::Relaxed};
5
6 struct Key(AtomicUsize);
7
8 impl Clone for Key {
9     fn clone(&self) -> Self {
10         Key(AtomicUsize::new(self.0.load(Relaxed)))
11     }
12 }
13
14 impl PartialEq for Key {
15     fn eq(&self, other: &Self) -> bool {
16         self.0.load(Relaxed) == other.0.load(Relaxed)
17     }
18 }
19
20 impl Eq for Key {}
21
22 impl Hash for Key {
23     fn hash<H: Hasher>(&self, h: &mut H) {
24         self.0.load(Relaxed).hash(h);
25     }
26 }
27
28 fn should_not_take_this_arg(m: &mut HashMap<Key, usize>, _n: usize) -> HashSet<Key> {
29     let _other: HashMap<Key, bool> = HashMap::new();
30     m.keys().cloned().collect()
31 }
32
33 fn this_is_ok(_m: &mut HashMap<usize, Key>) {}
34
35 // Raw pointers are hashed by the address they point to, so it doesn't matter if they point to a
36 // type with interior mutability.  See:
37 // - clippy issue: https://github.com/rust-lang/rust-clippy/issues/6745
38 // - std lib: https://github.com/rust-lang/rust/blob/1.54.0/library/core/src/hash/mod.rs#L717-L736
39 // So these are OK:
40 fn raw_ptr_is_ok(_m: &mut HashMap<*const Key, ()>) {}
41 fn raw_mut_ptr_is_ok(_m: &mut HashMap<*mut Key, ()>) {}
42
43 #[allow(unused)]
44 trait Trait {
45     type AssociatedType;
46
47     fn trait_fn(&self, set: HashSet<Self::AssociatedType>);
48 }
49
50 fn generics_are_ok_too<K>(_m: &mut HashSet<K>) {
51     // nothing to see here, move along
52 }
53
54 fn tuples<U>(_m: &mut HashMap<((), U), ()>) {}
55
56 fn tuples_bad<U>(_m: &mut HashMap<(Key, U), bool>) {}
57
58 fn main() {
59     let _ = should_not_take_this_arg(&mut HashMap::new(), 1);
60     this_is_ok(&mut HashMap::new());
61     tuples::<Key>(&mut HashMap::new());
62     tuples::<()>(&mut HashMap::new());
63     tuples_bad::<()>(&mut HashMap::new());
64
65     raw_ptr_is_ok(&mut HashMap::new());
66     raw_mut_ptr_is_ok(&mut HashMap::new());
67 }