Skip to content

Commit 7c80405

Browse files
mbaumanadienes
andauthored
fix setindex(::ReinterpretArray,...) for zero-d arrays (#58868)
by copying the way getindex works. Found in #58814 (comment) --------- Co-authored-by: Andy Dienes <[email protected]>
1 parent ed1fd39 commit 7c80405

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

base/reinterpretarray.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,7 @@ end
412412
# Convert to full indices here, to avoid needing multiple conversions in
413413
# the loop in _getindex_ra
414414
inds = _to_subscript_indices(a, i)
415-
isempty(inds) ? _getindex_ra(a, 1, ()) : _getindex_ra(a, inds[1], tail(inds))
415+
isempty(inds) ? _getindex_ra(a, firstindex(a), ()) : _getindex_ra(a, inds[1], tail(inds))
416416
end
417417

418418
@propagate_inbounds function getindex(a::ReshapedReinterpretArray{T,N,S}, ind::SCartesianIndex2) where {T,N,S}
@@ -556,7 +556,7 @@ end
556556
return _setindex_ra!(a, v, i, ())
557557
end
558558
inds = _to_subscript_indices(a, i)
559-
_setindex_ra!(a, v, inds[1], tail(inds))
559+
isempty(inds) ? _setindex_ra!(a, v, firstindex(a), ()) : _setindex_ra!(a, v, inds[1], tail(inds))
560560
end
561561

562562
@propagate_inbounds function setindex!(a::ReshapedReinterpretArray{T,N,S}, v, ind::SCartesianIndex2) where {T,N,S}

test/reinterpretarray.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,16 @@ test_many_wrappers(A3) do A3_
160160
@test A3[2,1,2] == 400
161161
end
162162

163+
test_many_wrappers(C) do Cr
164+
r = reinterpret(reshape, Tuple{Int, Int}, Cr)
165+
r[] = (2,2)
166+
@test r[] === (2,2)
167+
r[1] = (3,3)
168+
@test r[1] === (3,3)
169+
r[1,1] = (4,4)
170+
@test r[1,1] === (4,4)
171+
end
172+
163173
# same-size reinterpret where one of the types is non-primitive
164174
let a = NTuple{4,UInt8}[(0x01,0x02,0x03,0x04)]
165175
test_many_wrappers(a, (identity, wrapper, fcviews)) do a_

0 commit comments

Comments
 (0)