Skip to content

Commit e7b5082

Browse files
authored
Change how we handle unions of missings and other types in unitary functions (#53616)
1 parent bcbe9fe commit e7b5082

File tree

4 files changed

+9
-18
lines changed

4 files changed

+9
-18
lines changed

base/complex.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ Float64
120120
real(T::Type) = typeof(real(zero(T)))
121121
real(::Type{T}) where {T<:Real} = T
122122
real(C::Type{<:Complex}) = fieldtype(C, 1)
123-
real(::Type{Union{}}, slurp...) = Union{}(im)
123+
real(::Type{Union{}}, slurp...) = Union{}
124124

125125
"""
126126
isreal(x)::Bool
@@ -188,6 +188,7 @@ Union{Missing, Complex{Int64}}
188188
"""
189189
complex(::Type{T}) where {T<:Real} = Complex{T}
190190
complex(::Type{Complex{T}}) where {T<:Real} = Complex{T}
191+
complex(::Type{Union{}}, slurp...) = Union{}
191192

192193
flipsign(x::Complex, y::Real) = ifelse(signbit(y), -x, x)
193194

base/float.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,7 @@ Float64
391391
"""
392392
float(::Type{T}) where {T<:Number} = typeof(float(zero(T)))
393393
float(::Type{T}) where {T<:AbstractFloat} = T
394-
float(::Type{Union{}}, slurp...) = Union{}(0.0)
394+
float(::Type{Union{}}, slurp...) = Union{}
395395

396396
"""
397397
unsafe_trunc(T, x)

base/missing.jl

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -102,19 +102,14 @@ for f in (:(!), :(~), :(+), :(-), :(*), :(&), :(|), :(xor),
102102
:(real), :(imag), :(sign), :(inv))
103103
@eval ($f)(::Missing) = missing
104104
end
105-
for f in (:(Base.zero), :(Base.one), :(Base.oneunit))
105+
for f in (:zero, :one, :oneunit)
106+
@eval ($f)(::Type{Any}) = throw(MethodError($f, (Any,))) # To prevent StackOverflowError
106107
@eval ($f)(::Type{Missing}) = missing
107-
@eval function $(f)(::Type{Union{T, Missing}}) where T
108-
T === Any && throw(MethodError($f, (Any,))) # To prevent StackOverflowError
109-
$f(T)
110-
end
108+
@eval ($f)(::Type{T}) where {T>:Missing} = $f(nonmissingtype_checked(T))
111109
end
112-
for f in (:(Base.float), :(Base.complex))
113-
@eval $f(::Type{Missing}) = Missing
114-
@eval function $f(::Type{Union{T, Missing}}) where T
115-
T === Any && throw(MethodError($f, (Any,))) # To prevent StackOverflowError
116-
Union{$f(T), Missing}
117-
end
110+
for f in (:float, :real, :complex)
111+
@eval ($f)(::Type{Any}) = throw(MethodError($f, (Any,))) # To prevent StackOverflowError
112+
@eval ($f)(::Type{T}) where {T>:Missing} = Union{$f(nonmissingtype(T)), Missing}
118113
end
119114

120115
# Binary operators/functions

test/ambiguous.jl

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -354,11 +354,6 @@ end
354354
pop!(need_to_handle_undef_sparam, which(Base._cat, Tuple{Any, AbstractArray}))
355355
pop!(need_to_handle_undef_sparam, which(Base.byteenv, (Union{AbstractArray{Pair{T,V}, 1}, Tuple{Vararg{Pair{T,V}}}} where {T<:AbstractString,V},)))
356356
pop!(need_to_handle_undef_sparam, which(Base.float, Tuple{AbstractArray{Union{Missing, T},N} where {T, N}}))
357-
pop!(need_to_handle_undef_sparam, which(Base.float, Tuple{Type{Union{Missing, T}} where T}))
358-
pop!(need_to_handle_undef_sparam, which(Base.complex, Tuple{Type{Union{Missing, T}} where T}))
359-
pop!(need_to_handle_undef_sparam, which(Base.zero, Tuple{Type{Union{Missing, T}} where T}))
360-
pop!(need_to_handle_undef_sparam, which(Base.one, Tuple{Type{Union{Missing, T}} where T}))
361-
pop!(need_to_handle_undef_sparam, which(Base.oneunit, Tuple{Type{Union{Missing, T}} where T}))
362357
@test isempty(need_to_handle_undef_sparam)
363358
end
364359
end

0 commit comments

Comments
 (0)