Skip to content

Commit 40c6d1b

Browse files
giordanombauman
andauthored
Unify _checkbounds_array into checkbounds and use it in more places (#58785)
Ref: #58755 (comment). --------- Co-authored-by: Matt Bauman <mbauman@juliahub.com> Co-authored-by: Matt Bauman <mbauman@juliacomputing.com>
1 parent a595ea4 commit 40c6d1b

File tree

4 files changed

+11
-10
lines changed

4 files changed

+11
-10
lines changed

base/array.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -993,7 +993,7 @@ function setindex!(A::Array{T}, x, i::Int) where {T}
993993
end
994994
function _setindex!(A::Array{T}, x::T, i::Int) where {T}
995995
@_noub_if_noinbounds_meta
996-
@boundscheck (i - 1)%UInt < length(A)%UInt || throw_boundserror(A, (i,))
996+
@boundscheck checkbounds(Bool, A, i) || throw_boundserror(A, (i,))
997997
memoryrefset!(memoryrefnew(A.ref, i, false), x, :not_atomic, false)
998998
return A
999999
end

base/essentials.jl

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

380-
function _checkbounds_array(::Type{Bool}, A::Union{Array, GenericMemory}, i::Int)
380+
# These special checkbounds methods are defined early for bootstrapping
381+
function checkbounds(::Type{Bool}, A::Union{Array, Memory}, i::Int)
381382
@inline
382383
ult_int(bitcast(UInt, sub_int(i, 1)), bitcast(UInt, length(A)))
383384
end
384-
function _checkbounds_array(A::Union{Array, GenericMemory}, i::Int)
385+
function checkbounds(A::Union{Array, GenericMemory}, i::Int)
385386
@inline
386-
_checkbounds_array(Bool, A, i) || throw_boundserror(A, (i,))
387+
checkbounds(Bool, A, i) || throw_boundserror(A, (i,))
387388
end
388389

389390
default_access_order(a::GenericMemory{:not_atomic}) = :not_atomic
@@ -393,7 +394,7 @@ default_access_order(a::GenericMemoryRef{:atomic}) = :monotonic
393394

394395
function getindex(A::GenericMemory, i::Int)
395396
@_noub_if_noinbounds_meta
396-
(@_boundscheck) && _checkbounds_array(A, i)
397+
(@_boundscheck) && checkbounds(A, i)
397398
memoryrefget(memoryrefnew(memoryrefnew(A), i, false), default_access_order(A), false)
398399
end
399400

@@ -962,13 +963,13 @@ end
962963
# linear indexing
963964
function getindex(A::Array, i::Int)
964965
@_noub_if_noinbounds_meta
965-
@boundscheck _checkbounds_array(A, i)
966+
@boundscheck checkbounds(A, i)
966967
memoryrefget(memoryrefnew(getfield(A, :ref), i, false), :not_atomic, false)
967968
end
968969
# simple Array{Any} operations needed for bootstrap
969970
function setindex!(A::Array{Any}, @nospecialize(x), i::Int)
970971
@_noub_if_noinbounds_meta
971-
@boundscheck _checkbounds_array(A, i)
972+
@boundscheck checkbounds(A, i)
972973
memoryrefset!(memoryrefnew(getfield(A, :ref), i, false), x, :not_atomic, false)
973974
return A
974975
end

base/genericmemory.jl

Lines changed: 2 additions & 2 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 (i - 1)%UInt < length(a)%UInt || 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

@@ -227,7 +227,7 @@ Memory{T}(x::AbstractArray{S,1}) where {T,S} = copyto_axcheck!(Memory{T}(undef,
227227

228228
function _iterate_array(A::Union{Memory, Array}, i::Int)
229229
@inline
230-
(i - 1)%UInt < length(A)%UInt ? (A[i], i + 1) : nothing
230+
checkbounds(Bool, A, i) ? (A[i], i + 1) : nothing
231231
end
232232

233233
iterate(A::Memory, i=1) = (@inline; _iterate_array(A, i))

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) = (i % UInt) - 1 < length(s) ? (@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)