Skip to content

Commit

Permalink
Avoid Style() during broadcast whenever possible. (#51708)
Browse files Browse the repository at this point in the history
On master, `combine_styles(bc::Broadcasted)` calls
`BroadcastStyle(typeof(bc))`, which seems bad after #49395 as it has a
`Style()` call by default.
  • Loading branch information
N5N3 authored Oct 15, 2023
1 parent 730450c commit b1e711e
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 0 deletions.
4 changes: 4 additions & 0 deletions base/broadcast.jl
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,10 @@ function combine_styles end

combine_styles() = DefaultArrayStyle{0}()
combine_styles(c) = result_style(BroadcastStyle(typeof(c)))
function combine_styles(bc::Broadcasted)
bc.style isa Union{Nothing,Unknown} || return bc.style
throw(ArgumentError("Broadcasted{Unknown} wrappers do not have a style assigned"))
end
combine_styles(c1, c2) = result_style(combine_styles(c1), combine_styles(c2))
@inline combine_styles(c1, c2, cs...) = result_style(combine_styles(c1), combine_styles(c2, cs...))

Expand Down
3 changes: 3 additions & 0 deletions test/broadcast.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1153,6 +1153,9 @@ Base.BroadcastStyle(a::MyBroadcastStyleWithField, b::MyBroadcastStyleWithField)
MyBroadcastStyleWithField(1)
@test_throws ErrorException Broadcast.result_style(MyBroadcastStyleWithField(1),
MyBroadcastStyleWithField(2))
dest = [0, 0]
dest .= Broadcast.Broadcasted(MyBroadcastStyleWithField(1), +, (1:2, 2:3))
@test dest == [3, 5]
end

# test that `Broadcast` definition is defined as total and eligible for concrete evaluation
Expand Down

0 comments on commit b1e711e

Please sign in to comment.