Skip to content

Commit

Permalink
Fix some -Wx-partial warnings
Browse files Browse the repository at this point in the history
These were uncovered by GHC 9.8, where -Wx-partial is included in -Wall.
  • Loading branch information
RyanGlScott committed Aug 6, 2023
1 parent cc7e0dd commit 7571f4b
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 30 deletions.
1 change: 1 addition & 0 deletions deriving-compat.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ library
cpp-options: "-DNEW_FUNCTOR_CLASSES"
else
build-depends: base >= 4.3 && < 4.9
, semigroups >= 0.6 && < 0.21

if flag(template-haskell-2-11)
build-depends: template-haskell >= 2.11 && < 2.22
Expand Down
13 changes: 9 additions & 4 deletions src/Data/Bounded/Deriving/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ module Data.Bounded.Deriving.Internal (
) where

import Data.Deriving.Internal
import qualified Data.List.NonEmpty as NE
import Data.List.NonEmpty (NonEmpty(..))

import Language.Haskell.TH.Datatype
import Language.Haskell.TH.Lib
Expand Down Expand Up @@ -90,7 +92,7 @@ makeBoundedFun bf name = do
-- given constructors. All constructors must be from the same type.
makeBoundedFunForCons :: BoundedFun -> Name -> [ConstructorInfo] -> Q Exp
makeBoundedFunForCons _ _ [] = noConstructorsError
makeBoundedFunForCons bf tyName cons
makeBoundedFunForCons bf tyName (con:cons')
| not (isProduct || isEnumeration)
= enumerationOrProductError $ nameBase tyName
| isEnumeration
Expand All @@ -102,9 +104,12 @@ makeBoundedFunForCons bf tyName cons
isProduct = isProductType cons
isEnumeration = isEnumerationType cons

cons :: NonEmpty ConstructorInfo
cons = con :| cons'

con1, conN :: Q Exp
con1 = conE $ constructorName $ head cons
conN = conE $ constructorName $ last cons
con1 = conE $ constructorName con
conN = conE $ constructorName $ NE.last cons

pickCon :: Q Exp
pickCon = case bf of
Expand All @@ -114,7 +119,7 @@ makeBoundedFunForCons bf tyName cons
pickConApp :: Q Exp
pickConApp = appsE
$ pickCon
: map varE (replicate (conArity $ head cons) (boundedFunName bf))
: map varE (replicate (conArity con) (boundedFunName bf))

-------------------------------------------------------------------------------
-- Class-specific constants
Expand Down
12 changes: 6 additions & 6 deletions src/Data/Deriving/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import Data.Functor.Classes
)
#endif
import qualified Data.List as List
import Data.List.NonEmpty (NonEmpty(..))
import qualified Data.Map as Map
import Data.Map (Map)
import Data.Maybe
Expand Down Expand Up @@ -824,15 +825,14 @@ conArity :: ConstructorInfo -> Int
conArity (ConstructorInfo { constructorFields = tys }) = length tys

-- | Returns 'True' if it's a datatype with exactly one, non-existential constructor.
isProductType :: [ConstructorInfo] -> Bool
isProductType [con] = null (constructorVars con)
isProductType _ = False
isProductType :: NonEmpty ConstructorInfo -> Bool
isProductType (con :| []) = null (constructorVars con)
isProductType _ = False

-- | Returns 'True' if it's a datatype with one or more nullary, non-GADT
-- constructors.
isEnumerationType :: [ConstructorInfo] -> Bool
isEnumerationType cons@(_:_) = all (App.liftA2 (&&) isNullaryCon isVanillaCon) cons
isEnumerationType _ = False
isEnumerationType :: NonEmpty ConstructorInfo -> Bool
isEnumerationType cons = F.all (App.liftA2 (&&) isNullaryCon isVanillaCon) cons

-- | Returns 'False' if we're dealing with existential quantification or GADTs.
isVanillaCon :: ConstructorInfo -> Bool
Expand Down
8 changes: 6 additions & 2 deletions src/Data/Enum/Deriving/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ module Data.Enum.Deriving.Internal (
) where

import Data.Deriving.Internal
import Data.List.NonEmpty (NonEmpty(..))

import Language.Haskell.TH.Datatype
import Language.Haskell.TH.Lib
Expand Down Expand Up @@ -116,7 +117,7 @@ makeEnumFun ef name = do
-- given constructors. All constructors must be from the same type.
makeEnumFunForCons :: EnumFun -> Name -> Type -> [ConstructorInfo] -> Q Exp
makeEnumFunForCons _ _ _ [] = noConstructorsError
makeEnumFunForCons ef tyName ty cons
makeEnumFunForCons ef tyName ty (con:cons')
| not $ isEnumerationType cons
= enumerationError tyNameBase
| otherwise = case ef of
Expand Down Expand Up @@ -173,8 +174,11 @@ makeEnumFunForCons ef tyName ty cons
tyNameBase :: String
tyNameBase = nameBase tyName

cons :: NonEmpty ConstructorInfo
cons = con :| cons'

maxTagExpr :: Q Exp
maxTagExpr = integerE (length cons - 1) `sigE` conT intTypeName
maxTagExpr = integerE (length cons') `sigE` conT intTypeName

lamOne :: (Name -> Q Exp) -> Q Exp
lamOne f = do
Expand Down
11 changes: 6 additions & 5 deletions src/Data/Ix/Deriving/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ module Data.Ix.Deriving.Internal (
) where

import Data.Deriving.Internal
import Data.List.NonEmpty (NonEmpty(..))

import Language.Haskell.TH.Datatype
import Language.Haskell.TH.Lib
Expand Down Expand Up @@ -95,7 +96,7 @@ makeIxFun ixf name = do
-- given constructors. All constructors must be from the same type.
makeIxFunForCons :: IxFun -> Name -> Type -> [ConstructorInfo] -> Q Exp
makeIxFunForCons _ _ _ [] = noConstructorsError
makeIxFunForCons ixf tyName ty cons
makeIxFunForCons ixf tyName ty (con:cons')
| not (isProduct || isEnumeration)
= enumerationOrProductError $ nameBase tyName
| isEnumeration
Expand Down Expand Up @@ -144,10 +145,7 @@ makeIxFunForCons ixf tyName ty cons
]

| otherwise -- It's a product type
= do let con :: ConstructorInfo
con = head cons

conName :: Name
= do let conName :: Name
conName = constructorName con

conFields :: Int
Expand Down Expand Up @@ -204,6 +202,9 @@ makeIxFunForCons ixf tyName ty cons
mkInRange a b c = varE inRangeValName `appE` tupE [varE a, varE b]
`appE` varE c
where
cons :: NonEmpty ConstructorInfo
cons = con :| cons'

isProduct, isEnumeration :: Bool
isProduct = isProductType cons
isEnumeration = isEnumerationType cons
Expand Down
32 changes: 19 additions & 13 deletions src/Data/Ord/Deriving/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ module Data.Ord.Deriving.Internal (

import Data.Deriving.Internal
import Data.List (partition)
import qualified Data.List.NonEmpty as NE
import Data.List.NonEmpty (NonEmpty(..))
import qualified Data.Map as Map
import Data.Map (Map)

Expand Down Expand Up @@ -277,10 +279,6 @@ makeOrdFunForCons oFun instTypes cons = do
singleConType :: Bool
singleConType = isSingleton cons

firstConName, lastConName :: Name
firstConName = constructorName $ head cons
lastConName = constructorName $ last cons

-- Alternatively, we could look these up from dataConTagMap, but this
-- is slightly faster due to the lack of Map lookups.
firstTag, lastTag :: Int
Expand All @@ -290,22 +288,30 @@ makeOrdFunForCons oFun instTypes cons = do
dataConTagMap :: Map Name Int
dataConTagMap = Map.fromList $ zip (map constructorName cons) [0..]

ordMatches :: ConstructorInfo -> Q Match
ordMatches = makeOrdFunForCon oFun v2 v2Hash tvMap singleConType
firstTag firstConName lastTag lastConName
dataConTagMap

ordFunRhs :: Q Exp
ordFunRhs
| null cons
= conE eqDataName
ordFunRhs =
case cons of
[] -> conE eqDataName
c:cs -> ordFunRhsNonEmptyCons (c :| cs)

ordFunRhsNonEmptyCons :: NonEmpty ConstructorInfo -> Q Exp
ordFunRhsNonEmptyCons cs@(c :| _)
| length nullaryCons <= 2
= caseE (varE v1) $ map ordMatches cons
= caseE (varE v1) $ map ordMatches $ NE.toList cs
| null nonNullaryCons
= mkTagCmp
| otherwise
= caseE (varE v1) $ map ordMatches nonNullaryCons
++ [match wildP (normalB mkTagCmp) []]
where
firstConName, lastConName :: Name
firstConName = constructorName c
lastConName = constructorName $ NE.last cs

ordMatches :: ConstructorInfo -> Q Match
ordMatches = makeOrdFunForCon oFun v2 v2Hash tvMap singleConType
firstTag firstConName lastTag lastConName
dataConTagMap

mkTagCmp :: Q Exp
mkTagCmp = untagExpr [(v1, v1Hash), (v2, v2Hash)] $
Expand Down

0 comments on commit 7571f4b

Please sign in to comment.