From 020efa3aeee0170c532de2bdae84ef3b555fd354 Mon Sep 17 00:00:00 2001 From: Matt Fishman Date: Fri, 11 Oct 2024 20:18:39 -0400 Subject: [PATCH] [NDTensors] Fix type promotion of EmptyNumber in Julia 1.11 (#1541) * [NDTensors] Fix type promotion of EmptyNumber in Julia 1.11 * [NDTensors] Bump to v0.3.44 --- NDTensors/Project.toml | 2 +- NDTensors/src/empty/EmptyTensor.jl | 2 -- NDTensors/src/emptynumber.jl | 8 ++++++++ NDTensors/test/test_emptynumber.jl | 13 ++++++++++--- 4 files changed, 19 insertions(+), 6 deletions(-) diff --git a/NDTensors/Project.toml b/NDTensors/Project.toml index 65771b0c5a..5fa45ba4d6 100644 --- a/NDTensors/Project.toml +++ b/NDTensors/Project.toml @@ -1,7 +1,7 @@ name = "NDTensors" uuid = "23ae76d9-e61a-49c4-8f12-3f1a16adf9cf" authors = ["Matthew Fishman "] -version = "0.3.43" +version = "0.3.44" [deps] Accessors = "7d9f7c33-5ae7-4f3b-8dc6-eff91059b697" diff --git a/NDTensors/src/empty/EmptyTensor.jl b/NDTensors/src/empty/EmptyTensor.jl index 3fcbf4ed63..f5e04e0652 100644 --- a/NDTensors/src/empty/EmptyTensor.jl +++ b/NDTensors/src/empty/EmptyTensor.jl @@ -152,8 +152,6 @@ end setindex!!(T::EmptyTensor, x, I...) = setindex(T, x, I...) -promote_rule(::Type{EmptyNumber}, ::Type{T}) where {T<:Number} = T - function promote_rule( ::Type{T1}, ::Type{T2} ) where {T1<:EmptyStorage{EmptyNumber},T2<:TensorStorage} diff --git a/NDTensors/src/emptynumber.jl b/NDTensors/src/emptynumber.jl index d84abb52ca..1d9799b740 100644 --- a/NDTensors/src/emptynumber.jl +++ b/NDTensors/src/emptynumber.jl @@ -18,6 +18,14 @@ convert(::Type{T}, x::EmptyNumber) where {T<:Number} = T(zero(T)) # This helps with defining `norm` of `EmptyStorage{EmptyNumber}`. AbstractFloat(::EmptyNumber) = zero(AbstractFloat) +# Extra definitions fix ambiguity errors. +Base.promote_rule(::Type{EmptyNumber}, T::Type{<:Number}) = T +Base.promote_rule(T::Type{<:Number}, ::Type{EmptyNumber}) = T +Base.promote_rule(::Type{EmptyNumber}, ::Type{Bool}) = Bool +Base.promote_rule(::Type{Bool}, ::Type{EmptyNumber}) = Bool +Base.promote_rule(::Type{EmptyNumber}, T::Type{Complex{R}}) where {R<:Real} = T +Base.promote_rule(T::Type{Complex{R}}, ::Type{EmptyNumber}) where {R<:Real} = T + # Basic arithmetic (::EmptyNumber + ::EmptyNumber) = EmptyNumber() (::EmptyNumber - ::EmptyNumber) = EmptyNumber() diff --git a/NDTensors/test/test_emptynumber.jl b/NDTensors/test/test_emptynumber.jl index dc8357a115..73d82117f5 100644 --- a/NDTensors/test/test_emptynumber.jl +++ b/NDTensors/test/test_emptynumber.jl @@ -1,14 +1,21 @@ @eval module $(gensym()) -using NDTensors +using LinearAlgebra: norm +using NDTensors: EmptyNumber using Test: @testset, @test, @test_throws -const 𝟎 = NDTensors.EmptyNumber() +const 𝟎 = EmptyNumber() @testset "NDTensors.EmptyNumber" begin x = 2.3 @test complex(𝟎) == 𝟎 - @test complex(NDTensors.EmptyNumber) == Complex{NDTensors.EmptyNumber} + @test complex(EmptyNumber) == Complex{EmptyNumber} + + # Promotion + for T in (Bool, Float32, Float64, Complex{Float32}, Complex{Float64}) + @test promote_type(EmptyNumber, T) === T + @test promote_type(T, EmptyNumber) === T + end # Basic arithmetic @test 𝟎 + 𝟎 == 𝟎