Skip to content

type roles for unboxed arrays

rwbarton@morphism:~$ ghci
GHCi, version 7.8.1: http://www.haskell.org/ghc/  :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
Prelude> :i Data.Array.Unboxed.UArray
type role Data.Array.Base.UArray representational phantom
data Data.Array.Base.UArray i e
  = Data.Array.Base.UArray !i
                           !i
                           {-# UNPACK #-} !Int
                           GHC.Prim.ByteArray#
  	-- Defined in ‘Data.Array.Base’
-- [instances trimmed]
Prelude> :i Data.Array.IO.IOUArray
type role Data.Array.IO.Internals.IOUArray representational phantom
newtype Data.Array.IO.Internals.IOUArray i e
  = Data.Array.IO.Internals.IOUArray (Data.Array.Base.STUArray
                                        GHC.Prim.RealWorld i e)
  	-- Defined in ‘Data.Array.IO.Internals’
Prelude> :i Data.Array.ST.STUArray
type role Data.Array.Base.STUArray nominal representational phantom
data Data.Array.Base.STUArray s i e
  = Data.Array.Base.STUArray !i
                             !i
                             {-# UNPACK #-} !Int
                             (GHC.Prim.MutableByteArray# s)
  	-- Defined in ‘Data.Array.Base’
Prelude> :i Data.Array.Storable.StorableArray
type role Data.Array.Storable.Internals.StorableArray representational phantom
data Data.Array.Storable.Internals.StorableArray i e
  = Data.Array.Storable.Internals.StorableArray !i
                                                !i
                                                Int
                                                !(GHC.ForeignPtr.ForeignPtr e)
  	-- Defined in ‘Data.Array.Storable.Internals’

These phantom roles for the element types let me create an unboxed array of one type (like Word8), cast it with coerce to another type (like Word64) and read outside the bounds of the array. I think they should all be nominal, since a newtype of an existing type could have a totally unrelated MArray or Storable instance.

Trac metadata
Trac field Value
Version 7.8.1
Type Bug
TypeOfFailure OtherFailure
Priority normal
Resolution Unresolved
Component libraries (other)
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