From fe586526dfc74c9f77bfa541607c785a9b15b3c9 Mon Sep 17 00:00:00 2001 From: Alek Ratzloff Date: Mon, 23 Sep 2024 21:48:44 -0700 Subject: [PATCH] Fix get_attr_lazy I had misunderstood/misused the ? suffix operator for the Option type. Signed-off-by: Alek Ratzloff --- src/obj.rs | 33 +++++++++++++++++++-------------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/src/obj.rs b/src/obj.rs index 08a1876..82324d2 100644 --- a/src/obj.rs +++ b/src/obj.rs @@ -256,20 +256,24 @@ pub trait Obj: Debug + Display + Any + Trace { let mut type_inst = self.type_inst(); loop { - with_obj_downcast_mut(type_inst.clone(), |type_inst: &mut TypeInst| { - type_inst.vtable.get(name).cloned() - }) - .map(|vtable_entry| { - let ptr = if obj_is_inst::(&vtable_entry) - || obj_is_inst::(&vtable_entry) - { - MethodInst::create(method_ty.clone(), self_ptr.clone(), vtable_entry) - } else { - vtable_entry - }; - self.set_attr(name, ptr.clone()); - ptr - })?; + let vtable_entry = + with_obj_downcast_mut(type_inst.clone(), |type_inst: &mut TypeInst| { + type_inst.vtable.get(name).cloned() + }) + .map(|vtable_entry| { + let ptr = if obj_is_inst::(&vtable_entry) + || obj_is_inst::(&vtable_entry) + { + MethodInst::create(method_ty.clone(), self_ptr.clone(), vtable_entry) + } else { + vtable_entry + }; + self.set_attr(name, ptr.clone()); + ptr + }); + if vtable_entry.is_some() { + return vtable_entry; + } let type_inst_copy = type_inst.borrow().type_inst(); if type_inst.borrow().equals(&*type_inst_copy.borrow()) { return None; @@ -1158,6 +1162,7 @@ fn test_obj_equals() { assert!(obj2.borrow().equals(&*obj1.borrow())); } +#[test] fn test_obj_vtable() { let mut builtins = HashMap::new(); init_types(&mut builtins);