[Git][ghc/ghc][wip/T22141] Downgrade typechecker-related DataKinds errors to warnings

Ryan Scott (@RyanGlScott) gitlab at gitlab.haskell.org
Fri Sep 29 12:00:27 UTC 2023



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


Commits:
56c9d346 by Ryan Scott at 2023-09-29T07:59:55-04:00
Downgrade typechecker-related DataKinds errors to warnings

- - - - -


16 changed files:

- compiler/GHC/Tc/Errors/Ppr.hs
- compiler/GHC/Tc/Errors/Types.hs
- compiler/GHC/Tc/Validity.hs
- testsuite/tests/typecheck/should_fail/T22141a.hs → testsuite/tests/typecheck/should_compile/T22141a.hs
- + testsuite/tests/typecheck/should_compile/T22141a.stderr
- testsuite/tests/typecheck/should_fail/T22141b.hs → testsuite/tests/typecheck/should_compile/T22141b.hs
- + testsuite/tests/typecheck/should_compile/T22141b.stderr
- testsuite/tests/typecheck/should_fail/T22141c.hs → testsuite/tests/typecheck/should_compile/T22141c.hs
- + testsuite/tests/typecheck/should_compile/T22141c.stderr
- testsuite/tests/typecheck/should_fail/T22141d.hs → testsuite/tests/typecheck/should_compile/T22141d.hs
- + testsuite/tests/typecheck/should_compile/T22141d.stderr
- testsuite/tests/typecheck/should_fail/T22141e.hs → 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/all.T
- testsuite/tests/typecheck/should_fail/all.T


Changes:

=====================================
compiler/GHC/Tc/Errors/Ppr.hs
=====================================
@@ -1654,13 +1654,21 @@ instance Diagnostic TcRnMessage where
                 , inHsDocContext doc ]
 
     TcRnDataKindsError typeOrKind thing
-      -> mkSimpleDecorated $
-           text "Illegal" <+> (text $ levelString typeOrKind) <> colon <+> quotes ppr_thing
+      -- See Note [Checking for DataKinds] (Wrinkle: Migration story for
+      -- DataKinds typechecker errors) in GHC.Tc.Validity for why we give
+      -- different diagnostic messages below.
+      -> case thing of
+           Left renamer_thing ->
+             mkSimpleDecorated $
+               text "Illegal" <+> ppr_level <> colon <+> quotes (ppr renamer_thing)
+           Right typechecker_thing ->
+             mkSimpleDecorated $ vcat
+               [ text "An occurrence of" <+> quotes (ppr typechecker_thing) <+>
+                 text "in a" <+> ppr_level <+> text "requires DataKinds."
+               , text "Future versions of GHC will turn this warning into an error."
+               ]
       where
-        ppr_thing =
-          case thing of
-            Left renamer_ast     -> ppr renamer_ast
-            Right typechecker_ty -> ppr typechecker_ty
+        ppr_level = text $ levelString typeOrKind
 
     TcRnTypeSynonymCycle decl_or_tcs
       -> mkSimpleDecorated $
@@ -2405,8 +2413,17 @@ instance Diagnostic TcRnMessage where
       -> ErrorWithoutFlag
     TcRnUnusedQuantifiedTypeVar{}
       -> WarningWithFlag Opt_WarnUnusedForalls
-    TcRnDataKindsError{}
-      -> ErrorWithoutFlag
+    TcRnDataKindsError _ thing
+      -- DataKinds errors can arise from either the renamer (Left) or the
+      -- typechecker (Right). The latter category of DataKinds errors are a
+      -- fairly recent addition to GHC (introduced in GHC 9.10), and in order
+      -- to prevent these new errors from breaking users' code, we temporarily
+      -- downgrade these errors to warnings. See Note [Checking for DataKinds]
+      -- (Wrinkle: Migration story for DataKinds typechecker errors)
+      -- in GHC.Tc.Validity.
+      -> case thing of
+           Left  _ -> ErrorWithoutFlag
+           Right _ -> WarningWithoutFlag
     TcRnTypeSynonymCycle{}
       -> ErrorWithoutFlag
     TcRnZonkerMessage msg


=====================================
compiler/GHC/Tc/Errors/Types.hs
=====================================
@@ -2398,14 +2398,16 @@ data TcRnMessage where
                   rename/should_fail/T13568
                   rename/should_fail/T22478e
                   th/TH_Promoted1Tuple
-                  typecheck/should_fail/tcfail094
-                  typecheck/should_fail/T22141fa.hs
-                  typecheck/should_fail/T22141fb.hs
-                  typecheck/should_fail/T22141fc.hs
-                  typecheck/should_fail/T22141fd.hs
-                  typecheck/should_fail/T22141fe.hs
-                  typecheck/should_compile/T22141f.hs
-                  typecheck/should_compile/T22141g.hs
+                  typecheck/should_compile/tcfail094
+                  typecheck/should_compile/T22141a
+                  typecheck/should_compile/T22141b
+                  typecheck/should_compile/T22141c
+                  typecheck/should_compile/T22141d
+                  typecheck/should_compile/T22141e
+                  typecheck/should_compile/T22141f
+                  typecheck/should_compile/T22141g
+                  typecheck/should_fail/T20873c
+                  typecheck/should_fail/T20873d
   -}
   TcRnDataKindsError :: TypeOrKind -> Either (HsType GhcPs) Type -> TcRnMessage
 


=====================================
compiler/GHC/Tc/Validity.hs
=====================================
@@ -1015,11 +1015,18 @@ checkVdqOK ve tvbs ty = do
 
 -- | Check for a DataKinds violation in a kind context.
 -- See @Note [Checking for DataKinds]@.
+--
+-- Note that emitting DataKinds errors from the typechecker is a fairly recent
+-- addition to GHC (introduced in GHC 9.10), and in order to prevent these new
+-- errors from breaking users' code, we temporarily downgrade these errors to
+-- warnings. (This is why we use 'diagnosticTcM' below.) See
+-- @Note [Checking for DataKinds] (Wrinkle: Migration story for DataKinds
+-- typechecker errors)@.
 checkDataKinds :: ValidityEnv -> Type -> TcM ()
 checkDataKinds (ValidityEnv{ ve_ctxt = ctxt, ve_tidy_env = env }) ty = do
   data_kinds <- xoptM LangExt.DataKinds
-  checkTcM
-    (data_kinds || typeLevelUserTypeCtxt ctxt) $
+  diagnosticTcM
+    (not (data_kinds || typeLevelUserTypeCtxt ctxt)) $
     (env, TcRnDataKindsError KindLevel (Right (tidyType env ty)))
 
 {- Note [No constraints in kinds]
@@ -1173,6 +1180,28 @@ different places in the code:
   synonym), so we also catch a subset of kind-level violations in the renamer
   to allow for earlier reporting of these errors.
 
+-----
+-- Wrinkle: Migration story for DataKinds typechecker errors
+-----
+
+As mentioned above, DataKinds is checked in two different places: the renamer
+and the typechecker. The checks in the renamer have been around since DataKinds
+was introduced. The checks in the typechecker, on the other hand, are a fairly
+recent addition, having been introduced in GHC 9.10. As such, it is possible
+that there are some programs in the wild that (1) do not enable DataKinds, and
+(2) were accepted by a previous GHC version, but would now be rejected by the
+new DataKinds checks in the typechecker.
+
+To prevent the new DataKinds checks in the typechecker from breaking users'
+code, we temporarily allow programs to compile if they violate a DataKinds
+check in the typechecker, but GHC will emit a warning if such a violation
+occurs. Users can then silence the warning by enabling DataKinds in the module
+where the affected code lives. It is fairly straightforward to distinguish
+between DataKinds violations arising from the renamer versus the typechecker,
+as TcRnDataKindsError (the error message type classifying all DataKinds errors)
+stores an Either field that is Left when the error comes from the renamer and
+Right when the error comes from the typechecker.
+
 ************************************************************************
 *                                                                      *
 \subsection{Checking a theta or source type}


=====================================
testsuite/tests/typecheck/should_fail/T22141a.hs → testsuite/tests/typecheck/should_compile/T22141a.hs
=====================================


=====================================
testsuite/tests/typecheck/should_compile/T22141a.stderr
=====================================
@@ -0,0 +1,7 @@
+
+T22141a.hs:8:1: warning: [GHC-68567]
+    • An occurrence of ‘GHC.Num.Natural.Natural’ in a kind requires DataKinds.
+      Future versions of GHC will turn this warning into an error.
+    • In the expansion of type synonym ‘Nat’
+      In the data type declaration for ‘Vector’
+    Suggested fix: Perhaps you intended to use DataKinds


=====================================
testsuite/tests/typecheck/should_fail/T22141b.hs → testsuite/tests/typecheck/should_compile/T22141b.hs
=====================================


=====================================
testsuite/tests/typecheck/should_compile/T22141b.stderr
=====================================
@@ -0,0 +1,8 @@
+
+T22141b.hs:10:1: warning: [GHC-68567]
+    • An occurrence of ‘GHC.Num.Natural.Natural’ in a kind requires DataKinds.
+      Future versions of GHC will turn this warning into an error.
+    • In the expansion of type synonym ‘Nat’
+      In the expansion of type synonym ‘MyNat’
+      In the data type declaration for ‘Vector’
+    Suggested fix: Perhaps you intended to use DataKinds


=====================================
testsuite/tests/typecheck/should_fail/T22141c.hs → testsuite/tests/typecheck/should_compile/T22141c.hs
=====================================
@@ -5,5 +5,7 @@ module T22141c where
 import Data.Kind (Type)
 import Data.Proxy (Proxy)
 
-type D :: Proxy (# Type, Type #) -> Type
+type T = (# Type, Type #)
+
+type D :: Proxy T -> Type
 data D a


=====================================
testsuite/tests/typecheck/should_compile/T22141c.stderr
=====================================
@@ -0,0 +1,32 @@
+
+T22141c.hs:10:11: warning: [GHC-68567]
+    • An occurrence of ‘(# *, * #)’ in a kind requires DataKinds.
+      Future versions of GHC will turn this warning into an error.
+    • In the expansion of type synonym ‘T’
+      In a standalone kind signature for ‘D’: Proxy T -> Type
+    Suggested fix: Perhaps you intended to use DataKinds
+
+T22141c.hs:10:11: warning: [GHC-68567]
+    • An occurrence of ‘'[]’ in a kind requires DataKinds.
+      Future versions of GHC will turn this warning into an error.
+    • In a standalone kind signature for ‘D’: Proxy T -> Type
+    Suggested fix: Perhaps you intended to use DataKinds
+
+T22141c.hs:10:11: warning: [GHC-68567]
+    • An occurrence of ‘'[GHC.Types.LiftedRep]’ in a kind requires DataKinds.
+      Future versions of GHC will turn this warning into an error.
+    • In a standalone kind signature for ‘D’: Proxy T -> Type
+    Suggested fix: Perhaps you intended to use DataKinds
+
+T22141c.hs:10:11: warning: [GHC-68567]
+    • An occurrence of ‘[GHC.Types.LiftedRep,
+                         GHC.Types.LiftedRep]’ in a kind requires DataKinds.
+      Future versions of GHC will turn this warning into an error.
+    • In a standalone kind signature for ‘D’: Proxy T -> Type
+    Suggested fix: Perhaps you intended to use DataKinds
+
+T22141c.hs:10:11: warning: [GHC-68567]
+    • An occurrence of ‘Proxy T’ in a kind requires DataKinds.
+      Future versions of GHC will turn this warning into an error.
+    • In a standalone kind signature for ‘D’: Proxy T -> Type
+    Suggested fix: Perhaps you intended to use DataKinds


=====================================
testsuite/tests/typecheck/should_fail/T22141d.hs → testsuite/tests/typecheck/should_compile/T22141d.hs
=====================================
@@ -5,5 +5,7 @@ module T22141d where
 import Data.Kind (Type)
 import Data.Proxy (Proxy)
 
-type D :: Proxy (# Type | Type #) -> Type
+type T = (# Type | Type #)
+
+type D :: Proxy T -> Type
 data D a


=====================================
testsuite/tests/typecheck/should_compile/T22141d.stderr
=====================================
@@ -0,0 +1,32 @@
+
+T22141d.hs:10:11: warning: [GHC-68567]
+    • An occurrence of ‘(# * | * #)’ in a kind requires DataKinds.
+      Future versions of GHC will turn this warning into an error.
+    • In the expansion of type synonym ‘T’
+      In a standalone kind signature for ‘D’: Proxy T -> Type
+    Suggested fix: Perhaps you intended to use DataKinds
+
+T22141d.hs:10:11: warning: [GHC-68567]
+    • An occurrence of ‘'[]’ in a kind requires DataKinds.
+      Future versions of GHC will turn this warning into an error.
+    • In a standalone kind signature for ‘D’: Proxy T -> Type
+    Suggested fix: Perhaps you intended to use DataKinds
+
+T22141d.hs:10:11: warning: [GHC-68567]
+    • An occurrence of ‘'[GHC.Types.LiftedRep]’ in a kind requires DataKinds.
+      Future versions of GHC will turn this warning into an error.
+    • In a standalone kind signature for ‘D’: Proxy T -> Type
+    Suggested fix: Perhaps you intended to use DataKinds
+
+T22141d.hs:10:11: warning: [GHC-68567]
+    • An occurrence of ‘[GHC.Types.LiftedRep,
+                         GHC.Types.LiftedRep]’ in a kind requires DataKinds.
+      Future versions of GHC will turn this warning into an error.
+    • In a standalone kind signature for ‘D’: Proxy T -> Type
+    Suggested fix: Perhaps you intended to use DataKinds
+
+T22141d.hs:10:11: warning: [GHC-68567]
+    • An occurrence of ‘Proxy T’ in a kind requires DataKinds.
+      Future versions of GHC will turn this warning into an error.
+    • In a standalone kind signature for ‘D’: Proxy T -> Type
+    Suggested fix: Perhaps you intended to use DataKinds


=====================================
testsuite/tests/typecheck/should_fail/T22141e.hs → testsuite/tests/typecheck/should_compile/T22141e.hs
=====================================
@@ -3,6 +3,7 @@ module T22141e where
 
 import Data.Kind (Type)
 import Data.Proxy (Proxy)
+import T22141e_Aux
 
-type D :: Proxy 42 -> Type
+type D :: Proxy T -> Type
 data D a


=====================================
testsuite/tests/typecheck/should_compile/T22141e.stderr
=====================================
@@ -0,0 +1,19 @@
+
+T22141e.hs:8:11: warning: [GHC-68567]
+    • An occurrence of ‘42’ in a kind requires DataKinds.
+      Future versions of GHC will turn this warning into an error.
+    • In the expansion of type synonym ‘T’
+      In a standalone kind signature for ‘D’: Proxy T -> Type
+    Suggested fix: Perhaps you intended to use DataKinds
+
+T22141e.hs:8:11: warning: [GHC-68567]
+    • An occurrence of ‘GHC.Num.Natural.Natural’ in a kind requires DataKinds.
+      Future versions of GHC will turn this warning into an error.
+    • In a standalone kind signature for ‘D’: Proxy T -> Type
+    Suggested fix: Perhaps you intended to use DataKinds
+
+T22141e.hs:8:11: warning: [GHC-68567]
+    • An occurrence of ‘Proxy T’ in a kind requires DataKinds.
+      Future versions of GHC will turn this warning into an error.
+    • In a standalone kind signature for ‘D’: Proxy T -> Type
+    Suggested fix: Perhaps you intended to use DataKinds


=====================================
testsuite/tests/typecheck/should_compile/T22141e_Aux.hs
=====================================
@@ -0,0 +1,4 @@
+{-# LANGUAGE DataKinds #-}
+module T22141e_Aux where
+
+type T = 42


=====================================
testsuite/tests/typecheck/should_compile/all.T
=====================================
@@ -851,6 +851,11 @@ test('T21765', normal, compile, [''])
 test('T21951a', normal, compile, ['-Wredundant-strictness-flags'])
 test('T21951b', normal, compile, ['-Wredundant-strictness-flags'])
 test('T21550', normal, compile, [''])
+test('T22141a', normal, compile, [''])
+test('T22141b', normal, compile, [''])
+test('T22141c', normal, compile, [''])
+test('T22141d', normal, compile, [''])
+test('T22141e', [extra_files(['T22141e_Aux.hs'])], multimod_compile, ['T22141e.hs', '-v0'])
 test('T22141f', normal, compile, [''])
 test('T22141g', normal, compile, [''])
 test('T22310', normal, compile, [''])


=====================================
testsuite/tests/typecheck/should_fail/all.T
=====================================
@@ -665,11 +665,6 @@ test('MissingDefaultMethodBinding', normal, compile_fail, [''])
 test('T21447', normal, compile_fail, [''])
 test('T21530a', normal, compile_fail, [''])
 test('T21530b', normal, compile_fail, [''])
-test('T22141a', normal, compile_fail, [''])
-test('T22141b', normal, compile_fail, [''])
-test('T22141c', normal, compile_fail, [''])
-test('T22141d', normal, compile_fail, [''])
-test('T22141e', normal, compile_fail, [''])
 test('T22570', normal, compile_fail, [''])
 test('T22645', normal, compile_fail, [''])
 test('T20666', normal, compile, [''])   # To become compile_fail after migration period (see #22912)



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

-- 
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/56c9d3461d369b37ca7f5523e5a08eb6a375212d
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/20230929/8e74decd/attachment-0001.html>


More information about the ghc-commits mailing list