Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix broadcasting for non-DefaultArrayStyle containers #222

Merged
merged 1 commit into from
Aug 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading