From b9617a334d994342b03ed1c9e1fb364543e9530f Mon Sep 17 00:00:00 2001 From: mtfishman Date: Thu, 16 Jan 2025 18:11:05 -0500 Subject: [PATCH] Fix promotion between UnspecifiedZero and Bool --- Project.toml | 2 +- src/unspecifiedzero.jl | 6 ++++++ test/test_basics.jl | 5 +++++ 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 71d9ac3..c9aa52c 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "UnspecifiedTypes" uuid = "42b3faec-625b-4613-8ddc-352bf9672b8d" authors = ["ITensor developers and contributors"] -version = "0.1.2" +version = "0.1.3" [compat] julia = "1.10" diff --git a/src/unspecifiedzero.jl b/src/unspecifiedzero.jl index 47ede0e..adf81fd 100644 --- a/src/unspecifiedzero.jl +++ b/src/unspecifiedzero.jl @@ -35,3 +35,9 @@ Base.:-(::UnspecifiedZero) = UnspecifiedZero() Base.promote_rule(::Type{<:UnspecifiedZero}, t::Type) = t Base.promote_rule(::Type{<:UnspecifiedZero}, ::Type{<:UnspecifiedZero}) = UnspecifiedZero Base.promote_type(::Type{<:Complex{<:UnspecifiedZero}}, t::Type) = complex(t) + +# Avoid circular definition in `Base.promote_type`, since +# Base defines `promote_rule(::Type{Bool}, ::Type{T}) where {T<:Number} = T`. +# I.e. normally numbers take precedence over `Bool`, but that +# isn't the case for `UnspecifiedZero`. +Base.promote_rule(::Type{Bool}, ::Type{<:UnspecifiedZero}) = Union{} diff --git a/test/test_basics.jl b/test/test_basics.jl index dbc4214..25c0bd0 100644 --- a/test/test_basics.jl +++ b/test/test_basics.jl @@ -13,4 +13,9 @@ using Test: @testset, @test @test x isa UnspecifiedZero @test x === UnspecifiedZero() end + + # Normally, any number type takes precedence over + # `Bool`, check this isn't the case with `UnspecifiedZero`. + @test promote_type(UnspecifiedZero, Bool) === Bool + @test promote_type(Bool, UnspecifiedZero) === Bool end