Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Range indexing: error with scalar bool index like all other arrays #31829

Merged
merged 25 commits into from
Mar 8, 2021
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ New language features
Language changes
----------------

* Subtypes of `AbstractRange` now correctly follow the general array indexing behavior when indexed by `Bool`s, erroring for scalar `Bool`s and treating arrays (including ranges) of `Bool` as an logical index ([#31829])
bkamins marked this conversation as resolved.
Show resolved Hide resolved

Compiler/Runtime improvements
-----------------------------
Expand Down
103 changes: 103 additions & 0 deletions base/range.jl
Original file line number Diff line number Diff line change
Expand Up @@ -687,6 +687,9 @@ function getindex(v::UnitRange{T}, i::Integer) where T
val
end

getindex(v::UnitRange{T}, i::Bool) where T =
throw(ArgumentError("invalid index: $i of type Bool"))

const OverflowSafe = Union{Bool,Int8,Int16,Int32,Int64,Int128,
UInt8,UInt16,UInt32,UInt64,UInt128}

Expand All @@ -697,12 +700,18 @@ function getindex(v::UnitRange{T}, i::Integer) where {T<:OverflowSafe}
val % T
end

getindex(v::UnitRange{T}, i::Bool) where {T<:OverflowSafe} =
throw(ArgumentError("invalid index: $i of type Bool"))

function getindex(v::OneTo{T}, i::Integer) where T
@_inline_meta
@boundscheck ((i > 0) & (i <= v.stop)) || throw_boundserror(v, i)
convert(T, i)
end

getindex(v::OneTo{T}, i::Bool) where T =
throw(ArgumentError("invalid index: $i of type Bool"))

function getindex(v::AbstractRange{T}, i::Integer) where T
@_inline_meta
ret = convert(T, first(v) + (i - 1)*step_hp(v))
Expand All @@ -713,12 +722,18 @@ function getindex(v::AbstractRange{T}, i::Integer) where T
ret
end

getindex(v::AbstractRange{T}, i::Bool) where T =
throw(ArgumentError("invalid index: $i of type Bool"))

function getindex(r::Union{StepRangeLen,LinRange}, i::Integer)
@_inline_meta
@boundscheck checkbounds(r, i)
unsafe_getindex(r, i)
end

getindex(r::Union{StepRangeLen,LinRange}, i::Bool) =
throw(ArgumentError("invalid index: $i of type Bool"))

# This is separate to make it useful even when running with --check-bounds=yes
function unsafe_getindex(r::StepRangeLen{T}, i::Integer) where T
u = i - r.offset
Expand Down Expand Up @@ -750,26 +765,80 @@ function getindex(r::AbstractUnitRange, s::AbstractUnitRange{<:Integer})
range(st, length=length(s))
end

function getindex(r::AbstractUnitRange, s::AbstractUnitRange{Bool})
@_inline_meta
@boundscheck checkbounds(r, s)
if length(s) == 0
return r
elseif length(s) == 1
if s[1]
return r
else
return range(r[1], length=0)
end
else # length(s) == 2
return range(r[2], length=1)
end
end

function getindex(r::OneTo{T}, s::OneTo) where T
@_inline_meta
@boundscheck checkbounds(r, s)
OneTo(T(s.stop))
end

function getindex(r::OneTo{T}, s::OneTo{Bool}) where T
@_inline_meta
@boundscheck checkbounds(r, s)
return r
end

function getindex(r::AbstractUnitRange, s::StepRange{<:Integer})
@_inline_meta
@boundscheck checkbounds(r, s)
st = oftype(first(r), first(r) + s.start-1)
range(st, step=step(s), length=length(s))
end

function getindex(r::AbstractUnitRange, s::StepRange{Bool})
@_inline_meta
@boundscheck checkbounds(r, s)
if length(s) == 0
return range(r[1], step=one(eltype(r)), length=0)
elseif length(s) == 1
if s[1]
return range(r[1], step=one(eltype(r)), length=1)
else
return range(r[1], step=one(eltype(r)), length=0)
end
else # length(s) == 2
return range(r[2], step=one(eltype(r)), length=1)
end
end

function getindex(r::StepRange, s::AbstractRange{<:Integer})
@_inline_meta
@boundscheck checkbounds(r, s)
st = oftype(r.start, r.start + (first(s)-1)*step(r))
range(st, step=step(r)*step(s), length=length(s))
end

function getindex(r::StepRange, s::AbstractRange{Bool})
@_inline_meta
@boundscheck checkbounds(r, s)
if length(s) == 0
return range(r[1], step=step(r), length=0)
elseif length(s) == 1
if s[1]
return range(r[1], step=step(r), length=1)
else
return range(r[1], step=step(r), length=0)
end
else # length(s) == 2
return range(r[2], step=step(r), length=1)
end
end

function getindex(r::StepRangeLen{T}, s::OrdinalRange{<:Integer}) where {T}
@_inline_meta
@boundscheck checkbounds(r, s)
Expand All @@ -780,6 +849,23 @@ function getindex(r::StepRangeLen{T}, s::OrdinalRange{<:Integer}) where {T}
return StepRangeLen{T}(ref, r.step*step(s), length(s), offset)
end

function getindex(r::StepRangeLen{T}, s::OrdinalRange{Bool}) where {T}
@_inline_meta
@boundscheck checkbounds(r, s)

if length(s) == 0
return StepRangeLen{T}(r[1], step(r), 0, 1)
elseif length(s) == 1
if s[1]
return StepRangeLen{T}(r[1], step(r), 1, 1)
else
return StepRangeLen{T}(r[1], step(r), 0, 1)
end
else # length(s) == 2
return StepRangeLen{T}(r[2], step(r), 1, 1)
end
end

function getindex(r::LinRange{T}, s::OrdinalRange{<:Integer}) where {T}
@_inline_meta
@boundscheck checkbounds(r, s)
Expand All @@ -788,6 +874,23 @@ function getindex(r::LinRange{T}, s::OrdinalRange{<:Integer}) where {T}
return LinRange{T}(vfirst, vlast, length(s))
end

function getindex(r::LinRange, s::OrdinalRange{Bool})
@_inline_meta
@boundscheck checkbounds(r, s)

if length(s) == 0
return LinRange(r[1], r[1], 0)
elseif length(s) == 1
if s[1]
return LinRange(r[1], r[1], 1)
else
return LinRange(r[1], r[1], 0)
end
else # length(s) == 2
return LinRange(r[2], r[2], 1)
end
end

show(io::IO, r::AbstractRange) = print(io, repr(first(r)), ':', repr(step(r)), ':', repr(last(r)))
show(io::IO, r::UnitRange) = print(io, repr(first(r)), ':', repr(last(r)))
show(io::IO, r::OneTo) = print(io, "Base.OneTo(", r.stop, ")")
Expand Down
113 changes: 113 additions & 0 deletions test/ranges.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1730,3 +1730,116 @@ end
@test eltype(StepRangeLen(Int8(1), Int8(2), 3, 2)) === Int8
@test typeof(step(StepRangeLen(Int8(1), Int8(2), 3, 2))) === Int8
end

@testset "Bool indexing of ranges" begin
@test_throws ArgumentError (1:2)[true]
bkamins marked this conversation as resolved.
Show resolved Hide resolved
@test_throws ArgumentError (big(1):big(2))[true]
@test_throws ArgumentError Base.OneTo(10)[true]
@test_throws ArgumentError (1:2:5)[true]
@test_throws ArgumentError LinRange(1,2,2)[true]
@test_throws ArgumentError (1.0:2.0:5.0)[true]
r = 3:2
r2 = r[true:false]
@test r.start == r2.start && r.stop == r2.stop
@test_throws BoundsError r[true:true]
@test_throws BoundsError r[false:true]
r = 3:3
r2 = r[true:true]
@test r.start == r2.start && r.stop == r2.stop
r2 = r[false:false]
@test r2.start == 3 && r2.stop == 2
@test_throws BoundsError r[true:false]
@test_throws BoundsError r[false:true]
r = 2:3
r2 = r[false:true]
@test r2.start == r2.stop == 3
@test_throws BoundsError r[true:false]
@test_throws BoundsError r[true:true]
r = Base.OneTo(0)
r2 = r[Base.OneTo(false)]
@test r2 isa Base.OneTo && r2.stop == 0
@test_throws BoundsError r[Base.OneTo(true)]
bkamins marked this conversation as resolved.
Show resolved Hide resolved

r = Base.OneTo(1)
r2 = r[Base.OneTo(true)]
@test r2 isa Base.OneTo && r2.stop == 1
@test_throws BoundsError r[Base.OneTo(false)]

@test_throws BoundsError Base.OneTo(2)[Base.OneTo(false)]
@test_throws BoundsError Base.OneTo(2)[Base.OneTo(true)]
bkamins marked this conversation as resolved.
Show resolved Hide resolved
r = 2:1
r2 = r[true:true:false]
@test r2 isa StepRange && r2.start == 2 && r2.step == 1 && r2.stop == 1
@test_throws BoundsError r[false:true:false]

r = 2:2
r2 = r[false:true:false]
@test r2 isa StepRange && r2.start == 2 && r2.step == 1 && r2.stop == 1
r2 = r[true:true:true]
@test r2 isa StepRange && r2.start == 2 && r2.step == 1 && r2.stop == 2
@test_throws BoundsError r[true:true:false]
@test_throws BoundsError r[false:true:true]

r = 1:2
r2 = r[false:true:true]
@test r2 isa StepRange && r2.start == 2 && r2.step == 1 && r2.stop == 2
@test_throws BoundsError r[true:true:false]
@test_throws BoundsError r[true:true:true]

r = 2:1:1
r2 = r[true:true:false]
@test r2 isa StepRange && r2.start == 2 && r2.step == 1 && r2.stop == 1
@test_throws BoundsError r[false:true:false]

r = 2:1:2
r2 = r[false:true:false]
@test r2 isa StepRange && r2.start == 2 && r2.step == 1 && r2.stop == 1
r2 = r[true:true:true]
@test r2 isa StepRange && r2.start == 2 && r2.step == 1 && r2.stop == 2
@test_throws BoundsError r[true:true:false]
@test_throws BoundsError r[false:true:true]

r = 1:1:2
r2 = r[false:true:true]
@test r2 isa StepRange && r2.start == 2 && r2.step == 1 && r2.stop == 2
@test_throws BoundsError r[true:true:false]
@test_throws BoundsError r[true:true:true]

r = 2.0:1.0:1.0
r2 = r[true:true:false]
@test r2 isa StepRangeLen && r2 == 2:1
@test_throws BoundsError r[false:true:false]

r = 2.0:1.0:2.0
r2 = r[false:true:false]
@test r2 isa StepRangeLen && r2 == 2:1
r2 = r[true:true:true]
@test r2 isa StepRangeLen && r2 == 2:2
@test_throws BoundsError r[true:true:false]
@test_throws BoundsError r[false:true:true]

r = 1.0:1.0:2.0
r2 = r[false:true:true]
@test r2 isa StepRangeLen && r2 == 2:2
@test_throws BoundsError r[true:true:false]
@test_throws BoundsError r[true:true:true]

r = LinRange(2, 1, 0)
r2 = r[true:true:false]
@test r2 isa LinRange && r2 == 2:1
@test_throws BoundsError r[false:true:false]

r = LinRange(2, 2, 1)
r2 = r[false:true:false]
@test r2 isa LinRange && r2 == 2:1
r2 = r[true:true:true]
@test r2 isa LinRange && r2 == 2:2
@test_throws BoundsError r[true:true:false]
@test_throws BoundsError r[false:true:true]

r = LinRange(1, 2, 1)
r2 = r[false:true:true]
@test r2 isa LinRange && r2 == 2:2
@test_throws BoundsError r[true:true:false]
@test_throws BoundsError r[true:true:true]
end