Skip to content

Keep a ChainedVector consistent when deleteat! throws - #140

Open
quinnj-bot[bot] wants to merge 2 commits into
mainfrom
claude/chainedvector-deleteat-consistency
Open

quinnj-bot[bot] wants to merge 2 commits into
mainfrom
claude/chainedvector-deleteat-consistency

Conversation

@quinnj-bot

@quinnj-bot quinnj-bot Bot commented Nov 19, 2026 •

Copy link
Copy Markdown

Why

A ChainedVector should stay usable after an operation throws, as a Vector does. deleteat!(x, inds) deletes from one chunk at a time and recomputed its chunk offsets only at the end. When a later index was out of bounds or out of order, the error skipped that step. x kept its old length while its chunks were shorter, so later reads went past the end of a chunk. With bounds checks off, those reads return unrelated memory.

julia> x = ChainedVector([[1, 2, 3], [4, 5]]); deleteat!(x, [1, 9])
ERROR: BoundsError: attempt to access 5-element ChainedVector{Int64, Vector{Int64}} at index [9]

julia> length(x), length.(x.arrays)
(5, [2, 2])     # 5 elements claimed, 4 stored

What changed

  • deleteat!(x, inds) recomputes the chunk offsets in a finally block, with the same setinds! helper the constructor uses, so x matches its chunks whether or not an index throws.

Scope

  • This change keeps x consistent. It does not undo deletions made before the error, so x ends with those elements removed. Vector doesn't undo a failed deleteat! either: it keeps its length but can be left with shifted, duplicated elements (deleteat!([1, 2, 3, 4, 5], [1, 4, 3]) leaves [2, 3, 3, 4, 5]).

Blast radius

Successful calls do the same work as before. The diff re-indents the loop inside try.

Verification

🤖 Generated with Claude Code

quinnj and others added 2 commits November 18, 2026 23:04
Fails on main; passes with the next commit.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
deleteat!(x, inds) shrinks one chunk at a time and recomputed the chunk
offsets only after the last index. An out-of-bounds or unsorted index
threw before that step, leaving offsets that point past the end of the
shortened chunks. Recompute them with setinds! in a finally block.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant