diff options
author | Alan Zimmerman <alan.zimm@gmail.com> | 2016-05-25 00:09:34 +0200 |
---|---|---|
committer | Alan Zimmerman <alan.zimm@gmail.com> | 2016-06-06 21:52:49 +0200 |
commit | a13cb27960f9bdb0bc9eececf9159f034f113481 (patch) | |
tree | 1f6d154698f022b76042b1b796ca0ed959a2b201 /compiler/hsSyn | |
parent | 1937ef1c506b538f0f93cd290fa4a42fc85ab769 (diff) | |
download | haskell-a13cb27960f9bdb0bc9eececf9159f034f113481.tar.gz |
Merge MatchFixity and HsMatchContext
Summary:
MatchFixity was introduced to facilitate use of API Annotations.
HsMatchContext does the same thing with more detail, but is chased
through all over the place to provide context when processing a Match.
Since we already have MatchFixity in the Match, it may as well provide
the full context.
updates submodule haddock
Test Plan: ./validate
Reviewers: austin, goldfire, bgamari
Subscribers: thomie, mpickering
Differential Revision: https://phabricator.haskell.org/D2271
GHC Trac Issues: #12105
(cherry picked from commit 306ecad591951521ac3f5888ca8be85bf749d271)
Diffstat (limited to 'compiler/hsSyn')
-rw-r--r-- | compiler/hsSyn/Convert.hs | 35 | ||||
-rw-r--r-- | compiler/hsSyn/HsBinds.hs | 39 | ||||
-rw-r--r-- | compiler/hsSyn/HsDecls.hs | 67 | ||||
-rw-r--r-- | compiler/hsSyn/HsExpr.hs | 232 | ||||
-rw-r--r-- | compiler/hsSyn/HsExpr.hs-boot | 23 | ||||
-rw-r--r-- | compiler/hsSyn/HsLit.hs | 4 | ||||
-rw-r--r-- | compiler/hsSyn/HsPat.hs | 17 | ||||
-rw-r--r-- | compiler/hsSyn/HsPat.hs-boot | 4 | ||||
-rw-r--r-- | compiler/hsSyn/HsSyn.hs | 2 | ||||
-rw-r--r-- | compiler/hsSyn/HsTypes.hs | 43 | ||||
-rw-r--r-- | compiler/hsSyn/HsUtils.hs | 36 | ||||
-rw-r--r-- | compiler/hsSyn/PlaceHolder.hs | 27 |
12 files changed, 300 insertions, 229 deletions
diff --git a/compiler/hsSyn/Convert.hs b/compiler/hsSyn/Convert.hs index 63904ed219..8d85ca9332 100644 --- a/compiler/hsSyn/Convert.hs +++ b/compiler/hsSyn/Convert.hs @@ -142,7 +142,7 @@ cvtDec :: TH.Dec -> CvtM (Maybe (LHsDecl RdrName)) cvtDec (TH.ValD pat body ds) | TH.VarP s <- pat = do { s' <- vNameL s - ; cl' <- cvtClause (Clause [] body ds) + ; cl' <- cvtClause (FunRhs s' Prefix) (Clause [] body ds) ; returnJustL $ Hs.ValD $ mkFunBind s' [cl'] } | otherwise @@ -161,7 +161,7 @@ cvtDec (TH.FunD nm cls) <+> text "has no equations") | otherwise = do { nm' <- vNameL nm - ; cls' <- mapM cvtClause cls + ; cls' <- mapM (cvtClause (FunRhs nm' Prefix)) cls ; returnJustL $ Hs.ValD $ mkFunBind nm' cls' } cvtDec (TH.SigD nm typ) @@ -354,7 +354,7 @@ cvtDec (TH.DefaultSigD nm typ) cvtDec (TH.PatSynD nm args dir pat) = do { nm' <- cNameL nm ; args' <- cvtArgs args - ; dir' <- cvtDir dir + ; dir' <- cvtDir nm' dir ; pat' <- cvtPat pat ; returnJustL $ Hs.ValD $ PatSynBind $ PSB nm' placeHolderType args' pat' dir' } @@ -366,10 +366,10 @@ cvtDec (TH.PatSynD nm args dir pat) ; vars' <- mapM (vNameL . mkNameS . nameBase) sels ; return $ Hs.RecordPatSyn $ zipWith RecordPatSynField sels' vars' } - cvtDir Unidir = return Unidirectional - cvtDir ImplBidir = return ImplicitBidirectional - cvtDir (ExplBidir cls) = - do { ms <- mapM cvtClause cls + cvtDir _ Unidir = return Unidirectional + cvtDir _ ImplBidir = return ImplicitBidirectional + cvtDir n (ExplBidir cls) = + do { ms <- mapM (cvtClause (FunRhs n Prefix)) cls ; return $ ExplicitBidirectional $ mkMatchGroup FromSource ms } cvtDec (TH.PatSynSigD nm ty) @@ -730,12 +730,13 @@ cvtLocalDecs doc ds ; unless (null bads) (failWith (mkBadDecMsg doc bads)) ; return (HsValBinds (ValBindsIn (listToBag binds) sigs)) } -cvtClause :: TH.Clause -> CvtM (Hs.LMatch RdrName (LHsExpr RdrName)) -cvtClause (Clause ps body wheres) +cvtClause :: HsMatchContext RdrName + -> TH.Clause -> CvtM (Hs.LMatch RdrName (LHsExpr RdrName)) +cvtClause ctxt (Clause ps body wheres) = do { ps' <- cvtPats ps ; g' <- cvtGuard body ; ds' <- cvtLocalDecs (text "a where clause") wheres - ; returnL $ Hs.Match NonFunBindMatch ps' Nothing + ; returnL $ Hs.Match ctxt ps' Nothing (GRHSs g' (noLoc ds')) } @@ -756,8 +757,9 @@ cvtl e = wrapL (cvt e) cvt (AppE x y) = do { x' <- cvtl x; y' <- cvtl y ; return $ HsApp x' y' } cvt (LamE ps e) = do { ps' <- cvtPats ps; e' <- cvtl e - ; return $ HsLam (mkMatchGroup FromSource [mkSimpleMatch ps' e']) } - cvt (LamCaseE ms) = do { ms' <- mapM cvtMatch ms + ; return $ HsLam (mkMatchGroup FromSource + [mkSimpleMatch LambdaExpr ps' e'])} + cvt (LamCaseE ms) = do { ms' <- mapM (cvtMatch LambdaExpr) ms ; return $ HsLamCase (mkMatchGroup FromSource ms') } cvt (TupE [e]) = do { e' <- cvtl e; return $ HsPar e' } @@ -777,7 +779,7 @@ cvtl e = wrapL (cvt e) ; return $ HsMultiIf placeHolderType alts' } cvt (LetE ds e) = do { ds' <- cvtLocalDecs (text "a let expression") ds ; e' <- cvtl e; return $ HsLet (noLoc ds') e' } - cvt (CaseE e ms) = do { e' <- cvtl e; ms' <- mapM cvtMatch ms + cvt (CaseE e ms) = do { e' <- cvtl e; ms' <- mapM (cvtMatch CaseAlt) ms ; return $ HsCase e' (mkMatchGroup FromSource ms') } cvt (DoE ss) = cvtHsDo DoExpr ss cvt (CompE ss) = cvtHsDo ListComp ss @@ -950,12 +952,13 @@ cvtStmt (TH.ParS dss) = do { dss' <- mapM cvt_one dss; returnL $ ParStmt dss' n where cvt_one ds = do { ds' <- cvtStmts ds; return (ParStmtBlock ds' undefined noSyntaxExpr) } -cvtMatch :: TH.Match -> CvtM (Hs.LMatch RdrName (LHsExpr RdrName)) -cvtMatch (TH.Match p body decs) +cvtMatch :: HsMatchContext RdrName + -> TH.Match -> CvtM (Hs.LMatch RdrName (LHsExpr RdrName)) +cvtMatch ctxt (TH.Match p body decs) = do { p' <- cvtPat p ; g' <- cvtGuard body ; decs' <- cvtLocalDecs (text "a where clause") decs - ; returnL $ Hs.Match NonFunBindMatch [p'] Nothing + ; returnL $ Hs.Match ctxt [p'] Nothing (GRHSs g' (noLoc decs')) } cvtGuard :: TH.Body -> CvtM [LGRHS RdrName (LHsExpr RdrName)] diff --git a/compiler/hsSyn/HsBinds.hs b/compiler/hsSyn/HsBinds.hs index ce3d3c7d2e..5383ee5c6b 100644 --- a/compiler/hsSyn/HsBinds.hs +++ b/compiler/hsSyn/HsBinds.hs @@ -22,7 +22,7 @@ import {-# SOURCE #-} HsExpr ( pprExpr, LHsExpr, GRHSs, pprPatBind ) import {-# SOURCE #-} HsPat ( LPat ) -import PlaceHolder ( PostTc,PostRn,DataId ) +import PlaceHolder ( PostTc,PostRn,DataId,OutputableBndrId ) import HsTypes import PprCore () import CoreSyn @@ -405,12 +405,14 @@ Specifically, it's just an error thunk -} -instance (OutputableBndr idL, OutputableBndr idR) => Outputable (HsLocalBindsLR idL idR) where +instance (OutputableBndrId idL, OutputableBndrId idR) + => Outputable (HsLocalBindsLR idL idR) where ppr (HsValBinds bs) = ppr bs ppr (HsIPBinds bs) = ppr bs ppr EmptyLocalBinds = empty -instance (OutputableBndr idL, OutputableBndr idR) => Outputable (HsValBindsLR idL idR) where +instance (OutputableBndrId idL, OutputableBndrId idR) + => Outputable (HsValBindsLR idL idR) where ppr (ValBindsIn binds sigs) = pprDeclList (pprLHsBindsForUser binds sigs) @@ -425,12 +427,14 @@ instance (OutputableBndr idL, OutputableBndr idR) => Outputable (HsValBindsLR id pp_rec Recursive = text "rec" pp_rec NonRecursive = text "nonrec" -pprLHsBinds :: (OutputableBndr idL, OutputableBndr idR) => LHsBindsLR idL idR -> SDoc +pprLHsBinds :: (OutputableBndrId idL, OutputableBndrId idR) + => LHsBindsLR idL idR -> SDoc pprLHsBinds binds | isEmptyLHsBinds binds = empty | otherwise = pprDeclList (map ppr (bagToList binds)) -pprLHsBindsForUser :: (OutputableBndr idL, OutputableBndr idR, OutputableBndr id2) +pprLHsBindsForUser :: (OutputableBndrId idL, OutputableBndrId idR, + OutputableBndrId id2) => LHsBindsLR idL idR -> [LSig id2] -> [SDoc] -- pprLHsBindsForUser is different to pprLHsBinds because -- a) No braces: 'let' and 'where' include a list of HsBindGroups @@ -491,7 +495,6 @@ plusHsValBinds (ValBindsOut ds1 sigs1) (ValBindsOut ds2 sigs2) plusHsValBinds _ _ = panic "HsBinds.plusHsValBinds" - {- What AbsBinds means ~~~~~~~~~~~~~~~~~~~ @@ -518,10 +521,12 @@ So the desugarer tries to do a better job: in (fm,gm) -} -instance (OutputableBndr idL, OutputableBndr idR) => Outputable (HsBindLR idL idR) where +instance (OutputableBndrId idL, OutputableBndrId idR) + => Outputable (HsBindLR idL idR) where ppr mbind = ppr_monobind mbind -ppr_monobind :: (OutputableBndr idL, OutputableBndr idR) => HsBindLR idL idR -> SDoc +ppr_monobind :: (OutputableBndrId idL, OutputableBndrId idR) + => HsBindLR idL idR -> SDoc ppr_monobind (PatBind { pat_lhs = pat, pat_rhs = grhss }) = pprPatBind pat grhss @@ -534,7 +539,7 @@ ppr_monobind (FunBind { fun_id = fun, = pprTicks empty (if null ticks then empty else text "-- ticks = " <> ppr ticks) $$ ifPprDebug (pprBndr LetBind (unLoc fun)) - $$ pprFunBind (unLoc fun) matches + $$ pprFunBind matches $$ ifPprDebug (ppr wrap) ppr_monobind (PatSynBind psb) = ppr psb ppr_monobind (AbsBinds { abs_tvs = tyvars, abs_ev_vars = dictvars @@ -574,8 +579,10 @@ instance (OutputableBndr id) => Outputable (ABExport id) where , nest 2 (pprTcSpecPrags prags) , nest 2 (text "wrap:" <+> ppr wrap)] -instance (OutputableBndr idL, OutputableBndr idR) => Outputable (PatSynBind idL idR) where - ppr (PSB{ psb_id = L _ psyn, psb_args = details, psb_def = pat, psb_dir = dir }) +instance (OutputableBndr idL, OutputableBndrId idR) + => Outputable (PatSynBind idL idR) where + ppr (PSB{ psb_id = (L _ psyn), psb_args = details, psb_def = pat, + psb_dir = dir }) = ppr_lhs <+> ppr_rhs where ppr_lhs = text "pattern" <+> ppr_details @@ -592,7 +599,7 @@ instance (OutputableBndr idL, OutputableBndr idR) => Outputable (PatSynBind idL Unidirectional -> ppr_simple (text "<-") ImplicitBidirectional -> ppr_simple equals ExplicitBidirectional mg -> ppr_simple (text "<-") <+> ptext (sLit "where") $$ - (nest 2 $ pprFunBind psyn mg) + (nest 2 $ pprFunBind mg) pprTicks :: SDoc -> SDoc -> SDoc -- Print stuff about ticks only when -dppr-debug is on, to avoid @@ -642,11 +649,11 @@ data IPBind id = IPBind (Either (Located HsIPName) id) (LHsExpr id) deriving instance (DataId name) => Data (IPBind name) -instance (OutputableBndr id) => Outputable (HsIPBinds id) where +instance (OutputableBndrId id) => Outputable (HsIPBinds id) where ppr (IPBinds bs ds) = pprDeeperList vcat (map ppr bs) $$ ifPprDebug (ppr ds) -instance (OutputableBndr id) => Outputable (IPBind id) where +instance (OutputableBndrId id) => Outputable (IPBind id) where ppr (IPBind lr rhs) = name <+> equals <+> pprExpr (unLoc rhs) where name = case lr of Left (L _ ip) -> pprBndr LetBind ip @@ -878,10 +885,10 @@ signatures. Since some of the signatures contain a list of names, testing for equality is not enough -- we have to check if they overlap. -} -instance (OutputableBndr name) => Outputable (Sig name) where +instance (OutputableBndrId name) => Outputable (Sig name) where ppr sig = ppr_sig sig -ppr_sig :: OutputableBndr name => Sig name -> SDoc +ppr_sig :: (OutputableBndrId name) => Sig name -> SDoc ppr_sig (TypeSig vars ty) = pprVarSig (map unLoc vars) (ppr ty) ppr_sig (ClassOpSig is_deflt vars ty) | is_deflt = text "default" <+> pprVarSig (map unLoc vars) (ppr ty) diff --git a/compiler/hsSyn/HsDecls.hs b/compiler/hsSyn/HsDecls.hs index c6026c484e..7bf10c9137 100644 --- a/compiler/hsSyn/HsDecls.hs +++ b/compiler/hsSyn/HsDecls.hs @@ -96,7 +96,7 @@ import Name import BasicTypes import Coercion import ForeignCall -import PlaceHolder ( PostTc,PostRn,PlaceHolder(..),DataId ) +import PlaceHolder ( PostTc,PostRn,PlaceHolder(..),DataId, OutputableBndrId ) import NameSet -- others: @@ -246,7 +246,7 @@ appendGroups hs_vects = vects1 ++ vects2, hs_docs = docs1 ++ docs2 } -instance OutputableBndr name => Outputable (HsDecl name) where +instance (OutputableBndrId name) => Outputable (HsDecl name) where ppr (TyClD dcl) = ppr dcl ppr (ValD binds) = ppr binds ppr (DefD def) = ppr def @@ -262,7 +262,7 @@ instance OutputableBndr name => Outputable (HsDecl name) where ppr (DocD doc) = ppr doc ppr (RoleAnnotD ra) = ppr ra -instance OutputableBndr name => Outputable (HsGroup name) where +instance (OutputableBndrId name) => Outputable (HsGroup name) where ppr (HsGroup { hs_valds = val_decls, hs_tyclds = tycl_decls, hs_derivds = deriv_decls, @@ -307,7 +307,7 @@ data SpliceDecl id SpliceExplicitFlag deriving instance (DataId id) => Data (SpliceDecl id) -instance OutputableBndr name => Outputable (SpliceDecl name) where +instance (OutputableBndrId name) => Outputable (SpliceDecl name) where ppr (SpliceDecl (L _ e) _) = pprSplice e {- @@ -623,8 +623,7 @@ hsDeclHasCusk (ClassDecl { tcdTyVars = tyvars }) = hsTvbAllKinded tyvars -- Pretty-printing TyClDecl -- ~~~~~~~~~~~~~~~~~~~~~~~~ -instance OutputableBndr name - => Outputable (TyClDecl name) where +instance (OutputableBndrId name) => Outputable (TyClDecl name) where ppr (FamDecl { tcdFam = decl }) = ppr decl ppr (SynDecl { tcdLName = ltycon, tcdTyVars = tyvars, tcdRhs = rhs }) @@ -652,7 +651,7 @@ instance OutputableBndr name <+> pp_vanilla_decl_head lclas tyvars (unLoc context) <+> pprFundeps (map unLoc fds) -instance OutputableBndr name => Outputable (TyClGroup name) where +instance (OutputableBndrId name) => Outputable (TyClGroup name) where ppr (TyClGroup { group_tyclds = tyclds , group_roles = roles , group_instds = instds @@ -662,7 +661,7 @@ instance OutputableBndr name => Outputable (TyClGroup name) where ppr roles $$ ppr instds -pp_vanilla_decl_head :: OutputableBndr name +pp_vanilla_decl_head :: (OutputableBndrId name) => Located name -> LHsQTyVars name -> HsContext name @@ -928,10 +927,11 @@ resultVariableName :: FamilyResultSig a -> Maybe a resultVariableName (TyVarSig sig) = Just $ hsLTyVarName sig resultVariableName _ = Nothing -instance (OutputableBndr name) => Outputable (FamilyDecl name) where +instance (OutputableBndrId name) => Outputable (FamilyDecl name) where ppr = pprFamilyDecl TopLevel -pprFamilyDecl :: OutputableBndr name => TopLevelFlag -> FamilyDecl name -> SDoc +pprFamilyDecl :: (OutputableBndrId name) + => TopLevelFlag -> FamilyDecl name -> SDoc pprFamilyDecl top_level (FamilyDecl { fdInfo = info, fdLName = ltycon , fdTyVars = tyvars , fdResultSig = L _ result @@ -1126,7 +1126,7 @@ hsConDeclArgTys (PrefixCon tys) = tys hsConDeclArgTys (InfixCon ty1 ty2) = [ty1,ty2] hsConDeclArgTys (RecCon flds) = map (cd_fld_type . unLoc) (unLoc flds) -pp_data_defn :: OutputableBndr name +pp_data_defn :: (OutputableBndrId name) => (HsContext name -> SDoc) -- Printing the header -> HsDataDefn name -> SDoc @@ -1148,23 +1148,23 @@ pp_data_defn pp_hdr (HsDataDefn { dd_ND = new_or_data, dd_ctxt = L _ context Just (L _ ds) -> hsep [ text "deriving" , parens (interpp'SP ds)] -instance OutputableBndr name => Outputable (HsDataDefn name) where +instance (OutputableBndrId name) => Outputable (HsDataDefn name) where ppr d = pp_data_defn (\_ -> text "Naked HsDataDefn") d instance Outputable NewOrData where ppr NewType = text "newtype" ppr DataType = text "data" -pp_condecls :: OutputableBndr name => [LConDecl name] -> SDoc +pp_condecls :: (OutputableBndrId name) => [LConDecl name] -> SDoc pp_condecls cs@(L _ ConDeclGADT{} : _) -- In GADT syntax = hang (text "where") 2 (vcat (map ppr cs)) pp_condecls cs -- In H98 syntax = equals <+> sep (punctuate (text " |") (map ppr cs)) -instance (OutputableBndr name) => Outputable (ConDecl name) where +instance (OutputableBndrId name) => Outputable (ConDecl name) where ppr = pprConDecl -pprConDecl :: OutputableBndr name => ConDecl name -> SDoc +pprConDecl :: (OutputableBndrId name) => ConDecl name -> SDoc pprConDecl (ConDeclH98 { con_name = L _ con , con_qvars = mtvs , con_cxt = mcxt @@ -1346,10 +1346,11 @@ data InstDecl name -- Both class and family instances { tfid_inst :: TyFamInstDecl name } deriving instance (DataId id) => Data (InstDecl id) -instance (OutputableBndr name) => Outputable (TyFamInstDecl name) where +instance (OutputableBndrId name) => Outputable (TyFamInstDecl name) where ppr = pprTyFamInstDecl TopLevel -pprTyFamInstDecl :: OutputableBndr name => TopLevelFlag -> TyFamInstDecl name -> SDoc +pprTyFamInstDecl :: (OutputableBndrId name) + => TopLevelFlag -> TyFamInstDecl name -> SDoc pprTyFamInstDecl top_lvl (TyFamInstDecl { tfid_eqn = eqn }) = text "type" <+> ppr_instance_keyword top_lvl <+> ppr_fam_inst_eqn eqn @@ -1357,22 +1358,23 @@ ppr_instance_keyword :: TopLevelFlag -> SDoc ppr_instance_keyword TopLevel = text "instance" ppr_instance_keyword NotTopLevel = empty -ppr_fam_inst_eqn :: OutputableBndr name => LTyFamInstEqn name -> SDoc +ppr_fam_inst_eqn :: (OutputableBndrId name) => LTyFamInstEqn name -> SDoc ppr_fam_inst_eqn (L _ (TyFamEqn { tfe_tycon = tycon , tfe_pats = pats , tfe_rhs = rhs })) = pp_fam_inst_lhs tycon pats [] <+> equals <+> ppr rhs -ppr_fam_deflt_eqn :: OutputableBndr name => LTyFamDefltEqn name -> SDoc +ppr_fam_deflt_eqn :: (OutputableBndrId name) => LTyFamDefltEqn name -> SDoc ppr_fam_deflt_eqn (L _ (TyFamEqn { tfe_tycon = tycon , tfe_pats = tvs , tfe_rhs = rhs })) = text "type" <+> pp_vanilla_decl_head tycon tvs [] <+> equals <+> ppr rhs -instance (OutputableBndr name) => Outputable (DataFamInstDecl name) where +instance (OutputableBndrId name) => Outputable (DataFamInstDecl name) where ppr = pprDataFamInstDecl TopLevel -pprDataFamInstDecl :: OutputableBndr name => TopLevelFlag -> DataFamInstDecl name -> SDoc +pprDataFamInstDecl :: (OutputableBndrId name) + => TopLevelFlag -> DataFamInstDecl name -> SDoc pprDataFamInstDecl top_lvl (DataFamInstDecl { dfid_tycon = tycon , dfid_pats = pats , dfid_defn = defn }) @@ -1384,7 +1386,7 @@ pprDataFamInstFlavour :: DataFamInstDecl name -> SDoc pprDataFamInstFlavour (DataFamInstDecl { dfid_defn = (HsDataDefn { dd_ND = nd }) }) = ppr nd -pp_fam_inst_lhs :: OutputableBndr name +pp_fam_inst_lhs :: (OutputableBndrId name) => Located name -> HsTyPats name -> HsContext name @@ -1393,7 +1395,7 @@ pp_fam_inst_lhs thing (HsIB { hsib_body = typats }) context -- explicit type pat = hsep [ pprHsContext context, pprPrefixOcc (unLoc thing) , hsep (map (pprParendHsType.unLoc) typats)] -instance (OutputableBndr name) => Outputable (ClsInstDecl name) where +instance (OutputableBndrId name) => Outputable (ClsInstDecl name) where ppr (ClsInstDecl { cid_poly_ty = inst_ty, cid_binds = binds , cid_sigs = sigs, cid_tyfam_insts = ats , cid_overlap_mode = mbOverlap @@ -1422,7 +1424,7 @@ ppOverlapPragma mb = Just (L _ (Incoherent _)) -> text "{-# INCOHERENT #-}" -instance (OutputableBndr name) => Outputable (InstDecl name) where +instance (OutputableBndrId name) => Outputable (InstDecl name) where ppr (ClsInstD { cid_inst = decl }) = ppr decl ppr (TyFamInstD { tfid_inst = decl }) = ppr decl ppr (DataFamInstD { dfid_inst = decl }) = ppr decl @@ -1460,7 +1462,7 @@ data DerivDecl name = DerivDecl } deriving instance (DataId name) => Data (DerivDecl name) -instance (OutputableBndr name) => Outputable (DerivDecl name) where +instance (OutputableBndrId name) => Outputable (DerivDecl name) where ppr (DerivDecl ty o) = hsep [text "deriving instance", ppOverlapPragma o, ppr ty] @@ -1486,8 +1488,7 @@ data DefaultDecl name -- For details on above see note [Api annotations] in ApiAnnotation deriving instance (DataId name) => Data (DefaultDecl name) -instance (OutputableBndr name) - => Outputable (DefaultDecl name) where +instance (OutputableBndrId name) => Outputable (DefaultDecl name) where ppr (DefaultDecl tys) = text "default" <+> parens (interpp'SP tys) @@ -1588,7 +1589,7 @@ data ForeignExport = CExport (Located CExportSpec) -- contains the calling -- pretty printing of foreign declarations -- -instance OutputableBndr name => Outputable (ForeignDecl name) where +instance (OutputableBndrId name) => Outputable (ForeignDecl name) where ppr (ForeignImport { fd_name = n, fd_sig_ty = ty, fd_fi = fimport }) = hang (text "foreign import" <+> ppr fimport <+> ppr n) 2 (dcolon <+> ppr ty) @@ -1679,10 +1680,10 @@ collectRuleBndrSigTys bndrs = [ty | RuleBndrSig _ ty <- bndrs] pprFullRuleName :: Located (SourceText, RuleName) -> SDoc pprFullRuleName (L _ (_, n)) = doubleQuotes $ ftext n -instance OutputableBndr name => Outputable (RuleDecls name) where +instance (OutputableBndrId name) => Outputable (RuleDecls name) where ppr (HsRules _ rules) = ppr rules -instance OutputableBndr name => Outputable (RuleDecl name) where +instance (OutputableBndrId name) => Outputable (RuleDecl name) where ppr (HsRule name act ns lhs _fv_lhs rhs _fv_rhs) = sep [text "{-# RULES" <+> pprFullRuleName name <+> ppr act, @@ -1692,7 +1693,7 @@ instance OutputableBndr name => Outputable (RuleDecl name) where pp_forall | null ns = empty | otherwise = forAllLit <+> fsep (map ppr ns) <> dot -instance OutputableBndr name => Outputable (RuleBndr name) where +instance (OutputableBndrId name) => Outputable (RuleBndr name) where ppr (RuleBndr name) = ppr name ppr (RuleBndrSig name ty) = ppr name <> dcolon <> ppr ty @@ -1777,7 +1778,7 @@ lvectInstDecl (L _ (HsVectInstIn _)) = True lvectInstDecl (L _ (HsVectInstOut _)) = True lvectInstDecl _ = False -instance OutputableBndr name => Outputable (VectDecl name) where +instance (OutputableBndrId name) => Outputable (VectDecl name) where ppr (HsVect _ v rhs) = sep [text "{-# VECTORISE" <+> ppr v, nest 4 $ @@ -1889,7 +1890,7 @@ data AnnDecl name = HsAnnotation -- For details on above see note [Api annotations] in ApiAnnotation deriving instance (DataId name) => Data (AnnDecl name) -instance (OutputableBndr name) => Outputable (AnnDecl name) where +instance (OutputableBndrId name) => Outputable (AnnDecl name) where ppr (HsAnnotation _ provenance expr) = hsep [text "{-#", pprAnnProvenance provenance, pprExpr (unLoc expr), text "#-}"] diff --git a/compiler/hsSyn/HsExpr.hs b/compiler/hsSyn/HsExpr.hs index 0937d29f65..79cf079882 100644 --- a/compiler/hsSyn/HsExpr.hs +++ b/compiler/hsSyn/HsExpr.hs @@ -10,6 +10,7 @@ -- in module PlaceHolder {-# LANGUAGE ConstraintKinds #-} {-# LANGUAGE ExistentialQuantification #-} +{-# LANGUAGE DeriveFunctor #-} -- | Abstract Haskell syntax for expressions. module HsExpr where @@ -20,7 +21,8 @@ module HsExpr where import HsDecls import HsPat import HsLit -import PlaceHolder ( PostTc,PostRn,DataId ) +import PlaceHolder ( PostTc,PostRn,DataId,DataIdPost, + NameOrRdrName,OutputableBndrId ) import HsTypes import HsBinds @@ -42,7 +44,7 @@ import FastString import Type -- libraries: -import Data.Data hiding (Fixity) +import Data.Data hiding (Fixity(..)) import Data.Maybe (isNothing) {- @@ -117,7 +119,7 @@ mkRnSyntaxExpr name = SyntaxExpr { syn_expr = HsVar $ noLoc name -- don't care about filling in syn_arg_wraps because we're clearly -- not past the typechecker -instance OutputableBndr id => Outputable (SyntaxExpr id) where +instance (OutputableBndrId id) => Outputable (SyntaxExpr id) where ppr (SyntaxExpr { syn_expr = expr , syn_arg_wraps = arg_wraps , syn_res_wrap = res_wrap }) @@ -741,16 +743,16 @@ RenamedSource that the API Annotations cannot be used directly with RenamedSource, so this allows a simple mapping to be used based on the location. -} -instance OutputableBndr id => Outputable (HsExpr id) where +instance (OutputableBndrId id) => Outputable (HsExpr id) where ppr expr = pprExpr expr ----------------------- -- pprExpr, pprLExpr, pprBinds call pprDeeper; -- the underscore versions do not -pprLExpr :: OutputableBndr id => LHsExpr id -> SDoc +pprLExpr :: (OutputableBndrId id) => LHsExpr id -> SDoc pprLExpr (L _ e) = pprExpr e -pprExpr :: OutputableBndr id => HsExpr id -> SDoc +pprExpr :: (OutputableBndrId id) => HsExpr id -> SDoc pprExpr e | isAtomicHsExpr e || isQuietHsExpr e = ppr_expr e | otherwise = pprDeeper (ppr_expr e) @@ -766,15 +768,15 @@ isQuietHsExpr (HsAppTypeOut _ _) = True isQuietHsExpr (OpApp _ _ _ _) = True isQuietHsExpr _ = False -pprBinds :: (OutputableBndr idL, OutputableBndr idR) +pprBinds :: (OutputableBndrId idL, OutputableBndrId idR) => HsLocalBindsLR idL idR -> SDoc pprBinds b = pprDeeper (ppr b) ----------------------- -ppr_lexpr :: OutputableBndr id => LHsExpr id -> SDoc +ppr_lexpr :: (OutputableBndrId id) => LHsExpr id -> SDoc ppr_lexpr e = ppr_expr (unLoc e) -ppr_expr :: forall id. OutputableBndr id => HsExpr id -> SDoc +ppr_expr :: forall id. (OutputableBndrId id) => HsExpr id -> SDoc ppr_expr (HsVar (L _ v)) = pprPrefixOcc v ppr_expr (HsUnboundVar uv)= pprPrefixOcc (unboundVarOcc uv) ppr_expr (HsIPVar v) = ppr v @@ -841,15 +843,15 @@ ppr_expr (ExplicitTuple exprs boxity) punc [] = empty ppr_expr (HsLam matches) - = pprMatches (LambdaExpr :: HsMatchContext id) matches + = pprMatches matches ppr_expr (HsLamCase matches) = sep [ sep [text "\\case {"], - nest 2 (pprMatches (CaseAlt :: HsMatchContext id) matches <+> char '}') ] + nest 2 (pprMatches matches <+> char '}') ] ppr_expr (HsCase expr matches) = sep [ sep [text "case", nest 4 (ppr expr), ptext (sLit "of {")], - nest 2 (pprMatches (CaseAlt :: HsMatchContext id) matches <+> char '}') ] + nest 2 (pprMatches matches <+> char '}') ] ppr_expr (HsIf _ e1 e2 e3) = sep [hsep [text "if", nest 2 (ppr e1), ptext (sLit "then")], @@ -959,9 +961,9 @@ ppr_expr (HsRecFld f) = ppr f -- We must tiresomely make the "id" parameter to the LHsWcType existential -- because it's different in the HsAppType case and the HsAppTypeOut case -data LHsWcTypeX = forall id. OutputableBndr id => LHsWcTypeX (LHsWcType id) +data LHsWcTypeX = forall id. (OutputableBndrId id) => LHsWcTypeX (LHsWcType id) -ppr_apps :: OutputableBndr id +ppr_apps :: (OutputableBndrId id) => HsExpr id -> [Either (LHsExpr id) LHsWcTypeX] -> SDoc @@ -993,16 +995,16 @@ fixities should do the job, except in debug mode (-dppr-debug) so we can see the structure of the parse tree. -} -pprDebugParendExpr :: OutputableBndr id => LHsExpr id -> SDoc +pprDebugParendExpr :: (OutputableBndrId id) => LHsExpr id -> SDoc pprDebugParendExpr expr = getPprStyle (\sty -> if debugStyle sty then pprParendLExpr expr else pprLExpr expr) -pprParendLExpr :: OutputableBndr id => LHsExpr id -> SDoc +pprParendLExpr :: (OutputableBndrId id) => LHsExpr id -> SDoc pprParendLExpr (L _ e) = pprParendExpr e -pprParendExpr :: OutputableBndr id => HsExpr id -> SDoc +pprParendExpr :: (OutputableBndrId id) => HsExpr id -> SDoc pprParendExpr expr | hsExprNeedsParens expr = parens (pprExpr expr) | otherwise = pprExpr expr @@ -1160,16 +1162,16 @@ data HsCmdTop id (CmdSyntaxTable id) -- See Note [CmdSyntaxTable] deriving instance (DataId id) => Data (HsCmdTop id) -instance OutputableBndr id => Outputable (HsCmd id) where +instance (OutputableBndrId id) => Outputable (HsCmd id) where ppr cmd = pprCmd cmd ----------------------- -- pprCmd and pprLCmd call pprDeeper; -- the underscore versions do not -pprLCmd :: OutputableBndr id => LHsCmd id -> SDoc +pprLCmd :: (OutputableBndrId id) => LHsCmd id -> SDoc pprLCmd (L _ c) = pprCmd c -pprCmd :: OutputableBndr id => HsCmd id -> SDoc +pprCmd :: (OutputableBndrId id) => HsCmd id -> SDoc pprCmd c | isQuietHsCmd c = ppr_cmd c | otherwise = pprDeeper (ppr_cmd c) @@ -1183,10 +1185,10 @@ isQuietHsCmd (HsCmdApp _ _) = True isQuietHsCmd _ = False ----------------------- -ppr_lcmd :: OutputableBndr id => LHsCmd id -> SDoc +ppr_lcmd :: (OutputableBndrId id) => LHsCmd id -> SDoc ppr_lcmd c = ppr_cmd (unLoc c) -ppr_cmd :: forall id. OutputableBndr id => HsCmd id -> SDoc +ppr_cmd :: forall id. (OutputableBndrId id) => HsCmd id -> SDoc ppr_cmd (HsCmdPar c) = parens (ppr_lcmd c) ppr_cmd (HsCmdApp c e) @@ -1197,11 +1199,11 @@ ppr_cmd (HsCmdApp c e) collect_args fun args = (fun, args) ppr_cmd (HsCmdLam matches) - = pprMatches (LambdaExpr :: HsMatchContext id) matches + = pprMatches matches ppr_cmd (HsCmdCase expr matches) = sep [ sep [text "case", nest 4 (ppr expr), ptext (sLit "of {")], - nest 2 (pprMatches (CaseAlt :: HsMatchContext id) matches <+> char '}') ] + nest 2 (pprMatches matches <+> char '}') ] ppr_cmd (HsCmdIf _ e ct ce) = sep [hsep [text "if", nest 2 (ppr e), ptext (sLit "then")], @@ -1237,13 +1239,13 @@ ppr_cmd (HsCmdArrForm op _ args) = hang (text "(|" <> ppr_lexpr op) 4 (sep (map (pprCmdArg.unLoc) args) <> text "|)") -pprCmdArg :: OutputableBndr id => HsCmdTop id -> SDoc +pprCmdArg :: (OutputableBndrId id) => HsCmdTop id -> SDoc pprCmdArg (HsCmdTop cmd@(L _ (HsCmdArrForm _ Nothing [])) _ _ _) = ppr_lcmd cmd pprCmdArg (HsCmdTop cmd _ _ _) = parens (ppr_lcmd cmd) -instance OutputableBndr id => Outputable (HsCmdTop id) where +instance (OutputableBndrId id) => Outputable (HsCmdTop id) where ppr = pprCmdArg {- @@ -1295,8 +1297,8 @@ type LMatch id body = Located (Match id body) -- For details on above see note [Api annotations] in ApiAnnotation data Match id body = Match { - m_fixity :: MatchFixity id, - -- See note [m_fixity in Match] + m_ctxt :: HsMatchContext (NameOrRdrName id), + -- See note [m_ctxt in Match] m_pats :: [LPat id], -- The patterns m_type :: (Maybe (LHsType id)), -- A type signature for the result of the match @@ -1307,9 +1309,18 @@ data Match id body deriving instance (Data body,DataId id) => Data (Match id body) {- -Note [m_fixity in Match] +Note [m_ctxt in Match] ~~~~~~~~~~~~~~~~~~~~~~ +A Match can occur in a number of contexts, such as a FunBind, HsCase, HsLam and +so on. + +In order to simplify tooling processing and pretty print output, the provenance +is captured in an HsMatchContext. + +This is particularly important for the API Annotations for a multi-equation +FunBind. + The parser initially creates a FunBind with a single Match in it for every function definition it sees. @@ -1330,20 +1341,14 @@ Example infix function definition requiring individual API Annotations ( &&& ) [] ys = ys + -} --- |When a Match is part of a FunBind, it captures one complete equation for the --- function. As such it has the function name, and its fixity. -data MatchFixity id - = NonFunBindMatch - | FunBindMatch (Located id) -- of the Id - Bool -- is infix -deriving instance (DataId id) => Data (MatchFixity id) isInfixMatch :: Match id body -> Bool -isInfixMatch match = case m_fixity match of - FunBindMatch _ True -> True - _ -> False +isInfixMatch match = case m_ctxt match of + FunRhs _ Infix -> True + _ -> False isEmptyMatchGroup :: MatchGroup id body -> Bool isEmptyMatchGroup (MG { mg_alts = ms }) = null $ unLoc ms @@ -1391,35 +1396,35 @@ deriving instance (Data body,DataId id) => Data (GRHS id body) -- We know the list must have at least one @Match@ in it. -pprMatches :: (OutputableBndr idL, OutputableBndr idR, Outputable body) - => HsMatchContext idL -> MatchGroup idR body -> SDoc -pprMatches ctxt (MG { mg_alts = matches }) - = vcat (map (pprMatch ctxt) (map unLoc (unLoc matches))) +pprMatches :: (OutputableBndrId idR, Outputable body) + => MatchGroup idR body -> SDoc +pprMatches MG { mg_alts = matches } + = vcat (map pprMatch (map unLoc (unLoc matches))) -- Don't print the type; it's only a place-holder before typechecking -- Exported to HsBinds, which can't see the defn of HsMatchContext -pprFunBind :: (OutputableBndr idL, OutputableBndr idR, Outputable body) - => idL -> MatchGroup idR body -> SDoc -pprFunBind fun matches = pprMatches (FunRhs fun) matches +pprFunBind :: (OutputableBndrId idR, Outputable body) + => MatchGroup idR body -> SDoc +pprFunBind matches = pprMatches matches -- Exported to HsBinds, which can't see the defn of HsMatchContext -pprPatBind :: forall bndr id body. (OutputableBndr bndr, OutputableBndr id, Outputable body) +pprPatBind :: forall bndr id body. (OutputableBndrId bndr, + OutputableBndrId id, Outputable body) => LPat bndr -> GRHSs id body -> SDoc pprPatBind pat (grhss) = sep [ppr pat, nest 2 (pprGRHSs (PatBindRhs :: HsMatchContext id) grhss)] -pprMatch :: (OutputableBndr idL, OutputableBndr idR, Outputable body) - => HsMatchContext idL -> Match idR body -> SDoc -pprMatch ctxt match +pprMatch :: (OutputableBndrId idR, Outputable body) => Match idR body -> SDoc +pprMatch match = sep [ sep (herald : map (nest 2 . pprParendLPat) other_pats) , nest 2 ppr_maybe_ty , nest 2 (pprGRHSs ctxt (m_grhss match)) ] where - is_infix = isInfixMatch match + ctxt = m_ctxt match (herald, other_pats) = case ctxt of - FunRhs fun - | not is_infix -> (pprPrefixOcc fun, m_pats match) + FunRhs (L _ fun) fixity + | fixity == Prefix -> (pprPrefixOcc fun, m_pats match) -- f x y z = e -- Not pprBndr; the AbsBinds will -- have printed the signature @@ -1444,14 +1449,14 @@ pprMatch ctxt match Nothing -> empty -pprGRHSs :: (OutputableBndr idR, Outputable body) +pprGRHSs :: (OutputableBndrId idR, Outputable body) => HsMatchContext idL -> GRHSs idR body -> SDoc pprGRHSs ctxt (GRHSs grhss (L _ binds)) = vcat (map (pprGRHS ctxt . unLoc) grhss) $$ ppUnless (isEmptyLocalBinds binds) (text "where" $$ nest 4 (pprBinds binds)) -pprGRHS :: (OutputableBndr idR, Outputable body) +pprGRHS :: (OutputableBndrId idR, Outputable body) => HsMatchContext idL -> GRHS idR body -> SDoc pprGRHS ctxt (GRHS [] body) = pp_rhs ctxt body @@ -1777,15 +1782,15 @@ In any other context than 'MonadComp', the fields for most of these 'SyntaxExpr's stay bottom. -} -instance (OutputableBndr idL) - => Outputable (ParStmtBlock idL idR) where +instance (OutputableBndrId idL) => Outputable (ParStmtBlock idL idR) where ppr (ParStmtBlock stmts _ _) = interpp'SP stmts -instance (OutputableBndr idL, OutputableBndr idR, Outputable body) +instance (OutputableBndrId idL, OutputableBndrId idR, Outputable body) => Outputable (StmtLR idL idR body) where ppr stmt = pprStmt stmt -pprStmt :: forall idL idR body . (OutputableBndr idL, OutputableBndr idR, Outputable body) +pprStmt :: forall idL idR body . (OutputableBndrId idL, OutputableBndrId idR, + Outputable body) => (StmtLR idL idR body) -> SDoc pprStmt (LastStmt expr ret_stripped _) = ifPprDebug (text "[last]") <+> @@ -1848,7 +1853,8 @@ pprStmt (ApplicativeStmt args mb_join _) (stmts ++ [noLoc (LastStmt (noLoc return) False noSyntaxExpr)])) (error "pprStmt")) -pprTransformStmt :: OutputableBndr id => [id] -> LHsExpr id -> Maybe (LHsExpr id) -> SDoc +pprTransformStmt :: (OutputableBndrId id) + => [id] -> LHsExpr id -> Maybe (LHsExpr id) -> SDoc pprTransformStmt bndrs using by = sep [ text "then" <+> ifPprDebug (braces (ppr bndrs)) , nest 2 (ppr using) @@ -1864,7 +1870,7 @@ pprBy :: Outputable body => Maybe body -> SDoc pprBy Nothing = empty pprBy (Just e) = text "by" <+> ppr e -pprDo :: (OutputableBndr id, Outputable body) +pprDo :: (OutputableBndrId id, Outputable body) => HsStmtContext any -> [LStmt id body] -> SDoc pprDo DoExpr stmts = text "do" <+> ppr_do_stmts stmts pprDo GhciStmtCtxt stmts = text "do" <+> ppr_do_stmts stmts @@ -1875,7 +1881,7 @@ pprDo PArrComp stmts = paBrackets $ pprComp stmts pprDo MonadComp stmts = brackets $ pprComp stmts pprDo _ _ = panic "pprDo" -- PatGuard, ParStmtCxt -ppr_do_stmts :: (OutputableBndr idL, OutputableBndr idR, Outputable body) +ppr_do_stmts :: (OutputableBndrId idL, OutputableBndrId idR, Outputable body) => [LStmtLR idL idR body] -> SDoc -- Print a bunch of do stmts, with explicit braces and semicolons, -- so that we are not vulnerable to layout bugs @@ -1883,7 +1889,7 @@ ppr_do_stmts stmts = lbrace <+> pprDeeperList vcat (punctuate semi (map ppr stmts)) <+> rbrace -pprComp :: (OutputableBndr id, Outputable body) +pprComp :: (OutputableBndrId id, Outputable body) => [LStmt id body] -> SDoc pprComp quals -- Prints: body | qual1, ..., qualn | not (null quals) @@ -1892,7 +1898,7 @@ pprComp quals -- Prints: body | qual1, ..., qualn | otherwise = pprPanic "pprComp" (pprQuals quals) -pprQuals :: (OutputableBndr id, Outputable body) +pprQuals :: (OutputableBndrId id, Outputable body) => [LStmt id body] -> SDoc -- Show list comprehension qualifiers separated by commas pprQuals quals = interpp'SP quals @@ -2009,13 +2015,14 @@ splices. In contrast, when pretty printing the output of the type checker, we sense, although I hate to add another constructor to HsExpr. -} -instance OutputableBndr id => Outputable (HsSplice id) where +instance (OutputableBndrId id) => Outputable (HsSplice id) where ppr s = pprSplice s -pprPendingSplice :: OutputableBndr id => SplicePointName -> LHsExpr id -> SDoc +pprPendingSplice :: (OutputableBndrId id) + => SplicePointName -> LHsExpr id -> SDoc pprPendingSplice n e = angleBrackets (ppr n <> comma <+> ppr e) -pprSplice :: OutputableBndr id => HsSplice id -> SDoc +pprSplice :: (OutputableBndrId id) => HsSplice id -> SDoc pprSplice (HsTypedSplice n e) = ppr_splice (text "$$") n e pprSplice (HsUntypedSplice n e) = ppr_splice (text "$") n e pprSplice (HsQuasiQuote n q _ s) = ppr_quasi n q s @@ -2025,7 +2032,7 @@ ppr_quasi n quoter quote = ifPprDebug (brackets (ppr n)) <> char '[' <> ppr quoter <> vbar <> ppr quote <> text "|]" -ppr_splice :: OutputableBndr id => SDoc -> id -> LHsExpr id -> SDoc +ppr_splice :: (OutputableBndrId id) => SDoc -> id -> LHsExpr id -> SDoc ppr_splice herald n e = herald <> ifPprDebug (brackets (ppr n)) <> eDoc where @@ -2052,11 +2059,11 @@ isTypedBracket :: HsBracket id -> Bool isTypedBracket (TExpBr {}) = True isTypedBracket _ = False -instance OutputableBndr id => Outputable (HsBracket id) where +instance (OutputableBndrId id) => Outputable (HsBracket id) where ppr = pprHsBracket -pprHsBracket :: OutputableBndr id => HsBracket id -> SDoc +pprHsBracket :: (OutputableBndrId id) => HsBracket id -> SDoc pprHsBracket (ExpBr e) = thBrackets empty (ppr e) pprHsBracket (PatBr p) = thBrackets (char 'p') (ppr p) pprHsBracket (DecBrG gp) = thBrackets (char 'd') (ppr gp) @@ -2098,7 +2105,7 @@ data ArithSeqInfo id (LHsExpr id) deriving instance (DataId id) => Data (ArithSeqInfo id) -instance OutputableBndr id => Outputable (ArithSeqInfo id) where +instance (OutputableBndrId id) => Outputable (ArithSeqInfo id) where ppr (From e1) = hcat [ppr e1, pp_dotdot] ppr (FromThen e1 e2) = hcat [ppr e1, comma, space, ppr e2, pp_dotdot] ppr (FromTo e1 e3) = hcat [ppr e1, pp_dotdot, ppr e3] @@ -2116,40 +2123,49 @@ pp_dotdot = text " .. " ************************************************************************ -} -data HsMatchContext id -- Context of a Match - = FunRhs id -- Function binding for f - | LambdaExpr -- Patterns of a lambda - | CaseAlt -- Patterns and guards on a case alternative - | IfAlt -- Guards of a multi-way if alternative - | ProcExpr -- Patterns of a proc - | PatBindRhs -- A pattern binding eg [y] <- e = e +data FunctionFixity = Prefix | Infix deriving (Typeable,Data,Eq) - | RecUpd -- Record update [used only in DsExpr to +instance Outputable FunctionFixity where + ppr Prefix = text "Prefix" + ppr Infix = text "Infix" + +-- | Context of a Match +data HsMatchContext id + = FunRhs (Located id) FunctionFixity -- ^Function binding for f, fixity + | LambdaExpr -- ^Patterns of a lambda + | CaseAlt -- ^Patterns and guards on a case alternative + | IfAlt -- ^Guards of a multi-way if alternative + | ProcExpr -- ^Patterns of a proc + | PatBindRhs -- ^A pattern binding eg [y] <- e = e + + | RecUpd -- ^Record update [used only in DsExpr to -- tell matchWrapper what sort of -- runtime error message to generate] - | StmtCtxt (HsStmtContext id) -- Pattern of a do-stmt, list comprehension, + | StmtCtxt (HsStmtContext id) -- ^Pattern of a do-stmt, list comprehension, -- pattern guard, etc - | ThPatSplice -- A Template Haskell pattern splice - | ThPatQuote -- A Template Haskell pattern quotation [p| (a,b) |] - | PatSyn -- A pattern synonym declaration - deriving Data + | ThPatSplice -- ^A Template Haskell pattern splice + | ThPatQuote -- ^A Template Haskell pattern quotation [p| (a,b) |] + | PatSyn -- ^A pattern synonym declaration + deriving Functor +deriving instance (DataIdPost id) => Data (HsMatchContext id) data HsStmtContext id = ListComp | MonadComp - | PArrComp -- Parallel array comprehension + | PArrComp -- ^Parallel array comprehension - | DoExpr -- do { ... } - | MDoExpr -- mdo { ... } ie recursive do-expression - | ArrowExpr -- do-notation in an arrow-command context + | DoExpr -- ^do { ... } + | MDoExpr -- ^mdo { ... } ie recursive do-expression + | ArrowExpr -- ^do-notation in an arrow-command context - | GhciStmtCtxt -- A command-line Stmt in GHCi pat <- rhs - | PatGuard (HsMatchContext id) -- Pattern guard for specified thing - | ParStmtCtxt (HsStmtContext id) -- A branch of a parallel stmt - | TransStmtCtxt (HsStmtContext id) -- A branch of a transform stmt - deriving Data + | GhciStmtCtxt -- ^A command-line Stmt in GHCi pat <- rhs + | PatGuard (HsMatchContext id) -- ^Pattern guard for specified thing + | ParStmtCtxt (HsStmtContext id) -- ^A branch of a parallel stmt + | TransStmtCtxt (HsStmtContext id) -- ^A branch of a transform stmt + deriving Functor +deriving instance (DataIdPost id) => Data (HsStmtContext id) isListCompExpr :: HsStmtContext id -> Bool -- Uses syntax [ e | quals ] @@ -2179,7 +2195,8 @@ matchSeparator ThPatSplice = panic "unused" matchSeparator ThPatQuote = panic "unused" matchSeparator PatSyn = panic "unused" -pprMatchContext :: Outputable id => HsMatchContext id -> SDoc +pprMatchContext :: (Outputable (NameOrRdrName id),Outputable id) + => HsMatchContext id -> SDoc pprMatchContext ctxt | want_an ctxt = text "an" <+> pprMatchContextNoun ctxt | otherwise = text "a" <+> pprMatchContextNoun ctxt @@ -2188,8 +2205,9 @@ pprMatchContext ctxt want_an ProcExpr = True want_an _ = False -pprMatchContextNoun :: Outputable id => HsMatchContext id -> SDoc -pprMatchContextNoun (FunRhs fun) = text "equation for" +pprMatchContextNoun :: (Outputable (NameOrRdrName id),Outputable id) + => HsMatchContext id -> SDoc +pprMatchContextNoun (FunRhs (L _ fun) _) = text "equation for" <+> quotes (ppr fun) pprMatchContextNoun CaseAlt = text "case alternative" pprMatchContextNoun IfAlt = text "multi-way if alternative" @@ -2204,7 +2222,9 @@ pprMatchContextNoun (StmtCtxt ctxt) = text "pattern binding in" pprMatchContextNoun PatSyn = text "pattern synonym declaration" ----------------- -pprAStmtContext, pprStmtContext :: Outputable id => HsStmtContext id -> SDoc +pprAStmtContext, pprStmtContext :: (Outputable id, + Outputable (NameOrRdrName id)) + => HsStmtContext id -> SDoc pprAStmtContext ctxt = article <+> pprStmtContext ctxt where pp_an = text "an" @@ -2240,8 +2260,9 @@ pprStmtContext (TransStmtCtxt c) -- Used to generate the string for a *runtime* error message -matchContextErrString :: Outputable id => HsMatchContext id -> SDoc -matchContextErrString (FunRhs fun) = text "function" <+> ppr fun +matchContextErrString :: Outputable id + => HsMatchContext id -> SDoc +matchContextErrString (FunRhs (L _ fun) _) = text "function" <+> ppr fun matchContextErrString CaseAlt = text "case" matchContextErrString IfAlt = text "multi-way if" matchContextErrString PatBindRhs = text "pattern binding" @@ -2262,12 +2283,15 @@ matchContextErrString (StmtCtxt ListComp) = text "list comprehension" matchContextErrString (StmtCtxt MonadComp) = text "monad comprehension" matchContextErrString (StmtCtxt PArrComp) = text "array comprehension" -pprMatchInCtxt :: (OutputableBndr idL, OutputableBndr idR, Outputable body) - => HsMatchContext idL -> Match idR body -> SDoc -pprMatchInCtxt ctxt match = hang (text "In" <+> pprMatchContext ctxt <> colon) - 4 (pprMatch ctxt match) +pprMatchInCtxt :: (OutputableBndrId idR, + Outputable (NameOrRdrName (NameOrRdrName idR)), + Outputable body) + => Match idR body -> SDoc +pprMatchInCtxt match = hang (text "In" <+> pprMatchContext (m_ctxt match) + <> colon) + 4 (pprMatch match) -pprStmtInCtxt :: (OutputableBndr idL, OutputableBndr idR, Outputable body) +pprStmtInCtxt :: (OutputableBndrId idL, OutputableBndrId idR, Outputable body) => HsStmtContext idL -> StmtLR idL idR body -> SDoc pprStmtInCtxt ctxt (LastStmt e _ _) | isListCompExpr ctxt -- For [ e | .. ], do not mutter about "stmts" diff --git a/compiler/hsSyn/HsExpr.hs-boot b/compiler/hsSyn/HsExpr.hs-boot index ff4b2bc07b..022ca6bbc4 100644 --- a/compiler/hsSyn/HsExpr.hs-boot +++ b/compiler/hsSyn/HsExpr.hs-boot @@ -8,9 +8,9 @@ module HsExpr where import SrcLoc ( Located ) -import Outputable ( SDoc, OutputableBndr, Outputable ) +import Outputable ( SDoc, Outputable ) import {-# SOURCE #-} HsPat ( LPat ) -import PlaceHolder ( DataId ) +import PlaceHolder ( DataId, OutputableBndrId ) import Data.Data hiding ( Fixity ) type role HsExpr nominal @@ -33,21 +33,20 @@ instance (Data body,DataId id) => Data (MatchGroup id body) instance (Data body,DataId id) => Data (GRHSs id body) instance (DataId id) => Data (SyntaxExpr id) -instance OutputableBndr id => Outputable (HsExpr id) -instance OutputableBndr id => Outputable (HsCmd id) +instance (OutputableBndrId id) => Outputable (HsExpr id) +instance (OutputableBndrId id) => Outputable (HsCmd id) type LHsExpr a = Located (HsExpr a) -pprLExpr :: (OutputableBndr i) => - LHsExpr i -> SDoc +pprLExpr :: (OutputableBndrId id) => LHsExpr id -> SDoc -pprExpr :: (OutputableBndr i) => - HsExpr i -> SDoc +pprExpr :: (OutputableBndrId id) => HsExpr id -> SDoc -pprSplice :: (OutputableBndr i) => HsSplice i -> SDoc +pprSplice :: (OutputableBndrId id) => HsSplice id -> SDoc -pprPatBind :: (OutputableBndr bndr, OutputableBndr id, Outputable body) +pprPatBind :: (OutputableBndrId bndr, + OutputableBndrId id, Outputable body) => LPat bndr -> GRHSs id body -> SDoc -pprFunBind :: (OutputableBndr idL, OutputableBndr idR, Outputable body) - => idL -> MatchGroup idR body -> SDoc +pprFunBind :: (OutputableBndrId idR, Outputable body) + => MatchGroup idR body -> SDoc diff --git a/compiler/hsSyn/HsLit.hs b/compiler/hsSyn/HsLit.hs index 4fa0a64afd..18746c057a 100644 --- a/compiler/hsSyn/HsLit.hs +++ b/compiler/hsSyn/HsLit.hs @@ -23,7 +23,7 @@ import BasicTypes ( FractionalLit(..),SourceText ) import Type ( Type ) import Outputable import FastString -import PlaceHolder ( PostTc,PostRn,DataId ) +import PlaceHolder ( PostTc,PostRn,DataId,OutputableBndrId ) import Data.ByteString (ByteString) import Data.Data hiding ( Fixity ) @@ -165,7 +165,7 @@ instance Outputable HsLit where ppr (HsWord64Prim _ w) = pprPrimWord64 w -- in debug mode, print the expression that it's resolved to, too -instance OutputableBndr id => Outputable (HsOverLit id) where +instance (OutputableBndrId id) => Outputable (HsOverLit id) where ppr (OverLit {ol_val=val, ol_witness=witness}) = ppr val <+> (ifPprDebug (parens (pprExpr witness))) diff --git a/compiler/hsSyn/HsPat.hs b/compiler/hsSyn/HsPat.hs index c168def337..ef667a1d71 100644 --- a/compiler/hsSyn/HsPat.hs +++ b/compiler/hsSyn/HsPat.hs @@ -43,7 +43,7 @@ import {-# SOURCE #-} HsExpr (SyntaxExpr, LHsExpr, HsSplice, pprLExpr -- friends: import HsBinds import HsLit -import PlaceHolder -- ( PostRn,PostTc,DataId ) +import PlaceHolder import HsTypes import TcEvidence import BasicTypes @@ -365,7 +365,7 @@ hsRecUpdFieldOcc = fmap unambiguousFieldOcc . hsRecFieldLbl ************************************************************************ -} -instance (OutputableBndr name) => Outputable (Pat name) where +instance (OutputableBndrId name) => Outputable (Pat name) where ppr = pprPat pprPatBndr :: OutputableBndr name => name -> SDoc @@ -377,10 +377,10 @@ pprPatBndr var -- Print with type info if -dppr-debug is on else pprPrefixOcc var -pprParendLPat :: (OutputableBndr name) => LPat name -> SDoc +pprParendLPat :: (OutputableBndrId name) => LPat name -> SDoc pprParendLPat (L _ p) = pprParendPat p -pprParendPat :: (OutputableBndr name) => Pat name -> SDoc +pprParendPat :: (OutputableBndrId name) => Pat name -> SDoc pprParendPat p = sdocWithDynFlags $ \ dflags -> if need_parens dflags p then parens (pprPat p) @@ -394,7 +394,7 @@ pprParendPat p = sdocWithDynFlags $ \ dflags -> -- But otherwise the CoPat is discarded, so it -- is the pattern inside that matters. Sigh. -pprPat :: (OutputableBndr name) => Pat name -> SDoc +pprPat :: (OutputableBndrId name) => Pat name -> SDoc pprPat (VarPat (L _ var)) = pprPatBndr var pprPat (WildPat _) = char '_' pprPat (LazyPat pat) = char '~' <> pprParendLPat pat @@ -430,11 +430,12 @@ pprPat (ConPatOut { pat_con = con, pat_tvs = tvs, pat_dicts = dicts, else pprUserCon (unLoc con) details -pprUserCon :: (OutputableBndr con, OutputableBndr id) => con -> HsConPatDetails id -> SDoc +pprUserCon :: (OutputableBndr con, OutputableBndrId id) + => con -> HsConPatDetails id -> SDoc pprUserCon c (InfixCon p1 p2) = ppr p1 <+> pprInfixOcc c <+> ppr p2 pprUserCon c details = pprPrefixOcc c <+> pprConArgs details -pprConArgs :: OutputableBndr id => HsConPatDetails id -> SDoc +pprConArgs :: (OutputableBndrId id) => HsConPatDetails id -> SDoc pprConArgs (PrefixCon pats) = sep (map pprParendLPat pats) pprConArgs (InfixCon p1 p2) = sep [pprParendLPat p1, pprParendLPat p2] pprConArgs (RecCon rpats) = ppr rpats @@ -546,7 +547,7 @@ looksLazyLPat (L _ (VarPat {})) = False looksLazyLPat (L _ (WildPat {})) = False looksLazyLPat _ = True -isIrrefutableHsPat :: OutputableBndr id => LPat id -> Bool +isIrrefutableHsPat :: (OutputableBndrId id) => LPat id -> Bool -- (isIrrefutableHsPat p) is true if matching against p cannot fail, -- in the sense of falling through to the next pattern. -- (NB: this is not quite the same as the (silly) defn diff --git a/compiler/hsSyn/HsPat.hs-boot b/compiler/hsSyn/HsPat.hs-boot index 6e000e3808..aba5686085 100644 --- a/compiler/hsSyn/HsPat.hs-boot +++ b/compiler/hsSyn/HsPat.hs-boot @@ -10,11 +10,11 @@ import SrcLoc( Located ) import Data.Data hiding (Fixity) import Outputable -import PlaceHolder ( DataId ) +import PlaceHolder ( DataId, OutputableBndrId ) type role Pat nominal data Pat (i :: *) type LPat i = Located (Pat i) instance (DataId id) => Data (Pat id) -instance (OutputableBndr name) => Outputable (Pat name) +instance (OutputableBndrId name) => Outputable (Pat name) diff --git a/compiler/hsSyn/HsSyn.hs b/compiler/hsSyn/HsSyn.hs index 76d31a4182..1cfb8b8a1d 100644 --- a/compiler/hsSyn/HsSyn.hs +++ b/compiler/hsSyn/HsSyn.hs @@ -107,7 +107,7 @@ data HsModule name -- For details on above see note [Api annotations] in ApiAnnotation deriving instance (DataId name) => Data (HsModule name) -instance (OutputableBndr name, HasOccName name) +instance (OutputableBndrId name, HasOccName name) => Outputable (HsModule name) where ppr (HsModule Nothing _ imports decls _ mbDoc) diff --git a/compiler/hsSyn/HsTypes.hs b/compiler/hsSyn/HsTypes.hs index 66145b6588..e5f0f9cde5 100644 --- a/compiler/hsSyn/HsTypes.hs +++ b/compiler/hsSyn/HsTypes.hs @@ -69,7 +69,8 @@ module HsTypes ( import {-# SOURCE #-} HsExpr ( HsSplice, pprSplice ) -import PlaceHolder ( PostTc,PostRn,DataId,PlaceHolder(..) ) +import PlaceHolder ( PostTc,PostRn,DataId,PlaceHolder(..), + OutputableBndrId ) import Id ( Id ) import Name( Name ) @@ -584,7 +585,7 @@ data HsAppType name | HsAppPrefix (LHsType name) -- anything else, including things like (+) deriving instance (DataId name) => Data (HsAppType name) -instance OutputableBndr name => Outputable (HsAppType name) where +instance (OutputableBndrId name) => Outputable (HsAppType name) where ppr = ppr_app_ty TopPrec {- @@ -715,7 +716,7 @@ data ConDeclField name -- Record fields have Haddoc docs on them -- For details on above see note [Api annotations] in ApiAnnotation deriving instance (DataId name) => Data (ConDeclField name) -instance (OutputableBndr name) => Outputable (ConDeclField name) where +instance (OutputableBndrId name) => Outputable (ConDeclField name) where ppr (ConDeclField fld_n fld_ty _) = ppr fld_n <+> dcolon <+> ppr fld_ty -- HsConDetails is used for patterns/expressions *and* for data type @@ -1104,16 +1105,16 @@ ambiguousFieldOcc (FieldOcc rdr sel) = Unambiguous rdr sel ************************************************************************ -} -instance (OutputableBndr name) => Outputable (HsType name) where +instance (OutputableBndrId name) => Outputable (HsType name) where ppr ty = pprHsType ty instance Outputable HsTyLit where ppr = ppr_tylit -instance (OutputableBndr name) => Outputable (LHsQTyVars name) where +instance (OutputableBndrId name) => Outputable (LHsQTyVars name) where ppr (HsQTvs { hsq_explicit = tvs }) = interppSP tvs -instance (OutputableBndr name) => Outputable (HsTyVarBndr name) where +instance (OutputableBndrId name) => Outputable (HsTyVarBndr name) where ppr (UserTyVar n) = ppr n ppr (KindedTyVar n k) = parens $ hsep [ppr n, dcolon, ppr k] @@ -1126,7 +1127,8 @@ instance (Outputable thing) => Outputable (HsWildCardBndrs name thing) where instance Outputable (HsWildCardInfo name) where ppr (AnonWildCard _) = char '_' -pprHsForAll :: OutputableBndr name => [LHsTyVarBndr name] -> LHsContext name -> SDoc +pprHsForAll :: (OutputableBndrId name) + => [LHsTyVarBndr name] -> LHsContext name -> SDoc pprHsForAll = pprHsForAllExtra Nothing -- | Version of 'pprHsForAll' that can also print an extra-constraints @@ -1136,32 +1138,34 @@ pprHsForAll = pprHsForAllExtra Nothing -- function for this is needed, as the extra-constraints wildcard is removed -- from the actual context and type, and stored in a separate field, thus just -- printing the type will not print the extra-constraints wildcard. -pprHsForAllExtra :: OutputableBndr name => Maybe SrcSpan -> [LHsTyVarBndr name] -> LHsContext name -> SDoc +pprHsForAllExtra :: (OutputableBndrId name) + => Maybe SrcSpan -> [LHsTyVarBndr name] -> LHsContext name + -> SDoc pprHsForAllExtra extra qtvs cxt = pprHsForAllTvs qtvs <+> pprHsContextExtra show_extra (unLoc cxt) where show_extra = isJust extra -pprHsForAllTvs :: OutputableBndr name => [LHsTyVarBndr name] -> SDoc +pprHsForAllTvs :: (OutputableBndrId name) => [LHsTyVarBndr name] -> SDoc pprHsForAllTvs qtvs | show_forall = forAllLit <+> interppSP qtvs <> dot | otherwise = empty where show_forall = opt_PprStyle_Debug || not (null qtvs) -pprHsContext :: (OutputableBndr name) => HsContext name -> SDoc +pprHsContext :: (OutputableBndrId name) => HsContext name -> SDoc pprHsContext = maybe empty (<+> darrow) . pprHsContextMaybe -pprHsContextNoArrow :: (OutputableBndr name) => HsContext name -> SDoc +pprHsContextNoArrow :: (OutputableBndrId name) => HsContext name -> SDoc pprHsContextNoArrow = fromMaybe empty . pprHsContextMaybe -pprHsContextMaybe :: (OutputableBndr name) => HsContext name -> Maybe SDoc +pprHsContextMaybe :: (OutputableBndrId name) => HsContext name -> Maybe SDoc pprHsContextMaybe [] = Nothing pprHsContextMaybe [L _ pred] = Just $ ppr_mono_ty FunPrec pred pprHsContextMaybe cxt = Just $ parens (interpp'SP cxt) -- True <=> print an extra-constraints wildcard, e.g. @(Show a, _) =>@ -pprHsContextExtra :: (OutputableBndr name) => Bool -> HsContext name -> SDoc +pprHsContextExtra :: (OutputableBndrId name) => Bool -> HsContext name -> SDoc pprHsContextExtra show_extra ctxt | not show_extra = pprHsContext ctxt @@ -1172,7 +1176,7 @@ pprHsContextExtra show_extra ctxt where ctxt' = map ppr ctxt ++ [char '_'] -pprConDeclFields :: OutputableBndr name => [LConDeclField name] -> SDoc +pprConDeclFields :: (OutputableBndrId name) => [LConDeclField name] -> SDoc pprConDeclFields fields = braces (sep (punctuate comma (map ppr_fld fields))) where ppr_fld (L _ (ConDeclField { cd_fld_names = ns, cd_fld_type = ty, @@ -1196,7 +1200,7 @@ seems like the Right Thing anyway.) -- Printing works more-or-less as for Types -pprHsType, pprParendHsType :: (OutputableBndr name) => HsType name -> SDoc +pprHsType, pprParendHsType :: (OutputableBndrId name) => HsType name -> SDoc pprHsType ty = ppr_mono_ty TopPrec (prepare ty) pprParendHsType ty = ppr_mono_ty TyConPrec ty @@ -1207,10 +1211,10 @@ prepare (HsParTy ty) = prepare (unLoc ty) prepare (HsAppsTy [L _ (HsAppPrefix (L _ ty))]) = prepare ty prepare ty = ty -ppr_mono_lty :: (OutputableBndr name) => TyPrec -> LHsType name -> SDoc +ppr_mono_lty :: (OutputableBndrId name) => TyPrec -> LHsType name -> SDoc ppr_mono_lty ctxt_prec ty = ppr_mono_ty ctxt_prec (unLoc ty) -ppr_mono_ty :: (OutputableBndr name) => TyPrec -> HsType name -> SDoc +ppr_mono_ty :: (OutputableBndrId name) => TyPrec -> HsType name -> SDoc ppr_mono_ty ctxt_prec (HsForAllTy { hst_bndrs = tvs, hst_body = ty }) = maybeParen ctxt_prec FunPrec $ sep [pprHsForAllTvs tvs, ppr_mono_lty TopPrec ty] @@ -1268,7 +1272,8 @@ ppr_mono_ty ctxt_prec (HsDocTy ty doc) -- postfix operators -------------------------- -ppr_fun_ty :: (OutputableBndr name) => TyPrec -> LHsType name -> LHsType name -> SDoc +ppr_fun_ty :: (OutputableBndrId name) + => TyPrec -> LHsType name -> LHsType name -> SDoc ppr_fun_ty ctxt_prec ty1 ty2 = let p1 = ppr_mono_lty FunPrec ty1 p2 = ppr_mono_lty TopPrec ty2 @@ -1277,7 +1282,7 @@ ppr_fun_ty ctxt_prec ty1 ty2 sep [p1, text "->" <+> p2] -------------------------- -ppr_app_ty :: OutputableBndr name => TyPrec -> HsAppType name -> SDoc +ppr_app_ty :: (OutputableBndrId name) => TyPrec -> HsAppType name -> SDoc ppr_app_ty _ (HsAppInfix (L _ n)) = pprInfixOcc n ppr_app_ty _ (HsAppPrefix (L _ (HsTyVar (L _ n)))) = pprPrefixOcc n ppr_app_ty ctxt (HsAppPrefix ty) = ppr_mono_lty ctxt ty diff --git a/compiler/hsSyn/HsUtils.hs b/compiler/hsSyn/HsUtils.hs index 6b90f001b0..43d60a3667 100644 --- a/compiler/hsSyn/HsUtils.hs +++ b/compiler/hsSyn/HsUtils.hs @@ -20,7 +20,7 @@ which deal with the instantiated versions are located elsewhere: module HsUtils( -- Terms - mkHsPar, mkHsApp, mkHsAppType, mkHsAppTypeOut, mkHsConApp, mkSimpleHsAlt, + mkHsPar, mkHsApp, mkHsAppType, mkHsAppTypeOut, mkHsConApp, mkHsCaseAlt, mkSimpleMatch, unguardedGRHSs, unguardedRHS, mkMatchGroup, mkMatchGroupName, mkMatch, mkHsLam, mkHsIf, mkHsWrap, mkLHsWrap, mkHsWrapCo, mkHsWrapCoR, mkLHsWrapCo, @@ -133,10 +133,12 @@ just attach noSrcSpan to everything. mkHsPar :: LHsExpr id -> LHsExpr id mkHsPar e = L (getLoc e) (HsPar e) -mkSimpleMatch :: [LPat id] -> Located (body id) -> LMatch id (Located (body id)) -mkSimpleMatch pats rhs +mkSimpleMatch :: HsMatchContext (NameOrRdrName id) + -> [LPat id] -> Located (body id) + -> LMatch id (Located (body id)) +mkSimpleMatch ctxt pats rhs = L loc $ - Match NonFunBindMatch pats Nothing (unguardedGRHSs rhs) + Match ctxt pats Nothing (unguardedGRHSs rhs) where loc = case pats of [] -> getLoc rhs @@ -178,8 +180,9 @@ mkHsAppTypeOut e t = addCLoc e (hswc_body t) (HsAppTypeOut e t) mkHsLam :: [LPat RdrName] -> LHsExpr RdrName -> LHsExpr RdrName mkHsLam pats body = mkHsPar (L (getLoc body) (HsLam matches)) - where - matches = mkMatchGroup Generated [mkSimpleMatch pats body] + where + matches = mkMatchGroup Generated + [mkSimpleMatch LambdaExpr pats body] mkHsLams :: [TyVar] -> [EvVar] -> LHsExpr Id -> LHsExpr Id mkHsLams tyvars dicts expr = mkLHsWrap (mkWpTyLams tyvars @@ -192,10 +195,11 @@ mkHsConApp data_con tys args where mk_app f a = noLoc (HsApp f (noLoc a)) -mkSimpleHsAlt :: LPat id -> (Located (body id)) -> LMatch id (Located (body id)) --- A simple lambda with a single pattern, no binds, no guards; pre-typechecking -mkSimpleHsAlt pat expr - = mkSimpleMatch [pat] expr +-- |A simple case alternative with a single pattern, no binds, no guards; +-- pre-typechecking +mkHsCaseAlt :: LPat id -> (Located (body id)) -> LMatch id (Located (body id)) +mkHsCaseAlt pat expr + = mkSimpleMatch CaseAlt [pat] expr nlHsTyApp :: name -> [Type] -> LHsExpr name nlHsTyApp fun_id tys = noLoc (HsWrap (mkWpTyApps tys) (HsVar (noLoc fun_id))) @@ -709,13 +713,15 @@ isInfixFunBind _ = False mk_easy_FunBind :: SrcSpan -> RdrName -> [LPat RdrName] -> LHsExpr RdrName -> LHsBind RdrName mk_easy_FunBind loc fun pats expr - = L loc $ mkFunBind (L loc fun) [mkMatch pats expr (noLoc emptyLocalBinds)] + = L loc $ mkFunBind (L loc fun) + [mkMatch (FunRhs (L loc fun) Prefix) pats expr + (noLoc emptyLocalBinds)] ------------ -mkMatch :: [LPat id] -> LHsExpr id -> Located (HsLocalBinds id) - -> LMatch id (LHsExpr id) -mkMatch pats expr lbinds - = noLoc (Match NonFunBindMatch (map paren pats) Nothing +mkMatch :: HsMatchContext (NameOrRdrName id) -> [LPat id] -> LHsExpr id + -> Located (HsLocalBinds id) -> LMatch id (LHsExpr id) +mkMatch ctxt pats expr lbinds + = noLoc (Match ctxt (map paren pats) Nothing (GRHSs (unguardedRHS noSrcSpan expr) lbinds)) where paren lp@(L l p) | hsPatNeedsParens p = L l (ParPat lp) diff --git a/compiler/hsSyn/PlaceHolder.hs b/compiler/hsSyn/PlaceHolder.hs index cacad7111c..7b3391d533 100644 --- a/compiler/hsSyn/PlaceHolder.hs +++ b/compiler/hsSyn/PlaceHolder.hs @@ -17,6 +17,7 @@ import ConLike (ConLike) import FieldLabel import SrcLoc (Located) import TcEvidence ( HsWrapper ) +import Outputable ( OutputableBndr ) import Data.Data hiding ( Fixity ) import BasicTypes (Fixity) @@ -97,9 +98,18 @@ In terms of actual usage, we have the following PostRn id NameSet TcId and Var are synonyms for Id + +Unfortunately the type checker termination checking conditions fail for the +DataId constraint type based on this, so even though it is safe the +UndecidableInstances pragma is required where this is used. -} type DataId id = + ( DataIdPost id + , DataIdPost (NameOrRdrName id) + ) + +type DataIdPost id = ( Data id , Data (PostRn id NameSet) , Data (PostRn id Fixity) @@ -107,7 +117,7 @@ type DataId id = , Data (PostRn id Name) , Data (PostRn id (Located Name)) , Data (PostRn id [Name]) --- , Data (PostRn id [id]) + , Data (PostRn id id) , Data (PostTc id Type) , Data (PostTc id Coercion) @@ -118,3 +128,18 @@ type DataId id = , Data (PostTc id HsWrapper) , Data (PostTc id [FieldLabel]) ) + + +-- |Follow the @id@, but never beyond Name. This is used in a 'HsMatchContext', +-- for printing messages related to a 'Match' +type family NameOrRdrName id where + NameOrRdrName Id = Name + NameOrRdrName Name = Name + NameOrRdrName RdrName = RdrName + +-- |Constraint type to bundle up the requirement for 'OutputableBndr' on both +-- the @id@ and the 'NameOrRdrName' type for it +type OutputableBndrId id = + ( OutputableBndr id + , OutputableBndr (NameOrRdrName id) + ) |