Skip to content

Commit d6a7d82

Browse files
authored
Merge pull request #481 from Lysxia/ghc-63394
Document GHC-63394 (messages from WARNING and DEPRECATED pragmas)
2 parents ecf719a + a489c04 commit d6a7d82

File tree

7 files changed

+114
-0
lines changed

7 files changed

+114
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
module PartialHead where
2+
3+
import Data.List.NonEmpty (NonEmpty((:|)))
4+
import qualified Data.List.NonEmpty as NonEmpty
5+
import Data.Maybe (listToMaybe)
6+
7+
-- Use pattern-matching, handling the empty case explicitly.
8+
example1 :: Int
9+
example1 =
10+
let list = 1 : 2 : 3 : [] in
11+
case list of
12+
[] -> 0
13+
hd : _ -> hd
14+
15+
-- Use listToMaybe.
16+
example2 :: Maybe Int
17+
example2 =
18+
let list = 1 : 2 : 3 : [] in
19+
listToMaybe list
20+
21+
-- Refactor to use NonEmpty.head.
22+
example3 :: Int
23+
example3 =
24+
let list :: NonEmpty Int
25+
list = 1 :| 2 : 3 : [] in
26+
NonEmpty.head list
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module PartialHead where
2+
3+
example :: Int
4+
example =
5+
let list = 1 : 2 : 3 : [] in
6+
head list
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
title: "`head` is a partial function"
3+
---
4+
5+
## Error Message
6+
7+
```
8+
before/PartialHead.hs:6:3: warning: [GHC-63394] [-Wx-partial]
9+
In the use of ‘head’
10+
(imported from Prelude, but defined in GHC.List):
11+
"This is a partial function, it throws an error on empty lists. Use pattern matching or Data.List.uncons instead. Consider refactoring to use Data.List.NonEmpty."
12+
|
13+
6 | head list
14+
| ^^^^
15+
```
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
module PartialTail where
2+
3+
import Data.List.NonEmpty (NonEmpty((:|)))
4+
import qualified Data.List.NonEmpty as NonEmpty
5+
6+
-- Replace `tail` with `drop 1`.
7+
example1 :: [Int]
8+
example1 =
9+
let list = 1 : 2 : 3 : [] in
10+
drop 1 list
11+
12+
-- Use pattern-matching.
13+
example2 :: [Int]
14+
example2 =
15+
let list = 1 : 2 : 3 : [] in
16+
case list of
17+
[] -> []
18+
_ : xs -> xs
19+
20+
-- Refactor to use NonEmpty.tail.
21+
example3 :: [Int]
22+
example3 =
23+
let list :: NonEmpty Int
24+
list = 1 :| 2 : 3 : [] in
25+
NonEmpty.tail list
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module PartialTail where
2+
3+
example :: [Int]
4+
example =
5+
let list = 1 : 2 : 3 : [] in
6+
tail list
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
title: "`tail` is a partial function"
3+
---
4+
5+
## Error Message
6+
7+
```
8+
PartialTail.hs:6:3: warning: [GHC-63394] [-Wx-partial]
9+
In the use of ‘tail’
10+
(imported from Prelude, but defined in GHC.List):
11+
"This is a partial function, it throws an error on empty lists. Replace it with drop 1, or use pattern matching or Data.List.uncons instead. Consider refactoring to use Data.List.NonEmpty."
12+
|
13+
6 | tail list
14+
| ^^^^
15+
```
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
---
2+
title: Messages from WARNING and DEPRECATED pragmas
3+
summary: Warning or deprecation message attached to a function, class, type, or module in a library
4+
severity: warning
5+
introduced: 9.6.1
6+
---
7+
8+
Libraries may attach warning messages to functions and other entities they
9+
export to appear when they are used. Common use cases include deprecated
10+
functions and partial functions (such as `head` and `tail`).
11+
12+
Those messages are declared using the pragmas `{-# WARNING ... #-}` and
13+
`{-# DEPRECATED ... #-}`.
14+
For more information, [see the GHC user's guide][users-guide].
15+
16+
[users-guide]: https://downloads.haskell.org/ghc/latest/docs/users_guide/exts/pragmas.html#warning-and-deprecated-pragmas
17+
18+
```haskell
19+
{-# WARNING in "x-partial" head "This function is partial..." #-}
20+
head :: [a] -> a
21+
```

0 commit comments

Comments
 (0)