Skip to content
This repository was archived by the owner on Aug 3, 2024. It is now read-only.

Commit 5628084

Browse files
Simon Peyton Jonesmpickering
authored andcommitted
Follow changes to HsTYpe
Not yet complete (but on a wip/ branch)
1 parent 4e1eef5 commit 5628084

File tree

5 files changed

+105
-104
lines changed

5 files changed

+105
-104
lines changed

haddock-api/src/Haddock/Backends/Hoogle.hs

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,8 @@ ppExport dflags ExportDecl { expItemDecl = L _ decl
122122
f (TyClD d@DataDecl{}) = ppData dflags d subdocs
123123
f (TyClD d@SynDecl{}) = ppSynonym dflags d
124124
f (TyClD d@ClassDecl{}) = ppClass dflags d subdocs
125-
f (ForD (ForeignImport name typ _ _)) = ppSig dflags $ TypeSig [name] typ
126-
f (ForD (ForeignExport name typ _ _)) = ppSig dflags $ TypeSig [name] typ
125+
f (ForD (ForeignImport name typ _ _)) = pp_sig dflags [name] (hsSigType typ)
126+
f (ForD (ForeignExport name typ _ _)) = pp_sig dflags [name] (hsSigType typ)
127127
f (SigD sig) = ppSig dflags sig ++ ppFixities
128128
f _ = []
129129

@@ -135,31 +135,33 @@ ppSigWithDoc dflags (TypeSig names sig) subdocs
135135
= concatMap mkDocSig names
136136
where
137137
mkDocSig n = concatMap (ppDocumentation dflags) (getDoc n)
138-
++ [mkSig n]
139-
mkSig n = operator (out dflags n) ++ " :: " ++ outHsType dflags typ
138+
++ [pp_sig dflags names (hsSigWcType sig)]
140139

141140
getDoc :: Located Name -> [Documentation Name]
142141
getDoc n = maybe [] (return . fst) (lookup (unL n) subdocs)
143142

144-
typ = unL (hsSigType sig)
143+
typ = unL (hsSigWcType sig)
145144
ppSigWithDoc _ _ _ = []
146145

147146
ppSig :: DynFlags -> Sig Name -> [String]
148147
ppSig dflags x = ppSigWithDoc dflags x []
149148

149+
pp_sig :: DynFlags -> [Located Name] -> LHsType Name -> String
150+
pp_sig dflags names (L _ typ) =
151+
operator prettyNames ++ " :: " ++ outHsType dflags typ
152+
where
153+
prettyNames = intercalate ", " $ map (out dflags) names
150154

151155
-- note: does not yet output documentation for class methods
152156
ppClass :: DynFlags -> TyClDecl Name -> [(Name, DocForDecl Name)] -> [String]
153-
ppClass dflags decl subdocs = (out dflags decl' ++ ppTyFams) : ppMethods
157+
ppClass dflags decl subdocs = (out dflags decl{tcdSigs=[]} ++ ppTyFams) : ppMethods
154158
where
155-
decl' = decl
156-
{ tcdSigs = [], tcdMeths = emptyBag
157-
, tcdATs = [], tcdATDefs = []
158-
}
159159

160-
ppMethods = concat . map (ppSig' . unLoc) $ tcdSigs decl
160+
ppMethods = concat . map (ppSig' . unL . add_ctxt) $ tcdSigs decl
161161
ppSig' = flip (ppSigWithDoc dflags) subdocs . addContext
162162

163+
add_ctxt = addClassContext (tcdName x) (tyClDeclTyVars x)
164+
163165
ppTyFams
164166
| null $ tcdATs decl = ""
165167
| otherwise = (" " ++) . showSDocUnqual dflags . whereWrapper $ concat
@@ -173,17 +175,6 @@ ppClass dflags decl subdocs = (out dflags decl' ++ ppTyFams) : ppMethods
173175
, rbrace
174176
]
175177

176-
addContext (TypeSig name sig) = TypeSig name (mkHsSigType (f (hsSigType sig)))
177-
addContext (MinimalSig src sig) = MinimalSig src sig
178-
addContext _ = error "expected TypeSig"
179-
180-
f (L _ (HsForAllTy a ty)) = reL (HsForallTy a (f ty))
181-
f (L _ (HsQualTy cxt ty)) = HsQualTy (reL (context : unLoc cxt)) ty
182-
f ty = HsQualTy (reL [context]) ty
183-
184-
context = nlHsTyConApp (tcdName decl)
185-
(map (reL . HsTyVar . hsTyVarName . unL) (hsQTvBndrs (tyClDeclTyVars decl)))
186-
187178
tyFamEqnToSyn :: TyFamDefltEqn Name -> TyClDecl Name
188179
tyFamEqnToSyn tfe = SynDecl
189180
{ tcdLName = tfe_tycon tfe

haddock-api/src/Haddock/Convert.hs

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -97,17 +97,10 @@ tyThingToLHsDecl t = case t of
9797

9898
-- a data-constructor alone just gets rendered as a function:
9999
AConLike (RealDataCon dc) -> allOK $ SigD (TypeSig [synifyName dc]
100-
(synifyType ImplicitizeForAll (dataConUserType dc)))
100+
(synifySigWcType ImplicitizeForAll (dataConUserType dc)))
101101

102102
AConLike (PatSynCon ps) ->
103-
let (univ_tvs, ex_tvs, req_theta, prov_theta, arg_tys, res_ty) = patSynSig ps
104-
qtvs = univ_tvs ++ ex_tvs
105-
ty = mkFunTys arg_tys res_ty
106-
in allOK . SigD $ PatSynSig (synifyName ps)
107-
(Implicit, synifyTyVars qtvs)
108-
(synifyCtx req_theta)
109-
(synifyCtx prov_theta)
110-
(synifyType WithinType ty)
103+
allOK . SigD $ PatSynSig (synifyName ps) (synifySigType WithinType (patSynType ps))
111104
where
112105
withErrs e x = return (e, x)
113106
allOK x = return (mempty, x)
@@ -317,16 +310,16 @@ synifyDataCon use_gadt_syntax dc =
317310
, con_cxt = ctx
318311
, con_details = hat
319312
, con_res = hs_res_ty
320-
, con_doc = Nothing }
313+
, con_doc = Nothing
321314
-- we don't want any "deprecated GADT syntax" warnings!
322-
False
315+
, con_old_rec = False }
323316

324317
synifyName :: NamedThing n => n -> Located Name
325318
synifyName = noLoc . getName
326319

327320

328321
synifyIdSig :: SynifyTypeState -> Id -> Sig Name
329-
synifyIdSig s i = TypeSig [synifyName i] (synifyType s (varType i)) []
322+
synifyIdSig s i = TypeSig [synifyName i] (synifySigWcType s (varType i))
330323

331324

332325
synifyCtx :: [PredType] -> LHsContext Name
@@ -338,12 +331,14 @@ synifyTyVars ktvs = HsQTvs { hsq_kvs = map tyVarName kvs
338331
, hsq_tvs = map synifyTyVar tvs }
339332
where
340333
(kvs, tvs) = partition isKindVar ktvs
341-
synifyTyVar tv
342-
| isLiftedTypeKind kind = noLoc (UserTyVar name)
343-
| otherwise = noLoc (KindedTyVar (noLoc name) (synifyKindSig kind))
344-
where
345-
kind = tyVarKind tv
346-
name = getName tv
334+
335+
synifyTyVar :: TyVar -> LHsTyVarBndr Name
336+
synifyTyVar tv
337+
| isLiftedTypeKind kind = noLoc (UserTyVar name)
338+
| otherwise = noLoc (KindedTyVar (noLoc name) (synifyKindSig kind))
339+
where
340+
kind = tyVarKind tv
341+
name = getName tv
347342

348343
--states of what to do with foralls:
349344
data SynifyTypeState
@@ -361,6 +356,15 @@ data SynifyTypeState
361356
-- the defining class gets to quantify all its functions for free!
362357

363358

359+
synifySigType :: SynifyTypeState -> Type -> LHsSigType Name
360+
-- The empty binders is a bit suspicious;
361+
-- what if the type has free variables?
362+
synifySigType s ty = mkEmptyImplicitBndrs (synifyType s ty)
363+
364+
synifySigWcType :: SynifyTypeState -> Type -> LHsSigWcType Name
365+
-- Ditto (see synifySigType)
366+
synifySigWcType s ty = mkEmptyImplicitBndrs (mkEmptyWildCardBndrs (synifyType s ty))
367+
364368
synifyType :: SynifyTypeState -> Type -> LHsType Name
365369
synifyType _ (TyVarTy tv) = noLoc $ HsTyVar (getName tv)
366370
synifyType _ (TyConApp tc tys)
@@ -399,11 +403,11 @@ synifyType _ (FunTy t1 t2) = let
399403
in noLoc $ HsFunTy s1 s2
400404
synifyType s forallty@(ForAllTy _tv _ty) =
401405
let (tvs, ctx, tau) = tcSplitSigmaTy forallty
402-
sPhi = HsQualTy { hst_ctxt = noLoc (synifyCtx ctx)
403-
, hst_body = noLoc (synify WithinType tau) }
406+
sPhi = HsQualTy { hst_ctxt = synifyCtx ctx
407+
, hst_body = synifyType WithinType tau }
404408
in case s of
405409
DeleteTopLevelQuantification -> synifyType ImplicitizeForAll tau
406-
WithinType -> noLoc $ HsForAllTy { hst_bndrs = synifyTyVars tvs
410+
WithinType -> noLoc $ HsForAllTy { hst_bndrs = map synifyTyVar tvs
407411
, hst_body = noLoc sPhi }
408412
ImplicitizeForAll -> noLoc sPhi
409413

haddock-api/src/Haddock/Interface/Create.hs

Lines changed: 14 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -353,15 +353,14 @@ typeDocs :: HsDecl Name -> Map Int HsDocString
353353
typeDocs d =
354354
let docs = go 0 in
355355
case d of
356-
SigD (TypeSig _ ty _) -> docs (unLoc ty)
357-
SigD (PatSynSig _ _ req prov ty) ->
358-
let allTys = ty : concat [ unLoc req, unLoc prov ]
359-
in F.foldMap (docs . unLoc) allTys
360-
ForD (ForeignImport _ ty _ _) -> docs (unLoc ty)
356+
SigD (TypeSig _ ty) -> docs (unLoc (hsSigWcType ty))
357+
SigD (PatSynSig _ ty) -> docs (unLoc (hsSigType ty))
358+
ForD (ForeignImport _ ty _ _) -> docs (unLoc (hsSigType ty))
361359
TyClD (SynDecl { tcdRhs = ty }) -> docs (unLoc ty)
362360
_ -> M.empty
363361
where
364-
go n (HsForAllTy _ _ _ _ ty) = go n (unLoc ty)
362+
go n (HsForAllTy { hst_body = ty }) = go n (unLoc ty)
363+
go n (HsQualTy { hst_body = ty }) = go n (unLoc ty)
365364
go n (HsFunTy (L _ (HsDocTy _ (L _ x))) (L _ ty)) = M.insert n x $ go (n+1) ty
366365
go n (HsFunTy _ ty) = go (n+1) (unLoc ty)
367366
go n (HsDocTy _ (L _ doc)) = M.singleton n doc
@@ -740,8 +739,8 @@ fullModuleContents dflags warnings gre (docMap, argMap, subMap, declMap, instMap
740739
expandSig = foldr f []
741740
where
742741
f :: LHsDecl name -> [LHsDecl name] -> [LHsDecl name]
743-
f (L l (SigD (TypeSig names t nwcs))) xs = foldr (\n acc -> L l (SigD (TypeSig [n] t nwcs)) : acc) xs names
744-
f (L l (SigD (GenericSig names t))) xs = foldr (\n acc -> L l (SigD (GenericSig [n] t)) : acc) xs names
742+
f (L l (SigD (TypeSig names t))) xs = foldr (\n acc -> L l (SigD (TypeSig [n] t)) : acc) xs names
743+
f (L l (SigD (ClassOpSig b names t))) xs = foldr (\n acc -> L l (SigD (ClassOpSig b [n] t)) : acc) xs names
745744
f x xs = x : xs
746745

747746
mkExportItem :: LHsDecl Name -> ErrMsgGhc (Maybe (ExportItem Name))
@@ -785,17 +784,17 @@ extractDecl name mdl decl
785784
case unLoc decl of
786785
TyClD d@ClassDecl {} ->
787786
let matches = [ sig | sig <- tcdSigs d, name `elem` sigName sig,
788-
isVanillaLSig sig ] -- TODO: document fixity
787+
isTypeLSig sig ] -- TODO: document fixity
789788
in case matches of
790-
[s0] -> let (n, tyvar_names) = (tcdName d, getTyVars d)
791-
L pos sig = extractClassDecl n tyvar_names s0
789+
[s0] -> let (n, tyvar_names) = (tcdName d, tyClDeclTyVars d)
790+
L pos sig = addClassContext n tyvar_names s0
792791
in L pos (SigD sig)
793792
_ -> error "internal: extractDecl (ClassDecl)"
794793
TyClD d@DataDecl {} ->
795-
let (n, tyvar_names) = (tcdName d, map toTypeNoLoc $ getTyVars d)
796-
in SigD <$> extractRecSel name mdl n tyvar_names (dd_cons (tcdDataDefn d))
794+
let (n, tyvar_tys) = (tcdName d, lHsQTyVarsToTypes (tyClDeclTyVars d))
795+
in SigD <$> extractRecSel name mdl n tyvar_tys (dd_cons (tcdDataDefn d))
797796
InstD (DataFamInstD DataFamInstDecl { dfid_tycon = L _ n
798-
, dfid_pats = HsWB { hswb_cts = tys }
797+
, dfid_pats = HsIB { hsib_body = tys }
799798
, dfid_defn = defn }) ->
800799
SigD <$> extractRecSel name mdl n tys (dd_cons defn)
801800
InstD (ClsInstD ClsInstDecl { cid_datafam_insts = insts }) ->
@@ -809,24 +808,6 @@ extractDecl name mdl decl
809808
[d0] -> extractDecl name mdl (noLoc . InstD $ DataFamInstD d0)
810809
_ -> error "internal: extractDecl (ClsInstD)"
811810
_ -> error "internal: extractDecl"
812-
where
813-
getTyVars = hsLTyVarLocNames . tyClDeclTyVars
814-
815-
816-
toTypeNoLoc :: Located Name -> LHsType Name
817-
toTypeNoLoc = noLoc . HsTyVar . unLoc
818-
819-
820-
extractClassDecl :: Name -> [Located Name] -> LSig Name -> LSig Name
821-
extractClassDecl c tvs0 (L pos (TypeSig lname ltype _)) = case ltype of
822-
L _ (HsForAllTy expl _ tvs (L _ preds) ty) ->
823-
L pos (TypeSig lname (noLoc (HsForAllTy expl Nothing tvs (lctxt preds) ty)) [])
824-
_ -> L pos (TypeSig lname (noLoc (HsForAllTy Implicit Nothing emptyHsQTvs (lctxt []) ltype)) [])
825-
where
826-
lctxt = noLoc . ctxt
827-
ctxt preds = nlHsTyConApp c (map toTypeNoLoc tvs0) : preds
828-
extractClassDecl _ _ _ = error "extractClassDecl: unexpected decl"
829-
830811

831812
extractRecSel :: Name -> Module -> Name -> [LHsType Name] -> [LConDecl Name]
832813
-> LSig Name
@@ -835,7 +816,7 @@ extractRecSel _ _ _ _ [] = error "extractRecSel: selector not found"
835816
extractRecSel nm mdl t tvs (L _ con : rest) =
836817
case con_details con of
837818
RecCon (L _ fields) | ((l,L _ (ConDeclField _nn ty _)) : _) <- matching_fields fields ->
838-
L l (TypeSig [noLoc nm] (noLoc (HsFunTy data_ty (getBangType ty))) [])
819+
L l (TypeSig [noLoc nm] (mkEmptySigWcType (noLoc (HsFunTy data_ty (getBangType ty)))))
839820
_ -> extractRecSel nm mdl t tvs rest
840821
where
841822
matching_fields :: [LConDeclField Name] -> [(SrcSpan, LConDeclField Name)]
@@ -845,7 +826,6 @@ extractRecSel nm mdl t tvs (L _ con : rest) =
845826
| ResTyGADT _ ty <- con_res con = ty
846827
| otherwise = foldl' (\x y -> noLoc (HsAppTy x y)) (noLoc (HsTyVar t)) tvs
847828

848-
849829
-- | Keep export items with docs.
850830
pruneExportItems :: [ExportItem Name] -> [ExportItem Name]
851831
pruneExportItems = filter hasDoc

0 commit comments

Comments
 (0)