diff options
author | Ryan Scott <ryan.gl.scott@gmail.com> | 2023-01-24 19:58:10 -0500 |
---|---|---|
committer | Marge Bot <ben+marge-bot@smart-cactus.org> | 2023-01-30 21:19:20 -0500 |
commit | 20598ef6d9e26e2e0af9ac42a42e7be00d7cc4f3 (patch) | |
tree | 62cc1c25f630a3caf923166eb5212672520ea7fb /compiler/GHC | |
parent | 08ba87200ff068aa37cac082e61ee7e2d534daf5 (diff) | |
download | haskell-20598ef6d9e26e2e0af9ac42a42e7be00d7cc4f3.tar.gz |
Handle `type data` properly in tyThingParent_maybe
Unlike most other data constructors, data constructors declared with `type data`
are represented in `TyThing`s as `ATyCon` rather than `ADataCon`. The `ATyCon`
case in `tyThingParent_maybe` previously did not consider the possibility of
the underlying `TyCon` being a promoted data constructor, which led to the
oddities observed in #22817. This patch adds a dedicated special case in
`tyThingParent_maybe`'s `ATyCon` case for `type data` data constructors to fix
these oddities.
Fixes #22817.
Diffstat (limited to 'compiler/GHC')
-rw-r--r-- | compiler/GHC/Rename/Module.hs | 16 | ||||
-rw-r--r-- | compiler/GHC/Types/TyThing.hs | 16 |
2 files changed, 24 insertions, 8 deletions
diff --git a/compiler/GHC/Rename/Module.hs b/compiler/GHC/Rename/Module.hs index fc6846e566..597d6acc27 100644 --- a/compiler/GHC/Rename/Module.hs +++ b/compiler/GHC/Rename/Module.hs @@ -2055,6 +2055,9 @@ Type data declarations have the syntax of `data` declarations (but not `newtype` declarations), either ordinary algebraic data types or GADTs, preceded by `type`, with the following restrictions: +(R0) 'data' decls only, not 'newtype' decls. This is checked by + the parser. + (R1) There are no data type contexts (even with the DatatypeContexts extension). @@ -2070,7 +2073,7 @@ preceded by `type`, with the following restrictions: The main parts of the implementation are: -* The parser recognizes `type data` (but not `type newtype`). +* (R0): The parser recognizes `type data` (but not `type newtype`). * During the initial construction of the AST, GHC.Parser.PostProcess.checkNewOrData sets the `Bool` argument of the @@ -2105,10 +2108,13 @@ The main parts of the implementation are: `dcPromotedField` is a `TyCon` (for `Zero`, say) that you can use in a type. -* After a `type data` declaration has been type-checked, the type-checker - environment entry for each constructor (`Zero` and `Succ` in our - example) is just the promoted type constructor, not the bundle required - for a data constructor. (GHC.Types.TyThing.implicitTyConThings) +* After a `type data` declaration has been type-checked, the + type-checker environment entry (a `TyThing`) for each constructor + (`Zero` and `Succ` in our example) is + - just an `ATyCon` for the promoted type constructor, + - not the bundle (`ADataCon` for the data con, `AnId` for the work id, + wrap id) required for a normal data constructor + See GHC.Types.TyThing.implicitTyConThings. * GHC.Core.TyCon.isDataKindsPromotedDataCon ignores promoted constructors from `type data`, which do not use the distinguishing quote mark added diff --git a/compiler/GHC/Types/TyThing.hs b/compiler/GHC/Types/TyThing.hs index 15fff48509..08d13b1257 100644 --- a/compiler/GHC/Types/TyThing.hs +++ b/compiler/GHC/Types/TyThing.hs @@ -239,9 +239,19 @@ tyThingParent_maybe :: TyThing -> Maybe TyThing tyThingParent_maybe (AConLike cl) = case cl of RealDataCon dc -> Just (ATyCon (dataConTyCon dc)) PatSynCon{} -> Nothing -tyThingParent_maybe (ATyCon tc) = case tyConAssoc_maybe tc of - Just tc -> Just (ATyCon tc) - Nothing -> Nothing +tyThingParent_maybe (ATyCon tc) + | -- Special case for `type data` data constructors. They appear as an + -- ATyCon (not ADataCon) but we want to display them here as if they were + -- a DataCon (i.e. with the parent declaration) (#22817). + -- See Note [Type data declarations] in GHC.Rename.Module. + Just dc <- isPromotedDataCon_maybe tc + , let parent_tc = dataConTyCon dc + , isTypeDataTyCon parent_tc + = Just (ATyCon parent_tc) + | Just tc <- tyConAssoc_maybe tc + = Just (ATyCon tc) + | otherwise + = Nothing tyThingParent_maybe (AnId id) = case idDetails id of RecSelId { sel_tycon = RecSelData tc } -> Just (ATyCon tc) |