Optimisations cause incorrect runtime result in Integer calculation
Summary
The following program
module Main where
import Numeric.Natural
( Natural )
a, q :: Natural
a = fromIntegral ( 18446744073709551616 :: Integer )
q = 18446744073709551616
main :: IO ()
main = print ( fromIntegral ( a `div` q ) :: Word )
runs into the following strange behaviour:
GHC 8.10 -O0
> main
1
GHC 8.10 -O1
> main
1
GHC 9.0 -O0
> main
1
GHC 9.0 -O1
> main
0
Tagging @hsyl20 as I expect him to know about what's going on.
Steps to reproduce
Simply run the above program with either ghc -O0
or ghc -O1
to get the different results.
Further remarks
Changing fromIntegral
to fromInteger
causes GHC 9.0 with optimisations to return the correct result, so it might be an incorrect rewrite rule involving fromIntegral
?
Environment
On GHC 9.0.1, Windows 10 x64.
Edited by sheaf