Skip to content

Commit 567bd06

Browse files
authored
Fix GHC's incomplete-uni-patterns warnings (#1175)
Add explicit errors for unexpected cases. Note * For the IntMap changes, the error case gets removed completely after inlining and case-of-case optimization. * For Sequence, the let has become a case, but there is no change in strictness since the result is strict in the matched value anyway. Unlike IntMap above, the error case remains (but, of course, is never executed as long as the implementation is correct).
1 parent d77b0f5 commit 567bd06

File tree

3 files changed

+30
-15
lines changed

3 files changed

+30
-15
lines changed

containers/src/Data/IntMap/Internal.hs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
#endif
1212

1313
{-# OPTIONS_HADDOCK not-home #-}
14-
{-# OPTIONS_GHC -fno-warn-incomplete-uni-patterns #-}
1514

1615
#include "containers.h"
1716

@@ -1227,7 +1226,10 @@ unionWith f m1 m2
12271226

12281227
unionWithKey :: (Key -> a -> a -> a) -> IntMap a -> IntMap a -> IntMap a
12291228
unionWithKey f m1 m2
1230-
= mergeWithKey' Bin (\(Tip k1 x1) (Tip _k2 x2) -> Tip k1 (f k1 x1 x2)) id id m1 m2
1229+
= mergeWithKey' Bin f' id id m1 m2
1230+
where
1231+
f' (Tip k1 x1) (Tip _k2 x2) = Tip k1 (f k1 x1 x2)
1232+
f' _ _ = error "not Tip"
12311233

12321234
{--------------------------------------------------------------------
12331235
Difference
@@ -1401,7 +1403,10 @@ intersectionWith f m1 m2
14011403

14021404
intersectionWithKey :: (Key -> a -> b -> c) -> IntMap a -> IntMap b -> IntMap c
14031405
intersectionWithKey f m1 m2
1404-
= mergeWithKey' bin (\(Tip k1 x1) (Tip _k2 x2) -> Tip k1 (f k1 x1 x2)) (const Nil) (const Nil) m1 m2
1406+
= mergeWithKey' bin f' (const Nil) (const Nil) m1 m2
1407+
where
1408+
f' (Tip k1 x1) (Tip _k2 x2) = Tip k1 (f k1 x1 x2)
1409+
f' _ _ = error "not Tip"
14051410

14061411
{--------------------------------------------------------------------
14071412
Symmetric difference
@@ -1492,11 +1497,12 @@ symDiffTip !t1 !k1 = go
14921497
mergeWithKey :: (Key -> a -> b -> Maybe c) -> (IntMap a -> IntMap c) -> (IntMap b -> IntMap c)
14931498
-> IntMap a -> IntMap b -> IntMap c
14941499
mergeWithKey f g1 g2 = mergeWithKey' bin combine g1 g2
1495-
where -- We use the lambda form to avoid non-exhaustive pattern matches warning.
1496-
combine = \(Tip k1 x1) (Tip _k2 x2) ->
1500+
where
1501+
combine (Tip k1 x1) (Tip _k2 x2) =
14971502
case f k1 x1 x2 of
14981503
Nothing -> Nil
14991504
Just x -> Tip k1 x
1505+
combine _ _ = error "not Tip"
15001506
{-# INLINE combine #-}
15011507
{-# INLINE mergeWithKey #-}
15021508

containers/src/Data/IntMap/Strict/Internal.hs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
{-# LANGUAGE BangPatterns #-}
33
{-# LANGUAGE PatternGuards #-}
44

5-
{-# OPTIONS_GHC -fno-warn-incomplete-uni-patterns #-}
6-
75
#include "containers.h"
86

97
-----------------------------------------------------------------------------
@@ -665,7 +663,10 @@ unionWith f m1 m2
665663

666664
unionWithKey :: (Key -> a -> a -> a) -> IntMap a -> IntMap a -> IntMap a
667665
unionWithKey f m1 m2
668-
= mergeWithKey' Bin (\(Tip k1 x1) (Tip _k2 x2) -> Tip k1 $! f k1 x1 x2) id id m1 m2
666+
= mergeWithKey' Bin f' id id m1 m2
667+
where
668+
f' (Tip k1 x1) (Tip _k2 x2) = Tip k1 $! f k1 x1 x2
669+
f' _ _ = error "not Tip"
669670

670671
{--------------------------------------------------------------------
671672
Difference
@@ -717,7 +718,10 @@ intersectionWith f m1 m2
717718

718719
intersectionWithKey :: (Key -> a -> b -> c) -> IntMap a -> IntMap b -> IntMap c
719720
intersectionWithKey f m1 m2
720-
= mergeWithKey' bin (\(Tip k1 x1) (Tip _k2 x2) -> Tip k1 $! f k1 x1 x2) (const Nil) (const Nil) m1 m2
721+
= mergeWithKey' bin f' (const Nil) (const Nil) m1 m2
722+
where
723+
f' (Tip k1 x1) (Tip _k2 x2) = Tip k1 $! f k1 x1 x2
724+
f' _ _ = error "not Tip"
721725

722726
{--------------------------------------------------------------------
723727
MergeWithKey
@@ -763,9 +767,11 @@ intersectionWithKey f m1 m2
763767
mergeWithKey :: (Key -> a -> b -> Maybe c) -> (IntMap a -> IntMap c) -> (IntMap b -> IntMap c)
764768
-> IntMap a -> IntMap b -> IntMap c
765769
mergeWithKey f g1 g2 = mergeWithKey' bin combine g1 g2
766-
where -- We use the lambda form to avoid non-exhaustive pattern matches warning.
767-
combine = \(Tip k1 x1) (Tip _k2 x2) -> case f k1 x1 x2 of Nothing -> Nil
768-
Just !x -> Tip k1 x
770+
where
771+
combine (Tip k1 x1) (Tip _k2 x2) = case f k1 x1 x2 of
772+
Nothing -> Nil
773+
Just !x -> Tip k1 x
774+
combine _ _ = error "not Tip"
769775
{-# INLINE combine #-}
770776
{-# INLINE mergeWithKey #-}
771777

containers/src/Data/Sequence/Internal.hs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
{-# LANGUAGE PatternGuards #-}
2222

2323
{-# OPTIONS_HADDOCK not-home #-}
24-
{-# OPTIONS_GHC -fno-warn-incomplete-uni-patterns #-}
2524

2625
-----------------------------------------------------------------------------
2726
-- |
@@ -4082,8 +4081,10 @@ tailsTree f (Deep n pr m sf) =
40824081
(tailsTree f' m)
40834082
(fmap (f . digitToTree) (tailsDigit sf))
40844083
where
4085-
f' ms = let ConsLTree node m' = viewLTree ms in
4084+
f' ms = case viewLTree ms of
4085+
ConsLTree node m' ->
40864086
fmap (\ pr' -> f (deep pr' m' sf)) (tailsNode node)
4087+
EmptyLTree -> error "EmptyLTree"
40874088

40884089
{-# SPECIALIZE initsTree :: (FingerTree (Elem a) -> Elem b) -> FingerTree (Elem a) -> FingerTree (Elem b) #-}
40894090
{-# SPECIALIZE initsTree :: (FingerTree (Node a) -> Node b) -> FingerTree (Node a) -> FingerTree (Node b) #-}
@@ -4097,8 +4098,10 @@ initsTree f (Deep n pr m sf) =
40974098
(initsTree f' m)
40984099
(fmap (f . deep pr m) (initsDigit sf))
40994100
where
4100-
f' ms = let SnocRTree m' node = viewRTree ms in
4101+
f' ms = case viewRTree ms of
4102+
SnocRTree m' node ->
41014103
fmap (\ sf' -> f (deep pr m' sf')) (initsNode node)
4104+
EmptyRTree -> error "EmptyRTree"
41024105

41034106
{-# INLINE foldlWithIndex #-}
41044107
-- | 'foldlWithIndex' is a version of 'foldl' that also provides access

0 commit comments

Comments
 (0)