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

Fix conversion in getindex #26615

Merged
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
18 changes: 15 additions & 3 deletions base/range.jl
Original file line number Diff line number Diff line change
Expand Up @@ -478,11 +478,23 @@ end

## indexing

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, yes, I totally believe that this is faster — bounds checks are very friendly to branch prediction and when they miss, well, we really don't care about performance anymore.


function getindex(v::UnitRange{T}, i::Integer) where T
@_inline_meta
ret = convert(T, first(v) + i - 1)
@boundscheck ((i > 0) & (ret <= v.stop) & (ret >= v.start)) || throw_boundserror(v, i)
ret
val = convert(T, v.start + i - 1)
@boundscheck _in_unit_range(v, val, i) || throw_boundserror(v, i)
val
end

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

function getindex(v::UnitRange{T}, i::Integer) where {T<:OverflowSafe}
Copy link
Sponsor Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could just use Union{Bool, Base.BitInteger} for this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I was trying that, but it turns out BitInteger is defined a couple includes away in int.jl, and when I tried moving the constants to a separate file int_types.jl, I broke stuff without very useful feedback from make. It's worth defining these types a bit earlier, because other places redefine them as well!

@_inline_meta
val = v.start + i - 1
@boundscheck _in_unit_range(v, val, i) || throw_boundserror(v, i)
val % T
end

function getindex(v::OneTo{T}, i::Integer) where T
Expand Down
6 changes: 6 additions & 0 deletions test/ranges.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1286,6 +1286,12 @@ end
@test x isa StepRangeLen{Float64,Base.TwicePrecision{Float64},Base.TwicePrecision{Float64}}
end

@testset "Issue #26608" begin
@test_throws BoundsError (Int8(-100):Int8(100))[400]
@test_throws BoundsError (-100:100)[typemax(UInt)]
@test_throws BoundsError (false:true)[3]
end

module NonStandardIntegerRangeTest

using Test
Expand Down