Skip to content

Commit e2d647e

Browse files
authored
cat: remove incorrect inbounds/inline annotations (#41062)
There are no boundschecks so these lead to problems such as #41047.
1 parent d5f1dca commit e2d647e

File tree

3 files changed

+33
-31
lines changed

3 files changed

+33
-31
lines changed

base/abstractarray.jl

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1529,10 +1529,10 @@ vcat(X::T...) where {T<:Number} = T[ X[i] for i=1:length(X) ]
15291529
hcat(X::T...) where {T} = T[ X[j] for i=1:1, j=1:length(X) ]
15301530
hcat(X::T...) where {T<:Number} = T[ X[j] for i=1:1, j=1:length(X) ]
15311531

1532-
vcat(X::Number...) = hvcat_fill(Vector{promote_typeof(X...)}(undef, length(X)), X)
1533-
hcat(X::Number...) = hvcat_fill(Matrix{promote_typeof(X...)}(undef, 1,length(X)), X)
1534-
typed_vcat(::Type{T}, X::Number...) where {T} = hvcat_fill(Vector{T}(undef, length(X)), X)
1535-
typed_hcat(::Type{T}, X::Number...) where {T} = hvcat_fill(Matrix{T}(undef, 1,length(X)), X)
1532+
vcat(X::Number...) = hvcat_fill!(Vector{promote_typeof(X...)}(undef, length(X)), X)
1533+
hcat(X::Number...) = hvcat_fill!(Matrix{promote_typeof(X...)}(undef, 1,length(X)), X)
1534+
typed_vcat(::Type{T}, X::Number...) where {T} = hvcat_fill!(Vector{T}(undef, length(X)), X)
1535+
typed_hcat(::Type{T}, X::Number...) where {T} = hvcat_fill!(Matrix{T}(undef, 1,length(X)), X)
15361536

15371537
vcat(V::AbstractVector...) = typed_vcat(promote_eltype(V...), V...)
15381538
vcat(V::AbstractVector{T}...) where {T} = typed_vcat(T, V...)
@@ -1993,9 +1993,13 @@ function hvcat(rows::Tuple{Vararg{Int}}, xs::T...) where T<:Number
19931993
a
19941994
end
19951995

1996-
function hvcat_fill(a::Array, xs::Tuple)
1997-
k = 1
1996+
function hvcat_fill!(a::Array, xs::Tuple)
19981997
nr, nc = size(a,1), size(a,2)
1998+
len = length(xs)
1999+
if nr*nc != len
2000+
throw(ArgumentError("argument count $(len) does not match specified shape $((nr,nc))"))
2001+
end
2002+
k = 1
19992003
for i=1:nr
20002004
@inbounds for j=1:nc
20012005
a[i,j] = xs[k]
@@ -2016,11 +2020,7 @@ function typed_hvcat(::Type{T}, rows::Tuple{Vararg{Int}}, xs::Number...) where T
20162020
throw(ArgumentError("row $(i) has mismatched number of columns (expected $nc, got $(rows[i]))"))
20172021
end
20182022
end
2019-
len = length(xs)
2020-
if nr*nc != len
2021-
throw(ArgumentError("argument count $(len) does not match specified shape $((nr,nc))"))
2022-
end
2023-
hvcat_fill(Matrix{T}(undef, nr, nc), xs)
2023+
hvcat_fill!(Matrix{T}(undef, nr, nc), xs)
20242024
end
20252025

20262026
function typed_hvcat(::Type{T}, rows::Tuple{Vararg{Int}}, as...) where T
@@ -2136,9 +2136,9 @@ function hvncat_fill!(A::Array, row_first::Bool, xs::Tuple)
21362136
if row_first
21372137
nr, nc = size(A, 1), size(A, 2)
21382138
nrc = nr * nc
2139-
@inbounds na = prod(size(A)[3:end])
2139+
na = prod(size(A)[3:end])
21402140
k = 1
2141-
@inbounds for d 1:na
2141+
for d 1:na
21422142
dd = nrc * (d - 1)
21432143
for i 1:nr
21442144
Ai = dd + i
@@ -2150,7 +2150,7 @@ function hvncat_fill!(A::Array, row_first::Bool, xs::Tuple)
21502150
end
21512151
end
21522152
else
2153-
@inbounds for k eachindex(xs)
2153+
for k eachindex(xs)
21542154
A[k] = xs[k]
21552155
end
21562156
end
@@ -2162,24 +2162,24 @@ _typed_hvncat(T::Type, ::Val{N}, xs::Number...) where N = _typed_hvncat(T, (ntup
21622162
function _typed_hvncat(::Type{T}, ::Val{N}, as::AbstractArray...) where {T, N}
21632163
# optimization for arrays that can be concatenated by copying them linearly into the destination
21642164
# conditions: the elements must all have 1- or 0-length dimensions above N
2165-
@inbounds for a as
2165+
for a as
21662166
ndims(a) <= N || all(x -> size(a, x) == 1, (N + 1):ndims(a)) ||
21672167
return _typed_hvncat(T, (ntuple(x -> 1, N - 1)..., length(as)), false, as...)
21682168
end
21692169

21702170
nd = max(N, ndims(as[1]))
21712171

21722172
Ndim = 0
2173-
@inbounds for i 1:lastindex(as)
2173+
for i 1:lastindex(as)
21742174
Ndim += cat_size(as[i], N)
21752175
for d 1:N - 1
21762176
cat_size(as[1], d) == cat_size(as[i], d) || throw(ArgumentError("mismatched size along axis $d in element $i"))
21772177
end
21782178
end
21792179

2180-
@inbounds A = Array{T, nd}(undef, ntuple(d -> cat_size(as[1], d), N - 1)..., Ndim, ntuple(x -> 1, nd - N)...)
2180+
A = Array{T, nd}(undef, ntuple(d -> cat_size(as[1], d), N - 1)..., Ndim, ntuple(x -> 1, nd - N)...)
21812181
k = 1
2182-
@inbounds for a as
2182+
for a as
21832183
for i eachindex(a)
21842184
A[k] = a[i]
21852185
k += 1
@@ -2196,7 +2196,7 @@ function _typed_hvncat(::Type{T}, ::Val{N}, as...) where {T, N}
21962196
# into the destination
21972197
nd = N
21982198
Ndim = 0
2199-
@inbounds for a as
2199+
for a as
22002200
if a isa AbstractArray
22012201
cat_size(a, N) == length(a) ||
22022202
throw(ArgumentError("all dimensions of elements other than $N must be of length 1"))
@@ -2205,10 +2205,10 @@ function _typed_hvncat(::Type{T}, ::Val{N}, as...) where {T, N}
22052205
Ndim += cat_size(a, N)
22062206
end
22072207

2208-
@inbounds A = Array{T, nd}(undef, ntuple(x -> 1, N - 1)..., Ndim, ntuple(x -> 1, nd - N)...)
2208+
A = Array{T, nd}(undef, ntuple(x -> 1, N - 1)..., Ndim, ntuple(x -> 1, nd - N)...)
22092209

22102210
k = 1
2211-
@inbounds for a as
2211+
for a as
22122212
if a isa AbstractArray
22132213
lena = length(a)
22142214
copyto!(A, k, a, 1, lena)
@@ -2226,17 +2226,17 @@ function _typed_hvncat(::Type{T}, dims::Tuple{Vararg{Int, N}}, row_first::Bool,
22262226
d2 = row_first ? 1 : 2
22272227

22282228
# discover dimensions
2229-
@inbounds nd = max(N, cat_ndims(as[1]))
2229+
nd = max(N, cat_ndims(as[1]))
22302230
outdims = zeros(Int, nd)
22312231

22322232
# discover number of rows or columns
2233-
@inbounds for i 1:dims[d1]
2233+
for i 1:dims[d1]
22342234
outdims[d1] += cat_size(as[i], d1)
22352235
end
22362236

22372237
currentdims = zeros(Int, nd)
22382238
blockcount = 0
2239-
@inbounds for i eachindex(as)
2239+
for i eachindex(as)
22402240
currentdims[d1] += cat_size(as[i], d1)
22412241
if currentdims[d1] == outdims[d1]
22422242
currentdims[d1] = 0
@@ -2287,18 +2287,18 @@ function _typed_hvncat(::Type{T}, shape::Tuple{Vararg{Tuple, N}}, row_first::Boo
22872287
d1 = row_first ? 2 : 1
22882288
d2 = row_first ? 1 : 2
22892289
shape = collect(shape) # saves allocations later
2290-
@inbounds shapelength = shape[end][1]
2290+
shapelength = shape[end][1]
22912291
lengthas = length(as)
22922292
shapelength == lengthas || throw(ArgumentError("number of elements does not match shape; expected $(shapelength), got $lengthas)"))
22932293

22942294
# discover dimensions
2295-
@inbounds nd = max(N, cat_ndims(as[1]))
2295+
nd = max(N, cat_ndims(as[1]))
22962296
outdims = zeros(Int, nd)
22972297
currentdims = zeros(Int, nd)
22982298
blockcounts = zeros(Int, nd)
22992299
shapepos = ones(Int, nd)
23002300

2301-
@inbounds for i eachindex(as)
2301+
for i eachindex(as)
23022302
wasstartblock = false
23032303
for d 1:N
23042304
ad = (d < 3 && row_first) ? (d == 1 ? 2 : 1) : d
@@ -2328,7 +2328,7 @@ function _typed_hvncat(::Type{T}, shape::Tuple{Vararg{Tuple, N}}, row_first::Boo
23282328
end
23292329

23302330
if row_first
2331-
@inbounds outdims[1], outdims[2] = outdims[2], outdims[1]
2331+
outdims[1], outdims[2] = outdims[2], outdims[1]
23322332
end
23332333

23342334
# @assert all(==(0), currentdims)
@@ -2340,11 +2340,11 @@ function _typed_hvncat(::Type{T}, shape::Tuple{Vararg{Tuple, N}}, row_first::Boo
23402340
return A
23412341
end
23422342

2343-
@inline function hvncat_fill!(A::Array{T, N}, scratch1::Vector{Int}, scratch2::Vector{Int}, d1::Int, d2::Int, as::Tuple{Vararg}) where {T, N}
2343+
function hvncat_fill!(A::Array{T, N}, scratch1::Vector{Int}, scratch2::Vector{Int}, d1::Int, d2::Int, as::Tuple{Vararg}) where {T, N}
23442344
outdims = size(A)
23452345
offsets = scratch1
23462346
inneroffsets = scratch2
2347-
@inbounds for a as
2347+
for a as
23482348
if isa(a, AbstractArray)
23492349
for ai a
23502350
Ai = hvncat_calcindex(offsets, inneroffsets, outdims, N)

stdlib/LinearAlgebra/src/LinearAlgebra.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import Base: USE_BLAS64, abs, acos, acosh, acot, acoth, acsc, acsch, adjoint, as
1515
oneunit, parent, power_by_squaring, print_matrix, promote_rule, real, round, sec, sech,
1616
setindex!, show, similar, sin, sincos, sinh, size, sqrt,
1717
strides, stride, tan, tanh, transpose, trunc, typed_hcat, vec
18-
using Base: hvcat_fill, IndexLinear, promote_op, promote_typeof,
18+
using Base: IndexLinear, promote_op, promote_typeof,
1919
@propagate_inbounds, @pure, reduce, typed_vcat, require_one_based_indexing
2020
using Base.Broadcast: Broadcasted, broadcasted
2121
import Libdl

test/abstractarray.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1388,6 +1388,8 @@ end
13881388
for v = (1, "test")
13891389
@test [v v;;; fill(v, 1, 2)] == fill(v, 1, 2, 2)
13901390
end
1391+
1392+
@test_throws BoundsError hvncat(((1, 2), (3,)), false, zeros(Int, 0, 0, 0), 7, 8)
13911393
end
13921394

13931395
@testset "keepat!" begin

0 commit comments

Comments
 (0)