diff options
author | RyanGlScott <ryan.gl.scott@gmail.com> | 2016-05-02 00:07:14 +0200 |
---|---|---|
committer | Ben Gamari <ben@smart-cactus.org> | 2016-05-02 00:07:21 +0200 |
commit | a28611b14930c9fd73b0028857e1ea8c0e64a38a (patch) | |
tree | b302193ceea50bd2e0d81b6c95cf1792dcaba435 /libraries/base/Foreign | |
parent | ecc060302b13d5a6ffb0fdfb0625b90f2fbfca0f (diff) | |
download | haskell-a28611b14930c9fd73b0028857e1ea8c0e64a38a.tar.gz |
Export constructors for IntPtr and WordPtr
This finishes what #5529 started by exporting the constructors for
`IntPtr` and `WordPtr` from `Foreign.Ptr`, allowing them to be used in
`foreign` declarations.
Fixes #11983.
Test Plan: `make TEST=T11983`
Reviewers: simonpj, hvr, bgamari, austin
Reviewed By: simonpj
Subscribers: simonpj, thomie
Differential Revision: https://phabricator.haskell.org/D2142
GHC Trac Issues: #11983
Diffstat (limited to 'libraries/base/Foreign')
-rw-r--r-- | libraries/base/Foreign/C/Types.hs | 6 | ||||
-rw-r--r-- | libraries/base/Foreign/Ptr.hs | 27 |
2 files changed, 29 insertions, 4 deletions
diff --git a/libraries/base/Foreign/C/Types.hs b/libraries/base/Foreign/C/Types.hs index fef8e4a3b6..b725a4a11b 100644 --- a/libraries/base/Foreign/C/Types.hs +++ b/libraries/base/Foreign/C/Types.hs @@ -10,7 +10,7 @@ -- Module : Foreign.C.Types -- Copyright : (c) The FFI task force 2001 -- License : BSD-style (see the file libraries/base/LICENSE) --- +-- -- Maintainer : ffi@haskell.org -- Stability : provisional -- Portability : portable @@ -63,6 +63,10 @@ module Foreign.C.Types -- XXX GHC doesn't support CLDouble yet -- , CLDouble(..) + -- See Note [Exporting constructors of marshallable foreign types] + -- in Foreign.Ptr for why the constructors for these newtypes are + -- exported. + -- ** Other types -- Instances of: Eq and Storable diff --git a/libraries/base/Foreign/Ptr.hs b/libraries/base/Foreign/Ptr.hs index efe580a70d..5e6bccf7ee 100644 --- a/libraries/base/Foreign/Ptr.hs +++ b/libraries/base/Foreign/Ptr.hs @@ -7,7 +7,7 @@ -- Module : Foreign.Ptr -- Copyright : (c) The FFI task force 2001 -- License : BSD-style (see the file libraries/base/LICENSE) --- +-- -- Maintainer : ffi@haskell.org -- Stability : provisional -- Portability : portable @@ -41,12 +41,15 @@ module Foreign.Ptr ( -- Free the function pointer created by foreign export dynamic. -- * Integral types with lossless conversion to and from pointers - IntPtr, + IntPtr(..), ptrToIntPtr, intPtrToPtr, - WordPtr, + WordPtr(..), ptrToWordPtr, wordPtrToPtr + + -- See Note [Exporting constructors of marshallable foreign types] + -- for why the constructors for IntPtr and WordPtr are exported. ) where import GHC.Ptr @@ -97,3 +100,21 @@ ptrToIntPtr (Ptr a#) = IntPtr (I# (addr2Int# a#)) -- | casts an @IntPtr@ to a @Ptr@ intPtrToPtr :: IntPtr -> Ptr a intPtrToPtr (IntPtr (I# i#)) = Ptr (int2Addr# i#) + +{- +Note [Exporting constructors of marshallable foreign types] +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +One might expect that IntPtr, WordPtr, and the other newtypes in the +Foreign.C.Types and System.Posix.Types modules to be abstract, but this is not +the case in GHC (see Trac #5229 and #11983). In fact, we deliberately export +the constructors for these datatypes in order to satisfy a requirement of the +Haskell 2010 Report (ยง 8.4.2) that if a newtype is used in a foreign +declaration, then its constructor must be visible. + +This requirement was motivated by the fact that using a type in a foreign +declaration necessarily exposes some information about the type to the user, +so being able to use abstract types in a foreign declaration breaks their +abstraction (see Trac #3008). As a result, the constructors of all FFI-related +newtypes in base must be exported in order to be useful for FFI programming, +even at the cost of exposing their underlying, architecture-dependent types. +-} |