Skip to content

Commit 3ccbd41

Browse files
authored
Merge pull request #563 from TheBB/setindex
Fix setindex with vector indices
2 parents 5e501b7 + 9cfe953 commit 3ccbd41

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

src/indexing.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ end
157157
end
158158

159159
@generated function _setindex!(a::StaticArray, v::AbstractArray, ::Length{L}, inds::StaticVector{<:Any, Int}) where {L}
160-
exprs = [:(a[$i] = v[$i]) for i = 1:L]
160+
exprs = [:(a[inds[$i]] = v[$i]) for i = 1:L]
161161
return quote
162162
@_propagate_inbounds_meta
163163
if length(v) != L
@@ -168,7 +168,7 @@ end
168168
end
169169

170170
@generated function _setindex!(a::StaticArray, v::StaticArray, ::Length{L}, inds::StaticVector{<:Any, Int}) where {L}
171-
exprs = [:(a[$i] = v[$i]) for i = 1:L]
171+
exprs = [:(a[inds[$i]] = v[$i]) for i = 1:L]
172172
return quote
173173
@_propagate_inbounds_meta
174174
if Length(typeof(v)) != L

test/indexing.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@ using StaticArrays, Test
3333
@test (mv[SVector(1,2,3,4)] = [4, 5, 6, 7]; (@inferred getindex(mv, SVector(4,3,2,1)))::MVector{4,Int} == MVector((7,6,5,4)))
3434
@test (mv[SVector(1,2,3,4)] = 2; (@inferred getindex(mv, SVector(4,3,2,1)))::MVector{4,Int} == MVector((2,2,2,2)))
3535

36+
mv = MVector(0,0,0)
37+
@test (mv[SVector(1,3)] = [4, 5]; (@inferred mv == MVector(4,0,5)))
38+
39+
mv = MVector(0,0,0)
40+
@test (mv[SVector(1,3)] = SVector(4, 5); (@inferred mv == MVector(4,0,5)))
41+
3642
# Colon
3743
mv = MVector{4,Int}(undef)
3844
@test (mv[:] = vec; (@inferred getindex(mv, :))::MVector{4,Int} == MVector((4,5,6,7)))

0 commit comments

Comments
 (0)