Skip to content

unsafeCoerce leaks types

Forgive the cryptic summary, here's the code:

import Unsafe.Coerce
import Foreign
import Data.Bits

foo = (unsafeCoerce (0.50094324::Float))::Int32
bar = (unsafeCoerce (0.30295494::Float))::Int32

main = print (foo, foo .&. 0xffffffff)
    >> print (bar, bar .&. 0xffffffff)

giving the output

% runhaskell bug.hs
(1056980433,1056980433)
(140502315506921,140502315506921)
*Main> :t bar
bar :: Int32
*Main> bar
140549560147177

where the too-big number changes from run to run, presumably because it has a different type-tag on each invocation. It does seem to include the correct data in the low 32 bits, though.

I couldn't replicate this with compiled code, but both GHCi and TH exhibit this behaviour.

Trac metadata
Trac field Value
Version 6.12.1
Type Bug
TypeOfFailure OtherFailure
Priority normal
Resolution Unresolved
Component Template Haskell
Test case
Differential revisions
BlockedBy
Related
Blocking
CC barsoap@web.de
Operating system
Architecture
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information