Skip to content

Commit

Permalink
Fix assignment to 0-dim array indexed by CartesianIndices{0} (#34893)
Browse files Browse the repository at this point in the history
* Fix assignment to 0-dim array indexed by CartesianIndices{0}

* Fix assigning element into ≥1-dim arrays from 0-dim array
  • Loading branch information
jmert authored Feb 28, 2020
1 parent 3f05b0d commit 6dd9f41
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
3 changes: 3 additions & 0 deletions base/multidimensional.jl
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,9 @@ module IteratorsMD
# When used as indices themselves, CartesianIndices can simply become its tuple of ranges
@inline to_indices(A, inds, I::Tuple{CartesianIndices, Vararg{Any}}) =
to_indices(A, inds, (I[1].indices..., tail(I)...))
# but preserve CartesianIndices{0} as they consume a dimension.
@inline to_indices(A, inds, I::Tuple{CartesianIndices{0},Vararg{Any}}) =
(first(I), to_indices(A, inds, tail(I))...)

@inline function in(i::CartesianIndex{N}, r::CartesianIndices{N}) where {N}
_in(true, i.I, first(r).I, last(r).I)
Expand Down
24 changes: 24 additions & 0 deletions test/arrayops.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2684,6 +2684,30 @@ end
end
@test !checkbounds(Bool, rand(3,3,3), :, CartesianIndex(0,0):CartesianIndex(1,1))
@test !checkbounds(Bool, rand(3,3,3), CartesianIndex(0,0):CartesianIndex(1,1), :)

CI0 = CartesianIndices(())
# 0-dimensional
@test setindex!(fill(0.0), fill(1.0), CI0) == fill(1.0)
@test setindex!(fill(0.0), fill(1.0), CI0, CI0) == fill(1.0)
@test setindex!(fill(0.0), fill(1.0), :, CI0) == fill(1.0)
@test setindex!(fill(0.0), fill(1.0), CI0, :) == fill(1.0)
@test setindex!(fill(0.0), fill(1.0), :, CI0, CI0) == fill(1.0)
@test setindex!(fill(0.0), fill(1.0), CI0, :, CI0) == fill(1.0)
@test setindex!(fill(0.0), fill(1.0), CI0, CI0, :) == fill(1.0)
@test setindex!(fill(fill(0.0)), fill(fill(1.0)), CI0) == fill(fill(1.0))
# 1-dimensional
@test setindex!(zeros(2), ones(2), :, CI0) == ones(2)
@test setindex!(zeros(2), ones(2), CI0, :) == ones(2)
@test setindex!(zeros(2), ones(2), :, CI0, CI0) == ones(2)
@test setindex!(zeros(2), ones(2), CI0, :, CI0) == ones(2)
@test setindex!(zeros(2), ones(2), CI0, CI0, :) == ones(2)
@test setindex!([fill(0.0)], fill(1.0), 1) == [fill(1.0)]
# 0-dimensional assigment into ≥1-dimensional arrays
@test setindex!(zeros(2), fill(1.0), 1, CI0) == [1.0, 0.0]
@test setindex!(zeros(2), fill(1.0), CI0, 1) == [1.0, 0.0]
@test setindex!(zeros(2,2), fill(1.0), 1, 1, CI0) == [1.0 0.0; 0.0 0.0]
@test setindex!(zeros(2,2), fill(1.0), 1, CI0, 1) == [1.0 0.0; 0.0 0.0]
@test setindex!(zeros(2,2), fill(1.0), CI0, 1, 1) == [1.0 0.0; 0.0 0.0]
end

# Throws ArgumentError for negative dimensions in Array
Expand Down

2 comments on commit 6dd9f41

@nanosoldier
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Executing the daily benchmark build, I will reply here when finished:

@nanosoldier runbenchmarks(ALL, isdaily = true)

@nanosoldier
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your benchmark job has completed - possible performance regressions were detected. A full report can be found here. cc @ararslan

Please sign in to comment.