Skip to content

Commit 327b2e3

Browse files
authored
Fix type of allocated array when broadcasting type unstable function (#37028)
We need to call similar on the `Broadcasted` object rather than on dest array. Otherwise the `BroadcastStyle` isn't taken into account when allocating new array due to function returning elements of different types.
1 parent 613af3c commit 327b2e3

File tree

2 files changed

+5
-1
lines changed

2 files changed

+5
-1
lines changed

base/broadcast.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1029,7 +1029,7 @@ function copyto_nonleaf!(dest, bc::Broadcasted, iter, state, count)
10291029
else
10301030
# This element type doesn't fit in dest. Allocate a new dest with wider eltype,
10311031
# copy over old values, and continue
1032-
newdest = Base.similar(dest, promote_typejoin(T, typeof(val)))
1032+
newdest = Base.similar(bc, promote_typejoin(T, typeof(val)))
10331033
return restart_copyto_nonleaf!(newdest, dest, bc, val, I, iter, state, count)
10341034
end
10351035
count += 1

test/broadcast.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,13 +510,17 @@ Base.BroadcastStyle(::Type{T}) where {T<:AD2Dim} = AD2DimStyle()
510510
aa = Array19745(a)
511511
fadd(aa) = aa .+ 1
512512
fadd2(aa) = aa .+ 1 .* 2
513+
fadd3(aa) = aa .+ [missing; 1:9]
513514
fprod(aa) = aa .* aa'
514515
@test a .+ 1 == @inferred(fadd(aa))
515516
@test a .+ 1 .* 2 == @inferred(fadd2(aa))
516517
@test a .* a' == @inferred(fprod(aa))
518+
@test isequal(a .+ [missing; 1:9], fadd3(aa))
519+
@test_broken Core.Compiler.return_type(fadd3, (typeof(aa),)) <: Array19745{<:Union{Float64, Missing}}
517520
@test isa(aa .+ 1, Array19745)
518521
@test isa(aa .+ 1 .* 2, Array19745)
519522
@test isa(aa .* aa', Array19745)
523+
@test isa(aa .* [missing; 1:9], Array19745)
520524
a1 = AD1(rand(2,3))
521525
a2 = AD2(rand(2))
522526
@test a1 .+ 1 isa AD1

0 commit comments

Comments
 (0)