diff --git a/base/multidimensional.jl b/base/multidimensional.jl index 4b462c8e2079b..072493f0264fd 100644 --- a/base/multidimensional.jl +++ b/base/multidimensional.jl @@ -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))) - @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) diff --git a/test/abstractarray.jl b/test/abstractarray.jl index c3409a4a059fb..06fc146ecfc2d 100644 --- a/test/abstractarray.jl +++ b/test/abstractarray.jl @@ -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))