Skip to content

Commit bba1f48

Browse files
authored
Don't @inbounds function evaluation in broadcasting (#1200)
1 parent 27a6079 commit bba1f48

File tree

3 files changed

+15
-5
lines changed

3 files changed

+15
-5
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "StaticArrays"
22
uuid = "90137ffa-7385-5640-81b9-e52037218182"
3-
version = "1.6.3"
3+
version = "1.6.4"
44

55
[deps]
66
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"

src/broadcast.jl

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ end
137137

138138
return quote
139139
@_inline_meta
140-
@inbounds return elements = tuple($(exprs...))
140+
return tuple($(exprs...))
141141
end
142142
end
143143

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

151151
indices = CartesianIndices(newsize)
152-
exprs = similar(indices, Expr)
152+
exprs_eval = similar(indices, Expr)
153+
exprs_setindex = similar(indices, Expr)
153154
for (j, current_ind) enumerate(indices)
154155
exprs_vals = (broadcast_getindex(sz, i, current_ind) for (i, sz) in enumerate(sizes))
155-
exprs[j] = :(dest[$j] = f($(exprs_vals...)))
156+
symb_val_j = Symbol(:val_, j)
157+
exprs_eval[j] = :($symb_val_j = f($(exprs_vals...)))
158+
exprs_setindex[j] = :(dest[$j] = $symb_val_j)
156159
end
157160

158161
return quote
159162
@_inline_meta
160-
@inbounds $(Expr(:block, exprs...))
163+
$(Expr(:block, exprs_eval...))
164+
@inbounds $(Expr(:block, exprs_setindex...))
161165
return dest
162166
end
163167
end

test/broadcast.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,3 +350,9 @@ end
350350
issue609(s, c::Integer) = (s .- s.^2) ./ c
351351
@test @inferred(issue609(SA[1.], 2)) == issue609([1.], 2)
352352
end
353+
354+
@testset "broadcasting out-of-bounds getindex" begin
355+
@test_throws BoundsError getindex.(SA[1, 2], 0)
356+
a = @MArray [1, 2]
357+
@test_throws BoundsError a .= getindex.(SA[1, 2], 0)
358+
end

0 commit comments

Comments
 (0)