Skip to content

Commit

Permalink
Don't @inbounds function evaluation in broadcasting (#1200)
Browse files Browse the repository at this point in the history
  • Loading branch information
mateuszbaran authored Sep 22, 2023
1 parent 27a6079 commit bba1f48
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "StaticArrays"
uuid = "90137ffa-7385-5640-81b9-e52037218182"
version = "1.6.3"
version = "1.6.4"

[deps]
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Expand Down
12 changes: 8 additions & 4 deletions src/broadcast.jl
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ end

return quote
@_inline_meta
@inbounds return elements = tuple($(exprs...))
return tuple($(exprs...))
end
end

Expand All @@ -149,15 +149,19 @@ end
sizes = [sz.parameters[1] for sz in s.parameters]

indices = CartesianIndices(newsize)
exprs = similar(indices, Expr)
exprs_eval = similar(indices, Expr)
exprs_setindex = similar(indices, Expr)
for (j, current_ind) enumerate(indices)
exprs_vals = (broadcast_getindex(sz, i, current_ind) for (i, sz) in enumerate(sizes))
exprs[j] = :(dest[$j] = f($(exprs_vals...)))
symb_val_j = Symbol(:val_, j)
exprs_eval[j] = :($symb_val_j = f($(exprs_vals...)))
exprs_setindex[j] = :(dest[$j] = $symb_val_j)
end

return quote
@_inline_meta
@inbounds $(Expr(:block, exprs...))
$(Expr(:block, exprs_eval...))
@inbounds $(Expr(:block, exprs_setindex...))
return dest
end
end
Expand Down
6 changes: 6 additions & 0 deletions test/broadcast.jl
Original file line number Diff line number Diff line change
Expand Up @@ -350,3 +350,9 @@ end
issue609(s, c::Integer) = (s .- s.^2) ./ c
@test @inferred(issue609(SA[1.], 2)) == issue609([1.], 2)
end

@testset "broadcasting out-of-bounds getindex" begin
@test_throws BoundsError getindex.(SA[1, 2], 0)
a = @MArray [1, 2]
@test_throws BoundsError a .= getindex.(SA[1, 2], 0)
end

0 comments on commit bba1f48

Please sign in to comment.