[Git][ghc/ghc][wip/T22141] More robust checking for DataKinds

Ryan Scott (@RyanGlScott) gitlab at gitlab.haskell.org
Fri Oct 13 21:40:39 UTC 2023



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


Commits:
ed5215cd by Ryan Scott at 2023-10-13T17:39:59-04:00
More robust checking for DataKinds

As observed in #22141, GHC was not doing its due diligence in catching code
that should require `DataKinds` in order to use. Most notably, it was allowing
the use of arbitrary data types in kind contexts without `DataKinds`, e.g.,

```hs
data Vector :: Nat -> Type -> Type where
```

This patch revamps how GHC tracks `DataKinds`. The full specification is
written out in the `DataKinds` section of the GHC User's Guide, and the
implementation thereof is described in `Note [Checking for DataKinds]` in
`GHC.Tc.Validity`. In brief:

* We catch _type_-level `DataKinds` violations in the renamer. See
  `checkDataKinds` in `GHC.Rename.HsType` and `check_data_kinds` in
  `GHC.Rename.Pat`.

* We catch _kind_-level `DataKinds` violations in the typechecker, as this
  allows us to catch things that appear beneath type synonyms. (We do *not*
  want to do this in type-level contexts, as it is perfectly fine for a type
  synonym to mention something that requires DataKinds while still using the
  type synonym in a module that doesn't enable DataKinds.) See `checkValidType`
  in `GHC.Tc.Validity`.

* There is now a single `TcRnDataKindsError` that classifies all manner of
  `DataKinds` violations, both in the renamer and the typechecker. The
  `NoDataKindsDC` error has been removed, as it has been subsumed by
  `TcRnDataKindsError`.

* I have added `CONSTRAINT` is `isKindTyCon`, which is what checks for illicit
  uses of data types at the kind level without `DataKinds`. Previously,
  `isKindTyCon` checked for `Constraint` but not `CONSTRAINT`. This is
  inconsistent, given that both `Type` and `TYPE` were checked by `isKindTyCon`.
  Moreover, it thwarted the implementation of the `DataKinds` check in
  `checkValidType`, since we would expand `Constraint` (which was OK without
  `DataKinds`) to `CONSTRAINT` (which was _not_ OK without `DataKinds`) and
  reject it. Now both are allowed.

* I have added a flurry of additional test cases that test various corners of
  `DataKinds` checking.

Fixes #22141.

- - - - -


30 changed files:

- compiler/GHC/Core/TyCon.hs
- compiler/GHC/Driver/Flags.hs
- compiler/GHC/Driver/Session.hs
- compiler/GHC/Rename/HsType.hs
- compiler/GHC/Rename/Pat.hs
- compiler/GHC/Tc/Errors/Ppr.hs
- compiler/GHC/Tc/Errors/Types.hs
- compiler/GHC/Tc/Errors/Types/PromotionErr.hs
- compiler/GHC/Tc/Gen/HsType.hs
- compiler/GHC/Tc/Validity.hs
- compiler/GHC/Types/Error/Codes.hs
- docs/users_guide/9.10.1-notes.rst
- docs/users_guide/exts/data_kinds.rst
- docs/users_guide/using-warnings.rst
- testsuite/tests/polykinds/T7433.stderr
- testsuite/tests/rename/should_fail/T22478e.hs
- testsuite/tests/rename/should_fail/T22478e.stderr
- + testsuite/tests/typecheck/should_compile/T22141a.hs
- + testsuite/tests/typecheck/should_compile/T22141a.stderr
- + testsuite/tests/typecheck/should_compile/T22141b.hs
- + testsuite/tests/typecheck/should_compile/T22141b.stderr
- + testsuite/tests/typecheck/should_compile/T22141c.hs
- + testsuite/tests/typecheck/should_compile/T22141c.stderr
- + testsuite/tests/typecheck/should_compile/T22141d.hs
- + testsuite/tests/typecheck/should_compile/T22141d.stderr
- + testsuite/tests/typecheck/should_compile/T22141e.hs
- + testsuite/tests/typecheck/should_compile/T22141e.stderr
- + testsuite/tests/typecheck/should_compile/T22141e_Aux.hs
- + testsuite/tests/typecheck/should_compile/T22141f.hs
- + testsuite/tests/typecheck/should_compile/T22141g.hs


The diff was not included because it is too large.


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

-- 
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/ed5215cd4cd2d3263e6c307c23d7d7dd9c2db6dc
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/20231013/97e19a54/attachment.html>


More information about the ghc-commits mailing list