Literals overflow
Consider the following example:
{-# LANGUAGE MagicHash #-}
import GHC.Int
main :: IO ()
main = do
let x = I# (0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff#)
print x
It gets desugared into:
main
= print
@ Int
GHC.Show.$fShowInt
(GHC.Types.I#
7237005577332262213973186563042994240829374041602535252466099000494570602495#)
Problem: the literal value isn't rounded and there is no overflow warning.
It breaks the invariant that literal values in Core have to be in range. Bad things can happen when we break this invariant:
{-# LANGUAGE MagicHash #-}
import GHC.Int
main :: IO ()
main = do
let x = I# (0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff#)
if (x > maxBound)
then print "Oups"
else print "Ok"
> ghc TestLitOverflow.hs -Wall -O0
> ./TestLitOverflow
"Ok"
> ghc TestLitOverflow.hs -Wall -O2
> ./TestLitOverflow
"Oups"