Skip to content

Commit 3f49902

Browse files
committed
Fix RecordWildCards regression using new haskell-names, and fix bug in R{a=x,..} construction
1 parent 6debfa8 commit 3f49902

File tree

4 files changed

+49
-32
lines changed

4 files changed

+49
-32
lines changed

src/Fay/Compiler/Exp.hs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import Control.Applicative
2929
import Control.Monad.Error
3030
import Control.Monad.RWS
3131
import Language.Haskell.Exts.Annotated
32+
import Language.Haskell.Names
3233

3334
-- | Compile Haskell expression.
3435
compileExp :: S.Exp -> Compile JsExp
@@ -287,14 +288,18 @@ compileRecConstr name fieldUpdates = do
287288
updateStmt (unAnn -> o) (FieldUpdate _ (unAnn -> field) value) = do
288289
exp <- compileExp value
289290
return [JsSetProp (JsNameVar $ unQualify o) (JsNameVar $ unQualify field) exp]
290-
updateStmt name (FieldWildcard _) = do
291-
fields <- map (UnQual ()) <$> recToFields name
291+
updateStmt name (FieldWildcard (wildcardFields -> fields)) = do
292292
return $ for fields $ \fieldName -> JsSetProp (JsNameVar $ unAnn name)
293293
(JsNameVar fieldName)
294294
(JsName $ JsNameVar fieldName)
295295
-- I couldn't find a code that generates (FieldUpdate (FieldPun ..))
296296
updateStmt _ u = error ("updateStmt: " ++ show u)
297297

298+
wildcardFields l = case l of
299+
Scoped (RecExpWildcard es) _ -> map (\(OrigName _ o) -> unQualify $ gname2Qname o) . map fst $ es
300+
_ -> []
301+
302+
298303
-- | Compile a record update.
299304
compileRecUpdate :: S.Exp -> [S.FieldUpdate] -> Compile JsExp
300305
compileRecUpdate rec fieldUpdates = do

tests/recordWildCards.hs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
{-# LANGUAGE NamedFieldPuns #-}
2+
{-# LANGUAGE RecordWildCards #-}
3+
4+
import FFI
5+
import Prelude
6+
7+
data C = C { a :: Int, b :: Int, c :: Int, d :: Int }
8+
9+
data X = X { foo :: Int } | Y { foo :: Int }
10+
11+
partialMatch :: C -> Int
12+
partialMatch C{a=x, ..} = x + d
13+
14+
con :: C
15+
con = let {a=10; b=20; c=30; d=40} in C{..}
16+
17+
match :: X -> Int
18+
match X{..} = foo
19+
20+
partialCon :: C
21+
partialCon = let a = 11; b = 2; c = 3; d = 4 in C { a = 1, ..}
22+
23+
partialMatch2 c =
24+
let a = 100
25+
in case c of
26+
C{a=x,..} -> a
27+
28+
main = do
29+
print con
30+
print partialCon
31+
32+
print $ match X{foo=9}
33+
print $ partialMatch C{a=1, b=2, c=3, d=4}
34+
print $ partialMatch2 $ C 1 2 3 4
35+
36+
37+
-- non exhaustive pattern match in `match`
38+
let y = Y{foo=6}
39+
print (match y)
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1-
5
21
{ instance: 'C', a: 10, b: 20, c: 30, d: 40 }
2+
{ instance: 'C', a: 1, b: 2, c: 3, d: 4 }
33
9
4+
5
5+
100

tests/regressions/recordWildCards.hs

Lines changed: 0 additions & 29 deletions
This file was deleted.

0 commit comments

Comments
 (0)