Skip to content

Commit

Permalink
Fix promote_type(Union{}, Missing/Nothing) and add tests
Browse files Browse the repository at this point in the history
Without this Dict(1=>missing, 2=>2) returns a Dict{Int,Any}.
  • Loading branch information
nalimilan committed Feb 10, 2018
1 parent 2e49e39 commit ea96757
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 8 deletions.
8 changes: 4 additions & 4 deletions base/promotion.jl
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,13 @@ Falls back to [`typejoin`](@ref).
promote_typejoin(@nospecialize(a), @nospecialize(b)) =
(@_pure_meta; join_types(a, b, promote_typejoin))
promote_typejoin(::Type{Nothing}, ::Type{T}) where {T} =
isconcretetype(T) ? Union{T, Nothing} : Any
isconcretetype(T) || T === Union{} ? Union{T, Nothing} : Any
promote_typejoin(::Type{T}, ::Type{Nothing}) where {T} =
isconcretetype(T) ? Union{T, Nothing} : Any
isconcretetype(T) || T === Union{} ? Union{T, Nothing} : Any
promote_typejoin(::Type{Missing}, ::Type{T}) where {T} =
isconcretetype(T) ? Union{T, Missing} : Any
isconcretetype(T) || T === Union{} ? Union{T, Missing} : Any
promote_typejoin(::Type{T}, ::Type{Missing}) where {T} =
isconcretetype(T) ? Union{T, Missing} : Any
isconcretetype(T) || T === Union{} ? Union{T, Missing} : Any
promote_typejoin(::Type{Nothing}, ::Type{Missing}) = Union{Nothing, Missing}
promote_typejoin(::Type{Missing}, ::Type{Nothing}) = Union{Nothing, Missing}
promote_typejoin(::Type{Nothing}, ::Type{Nothing}) = Nothing
Expand Down
2 changes: 2 additions & 0 deletions test/core.jl
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@ for T in (Nothing, Missing)
@test Base.promote_typejoin(Int, String) === Any
@test Base.promote_typejoin(Int, Union{Float64, T}) === Any
@test Base.promote_typejoin(Int, Union{String, T}) === Any
@test Base.promote_typejoin(T, Union{}) === T
@test Base.promote_typejoin(Union{}, T) === T
end

@test promote_type(Bool,Bottom) === Bool
Expand Down
10 changes: 6 additions & 4 deletions test/dict.jl
Original file line number Diff line number Diff line change
Expand Up @@ -165,10 +165,12 @@ end
@test Dict(1=>1, 2.0=>2) isa Dict{Real,Int}
@test Dict(1=>1.0, 2.0=>2) isa Dict{Real,Real}

@test Dict(1=>1, 2=>missing) isa Dict{Int,Union{Int,Missing}}
@test Dict(1=>1, 2=>nothing) isa Dict{Int,Union{Int,Nothing}}
@test Dict(1=>1, missing=>2) isa Dict{Union{Int,Missing},Int}
@test Dict(1=>1, nothing=>2) isa Dict{Union{Int,Nothing},Int}
for T in (Nothing, Missing)
@test Dict(1=>1, 2=>T()) isa Dict{Int,Union{Int,T}}
@test Dict(1=>T(), 2=>2) isa Dict{Int,Union{Int,T}}
@test Dict(1=>1, T()=>2) isa Dict{Union{Int,T},Int}
@test Dict(T()=>1, 2=>2) isa Dict{Union{Int,T},Int}
end
end

@test_throws KeyError Dict("a"=>2)[Base.secret_table_token]
Expand Down

0 comments on commit ea96757

Please sign in to comment.