[Git][ghc/ghc][wip/T22817] Handle `type data` properly in tyThingParent_maybe

Ryan Scott (@RyanGlScott) gitlab at gitlab.haskell.org
Wed Jan 25 12:37:18 UTC 2023



Ryan Scott pushed to branch wip/T22817 at Glasgow Haskell Compiler / GHC


Commits:
79133e93 by Ryan Scott at 2023-01-25T07:37:02-05:00
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.

- - - - -


6 changed files:

- compiler/GHC/Rename/Module.hs
- compiler/GHC/Types/TyThing.hs
- + testsuite/tests/ghci/scripts/T22817.hs
- + testsuite/tests/ghci/scripts/T22817.script
- + testsuite/tests/ghci/scripts/T22817.stdout
- testsuite/tests/ghci/scripts/all.T


Changes:

=====================================
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


=====================================
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)


=====================================
testsuite/tests/ghci/scripts/T22817.hs
=====================================
@@ -0,0 +1,4 @@
+{-# LANGUAGE TypeData #-}
+module T22817 where
+
+type data BoolKind = FalseType | TrueType


=====================================
testsuite/tests/ghci/scripts/T22817.script
=====================================
@@ -0,0 +1,4 @@
+:load T22817
+:info BoolKind
+:info FalseType
+:info TrueType


=====================================
testsuite/tests/ghci/scripts/T22817.stdout
=====================================
@@ -0,0 +1,9 @@
+type BoolKind :: *
+type data BoolKind = FalseType | TrueType
+  	-- Defined at T22817.hs:4:1
+type BoolKind :: *
+type data BoolKind = FalseType | ...
+  	-- Defined at T22817.hs:4:22
+type BoolKind :: *
+type data BoolKind = ... | TrueType
+  	-- Defined at T22817.hs:4:34


=====================================
testsuite/tests/ghci/scripts/all.T
=====================================
@@ -371,3 +371,4 @@ test('T17830', [filter_stdout_lines(r'======.*')], ghci_script, ['T17830.script'
 test('T21294a', normal, ghci_script, ['T21294a.script'])
 test('T21507', normal, ghci_script, ['T21507.script'])
 test('T22695', normal, ghci_script, ['T22695.script'])
+test('T22817', normal, ghci_script, ['T22817.script'])



View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/79133e932514141fdc1ace72da91272ab5aa8af3

-- 
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/79133e932514141fdc1ace72da91272ab5aa8af3
You're receiving this email because of your account on gitlab.haskell.org.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/ghc-commits/attachments/20230125/42adfb49/attachment-0001.html>


More information about the ghc-commits mailing list