Precompilation problem setting const Ref{Ptr} in a Module

question
package
module
precompilation

#1

Hi everyone,

I’m working on a PR to extend MPI.jl, which is a Julia package ccalling into a C/Fortran library.

I need to store inside my module a constant Ref{Ptr{Cvoid}}. Since the precompiler actually clears the value of all pointers, I learned that I must do it in the __init__ method of my Module.

Unfortunately, this does not seem to work unless I use an eval call, which breaks incremental compilation, and as such I want to avoid it.

For example, consider the following package:

bash> mkdir -p MyModule/src && echo "
module MyModule
 const a = Ref{Ptr{Cvoid}}()
 const b = Ref{Ptr{Cvoid}}()

 function __init__()
     a[] = Ptr{Cvoid}(1)
     eval(:(b[] = 1))
 end
end" >> MyModule/src/MyModule.jl && cd MyModule

then, if you dev it and using it (which is necessary to trigger a precompilation), you will see that a has been reset:

julia> ] dev .
julia> using MyModule
julia> MyModule.a
Base.RefValue{Ptr{Nothing}}(Ptr{Nothing} @0x0000000000000000)
julia> MyModule.b
Base.RefValue{Ptr{Nothing}}(Ptr{Nothing} @0x0000000000000001)

How can I set this variable without using an eval call?


#2

Just don’t use constants and call them pointers—either use runtime values or call them integers. It’s probably quite bad that we’re rewriting even the source code constant 1 into a null pointer, but that’s usually not observed in practice