diff --git a/src/other/index.jl b/src/other/index.jl index 849ab3cfda..9927c256af 100644 --- a/src/other/index.jl +++ b/src/other/index.jl @@ -112,7 +112,10 @@ Base.getindex(x::Index, idx::Symbol) = x.lookup[idx] Base.getindex(x::AbstractIndex, idx::Real) = Int(idx) Base.getindex(x::AbstractIndex, idx::AbstractVector{Union{Bool, Missing}}) = getindex(x, collect(Missings.replace(idx, false))) -Base.getindex(x::AbstractIndex, idx::AbstractVector{Bool}) = find(idx) +function Base.getindex(x::AbstractIndex, idx::AbstractVector{Bool}) + length(x) == length(idx) || throw(BoundsError(x, idx)) + find(idx) +end Base.getindex(x::AbstractIndex, idx::AbstractVector{T}) where {T >: Missing} = getindex(x, collect(skipmissing(idx))) Base.getindex(x::AbstractIndex, idx::AbstractRange) = [idx;] diff --git a/test/index.jl b/test/index.jl index 9b8f33df5b..dcc76ec7ff 100644 --- a/test/index.jl +++ b/test/index.jl @@ -8,14 +8,13 @@ push!(i, :B) inds = Any[1, 1.0, :A, - [true], - trues(1), + [true, false], [1], [1.0], 1:1, 1.0:1.0, [:A], - Union{Bool, Missing}[true], + Union{Bool, Missing}[true, false], Union{Int, Missing}[1], Union{Float64, Missing}[1.0], Union{Symbol, Missing}[:A]] @@ -28,6 +27,9 @@ for ind in inds end end +@test_throws BoundsError i[[true]] +@test_throws BoundsError i[[true, false, true]] + @test names(i) == [:A,:B] @test names!(i, [:a,:a], allow_duplicates=true) == Index([:a,:a_1]) @test_throws ArgumentError names!(i, [:a,:a])