Skip to content

Commit 0d9fee8

Browse files
meggartKristofferC
authored andcommitted
fix reinterpret for 0-dimensional arrays (#30376)
(cherry picked from commit c379900)
1 parent 47bd2ba commit 0d9fee8

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

base/reinterpretarray.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ struct ReinterpretArray{T,N,S,A<:AbstractArray{S, N}} <: AbstractArray{T, N}
3333
isbitstype(T) || throwbits(S, T, T)
3434
isbitstype(S) || throwbits(S, T, S)
3535
(N != 0 || sizeof(T) == sizeof(S)) || throwsize0(S, T)
36-
ax1 = axes(a)[1]
3736
if N != 0 && sizeof(S) != sizeof(T)
37+
ax1 = axes(a)[1]
3838
dim = length(ax1)
3939
rem(dim*sizeof(S),sizeof(T)) == 0 || thrownonint(S, T, dim)
4040
first(ax1) == 1 || throwaxes1(S, T, ax1)
@@ -74,13 +74,15 @@ function size(a::ReinterpretArray{T,N,S} where {N}) where {T,S}
7474
size1 = div(psize[1]*sizeof(S), sizeof(T))
7575
tuple(size1, tail(psize)...)
7676
end
77+
size(a::ReinterpretArray{T,0}) where {T} = ()
7778

7879
function axes(a::ReinterpretArray{T,N,S} where {N}) where {T,S}
7980
paxs = axes(a.parent)
8081
f, l = first(paxs[1]), length(paxs[1])
8182
size1 = div(l*sizeof(S), sizeof(T))
8283
tuple(oftype(paxs[1], f:f+size1-1), tail(paxs)...)
8384
end
85+
axes(a::ReinterpretArray{T,0}) where {T} = ()
8486

8587
elsize(::Type{<:ReinterpretArray{T}}) where {T} = sizeof(T)
8688
unsafe_convert(::Type{Ptr{T}}, a::ReinterpretArray{T,N,S} where N) where {T,S} = Ptr{T}(unsafe_convert(Ptr{S},a.parent))

test/reinterpretarray.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,3 +160,12 @@ let a = [0.1 0.2; 0.3 0.4], at = reshape([(i,i+1) for i = 1:2:8], 2, 2)
160160
r = reinterpret(Int, vt)
161161
@test r == OffsetArray(reshape(1:8, 2, 2, 2), (0, offsetvt...))
162162
end
163+
164+
# Test 0-dimensional Arrays
165+
A = zeros(UInt32)
166+
B = reinterpret(Int32,A)
167+
@test size(B) == ()
168+
@test axes(B) == ()
169+
B[] = Int32(5)
170+
@test B[] === Int32(5)
171+
@test A[] === UInt32(5)

0 commit comments

Comments
 (0)