From 843937b0ba36c0cd2ffe6a76835f1ae2b72116b9 Mon Sep 17 00:00:00 2001 From: Jishnu Bhattacharya Date: Thu, 10 Aug 2023 13:28:14 +0400 Subject: [PATCH] Disallow non-index Integer types in isassigned (#50594) Extend #50587 to more general `AbstractArray`s. This is mainly to disallow `Bool` as an index in `isassigned`, as this isn't supported by `getindex`. After this ```julia julia> isassigned(rand(2,2), 1, true) ERROR: ArgumentError: invalid index: true of type Bool ``` which matches the behavior on v1.9. (cherry picked from commit b9913973f84b80e9125e37a0312a44bb38239b10) --- base/multidimensional.jl | 2 +- test/abstractarray.jl | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/base/multidimensional.jl b/base/multidimensional.jl index ba4e6eb12695a..2b4bf54d88826 100644 --- a/base/multidimensional.jl +++ b/base/multidimensional.jl @@ -1564,7 +1564,7 @@ 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(i...)) + isa(i, Tuple{Vararg{Int}}) || return isassigned(A, CartesianIndex(to_indices(A, i))) @boundscheck checkbounds(Bool, A, i...) || return false S = IndexStyle(A) ninds = length(i) diff --git a/test/abstractarray.jl b/test/abstractarray.jl index 6e2294fec1f64..8a74f90f2d553 100644 --- a/test/abstractarray.jl +++ b/test/abstractarray.jl @@ -1844,3 +1844,9 @@ end # type stable [x;;] (https://github.com/JuliaLang/julia/issues/45952) f45952(x) = [x;;] @inferred f45952(1.0) + +@testset "isassigned with a Bool index" begin + A = zeros(2,2) + @test_throws "invalid index: true of type Bool" isassigned(A, 1, true) + @test_throws "invalid index: true of type Bool" isassigned(A, true) +end