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