Skip to content

Commit

Permalink
fix bit_map! with aliasing (#50781)
Browse files Browse the repository at this point in the history
fixes #50780 caused by #47013.
  • Loading branch information
oscardssmith committed Aug 5, 2023
1 parent 117ef2e commit 3e04129
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
10 changes: 6 additions & 4 deletions base/bitarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1791,9 +1791,10 @@ function bit_map!(f::F, dest::BitArray, A::BitArray) where F
dest_last = destc[len_Ac]
_msk = _msk_end(A)
# first zero out the bits mask is going to change
destc[len_Ac] = (dest_last & (~_msk))
# then update bits by `or`ing with a masked RHS
destc[len_Ac] |= f(Ac[len_Ac]) & _msk
# DO NOT SEPARATE ONTO TO LINES.
# Otherwise there will be bugs when Ac aliases destc
destc[len_Ac] = (dest_last & (~_msk)) | f(Ac[len_Ac]) & _msk
dest
end
function bit_map!(f::F, dest::BitArray, A::BitArray, B::BitArray) where F
Expand All @@ -1812,9 +1813,10 @@ function bit_map!(f::F, dest::BitArray, A::BitArray, B::BitArray) where F
dest_last = destc[len_Ac]
_msk = _msk_end(min_bitlen)
# first zero out the bits mask is going to change
destc[len_Ac] = (dest_last & ~(_msk))
# then update bits by `or`ing with a masked RHS
destc[len_Ac] |= f(Ac[end], Bc[end]) & _msk
# DO NOT SEPARATE ONTO TO LINES.
# Otherwise there will be bugs when Ac or Bc aliases destc
destc[len_Ac] = (dest_last & ~(_msk)) | f(Ac[end], Bc[end]) & _msk
dest
end

Expand Down
15 changes: 15 additions & 0 deletions test/bitarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1539,6 +1539,21 @@ timesofar("reductions")
end
end
end
@testset "Issue #50780, map! bitarray map! where dest aliases source" begin
a = BitVector([1,0])
b = map(!, a)
map!(!, a, a) # a .= !.a
@test a == b == BitVector([0,1])

a = BitVector([1,0])
c = map(|, a, b)
map!(|, a, a, b)
@test c == a == BitVector([1, 1])

a = BitVector([1,0])
map!(|, b, a, b)
@test c == b == BitVector([1, 1])
end
end

## Filter ##
Expand Down

0 comments on commit 3e04129

Please sign in to comment.