Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wingman: Don't clobber where clauses #2184

Merged
merged 4 commits into from
Sep 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In what way is this not a perfect check? I don't quite catch your meaning here.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also don't quite understand why this produces ugly code.

-- 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