on master help sign in
Preserve empty ‘forall’s
commit2517d98ba1141ce9e1046e6942bb63de249ee164 authormrkkrp <mark.karpov@tweag.io> dateSat 2 Nov 2019, 14:57 (7 years ago) parentfa96da7
6 files changed+57−9
CHANGELOG.md+3 data/examples/other/empty-forall-out.hs+18 data/examples/other/empty-forall.hs+17 src/Ormolu/Printer/Meat/Declaration/Data.hs+7−4 src/Ormolu/Printer/Meat/Declaration/Rule.hs+11−4 src/Ormolu/Printer/Meat/Type.hs+1−1
CHANGELOG.md+3
@@ -18,6 +18,9 @@
1818 inside the export list. See [issue
1919 430](https://github.com/tweag/ormolu/issues/430).
2020
21+* Empty `forall`s are now correctly preserved. See [issue
22+ 429](https://github.com/tweag/ormolu/issues/429).
23+
2124## Ormolu 0.0.1.0
2225
2326* Initial release.
data/examples/other/empty-forall-out.hsnew file+18
@@ -0,0 +1,18 @@
1+-- Empty foralls are handled correctly in different situations.
2+
3+data D = forall. D Int
4+
5+data G where
6+ G :: forall. Int -> G
7+
8+f :: forall. a -> a
9+f x = x
10+
11+type family T x where
12+ forall. T x = x
13+
14+{-# RULES
15+"r"
16+ r a =
17+ ()
18+ #-}
data/examples/other/empty-forall.hsnew file+17
@@ -0,0 +1,17 @@
1+-- Empty foralls are handled correctly in different situations.
2+
3+data D = forall. D Int
4+
5+data G where
6+ G :: forall. Int -> G
7+
8+f :: forall. a -> a
9+f x = x
10+
11+type family T x where
12+ forall. T x = x
13+
14+{-# RULES
15+ "r"
16+ forall. r a = ()
17+#-}
src/Ormolu/Printer/Meat/Declaration/Data.hs+7−4
@@ -104,8 +104,9 @@ p_conDecl = \case
104104 then newline
105105 else breakpoint
106106 interArgBreak
107− p_forallBndrs p_hsTyVarBndr (hsq_explicit con_qvars)
108− unless (null $ hsq_explicit con_qvars) interArgBreak
107+ when (unLoc con_forall) $ do
108+ p_forallBndrs p_hsTyVarBndr (hsq_explicit con_qvars)
109+ interArgBreak
109110 forM_ con_mb_cxt p_lhsContext
110111 case con_args of
111112 PrefixCon xs -> do
@@ -126,12 +127,14 @@ p_conDecl = \case
126127 mapM_ (p_hsDocString Pipe True) con_doc
127128 let conDeclSpn =
128129 [getLoc con_name]
130+ <> [getLoc con_forall]
129131 <> fmap getLoc con_ex_tvs
130132 <> maybeToList (fmap getLoc con_mb_cxt)
131133 <> conArgsSpans con_args
132134 switchLayout conDeclSpn $ do
133− p_forallBndrs p_hsTyVarBndr con_ex_tvs
134− unless (null con_ex_tvs) breakpoint
135+ when (unLoc con_forall) $ do
136+ p_forallBndrs p_hsTyVarBndr con_ex_tvs
137+ breakpoint
135138 forM_ con_mb_cxt p_lhsContext
136139 case con_args of
137140 PrefixCon xs -> do
src/Ormolu/Printer/Meat/Declaration/Rule.hs+11−4
@@ -8,7 +8,7 @@ module Ormolu.Printer.Meat.Declaration.Rule
88where
99
1010import BasicTypes
11−import Data.Maybe (fromMaybe)
11+import Control.Monad (unless)
1212import GHC
1313import Ormolu.Printer.Combinators
1414import Ormolu.Printer.Meat.Common
@@ -31,9 +31,16 @@ p_ruleDecl = \case
3131 space
3232 p_activation activation
3333 space
34− p_forallBndrs p_hsTyVarBndr (fromMaybe [] tyvars)
35− space
36− p_forallBndrs p_ruleBndr ruleBndrs
34+ case tyvars of
35+ Nothing -> return ()
36+ Just xs -> do
37+ p_forallBndrs p_hsTyVarBndr xs
38+ space
39+ -- NOTE It appears that there is no way to tell if there was an empty
40+ -- forall in the input or no forall at all. We do not want to add
41+ -- redundant foralls, so let's just skip the empty ones.
42+ unless (null ruleBndrs) $
43+ p_forallBndrs p_ruleBndr ruleBndrs
3744 breakpoint
3845 inci $ do
3946 located lhs p_hsExpr
src/Ormolu/Printer/Meat/Type.hs+1−1
@@ -184,7 +184,7 @@ p_hsTyVarBndr = \case
184184
185185-- | Render several @forall@-ed variables.
186186p_forallBndrs :: Data a => (a -> R ()) -> [Located a] -> R ()
187−p_forallBndrs _ [] = return ()
187+p_forallBndrs _ [] = txt "forall."
188188p_forallBndrs p tyvars =
189189 switchLayout (getLoc <$> tyvars) $ do
190190 txt "forall"