Skip to content

Commit 1da48c2

Browse files
Aaron Lowtimholy
Aaron Low
authored andcommitted
Audit !has_offset_axes(...) checks in Base. (#30610) (#30630)
1 parent 4be9339 commit 1da48c2

9 files changed

+25
-23
lines changed

base/abstractarray.jl

+2
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ has_offset_axes(A) = _tuple_any(x->first(x)!=1, axes(A))
8686
has_offset_axes(A...) = _tuple_any(has_offset_axes, A)
8787
has_offset_axes(::Colon) = false
8888

89+
require_one_based_indexing(A...) = !has_offset_axes(A...) || throw(ArgumentError("offset arrays are not supported but got an array with index other than 1"))
90+
8991
# Performance optimization: get rid of a branch on `d` in `axes(A, d)`
9092
# for d=1. 1d arrays are heavily used, and the first dimension comes up
9193
# in other applications.

base/array.jl

+4-4
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,7 @@ for (fname, felt) in ((:zeros, :zero), (:ones, :one))
455455
end
456456

457457
function _one(unit::T, x::AbstractMatrix) where T
458-
@assert !has_offset_axes(x)
458+
require_one_based_indexing(x)
459459
m,n = size(x)
460460
m==n || throw(DimensionMismatch("multiplicative identity defined only for square matrices"))
461461
# Matrix{T}(I, m, m)
@@ -771,7 +771,7 @@ function setindex! end
771771
function setindex!(A::Array, X::AbstractArray, I::AbstractVector{Int})
772772
@_propagate_inbounds_meta
773773
@boundscheck setindex_shape_check(X, length(I))
774-
@assert !has_offset_axes(X)
774+
require_one_based_indexing(X)
775775
X′ = unalias(A, X)
776776
I′ = unalias(A, I)
777777
count = 1
@@ -900,7 +900,7 @@ append!(a::Vector, iter) = _append!(a, IteratorSize(iter), iter)
900900
push!(a::Vector, iter...) = append!(a, iter)
901901

902902
function _append!(a, ::Union{HasLength,HasShape}, iter)
903-
@assert !has_offset_axes(a)
903+
require_one_based_indexing(a)
904904
n = length(a)
905905
resize!(a, n+length(iter))
906906
@inbounds for (i,item) in zip(n+1:length(a), iter)
@@ -948,7 +948,7 @@ prepend!(a::Vector, iter) = _prepend!(a, IteratorSize(iter), iter)
948948
pushfirst!(a::Vector, iter...) = prepend!(a, iter)
949949

950950
function _prepend!(a, ::Union{HasLength,HasShape}, iter)
951-
@assert !has_offset_axes(a)
951+
require_one_based_indexing(a)
952952
n = length(iter)
953953
_growbeg!(a, n)
954954
i = 0

base/c.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ transcode(T, src::String) = transcode(T, codeunits(src))
292292
transcode(::Type{String}, src) = String(transcode(UInt8, src))
293293

294294
function transcode(::Type{UInt16}, src::AbstractVector{UInt8})
295-
@assert !has_offset_axes(src)
295+
require_one_based_indexing(src)
296296
dst = UInt16[]
297297
i, n = 1, length(src)
298298
n > 0 || return dst
@@ -343,7 +343,7 @@ function transcode(::Type{UInt16}, src::AbstractVector{UInt8})
343343
end
344344

345345
function transcode(::Type{UInt8}, src::AbstractVector{UInt16})
346-
@assert !has_offset_axes(src)
346+
require_one_based_indexing(src)
347347
n = length(src)
348348
n == 0 && return UInt8[]
349349

base/combinatorics.jl

+3-3
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ isperm(p::Tuple{Int}) = p[1] == 1
6464
isperm(p::Tuple{Int,Int}) = ((p[1] == 1) & (p[2] == 2)) | ((p[1] == 2) & (p[2] == 1))
6565

6666
function permute!!(a, p::AbstractVector{<:Integer})
67-
@assert !has_offset_axes(a, p)
67+
require_one_based_indexing(a, p)
6868
count = 0
6969
start = 0
7070
while count < length(a)
@@ -115,7 +115,7 @@ julia> A
115115
permute!(a, p::AbstractVector) = permute!!(a, copymutable(p))
116116

117117
function invpermute!!(a, p::AbstractVector{<:Integer})
118-
@assert !has_offset_axes(a, p)
118+
require_one_based_indexing(a, p)
119119
count = 0
120120
start = 0
121121
while count < length(a)
@@ -196,7 +196,7 @@ julia> B[invperm(v)]
196196
```
197197
"""
198198
function invperm(a::AbstractVector)
199-
@assert !has_offset_axes(a)
199+
require_one_based_indexing(a)
200200
b = zero(a) # similar vector of zeros
201201
n = length(a)
202202
@inbounds for (i, j) in enumerate(a)

base/io.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -798,7 +798,7 @@ The size of `b` will be increased if needed (i.e. if `nb` is greater than `lengt
798798
and enough bytes could be read), but it will never be decreased.
799799
"""
800800
function readbytes!(s::IO, b::AbstractArray{UInt8}, nb=length(b))
801-
@assert !has_offset_axes(b)
801+
require_one_based_indexing(b)
802802
olb = lb = length(b)
803803
nr = 0
804804
while nr < nb && !eof(s)

base/iobuffer.jl

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ mutable struct GenericIOBuffer{T<:AbstractVector{UInt8}} <: IO
1616

1717
function GenericIOBuffer{T}(data::T, readable::Bool, writable::Bool, seekable::Bool, append::Bool,
1818
maxsize::Integer) where T<:AbstractVector{UInt8}
19-
@assert !has_offset_axes(data)
19+
require_one_based_indexing(data)
2020
new(data,readable,writable,seekable,append,length(data),maxsize,1,-1)
2121
end
2222
end
@@ -183,7 +183,7 @@ function read(from::GenericIOBuffer, T::Union{Type{Int16},Type{UInt16},Type{Int3
183183
end
184184

185185
function read_sub(from::GenericIOBuffer, a::AbstractArray{T}, offs, nel) where T
186-
@assert !has_offset_axes(a)
186+
require_one_based_indexing(a)
187187
from.readable || throw(ArgumentError("read failed, IOBuffer is not readable"))
188188
if offs+nel-1 > length(a) || offs < 1 || nel < 0
189189
throw(BoundsError())
@@ -425,7 +425,7 @@ function unsafe_write(to::GenericIOBuffer, p::Ptr{UInt8}, nb::UInt)
425425
end
426426

427427
function write_sub(to::GenericIOBuffer, a::AbstractArray{UInt8}, offs, nel)
428-
@assert !has_offset_axes(a)
428+
require_one_based_indexing(a)
429429
if offs+nel-1 > length(a) || offs < 1 || nel < 0
430430
throw(BoundsError())
431431
end

base/multidimensional.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -738,7 +738,7 @@ julia> diff(vec(a))
738738
```
739739
"""
740740
function diff(a::AbstractArray{T,N}; dims::Integer) where {T,N}
741-
has_offset_axes(a) && throw(ArgumentError("offset axes unsupported"))
741+
require_one_based_indexing(a)
742742
1 <= dims <= N || throw(ArgumentError("dimension $dims out of range (1:$N)"))
743743

744744
r = axes(a)
@@ -1526,7 +1526,7 @@ function _extrema_dims(f, A::AbstractArray, dims)
15261526
end
15271527

15281528
@noinline function extrema!(f, B, A)
1529-
@assert !has_offset_axes(B, A)
1529+
require_one_based_indexing(B, A)
15301530
sA = size(A)
15311531
sB = size(B)
15321532
for I in CartesianIndices(sB)

base/reshapedarray.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ _reshape(parent::Array, dims::Dims) = reshape(parent, dims)
164164

165165
# When reshaping Vector->Vector, don't wrap with a ReshapedArray
166166
function _reshape(v::AbstractVector, dims::Dims{1})
167-
@assert !has_offset_axes(v)
167+
require_one_based_indexing(v)
168168
len = dims[1]
169169
len == length(v) || _throw_dmrs(length(v), "length", len)
170170
v

base/sort.jl

+7-7
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ using .Base: copymutable, LinearIndices, length, (:),
1010
AbstractVector, @inbounds, AbstractRange, @eval, @inline, Vector, @noinline,
1111
AbstractMatrix, AbstractUnitRange, isless, identity, eltype, >, <, <=, >=, |, +, -, *, !,
1212
extrema, sub_with_overflow, add_with_overflow, oneunit, div, getindex, setindex!,
13-
length, resize!, fill, Missing, has_offset_axes
13+
length, resize!, fill, Missing, require_one_based_indexing
1414

1515
using .Base: >>>, !==
1616

@@ -222,7 +222,7 @@ function searchsorted(v::AbstractVector, x, ilo::Int, ihi::Int, o::Ordering)
222222
end
223223

224224
function searchsortedlast(a::AbstractRange{<:Real}, x::Real, o::DirectOrdering)
225-
has_offset_axes(a) && throw(ArgumentError("range must be indexed starting with 1"))
225+
require_one_based_indexing(a)
226226
if step(a) == 0
227227
lt(o, x, first(a)) ? 0 : length(a)
228228
else
@@ -232,7 +232,7 @@ function searchsortedlast(a::AbstractRange{<:Real}, x::Real, o::DirectOrdering)
232232
end
233233

234234
function searchsortedfirst(a::AbstractRange{<:Real}, x::Real, o::DirectOrdering)
235-
has_offset_axes(a) && throw(ArgumentError("range must be indexed starting with 1"))
235+
require_one_based_indexing(a)
236236
if step(a) == 0
237237
lt(o, first(a), x) ? length(a) + 1 : 1
238238
else
@@ -242,7 +242,7 @@ function searchsortedfirst(a::AbstractRange{<:Real}, x::Real, o::DirectOrdering)
242242
end
243243

244244
function searchsortedlast(a::AbstractRange{<:Integer}, x::Real, o::DirectOrdering)
245-
has_offset_axes(a) && throw(ArgumentError("range must be indexed starting with 1"))
245+
require_one_based_indexing(a)
246246
if step(a) == 0
247247
lt(o, x, first(a)) ? 0 : length(a)
248248
else
@@ -251,7 +251,7 @@ function searchsortedlast(a::AbstractRange{<:Integer}, x::Real, o::DirectOrderin
251251
end
252252

253253
function searchsortedfirst(a::AbstractRange{<:Integer}, x::Real, o::DirectOrdering)
254-
has_offset_axes(a) && throw(ArgumentError("range must be indexed starting with 1"))
254+
require_one_based_indexing(a)
255255
if step(a) == 0
256256
lt(o, first(a), x) ? length(a)+1 : 1
257257
else
@@ -260,7 +260,7 @@ function searchsortedfirst(a::AbstractRange{<:Integer}, x::Real, o::DirectOrderi
260260
end
261261

262262
function searchsortedfirst(a::AbstractRange{<:Integer}, x::Unsigned, o::DirectOrdering)
263-
has_offset_axes(a) && throw(ArgumentError("range must be indexed starting with 1"))
263+
require_one_based_indexing(a)
264264
if lt(o, first(a), x)
265265
if step(a) == 0
266266
length(a) + 1
@@ -273,7 +273,7 @@ function searchsortedfirst(a::AbstractRange{<:Integer}, x::Unsigned, o::DirectOr
273273
end
274274

275275
function searchsortedlast(a::AbstractRange{<:Integer}, x::Unsigned, o::DirectOrdering)
276-
has_offset_axes(a) && throw(ArgumentError("range must be indexed starting with 1"))
276+
require_one_based_indexing(a)
277277
if lt(o, x, first(a))
278278
0
279279
elseif step(a) == 0

0 commit comments

Comments
 (0)