Use fixed-width integers

Even though `Int` and `isize` should be the same in practice, we can't cleanly convert, as the type information isn't quite there. And anyway, strictly speaking per the report, `Int` is only guaranteed to hold 30 bits.

Note that this is essentially unchanged even if we specify `usize_is_size_t = true` for `cbindgen`.
This commit is contained in:
George Thomas 2026-02-19 15:47:55 +00:00
parent 024b6aec87
commit 5907a064a7
6 changed files with 30 additions and 35 deletions

View File

@ -16,7 +16,6 @@ import Foreign.C
import GarnetRs qualified as Raw
import GarnetRs.Safe qualified as Raw
import HsBindgen.Runtime.PtrConst
import Unsafe.Coerce
data T = T
{ a :: Bool
@ -42,9 +41,5 @@ helloStruct = Raw.hello_struct . convertT
helloShape :: Shape -> IO ()
helloShape = Raw.hello_shape . convertShape
add :: Int -> Int -> IO Int
add a b = from <$> Raw.add (to a) (to b)
where
-- these types should be the same in practice - both are pointer-sized
to = unsafeCoerce @Int @CIntPtr
from = unsafeCoerce @CIntPtr @Int
add :: Int64 -> Int64 -> IO Int64
add = Raw.add

View File

@ -44,9 +44,9 @@ $(HsBindgen.Runtime.Internal.CAPI.addCSource (HsBindgen.Runtime.Internal.CAPI.un
, "}"
, "/* com_garnet_GarnetRs_get_add */"
, "__attribute__ ((const))"
, "intptr_t (*hs_bindgen_bbabdbe61cd1eeb2 (void)) ("
, " intptr_t arg1,"
, " intptr_t arg2"
, "int64_t (*hs_bindgen_bbabdbe61cd1eeb2 (void)) ("
, " int64_t arg1,"
, " int64_t arg2"
, ")"
, "{"
, " return &add;"
@ -118,17 +118,17 @@ foreign import ccall unsafe "hs_bindgen_bbabdbe61cd1eeb2" hs_bindgen_bbabdbe61cd
IO (Ptr.FunPtr Void)
-- __unique:__ @com_garnet_GarnetRs_get_add@
hs_bindgen_bbabdbe61cd1eeb2 :: IO (Ptr.FunPtr (HsBindgen.Runtime.LibC.CIntPtr -> HsBindgen.Runtime.LibC.CIntPtr -> IO HsBindgen.Runtime.LibC.CIntPtr))
hs_bindgen_bbabdbe61cd1eeb2 :: IO (Ptr.FunPtr (HsBindgen.Runtime.LibC.Int64 -> HsBindgen.Runtime.LibC.Int64 -> IO HsBindgen.Runtime.LibC.Int64))
hs_bindgen_bbabdbe61cd1eeb2 =
HsBindgen.Runtime.Internal.HasFFIType.fromFFIType hs_bindgen_bbabdbe61cd1eeb2_base
{-# NOINLINE add #-}
{-| __C declaration:__ @add@
__defined at:__ @garnet_rs.h 38:10@
__defined at:__ @garnet_rs.h 38:9@
__exported by:__ @garnet_rs.h@
-}
add :: Ptr.FunPtr (HsBindgen.Runtime.LibC.CIntPtr -> HsBindgen.Runtime.LibC.CIntPtr -> IO HsBindgen.Runtime.LibC.CIntPtr)
add :: Ptr.FunPtr (HsBindgen.Runtime.LibC.Int64 -> HsBindgen.Runtime.LibC.Int64 -> IO HsBindgen.Runtime.LibC.Int64)
add =
GHC.IO.Unsafe.unsafePerformIO hs_bindgen_bbabdbe61cd1eeb2

View File

@ -37,9 +37,9 @@ $(HsBindgen.Runtime.Internal.CAPI.addCSource (HsBindgen.Runtime.Internal.CAPI.un
, "{"
, " hello_shape(*arg1);"
, "}"
, "intptr_t hs_bindgen_1c0c71fa74c428a9 ("
, " intptr_t arg1,"
, " intptr_t arg2"
, "int64_t hs_bindgen_1c0c71fa74c428a9 ("
, " int64_t arg1,"
, " int64_t arg2"
, ")"
, "{"
, " return add(arg1, arg2);"
@ -130,22 +130,22 @@ foreign import ccall safe "hs_bindgen_1c0c71fa74c428a9" hs_bindgen_1c0c71fa74c42
-- __unique:__ @com_garnet_GarnetRs_Safe_add@
hs_bindgen_1c0c71fa74c428a9 ::
HsBindgen.Runtime.LibC.CIntPtr
-> HsBindgen.Runtime.LibC.CIntPtr
-> IO HsBindgen.Runtime.LibC.CIntPtr
HsBindgen.Runtime.LibC.Int64
-> HsBindgen.Runtime.LibC.Int64
-> IO HsBindgen.Runtime.LibC.Int64
hs_bindgen_1c0c71fa74c428a9 =
HsBindgen.Runtime.Internal.HasFFIType.fromFFIType hs_bindgen_1c0c71fa74c428a9_base
{-| __C declaration:__ @add@
__defined at:__ @garnet_rs.h 38:10@
__defined at:__ @garnet_rs.h 38:9@
__exported by:__ @garnet_rs.h@
-}
add ::
HsBindgen.Runtime.LibC.CIntPtr
HsBindgen.Runtime.LibC.Int64
-- ^ __C declaration:__ @a@
-> HsBindgen.Runtime.LibC.CIntPtr
-> HsBindgen.Runtime.LibC.Int64
-- ^ __C declaration:__ @b@
-> IO HsBindgen.Runtime.LibC.CIntPtr
-> IO HsBindgen.Runtime.LibC.Int64
add = hs_bindgen_1c0c71fa74c428a9

View File

@ -37,9 +37,9 @@ $(HsBindgen.Runtime.Internal.CAPI.addCSource (HsBindgen.Runtime.Internal.CAPI.un
, "{"
, " hello_shape(*arg1);"
, "}"
, "intptr_t hs_bindgen_20eb651f0a8faf48 ("
, " intptr_t arg1,"
, " intptr_t arg2"
, "int64_t hs_bindgen_20eb651f0a8faf48 ("
, " int64_t arg1,"
, " int64_t arg2"
, ")"
, "{"
, " return add(arg1, arg2);"
@ -130,22 +130,22 @@ foreign import ccall unsafe "hs_bindgen_20eb651f0a8faf48" hs_bindgen_20eb651f0a8
-- __unique:__ @com_garnet_GarnetRs_Unsafe_add@
hs_bindgen_20eb651f0a8faf48 ::
HsBindgen.Runtime.LibC.CIntPtr
-> HsBindgen.Runtime.LibC.CIntPtr
-> IO HsBindgen.Runtime.LibC.CIntPtr
HsBindgen.Runtime.LibC.Int64
-> HsBindgen.Runtime.LibC.Int64
-> IO HsBindgen.Runtime.LibC.Int64
hs_bindgen_20eb651f0a8faf48 =
HsBindgen.Runtime.Internal.HasFFIType.fromFFIType hs_bindgen_20eb651f0a8faf48_base
{-| __C declaration:__ @add@
__defined at:__ @garnet_rs.h 38:10@
__defined at:__ @garnet_rs.h 38:9@
__exported by:__ @garnet_rs.h@
-}
add ::
HsBindgen.Runtime.LibC.CIntPtr
HsBindgen.Runtime.LibC.Int64
-- ^ __C declaration:__ @a@
-> HsBindgen.Runtime.LibC.CIntPtr
-> HsBindgen.Runtime.LibC.Int64
-- ^ __C declaration:__ @b@
-> IO HsBindgen.Runtime.LibC.CIntPtr
-> IO HsBindgen.Runtime.LibC.Int64
add = hs_bindgen_20eb651f0a8faf48

View File

@ -35,4 +35,4 @@ void hello_struct(struct T t);
void hello_shape(struct Shape s);
intptr_t add(intptr_t a, intptr_t b);
int64_t add(int64_t a, int64_t b);

View File

@ -35,6 +35,6 @@ extern "C" fn hello_shape(s: Shape) -> () {
}
#[unsafe(no_mangle)]
extern "C" fn add(a: isize, b: isize) -> isize {
extern "C" fn add(a: i64, b: i64) -> i64 {
a + b
}