Skip to content

Commit

Permalink
Wingman: Don't clobber where clauses (#2184)
Browse files Browse the repository at this point in the history
* Extend TopLevelRHS pattern to track the where clause

* Don't case split if there's a where clause

* Add tests

Co-authored-by: Javier Neira <atreyu.bbb@gmail.com>
  • Loading branch information
isovector and jneira committed Sep 14, 2021
1 parent b272941 commit 301a3e4
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 9 deletions.
11 changes: 8 additions & 3 deletions plugins/hls-tactics-plugin/src/Wingman/GHC.hs
Original file line number Diff line number Diff line change
Expand Up @@ -309,13 +309,18 @@ type PatCompat pass = LPat pass

------------------------------------------------------------------------------
-- | Should make sure it's a fun bind
pattern TopLevelRHS :: OccName -> [PatCompat GhcTc] -> LHsExpr GhcTc -> Match GhcTc (LHsExpr GhcTc)
pattern TopLevelRHS name ps body <-
pattern TopLevelRHS
:: OccName
-> [PatCompat GhcTc]
-> LHsExpr GhcTc
-> HsLocalBindsLR GhcTc GhcTc
-> Match GhcTc (LHsExpr GhcTc)
pattern TopLevelRHS name ps body where_binds <-
Match _
(FunRhs (L _ (occName -> name)) _ _)
ps
(GRHSs _
[L _ (GRHS _ [] body)] _)
[L _ (GRHS _ [] body)] (L _ where_binds))


dataConExTys :: DataCon -> [TyCoVar]
Expand Down
26 changes: 20 additions & 6 deletions plugins/hls-tactics-plugin/src/Wingman/LanguageServer.hs
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ mkJudgementAndContext cfg g (TrackedStale binds bmap) rss (TrackedStale tcg tcgm
mkFirstJudgement
ctx
(local_hy <> cls_hy)
(isRhsHole tcg_rss tcs)
(isRhsHoleWithoutWhere tcg_rss tcs)
g
, ctx
)
Expand Down Expand Up @@ -341,6 +341,7 @@ getRhsPosVals (unTrack -> rss) (unTrack -> tcs)
TopLevelRHS name ps
(L (RealSrcSpan span) -- body with no guards and a single defn
(HsVar _ (L _ hole)))
_
| containsSpan rss span -- which contains our span
, isHole $ occName hole -- and the span is a hole
-> flip evalState 0 $ buildTopLevelHypothesis name ps
Expand Down Expand Up @@ -478,12 +479,25 @@ mkIdHypothesis (splitId -> (name, ty)) prov =


------------------------------------------------------------------------------
-- | Is this hole immediately to the right of an equals sign?
isRhsHole :: Tracked age RealSrcSpan -> Tracked age TypecheckedSource -> Bool
isRhsHole (unTrack -> rss) (unTrack -> tcs) =
-- | Is this hole immediately to the right of an equals sign --- and is there
-- no where clause attached to it?
--
-- It's important that there is no where clause because otherwise it gets
-- clobbered. See #2183 for an example.
--
-- This isn't a perfect check, and produces some ugly code. But it's much much
-- better than the alternative, which is to destructively modify the user's
-- AST.
isRhsHoleWithoutWhere
:: Tracked age RealSrcSpan
-> Tracked age TypecheckedSource
-> Bool
isRhsHoleWithoutWhere (unTrack -> rss) (unTrack -> tcs) =
everything (||) (mkQ False $ \case
TopLevelRHS _ _ (L (RealSrcSpan span) _) -> containsSpan rss span
_ -> False
TopLevelRHS _ _
(L (RealSrcSpan span) _)
(EmptyLocalBinds _) -> containsSpan rss span
_ -> False
) tcs


Expand Down
1 change: 1 addition & 0 deletions plugins/hls-tactics-plugin/test/CodeAction/RefineSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ spec = do
refineTest 2 8 "RefineCon"
refineTest 4 10 "RefineReader"
refineTest 8 10 "RefineGADT"
refineTest 2 8 "RefineIntroWhere"

describe "messages" $ do
mkShowMessageTest Refine "" 2 8 "MessageForallA" TacticErrors
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
test :: Maybe Int -> Int
test = \ m_n -> _w0
where
-- Don't delete me!
blah = undefined

6 changes: 6 additions & 0 deletions plugins/hls-tactics-plugin/test/golden/RefineIntroWhere.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
test :: Maybe Int -> Int
test = _
where
-- Don't delete me!
blah = undefined

0 comments on commit 301a3e4

Please # to comment.