Skip to content

ResizableArray iteration throws BoundsError on ragged/sparse tensors #308

Description

@docxology

Summary

Base.iterate for ResizableArray (src/resizable_array.jl:175-194) indexes by the
reported (= max-extent) size without an isassigned guard. Iterating a ragged or
sparsely-filled tensor (collect, for … in arr, map, broadcast) raises BoundsError
for unassigned slots. Base.vec/first/firstwithindex/lastwithindex do guard with
isassigned, so iteration is the odd one out.

Repro

using GraphPPL
a = GraphPPL.ResizableArray(Float64, Val(2))
a[1, 1] = 1.0; a[1, 2] = 2.0; a[2, 1] = 3.0   # row 2 has no [2,2]
size(a)                    # (2, 2)  — max extent
isassigned(a, 2, 2)        # false
collect(a)                 # ERROR: BoundsError
GraphPPL.vec(a)            # [1.0, 3.0, 2.0]  (guarded — works)

Suggested fix

Guard iterate (both the 0-arg and state-carrying methods) with isassigned, skipping
unassigned slots — consistent with vec/first. Alternatively document that full iteration
requires a dense array and steer users to vec.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions