From 97623b4e727628c6036bddb12ca36555e7e36d85 Mon Sep 17 00:00:00 2001 From: Jeong YunWon Date: Fri, 15 Oct 2021 05:02:13 +0900 Subject: [PATCH 1/2] Fix __hash__ not to perform unnecessary downcast --- vm/src/types/slot.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/vm/src/types/slot.rs b/vm/src/types/slot.rs index 4ce18e15d4..fea9b2e091 100644 --- a/vm/src/types/slot.rs +++ b/vm/src/types/slot.rs @@ -486,8 +486,8 @@ pub trait Hashable: PyValue { #[inline] #[pymethod] - fn __hash__(zelf: PyRef, vm: &VirtualMachine) -> PyResult { - Self::hash(&zelf, vm) + fn __hash__(zelf: PyObjectRef, vm: &VirtualMachine) -> PyResult { + Self::slot_hash(&zelf, vm) } fn hash(zelf: &PyRef, vm: &VirtualMachine) -> PyResult; From 42912a36a1ce33e4a6d47ed26d7b1cc5044d0790 Mon Sep 17 00:00:00 2001 From: Jeong YunWon Date: Fri, 15 Oct 2021 16:31:31 +0900 Subject: [PATCH 2/2] No downcast for unhashable --- vm/src/types/slot.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/vm/src/types/slot.rs b/vm/src/types/slot.rs index fea9b2e091..ea5a1449c3 100644 --- a/vm/src/types/slot.rs +++ b/vm/src/types/slot.rs @@ -499,8 +499,11 @@ impl Hashable for T where T: Unhashable, { - fn hash(_zelf: &PyRef, vm: &VirtualMachine) -> PyResult { - Err(vm.new_type_error(format!("unhashable type: '{}'", _zelf.class().name()))) + fn slot_hash(zelf: &PyObjectRef, vm: &VirtualMachine) -> PyResult { + Err(vm.new_type_error(format!("unhashable type: '{}'", zelf.class().name()))) + } + fn hash(_zelf: &PyRef, _vm: &VirtualMachine) -> PyResult { + unreachable!("slot_hash is implemented for unhashable types"); } }