Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Propagate inbounds in isassigned with CartesianIndex indices #53305

Merged
merged 5 commits into from
Feb 16, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
18 changes: 11 additions & 7 deletions base/multidimensional.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1565,19 +1565,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)))
@propagate_inbounds isassigned(A::AbstractArray, i::CartesianIndex) = isassigned(A, Tuple(i)...)
@propagate_inbounds function isassigned(A::AbstractArray, i::Union{Integer, CartesianIndex}...)
jishnub marked this conversation as resolved.
Show resolved Hide resolved
return isassigned(A, CartesianIndex(to_indices(A, i)))
end
@inline function isassigned(A::AbstractArray, i::Integer...)
@boundscheck checkbounds(Bool, A, i...) || return false
# convert to valid indices, checking for Bool
inds = to_indices(A, i)
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
9 changes: 8 additions & 1 deletion test/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -548,12 +548,19 @@ 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))

mbauman marked this conversation as resolved.
Show resolved Hide resolved
# reshape(a::AbstractArray, dims::Dims)
@test_throws DimensionMismatch reshape(B, (0, 1))
Expand Down