From 20c75d84b7aa5e8e71657978b49d9a2c45103470 Mon Sep 17 00:00:00 2001 From: Sacha Verweij Date: Sun, 25 Dec 2016 14:46:36 -0800 Subject: [PATCH] Make broadcast!(f, A) populate A via independent f() calls rather than fill!(A, f()). --- base/sysimg.jl | 2 +- test/broadcast.jl | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/base/sysimg.jl b/base/sysimg.jl index 60c677b072df8..b43b7aa1562e4 100644 --- a/base/sysimg.jl +++ b/base/sysimg.jl @@ -78,7 +78,7 @@ Symbol(x...) = Symbol(string(x...)) # specific array types etc. # --Here, just define fallback routines for broadcasting with no arguments broadcast(f) = f() -broadcast!(f, X::AbstractArray) = fill!(X, f()) +broadcast!(f, X::AbstractArray) = (@inbounds for I in eachindex(X); X[I] = f(); end; X) # array structures include("array.jl") diff --git a/test/broadcast.jl b/test/broadcast.jl index c7b12a74fadcd..314a94844c2bf 100644 --- a/test/broadcast.jl +++ b/test/broadcast.jl @@ -373,3 +373,6 @@ end @test (+).(Ref(1), Ref(2)) == fill(3) @test (+).([[0,2], [1,3]], [1,-1]) == [[1,3], [0,2]] @test (+).([[0,2], [1,3]], Ref{Vector{Int}}([1,-1])) == [[1,1], [2,2]] + +# Check that broadcast!(f, A) populates A via independent calls to f (#12277, #19722). +@test let z = 1; A = broadcast!(() -> z += 1, zeros(2)); A[1] != A[2]; end