From 7e36695a26c502cb2f27f4ade270159043b2a65d Mon Sep 17 00:00:00 2001 From: Tim Besard Date: Tue, 27 Oct 2026 14:51:34 +0100 Subject: [PATCH] Return the destination from `map!` when there is nothing to map With no elements to process, `map!` returned `nothing` instead of `dest`. `map` of arguments with different lengths goes through `map!` and returned `nothing` too, e.g. `map(+, empty, nonempty)`. --- src/host/broadcast.jl | 2 +- test/testsuite/broadcasting.jl | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/host/broadcast.jl b/src/host/broadcast.jl index 7132863dc..4498c6e6d 100644 --- a/src/host/broadcast.jl +++ b/src/host/broadcast.jl @@ -114,7 +114,7 @@ function Base.map!(f, dest::AnyGPUArray, xs::AbstractArray...) # (avoids the reshape + view that our mapreduce impl has to do) indices = LinearIndices.((dest, xs...)) common_length = minimum(length.(indices)) - common_length==0 && return + common_length==0 && return dest bc = Broadcast.instantiate(Broadcast.broadcasted(f, xs...)) if bc isa Broadcast.Broadcasted diff --git a/test/testsuite/broadcasting.jl b/test/testsuite/broadcasting.jl index e892c69fb..19e8b1928 100644 --- a/test/testsuite/broadcasting.jl +++ b/test/testsuite/broadcasting.jl @@ -166,6 +166,9 @@ function broadcasting(AT, eltypes) @test compare(AT, rand(ET, 2), 1:2) do x, y map!(+, x, y) end + @test compare(AT, rand(ET, 0), rand(ET, 0)) do x, y + map!(+, x, y) + end end @testset "map $ET" begin @@ -178,6 +181,9 @@ function broadcasting(AT, eltypes) @test compare(AT, rand(ET, 2,2), rand(ET, 2)) do x,y map(+, x, y) end + @test compare(AT, rand(ET, 0), rand(ET, 2)) do x, y + map(+, x, y) + end # issue #580 @test compare(AT, rand(ET, 2), 1:2) do x, y map(+, x, y)