Skip to content

Commit b6da9b5

Browse files
Merge pull request #404 from jhrcek/GHC-97441
Document DsOverflowedLiterals -> 97441 (closes #2)
2 parents e95a1d6 + 4c4d325 commit b6da9b5

File tree

4 files changed

+72
-0
lines changed

4 files changed

+72
-0
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
module Main where
2+
3+
import Data.Word (Word8)
4+
5+
main :: IO ()
6+
main = do
7+
-- Word8 can represent values in range (0,255)
8+
-- prints 255
9+
print (255 :: Word8)
10+
11+
-- Int can represent values in range (-9223372036854775808,9223372036854775807)
12+
-- prints 9223372036854775807
13+
print (9223372036854775807 :: Int)
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
module Main where
2+
3+
import Data.Word (Word8)
4+
5+
main :: IO ()
6+
main = do
7+
-- Word8 can represent values in range (0,255)
8+
-- 258 is 3 larger than maxBound so it will wrap around 0 -> 1 -> 2
9+
-- prints 2 due to overflow
10+
print (258 :: Word8)
11+
12+
-- Int can represent values in range (-9223372036854775808,9223372036854775807)
13+
-- 9223372036854775817 is 10 larger than maxBound so it will wrap around to negative values
14+
-- prints -9223372036854775799 due to overflow
15+
print (9223372036854775817 :: Int)
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
---
2+
title: Overflowed Literals
3+
---
4+
5+
## Error Message
6+
```
7+
OverflowedLiterals.hs:10:12: warning: [GHC-97441] [-Woverflowed-literals]
8+
Literal 258 is out of the Word8 range 0..255
9+
|
10+
10 | print (258 :: Word8)
11+
| ^^^
12+
13+
OverflowedLiterals.hs:15:12: warning: [GHC-97441] [-Woverflowed-literals]
14+
Literal 9223372036854775817 is out of the Int range -9223372036854775808..9223372036854775807
15+
|
16+
15 | print (9223372036854775817 :: Int)
17+
| ^^^^^^^^^^^^^^^^^^^
18+
```
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
---
2+
title: Overflowed Literals
3+
summary: Literal overflowing range of supported values
4+
severity: warning
5+
introduced: 9.6.1
6+
flag: -Woverflowed-literals
7+
---
8+
9+
This warning is emitted if an integer literal (that is, a constant integer value in the source code) will overflow.
10+
11+
Many integer types have fixed precision. This means that only a certain number of bits are available to represent their values.
12+
You can check the minimum and maximum values representable by given type by using `minBound` and `maxBound` from the [Bounded](https://hackage.haskell.org/package/base-4.18.0.0/docs/Prelude.html#t:Bounded) type class defined in the `base` package. The range of supported values might differ based on what OS / platform you’re using.
13+
14+
```haskell
15+
>>> minBound :: Int
16+
-9223372036854775808
17+
>>> maxBound :: Int
18+
9223372036854775807
19+
```
20+
21+
Calculations that exceed this range cause the value to wrap around, which is called "overflow" or "underflow". Literals outside the range also overflow. This doesn't cause an error at runtime, but it might cause confusion because the overflowed value is usually not what you want or expect.
22+
23+
To fix the warning you can:
24+
25+
- Use a different value for the literal so that it fits within the range supported by its type
26+
- Use a type that is capable of representing the value (or `Integer`, which can represent values as big as your computer's memory)

0 commit comments

Comments
 (0)