Skip to content

Commit 15b24c4

Browse files
committed
Clarify ChunkSize invariants.
`ChunkedBitSet::is_empty` currently does an unnecessary check. This commit removes that check and adds clarifying comments and an assertion that demonstrate why it's unnecessary.
1 parent 6b6a867 commit 15b24c4

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

compiler/rustc_index/src/bit_set.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -369,9 +369,11 @@ pub struct ChunkedBitSet<T> {
369369
#[derive(Clone, Debug, PartialEq, Eq)]
370370
enum Chunk {
371371
/// A chunk that is all zeros; we don't represent the zeros explicitly.
372+
/// The `ChunkSize` is always non-zero.
372373
Zeros(ChunkSize),
373374

374375
/// A chunk that is all ones; we don't represent the ones explicitly.
376+
/// `ChunkSize` is always non-zero.
375377
Ones(ChunkSize),
376378

377379
/// A chunk that has a mix of zeros and ones, which are represented
@@ -384,8 +386,10 @@ enum Chunk {
384386
/// words are always be zero, as are any excess bits in the final in-use
385387
/// word.
386388
///
387-
/// The second field is the count of 1s set in the chunk, and must satisfy
388-
/// `0 < count < chunk_domain_size`.
389+
/// The first `ChunkSize` field is always non-zero.
390+
///
391+
/// The second `ChunkSize` field is the count of 1s set in the chunk, and
392+
/// must satisfy `0 < count < chunk_domain_size`.
389393
///
390394
/// The words are within an `Rc` because it's surprisingly common to
391395
/// duplicate an entire chunk, e.g. in `ChunkedBitSet::clone_from()`, or
@@ -461,7 +465,7 @@ impl<T: Idx> ChunkedBitSet<T> {
461465
}
462466

463467
pub fn is_empty(&self) -> bool {
464-
self.chunks.iter().all(|chunk| matches!(chunk, Chunk::Zeros(..) | Chunk::Ones(0)))
468+
self.chunks.iter().all(|chunk| matches!(chunk, Chunk::Zeros(..)))
465469
}
466470

467471
/// Returns `true` if `self` contains `elem`.
@@ -1005,7 +1009,7 @@ impl Chunk {
10051009
}
10061010

10071011
fn new(chunk_domain_size: usize, is_empty: bool) -> Self {
1008-
debug_assert!(chunk_domain_size <= CHUNK_BITS);
1012+
debug_assert!(0 < chunk_domain_size && chunk_domain_size <= CHUNK_BITS);
10091013
let chunk_domain_size = chunk_domain_size as ChunkSize;
10101014
if is_empty { Zeros(chunk_domain_size) } else { Ones(chunk_domain_size) }
10111015
}

0 commit comments

Comments
 (0)