Skip to content

Can't use IntPtr or WordPtr in a foreign import

Despite the docs claiming that you can use IntPtr and WordPtr to convert to and from intptr_t and uintptr_t, you can't actually do so in a foreign import as of GHC 7.6. Here's a minimal example:

-- Example.hs
{-# LANGUAGE ForeignFunctionInterface #-}
module Example where

{-# INCLUDE example.h #-}

import Foreign.Ptr

foreign import ccall "intptr_example"
    intPtrExample :: IntPtr -> IO ()
foreign import ccall "uintptr_example"
    wordPtrExample :: WordPtr -> IO ()
// example.h
#ifndef EXAMPLE_H
#define EXAMPLE_H

#include <stdint.h>

void intptr_example(intptr_t);
void uintptr_example(uintptr_t);

#endif // EXAMPLE_H
// example.c
#include <stdint.h>
#include "example.h"

void intptr_example(intptr_t i)   {}
void uintptr_example(uintptr_t u) {}

This appears to be a consequence of #3008 (closed), which prevents newtypes from being used as FFI types unless their constructors are exported. #5529 (closed) fixed this problem for the datatypes in Foreign.C.Types and System.Posix.Types by simply exporting their constructors, so I think all that's needed to fix this particular example is to export the constructors for IntPtr and WordPtr.

Trac metadata
Trac field Value
Version 8.0.1
Type Bug
TypeOfFailure OtherFailure
Priority normal
Resolution Unresolved
Component Compiler (FFI)
Test case
Differential revisions
BlockedBy
Related
Blocking
CC
Operating system
Architecture
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information