Adapt to ghc-8.8 (deps are not ready though)

remotes/felixonmars/release
Lennart Spitzner 2019-08-31 23:19:59 +02:00
parent c97f6dd559
commit 91d6e18aba
10 changed files with 155 additions and 82 deletions

View File

@ -84,10 +84,10 @@ library {
-fno-warn-redundant-constraints -fno-warn-redundant-constraints
} }
build-depends: build-depends:
{ base >=4.9 && <4.13 { base >=4.9 && <4.14
, ghc >=8.0.1 && <8.7 , ghc >=8.0.1 && <8.9
, ghc-paths >=0.1.0.9 && <0.2 , ghc-paths >=0.1.0.9 && <0.2
, ghc-exactprint >=0.5.8 && <0.6.2 , ghc-exactprint >=0.5.8 && <0.6.3
, transformers >=0.5.2.0 && <0.6 , transformers >=0.5.2.0 && <0.6
, containers >=0.5.7.1 && <0.7 , containers >=0.5.7.1 && <0.7
, mtl >=2.2.1 && <2.3 , mtl >=2.2.1 && <2.3
@ -112,7 +112,7 @@ library {
, semigroups >=0.18.2 && <0.19 , semigroups >=0.18.2 && <0.19
, cmdargs >=0.10.14 && <0.11 , cmdargs >=0.10.14 && <0.11
, czipwith >=1.0.1.0 && <1.1 , czipwith >=1.0.1.0 && <1.1
, ghc-boot-th >=8.0.1 && <8.7 , ghc-boot-th >=8.0.1 && <8.9
, filepath >=1.4.1.0 && <1.5 , filepath >=1.4.1.0 && <1.5
, random >= 1.1 && <1.2 , random >= 1.1 && <1.2
} }

View File

@ -1220,6 +1220,7 @@ type instance MyFam Bool = String
type instance MyFam (Maybe a) = a -> Bool type instance MyFam (Maybe a) = a -> Bool
#test simple-typefam-instance-parens #test simple-typefam-instance-parens
#pending the parens cause problems since ghc-8.8
type instance (MyFam (String -> Int)) = String type instance (MyFam (String -> Int)) = String
@ -1237,6 +1238,7 @@ type instance MyFam Bool -- This is an odd one
= AnotherType -- Here's another = AnotherType -- Here's another
#test simple-typefam-instance-parens-comment #test simple-typefam-instance-parens-comment
#pending the parens cause problems since ghc-8.8
-- | A happy family -- | A happy family
type instance (MyFam Bool) -- This is an odd one type instance (MyFam Bool) -- This is an odd one

View File

@ -607,7 +607,7 @@ go _ ((IRTypeError ps t1 t2, _) : _) = Left $ makeError ps t1 t2
go _ ((_, IRTypeError ps t1 t2) : _) = Left $ makeError ps t1 t2 go _ ((_, IRTypeError ps t1 t2) : _) = Left $ makeError ps t1 t2
#test issue 89 - type-family-instance #test issue 89 - type-family-instance
type instance (XPure StageParse) = () type instance XPure StageParse = ()
type Pair a = (a, a) type Pair a = (a, a)
#test issue 144 #test issue 144

View File

@ -173,14 +173,10 @@ layoutBriDocM = \case
-- layoutResetSepSpace -- layoutResetSepSpace
priors priors
`forM_` \(ExactPrint.Types.Comment comment _ _, ExactPrint.Types.DP (y, x)) -> `forM_` \(ExactPrint.Types.Comment comment _ _, ExactPrint.Types.DP (y, x)) ->
do when (not $ comment == "(" || comment == ")") $ do
case comment of case comment of
('#':_) -> layoutMoveToCommentPos y (-999) ('#':_) -> layoutMoveToCommentPos y (-999)
-- ^ evil hack for CPP -- ^ evil hack for CPP
"(" -> pure ()
")" -> pure ()
-- ^ these two fix the formatting of parens
-- on the lhs of type alias defs
_ -> layoutMoveToCommentPos y x _ -> layoutMoveToCommentPos y x
-- fixedX <- fixMoveToLineByIsNewline x -- fixedX <- fixMoveToLineByIsNewline x
-- replicateM_ fixedX layoutWriteNewline -- replicateM_ fixedX layoutWriteNewline
@ -217,7 +213,7 @@ layoutBriDocM = \case
Nothing -> pure () Nothing -> pure ()
Just comments -> do Just comments -> do
comments `forM_` \(ExactPrint.Types.Comment comment _ _, ExactPrint.Types.DP (y, x)) -> comments `forM_` \(ExactPrint.Types.Comment comment _ _, ExactPrint.Types.DP (y, x)) ->
do when (not $ comment == "(" || comment == ")") $ do
-- evil hack for CPP: -- evil hack for CPP:
case comment of case comment of
('#':_) -> layoutMoveToCommentPos y (-999) ('#':_) -> layoutMoveToCommentPos y (-999)
@ -251,7 +247,7 @@ layoutBriDocM = \case
Nothing -> pure () Nothing -> pure ()
Just comments -> do Just comments -> do
comments `forM_` \(ExactPrint.Types.Comment comment _ _, ExactPrint.Types.DP (y, x)) -> comments `forM_` \(ExactPrint.Types.Comment comment _ _, ExactPrint.Types.DP (y, x)) ->
do when (not $ comment == "(" || comment == ")") $ do
case comment of case comment of
('#':_) -> layoutMoveToCommentPos y (-999) ('#':_) -> layoutMoveToCommentPos y (-999)
-- ^ evil hack for CPP -- ^ evil hack for CPP

View File

@ -276,7 +276,10 @@ foldedAnnKeys ast = SYB.everything
Set.singleton Set.singleton
[ SYB.gmapQi 1 (\t -> ExactPrint.mkAnnKey $ L l t) x [ SYB.gmapQi 1 (\t -> ExactPrint.mkAnnKey $ L l t) x
| locTyCon == SYB.typeRepTyCon (SYB.typeOf x) | locTyCon == SYB.typeRepTyCon (SYB.typeOf x)
, l <- SYB.gmapQi 0 SYB.cast x , l :: SrcSpan <- SYB.gmapQi 0 SYB.cast x
-- for some reason, ghc-8.8 has forgotten how to infer the type of l,
-- even though it is passed to mkAnnKey above, which only accepts
-- SrcSpan.
] ]
) )
ast ast

View File

@ -20,6 +20,7 @@ where
import Language.Haskell.Brittany.Internal.Types import Language.Haskell.Brittany.Internal.Types
import Language.Haskell.Brittany.Internal.LayouterBasics import Language.Haskell.Brittany.Internal.LayouterBasics
import Language.Haskell.Brittany.Internal.Config.Types import Language.Haskell.Brittany.Internal.Config.Types
import Language.Haskell.Brittany.Internal.Layouters.Type
import qualified Language.Haskell.GHC.ExactPrint as ExactPrint import qualified Language.Haskell.GHC.ExactPrint as ExactPrint
import qualified Language.Haskell.GHC.ExactPrint.Types as ExactPrint import qualified Language.Haskell.GHC.ExactPrint.Types as ExactPrint
@ -743,7 +744,7 @@ layoutSynDecl isInfix wrapNodeRest name vars typ = do
let (a : b : rest) = vars let (a : b : rest) = vars
hasOwnParens <- hasAnnKeywordComment a AnnOpenP hasOwnParens <- hasAnnKeywordComment a AnnOpenP
-- This isn't quite right, but does give syntactically valid results -- This isn't quite right, but does give syntactically valid results
let needsParens = not $ null rest || hasOwnParens let needsParens = not (null rest) || hasOwnParens
docSeq docSeq
$ [ docLit $ Text.pack "type" $ [ docLit $ Text.pack "type"
, docSeparator , docSeparator
@ -800,24 +801,36 @@ layoutTyVarBndr needsSep lbndr@(L _ bndr) = do
-- TyFamInstDecl -- TyFamInstDecl
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
layoutTyFamInstDecl :: Bool -> ToBriDoc TyFamInstDecl layoutTyFamInstDecl :: Bool -> ToBriDoc TyFamInstDecl
layoutTyFamInstDecl inClass (L loc tfid) = do layoutTyFamInstDecl inClass (L loc tfid) = do
let let
#if MIN_VERSION_ghc(8,6,0) #if MIN_VERSION_ghc(8,8,0)
linst = L loc (TyFamInstD NoExt tfid)
feqn@(FamEqn _ name bndrsMay pats _fixity typ) = hsib_body $ tfid_eqn tfid
-- bndrsMay isJust e.g. with
-- type instance forall a . MyType (Maybe a) = Either () a
lfeqn = L loc feqn
#elif MIN_VERSION_ghc(8,6,0)
linst = L loc (TyFamInstD NoExt tfid) linst = L loc (TyFamInstD NoExt tfid)
feqn@(FamEqn _ name pats _fixity typ) = hsib_body $ tfid_eqn tfid feqn@(FamEqn _ name pats _fixity typ) = hsib_body $ tfid_eqn tfid
bndrsMay = Nothing
lfeqn = L loc feqn lfeqn = L loc feqn
#elif MIN_VERSION_ghc(8,4,0) #elif MIN_VERSION_ghc(8,4,0)
linst = L loc (TyFamInstD tfid) linst = L loc (TyFamInstD tfid)
feqn@(FamEqn name pats _fixity typ) = hsib_body $ tfid_eqn tfid feqn@(FamEqn name pats _fixity typ) = hsib_body $ tfid_eqn tfid
bndrsMay = Nothing
lfeqn = L loc feqn lfeqn = L loc feqn
#elif MIN_VERSION_ghc(8,2,0) #elif MIN_VERSION_ghc(8,2,0)
linst = L loc (TyFamInstD tfid) linst = L loc (TyFamInstD tfid)
lfeqn@(L _ (TyFamEqn name boundPats _fixity typ)) = tfid_eqn tfid lfeqn@(L _ (TyFamEqn name boundPats _fixity typ)) = tfid_eqn tfid
bndrsMay = Nothing
pats = hsib_body boundPats pats = hsib_body boundPats
#else #else
linst = L loc (TyFamInstD tfid) linst = L loc (TyFamInstD tfid)
lfeqn@(L _ (TyFamEqn name boundPats typ)) = tfid_eqn tfid lfeqn@(L _ (TyFamEqn name boundPats typ)) = tfid_eqn tfid
bndrsMay = Nothing
pats = hsib_body boundPats pats = hsib_body boundPats
#endif #endif
docWrapNodePrior linst $ do docWrapNodePrior linst $ do
@ -828,15 +841,23 @@ layoutTyFamInstDecl inClass (L loc tfid) = do
then docLit $ Text.pack "type" then docLit $ Text.pack "type"
else docSeq else docSeq
[appSep . docLit $ Text.pack "type", docLit $ Text.pack "instance"] [appSep . docLit $ Text.pack "type", docLit $ Text.pack "instance"]
makeForallDoc :: [LHsTyVarBndr GhcPs] -> ToBriDocM BriDocNumbered
makeForallDoc bndrs = do
bndrDocs <- layoutTyVarBndrs bndrs
docSeq
( [docLit (Text.pack "forall")]
++ processTyVarBndrsSingleline bndrDocs
)
lhs = lhs =
docWrapNode lfeqn docWrapNode lfeqn
. appSep . appSep
. docWrapNodeRest linst . docWrapNodeRest linst
. docSeq . docSeq
$ (appSep instanceDoc :) $ [appSep instanceDoc]
$ [ docParenL | needsParens ] ++ [ makeForallDoc foralls | Just foralls <- [bndrsMay] ]
++ [ docParenL | needsParens ]
++ [appSep $ docWrapNode name $ docLit nameStr] ++ [appSep $ docWrapNode name $ docLit nameStr]
++ intersperse docSeparator (layoutType <$> pats) ++ intersperse docSeparator (layoutHsTyPats pats)
++ [ docParenR | needsParens ] ++ [ docParenR | needsParens ]
hasComments <- (||) hasComments <- (||)
<$> hasAnyRegularCommentsConnected lfeqn <$> hasAnyRegularCommentsConnected lfeqn
@ -845,6 +866,20 @@ layoutTyFamInstDecl inClass (L loc tfid) = do
layoutLhsAndType hasComments lhs "=" typeDoc layoutLhsAndType hasComments lhs "=" typeDoc
#if MIN_VERSION_ghc(8,8,0)
layoutHsTyPats :: [HsArg (LHsType GhcPs) (LHsKind GhcPs)] -> [ToBriDocM BriDocNumbered]
layoutHsTyPats pats = pats <&> \case
HsValArg tm -> layoutType tm
HsTypeArg _l ty -> docSeq [docLit $ Text.pack "@", layoutType ty]
-- we ignore the SourceLoc here.. this LPat not being (L _ Pat{}) change
-- is a bit strange. Hopefully this does not ignore any important
-- annotations.
HsArgPar _l -> error "brittany internal error: HsArgPar{}"
#else
layoutHsTyPats :: [LHsType GhcPs] -> [ToBriDocM BriDocNumbered]
layoutHsTyPats pats = layoutType <$> pats
#endif
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
-- ClsInstDecl -- ClsInstDecl
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------

View File

@ -278,7 +278,11 @@ layoutExpr lexpr@(L _ expr) = do
expDoc1 expDoc1
expDoc2 expDoc2
] ]
#if MIN_VERSION_ghc(8,6,0) /* ghc-8.6 */ #if MIN_VERSION_ghc(8,8,0) /* ghc-8.8 */
HsAppType _ _ XHsWildCardBndrs{} ->
error "brittany internal error: HsAppType XHsWildCardBndrs"
HsAppType _ exp1 (HsWC _ ty1) -> do
#elif MIN_VERSION_ghc(8,6,0) /* ghc-8.6 */
HsAppType XHsWildCardBndrs{} _ -> HsAppType XHsWildCardBndrs{} _ ->
error "brittany internal error: HsAppType XHsWildCardBndrs" error "brittany internal error: HsAppType XHsWildCardBndrs"
HsAppType (HsWC _ ty1) exp1 -> do HsAppType (HsWC _ ty1) exp1 -> do
@ -1034,7 +1038,13 @@ layoutExpr lexpr@(L _ expr) = do
Ambiguous n _ -> (lfield, lrdrNameToText n, rFExpDoc) Ambiguous n _ -> (lfield, lrdrNameToText n, rFExpDoc)
#endif #endif
recordExpression indentPolicy lexpr rExprDoc rFs recordExpression indentPolicy lexpr rExprDoc rFs
#if MIN_VERSION_ghc(8,6,0) /* ghc-8.6 */ #if MIN_VERSION_ghc(8,8,0) /* ghc-8.6 */
ExprWithTySig _ _ (HsWC _ XHsImplicitBndrs{}) ->
error "brittany internal error: ExprWithTySig HsWC XHsImplicitBndrs"
ExprWithTySig _ _ XHsWildCardBndrs{} ->
error "brittany internal error: ExprWithTySig XHsWildCardBndrs"
ExprWithTySig _ exp1 (HsWC _ (HsIB _ typ1)) -> do
#elif MIN_VERSION_ghc(8,6,0) /* ghc-8.6 */
ExprWithTySig (HsWC _ XHsImplicitBndrs{}) _ -> ExprWithTySig (HsWC _ XHsImplicitBndrs{}) _ ->
error "brittany internal error: ExprWithTySig HsWC XHsImplicitBndrs" error "brittany internal error: ExprWithTySig HsWC XHsImplicitBndrs"
ExprWithTySig XHsWildCardBndrs{} _ -> ExprWithTySig XHsWildCardBndrs{} _ ->

View File

@ -1,4 +1,5 @@
{-# LANGUAGE DataKinds #-} {-# LANGUAGE DataKinds #-}
{-# LANGUAGE ViewPatterns #-}
module Language.Haskell.Brittany.Internal.Layouters.Pattern module Language.Haskell.Brittany.Internal.Layouters.Pattern
( layoutPat ( layoutPat
@ -13,7 +14,13 @@ where
import Language.Haskell.Brittany.Internal.Types import Language.Haskell.Brittany.Internal.Types
import Language.Haskell.Brittany.Internal.LayouterBasics import Language.Haskell.Brittany.Internal.LayouterBasics
import GHC ( Located, runGhc, GenLocated(L), moduleNameString, ol_val ) import GHC ( Located
, runGhc
, GenLocated(L)
, moduleNameString
, ol_val
)
import qualified GHC
import HsSyn import HsSyn
import Name import Name
import BasicTypes import BasicTypes
@ -33,8 +40,8 @@ import Language.Haskell.Brittany.Internal.Layouters.Type
-- ^^^^^^^^^^ this part -- ^^^^^^^^^^ this part
-- We will use `case .. of` as the imagined prefix to the examples used in -- We will use `case .. of` as the imagined prefix to the examples used in
-- the different cases below. -- the different cases below.
layoutPat :: ToBriDocC (Pat GhcPs) (Seq BriDocNumbered) layoutPat :: LPat GhcPs -> ToBriDocM (Seq BriDocNumbered)
layoutPat lpat@(L _ pat) = docWrapNode lpat $ case pat of layoutPat (ghcDL -> lpat@(L _ pat)) = docWrapNode lpat $ case pat of
WildPat _ -> fmap Seq.singleton $ docLit $ Text.pack "_" WildPat _ -> fmap Seq.singleton $ docLit $ Text.pack "_"
-- _ -> expr -- _ -> expr
#if MIN_VERSION_ghc(8,6,0) /* ghc-8.6 */ #if MIN_VERSION_ghc(8,6,0) /* ghc-8.6 */
@ -51,7 +58,9 @@ layoutPat lpat@(L _ pat) = docWrapNode lpat $ case pat of
#endif #endif
fmap Seq.singleton $ allocateNode $ litBriDoc lit fmap Seq.singleton $ allocateNode $ litBriDoc lit
-- 0 -> expr -- 0 -> expr
#if MIN_VERSION_ghc(8,6,0) /* ghc-8.6 */ #if MIN_VERSION_ghc(8,8,0) /* ghc-8.8 */
ParPat _ inner -> do
#elif MIN_VERSION_ghc(8,6,0) /* ghc-8.6 */
ParPat _ inner -> do ParPat _ inner -> do
#else /* ghc-8.0 8.2 8.4 */ #else /* ghc-8.0 8.2 8.4 */
ParPat inner -> do ParPat inner -> do
@ -177,7 +186,9 @@ layoutPat lpat@(L _ pat) = docWrapNode lpat $ case pat of
#endif #endif
-- bind@nestedpat -> expr -- bind@nestedpat -> expr
wrapPatPrepend asPat (docLit $ lrdrNameToText asName <> Text.pack "@") wrapPatPrepend asPat (docLit $ lrdrNameToText asName <> Text.pack "@")
#if MIN_VERSION_ghc(8,6,0) /* ghc-8.6 */ #if MIN_VERSION_ghc(8,8,0) /* ghc-8.8 */
SigPat _ pat1 (HsWC _ (HsIB _ ty1)) -> do
#elif MIN_VERSION_ghc(8,6,0) /* ghc-8.6 */
SigPat (HsWC _ (HsIB _ ty1)) pat1 -> do SigPat (HsWC _ (HsIB _ ty1)) pat1 -> do
#elif MIN_VERSION_ghc(8,2,0) /* ghc-8.2 8.4 */ #elif MIN_VERSION_ghc(8,2,0) /* ghc-8.2 8.4 */
SigPatIn pat1 (HsWC _ (HsIB _ ty1 _)) -> do SigPatIn pat1 (HsWC _ (HsIB _ ty1 _)) -> do
@ -242,13 +253,13 @@ layoutPat lpat@(L _ pat) = docWrapNode lpat $ case pat of
-- else -- else
-- VarPat n -> return $ stringLayouter lpat $ rdrNameToText n -- VarPat n -> return $ stringLayouter lpat $ rdrNameToText n
-- endif -- endif
_ -> return <$> briDocByExactInlineOnly "some unknown pattern" lpat _ -> return <$> briDocByExactInlineOnly "some unknown pattern" (ghcDL lpat)
colsWrapPat :: Seq BriDocNumbered -> ToBriDocM BriDocNumbered colsWrapPat :: Seq BriDocNumbered -> ToBriDocM BriDocNumbered
colsWrapPat = docCols ColPatterns . fmap return . Foldable.toList colsWrapPat = docCols ColPatterns . fmap return . Foldable.toList
wrapPatPrepend wrapPatPrepend
:: Located (Pat GhcPs) :: LPat GhcPs
-> ToBriDocM BriDocNumbered -> ToBriDocM BriDocNumbered
-> ToBriDocM (Seq BriDocNumbered) -> ToBriDocM (Seq BriDocNumbered)
wrapPatPrepend pat prepElem = do wrapPatPrepend pat prepElem = do
@ -260,7 +271,7 @@ wrapPatPrepend pat prepElem = do
return $ x1' Seq.<| xR return $ x1' Seq.<| xR
wrapPatListy wrapPatListy
:: [Located (Pat GhcPs)] :: [LPat GhcPs]
-> String -> String
-> ToBriDocM BriDocNumbered -> ToBriDocM BriDocNumbered
-> ToBriDocM BriDocNumbered -> ToBriDocM BriDocNumbered

View File

@ -2,6 +2,8 @@
module Language.Haskell.Brittany.Internal.Layouters.Type module Language.Haskell.Brittany.Internal.Layouters.Type
( layoutType ( layoutType
, layoutTyVarBndrs
, processTyVarBndrsSingleline
) )
where where
@ -32,21 +34,19 @@ import DataTreePrint
layoutType :: ToBriDoc HsType layoutType :: ToBriDoc HsType
layoutType ltype@(L _ typ) = docWrapNode ltype $ case typ of layoutType ltype@(L _ typ) = docWrapNode ltype $ case typ of
-- _ | traceShow (ExactPrint.Types.mkAnnKey ltype) False -> error "impossible" -- _ | traceShow (ExactPrint.Types.mkAnnKey ltype) False -> error "impossible"
#if MIN_VERSION_ghc(8,2,0)
#if MIN_VERSION_ghc(8,6,0) #if MIN_VERSION_ghc(8,6,0)
HsTyVar _ promoted name -> do HsTyVar _ promoted name -> do
t <- lrdrNameToTextAnn name #else /* ghc-8.2 ghc-8.4 */
case promoted of
Promoted -> docSeq
[ docSeparator
, docTick
, docWrapNode name $ docLit t
]
NotPromoted -> docWrapNode name $ docLit t
#elif MIN_VERSION_ghc(8,2,0) /* ghc-8.2 */
HsTyVar promoted name -> do HsTyVar promoted name -> do
#endif
t <- lrdrNameToTextAnn name t <- lrdrNameToTextAnn name
case promoted of case promoted of
#if MIN_VERSION_ghc(8,8,0)
IsPromoted -> docSeq
#else /* ghc-8.2 8.4 8.6 */
Promoted -> docSeq Promoted -> docSeq
#endif
[ docSeparator [ docSeparator
, docTick , docTick
, docWrapNode name $ docLit t , docWrapNode name $ docLit t
@ -63,32 +63,13 @@ layoutType ltype@(L _ typ) = docWrapNode ltype $ case typ of
HsForAllTy bndrs (L _ (HsQualTy (L _ cntxts) typ2)) -> do HsForAllTy bndrs (L _ (HsQualTy (L _ cntxts) typ2)) -> do
#endif #endif
typeDoc <- docSharedWrapper layoutType typ2 typeDoc <- docSharedWrapper layoutType typ2
tyVarDocs <- bndrs `forM` \case tyVarDocs <- layoutTyVarBndrs bndrs
#if MIN_VERSION_ghc(8,6,0)
(L _ (UserTyVar _ name)) -> return $ (lrdrNameToText name, Nothing)
(L _ (KindedTyVar _ lrdrName kind)) -> do
d <- docSharedWrapper layoutType kind
return $ (lrdrNameToText lrdrName, Just $ d)
(L _ (XTyVarBndr{})) -> error "brittany internal error: XTyVarBndr"
#else
(L _ (UserTyVar name)) -> return $ (lrdrNameToText name, Nothing)
(L _ (KindedTyVar lrdrName kind)) -> do
d <- docSharedWrapper layoutType kind
return $ (lrdrNameToText lrdrName, Just $ d)
#endif
cntxtDocs <- cntxts `forM` docSharedWrapper layoutType cntxtDocs <- cntxts `forM` docSharedWrapper layoutType
let maybeForceML = case typ2 of let maybeForceML = case typ2 of
(L _ HsFunTy{}) -> docForceMultiline (L _ HsFunTy{}) -> docForceMultiline
_ -> id _ -> id
let let
tyVarDocLineList = tyVarDocs >>= \case tyVarDocLineList = processTyVarBndrsSingleline tyVarDocs
(tname, Nothing) -> [docLit $ Text.pack " " <> tname]
(tname, Just doc) -> [ docLit $ Text.pack " ("
<> tname
<> Text.pack " :: "
, docForceSingleline $ doc
, docLit $ Text.pack ")"
]
forallDoc = docAlt forallDoc = docAlt
[ let [ let
open = docLit $ Text.pack "forall" open = docLit $ Text.pack "forall"
@ -142,7 +123,7 @@ layoutType ltype@(L _ typ) = docWrapNode ltype $ case typ of
else let else let
open = docLit $ Text.pack "forall" open = docLit $ Text.pack "forall"
close = docLit $ Text.pack " . " close = docLit $ Text.pack " . "
in docSeq ([open]++tyVarDocLineList++[close]) in docSeq ([open, docSeparator]++tyVarDocLineList++[close])
, docForceSingleline contextDoc , docForceSingleline contextDoc
, docLit $ Text.pack " => " , docLit $ Text.pack " => "
, docForceSingleline typeDoc , docForceSingleline typeDoc
@ -172,31 +153,11 @@ layoutType ltype@(L _ typ) = docWrapNode ltype $ case typ of
HsForAllTy bndrs typ2 -> do HsForAllTy bndrs typ2 -> do
#endif #endif
typeDoc <- layoutType typ2 typeDoc <- layoutType typ2
tyVarDocs <- bndrs `forM` \case tyVarDocs <- layoutTyVarBndrs bndrs
#if MIN_VERSION_ghc(8,6,0)
(L _ (UserTyVar _ name)) -> return $ (lrdrNameToText name, Nothing)
(L _ (KindedTyVar _ lrdrName kind)) -> do
d <- layoutType kind
return $ (lrdrNameToText lrdrName, Just $ return d)
(L _ (XTyVarBndr{})) -> error "brittany internal error: XTyVarBndr"
#else
(L _ (UserTyVar name)) -> return $ (lrdrNameToText name, Nothing)
(L _ (KindedTyVar lrdrName kind)) -> do
d <- layoutType kind
return $ (lrdrNameToText lrdrName, Just $ return d)
#endif
let maybeForceML = case typ2 of let maybeForceML = case typ2 of
(L _ HsFunTy{}) -> docForceMultiline (L _ HsFunTy{}) -> docForceMultiline
_ -> id _ -> id
let let tyVarDocLineList = processTyVarBndrsSingleline tyVarDocs
tyVarDocLineList = tyVarDocs >>= \case
(tname, Nothing) -> [docLit $ Text.pack " " <> tname]
(tname, Just doc) -> [ docLit $ Text.pack " ("
<> tname
<> Text.pack " :: "
, docForceSingleline doc
, docLit $ Text.pack ")"
]
docAlt docAlt
-- forall x . x -- forall x . x
[ docSeq [ docSeq
@ -771,3 +732,46 @@ layoutType ltype@(L _ typ) = docWrapNode ltype $ case typ of
else docLit $ Text.pack "*" else docLit $ Text.pack "*"
XHsType{} -> error "brittany internal error: XHsType" XHsType{} -> error "brittany internal error: XHsType"
#endif #endif
#if MIN_VERSION_ghc(8,8,0)
HsAppKindTy _ ty kind -> do
t <- docSharedWrapper layoutType ty
k <- docSharedWrapper layoutType kind
docAlt
[ docSeq
[ docForceSingleline t
, docSeparator
, docLit $ Text.pack "@"
, docForceSingleline k
]
, docPar
t
(docSeq [docLit $ Text.pack "@", k ])
]
#endif
layoutTyVarBndrs
:: [LHsTyVarBndr GhcPs]
-> ToBriDocM [(Text, Maybe (ToBriDocM BriDocNumbered))]
layoutTyVarBndrs = mapM $ \case
#if MIN_VERSION_ghc(8,6,0)
(L _ (UserTyVar _ name)) -> return $ (lrdrNameToText name, Nothing)
(L _ (KindedTyVar _ lrdrName kind)) -> do
d <- docSharedWrapper layoutType kind
return $ (lrdrNameToText lrdrName, Just $ d)
(L _ (XTyVarBndr{})) -> error "brittany internal error: XTyVarBndr"
#else
(L _ (UserTyVar name)) -> return $ (lrdrNameToText name, Nothing)
(L _ (KindedTyVar lrdrName kind)) -> do
d <- docSharedWrapper layoutType kind
return $ (lrdrNameToText lrdrName, Just $ d)
#endif
processTyVarBndrsSingleline
:: [(Text, Maybe (ToBriDocM BriDocNumbered))] -> [ToBriDocM BriDocNumbered]
processTyVarBndrsSingleline bndrDocs = bndrDocs >>= \case
(tname, Nothing) -> [docLit $ Text.pack " " <> tname]
(tname, Just doc) ->
[ docLit $ Text.pack " (" <> tname <> Text.pack " :: "
, docForceSingleline $ doc
, docLit $ Text.pack ")"
]

View File

@ -18,7 +18,10 @@ import HsExtension as E ( GhcPs )
#endif #endif
import RdrName as E ( RdrName ) import RdrName as E ( RdrName )
#if MIN_VERSION_ghc(8,8,0)
import qualified GHC ( dL, HasSrcSpan, SrcSpanLess )
#endif
import qualified GHC ( Located )
-- more general: -- more general:
@ -410,3 +413,12 @@ type instance IdP GhcPs = RdrName
type GhcPs = RdrName type GhcPs = RdrName
#endif #endif
#if MIN_VERSION_ghc(8,8,0)
ghcDL :: GHC.HasSrcSpan a => a -> GHC.Located (GHC.SrcSpanLess a)
ghcDL = GHC.dL
#else /* ghc-8.0 8.2 8.4 8.6 */
ghcDL :: GHC.Located a -> GHC.Located a
ghcDL x = x
#endif