From a887fabefc9d5c01374d16e38001b9595da2f361 Mon Sep 17 00:00:00 2001 From: Jeff Bezanson Date: Thu, 10 May 2018 17:04:17 -0400 Subject: [PATCH] don't allow promoting `Some{T}` and `Some{S}` add promotion rule for `Union{Nothing, Missing, T}` fixes the bug part of #26927 --- base/missing.jl | 2 ++ base/some.jl | 2 +- test/some.jl | 8 +++++++- 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/base/missing.jl b/base/missing.jl index 6fb7556aa9af49..dd6ec8d8f0a7a2 100644 --- a/base/missing.jl +++ b/base/missing.jl @@ -35,6 +35,8 @@ end promote_rule(::Type{Union{Nothing, Missing}}, ::Type{Any}) = Any promote_rule(::Type{Union{Nothing, Missing}}, ::Type{T}) where {T} = Union{Nothing, Missing, T} +promote_rule(::Type{Union{Nothing, Missing, S}}, ::Type{T}) where {T,S} = + Union{Nothing, Missing, promote_type(T, S)} convert(::Type{Union{T, Missing}}, x) where {T} = convert(T, x) # To fix ambiguities diff --git a/base/some.jl b/base/some.jl index f75a493d91703d..851ee2a53f2544 100644 --- a/base/some.jl +++ b/base/some.jl @@ -12,7 +12,7 @@ struct Some{T} value::T end -promote_rule(::Type{Some{S}}, ::Type{Some{T}}) where {S,T} = Some{promote_type(S, T)} +promote_rule(::Type{Some{T}}, ::Type{Some{S}}) where {T, S<:T} = Some{T} promote_rule(::Type{Some{T}}, ::Type{Nothing}) where {T} = Union{Some{T}, Nothing} convert(::Type{Some{T}}, x::Some) where {T} = Some{T}(convert(T, x.value)) diff --git a/test/some.jl b/test/some.jl index a4d37d8df03892..4f9f407a1728b5 100644 --- a/test/some.jl +++ b/test/some.jl @@ -2,7 +2,9 @@ ## promote() -@test promote_type(Some{Int}, Some{Float64}) === Some{Float64} +@test promote_type(Some{Int}, Some{Float64}) == Some +@test promote_type(Some{Int}, Some{Real}) == Some{Real} +@test promote_type(Some{Int}, Nothing) == Union{Some{Int},Nothing} ## convert() @@ -75,6 +77,10 @@ for v in (nothing, missing) @test coalesce(v, nothing) === nothing @test coalesce(v, missing, v) === v @test coalesce(v, nothing, v) === v + + # issue #26927 + @test all(coalesce.([missing, nothing, Some(nothing), Some(missing)], "replacement") .=== + [ "replacement", "replacement", nothing, missing ]) end # notnothing()