From 1e3f57b419ed6eb239ad27b81c64cbd2df8816c0 Mon Sep 17 00:00:00 2001 From: Matt Bauman Date: Tue, 5 Jul 2022 09:27:03 -0400 Subject: [PATCH] fix #45903, in place broadcast into a bit-masked bitmatrix (#45904) as reported in https://discourse.julialang.org/t/indexed-assignment-with-logical-indices-subarray-jl-error/83646 (cherry picked from commit 89bdcce76b01ae0327a7e575290a0cbd035c1950) --- base/broadcast.jl | 2 +- test/broadcast.jl | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/base/broadcast.jl b/base/broadcast.jl index b4d89e9a5fb1b..bc1a6254e59b8 100644 --- a/base/broadcast.jl +++ b/base/broadcast.jl @@ -1183,7 +1183,7 @@ Base.@propagate_inbounds dotview(B::BitArray, i::BitArray) = BitMaskedBitArray(B Base.show(io::IO, B::BitMaskedBitArray) = foreach(arg->show(io, arg), (typeof(B), (B.parent, B.mask))) # Override materialize! to prevent the BitMaskedBitArray from escaping to an overrideable method @inline materialize!(B::BitMaskedBitArray, bc::Broadcasted{<:Any,<:Any,typeof(identity),Tuple{Bool}}) = fill!(B, bc.args[1]) -@inline materialize!(B::BitMaskedBitArray, bc::Broadcasted{<:Any}) = materialize!(SubArray(B.parent, to_indices(B.parent, (B.mask,))), bc) +@inline materialize!(B::BitMaskedBitArray, bc::Broadcasted{<:Any}) = materialize!(@inbounds(view(B.parent, B.mask)), bc) function Base.fill!(B::BitMaskedBitArray, b::Bool) Bc = B.parent.chunks Ic = B.mask.chunks diff --git a/test/broadcast.jl b/test/broadcast.jl index 0cfede78afadf..4a01215206937 100644 --- a/test/broadcast.jl +++ b/test/broadcast.jl @@ -993,3 +993,11 @@ end @test Cyclotomic() .* [2, 3] == [[1, 2], [1, 2]] end + +@testset "issue #45903, in place broadcast into a bit-masked bitmatrix" begin + A = BitArray(ones(3,3)) + pos = randn(3,3) + A[pos .< 0] .= false + @test all(>=(0), pos[A]) + @test count(A) == count(>=(0), pos) +end