Conversation
`Base.iterate` indexed by the reported (max-extent) `size` without an `isassigned` guard, so iterating a ragged or sparsely filled array raised a bare `BoundsError` pointing at an internal vector, with no hint about the real problem or the supported alternative. `iterate` now checks `isassigned` and raises an error naming the unassigned index and pointing at `GraphPPL.vec`, which already guards with `isassigned` and is the supported way to traverse ragged arrays. Note it deliberately does *not* skip unassigned slots. `ResizableArray <: AbstractArray` and there is no `length`/`IteratorSize` override, so `length == prod(size)`; skipping would break the iteration protocol and leave `collect`/`map` results holding uninitialized memory. Failing loudly is the safer contract for a package that must build factor graphs deterministically. Also guards the empty case in the 0-argument method, which previously destructured `iterate(CartesianIndices(...))` unconditionally. Closes #308. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #323 +/- ##
==========================================
- Coverage 90.72% 90.70% -0.02%
==========================================
Files 16 16
Lines 2263 2270 +7
==========================================
+ Hits 2053 2059 +6
- Misses 210 211 +1 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #308. Replaces #317, which was closed — see that discussion.
Problem
Base.iterateforResizableArrayindexed by the reported (max-extent)sizewith noisassignedguard, so iterating a ragged or sparsely filled array raised a bareBoundsErroragainst an internal vector — no mention of the real problem, no hint at the alternative.
Why not just skip the unassigned slots
That was the approach in #317, and it is unsafe.
ResizableArray <: AbstractArray{T, N}andthere is no
Base.length/Base.IteratorSizeoverride, solength(a) == prod(size(a))— themax extent, not the assigned count. (
__length, which does count assigned elements, exists atsrc/resizable_array.jl:196but is never used.)collectand friends therefore preallocateprod(size)and fill by iteration; if iteration ends early, the tail is uninitialized:Silently returning garbage is strictly worse than crashing for a package that must build the same
factor graph deterministically. Making skipping correct would mean changing
length/IteratorSizeto disagree with
size, which is awkward while the type is anAbstractArray— out of scope here.Change
iterateguards withisassignedand raises a descriptive error instead:vecalready guards withisassignedand is the supported traversal for ragged arrays; theexisting
"sparse ResizableArray"testset already documented that intent.Also guards the empty case in the 0-argument method, which previously destructured
iterate(CartesianIndices(...))unconditionally.Dense iteration order and the
zip/enumerateprotocol are unchanged.Tests
Added to the existing
"iterate"test item intest/resizable_array_tests.jl: a ragged 2×2 arraywhere
collect,map, a comprehension and aforloop each throw the new message,vecstillreturns the assigned elements, and empty arrays of rank 1–3 iterate to zero elements.
Verified the new assertions fail on
main(8 failures) and pass here. Full suite on Julia 1.12:258/258 test items, all passing — relevant because GraphPPL uses
ResizableArrayfor vector andtensor model variables, so this confirms nothing internally relies on iterating ragged arrays.
Known limitation
Broadcasting (
a .+ 1) still raises a rawBoundsError, because it goes throughgetindexratherthan
iterate. That is pre-existing and still loud, so it is left alone here.🤖 Generated with Claude Code