From b1e711e5aeb8f0c8c16ec16efb12852812db3ac2 Mon Sep 17 00:00:00 2001 From: N5N3 <2642243996@qq.com> Date: Sun, 15 Oct 2023 21:56:22 +0800 Subject: [PATCH] Avoid `Style()` during broadcast whenever possible. (#51708) On master, `combine_styles(bc::Broadcasted)` calls `BroadcastStyle(typeof(bc))`, which seems bad after #49395 as it has a `Style()` call by default. --- base/broadcast.jl | 4 ++++ test/broadcast.jl | 3 +++ 2 files changed, 7 insertions(+) diff --git a/base/broadcast.jl b/base/broadcast.jl index 43044f9b7d6ed..145be717cbd58 100644 --- a/base/broadcast.jl +++ b/base/broadcast.jl @@ -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...)) diff --git a/test/broadcast.jl b/test/broadcast.jl index 269adc9f7276d..0a111f9523d60 100644 --- a/test/broadcast.jl +++ b/test/broadcast.jl @@ -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