Skip to content

Commit

Permalink
don't allow promoting Some{T} and Some{S}
Browse files Browse the repository at this point in the history
add promotion rule for `Union{Nothing, Missing, T}`

fixes the bug part of #26927
  • Loading branch information
JeffBezanson committed May 10, 2018
1 parent ee4b18f commit a887fab
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 2 deletions.
2 changes: 2 additions & 0 deletions base/missing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion base/some.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
8 changes: 7 additions & 1 deletion test/some.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit a887fab

Please sign in to comment.