From b0b4a482965d68dbd4083968f78f66b9122f4aac Mon Sep 17 00:00:00 2001 From: Simeon Schaub Date: Sat, 3 Apr 2021 20:43:43 +0200 Subject: [PATCH] fix inconsistent BroadcastFunction behavior (#40330) fixes #40309 --- base/broadcast.jl | 8 +++++++- test/broadcast.jl | 4 ++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/base/broadcast.jl b/base/broadcast.jl index de3263a64011b..c6d079d318acf 100644 --- a/base/broadcast.jl +++ b/base/broadcast.jl @@ -1300,7 +1300,13 @@ macro __dot__(x) esc(__dot__(x)) end -@inline broadcasted_kwsyntax(f, args...; kwargs...) = broadcasted((args...)->f(args...; kwargs...), args...) +@inline function broadcasted_kwsyntax(f, args...; kwargs...) + if isempty(kwargs) # some BroadcastStyles dispatch on `f`, so try to preserve its type + return broadcasted(f, args...) + else + return broadcasted((args...) -> f(args...; kwargs...), args...) + end +end @inline function broadcasted(f, args...) args′ = map(broadcastable, args) broadcasted(combine_styles(args′...), f, args′...) diff --git a/test/broadcast.jl b/test/broadcast.jl index 6e97d609a42b0..28c56b2c2e636 100644 --- a/test/broadcast.jl +++ b/test/broadcast.jl @@ -1015,3 +1015,7 @@ end @test a_ == dropdims(a .* c, dims=(findall(==(1), size(c))...,)) end end + +# issue 40309 +@test Base.broadcasted_kwsyntax(+, [1], [2]) isa Broadcast.Broadcasted{<:Any, <:Any, typeof(+)} +@test Broadcast.BroadcastFunction(+)(2:3, 2:3) === 4:2:6