diff options
author | Alec Theriault <alec.theriault@gmail.com> | 2018-12-03 07:03:44 -0500 |
---|---|---|
committer | Ryan Scott <ryan.gl.scott@gmail.com> | 2018-12-03 07:03:44 -0500 |
commit | 75a8349b2a7d0142d3d687837caf5a95bbb4368d (patch) | |
tree | 7a1c14b1ba4357dcc032d2d32a10039b8c3f1cd0 /testsuite | |
parent | 93a3f9070d5d69ad6a28fe94ccccd20c54609698 (diff) | |
download | haskell-75a8349b2a7d0142d3d687837caf5a95bbb4368d.tar.gz |
Warn on all out-of-range literals in pats/exprs
Summary:
These changes were motivated by #13256. While poking around, I
realized we weren't very consistent in our "-Woverflowed-literals"
warnings. This patch fixes that by:
* warning earlier on in the pipeline (ie. before we've desugared
'Int' patterns into 'I# Int#')
* handling 'HsLit' as well as 'HsOverLit' (this covers unboxed
literals)
* covering more pattern / expression forms
4/6 of the warnings in the 'Overflow' test are due to this patch. The
other two are mostly for completeness.
Also fixed a missing empty-enumeration warning for 'Natural'.
This warnings were tripped up by the 'Bounded Word' instance (see #9505),
but the fix was obvious and simple: use unboxed word literals.
Test Plan: make TEST=Overflow && make TEST=T10930
Reviewers: hvr, bgamari, RyanGlScott
Reviewed By: RyanGlScott
Subscribers: RyanGlScott, rwbarton, carter
GHC Trac Issues: #13256, #10930
Differential Revision: https://phabricator.haskell.org/D5181
Diffstat (limited to 'testsuite')
11 files changed, 119 insertions, 1 deletions
diff --git a/testsuite/tests/warnings/should_compile/Overflow.hs b/testsuite/tests/warnings/should_compile/Overflow.hs new file mode 100644 index 0000000000..7029b7aaa8 --- /dev/null +++ b/testsuite/tests/warnings/should_compile/Overflow.hs @@ -0,0 +1,32 @@ +{-# LANGUAGE MagicHash #-} +module Overflow where + +import GHC.Exts + +-- Overflow an 'Int#' expression +f x = let y :: Int# + y = 10000000000000000000000000000000# + in 9 + +-- Overflow an 'Int#' pattern +g :: Int# -> Bool +g 100000000000000000000000000# = True +g _ = False + +-- Overflow an 'Int' expression +h :: Int +h = 1000000000000000000000000000000 + +-- Overflow an 'Int' pattern +i :: Int -> Int +i 100000000000000000000000000000000 = 0 +i _ = 1 + +-- Underflow a 'Word' expression +j :: Word +j = -1 + +-- Underflow a 'Word' pattern +k :: Word -> Bool +k (-1) = True +k _ = False diff --git a/testsuite/tests/warnings/should_compile/Overflow.stderr b/testsuite/tests/warnings/should_compile/Overflow.stderr new file mode 100644 index 0000000000..ce657aebf6 --- /dev/null +++ b/testsuite/tests/warnings/should_compile/Overflow.stderr @@ -0,0 +1,18 @@ + +Overflow.hs:8:15: warning: [-Woverflowed-literals (in -Wdefault)] + Literal 10000000000000000000000000000000 is out of the Int# range -9223372036854775808..9223372036854775807 + +Overflow.hs:13:1: warning: [-Woverflowed-literals (in -Wdefault)] + Literal 100000000000000000000000000 is out of the Int# range -9223372036854775808..9223372036854775807 + +Overflow.hs:18:5: warning: [-Woverflowed-literals (in -Wdefault)] + Literal 1000000000000000000000000000000 is out of the Int range -9223372036854775808..9223372036854775807 + +Overflow.hs:22:1: warning: [-Woverflowed-literals (in -Wdefault)] + Literal 100000000000000000000000000000000 is out of the Int range -9223372036854775808..9223372036854775807 + +Overflow.hs:27:6: warning: [-Woverflowed-literals (in -Wdefault)] + Literal -1 is out of the Word range 0..18446744073709551615 + +Overflow.hs:31:1: warning: [-Woverflowed-literals (in -Wdefault)] + Literal -1 is out of the Word range 0..18446744073709551615 diff --git a/testsuite/tests/warnings/should_compile/T10930.hs b/testsuite/tests/warnings/should_compile/T10930.hs new file mode 100644 index 0000000000..e7a60952cf --- /dev/null +++ b/testsuite/tests/warnings/should_compile/T10930.hs @@ -0,0 +1,10 @@ +{-# LANGUAGE NegativeLiterals #-} +module T10930 where + +import Numeric.Natural + +x = -123 :: Word +y = -123 :: Natural + +w = [10..3] :: [Word] +z = [10..3] :: [Natural] diff --git a/testsuite/tests/warnings/should_compile/T10930.stderr b/testsuite/tests/warnings/should_compile/T10930.stderr new file mode 100644 index 0000000000..1b593b9f38 --- /dev/null +++ b/testsuite/tests/warnings/should_compile/T10930.stderr @@ -0,0 +1,11 @@ + +T10930.hs:6:5: warning: [-Woverflowed-literals (in -Wdefault)] + Literal -123 is out of the Word range 0..18446744073709551615 + +T10930.hs:7:5: warning: [-Woverflowed-literals (in -Wdefault)] + Literal -123 is negative but Natural only supports positive numbers + +T10930.hs:9:5: warning: [-Wempty-enumerations (in -Wdefault)] Enumeration is empty + +T10930.hs:10:5: warning: [-Wempty-enumerations (in -Wdefault)] + Enumeration is empty diff --git a/testsuite/tests/warnings/should_compile/T10930b.hs b/testsuite/tests/warnings/should_compile/T10930b.hs new file mode 100644 index 0000000000..59441669c2 --- /dev/null +++ b/testsuite/tests/warnings/should_compile/T10930b.hs @@ -0,0 +1,7 @@ +{-# LANGUAGE NegativeLiterals #-} +module T10930a where + +import Numeric.Natural + +x = [-10 .. -3] :: [Natural] +y = [-3 .. -10] :: [Natural] diff --git a/testsuite/tests/warnings/should_compile/T10930b.stderr b/testsuite/tests/warnings/should_compile/T10930b.stderr new file mode 100644 index 0000000000..a967efa839 --- /dev/null +++ b/testsuite/tests/warnings/should_compile/T10930b.stderr @@ -0,0 +1,15 @@ + +T10930b.hs:6:6: warning: [-Woverflowed-literals (in -Wdefault)] + Literal -10 is negative but Natural only supports positive numbers + +T10930b.hs:6:13: warning: [-Woverflowed-literals (in -Wdefault)] + Literal -3 is negative but Natural only supports positive numbers + +T10930b.hs:7:5: warning: [-Wempty-enumerations (in -Wdefault)] + Enumeration is empty + +T10930b.hs:7:6: warning: [-Woverflowed-literals (in -Wdefault)] + Literal -3 is negative but Natural only supports positive numbers + +T10930b.hs:7:12: warning: [-Woverflowed-literals (in -Wdefault)] + Literal -10 is negative but Natural only supports positive numbers diff --git a/testsuite/tests/warnings/should_compile/T13256.hs b/testsuite/tests/warnings/should_compile/T13256.hs new file mode 100644 index 0000000000..f02886331d --- /dev/null +++ b/testsuite/tests/warnings/should_compile/T13256.hs @@ -0,0 +1,4 @@ +module T13256 where + +v :: Int +v = (\x -> case (x :: Int) of 100000000000000000000000000000000 -> 0) 8 :: Int diff --git a/testsuite/tests/warnings/should_compile/T13256.stderr b/testsuite/tests/warnings/should_compile/T13256.stderr new file mode 100644 index 0000000000..6b06a7d093 --- /dev/null +++ b/testsuite/tests/warnings/should_compile/T13256.stderr @@ -0,0 +1,3 @@ + +T13256.hs:4:12: warning: [-Woverflowed-literals (in -Wdefault)] + Literal 100000000000000000000000000000000 is out of the Int range -9223372036854775808..9223372036854775807 diff --git a/testsuite/tests/warnings/should_compile/T15460.hs b/testsuite/tests/warnings/should_compile/T15460.hs new file mode 100644 index 0000000000..369acd8f35 --- /dev/null +++ b/testsuite/tests/warnings/should_compile/T15460.hs @@ -0,0 +1,9 @@ +{-# LANGUAGE MagicHash #-} +module T15460 where + +import GHC.Int + +main :: IO () +main = do + let x = I# (0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff#) + print x diff --git a/testsuite/tests/warnings/should_compile/T15460.stderr b/testsuite/tests/warnings/should_compile/T15460.stderr new file mode 100644 index 0000000000..5907b44320 --- /dev/null +++ b/testsuite/tests/warnings/should_compile/T15460.stderr @@ -0,0 +1,3 @@ + +T15460.hs:8:16: warning: [-Woverflowed-literals (in -Wdefault)] + Literal 7237005577332262213973186563042994240829374041602535252466099000494570602495 is out of the GHC.Prim.Int# range -9223372036854775808..9223372036854775807 diff --git a/testsuite/tests/warnings/should_compile/all.T b/testsuite/tests/warnings/should_compile/all.T index fd2ba85035..10a3ecf12c 100644 --- a/testsuite/tests/warnings/should_compile/all.T +++ b/testsuite/tests/warnings/should_compile/all.T @@ -6,9 +6,13 @@ test('T9178', [], multimod_compile, ['T9178', '-Wall']) test('T9230', normal, compile, ['']) test('T10908', normal, compile, ['']) +test('T10930', normal, compile, ['']) +test('T10930b', normal, compile, ['']) test('T11077', normal, compile, ['-fwarn-missing-exported-signatures']) test('T11128', normal, compile, ['']) test('T11128b', normal, compile, ['']) +test('T13256', normal, compile, ['']) +test('T15460', normal, compile, ['']) test('PluralS', normal, compile, ['']) # T12574 Test that suggest current flag over deprecated @@ -21,4 +25,6 @@ test('Werror02', normal, compile, ['']) test('MissingMod', normal, multimod_compile, ['MissingMod', '-Wmissing-home-modules']) -test('StarBinder', normal, compile, [''])
\ No newline at end of file +test('StarBinder', normal, compile, ['']) + +test('Overflow', normal, compile, ['']) |