Skip to content

Commit

Permalink
Fix broadcast error when eltype is inconsistent with getindex (#39185)
Browse files Browse the repository at this point in the history
* Fix broadcast error when eltype is inconsistent with getindex

In that case we can infer the return type as `Union{}`, which triggers
a type assertion error. This used to work in Julia 1.5.

* whitespace

Co-authored-by: Simeon Schaub <[email protected]>

* Update test/broadcast.jl

Co-authored-by: Matt Bauman <[email protected]>

Co-authored-by: Matt Bauman <[email protected]>
Co-authored-by: Simeon Schaub <[email protected]>
  • Loading branch information
3 people authored Jan 29, 2021
1 parent c8cc69b commit 750f42a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
2 changes: 1 addition & 1 deletion base/broadcast.jl
Original file line number Diff line number Diff line change
Expand Up @@ -924,7 +924,7 @@ const NonleafHandlingStyles = Union{DefaultArrayStyle,ArrayConflict}
# Now handle the remaining values
# The typeassert gives inference a helping hand on the element type and dimensionality
# (work-around for #28382)
ElType′ = ElType <: Type ? Type : ElType
ElType′ = ElType === Union{} ? Any : ElType <: Type ? Type : ElType
RT = dest isa AbstractArray ? AbstractArray{<:ElType′, ndims(dest)} : Any
return copyto_nonleaf!(dest, bc′, iter, state, 1)::RT
end
Expand Down
12 changes: 12 additions & 0 deletions test/broadcast.jl
Original file line number Diff line number Diff line change
Expand Up @@ -990,3 +990,15 @@ p0 = copy(p)
@test isequal(identity.(Vector{<:Union{Int, Missing}}[[1, 2],[missing, 1]]),
[[1, 2],[missing, 1]])
end

@testset "Issue #28382: eltype inconsistent with getindex" begin
struct Cyclotomic <: Number
end

Base.eltype(::Type{<:Cyclotomic}) = Tuple{Int,Int}

Base.:*(c::T, x::Cyclotomic) where {T<:Real} = [1, 2]
Base.:*(x::Cyclotomic, c::T) where {T<:Real} = [1, 2]

@test Cyclotomic() .* [2, 3] == [[1, 2], [1, 2]]
end

0 comments on commit 750f42a

Please sign in to comment.