[GHC] #11983: Can't use IntPtr or WordPtr in a foreign import

GHC ghc-devs at haskell.org
Tue Apr 26 13:43:30 UTC 2016


#11983: Can't use IntPtr or WordPtr in a foreign import
-------------------------------------+-------------------------------------
           Reporter:  RyanGlScott    |             Owner:  RyanGlScott
               Type:  bug            |            Status:  new
           Priority:  normal         |         Milestone:  8.2.1
          Component:  Compiler       |           Version:  8.0.1
  (FFI)                              |
           Keywords:                 |  Operating System:  Unknown/Multiple
       Architecture:                 |   Type of failure:  GHC rejects
  Unknown/Multiple                   |  valid program
          Test Case:                 |        Blocked By:
           Blocking:                 |   Related Tickets:  #3008, #5529
Differential Rev(s):                 |         Wiki Page:
-------------------------------------+-------------------------------------
 Despite the [http://hackage.haskell.org/package/base-4.8.2.0/docs/Foreign-
 Ptr.html#t:IntPtr 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:

 {{{#!hs
 -- 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 ()
 }}}

 {{{#!c
 // example.h
 #ifndef EXAMPLE_H
 #define EXAMPLE_H

 #include <stdint.h>

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

 #endif // EXAMPLE_H
 }}}

 {{{#!c
 // 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, which prevents `newtype`s from
 being used as FFI types unless their constructors are exported. #5529
 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`.

--
Ticket URL: <http://ghc.haskell.org/trac/ghc/ticket/11983>
GHC <http://www.haskell.org/ghc/>
The Glasgow Haskell Compiler


More information about the ghc-tickets mailing list