Skip to content

Commit

Permalink
Check bounds for indexing tuples by logical masks (#19737)
Browse files Browse the repository at this point in the history
* Check bounds for indexing tuples by logical masks

Also deprecate indexing tuples by non-vectors since that should increase the
dimensionality of the tuple, but tuples are 1-d only structures.

Fix #19719

* Don't use the AbstractVector typealias
  • Loading branch information
mbauman authored and tkelman committed Dec 29, 2016
1 parent e73c556 commit d7b6ac3
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
4 changes: 4 additions & 0 deletions base/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1146,6 +1146,10 @@ for (dep, f, op) in [(:sumabs!, :sum!, :abs),
end
end

# #19719
@deprecate getindex(t::Tuple, r::AbstractArray) getindex(t, vec(r))
@deprecate getindex(t::Tuple, b::AbstractArray{Bool}) getindex(t, vec(b))

@deprecate airy(z::Number) airyai(z)
@deprecate airyx(z::Number) airyaix(z)
@deprecate airyprime(z::Number) airyaiprime(z)
Expand Down
4 changes: 2 additions & 2 deletions base/tuple.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ endof(t::Tuple) = length(t)
size(t::Tuple, d) = d==1 ? length(t) : throw(ArgumentError("invalid tuple dimension $d"))
getindex(t::Tuple, i::Int) = getfield(t, i)
getindex(t::Tuple, i::Real) = getfield(t, convert(Int, i))
getindex(t::Tuple, r::AbstractArray) = tuple([t[ri] for ri in r]...)
getindex(t::Tuple, b::AbstractArray{Bool}) = getindex(t,find(b))
getindex{T}(t::Tuple, r::AbstractArray{T,1}) = tuple([t[ri] for ri in r]...)
getindex(t::Tuple, b::AbstractArray{Bool,1}) = length(b) == length(t) ? getindex(t,find(b)) : throw(BoundsError(t, b))

## iterating ##

Expand Down
8 changes: 8 additions & 0 deletions test/tuple.jl
Original file line number Diff line number Diff line change
Expand Up @@ -170,3 +170,11 @@ for n = 0:20
@test t[i] == i
end
end

# issue #19719
@test_throws BoundsError (1,2,3)[falses(4)]
@test_throws BoundsError (1,2,3)[[false,false,true,true]]
@test_throws BoundsError (1,2,3)[trues(2)]
@test_throws BoundsError (1,2,3)[falses(2)]
@test_throws BoundsError ()[[false]]
@test_throws BoundsError ()[[true]]

0 comments on commit d7b6ac3

Please sign in to comment.