Skip to content

Commit

Permalink
Propagate inbounds in isassigned with CartesianIndex indices (JuliaLa…
Browse files Browse the repository at this point in the history
…ng#53305)

Close JuliaLang#53302

---------

Co-authored-by: Matt Bauman <mbauman@juliahub.com>
  • Loading branch information
2 people authored and tecosaur committed Mar 4, 2024
1 parent ee86cca commit edd2679
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
20 changes: 12 additions & 8 deletions base/multidimensional.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1582,19 +1582,23 @@ end
end
end

isassigned(a::AbstractArray, i::CartesianIndex) = isassigned(a, Tuple(i)...)
function isassigned(A::AbstractArray, i::Union{Integer, CartesianIndex}...)
isa(i, Tuple{Vararg{Int}}) || return isassigned(A, CartesianIndex(to_indices(A, i)))
@boundscheck checkbounds(Bool, A, i...) || return false
@propagate_inbounds isassigned(A::AbstractArray, i::CartesianIndex) = isassigned(A, Tuple(i)...)
@propagate_inbounds function isassigned(A::AbstractArray, i::Union{Integer, CartesianIndex}...)
return isassigned(A, CartesianIndex(to_indices(A, i)))
end
@inline function isassigned(A::AbstractArray, i::Integer...)
# convert to valid indices, checking for Bool
inds = to_indices(A, i)
@boundscheck checkbounds(Bool, A, inds...) || return false
S = IndexStyle(A)
ninds = length(i)
ninds = length(inds)
if (isa(S, IndexLinear) && ninds != 1)
return @inbounds isassigned(A, _to_linear_index(A, i...))
return @inbounds isassigned(A, _to_linear_index(A, inds...))
elseif (!isa(S, IndexLinear) && ninds != ndims(A))
return @inbounds isassigned(A, _to_subscript_indices(A, i...)...)
return @inbounds isassigned(A, _to_subscript_indices(A, inds...)...)
else
try
A[i...]
A[inds...]
true
catch e
if isa(e, BoundsError) || isa(e, UndefRefError)
Expand Down
14 changes: 13 additions & 1 deletion test/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -548,12 +548,24 @@ function test_primitives(::Type{T}, shape, ::Type{TestAbstractArray}) where T
@test firstindex(B, 1) == firstindex(A, 1) == first(axes(B, 1))
@test firstindex(B, 2) == firstindex(A, 2) == first(axes(B, 2))

# isassigned(a::AbstractArray, i::Int...)
@test !isassigned(B)
# isassigned(a::AbstractArray, i::Integer...)
j = rand(1:length(B))
@test isassigned(B, j)
if T == T24Linear
@test !isassigned(B, length(B) + 1)
end
# isassigned(a::AbstractArray, i::CartesianIndex)
@test isassigned(B, first(CartesianIndices(B)))
ind = last(CartesianIndices(B))
@test !isassigned(B, ind + oneunit(ind))
# isassigned(a::AbstractArray, i::Union{Integer,CartesianIndex}...)
@test isassigned(B, Int16.(first.(axes(B)))..., CartesianIndex(1,1))
# Bool isn't a valid index
@test_throws ArgumentError isassigned(B, Bool.(first.(axes(B)))..., CartesianIndex(1,1))
@test_throws ArgumentError isassigned(B, Bool.(first.(axes(B)))...)
@test_throws ArgumentError isassigned(B, true)
@test_throws ArgumentError isassigned(B, false)

# reshape(a::AbstractArray, dims::Dims)
@test_throws DimensionMismatch reshape(B, (0, 1))
Expand Down

0 comments on commit edd2679

Please sign in to comment.