-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Make return type of broadcast inferrable with heterogeneous arrays #30485
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
Changes from all commits
d487b1d
3f1a1f1
8a1e65f
999f17f
707e0fd
f32e2a3
1da52df
d5767ab
a59f364
d7721a0
42d1020
56d37ff
5b55e06
0c78029
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -365,7 +365,7 @@ end | |
let f17314 = x -> x < 0 ? false : x | ||
@test eltype(broadcast(f17314, 1:3)) === Int | ||
@test eltype(broadcast(f17314, -1:1)) === Integer | ||
@test eltype(broadcast(f17314, Int[])) == Union{Bool,Int} | ||
@test eltype(broadcast(f17314, Int[])) === Integer | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a minor change which I think can be considered as a bug fix, in the sense that before this PR the element type when the input is empty will never be observed when the array isn't empty (we can only ever observe There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh that's interesting. Yeah, here's the current behaviors:
The reason for using inference here in the first place is to preserve the performance in the non-empty case. Adding a fourth possible return type defeats such a purpose, so I'm in support of this change. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Exactly - we either need to change the last to match the first, or the first to match the last. This PR seems the least breaking, and suitable for v1.x. If we ever wanted to consider the other way around maybe that should be a v2.0 change? |
||
end | ||
let io = IOBuffer() | ||
broadcast(x->print(io,x), 1:5) # broadcast with side effects | ||
|
@@ -950,3 +950,41 @@ p0 = copy(p) | |
@test map(.+, [[1,2], [3,4]], [5, 6]) == [[6,7], [9,10]] | ||
@test repr(.!) == "Base.Broadcast.BroadcastFunction(!)" | ||
@test eval(:(.+)) == Base.BroadcastFunction(+) | ||
|
||
@testset "Issue #28382: inferrability of broadcast with Union eltype" begin | ||
@test isequal([1, 2] .+ [3.0, missing], [4.0, missing]) | ||
@test_broken Core.Compiler.return_type(broadcast, Tuple{typeof(+), Vector{Int}, | ||
Vector{Union{Float64, Missing}}}) == | ||
Vector{<:Union{Float64, Missing}} | ||
@test Core.Compiler.return_type(broadcast, Tuple{typeof(+), Vector{Int}, | ||
Vector{Union{Float64, Missing}}}) == | ||
AbstractVector{<:Union{Float64, Missing}} | ||
@test isequal([1, 2] + [3.0, missing], [4.0, missing]) | ||
@test_broken Core.Compiler.return_type(+, Tuple{Vector{Int}, | ||
Vector{Union{Float64, Missing}}}) == | ||
Vector{<:Union{Float64, Missing}} | ||
@test Core.Compiler.return_type(+, Tuple{Vector{Int}, | ||
Vector{Union{Float64, Missing}}}) == | ||
AbstractVector{<:Union{Float64, Missing}} | ||
@test_broken Core.Compiler.return_type(+, Tuple{Vector{Int}, | ||
Vector{Union{Float64, Missing}}}) == | ||
Vector{<:Union{Float64, Missing}} | ||
@test isequal(tuple.([1, 2], [3.0, missing]), [(1, 3.0), (2, missing)]) | ||
@test_broken Core.Compiler.return_type(broadcast, Tuple{typeof(tuple), Vector{Int}, | ||
Vector{Union{Float64, Missing}}}) == | ||
Vector{<:Tuple{Int, Any}} | ||
@test Core.Compiler.return_type(broadcast, Tuple{typeof(tuple), Vector{Int}, | ||
Vector{Union{Float64, Missing}}}) == | ||
AbstractVector{<:Tuple{Int, Any}} | ||
Comment on lines
+956
to
+978
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. #39618 seems to fix these tests. @nalimilan Could that mean that this workaround might not even be needed anymore? Or does it maybe just fix this particular example? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah then it's great! Actually all the work I did in this PR had not effect for now due to this inference failure (a regression due to changes to reduce compile time introduced since 1.5). So if you can replace these There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, cool. Thanks for checking! |
||
# Check that corner cases do not throw an error | ||
@test isequal(broadcast(x -> x === 1 ? nothing : x, [1, 2, missing]), | ||
[nothing, 2, missing]) | ||
@test isequal(broadcast(x -> x === 1 ? nothing : x, Any[1, 2, 3.0, missing]), | ||
[nothing, 2, 3, missing]) | ||
@test broadcast((x,y)->(x==1 ? 1.0 : x, y), [1 2 3], ["a", "b", "c"]) == | ||
[(1.0, "a") (2, "a") (3, "a") | ||
(1.0, "b") (2, "b") (3, "b") | ||
(1.0, "c") (2, "c") (3, "c")] | ||
@test typeof.([iszero, isdigit]) == [typeof(iszero), typeof(isdigit)] | ||
@test typeof.([iszero, iszero]) == [typeof(iszero), typeof(iszero)] | ||
end |
Uh oh!
There was an error while loading. Please reload this page.