Skip to content

Commit

Permalink
Fix for infinite loop when passing 0d array to setindex of n-dim arra…
Browse files Browse the repository at this point in the history
…ys (#39608)

* Fix for infinite loop with 0d array

* Consistency

* More consistency

* Test

(cherry picked from commit 296acf2)
  • Loading branch information
BioTurboNick authored and KristofferC committed Feb 17, 2021
1 parent 684587d commit de9832c
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
3 changes: 3 additions & 0 deletions base/indices.jl
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,9 @@ setindex_shape_check(X::AbstractArray) =
setindex_shape_check(X::AbstractArray, i::Integer) =
(length(X)==i || throw_setindex_mismatch(X, (i,)))

setindex_shape_check(X::AbstractArray{<:Any, 0}, i::Integer...) =
(length(X) == prod(i) || throw_setindex_mismatch(X, i))

setindex_shape_check(X::AbstractArray{<:Any,1}, i::Integer) =
(length(X)==i || throw_setindex_mismatch(X, (i,)))

Expand Down
6 changes: 6 additions & 0 deletions test/arrayops.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2866,3 +2866,9 @@ end
@test only(Base.return_types(f, (Int,))) === Union{Array{Int,0}, Array{Nothing,0}}
@test only(Base.return_types(f, (UnitRange{Int},))) <: Vector
end

@testset "0-dimensional shape checking #39608" begin
@test [fill(1); [2; 2]] == [1; 2; 2]
@test [fill(1); fill(2, (2,1,1))] == reshape([1; 2; 2], (3, 1, 1))
@test_throws DimensionMismatch [fill(1); rand(2, 2, 2)]
end

0 comments on commit de9832c

Please sign in to comment.