Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ is considered a bug fix ([#47102])

- The `hash` algorithm and its values have changed. Most `hash` specializations will remain correct and require no action. Types that reimplement the core hashing logic independently, such as some third-party string packages do, may require a migration to the new algorithm. ([#57509])

* Indexless `getindex` and `setindex!` (i.e. `A[]`) on `ReinterpretArray` now correctly throw a `BoundsError` when there is more than one element. ([#58814])

Compiler/Runtime improvements
-----------------------------

Expand Down
4 changes: 2 additions & 2 deletions base/reinterpretarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ check_ptr_indexable(a::Array, sz) = sizeof(eltype(a)) !== sz
check_ptr_indexable(a::Memory, sz) = true
check_ptr_indexable(a::AbstractArray, sz) = false

@propagate_inbounds getindex(a::ReinterpretArray) = a[firstindex(a)]
@propagate_inbounds getindex(a::ReshapedReinterpretArray{T,0}) where {T} = a[firstindex(a)]

@propagate_inbounds isassigned(a::ReinterpretArray, inds::Integer...) = checkbounds(Bool, a, inds...) && (check_ptr_indexable(a) || _isassigned_ra(a, inds...))
@propagate_inbounds isassigned(a::ReinterpretArray, inds::SCartesianIndex2) = isassigned(a.parent, inds.j)
Expand Down Expand Up @@ -541,7 +541,7 @@ end
setindex!(a, v, firstindex(a))
end

@propagate_inbounds setindex!(a::ReinterpretArray, v) = setindex!(a, v, firstindex(a))
@propagate_inbounds setindex!(a::ReshapedReinterpretArray{T,0}, v) where {T} = setindex!(a, v, firstindex(a))

@propagate_inbounds function setindex!(a::ReinterpretArray{T,N,S}, v, inds::Vararg{Int, N}) where {T,N,S}
check_writable(a)
Expand Down
13 changes: 9 additions & 4 deletions test/reinterpretarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ A = Int64[1, 2, 3, 4]
Ars = Int64[1 3; 2 4]
B = Complex{Int64}[5+6im, 7+8im, 9+10im]
Av = [Int32[1,2], Int32[3,4]]
C = view([1,1], [1,2])

test_many_wrappers(Ars, (identity, tslow)) do Ar
@test @inferred(ndims(reinterpret(reshape, Complex{Int64}, Ar))) == 1
Expand All @@ -36,6 +37,10 @@ test_many_wrappers(B, (identity, tslow)) do _B
@test @inferred(size(reinterpret(reshape, Int128, _B))) == (3,)
end

test_many_wrappers(C) do Cr
@test reinterpret(reshape, Tuple{Int8, Int}, Cr) == fill((1,1))
end

@test_throws ArgumentError("cannot reinterpret `Int64` as `Vector{Int64}`, type `Vector{Int64}` is not a bits type") reinterpret(Vector{Int64}, A)
@test_throws ArgumentError("cannot reinterpret `Vector{Int32}` as `Int32`, type `Vector{Int32}` is not a bits type") reinterpret(Int32, Av)
@test_throws ArgumentError("cannot reinterpret a zero-dimensional `Int64` array to `Int32` which is of a different size") reinterpret(Int32, reshape([Int64(0)]))
Expand Down Expand Up @@ -272,7 +277,7 @@ test_many_wrappers(fill(1.0, 5, 3), (identity, wrapper)) do a_
fill!(r, 2)
@test all(a .=== reinterpret(Float64, [Int64(2)])[1])
@test all(r .=== Int64(2))
for badinds in (0, 16, (0,1), (1,0), (6,3), (5,4))
for badinds in ((), 0, 16, (0,1), (1,0), (6,3), (5,4))
@test_throws BoundsError r[badinds...]
@test_throws BoundsError r[badinds...] = -2
end
Expand All @@ -285,7 +290,7 @@ test_many_wrappers(fill(1.0, 5, 3), (identity, wrapper)) do a_
fill!(r, 3)
@test all(a .=== reinterpret(Float64, [(Int32(3), Int32(3))])[1])
@test all(r .=== Int32(3))
for badinds in (0, 31, (0,1), (1,0), (11,3), (10,4))
for badinds in ((), 0, 31, (0,1), (1,0), (11,3), (10,4))
@test_throws BoundsError r[badinds...]
@test_throws BoundsError r[badinds...] = -3
end
Expand All @@ -298,7 +303,7 @@ test_many_wrappers(fill(1.0, 5, 3), (identity, wrapper)) do a_
fill!(r, 4)
@test all(a[1:2:5,:] .=== reinterpret(Float64, [Int64(4)])[1])
@test all(r .=== Int64(4))
for badinds in (0, 10, (0,1), (1,0), (4,3), (3,4))
for badinds in ((), 0, 10, (0,1), (1,0), (4,3), (3,4))
@test_throws BoundsError r[badinds...]
@test_throws BoundsError r[badinds...] = -4
end
Expand All @@ -311,7 +316,7 @@ test_many_wrappers(fill(1.0, 5, 3), (identity, wrapper)) do a_
fill!(r, 5)
@test all(a[1:2:5,:] .=== reinterpret(Float64, [(Int32(5), Int32(5))])[1])
@test all(r .=== Int32(5))
for badinds in (0, 19, (0,1), (1,0), (7,3), (6,4))
for badinds in ((), 0, 19, (0,1), (1,0), (7,3), (6,4))
@test_throws BoundsError r[badinds...]
@test_throws BoundsError r[badinds...] = -5
end
Expand Down