Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[NDTensors] Fix type promotion of EmptyNumber in Julia 1.11 #1541

Merged
merged 3 commits into from
Oct 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion NDTensors/Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "NDTensors"
uuid = "23ae76d9-e61a-49c4-8f12-3f1a16adf9cf"
authors = ["Matthew Fishman <[email protected]>"]
version = "0.3.43"
version = "0.3.44"

[deps]
Accessors = "7d9f7c33-5ae7-4f3b-8dc6-eff91059b697"
Expand Down
2 changes: 0 additions & 2 deletions NDTensors/src/empty/EmptyTensor.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
8 changes: 8 additions & 0 deletions NDTensors/src/emptynumber.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
13 changes: 10 additions & 3 deletions NDTensors/test/test_emptynumber.jl
Original file line number Diff line number Diff line change
@@ -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 𝟎 + 𝟎 == 𝟎
Expand Down
Loading