Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tighten validation of hvncat implementation #43940

Merged
merged 3 commits into from
Feb 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions base/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2327,6 +2327,24 @@ function _typed_hvncat_dims(::Type{T}, dims::NTuple{N, Int}, row_first::Bool, as

outdims = zeros(Int, N)

# validate shapes for lowest level of concatenation
d = findfirst(>(1), dims)
if d !== nothing # all dims are 1
nblocks = length(as) ÷ dims[d]
for b ∈ 1:nblocks
offset = ((b - 1) * dims[d])
startelementi = offset + 1
for i ∈ offset .+ (2:dims[d])
for dd ∈ 1:N
dd == d && continue
if size(as[startelementi], dd) != size(as[i], dd)
throw(ArgumentError("incompatible shape in element $i"))
end
end
end
end
end

# discover number of rows or columns
for i ∈ 1:dims[d1]
outdims[d1] += cat_size(as[i], d1)
Expand Down
4 changes: 4 additions & 0 deletions test/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1534,6 +1534,10 @@ using Base: typed_hvncat
@test Int[] == typed_hvncat(Int, 1) isa Array{Int, 1}
@test Array{Int, 2}(undef, 0, 0) == typed_hvncat(Int, 2) isa Array{Int, 2}
@test Array{Int, 3}(undef, 0, 0, 0) == typed_hvncat(Int, 3) isa Array{Int, 3}

# Issue 43933 - semicolon precedence mistake should produce an error
@test_throws ArgumentError [[1 1]; 2 ;; 3 ; [3 4]]
@test_throws ArgumentError [[1 ;;; 1]; 2 ;;; 3 ; [3 ;;; 4]]
end

@testset "keepat!" begin
Expand Down