Skip to content

Commit ebef610

Browse files
stevengjKristofferC
authored andcommitted
cconvert(Ref{BigFloat}, x) should return BigFloatData (#57367)
#55906 changed `cconvert(Ref{BigFloat}, x::BigFloat)` to return `x.d`, but neglected to do so for other types of `x`, where it still returns a `Ref{BigFloat}` and hence is now returning the wrong type for `ccall`. Not only does this break backwards compatibility (JuliaMath/SpecialFunctions.jl#485), but it also seems simply wrong: the *whole* job of `cconvert` is to convert objects to the correct type for use with `ccall`. This PR does so (at least for `Number` and `Ref{BigFloat}`). (cherry picked from commit 6dca4f4)
1 parent ba4290b commit ebef610

File tree

2 files changed

+9
-0
lines changed

2 files changed

+9
-0
lines changed

base/mpfr.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,8 @@ end
211211
Base.unsafe_convert(::Type{Ref{BigFloat}}, x::Ptr{BigFloat}) = error("not compatible with mpfr")
212212
Base.unsafe_convert(::Type{Ref{BigFloat}}, x::Ref{BigFloat}) = error("not compatible with mpfr")
213213
Base.cconvert(::Type{Ref{BigFloat}}, x::BigFloat) = x.d # BigFloatData is the Ref type for BigFloat
214+
Base.cconvert(::Type{Ref{BigFloat}}, x::Number) = convert(BigFloat, x).d # avoid default conversion to Ref(BigFloat(x))
215+
Base.cconvert(::Type{Ref{BigFloat}}, x::Ref{BigFloat}) = x[].d
214216
function Base.unsafe_convert(::Type{Ref{BigFloat}}, x::BigFloatData)
215217
d = getfield(x, :d)
216218
p = Base.unsafe_convert(Ptr{Limb}, d)

test/mpfr.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1097,3 +1097,10 @@ end
10971097
end
10981098
end
10991099
end
1100+
1101+
# BigFloatData is the Ref type for BigFloat in ccall:
1102+
@testset "cconvert(Ref{BigFloat}, x)" begin
1103+
for x in (1.0, big"1.0", Ref(big"1.0"))
1104+
@test Base.cconvert(Ref{BigFloat}, x) isa Base.MPFR.BigFloatData
1105+
end
1106+
end

0 commit comments

Comments
 (0)