Skip to content

more conservative promote_rule for Pair and Some #58836

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

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
3 changes: 0 additions & 3 deletions base/pair.jl
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,3 @@ function convert(::Type{Pair{A,B}}, x::Pair) where {A,B}
b isa B || (b = convert(B, b))
return Pair{A,B}(a, b)::Pair{A,B}
end

promote_rule(::Type{Pair{A1,B1}}, ::Type{Pair{A2,B2}}) where {A1,B1,A2,B2} =
Pair{promote_type(A1, A2), promote_type(B1, B2)}
11 changes: 8 additions & 3 deletions base/set.jl
Original file line number Diff line number Diff line change
Expand Up @@ -814,11 +814,11 @@ julia> replace([1, missing], missing=>0)
"""
function replace(A, old_new::Pair...; count::Union{Integer,Nothing}=nothing)
V = promote_valuetype(old_new...)
if count isa Nothing
T = promote_type(subtract_singletontype(eltype(A), old_new...), V)
if isnothing(count)
T = _replace_eltype(A, subtract_singletontype(eltype(A), old_new...), V)
replace_pairs!(_similar_or_copy(A, T), A, typemax(Int), old_new)
else
U = promote_type(eltype(A), V)
U = _replace_eltype(A, eltype(A), V)
replace_pairs!(_similar_or_copy(A, U), A, check_count(count), old_new)
end
end
Expand All @@ -827,6 +827,11 @@ promote_valuetype(x::Pair{K, V}) where {K, V} = V
promote_valuetype(x::Pair{K, V}, y::Pair...) where {K, V} =
promote_type(V, promote_valuetype(y...))

_replace_eltype(::Any, T::Type, S::Type) = promote_type(T, S)
function _replace_eltype(::AbstractDict, ::Type{Pair{K1,V1}}, ::Type{Pair{K2,V2}}) where {K1,V1,K2,V2}
return Pair{promote_type(K1, K2), promote_type(V1, V2)}
end

# Subtract singleton types which are going to be replaced
function subtract_singletontype(::Type{T}, x::Pair{K}) where {T, K}
if issingletontype(K)
Expand Down
2 changes: 0 additions & 2 deletions base/some.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ end

Some(::Type{T}) where {T} = Some{Type{T}}(T)

promote_rule(::Type{Some{T}}, ::Type{Some{S}}) where {T, S<:T} = Some{T}

nonnothingtype(@nospecialize(T::Type)) = typesplit(T, Nothing)
promote_rule(T::Type{Nothing}, S::Type) = Union{S, Nothing}
function promote_rule(T::Type{>:Nothing}, S::Type)
Expand Down
13 changes: 6 additions & 7 deletions test/operators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,12 @@ end
@test convert(Pair{Float64,Int}, 17 => 4711) === (17.0 => 4711)
@test convert(Pair{Any,Any}, 17 => 4711) === Pair{Any,Any}(17, 4711)
@test convert(Pair{Number,Number}, 17 => 4711) === Pair{Number,Number}(17, 4711)
@test promote(1=>1, 2=>2.0) === (1=>1.0, 2=>2.0)
@test promote(1=>1, 2.0=>2) === (1.0=>1, 2.0=>2)
@test promote(1=>1.0, 2.0=>2) === (1.0=>1.0, 2.0=>2.0)
@test promote(1=>1, :b=>2.0) === (Pair{Any,Float64}(1,1.0),Pair{Any,Float64}(:b,2.0))
@test isa([:a=>1, :b=>2], Vector{Pair{Symbol,Int}})
@test isa([:a=>1, :b=>2.0], Vector{Pair{Symbol,Float64}})
@test isa(["a"=>1, :b=>2.0], Vector{Pair{Any,Float64}})
@test_throws "promotion of types" promote(1=>1, 2=>2.0)
@test_throws "promotion of types" promote(1=>1, 2.0=>2)
@test isa([:a=>1, :b=>2], Vector{Pair{Symbol, Int}})
@test isa([:a=>1, :b=>2.0], Vector{Pair{Symbol}})
@test isa(["a"=>1, :b=>2], Vector{Pair{A, Int} where A})
@test isa(["a"=>1, :b=>2.0], Vector{Pair})

p = 1=>:foo
@test first(p) == 1
Expand Down
2 changes: 1 addition & 1 deletion test/some.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
## promote()

@test promote_type(Some{Int}, Some{Float64}) == Some
@test promote_type(Some{Int}, Some{Real}) == Some{Real}
@test promote_type(Some{Int}, Some{Real}) == Some
@test promote_type(Some{Int}, Nothing) == Union{Some{Int},Nothing}

## convert()
Expand Down
Loading