Skip to content

Commit d270a71

Browse files
authored
isassigned for ranges with BigInt indices (#50587)
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.
1 parent 2cee483 commit d270a71

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

base/range.jl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -905,7 +905,10 @@ end
905905

906906
## indexing
907907

908-
isassigned(r::AbstractRange, i::Int) = firstindex(r) <= i <= lastindex(r)
908+
function isassigned(r::AbstractRange, i::Integer)
909+
i isa Bool && throw(ArgumentError("invalid index: $i of type Bool"))
910+
firstindex(r) <= i <= lastindex(r)
911+
end
909912

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

test/ranges.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2498,3 +2498,12 @@ end
24982498
# a case that using mul_with_overflow & add_with_overflow might get wrong:
24992499
@test (-10:2:typemax(Int))[typemax(Int)÷2+2] == typemax(Int)-9
25002500
end
2501+
2502+
@testset "isassigned" begin
2503+
for (r, val) in ((1:3, 3), (1:big(2)^65, big(2)^65))
2504+
@test isassigned(r, lastindex(r))
2505+
# test that the indexing actually succeeds
2506+
@test r[end] == val
2507+
@test_throws ArgumentError isassigned(r, true)
2508+
end
2509+
end

0 commit comments

Comments
 (0)