Opened 17 months ago

Closed 13 months ago

Last modified 13 months ago

#15460 closed bug (fixed)

Literals overflow

Reported by: hsyl20 Owned by: harpocrates
Priority: normal Milestone: 8.8.1
Component: Compiler Version: 8.4.3
Keywords: Cc:
Operating System: Unknown/Multiple Architecture: Unknown/Multiple
Type of failure: Incorrect result at runtime Test Case: warnings/should_compile/T15460
Blocked By: Blocking:
Related Tickets: Differential Rev(s): Phab:D5181
Wiki Page:

Description (last modified by hsyl20)

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"

Change History (7)

comment:1 Changed 17 months ago by hsyl20

Description: modified (diff)

comment:2 Changed 17 months ago by bgamari

Milestone: 8.6.18.8.1

These won't be fixed for in GHC 8.6.

comment:3 Changed 16 months ago by RolandSenn

With GHC 8.4.2 I can reproduce the error.

roland@goms:~/Projekte/ghctest/T15460$ ghc --version
The Glorious Glasgow Haskell Compilation System, version 8.4.2
roland@goms:~/Projekte/ghctest/T15460$ ghc -O2 T15460.hs
[1 of 1] Compiling Main             ( T15460.hs, T15460.o )
Linking T15460 ...
roland@goms:~/Projekte/ghctest/T15460$ ./T15460
"Oups"

However GHC 8.6.0-20180810 (8.6.1-beta1) seems to work:

roland@goms:~/Projekte/ghctest/T15460$ switchghc 8.6.1
The Glorious Glasgow Haskell Compilation System, version 8.6.0.20180810
roland@goms:~/Projekte/ghctest/T15460$ rm *.hi | rm *.o
roland@goms:~/Projekte/ghctest/T15460$ ghc -O2 T15460.hs
[1 of 1] Compiling Main             ( T15460.hs, T15460.o )
Linking T15460 ...
roland@goms:~/Projekte/ghctest/T15460$ ./T15460
"Ok"

Same result for GHC head version 8.7.20180825!

Do you want a patch containing only a testcase?

Last edited 16 months ago by RolandSenn (previous) (diff)

comment:4 Changed 15 months ago by harpocrates

Owner: set to harpocrates

Literal overflow warnings are being added in https://phabricator.haskell.org/D5181. After that gets merged, I'll add a testcase for this (then we'll close?).

comment:5 Changed 13 months ago by RyanGlScott

harpocrates, was this fixed in Phab:D5181, or is there more to do here?

comment:6 Changed 13 months ago by RyanGlScott

Resolution: fixed
Status: newclosed
Test Case: warnings/should_compile/T15460

comment:7 Changed 13 months ago by RyanGlScott

Differential Rev(s): ​Phab:D5181
Note: See TracTickets for help on using tickets.