Skip to content

Commit

Permalink
Broadcast shouldn't use inbounds when calling f
Browse files Browse the repository at this point in the history
Previously, `broadcast(getindex, A, I...)` would call `getindex` within an
at-inbounds context. This patch simply moves the function call so that it
will throw a BoundsError appropriately.
  • Loading branch information
mbauman committed Nov 2, 2016
1 parent 7aba3f5 commit 409650a
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
3 changes: 2 additions & 1 deletion base/broadcast.jl
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,8 @@ end
# extract array values
@nexprs $nargs i->(@inbounds val_i = _broadcast_getindex(A_i, I_i))
# call the function and store the result
@inbounds B[I] = @ncall $nargs f val
result = @ncall $nargs f val
@inbounds B[I] = result
end
end
end
Expand Down
5 changes: 5 additions & 0 deletions test/broadcast.jl
Original file line number Diff line number Diff line change
Expand Up @@ -337,3 +337,8 @@ end
@test broadcast(+, 1.0, (0, -2.0)) == (1.0,-1.0)
@test broadcast(+, 1.0, (0, -2.0), [1]) == [2.0, 0.0]
@test broadcast(*, ["Hello"], ", ", ["World"], "!") == ["Hello, World!"]

# Ensure that broadcast doesn't use @inbounds when calling the function
let A = zeros(3,3)
@test_throws BoundsError broadcast(getindex, A, 1:3, 1:3)
end

0 comments on commit 409650a

Please sign in to comment.