From 22987e393241514ee16a7d04e7db053ee2833953 Mon Sep 17 00:00:00 2001 From: Rafael Fourquet Date: Fri, 8 Sep 2017 08:05:11 +0200 Subject: [PATCH] fix #19182: convert([Un]Signed, x::BigInt) (#23474) --- base/gmp.jl | 5 +++-- test/bigint.jl | 7 +++++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/base/gmp.jl b/base/gmp.jl index 2b2afc9fcc558..04e627881c8b9 100644 --- a/base/gmp.jl +++ b/base/gmp.jl @@ -229,6 +229,7 @@ widen(::Type{BigInt}) = BigInt signed(x::BigInt) = x convert(::Type{BigInt}, x::BigInt) = x +convert(::Type{Signed}, x::BigInt) = x hastypemax(::Type{BigInt}) = false @@ -323,7 +324,7 @@ end rem(x::Integer, ::Type{BigInt}) = convert(BigInt, x) -function convert(::Type{T}, x::BigInt) where T<:Unsigned +function convert(::Type{T}, x::BigInt) where T<:Base.BitUnsigned if sizeof(T) < sizeof(Limb) convert(T, convert(Limb,x)) else @@ -332,7 +333,7 @@ function convert(::Type{T}, x::BigInt) where T<:Unsigned end end -function convert(::Type{T}, x::BigInt) where T<:Signed +function convert(::Type{T}, x::BigInt) where T<:Base.BitSigned n = abs(x.size) if sizeof(T) < sizeof(Limb) SLimb = typeof(Signed(one(Limb))) diff --git a/test/bigint.jl b/test/bigint.jl index 6dadc6556cece..180fca5ee4d33 100644 --- a/test/bigint.jl +++ b/test/bigint.jl @@ -374,3 +374,10 @@ end @test BigInt <: Signed @test big(1) isa Signed + +let x = big(1) + @test signed(x) === x + @test convert(Signed, x) === x + @test Signed(x) === x + @test_throws MethodError convert(Unsigned, x) # could change in the future +end