diff --git a/base/array.jl b/base/array.jl index 5c9b37f887ea8..61a3c39ca2bb7 100644 --- a/base/array.jl +++ b/base/array.jl @@ -993,7 +993,7 @@ function setindex!(A::Array{T}, x, i::Int) where {T} end function _setindex!(A::Array{T}, x::T, i::Int) where {T} @_noub_if_noinbounds_meta - @boundscheck (i - 1)%UInt < length(A)%UInt || throw_boundserror(A, (i,)) + @boundscheck checkbounds(Bool, A, i) || throw_boundserror(A, (i,)) memoryrefset!(memoryrefnew(A.ref, i, false), x, :not_atomic, false) return A end diff --git a/base/essentials.jl b/base/essentials.jl index 820cce6839f13..e1bc648e7dbd0 100644 --- a/base/essentials.jl +++ b/base/essentials.jl @@ -377,13 +377,14 @@ macro _nospecializeinfer_meta() return Expr(:meta, :nospecializeinfer) end -function _checkbounds_array(::Type{Bool}, A::Union{Array, GenericMemory}, i::Int) +# These special checkbounds methods are defined early for bootstrapping +function checkbounds(::Type{Bool}, A::Union{Array, Memory}, i::Int) @inline ult_int(bitcast(UInt, sub_int(i, 1)), bitcast(UInt, length(A))) end -function _checkbounds_array(A::Union{Array, GenericMemory}, i::Int) +function checkbounds(A::Union{Array, GenericMemory}, i::Int) @inline - _checkbounds_array(Bool, A, i) || throw_boundserror(A, (i,)) + checkbounds(Bool, A, i) || throw_boundserror(A, (i,)) end default_access_order(a::GenericMemory{:not_atomic}) = :not_atomic @@ -393,7 +394,7 @@ default_access_order(a::GenericMemoryRef{:atomic}) = :monotonic function getindex(A::GenericMemory, i::Int) @_noub_if_noinbounds_meta - (@_boundscheck) && _checkbounds_array(A, i) + (@_boundscheck) && checkbounds(A, i) memoryrefget(memoryrefnew(memoryrefnew(A), i, false), default_access_order(A), false) end @@ -962,13 +963,13 @@ end # linear indexing function getindex(A::Array, i::Int) @_noub_if_noinbounds_meta - @boundscheck _checkbounds_array(A, i) + @boundscheck checkbounds(A, i) memoryrefget(memoryrefnew(getfield(A, :ref), i, false), :not_atomic, false) end # simple Array{Any} operations needed for bootstrap function setindex!(A::Array{Any}, @nospecialize(x), i::Int) @_noub_if_noinbounds_meta - @boundscheck _checkbounds_array(A, i) + @boundscheck checkbounds(A, i) memoryrefset!(memoryrefnew(getfield(A, :ref), i, false), x, :not_atomic, false) return A end diff --git a/base/genericmemory.jl b/base/genericmemory.jl index 1b45ba23bcded..d25d213a3546e 100644 --- a/base/genericmemory.jl +++ b/base/genericmemory.jl @@ -106,7 +106,7 @@ sizeof(a::GenericMemory) = Core.sizeof(a) # multi arg case will be overwritten later. This is needed for bootstrapping function isassigned(a::GenericMemory, i::Int) @inline - @boundscheck (i - 1)%UInt < length(a)%UInt || return false + @boundscheck checkbounds(Bool, a, i) || return false return @inbounds memoryref_isassigned(memoryref(a, i), default_access_order(a), false) end @@ -227,7 +227,7 @@ Memory{T}(x::AbstractArray{S,1}) where {T,S} = copyto_axcheck!(Memory{T}(undef, function _iterate_array(A::Union{Memory, Array}, i::Int) @inline - (i - 1)%UInt < length(A)%UInt ? (A[i], i + 1) : nothing + checkbounds(Bool, A, i) ? (A[i], i + 1) : nothing end iterate(A::Memory, i=1) = (@inline; _iterate_array(A, i)) diff --git a/base/strings/basic.jl b/base/strings/basic.jl index 314903898b92a..6619a2b25574e 100644 --- a/base/strings/basic.jl +++ b/base/strings/basic.jl @@ -797,7 +797,7 @@ size(s::CodeUnits) = (length(s),) elsize(s::Type{<:CodeUnits{T}}) where {T} = sizeof(T) @propagate_inbounds getindex(s::CodeUnits, i::Int) = codeunit(s.s, i) IndexStyle(::Type{<:CodeUnits}) = IndexLinear() -@inline iterate(s::CodeUnits, i=1) = (i % UInt) - 1 < length(s) ? (@inbounds s[i], i + 1) : nothing +@inline iterate(s::CodeUnits, i=1) = checkbounds(Bool, s, i) ? (@inbounds s[i], i + 1) : nothing write(io::IO, s::CodeUnits) = write(io, s.s)