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

implement promote for BitArray #44096

Merged
merged 2 commits into from
Mar 2, 2022
Merged

implement promote for BitArray #44096

merged 2 commits into from
Mar 2, 2022

Conversation

vtjnash
Copy link
Sponsor Member

@vtjnash vtjnash commented Feb 9, 2022

Fix #43551
Replaces #43646 @JakobSachs following my comment there, based on your work

@vtjnash
Copy link
Sponsor Member Author

vtjnash commented Feb 22, 2022

@JakobSachs this turned out to be more complicated than I thought. WDYT?

@JeffBezanson Thought you might be interested to look at the new promote_rule/promote_type fallback behavior added here.

base/array.jl Outdated Show resolved Hide resolved
@vtjnash vtjnash added the status:merge me PR is reviewed. Merge when all tests are passing label Feb 28, 2022
@JakobSachs
Copy link
Contributor

Seems good to me, tho i cant say i full understand the changes you made, regarding the indices.

vtjnash and others added 2 commits March 1, 2022 15:57
The result is type-asserted to be equal to the input, so we need to use
a non-promoting function.
Defines a fallback promote_result for any AbstractArray, for the case
when specific promote_rules are not defined for the specific array type.

Add a few good promote_rules too.

Fix #43551

Co-authored-by: Jakob Sachs <[email protected]>
@vtjnash vtjnash merged commit 0d29712 into master Mar 2, 2022
@vtjnash vtjnash deleted the jn/43551 branch March 2, 2022 03:48
@N5N3 N5N3 removed the status:merge me PR is reviewed. Merge when all tests are passing label Mar 2, 2022
promote_result(::Type{<:AbstractArray}, ::Type{<:AbstractArray}, ::Type{T}, ::Type{S}) where {T,S} = (@inline; promote_type(T,S))
promote_result(::Type{T}, ::Type{S}, ::Type{Bottom}, ::Type{Bottom}) where {T<:AbstractArray,S<:AbstractArray} = (@inline; promote_typejoin(T,S))
# If no promote_rule is defined, both directions give Bottom. In that case use typejoin on the eltypes instead and give Array as the container.
promote_result(::Type{<:AbstractArray{T,n}}, ::Type{<:AbstractArray{S,n}}, ::Type{Bottom}, ::Type{Bottom}) where {T,S,n} = (@inline; Array{promote_type(T,S),n})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vtjnash Line 1275 breaks StaticArrays.jl's test on master.
After this PR

julia> [(@MArray [1;2;3]), (@SArray [1;2;3])]
2-element Vector{Vector{Int64}}:
 [1, 2, 3]
 [1, 2, 3]

It was a Vector{Any} before this PR.
for a in [array1, array2] is general pattern for test. So this change seems a little "break"?

Copy link
Sponsor Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test should not do that, that is an incorrect test. [a, b] is a general pattern for changing a and b to a common type. If you do not want that, you must specify the type of the array you want (usually Any[...])

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW CategoricalArrays tests also used this pattern. Actually this revealed the lack of promote_type methods for CategoricalArrays.

This has the potential to disrupt lots of tests, as things like e.g. [["b", "a"], ["b", missing]] will suddenly give two Vector{Union{Missing, String}} objects while previously the first one was Vector{String}. I'm not saying it shouldn't be done though but we have to be ready to notice some breakage.

Copy link
Sponsor Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@KristofferC these are mostly the 3 lines that would be removed to change the behavior back, or changed to define a different combination of desired behaviors (for example, just removing the last rule so that it falls back to the promote_typejoin behavior defined on the previous line). The remaining parts of the PR are cleanup (e.g. to add missing constructors revealed by this change) that should still be valid.

You could also try to add a specific rule just for BitArray, as begun here, to just specifically keep the change requested in #43551: https://github.com/JuliaLang/julia/pull/43646/files#r781334697

staticfloat pushed a commit to JuliaCI/julia-buildkite-testing that referenced this pull request Mar 2, 2022
The result is type-asserted to be equal to the input, so we need to use
a non-promoting function.
staticfloat pushed a commit to JuliaCI/julia-buildkite-testing that referenced this pull request Mar 2, 2022
Defines a fallback promote_result for any AbstractArray, for the case
when specific promote_rules are not defined for the specific array type.

Add a few good promote_rules too.

Fix JuliaLang#43551

Co-authored-by: Jakob Sachs <[email protected]>
N5N3 added a commit to N5N3/StaticArrays.jl that referenced this pull request Mar 3, 2022
N5N3 added a commit to N5N3/StaticArrays.jl that referenced this pull request Mar 3, 2022
N5N3 added a commit to N5N3/StaticArrays.jl that referenced this pull request Mar 3, 2022
N5N3 added a commit to N5N3/julia that referenced this pull request Mar 3, 2022
It turns out JuliaLang#44096 changes the default promote behavior of `AbstractArray` with the same `ndims`. Thus some Test in `Base` is "skipped" silently.

This PR tries to re-activate them.
N5N3 added a commit to N5N3/StaticArrays.jl that referenced this pull request Mar 3, 2022
N5N3 added a commit to N5N3/StaticArrays.jl that referenced this pull request Mar 3, 2022
vtjnash pushed a commit that referenced this pull request Mar 3, 2022
The change in #44096 to the default promote behavior of `AbstractArray`
with the same `ndims` caused some some tests in `Base` to be "skipped"
silently. This PR tries to re-activate them.
N5N3 added a commit to N5N3/StaticArrays.jl that referenced this pull request Mar 4, 2022
N5N3 added a commit to N5N3/StaticArrays.jl that referenced this pull request Mar 6, 2022
Comment on lines +1272 to +1275
promote_result(::Type{<:AbstractArray}, ::Type{<:AbstractArray}, ::Type{T}, ::Type{S}) where {T,S} = (@inline; promote_type(T,S))
promote_result(::Type{T}, ::Type{S}, ::Type{Bottom}, ::Type{Bottom}) where {T<:AbstractArray,S<:AbstractArray} = (@inline; promote_typejoin(T,S))
# If no promote_rule is defined, both directions give Bottom. In that case use typejoin on the eltypes instead and give Array as the container.
promote_result(::Type{<:AbstractArray{T,n}}, ::Type{<:AbstractArray{S,n}}, ::Type{Bottom}, ::Type{Bottom}) where {T,S,n} = (@inline; Array{promote_type(T,S),n})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why define these in range.jl? When I was looking for such methods I looked in promotion.jl and in abstractarray.jl and I couldn't find them.

nalimilan added a commit to JuliaData/CategoricalArrays.jl that referenced this pull request Mar 13, 2022
A recent change on Julia master (JuliaLang/julia#44096) makes an array
of arrays use more precise promotion, which ends up converting
`CategoricalArray`s into `Vector`s.
nalimilan added a commit to JuliaData/CategoricalArrays.jl that referenced this pull request Mar 13, 2022
A recent change on Julia master (JuliaLang/julia#44096) makes an array
of arrays use more precise promotion, which ends up converting
`CategoricalArray`s into `Vector`s.
nalimilan added a commit to JuliaData/CategoricalArrays.jl that referenced this pull request Mar 13, 2022
A recent change on Julia master (JuliaLang/julia#44096) makes an array
of arrays use more precise promotion, which ends up converting
`CategoricalArray`s into `Vector`s.
nalimilan added a commit to JuliaData/CategoricalArrays.jl that referenced this pull request Mar 13, 2022
A recent change on Julia master (JuliaLang/julia#44096) makes an array
of arrays use more precise promotion, which ends up converting
`CategoricalArray`s into `Vector`s.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

promote fails with BitArrays
4 participants