From 6c1f2f0887c00b434dffbc265f8c364676cae074 Mon Sep 17 00:00:00 2001 From: Igor Malovitsa Date: Mon, 30 Nov 2026 08:45:59 +0000 Subject: [PATCH] Answer None from try_borrow_focus below a dangling path held as an empty node With the focus below a dangling path whose node is empty, node_get_child finds that empty node, having consumed less of the key than the focus needs. try_borrow_focus and try_borrow_focus_mut handed it out as if it were the focus's node; a debug build failed the consumed == key length assertion. The node is above the focus, and the focus doesn't exist, so both now answer None, as the read zipper does. --- src/write_zipper.rs | 62 +++++++++++++++++++++++++++++++++++++-------- 1 file changed, 52 insertions(+), 10 deletions(-) diff --git a/src/write_zipper.rs b/src/write_zipper.rs index 1dd7a8d8..364a8394 100644 --- a/src/write_zipper.rs +++ b/src/write_zipper.rs @@ -1420,11 +1420,10 @@ impl <'a, 'path, V: Clone + Send + Sync + Unpin, A: Allocator + 'a> WriteZipperC self.focus_stack.root_mut() } else { match self.focus_stack.top_mut().unwrap().node_into_child_mut(node_key) { - Some((consumed_bytes, child_node)) => { - debug_assert_eq!(consumed_bytes, node_key.len()); - Some(child_node) - }, - None => None + Some((consumed_bytes, child_node)) if consumed_bytes == node_key.len() => Some(child_node), + //A child that ends short of the focus is a node *above* it: the focus is below a dangling + // path held as an empty node, so it doesn't exist + _ => None } } } @@ -1455,11 +1454,10 @@ impl <'a, 'path, V: Clone + Send + Sync + Unpin, A: Allocator + 'a> WriteZipperC Some(OpaqueTrieNodeRef( unsafe{ self.focus_stack.root_unchecked() } )) } else { match self.focus_stack.top().unwrap().node_get_child(node_key) { - Some((consumed_bytes, child_node)) => { - debug_assert_eq!(consumed_bytes, node_key.len()); - Some(OpaqueTrieNodeRef(child_node)) - }, - None => None + Some((consumed_bytes, child_node)) if consumed_bytes == node_key.len() => Some(OpaqueTrieNodeRef(child_node)), + //A child that ends short of the focus is a node *above* it: the focus is below a dangling + // path held as an empty node, so it doesn't exist + _ => None } } } @@ -4441,6 +4439,50 @@ mod tests { } } + /// `try_borrow_focus` / `try_borrow_focus_mut` at a missing focus below a dangling path held as an + /// empty node are `None`, as the read zipper's are. They used to hand out the empty node above the + /// focus as the focus's own (a debug assertion in debug builds) + #[test] + fn write_zipper_try_borrow_focus_below_dangling() { + let mut m = PathMap::::new(); + m.set_val_at(&[1u8], 0); + m.create_path(&[0u8]); + let mut wz = m.write_zipper(); + wz.descend_to(&[0u8, 0]); + assert!(!wz.path_exists()); + assert!(wz.try_borrow_focus().is_none()); + assert!(wz.core().try_borrow_focus_mut().is_none()); + drop(wz); + assert!(m.read_zipper_at_path(&[0u8, 0]).try_borrow_focus().is_none()); + + //Below a ZipperHead writer's stump + let mut m = PathMap::::new(); + { let zh = m.zipper_head(); let _w = zh.write_zipper_at_exclusive_path(&[0u8]).unwrap(); } + let mut wz = m.write_zipper(); + wz.descend_to(&[0u8, 7, 7]); + assert!(!wz.path_exists()); + assert!(wz.try_borrow_focus().is_none()); + assert!(wz.core().try_borrow_focus_mut().is_none()); + //At the stump itself, the node is the focus's + wz.reset(); + wz.descend_to(&[0u8]); + assert!(wz.path_exists()); + assert!(wz.try_borrow_focus().is_some()); + assert!(wz.core().try_borrow_focus_mut().is_some()); + drop(wz); + + //`graft_masked_branches` at a missing focus below the stump grafts at the focus, not into the + // stump's node + let src = PathMap::from_iter([(&[5u8][..], 50u64), (&[6u8][..], 60)]); + let mut wz = m.write_zipper(); + wz.descend_to(&[0u8, 7]); + wz.graft_masked_branches(&src.read_zipper(), ByteMask::from_iter([5u8]), false); + drop(wz); + assert_eq!(m.val_at(&[0u8, 7, 5]), Some(&50)); + assert_eq!(m.val_at(&[0u8, 5]), None); + assert_eq!(m.val_at(&[0u8, 7, 6]), None); + } + /// A write after `prune_path` (or `meet_into(.., true)`) must reach the focus node #[test] fn write_zipper_write_after_prune_path_below_a_graft() {