Skip to content

Commit 59821a1

Browse files
committed
Use checkbounds in more places
1 parent e2225ac commit 59821a1

File tree

5 files changed

+21
-26
lines changed

5 files changed

+21
-26
lines changed

base/abstractarray.jl

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,7 @@ function eachindex(A::AbstractArray, B::AbstractArray...)
387387
@inline
388388
eachindex(IndexStyle(A,B...), A, B...)
389389
end
390+
eachindex(::IndexLinear, A::Union{Array, Memory}) = unchecked_oneto(length(A))
390391
eachindex(::IndexLinear, A::AbstractArray) = (@inline; oneto(length(A)))
391392
eachindex(::IndexLinear, A::AbstractVector) = (@inline; axes1(A))
392393
function eachindex(::IndexLinear, A::AbstractArray, B::AbstractArray...)
@@ -749,7 +750,7 @@ false
749750
checkindex(::Type{Bool}, inds, i) = throw(ArgumentError(LazyString("unable to check bounds for indices of type ", typeof(i))))
750751
checkindex(::Type{Bool}, inds::AbstractUnitRange, i::Real) = (first(inds) <= i) & (i <= last(inds))
751752
checkindex(::Type{Bool}, inds::IdentityUnitRange, i::Real) = checkindex(Bool, inds.indices, i)
752-
checkindex(::Type{Bool}, inds::OneTo{T}, i::T) where {T<:BitInteger} = unsigned(i - one(i)) < unsigned(last(inds))
753+
checkindex(::Type{Bool}, inds::OneTo{T}, i::T) where {T<:BitInteger} = _checkbounds_array_onebased(Bool, last(inds), i)
753754
checkindex(::Type{Bool}, inds::AbstractUnitRange, ::Colon) = true
754755
checkindex(::Type{Bool}, inds::AbstractUnitRange, ::Slice) = true
755756
checkindex(::Type{Bool}, inds::AbstractUnitRange, i::AbstractRange) =
@@ -1237,15 +1238,15 @@ oneunit(x::AbstractMatrix{T}) where {T} = _one(oneunit(T), x)
12371238
iterate_starting_state(A) = iterate_starting_state(A, IndexStyle(A))
12381239
iterate_starting_state(A, ::IndexLinear) = firstindex(A)
12391240
iterate_starting_state(A, ::IndexStyle) = (eachindex(A),)
1240-
iterate(A::AbstractArray, state = iterate_starting_state(A)) = _iterate(A, state)
1241+
@inline iterate(A::AbstractArray, state = iterate_starting_state(A)) = _iterate(A, state)
12411242
function _iterate(A::AbstractArray, state::Tuple)
12421243
y = iterate(state...)
12431244
y === nothing && return nothing
12441245
A[y[1]], (state[1], tail(y)...)
12451246
end
12461247
function _iterate(A::AbstractArray, state::Integer)
12471248
checkbounds(Bool, A, state) || return nothing
1248-
@inbounds(A[state]), state + one(state)
1249+
A[state], state + one(state)
12491250
end
12501251

12511252
isempty(a::AbstractArray) = (length(a) == 0)

base/array.jl

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -902,10 +902,6 @@ function grow_to!(dest, itr, st)
902902
return dest
903903
end
904904

905-
## Iteration ##
906-
907-
iterate(A::Array, i=1) = (@inline; _iterate_array(A, i))
908-
909905
## Indexing: getindex ##
910906

911907
"""
@@ -993,7 +989,7 @@ function setindex!(A::Array{T}, x, i::Int) where {T}
993989
end
994990
function _setindex!(A::Array{T}, x::T, i::Int) where {T}
995991
@_noub_if_noinbounds_meta
996-
@boundscheck _checkbounds_array(Bool, A, i) || throw_boundserror(A, (i,))
992+
@boundscheck checkbounds(Bool, A, i) || throw_boundserror(A, (i,))
997993
memoryrefset!(memoryrefnew(A.ref, i, false), x, :not_atomic, false)
998994
return A
999995
end

base/essentials.jl

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -377,13 +377,20 @@ macro _nospecializeinfer_meta()
377377
return Expr(:meta, :nospecializeinfer)
378378
end
379379

380-
function _checkbounds_array(::Type{Bool}, A::Union{Array, GenericMemory}, i::Int)
380+
# NOTE: this function expects one-based indexing. We can't use `require_one_based_indexing`
381+
# here because it's defined later in the bootstrap process. Use only where you are really
382+
# sure about indexing.
383+
function _checkbounds_array_onebased(::Type{Bool}, len::Integer, i::Int)
381384
@inline
382-
ult_int(bitcast(UInt, sub_int(i, 1)), bitcast(UInt, length(A)))
385+
ult_int(bitcast(UInt, sub_int(i, 1)), bitcast(UInt, len))
383386
end
384-
function _checkbounds_array(A::Union{Array, GenericMemory}, i::Int)
387+
function _checkbounds_array_onebased(::Type{Bool}, A::AbstractArray, i::Int)
385388
@inline
386-
_checkbounds_array(Bool, A, i) || throw_boundserror(A, (i,))
389+
_checkbounds_array_onebased(Bool, length(A), i)
390+
end
391+
function _checkbounds_array_onebased(A::Union{Array, GenericMemory}, i::Int)
392+
@inline
393+
_checkbounds_array_onebased(Bool, A, i) || throw_boundserror(A, (i,))
387394
end
388395

389396
default_access_order(a::GenericMemory{:not_atomic}) = :not_atomic
@@ -393,7 +400,7 @@ default_access_order(a::GenericMemoryRef{:atomic}) = :monotonic
393400

394401
function getindex(A::GenericMemory, i::Int)
395402
@_noub_if_noinbounds_meta
396-
(@_boundscheck) && _checkbounds_array(A, i)
403+
(@_boundscheck) && _checkbounds_array_onebased(A, i)
397404
memoryrefget(memoryrefnew(memoryrefnew(A), i, false), default_access_order(A), false)
398405
end
399406

@@ -962,13 +969,13 @@ end
962969
# linear indexing
963970
function getindex(A::Array, i::Int)
964971
@_noub_if_noinbounds_meta
965-
@boundscheck _checkbounds_array(A, i)
972+
@boundscheck _checkbounds_array_onebased(A, i)
966973
memoryrefget(memoryrefnew(getfield(A, :ref), i, false), :not_atomic, false)
967974
end
968975
# simple Array{Any} operations needed for bootstrap
969976
function setindex!(A::Array{Any}, @nospecialize(x), i::Int)
970977
@_noub_if_noinbounds_meta
971-
@boundscheck _checkbounds_array(A, i)
978+
@boundscheck _checkbounds_array_onebased(A, i)
972979
memoryrefset!(memoryrefnew(getfield(A, :ref), i, false), x, :not_atomic, false)
973980
return A
974981
end

base/genericmemory.jl

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ sizeof(a::GenericMemory) = Core.sizeof(a)
106106
# multi arg case will be overwritten later. This is needed for bootstrapping
107107
function isassigned(a::GenericMemory, i::Int)
108108
@inline
109-
@boundscheck _checkbounds_array(Bool, a, i) || return false
109+
@boundscheck checkbounds(Bool, a, i) || return false
110110
return @inbounds memoryref_isassigned(memoryref(a, i), default_access_order(a), false)
111111
end
112112

@@ -223,15 +223,6 @@ Memory{T}(x::AbstractArray{S,1}) where {T,S} = copyto_axcheck!(Memory{T}(undef,
223223

224224
## copying iterators to containers
225225

226-
## Iteration ##
227-
228-
function _iterate_array(A::Union{Memory, Array}, i::Int)
229-
@inline
230-
_checkbounds_array(Bool, A, i) ? (A[i], i + 1) : nothing
231-
end
232-
233-
iterate(A::Memory, i=1) = (@inline; _iterate_array(A, i))
234-
235226
## Indexing: getindex ##
236227

237228
# Faster contiguous indexing using copyto! for AbstractUnitRange and Colon

base/strings/basic.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -797,7 +797,7 @@ size(s::CodeUnits) = (length(s),)
797797
elsize(s::Type{<:CodeUnits{T}}) where {T} = sizeof(T)
798798
@propagate_inbounds getindex(s::CodeUnits, i::Int) = codeunit(s.s, i)
799799
IndexStyle(::Type{<:CodeUnits}) = IndexLinear()
800-
@inline iterate(s::CodeUnits, i=1) = _checkbounds_array(Bool, s, i) ? (@inbounds s[i], i + 1) : nothing
800+
@inline iterate(s::CodeUnits, i=1) = checkbounds(Bool, s, i) ? (@inbounds s[i], i + 1) : nothing
801801

802802

803803
write(io::IO, s::CodeUnits) = write(io, s.s)

0 commit comments

Comments
 (0)