Skip to content

Commit 34805f3

Browse files
committed
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.
2 parents f9cba63 + 9139a63 commit 34805f3

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

compiler/rustc_index/src/bit_set.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -1061,12 +1061,8 @@ impl<T> Clone for BitSet<T> {
10611061
}
10621062

10631063
fn clone_from(&mut self, from: &Self) {
1064-
if self.domain_size != from.domain_size {
1065-
self.words.resize(from.domain_size, 0);
1066-
self.domain_size = from.domain_size;
1067-
}
1068-
1069-
self.words.copy_from_slice(&from.words);
1064+
self.domain_size = from.domain_size;
1065+
self.words.clone_from(&from.words);
10701066
}
10711067
}
10721068

compiler/rustc_index/src/bit_set/tests.rs

+17
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,23 @@ fn bitset_iter_works_2() {
4040
assert_eq!(bitset.iter().collect::<Vec<_>>(), [0, 127, 191, 255, 319]);
4141
}
4242

43+
#[test]
44+
fn bitset_clone_from() {
45+
let mut a: BitSet<usize> = BitSet::new_empty(10);
46+
a.insert(4);
47+
a.insert(7);
48+
a.insert(9);
49+
50+
let mut b = BitSet::new_empty(2);
51+
b.clone_from(&a);
52+
assert_eq!(b.domain_size(), 10);
53+
assert_eq!(b.iter().collect::<Vec<_>>(), [4, 7, 9]);
54+
55+
b.clone_from(&BitSet::new_empty(40));
56+
assert_eq!(b.domain_size(), 40);
57+
assert_eq!(b.iter().collect::<Vec<_>>(), []);
58+
}
59+
4360
#[test]
4461
fn union_two_sets() {
4562
let mut set1: BitSet<usize> = BitSet::new_empty(65);

0 commit comments

Comments
 (0)