Skip to content

Commit 2683a92

Browse files
authored
Wingman: case split on punned record fields (#1739)
* Fix #1736 * Use PatCompat
1 parent 9ae2092 commit 2683a92

File tree

4 files changed

+27
-3
lines changed

4 files changed

+27
-3
lines changed

plugins/hls-tactics-plugin/src/Wingman/CaseSplit.hs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,17 @@ wildifyT (S.map occNameString -> used) = everywhere $ mkT $ \case
5656
------------------------------------------------------------------------------
5757
-- | Replace a 'VarPat' with the given @'Pat' GhcPs@.
5858
rewriteVarPat :: Data a => RdrName -> Pat GhcPs -> a -> a
59-
rewriteVarPat name rep = everywhere $ mkT $ \case
60-
VarPat _ (L _ var) | eqRdrName name var -> rep
61-
(x :: Pat GhcPs) -> x
59+
rewriteVarPat name rep = everywhere $
60+
mkT (\case
61+
VarPat _ (L _ var) | eqRdrName name var -> rep
62+
(x :: Pat GhcPs) -> x
63+
)
64+
`extT` \case
65+
HsRecField lbl _ True
66+
| eqRdrName name $ unLoc $ rdrNameFieldOcc $ unLoc lbl
67+
-> HsRecField lbl (toPatCompat rep) False
68+
(x :: HsRecField' (FieldOcc GhcPs) (PatCompat GhcPs)) -> x
69+
6270

6371

6472
------------------------------------------------------------------------------

plugins/hls-tactics-plugin/test/CodeAction/DestructSpec.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ spec = do
2020
destructTest "gadt" 7 17 "GoldenGADTDestruct.hs"
2121
destructTest "gadt" 8 17 "GoldenGADTDestructCoercion.hs"
2222
destructTest "a" 7 25 "SplitPattern.hs"
23+
destructTest "a" 6 18 "DestructPun.hs"
2324

2425
describe "layout" $ do
2526
destructTest "b" 4 3 "LayoutBind.hs"
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{-# LANGUAGE NamedFieldPuns #-}
2+
3+
4+
data Foo = Foo { a :: Bool, b :: Bool }
5+
6+
foo Foo {a, b} = _
7+
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{-# LANGUAGE NamedFieldPuns #-}
2+
3+
4+
data Foo = Foo { a :: Bool, b :: Bool }
5+
6+
foo Foo {a = False, b} = _
7+
foo Foo {a = True, b} = _
8+

0 commit comments

Comments
 (0)