Simon Jakobi pushed to branch wip/sjakobi/T27732-type-data-field-annotations at Glasgow Haskell Compiler / GHC

Commits:

15 changed files:

Changes:

  • changelog.d/27732
    1
    +section: language
    
    2
    +synopsis: Reject `~` and UNPACK annotations on type data fields
    
    3
    +description:
    
    4
    +  ``type data`` declarations (:extension:`TypeData`) now reject any
    
    5
    +  strictness or unpackedness annotation on constructor fields.
    
    6
    +  Previously only ``!`` was rejected.
    
    7
    +mrs: !16563
    
    8
    +issues: #27732

  • compiler/GHC/Rename/Module.hs
    ... ... @@ -71,7 +71,6 @@ import GHC.Data.Graph.Directed ( SCC, flattenSCC, Node(..)
    71 71
                                    , stronglyConnCompFromEdgedVerticesUniq )
    
    72 72
     import GHC.Data.OrdList
    
    73 73
     import qualified GHC.LanguageExtensions as LangExt
    
    74
    -import GHC.Core.DataCon ( isSrcStrict )
    
    75 74
     
    
    76 75
     import Control.Monad
    
    77 76
     import Data.Bifunctor ( first )
    
    ... ... @@ -2116,8 +2115,8 @@ rnDataDefn doc (HsDataDefn { dd_cType = cType, dd_ctxt = context, dd_cons = cond
    2116 2115
           = do {
    
    2117 2116
                ; when (has_labelled_fields condecl) $
    
    2118 2117
                    failWith $ TcRnTypeDataForbids TypeDataForbidsLabelledFields
    
    2119
    -           ; when (has_strictness_flags condecl) $
    
    2120
    -               failWith $ TcRnTypeDataForbids TypeDataForbidsStrictnessAnnotations
    
    2118
    +           ; when (has_field_annotations condecl) $
    
    2119
    +               failWith $ TcRnTypeDataForbids TypeDataForbidsFieldAnnotations
    
    2121 2120
                }
    
    2122 2121
     
    
    2123 2122
         has_labelled_fields (ConDeclGADT { con_g_args = RecConGADT _ _ }) = True
    
    ... ... @@ -2125,13 +2124,16 @@ rnDataDefn doc (HsDataDefn { dd_cType = cType, dd_ctxt = context, dd_cons = cond
    2125 2124
           = not (null (unLoc flds))
    
    2126 2125
         has_labelled_fields _ = False
    
    2127 2126
     
    
    2128
    -    has_strictness_flags condecl
    
    2129
    -      = any isSrcStrict (con_arg_bangs condecl)
    
    2127
    +    has_field_annotations condecl
    
    2128
    +      = any is_annotated (con_arg_fields condecl)
    
    2129
    +      where
    
    2130
    +        is_annotated (CDF { cdf_bang = bang, cdf_unpack = unpack })
    
    2131
    +          = bang /= NoSrcStrict || unpack /= NoSrcUnpack
    
    2130 2132
     
    
    2131
    -    con_arg_bangs (ConDeclGADT { con_g_args = PrefixConGADT _ args }) = map cdf_bang args
    
    2132
    -    con_arg_bangs (ConDeclH98 { con_args = PrefixCon _ args }) = map cdf_bang args
    
    2133
    -    con_arg_bangs (ConDeclH98 { con_args = InfixCon _ arg1 arg2 }) = [cdf_bang arg1, cdf_bang arg2]
    
    2134
    -    con_arg_bangs _ = []
    
    2133
    +    con_arg_fields (ConDeclGADT { con_g_args = PrefixConGADT _ args }) = args
    
    2134
    +    con_arg_fields (ConDeclH98 { con_args = PrefixCon _ args }) = args
    
    2135
    +    con_arg_fields (ConDeclH98 { con_args = InfixCon _ arg1 arg2 }) = [arg1, arg2]
    
    2136
    +    con_arg_fields _ = []
    
    2135 2137
     
    
    2136 2138
     {-
    
    2137 2139
     Note [Type data declarations]
    
    ... ... @@ -2166,8 +2168,9 @@ preceded by `type`, with the following restrictions:
    2166 2168
     (R2) There are no labelled fields.  Perhaps these could be supported
    
    2167 2169
          using type families, but they are omitted for now.
    
    2168 2170
     
    
    2169
    -(R3) There are no strictness flags, because they don't make sense at
    
    2170
    -     the type level.
    
    2171
    +(R3) There are no strictness or unpackedness annotations (!, ~,
    
    2172
    +     {-# UNPACK #-}, {-# NOUNPACK #-}), because they don't make sense
    
    2173
    +     at the type level.
    
    2171 2174
     
    
    2172 2175
     (R4) The types of the constructors contain no constraints.
    
    2173 2176
     
    

  • compiler/GHC/Tc/Errors/Types.hs
    ... ... @@ -2865,6 +2865,10 @@ data TcRnMessage where
    2865 2865
            type-data/should_fail/TDRecordsH98
    
    2866 2866
            type-data/should_fail/TDStrictnessGADT
    
    2867 2867
            type-data/should_fail/TDStrictnessH98
    
    2868
    +       type-data/should_fail/T27732a
    
    2869
    +       type-data/should_fail/T27732b
    
    2870
    +       type-data/should_fail/T27732c
    
    2871
    +       type-data/should_fail/T27732d
    
    2868 2872
       -}
    
    2869 2873
       TcRnTypeDataForbids :: !TypeDataForbids -> TcRnMessage
    
    2870 2874
     
    
    ... ... @@ -4523,15 +4527,15 @@ data ZonkerMessage where
    4523 4527
     data TypeDataForbids
    
    4524 4528
       = TypeDataForbidsDatatypeContexts
    
    4525 4529
       | TypeDataForbidsLabelledFields
    
    4526
    -  | TypeDataForbidsStrictnessAnnotations
    
    4530
    +  | TypeDataForbidsFieldAnnotations
    
    4527 4531
       | TypeDataForbidsDerivingClauses
    
    4528 4532
       deriving Generic
    
    4529 4533
     
    
    4530 4534
     instance Outputable TypeDataForbids where
    
    4531
    -  ppr TypeDataForbidsDatatypeContexts      = text "Data type contexts"
    
    4532
    -  ppr TypeDataForbidsLabelledFields        = text "Labelled fields"
    
    4533
    -  ppr TypeDataForbidsStrictnessAnnotations = text "Strictness flags"
    
    4534
    -  ppr TypeDataForbidsDerivingClauses       = text "Deriving clauses"
    
    4535
    +  ppr TypeDataForbidsDatatypeContexts = text "Data type contexts"
    
    4536
    +  ppr TypeDataForbidsLabelledFields   = text "Labelled fields"
    
    4537
    +  ppr TypeDataForbidsFieldAnnotations = text "Strictness or unpackedness annotations"
    
    4538
    +  ppr TypeDataForbidsDerivingClauses  = text "Deriving clauses"
    
    4535 4539
     
    
    4536 4540
     -- | Specifies which back ends can handle a requested foreign import or export
    
    4537 4541
     type ExpectedBackends = [Backend]
    

  • docs/users_guide/exts/type_data.rst
    ... ... @@ -33,7 +33,7 @@ either an ordinary algebraic data type or a GADT, prefixed with the keyword
    33 33
     ``type``, except that it may not contain
    
    34 34
     a datatype context (even with :extension:`DatatypeContexts`),
    
    35 35
     labelled fields,
    
    36
    -strictness flags, or
    
    36
    +:ref:`strictness <strict-haskell>` or :ref:`unpackedness <unpack-pragma>` annotations, or
    
    37 37
     a ``deriving`` clause.
    
    38 38
     
    
    39 39
     The only constraints permitted in the types of constructors are
    

  • testsuite/tests/type-data/should_fail/T27732a.hs
    1
    +{-# LANGUAGE TypeData, LazyFieldAnnotations #-}
    
    2
    +module T27732a where
    
    3
    +
    
    4
    +type data T a = Cons ~a

  • testsuite/tests/type-data/should_fail/T27732a.stderr
    1
    +T27732a.hs:4:17: error: [GHC-67297]
    
    2
    +    Strictness or unpackedness annotations are not allowed in type data declarations.
    
    3
    +

  • testsuite/tests/type-data/should_fail/T27732b.hs
    1
    +{-# LANGUAGE TypeData, LazyFieldAnnotations #-}
    
    2
    +module T27732b where
    
    3
    +
    
    4
    +type data T a where
    
    5
    +     Cons :: ~a -> T a

  • testsuite/tests/type-data/should_fail/T27732b.stderr
    1
    +T27732b.hs:5:6: error: [GHC-67297]
    
    2
    +    Strictness or unpackedness annotations are not allowed in type data declarations.
    
    3
    +

  • testsuite/tests/type-data/should_fail/T27732c.hs
    1
    +{-# LANGUAGE TypeData #-}
    
    2
    +module T27732c where
    
    3
    +
    
    4
    +type data T a = Cons {-# UNPACK #-} a

  • testsuite/tests/type-data/should_fail/T27732c.stderr
    1
    +T27732c.hs:4:17: error: [GHC-67297]
    
    2
    +    Strictness or unpackedness annotations are not allowed in type data declarations.
    
    3
    +

  • testsuite/tests/type-data/should_fail/T27732d.hs
    1
    +{-# LANGUAGE TypeData #-}
    
    2
    +module T27732d where
    
    3
    +
    
    4
    +type data T a = Cons {-# NOUNPACK #-} a

  • testsuite/tests/type-data/should_fail/T27732d.stderr
    1
    +T27732d.hs:4:17: error: [GHC-67297]
    
    2
    +    Strictness or unpackedness annotations are not allowed in type data declarations.
    
    3
    +

  • testsuite/tests/type-data/should_fail/TDStrictnessGADT.stderr
    1
    +TDStrictnessGADT.hs:5:6: error: [GHC-67297]
    
    2
    +    Strictness or unpackedness annotations are not allowed in type data declarations.
    
    1 3
     
    2
    -TDStrictnessGADT.hs:5:6: [GHC-67297]
    
    3
    -    Strictness flags are not allowed in type data declarations.

  • testsuite/tests/type-data/should_fail/TDStrictnessH98.stderr
    1
    +TDStrictnessH98.hs:4:17: error: [GHC-67297]
    
    2
    +    Strictness or unpackedness annotations are not allowed in type data declarations.
    
    1 3
     
    2
    -TDStrictnessH98.hs:4:17: [GHC-67297]
    
    3
    -    Strictness flags are not allowed in type data declarations.

  • testsuite/tests/type-data/should_fail/all.T
    ... ... @@ -13,3 +13,7 @@ test('TDStrictnessGADT', normal, compile_fail, [''])
    13 13
     test('TDStrictnessH98', normal, compile_fail, [''])
    
    14 14
     test('TDTagToEnum', normal, compile_fail, [''])
    
    15 15
     test('T22332b', normal, compile_fail, [''])
    
    16
    +test('T27732a', normal, compile_fail, [''])
    
    17
    +test('T27732b', normal, compile_fail, [''])
    
    18
    +test('T27732c', normal, compile_fail, [''])
    
    19
    +test('T27732d', normal, compile_fail, [''])