Skip to content

Commit

Permalink
Fix broadcasting for non-DefaultArrayStyle containers
Browse files Browse the repository at this point in the history
  • Loading branch information
odow committed Aug 20, 2023
1 parent 42930a3 commit f90d253
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/broadcast.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,19 @@ function _broadcasted_type(
return BitArray{N}
end

"""
This method is a generic fallback for array types that are not
`DefaultArrayStyle`. Because we can't tell the container from a generic
broadcast style, we fallback to `Any`, which is always a valid super type (just
not a helpful one).
In MutableArithmetics, `_broadcasted_type` appears only in `promote_broadcast`,
which itself appears only in `broadcast_mutability`, and so types hitting this
method will fallback to the `IsNotMutable()` branch, which is the expected
outcome.
"""
_broadcasted_type(::Broadcast.BroadcastStyle, ::Base.HasShape, ::Type) = Any

# Same as `Base.Broadcast._combine_styles` but with types as argument.
_combine_styles() = Broadcast.DefaultArrayStyle{0}()

Expand Down
8 changes: 8 additions & 0 deletions test/broadcast.jl
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,11 @@ end
@test MA.@rewrite(y .* x) == y .* x
@test MA.@rewrite(x .* y) == x .* y
end

struct Struct221 <: AbstractArray{Int,1} end
struct BroadcastStyle221 <: Broadcast.BroadcastStyle end
Base.BroadcastStyle(::Type{Struct221}) = BroadcastStyle221()

@testset "promote_broadcast_for_new_style" begin
@test MA.promote_broadcast(MA.add_mul, Vector{Int}, Struct221) === Any
end

0 comments on commit f90d253

Please sign in to comment.