Skip to content

Commit edd2679

Browse files
jishnubmbauman
authored andcommitted
Propagate inbounds in isassigned with CartesianIndex indices (JuliaLang#53305)
Close JuliaLang#53302 --------- Co-authored-by: Matt Bauman <mbauman@juliahub.com>
1 parent ee86cca commit edd2679

File tree

2 files changed

+25
-9
lines changed

2 files changed

+25
-9
lines changed

base/multidimensional.jl

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1582,19 +1582,23 @@ end
15821582
end
15831583
end
15841584

1585-
isassigned(a::AbstractArray, i::CartesianIndex) = isassigned(a, Tuple(i)...)
1586-
function isassigned(A::AbstractArray, i::Union{Integer, CartesianIndex}...)
1587-
isa(i, Tuple{Vararg{Int}}) || return isassigned(A, CartesianIndex(to_indices(A, i)))
1588-
@boundscheck checkbounds(Bool, A, i...) || return false
1585+
@propagate_inbounds isassigned(A::AbstractArray, i::CartesianIndex) = isassigned(A, Tuple(i)...)
1586+
@propagate_inbounds function isassigned(A::AbstractArray, i::Union{Integer, CartesianIndex}...)
1587+
return isassigned(A, CartesianIndex(to_indices(A, i)))
1588+
end
1589+
@inline function isassigned(A::AbstractArray, i::Integer...)
1590+
# convert to valid indices, checking for Bool
1591+
inds = to_indices(A, i)
1592+
@boundscheck checkbounds(Bool, A, inds...) || return false
15891593
S = IndexStyle(A)
1590-
ninds = length(i)
1594+
ninds = length(inds)
15911595
if (isa(S, IndexLinear) && ninds != 1)
1592-
return @inbounds isassigned(A, _to_linear_index(A, i...))
1596+
return @inbounds isassigned(A, _to_linear_index(A, inds...))
15931597
elseif (!isa(S, IndexLinear) && ninds != ndims(A))
1594-
return @inbounds isassigned(A, _to_subscript_indices(A, i...)...)
1598+
return @inbounds isassigned(A, _to_subscript_indices(A, inds...)...)
15951599
else
15961600
try
1597-
A[i...]
1601+
A[inds...]
15981602
true
15991603
catch e
16001604
if isa(e, BoundsError) || isa(e, UndefRefError)

test/abstractarray.jl

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -548,12 +548,24 @@ function test_primitives(::Type{T}, shape, ::Type{TestAbstractArray}) where T
548548
@test firstindex(B, 1) == firstindex(A, 1) == first(axes(B, 1))
549549
@test firstindex(B, 2) == firstindex(A, 2) == first(axes(B, 2))
550550

551-
# isassigned(a::AbstractArray, i::Int...)
551+
@test !isassigned(B)
552+
# isassigned(a::AbstractArray, i::Integer...)
552553
j = rand(1:length(B))
553554
@test isassigned(B, j)
554555
if T == T24Linear
555556
@test !isassigned(B, length(B) + 1)
556557
end
558+
# isassigned(a::AbstractArray, i::CartesianIndex)
559+
@test isassigned(B, first(CartesianIndices(B)))
560+
ind = last(CartesianIndices(B))
561+
@test !isassigned(B, ind + oneunit(ind))
562+
# isassigned(a::AbstractArray, i::Union{Integer,CartesianIndex}...)
563+
@test isassigned(B, Int16.(first.(axes(B)))..., CartesianIndex(1,1))
564+
# Bool isn't a valid index
565+
@test_throws ArgumentError isassigned(B, Bool.(first.(axes(B)))..., CartesianIndex(1,1))
566+
@test_throws ArgumentError isassigned(B, Bool.(first.(axes(B)))...)
567+
@test_throws ArgumentError isassigned(B, true)
568+
@test_throws ArgumentError isassigned(B, false)
557569

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

0 commit comments

Comments
 (0)