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 (JuliaLang#39608)

* Fix for infinite loop with 0d array

* Consistency

* More consistency

* Test
  • Loading branch information
BioTurboNick authored and ElOceanografo committed May 4, 2021
1 parent 8e19adc commit a3fb768
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 @@ -2898,3 +2898,9 @@ end
@test as isa TSlow{Int,3}
@test size(as) == (3, 5, 1)
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 a3fb768

Please sign in to comment.