Skip to content

Commit f36121e

Browse files
committed
Apply review suggestions
1 parent 879c95f commit f36121e

File tree

3 files changed

+25
-15
lines changed

3 files changed

+25
-15
lines changed

message-index/messages/GHC-97441/example1/before/OverflowedLiterals.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ import Data.Word (Word8)
55
main :: IO ()
66
main = do
77
-- Word8 can represent values in range (0,255)
8+
-- 258 is 3 larger than maxBound so it will wrap around 0 -> 1 -> 2
89
-- prints 2 due to overflow
910
print (258 :: Word8)
10-
-- ^ value is 3 larger than maxBound so it will wrap around 0 -> 1 -> 2
1111

1212
-- Int can represent values in range (-9223372036854775808,9223372036854775807)
13+
-- 9223372036854775817 is 10 larger than maxBound so it will wrap around to negative values
1314
-- prints -9223372036854775799 due to overflow
1415
print (9223372036854775817 :: Int)
15-
-- ^ value is 10 larger than maxBound
Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
11
---
2-
title: Overflowed Literal
2+
title: Overflowed Literals
33
---
44

55
## Error Message
66
```
7-
before/OverflowedLiterals.hs:9:12: warning: [GHC-97441] [-Woverflowed-literals]
7+
OverflowedLiterals.hs:10:12: warning: [GHC-97441] [-Woverflowed-literals]
88
Literal 258 is out of the Word8 range 0..255
9-
|
10-
9 | print (258 :: Word8)
11-
| ^^^
9+
|
10+
10 | print (258 :: Word8)
11+
| ^^^
1212
13-
before/OverflowedLiterals.hs:14:12: warning: [GHC-97441] [-Woverflowed-literals]
13+
OverflowedLiterals.hs:15:12: warning: [GHC-97441] [-Woverflowed-literals]
1414
Literal 9223372036854775817 is out of the Int range -9223372036854775808..9223372036854775807
1515
|
16-
14 | print (9223372036854775817 :: Int)
16+
15 | print (9223372036854775817 :: Int)
1717
| ^^^^^^^^^^^^^^^^^^^
18-
1918
```
Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,26 @@
11
---
2-
title: Overflowed Literal
2+
title: Overflowed Literals
33
summary: Literal overflowing range of supported values
44
severity: warning
55
introduced: 9.6.1
66
flag: -Woverflowed-literals
77
---
88

9-
Warning emitted if a literal (constant value in the source code) will overflow.
10-
This doesn't cause error at runtime, but might cause confusion because overflowed value might be different than what you expect.
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 / maximum values representable by given type by using `minBound` / `maxBound` from [Bounded](https://hackage.haskell.org/package/base-4.18.0.0/docs/Prelude.html#t:Bounded) typeclass 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 error at runtime, but might cause confusion because overflowed value is usually not what you want or expect.
1122

1223
To fix the warning you can:
1324

14-
- Use different value for the literal so that it fits within the range supported by its type
15-
- Use different type, which is capable of representing the value
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)