Skip to content

Commit

Permalink
Fix sizeof for non-Array subarrays
Browse files Browse the repository at this point in the history
Not all `AbstractArray`s define elsize.  Cf. #35900, #36714.
  • Loading branch information
mbauman committed Jul 17, 2020
1 parent 971e769 commit 2cd6466
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
3 changes: 2 additions & 1 deletion base/subarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ size(V::SubArray) = (@_inline_meta; map(n->Int(unsafe_length(n)), axes(V)))

similar(V::SubArray, T::Type, dims::Dims) = similar(V.parent, T, dims)

sizeof(V::SubArray) = length(V) * elsize(V.parent)
sizeof(V::SubArray) = length(V) * sizeof(eltype(V))
sizeof(V::SubArray{<:Any,<:Any,<:Array}) = length(V) * elsize(V.parent)

copy(V::SubArray) = V.parent[V.indices...]

Expand Down
4 changes: 4 additions & 0 deletions test/subarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,10 @@ end
arrayOfUInt48 = [a, b, c];

@test sizeof(view(arrayOfUInt48, 1:2)) == 16

@test sizeof(view(Diagonal(zeros(UInt8, 10)), 1:4)) == 4
@test sizeof(view(Diagonal(zeros(UInt8, 10)), 1:3)) == 3
@test sizeof(view(Diagonal(zeros(Float64, 10)), 1:3, 2:6)) == 120
end


Expand Down

0 comments on commit 2cd6466

Please sign in to comment.