diff options
author | Alan Zimmerman <alan.zimm@gmail.com> | 2015-12-07 12:40:38 +0100 |
---|---|---|
committer | Ben Gamari <ben@smart-cactus.org> | 2015-12-07 13:09:17 +0100 |
commit | 51a5e68db887adb3565ff2f077267e2b513be562 (patch) | |
tree | 62a44143c3b5ddb6f42170dc057ec8f3292fbf1e /compiler/hsSyn/HsUtils.hs | |
parent | 700c42b5e0ffd27884e6bdfa9a940e55449cff6f (diff) | |
download | haskell-51a5e68db887adb3565ff2f077267e2b513be562.tar.gz |
Refactor ConDecl
The ConDecl type in HsDecls is an uneasy compromise. For the most part,
HsSyn directly reflects the syntax written by the programmer; and that
gives just the right "pegs" on which to hang Alan's API annotations. But
ConDecl doesn't properly reflect the syntax of Haskell-98 and GADT-style
data type declarations.
To be concrete, here's a draft new data type
```lang=hs
data ConDecl name
| ConDeclGADT
{ con_names :: [Located name]
, con_type :: LHsSigType name -- The type after the ‘::’
, con_doc :: Maybe LHsDocString }
| ConDeclH98
{ con_name :: Located name
, con_qvars :: Maybe (LHsQTyVars name)
-- User-written forall (if any), and its implicit
-- kind variables
-- Non-Nothing needs -XExistentialQuantification
, con_cxt :: Maybe (LHsContext name)
-- ^ User-written context (if any)
, con_details :: HsConDeclDetails name
-- ^ Arguments
, con_doc :: Maybe LHsDocString
-- ^ A possible Haddock comment.
} deriving (Typeable)
```
Note that
For GADTs, just keep a type. That's what the user writes.
NB:HsType can represent records on the LHS of an arrow:
{ x:Int,y:Bool} -> T
con_qvars and con_cxt are both Maybe because they are both
optional (the forall and the context of an existential data type
For ConDeclGADT the type variables of the data type do not scope
over the con_type; whereas for ConDeclH98 they do scope over con_cxt
and con_details.
Updates haddock submodule.
Test Plan: ./validate
Reviewers: simonpj, erikd, hvr, goldfire, austin, bgamari
Subscribers: erikd, goldfire, thomie, mpickering
Differential Revision: https://phabricator.haskell.org/D1558
GHC Trac Issues: #11028
Diffstat (limited to 'compiler/hsSyn/HsUtils.hs')
-rw-r--r-- | compiler/hsSyn/HsUtils.hs | 26 |
1 files changed, 22 insertions, 4 deletions
diff --git a/compiler/hsSyn/HsUtils.hs b/compiler/hsSyn/HsUtils.hs index 19996fd0f1..ca3cae5260 100644 --- a/compiler/hsSyn/HsUtils.hs +++ b/compiler/hsSyn/HsUtils.hs @@ -953,14 +953,32 @@ hsConDeclsBinders cons = go id cons case r of -- remove only the first occurrence of any seen field in order to -- avoid circumventing detection of duplicate fields (#9156) - L loc (ConDecl { con_names = names, con_details = RecCon flds }) -> - (map (L loc . unLoc) names ++ ns, r' ++ fs) + L loc (ConDeclGADT { con_names = names + , con_type = HsIB { hsib_body = res_ty}}) -> + case tau of + L _ (HsFunTy (L _ (HsRecTy flds)) _res_ty) + -> (map (L loc . unLoc) names ++ ns, r' ++ fs) + where r' = remSeen (concatMap (cd_fld_names . unLoc) + flds) + remSeen' + = foldr (.) remSeen + [deleteBy ((==) `on` + rdrNameFieldOcc . unLoc) v + | v <- r'] + (ns, fs) = go remSeen' rs + _other -> (map (L loc . unLoc) names ++ ns, fs) + where (ns, fs) = go remSeen rs + where + (_tvs, _cxt, tau) = splitLHsSigmaTy res_ty + L loc (ConDeclH98 { con_name = name + , con_details = RecCon flds }) -> + ([L loc (unLoc name)] ++ ns, r' ++ fs) where r' = remSeen (concatMap (cd_fld_names . unLoc) (unLoc flds)) remSeen' = foldr (.) remSeen [deleteBy ((==) `on` rdrNameFieldOcc . unLoc) v | v <- r'] (ns, fs) = go remSeen' rs - L loc (ConDecl { con_names = names }) -> - (map (L loc . unLoc) names ++ ns, fs) + L loc (ConDeclH98 { con_name = name }) -> + ([L loc (unLoc name)] ++ ns, fs) where (ns, fs) = go remSeen rs {- |