Skip to content

Commit 0ad892b

Browse files
authored
Merge pull request RustPython#3308 from youknowone/fix-hash
Fix __hash__ not to perform unnecessary downcast
2 parents 490a39e + 42912a3 commit 0ad892b

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

vm/src/types/slot.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -486,8 +486,8 @@ pub trait Hashable: PyValue {
486486

487487
#[inline]
488488
#[pymethod]
489-
fn __hash__(zelf: PyRef<Self>, vm: &VirtualMachine) -> PyResult<PyHash> {
490-
Self::hash(&zelf, vm)
489+
fn __hash__(zelf: PyObjectRef, vm: &VirtualMachine) -> PyResult<PyHash> {
490+
Self::slot_hash(&zelf, vm)
491491
}
492492

493493
fn hash(zelf: &PyRef<Self>, vm: &VirtualMachine) -> PyResult<PyHash>;
@@ -499,8 +499,11 @@ impl<T> Hashable for T
499499
where
500500
T: Unhashable,
501501
{
502-
fn hash(_zelf: &PyRef<Self>, vm: &VirtualMachine) -> PyResult<PyHash> {
503-
Err(vm.new_type_error(format!("unhashable type: '{}'", _zelf.class().name())))
502+
fn slot_hash(zelf: &PyObjectRef, vm: &VirtualMachine) -> PyResult<PyHash> {
503+
Err(vm.new_type_error(format!("unhashable type: '{}'", zelf.class().name())))
504+
}
505+
fn hash(_zelf: &PyRef<Self>, _vm: &VirtualMachine) -> PyResult<PyHash> {
506+
unreachable!("slot_hash is implemented for unhashable types");
504507
}
505508
}
506509

0 commit comments

Comments
 (0)