crystal-book icon indicating copy to clipboard operation
crystal-book copied to clipboard

Documentation on `fun` argument conversion is incorrect

Open HertzDevil opened this issue 1 year ago • 1 comments

The docs say:

Note that there are no implicit conversions (except to_unsafe, which is explained later) when invoking a C function: you must pass the exact type that is expected. For integers and floats you can use the various to_... methods.

But this is not true:

lib LibC
  alias Int = Int32
  fun exit(status : Int) : NoReturn
end

LibC.exit(69_u8)   # okay
LibC.exit(69_i128) # okay

More surprising is that it is okay to pass a BigInt here too, but not its unsafe counterpart:

require "big"

LibC.exit(69.to_big_i)           # okay
LibC.exit(69.to_big_i.to_unsafe) # Error: argument 'x0' of 'LibC#exit' must be Int32, not Pointer(LibGMP::MPZ)

So something else must be going on.

HertzDevil avatar Apr 05 '23 22:04 HertzDevil