summaryrefslogtreecommitdiff
path: root/libraries
diff options
context:
space:
mode:
authorRichard Eisenberg <eir@cis.upenn.edu>2014-06-10 13:38:06 -0400
committerRichard Eisenberg <eir@cis.upenn.edu>2014-06-11 09:27:40 -0400
commit9e6c6b4206cd893434e49cd893eb67081eeffe99 (patch)
tree5a7b6448cf111c04f2c31b65a70e123e2f59019e /libraries
parent0e6bc84ca958f6da8c10c2ed489f87d8c4c9b463 (diff)
downloadhaskell-9e6c6b4206cd893434e49cd893eb67081eeffe99.tar.gz
Make FunPtr's role be phantom; add comments.
This change also updates castFunPtr to make it free at runtime. This fixes #9163.
Diffstat (limited to 'libraries')
-rw-r--r--libraries/base/GHC/Ptr.lhs20
-rw-r--r--libraries/ghc-prim/GHC/Types.hs11
2 files changed, 20 insertions, 11 deletions
diff --git a/libraries/base/GHC/Ptr.lhs b/libraries/base/GHC/Ptr.lhs
index 341512b838..a55f01e9b1 100644
--- a/libraries/base/GHC/Ptr.lhs
+++ b/libraries/base/GHC/Ptr.lhs
@@ -31,13 +31,18 @@ import GHC.Show
import GHC.Num
import GHC.List ( length, replicate )
import Numeric ( showHex )
-import Data.Coerce
#include "MachDeps.h"
------------------------------------------------------------------------
-- Data pointers.
+-- The role of Ptr's parameter is phantom, as there is no relation between
+-- the Haskell representation and whathever the user puts at the end of the
+-- pointer. And phantom is useful to implement castPtr (see #9163)
+
+-- redundant role annotation checks that this doesn't change
+type role Ptr phantom
data Ptr a = Ptr Addr# deriving (Eq, Ord)
-- ^ A value of type @'Ptr' a@ represents a pointer to an object, or an
-- array of objects, which may be marshalled to or from Haskell values
@@ -49,10 +54,6 @@ data Ptr a = Ptr Addr# deriving (Eq, Ord)
-- to access the pointer. For example you might write small foreign
-- functions to get or set the fields of a C @struct@.
--- The role of Ptr's parameter is phantom, as there is relation between
--- the Haskell representation and whathever the user puts at the end of the
--- pointer. And phantom is useful to implement castPtr (see #9163)
-
-- |The constant 'nullPtr' contains a distinguished value of 'Ptr'
-- that is not associated with a valid memory location.
nullPtr :: Ptr a
@@ -86,7 +87,10 @@ minusPtr (Ptr a1) (Ptr a2) = I# (minusAddr# a1 a2)
------------------------------------------------------------------------
-- Function pointers for the default calling convention.
-type role FunPtr representational
+-- 'FunPtr' has a phantom role for similar reasons to 'Ptr'. Note
+-- that 'FunPtr's role cannot become nominal without changes elsewhere
+-- in GHC. See Note [FFI type roles] in TcForeign.
+type role FunPtr phantom
data FunPtr a = FunPtr Addr# deriving (Eq, Ord)
-- ^ A value of type @'FunPtr' a@ is a pointer to a function callable
-- from foreign code. The type @a@ will normally be a /foreign type/,
@@ -128,8 +132,6 @@ data FunPtr a = FunPtr Addr# deriving (Eq, Ord)
-- > foreign import ccall "dynamic"
-- > mkFun :: FunPtr IntFunction -> IntFunction
--- The role of FunPtr is representational, to be on the safe side (see #9163)
-
-- |The constant 'nullFunPtr' contains a
-- distinguished value of 'FunPtr' that is not
-- associated with a valid memory location.
@@ -138,7 +140,7 @@ nullFunPtr = FunPtr nullAddr#
-- |Casts a 'FunPtr' to a 'FunPtr' of a different type.
castFunPtr :: FunPtr a -> FunPtr b
-castFunPtr (FunPtr addr) = FunPtr addr
+castFunPtr = coerce
-- |Casts a 'FunPtr' to a 'Ptr'.
--
diff --git a/libraries/ghc-prim/GHC/Types.hs b/libraries/ghc-prim/GHC/Types.hs
index 44351d8777..f6f4233b5b 100644
--- a/libraries/ghc-prim/GHC/Types.hs
+++ b/libraries/ghc-prim/GHC/Types.hs
@@ -1,4 +1,5 @@
-{-# LANGUAGE MagicHash, NoImplicitPrelude, TypeFamilies, UnboxedTuples #-}
+{-# LANGUAGE MagicHash, NoImplicitPrelude, TypeFamilies, UnboxedTuples,
+ RoleAnnotations #-}
-----------------------------------------------------------------------------
-- |
-- Module : GHC.Types
@@ -80,7 +81,13 @@ at some point, directly or indirectly, from @Main.main@.
or the '>>' and '>>=' operations from the 'Monad' class.
-}
newtype IO a = IO (State# RealWorld -> (# State# RealWorld, a #))
-
+type role IO representational
+{-
+The above role annotation is redundant but is included because this role
+is significant in the normalisation of FFI types. Specifically, if this
+role were to become nominal (which would be very strange, indeed!), changes
+elsewhere in GHC would be necessary. See [FFI type roles] in TcForeign.
+-}
{-
Note [Kind-changing of (~) and Coercible]