-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
Make return type of broadcast inferrable with heterogeneous arrays #30485
Conversation
@@ -360,7 +360,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 comment
The 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 Int
, Bool
or Integer
). I could change the PR to preserve the existing behavior if we want (e.g. for backports).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh that's interesting. Yeah, here's the current behaviors:
julia> eltype(broadcast(f17314, Int[]))
Union{Bool, Int64}
julia> eltype(broadcast(f17314, Int[1]))
Int64
julia> eltype(broadcast(f17314, Int[-1]))
Bool
julia> eltype(broadcast(f17314, Int[1,-1]))
Integer
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 comment
The 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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I cannot meaningfully review the way in which you achieve the change in behaviors here, but I approve of the result and think this is a minor change worth making.
I'm still not sure. I'm not really sure that |
What do you mean? Why would Or do you have a better proposal? I agree it would be nice if the compiler did that automatically, but until it does we really need to avoid the inference failure that |
Upon much reflection, I now think this is actually sensible, and does actually help slightly to decouple inference from the result here, as |
Inference is not able to detect the element type automatically, but we can do it manually since we know promote_typejoin is used for widening.
eb61cce
to
707e0fd
Compare
Thanks for the review. I'm amazed I didn't get more things wrong. :-p Unfortunately, after rebasing against current master, the return type is only inferred as |
Good to go @vtjnash? |
Co-authored-by: Jameson Nash <vtjnash@gmail.com>
IIUC, only the type assertion is needed for the inference improvement, and it seems to me that is much easier? |
AFAICT all changes are needed. What could be avoided is the minor change of eltype from |
Bump. This is going to miss 1.6 (meaning it will reach its second anniversary without being released...). |
Thanks @vtjnash and @JeffBezanson. I'll merge tomorrow if nobody objects (FreeBSD failure seems unrelated). |
@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}} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The 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 comment
The 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 @test_broken
with @test
then it's perfect!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, cool. Thanks for checking!
Inference is not able to detect the element type automatically, but we can do it manually since we know promote_typejoin is used for widening. This is similar to the approach used for `broadcast` at #30485.
Inference is not able to detect the element type automatically, but we can do it manually since we know promote_typejoin is used for widening. This is similar to the approach used for `broadcast` at #30485.
Inference is not able to detect the element type automatically, but we can do it manually since we know promote_typejoin is used for widening. This is similar to the approach used for `broadcast` at #30485.
…ng#42046) Inference is not able to detect the element type automatically, but we can do it manually since we know promote_typejoin is used for widening. This is similar to the approach used for `broadcast` at JuliaLang#30485.
…ng#42046) Inference is not able to detect the element type automatically, but we can do it manually since we know promote_typejoin is used for widening. This is similar to the approach used for `broadcast` at JuliaLang#30485.
Inference is not able to detect the element type automatically, but we can do it manually since we know
promote_typejoin
is used for widening.Fixes #28382. The same changes should be applied to
map
.