diff --git a/common/src/hash.rs b/common/src/hash.rs index f1762c2eb..3447f9193 100644 --- a/common/src/hash.rs +++ b/common/src/hash.rs @@ -141,11 +141,21 @@ pub fn hash_float(value: f64) -> Option { } #[inline] -pub fn hash_complex(value: &Complex64) -> Option { - let re_hash = hash_float(value.re)?; - let im_hash = hash_float(value.im)?; +pub fn hash_complex(value: &Complex64) -> PyHash { + let re_hash = (match hash_float(value.re) { + Some(value) => Some(value), + None => Some(hash_pointer(value as *const _ as *const std::ffi::c_void)), + }) + .unwrap(); + + let im_hash = (match hash_float(value.im) { + Some(value) => Some(value), + None => Some(hash_pointer(value as *const _ as *const std::ffi::c_void)), + }) + .unwrap(); + let Wrapping(ret) = Wrapping(re_hash) + Wrapping(im_hash) * Wrapping(IMAG); - Some(fix_sentinel(ret)) + fix_sentinel(ret) } pub fn hash_iter_unordered<'a, T: 'a, I, F, E>(iter: I, hashf: F) -> Result diff --git a/vm/src/builtins/complex.rs b/vm/src/builtins/complex.rs index 642248e1a..333269e4d 100644 --- a/vm/src/builtins/complex.rs +++ b/vm/src/builtins/complex.rs @@ -417,12 +417,7 @@ impl Comparable for PyComplex { impl Hashable for PyComplex { #[inline] fn hash(zelf: &crate::Py, _vm: &VirtualMachine) -> PyResult { - match hash::hash_complex(&zelf.value) { - Some(value) => Ok(value), - None => Ok(hash::hash_pointer( - zelf as *const _ as *const std::ffi::c_void, - )), - } + Ok(hash::hash_complex(&zelf.value)) } }