Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 4 additions & 1 deletion base/range.jl
Original file line number Diff line number Diff line change
Expand Up @@ -905,7 +905,10 @@ end

## indexing

isassigned(r::AbstractRange, i::Int) = firstindex(r) <= i <= lastindex(r)
function isassigned(r::AbstractRange, i::Integer)
i isa Bool && throw(ArgumentError("invalid index: $i of type Bool"))
firstindex(r) <= i <= lastindex(r)
end

_in_unit_range(v::UnitRange, val, i::Integer) = i > 0 && val <= v.stop && val >= v.start

Expand Down
9 changes: 9 additions & 0 deletions test/ranges.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2498,3 +2498,12 @@ end
# a case that using mul_with_overflow & add_with_overflow might get wrong:
@test (-10:2:typemax(Int))[typemax(Int)÷2+2] == typemax(Int)-9
end

@testset "isassigned" begin
for (r, val) in ((1:3, 3), (1:big(2)^65, big(2)^65))
@test isassigned(r, lastindex(r))
# test that the indexing actually succeeds
@test r[end] == val
@test_throws ArgumentError isassigned(r, true)
end
end