Skip to content

Commit 6098eb6

Browse files
committed
Fix wrong implementation of hash_complex
1 parent 3c0f3ca commit 6098eb6

File tree

2 files changed

+15
-10
lines changed

2 files changed

+15
-10
lines changed

common/src/hash.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -141,11 +141,21 @@ pub fn hash_float(value: f64) -> Option<PyHash> {
141141
}
142142

143143
#[inline]
144-
pub fn hash_complex(value: &Complex64) -> Option<PyHash> {
145-
let re_hash = hash_float(value.re)?;
146-
let im_hash = hash_float(value.im)?;
144+
pub fn hash_complex(value: &Complex64) -> PyHash {
145+
let re_hash = (match hash_float(value.re) {
146+
Some(value) => Some(value),
147+
None => Some(hash_pointer(value as *const _ as *const std::ffi::c_void)),
148+
})
149+
.unwrap();
150+
151+
let im_hash = (match hash_float(value.im) {
152+
Some(value) => Some(value),
153+
None => Some(hash_pointer(value as *const _ as *const std::ffi::c_void)),
154+
})
155+
.unwrap();
156+
147157
let Wrapping(ret) = Wrapping(re_hash) + Wrapping(im_hash) * Wrapping(IMAG);
148-
Some(fix_sentinel(ret))
158+
fix_sentinel(ret)
149159
}
150160

151161
pub fn hash_iter_unordered<'a, T: 'a, I, F, E>(iter: I, hashf: F) -> Result<PyHash, E>

vm/src/builtins/complex.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -417,12 +417,7 @@ impl Comparable for PyComplex {
417417
impl Hashable for PyComplex {
418418
#[inline]
419419
fn hash(zelf: &crate::Py<Self>, _vm: &VirtualMachine) -> PyResult<hash::PyHash> {
420-
match hash::hash_complex(&zelf.value) {
421-
Some(value) => Ok(value),
422-
None => Ok(hash::hash_pointer(
423-
zelf as *const _ as *const std::ffi::c_void,
424-
)),
425-
}
420+
Ok(hash::hash_complex(&zelf.value))
426421
}
427422
}
428423

0 commit comments

Comments
 (0)