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

WIP: Add promote_strict mechanism and use it instead of typejoin() #25423

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions base/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -572,7 +572,7 @@ function collect_to!(dest::AbstractArray{T}, itr, offs, st) where T
@inbounds dest[i] = el::T
i += 1
else
R = typejoin(T, S)
R = promote_type(ExactPromotion(), T, S)
new = similar(dest, R)
copyto!(new,1, dest,1, i-1)
@inbounds new[i] = el
Expand All @@ -595,7 +595,7 @@ function grow_to!(dest, itr, st)
if S === T || S <: T
push!(dest, el::T)
else
new = sizehint!(empty(dest, typejoin(T, S)), length(dest))
new = sizehint!(empty(dest, promote_type(ExactPromotion(), T, S)), length(dest))
if new isa AbstractSet
# TODO: merge back these two branches when copy! is re-enabled for sets/vectors
union!(new, dest)
Expand Down
4 changes: 2 additions & 2 deletions base/broadcast.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
module Broadcast

using Base.Cartesian
using Base: Indices, OneTo, linearindices, tail, to_shape,
using Base: Indices, OneTo, ExactPromotion, linearindices, tail, to_shape,
_msk_end, unsafe_bitgetindex, bitcache_chunks, bitcache_size, dumpbitcache,
isoperator
import Base: broadcast, broadcast!
Expand Down Expand Up @@ -507,7 +507,7 @@ end
else
# This element type doesn't fit in B. Allocate a new B with wider eltype,
# copy over old values, and continue
newB = Base.similar(B, typejoin(eltype(B), S))
newB = Base.similar(B, promote_type(ExactPromotion(), eltype(B), S))
for II in Iterators.take(iter, count)
newB[II] = B[II]
end
Expand Down
4 changes: 3 additions & 1 deletion base/dict.jl
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,9 @@ function grow_to!(dest::AbstractDict{K,V}, itr, st) where V where K
if isa(k,K) && isa(v,V)
dest[k] = v
else
new = empty(dest, typejoin(K,typeof(k)), typejoin(V,typeof(v)))
new = empty(dest,
promote_type(ExactPromotion(),K,typeof(k)),
promote_type(ExactPromotion(),V,typeof(v)))
merge!(new, dest)
new[k] = v
return grow_to!(new, itr, st)
Expand Down
13 changes: 12 additions & 1 deletion base/float.jl
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,18 @@ A not-a-number value of type [`Float64`](@ref).
"""
const NaN = NaN64

## conversions to floating-point ##
## promotions and conversions to floating-point ##

promote_rule(::ExactPromotion, ::Type{Float16}, ::Union{Type{Int8}, Type{UInt8}}) = Float16
promote_rule(::ExactPromotion, ::Type{Float32}, ::Union{Type{Int8}, Type{UInt8}, Type{Int16}, Type{UInt16}}) = Float32
promote_rule(::ExactPromotion, ::Type{Float64}, ::Union{Type{Int8}, Type{UInt8}, Type{Int16}, Type{UInt16}, Type{Int32}, Type{UInt32}}) = Float64

promote_rule(::ExactPromotion, ::Type{Float16}, ::Union{Type{Int16}, Type{UInt16}, Type{Int32}, Type{UInt32}}) = Float32
promote_rule(::ExactPromotion, ::Type{Float32}, ::Union{Type{Int32}, Type{UInt32}}) = Float64

promote_rule(::ExactPromotion, ::Type{Float32}, ::Type{Float16}) = Float32
promote_rule(::ExactPromotion, ::Type{Float64}, ::Union{Type{Float16}, Type{Float32}}) = Float64

Float16(x::Integer) = convert(Float16, convert(Float32, x))
for t in (Int8, Int16, Int32, Int64, Int128, UInt8, UInt16, UInt32, UInt64, UInt128)
@eval promote_rule(::Type{Float16}, ::Type{$t}) = Float16
Expand Down
2 changes: 1 addition & 1 deletion base/gmp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ Float64(n::BigInt) = Float64(n, RoundNearest)
Float32(n::BigInt) = Float32(n, RoundNearest)
Float16(n::BigInt) = Float16(n, RoundNearest)

promote_rule(::Type{BigInt}, ::Type{<:Integer}) = BigInt
promote_rule(::Base.ExactPromotion, ::Type{BigInt}, ::Type{<:Integer}) = BigInt

"""
big(x)
Expand Down
17 changes: 17 additions & 0 deletions base/int.jl
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,23 @@ end

## integer promotions ##

# ExactPromotion
# with same signedness or when unsigned type is smaller, promote to larger signed type
promote_rule(::ExactPromotion, ::Type{Int16}, ::Union{Type{Int8}, Type{UInt8}}) = Int16
promote_rule(::ExactPromotion, ::Type{Int32}, ::Union{Type{Int16}, Type{Int8}, Type{UInt16}, Type{UInt8}}) = Int32
promote_rule(::ExactPromotion, ::Type{Int64}, ::Union{Type{Int16}, Type{Int32}, Type{Int8}, Type{UInt16}, Type{UInt32}, Type{UInt8}}) = Int64
promote_rule(::ExactPromotion, ::Type{Int128}, ::Union{Type{Int16}, Type{Int32}, Type{Int64}, Type{Int8}, Type{UInt16}, Type{UInt32}, Type{UInt64}, Type{UInt8}}) = Int128
promote_rule(::ExactPromotion, ::Type{UInt16}, ::Type{UInt8}) = UInt16
promote_rule(::ExactPromotion, ::Type{UInt32}, ::Union{Type{UInt16}, Type{UInt8}}) = UInt32
promote_rule(::ExactPromotion, ::Type{UInt64}, ::Union{Type{UInt16}, Type{UInt32}, Type{UInt8}}) = UInt64
promote_rule(::ExactPromotion, ::Type{UInt128}, ::Union{Type{UInt16}, Type{UInt32}, Type{UInt64}, Type{UInt8}}) = UInt128
# with mixed signedness when unsigned type is not smaller, promote to another larger signed type
promote_rule(::ExactPromotion, ::Type{UInt8}, ::Type{Int8}) = Int16
promote_rule(::ExactPromotion, ::Type{UInt16}, ::Union{Type{Int16}, Type{Int8}}) = Int32
promote_rule(::ExactPromotion, ::Type{UInt32}, ::Union{Type{Int32}, Type{Int16}, Type{Int8}}) = Int64
promote_rule(::ExactPromotion, ::Type{UInt64}, ::Union{Type{Int16}, Type{Int32}, Type{Int64}, Type{Int8}}) = Int128

# DefaultPromotion
# with different sizes, promote to larger type
promote_rule(::Type{Int16}, ::Union{Type{Int8}, Type{UInt8}}) = Int16
promote_rule(::Type{Int32}, ::Union{Type{Int16}, Type{Int8}, Type{UInt16}, Type{UInt8}}) = Int32
Expand Down
23 changes: 17 additions & 6 deletions base/missing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,23 @@ nonmissingtype(::Type{Missing}) = Union{}
nonmissingtype(::Type{T}) where {T} = T
nonmissingtype(::Type{Any}) = Any

promote_rule(::Type{Missing}, ::Type{T}) where {T} = Union{T, Missing}
promote_rule(::Type{Union{S,Missing}}, ::Type{T}) where {T,S} = Union{promote_type(T, S), Missing}
promote_rule(::Type{Any}, ::Type{T}) where {T} = Any
promote_rule(::Type{Any}, ::Type{Missing}) = Any
promote_rule(::Type{Missing}, ::Type{Any}) = Any
promote_rule(::Type{Missing}, ::Type{Missing}) = Missing
# Both rules need to be defined for types which implement
# DefaultPromotion but not ExactPromotion
for P in (:DefaultPromotion, :ExactPromotion), U in (:Nothing, :Missing)
@eval begin
promote_rule(::$P, ::Type{$U}, ::Type{T}) where {T} = Union{T, $U}
promote_rule(::$P, ::Type{Union{S,$U}}, ::Type{T}) where {T,S} = Union{promote_type($P(), T, S), $U}
promote_rule(::$P, ::Type{Any}, ::Type{$U}) = Any
promote_rule(::$P, ::Type{$U}, ::Type{Any}) = Any
promote_rule(::$P, ::Type{$U}, ::Type{$U}) = U
end
end
# To fix ambiguities
# FIXME: needed?
promote_rule(::DefaultPromotion, ::Type{Union{Nothing, Missing}}, ::Type{T}) where {T} =
Union{Nothing, Missing, T}
promote_rule(::ExactPromotion, ::Type{Union{Nothing, Missing}}, ::Type{T}) where {T} =
Union{Nothing, Missing, T}

convert(::Type{Union{T, Missing}}, x) where {T} = convert(T, x)
# To fix ambiguities
Expand Down
3 changes: 3 additions & 0 deletions base/namedtuple.jl
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ indexed_next(t::NamedTuple, i::Int, state) = (getfield(t, i), i+1)
isempty(::NamedTuple{()}) = true
isempty(::NamedTuple) = false

promote_rule(::ExactPromotion, ::Type{NamedTuple{n, S}}, ::Type{NamedTuple{n, T}}) where {n, S, T} =
NamedTuple{n, promote_type(ExactPromotion(), S, T)}

convert(::Type{NamedTuple{names,T}}, nt::NamedTuple{names,T}) where {names,T} = nt
convert(::Type{NamedTuple{names}}, nt::NamedTuple{names}) where {names} = nt

Expand Down
Loading