Skip to content

Commit

Permalink
one more error
Browse files Browse the repository at this point in the history
  • Loading branch information
mcabbott committed Nov 13, 2021
1 parent 8667372 commit bfa54e3
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 2 deletions.
1 change: 1 addition & 0 deletions base/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2774,6 +2774,7 @@ function mapslices(f, A::AbstractArray; dims)
isempty(dims) && return map(f, A)

for d in dims
d >= 1 || throw(ArgumentError("dimension must be ≥ 1, got $d"))
# Indexing a matrix M[:,1,:] produces a 1-column matrix, but dims=(1,3) here
# would otherwise ignore 3, and slice M[:,i]. Previously this gave error:
# BoundsError: attempt to access 2-element Vector{Any} at index [3]
Expand Down
6 changes: 4 additions & 2 deletions test/arrayops.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1200,12 +1200,14 @@ end
# issue #21123
@test mapslices(nnz, sparse(1.0I, 3, 3), dims=1) == [1 1 1]

# path which doesn't re-use Aslice
r = rand(Int8, 2,3,4)
r = rand(Int8, 4,5,2)
@test mapslices(transpose, r, dims=(1,3)) == permutedims(r, (3,2,1))
@test vec(mapslices(repr, r, dims=(2,1))) == map(repr, eachslice(r, dims=3))
@test mapslices(cumsum, sparse(r[:,:,1]), dims=1) == cumsum(r[:,:,1], dims=1)
@test mapslices(prod, sparse(r[:,:,1]), dims=1) == prod(r[:,:,1], dims=1)

# re-write, #40996
@test_throws ArgumentError mapslices(identity, rand(2,3), dims=0) # previously BoundsError
@test_throws ArgumentError mapslices(identity, rand(2,3), dims=(1,3)) # previously BoundsError
@test_throws DimensionMismatch mapslices(x -> x * x', rand(2,3), dims=1) # explicitly caught
@test mapslices(hcat, [1 2; 3 4], dims=1) == [1 2; 3 4] # previously an error, now allowed
Expand Down

0 comments on commit bfa54e3

Please sign in to comment.