Opened 2 years ago

Closed 2 years ago

#10517 closed bug (invalid)

Unexpected behavior of unsafeCoerce converting Word32 to Word8

Reported by: svenk Owned by:
Priority: normal Milestone:
Component: Compiler Version: 7.10.1
Keywords: Cc:
Operating System: Linux Architecture: x86_64 (amd64)
Type of failure: Incorrect result at runtime Test Case:
Blocked By: Blocking:
Related Tickets: Differential Rev(s):
Wiki Page:

Description

The function unsafeCoerce behaves unexpected if it is used to convert Word32 to Word8:

main = do
  -- prints 135
  print (fromIntegral (1234567 :: Word32) :: Word8)

  -- prints 1234567
  print (unsafeCoerce (1234567 :: Word32) :: Word8)

On the other hand coercing Ptr Word32 to Ptr Word8 or the other way around works perfectly fine.

Change History (1)

comment:1 Changed 2 years ago by ezyang

Resolution: invalid
Status: newclosed

GHC gives no guarantees for this case: http://hackage.haskell.org/package/ghc-prim-0.3.1.0/docs/GHC-Prim.html#v:unsafeCoerce-35- since Word32 and Word8 are boxed types.

Note: See TracTickets for help on using tickets.