From c28a9de26dd467ad847b0f694419e061df205574 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20G=C3=B6ttgens?= Date: Sat, 18 May 2024 16:42:14 +0200 Subject: [PATCH] Make `deepcopy_internal` inferrable for BigInt and BigFloat (#54496) Co-authored-by: Lilith Orion Hafner --- base/gmp.jl | 2 +- base/mpfr.jl | 2 +- test/copy.jl | 5 +++++ 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/base/gmp.jl b/base/gmp.jl index 1f5b64a80f63b..e3e90ce34f82c 100644 --- a/base/gmp.jl +++ b/base/gmp.jl @@ -832,7 +832,7 @@ Base.add_with_overflow(a::BigInt, b::BigInt) = a + b, false Base.sub_with_overflow(a::BigInt, b::BigInt) = a - b, false Base.mul_with_overflow(a::BigInt, b::BigInt) = a * b, false -Base.deepcopy_internal(x::BigInt, stackdict::IdDict) = get!(() -> MPZ.set(x), stackdict, x) +Base.deepcopy_internal(x::BigInt, stackdict::IdDict) = get!(() -> MPZ.set(x), stackdict, x)::BigInt ## streamlined hashing for BigInt, by avoiding allocation from shifts ## diff --git a/base/mpfr.jl b/base/mpfr.jl index e61f115b35093..ed3ea5937ce87 100644 --- a/base/mpfr.jl +++ b/base/mpfr.jl @@ -1199,7 +1199,7 @@ function Base.deepcopy_internal(x::BigFloat, stackdict::IdDict) y = _BigFloat(x.prec, x.sign, x.exp, d′) #ccall((:mpfr_custom_move,libmpfr), Cvoid, (Ref{BigFloat}, Ptr{Limb}), y, d) # unnecessary return y - end + end::BigFloat end function decompose(x::BigFloat)::Tuple{BigInt, Int, Int} diff --git a/test/copy.jl b/test/copy.jl index aa7654927454a..e83f0519d47f4 100644 --- a/test/copy.jl +++ b/test/copy.jl @@ -248,6 +248,11 @@ end @test (@inferred Base.deepcopy_internal(zeros(), IdDict())) == zeros() end +@testset "deepcopy_internal big" begin + @inferred Base.deepcopy_internal(big(1), IdDict()) + @inferred Base.deepcopy_internal(big(1.0), IdDict()) +end + @testset "`copyto!`'s unaliasing" begin a = view([1:3;], :) @test copyto!(a, 2, a, 1, 2) == [1;1:2;]