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.
Showing
- compiler/GHC/Core/TyCon.hs 21 additions, 5 deletionscompiler/GHC/Core/TyCon.hs
- compiler/GHC/Driver/Flags.hs 4 additions, 1 deletioncompiler/GHC/Driver/Flags.hs
- compiler/GHC/Driver/Session.hs 1 addition, 0 deletionscompiler/GHC/Driver/Session.hs
- compiler/GHC/Rename/HsType.hs 26 additions, 20 deletionscompiler/GHC/Rename/HsType.hs
- compiler/GHC/Rename/Pat.hs 17 additions, 9 deletionscompiler/GHC/Rename/Pat.hs
- compiler/GHC/Tc/Errors/Ppr.hs 26 additions, 5 deletionscompiler/GHC/Tc/Errors/Ppr.hs
- compiler/GHC/Tc/Errors/Types.hs 22 additions, 3 deletionscompiler/GHC/Tc/Errors/Types.hs
- compiler/GHC/Tc/Errors/Types/PromotionErr.hs 0 additions, 3 deletionscompiler/GHC/Tc/Errors/Types/PromotionErr.hs
- compiler/GHC/Tc/Gen/HsType.hs 1 addition, 4 deletionscompiler/GHC/Tc/Gen/HsType.hs
- compiler/GHC/Tc/Validity.hs 132 additions, 4 deletionscompiler/GHC/Tc/Validity.hs
- compiler/GHC/Types/Error/Codes.hs 0 additions, 1 deletioncompiler/GHC/Types/Error/Codes.hs
- docs/users_guide/9.10.1-notes.rst 19 additions, 0 deletionsdocs/users_guide/9.10.1-notes.rst
- docs/users_guide/exts/data_kinds.rst 85 additions, 0 deletionsdocs/users_guide/exts/data_kinds.rst
- docs/users_guide/using-warnings.rst 20 additions, 0 deletionsdocs/users_guide/using-warnings.rst
- testsuite/tests/polykinds/T7433.stderr 3 additions, 5 deletionstestsuite/tests/polykinds/T7433.stderr
- testsuite/tests/rename/should_fail/T22478e.hs 1 addition, 0 deletionstestsuite/tests/rename/should_fail/T22478e.hs
- testsuite/tests/rename/should_fail/T22478e.stderr 8 additions, 0 deletionstestsuite/tests/rename/should_fail/T22478e.stderr
- testsuite/tests/typecheck/should_compile/T22141a.hs 8 additions, 0 deletionstestsuite/tests/typecheck/should_compile/T22141a.hs
- testsuite/tests/typecheck/should_compile/T22141a.stderr 7 additions, 0 deletionstestsuite/tests/typecheck/should_compile/T22141a.stderr
- testsuite/tests/typecheck/should_compile/T22141b.hs 10 additions, 0 deletionstestsuite/tests/typecheck/should_compile/T22141b.hs
Loading
Please register or sign in to comment