From 5d279b1b01b9c3d19cf489ed8ab9d764ba8eaba1 Mon Sep 17 00:00:00 2001 From: Frames White Date: Wed, 6 Mar 2024 10:47:34 +0800 Subject: [PATCH] change how we handle unions of missings and other types in unitary functions --- base/missing.jl | 18 ++++++------------ test/ambiguous.jl | 5 ----- 2 files changed, 6 insertions(+), 17 deletions(-) diff --git a/base/missing.jl b/base/missing.jl index ce174edc297e3f..42fd9cb591854c 100644 --- a/base/missing.jl +++ b/base/missing.jl @@ -100,19 +100,13 @@ for f in (:(!), :(~), :(+), :(-), :(*), :(&), :(|), :(xor), :(real), :(imag), :(sign), :(inv)) @eval ($f)(::Missing) = missing end -for f in (:(Base.zero), :(Base.one), :(Base.oneunit)) - @eval ($f)(::Type{Missing}) = missing - @eval function $(f)(::Type{Union{T, Missing}}) where T - T === Any && throw(MethodError($f, (Any,))) # To prevent StackOverflowError - $f(T) - end +for f in (:zero, :one, :oneunit) + @eval ($f)(::Type{Any}) = throw(MethodError($f, (Any,))) # To prevent StackOverflowError + @eval ($f)(::Type{T}) where {T>:Missing} = $f(nonmissingtype_checked(T)) end -for f in (:(Base.float), :(Base.complex)) - @eval $f(::Type{Missing}) = Missing - @eval function $f(::Type{Union{T, Missing}}) where T - T === Any && throw(MethodError($f, (Any,))) # To prevent StackOverflowError - Union{$f(T), Missing} - end +for f in (:float, :complex) + @eval ($f)(::Type{Any}) = throw(MethodError($f, (Any,))) # To prevent StackOverflowError + @eval ($f)(::Type{T}) where {T>:Missing} = Union{$f(nonmissingtype_checked(T)), Missing} end # Binary operators/functions diff --git a/test/ambiguous.jl b/test/ambiguous.jl index 748660cc9c981f..20253bb092d113 100644 --- a/test/ambiguous.jl +++ b/test/ambiguous.jl @@ -351,11 +351,6 @@ end pop!(need_to_handle_undef_sparam, which(Base._cat, Tuple{Any, AbstractArray})) pop!(need_to_handle_undef_sparam, which(Base.byteenv, (Union{AbstractArray{Pair{T,V}, 1}, Tuple{Vararg{Pair{T,V}}}} where {T<:AbstractString,V},))) pop!(need_to_handle_undef_sparam, which(Base.float, Tuple{AbstractArray{Union{Missing, T},N} where {T, N}})) - pop!(need_to_handle_undef_sparam, which(Base.float, Tuple{Type{Union{Missing, T}} where T})) - pop!(need_to_handle_undef_sparam, which(Base.complex, Tuple{Type{Union{Missing, T}} where T})) - pop!(need_to_handle_undef_sparam, which(Base.zero, Tuple{Type{Union{Missing, T}} where T})) - pop!(need_to_handle_undef_sparam, which(Base.one, Tuple{Type{Union{Missing, T}} where T})) - pop!(need_to_handle_undef_sparam, which(Base.oneunit, Tuple{Type{Union{Missing, T}} where T})) @test isempty(need_to_handle_undef_sparam) end end