diff options
| author | bors <bors@rust-lang.org> | 2022-07-31 21:40:21 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2022-07-31 21:40:21 +0000 |
| commit | 34805f36752fd81cabc5104579e065e62fc0fec6 (patch) | |
| tree | c975eb618c87082bf5bcca000e122acb8b817836 | |
| parent | f9cba63746d0fff816250b2ba7b706b5d4dcf000 (diff) | |
| parent | 9139a63b25e53722d7225cf373e4d713e0d5596f (diff) | |
Auto merge of #99052 - tmiasko:bitset-clone-from, r=Mark-Simulacrum
Fix cloning from a BitSet with a different domain size The previous implementation incorrectly assumed that the number of words in a bit set is equal to the domain size. The new implementation delegates to `Vec::clone_from` which is specialized for `Copy` elements. Fixes #99006.
| -rw-r--r-- | compiler/rustc_index/src/bit_set.rs | 8 | ||||
| -rw-r--r-- | compiler/rustc_index/src/bit_set/tests.rs | 17 |
2 files changed, 19 insertions, 6 deletions
diff --git a/compiler/rustc_index/src/bit_set.rs b/compiler/rustc_index/src/bit_set.rs index 5b664e19c18..777112442f0 100644 --- a/compiler/rustc_index/src/bit_set.rs +++ b/compiler/rustc_index/src/bit_set.rs @@ -1061,12 +1061,8 @@ impl<T> Clone for BitSet<T> { } fn clone_from(&mut self, from: &Self) { - if self.domain_size != from.domain_size { - self.words.resize(from.domain_size, 0); - self.domain_size = from.domain_size; - } - - self.words.copy_from_slice(&from.words); + self.domain_size = from.domain_size; + self.words.clone_from(&from.words); } } diff --git a/compiler/rustc_index/src/bit_set/tests.rs b/compiler/rustc_index/src/bit_set/tests.rs index a58133e4aed..351d62feed9 100644 --- a/compiler/rustc_index/src/bit_set/tests.rs +++ b/compiler/rustc_index/src/bit_set/tests.rs @@ -41,6 +41,23 @@ fn bitset_iter_works_2() { } #[test] +fn bitset_clone_from() { + let mut a: BitSet<usize> = BitSet::new_empty(10); + a.insert(4); + a.insert(7); + a.insert(9); + + let mut b = BitSet::new_empty(2); + b.clone_from(&a); + assert_eq!(b.domain_size(), 10); + assert_eq!(b.iter().collect::<Vec<_>>(), [4, 7, 9]); + + b.clone_from(&BitSet::new_empty(40)); + assert_eq!(b.domain_size(), 40); + assert_eq!(b.iter().collect::<Vec<_>>(), []); +} + +#[test] fn union_two_sets() { let mut set1: BitSet<usize> = BitSet::new_empty(65); let mut set2: BitSet<usize> = BitSet::new_empty(65); |
