Skip to content

Commit

Permalink
isassigned for ranges with BigInt indices (#50587)
Browse files Browse the repository at this point in the history
This fixes certain possible regressions in `isassigned` for ranges to
restore the 1.9-like behavior.
On v1.9
```julia
julia> r = 1:big(2)^65
1:36893488147419103232

julia> isassigned(r, lastindex(r))
true

julia> isassigned(r, true)
ERROR: ArgumentError: invalid index: true of type Bool
```
On v1.10.0-alpha1
```julia
julia> isassigned(r, lastindex(r))
ERROR: InexactError: Int64(36893488147419103232)

julia> isassigned(r, true) # Bool is converted to Int
true

julia> r[true] # but indexing with Bool doesn't work
ERROR: ArgumentError: invalid index: true of type Bool
```
This PR
```julia
julia> isassigned(r, lastindex(r))
true

julia> isassigned(r, true)
ERROR: ArgumentError: invalid index: true of type Bool
```
This still leaves
```julia
julia> isassigned(collect(1:3), true)
true
```
so that should perhaps be changed as well.

(cherry picked from commit d270a71)
  • Loading branch information
jishnub authored and KristofferC committed Jul 24, 2023
1 parent def06fe commit f8b0f29
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
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 @@ -2490,3 +2490,12 @@ function check_ranges(rx, ry)
end
@test Core.Compiler.is_foldable(Base.infer_effects(check_ranges, (UnitRange{Int},UnitRange{Int})))
# TODO JET.@test_opt check_ranges(1:2, 3:4)

@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

0 comments on commit f8b0f29

Please sign in to comment.