From 0c2ab06e6601b0b2428e1384f15997015ef3ac4e Mon Sep 17 00:00:00 2001 From: Dan Gohman Date: Fri, 1 Sep 2017 13:25:37 -0700 Subject: [PATCH] Make EntitySet::contains return false for out-of-bounds indices. This is consistent with EntityMap. --- lib/cretonne/src/entity/set.rs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/lib/cretonne/src/entity/set.rs b/lib/cretonne/src/entity/set.rs index 6d8d88fadd..b1c351b650 100644 --- a/lib/cretonne/src/entity/set.rs +++ b/lib/cretonne/src/entity/set.rs @@ -34,8 +34,11 @@ where /// Get the element at `k` if it exists. pub fn contains(&self, k: K) -> bool { let index = k.index(); - debug_assert!(index < self.len); - (self.elems[index / 8] & (1 << (index % 8))) != 0 + if index < self.len { + (self.elems[index / 8] & (1 << (index % 8))) != 0 + } else { + false + } } /// Is this set completely empty? @@ -106,6 +109,7 @@ mod tests { assert!(!m.contains(r0)); assert!(m.contains(r1)); assert!(m.contains(r2)); + assert!(!m.contains(E(3))); assert!(!m.is_empty()); let v: Vec = m.keys().collect(); @@ -128,6 +132,8 @@ mod tests { assert!(m.contains(E(15))); assert!(!m.contains(E(16))); assert!(!m.contains(E(19))); + assert!(!m.contains(E(20))); + assert!(!m.contains(E(u32::max_value()))); m.clear(); assert!(m.is_empty());