Skip to content

Commit 6dca4f4

Browse files
authored
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}`).
1 parent 9339dea commit 6dca4f4

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
@@ -214,6 +214,8 @@ end
214214
Base.unsafe_convert(::Type{Ref{BigFloat}}, x::Ptr{BigFloat}) = error("not compatible with mpfr")
215215
Base.unsafe_convert(::Type{Ref{BigFloat}}, x::Ref{BigFloat}) = error("not compatible with mpfr")
216216
Base.cconvert(::Type{Ref{BigFloat}}, x::BigFloat) = x.d # BigFloatData is the Ref type for BigFloat
217+
Base.cconvert(::Type{Ref{BigFloat}}, x::Number) = convert(BigFloat, x).d # avoid default conversion to Ref(BigFloat(x))
218+
Base.cconvert(::Type{Ref{BigFloat}}, x::Ref{BigFloat}) = x[].d
217219
function Base.unsafe_convert(::Type{Ref{BigFloat}}, x::BigFloatData)
218220
d = getfield(x, :d)
219221
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)