Skip to content

Commit b4e5bf2

Browse files
committed
More tests, handle Void types
1 parent 8838396 commit b4e5bf2

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

examples/passing/Deriving.purs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@ module Main where
33
import Prelude
44
import Test.Assert
55

6+
data V
7+
8+
derive instance eqV :: Eq V
9+
10+
derive instance ordV :: Ord V
11+
612
data X = X Int | Y String
713

814
derive instance eqX :: Eq X

src/Language/PureScript/Sugar/TypeClasses/Deriving.hs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,9 @@ deriveEq mn ds tyConNm = do
251251
preludeEq = App . App (Var (Qualified (Just (ModuleName [ProperName C.prelude])) (Ident C.eq)))
252252

253253
addCatch :: [CaseAlternative] -> [CaseAlternative]
254-
addCatch = (++ [catchAll])
254+
addCatch xs
255+
| length xs /= 1 = xs ++ [catchAll]
256+
| otherwise = xs -- Avoid redundant case
255257
where
256258
catchAll = CaseAlternative [NullBinder, NullBinder] (Right (BooleanLiteral False))
257259

@@ -290,7 +292,7 @@ deriveOrd mn ds tyConNm = do
290292
mkCompareFunction (DataDeclaration _ _ _ args) = do
291293
x <- freshIdent "x"
292294
y <- freshIdent "y"
293-
lamCase2 x y <$> (concat <$> mapM mkCtorClauses (splitLast args))
295+
lamCase2 x y <$> (addCatch . concat <$> mapM mkCtorClauses (splitLast args))
294296
mkCompareFunction (PositionedDeclaration _ _ d) = mkCompareFunction d
295297
mkCompareFunction _ = internalError "mkCompareFunction: expected DataDeclaration"
296298

@@ -299,6 +301,13 @@ deriveOrd mn ds tyConNm = do
299301
splitLast [x] = [(x, True)]
300302
splitLast (x : xs) = (x, False) : splitLast xs
301303

304+
addCatch :: [CaseAlternative] -> [CaseAlternative]
305+
addCatch xs
306+
| null xs = [catchAll] -- No type constructors
307+
| otherwise = xs
308+
where
309+
catchAll = CaseAlternative [NullBinder, NullBinder] (Right (preludeCtor "EQ"))
310+
302311
preludeCtor :: String -> Expr
303312
preludeCtor = Constructor . Qualified (Just (ModuleName [ProperName C.prelude])) . ProperName
304313

0 commit comments

Comments
 (0)