Skip to content

Commit

Permalink
implement promote for BitArray
Browse files Browse the repository at this point in the history
Fix #43551
Co-authored-by: Jakob Sachs <[email protected]>
  • Loading branch information
vtjnash committed Feb 9, 2022
1 parent dc0aa61 commit 81a594a
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
2 changes: 1 addition & 1 deletion base/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -614,7 +614,7 @@ oneunit(x::AbstractMatrix{T}) where {T} = _one(oneunit(T), x)
convert(::Type{T}, a::AbstractArray) where {T<:Array} = a isa T ? a : T(a)
convert(::Type{Union{}}, a::AbstractArray) = throw(MethodError(convert, (Union{}, a)))

promote_rule(a::Type{Array{T,n}}, b::Type{Array{S,n}}) where {T,n,S} = el_same(promote_type(T,S), a, b)
promote_rule(a::Type{<:AbstractArray{T,n}}, b::Type{<:AbstractArray{S,n}}) where {T,n,S} = el_same(promote_type(T, S), a, b)

## Constructors ##

Expand Down
2 changes: 1 addition & 1 deletion base/range.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1265,7 +1265,7 @@ end


# promote eltype if at least one container wouldn't change, otherwise join container types.
el_same(::Type{T}, a::Type{<:AbstractArray{T,n}}, b::Type{<:AbstractArray{T,n}}) where {T,n} = a
el_same(::Type{T}, a::Type{<:AbstractArray{T,n}}, b::Type{<:AbstractArray{T,n}}) where {T,n} = a === b ? a : Array{T,n}
el_same(::Type{T}, a::Type{<:AbstractArray{T,n}}, b::Type{<:AbstractArray{S,n}}) where {T,S,n} = a
el_same(::Type{T}, a::Type{<:AbstractArray{S,n}}, b::Type{<:AbstractArray{T,n}}) where {T,S,n} = b
el_same(::Type, a, b) = promote_typejoin(a, b)
Expand Down
14 changes: 13 additions & 1 deletion test/bitarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,18 @@ end

timesofar("conversions")

@testset "Promotions for size $sz" for (sz, T) in allsizes
@test isequal(promote(falses(sz...),zeros(sz...)),
(zeros(sz...),zeros(sz...)))
@test isequal(promote(trues(sz...),ones(sz...)),
(ones(sz...),ones(sz...)))
ae = falses(1,sz...)
@test_throws ErrorException promote(ae,ones(sz...))
@test_throws ErrorException promote(ae,[ones(sz...)])
end

timesofar("promotions")

@testset "utility functions" begin
b1 = bitrand(v1)
@test isequal(fill!(b1, true), trues(size(b1)))
Expand Down Expand Up @@ -1767,4 +1779,4 @@ end
@test all(bitarray[rangeout, rangein] .== true)
@test all(bitarray[rangein, rangeout] .== true)
end
end
end

0 comments on commit 81a594a

Please sign in to comment.