[GHC] #15460: Literals overflow

#15460: Literals overflow -------------------------------------+------------------------------------- Reporter: hsyl20 | Owner: (none) Type: bug | Status: new Priority: normal | Milestone: 8.6.1 Component: Compiler | Version: 8.4.3 Keywords: | Operating System: Unknown/Multiple Architecture: | Type of failure: Incorrect result Unknown/Multiple | at runtime Test Case: | Blocked By: Blocking: | Related Tickets: Differential Rev(s): | Wiki Page: -------------------------------------+------------------------------------- Consider the following example: {{{#!hs {-# LANGUAGE MagicHash #-} import GHC.Int main :: IO () main = do let x = I# (0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff#) print x }}} It gets desugared into: {{{#!hs 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: {{{#!hs {-# LANGUAGE MagicHash #-} import GHC.Int import Control.Monad main :: IO () main = do let x = I# (0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff#) when (x > maxBound) $ do print "Oups"
ghc TestLitOverflow.hs -Wall -O2 ./TestLitOverflow "Oups" }}}
-- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/15460 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#15460: Literals overflow -------------------------------------+------------------------------------- Reporter: hsyl20 | Owner: (none) Type: bug | Status: new Priority: normal | Milestone: 8.6.1 Component: Compiler | Version: 8.4.3 Resolution: | Keywords: Operating System: Unknown/Multiple | Architecture: Type of failure: Incorrect result | Unknown/Multiple at runtime | Test Case: Blocked By: | Blocking: Related Tickets: | Differential Rev(s): Wiki Page: | -------------------------------------+------------------------------------- Description changed by hsyl20: Old description:
Consider the following example:
{{{#!hs {-# LANGUAGE MagicHash #-} import GHC.Int
main :: IO () main = do let x = I# (0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff#) print x }}}
It gets desugared into: {{{#!hs 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:
{{{#!hs {-# LANGUAGE MagicHash #-} import GHC.Int import Control.Monad
main :: IO () main = do let x = I# (0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff#) when (x > maxBound) $ do print "Oups"
ghc TestLitOverflow.hs -Wall -O2 ./TestLitOverflow "Oups" }}}
New description: Consider the following example: {{{#!hs {-# LANGUAGE MagicHash #-} import GHC.Int main :: IO () main = do let x = I# (0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff#) print x }}} It gets desugared into: {{{#!hs 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: {{{#!hs {-# 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" }}}
-- -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/15460#comment:1 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#15460: Literals overflow -------------------------------------+------------------------------------- Reporter: hsyl20 | Owner: (none) Type: bug | Status: new Priority: normal | Milestone: 8.8.1 Component: Compiler | Version: 8.4.3 Resolution: | Keywords: Operating System: Unknown/Multiple | Architecture: Type of failure: Incorrect result | Unknown/Multiple at runtime | Test Case: Blocked By: | Blocking: Related Tickets: | Differential Rev(s): Wiki Page: | -------------------------------------+------------------------------------- Comment (by RolandSenn): With GHC 8.4.2 I could reproduce the error. However GHC 8.6.0-20180810 (8.6.1-beta1) seems to work: {{{ 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" 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" }}} Do you want a patch containing only a testcase? -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/15460#comment:3 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#15460: Literals overflow -------------------------------------+------------------------------------- Reporter: hsyl20 | Owner: harpocrates Type: bug | Status: new Priority: normal | Milestone: 8.8.1 Component: Compiler | Version: 8.4.3 Resolution: | Keywords: Operating System: Unknown/Multiple | Architecture: Type of failure: Incorrect result | Unknown/Multiple at runtime | Test Case: Blocked By: | Blocking: Related Tickets: | Differential Rev(s): Wiki Page: | -------------------------------------+------------------------------------- Changes (by harpocrates): * owner: (none) => harpocrates Comment: 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?). -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/15460#comment:4 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#15460: Literals overflow -------------------------------------+------------------------------------- Reporter: hsyl20 | Owner: harpocrates Type: bug | Status: new Priority: normal | Milestone: 8.8.1 Component: Compiler | Version: 8.4.3 Resolution: | Keywords: Operating System: Unknown/Multiple | Architecture: Type of failure: Incorrect result | Unknown/Multiple at runtime | Test Case: Blocked By: | Blocking: Related Tickets: | Differential Rev(s): Wiki Page: | -------------------------------------+------------------------------------- Comment (by RyanGlScott): harpocrates, was this fixed in Phab:D5181, or is there more to do here? -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/15460#comment:5 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#15460: Literals overflow -------------------------------------+------------------------------------- Reporter: hsyl20 | Owner: harpocrates Type: bug | Status: closed Priority: normal | Milestone: 8.8.1 Component: Compiler | Version: 8.4.3 Resolution: fixed | Keywords: Operating System: Unknown/Multiple | Architecture: | Unknown/Multiple Type of failure: Incorrect result | Test Case: at runtime | warnings/should_compile/T15460 Blocked By: | Blocking: Related Tickets: | Differential Rev(s): Wiki Page: | -------------------------------------+------------------------------------- Changes (by RyanGlScott): * testcase: => warnings/should_compile/T15460 * status: new => closed * resolution: => fixed -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/15460#comment:6 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#15460: Literals overflow -------------------------------------+------------------------------------- Reporter: hsyl20 | Owner: harpocrates Type: bug | Status: closed Priority: normal | Milestone: 8.8.1 Component: Compiler | Version: 8.4.3 Resolution: fixed | Keywords: Operating System: Unknown/Multiple | Architecture: | Unknown/Multiple Type of failure: Incorrect result | Test Case: at runtime | warnings/should_compile/T15460 Blocked By: | Blocking: Related Tickets: | Differential Rev(s): Phab:D5181 Wiki Page: | -------------------------------------+------------------------------------- Changes (by RyanGlScott): * differential: => Phab:D5181 -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/15460#comment:7 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler
participants (1)
-
GHC