diff --git a/compiler/GHC/Driver/Flags.hs b/compiler/GHC/Driver/Flags.hs index bf4ac2bc788cf241c6dfe4eb745b52536497fdf8..17be75a7cc3a07eab6fc400c02730a9c9c7143fe 100644 --- a/compiler/GHC/Driver/Flags.hs +++ b/compiler/GHC/Driver/Flags.hs @@ -697,6 +697,7 @@ data WarningFlag = | Opt_WarnBadlyStagedTypes -- Since 9.10 | Opt_WarnInconsistentFlags -- Since 9.8 | Opt_WarnDataKindsTC -- Since 9.10 + | Opt_WarnDeprecatedTypeAbstractions -- Since 9.10 deriving (Eq, Ord, Show, Enum, Bounded) -- | Return the names of a WarningFlag @@ -811,6 +812,7 @@ warnFlagNames wflag = case wflag of Opt_WarnBadlyStagedTypes -> "badly-staged-types" :| [] Opt_WarnInconsistentFlags -> "inconsistent-flags" :| [] Opt_WarnDataKindsTC -> "data-kinds-tc" :| [] + Opt_WarnDeprecatedTypeAbstractions -> "deprecated-type-abstractions" :| [] -- ----------------------------------------------------------------------------- -- Standard sets of warning options @@ -1006,6 +1008,7 @@ minusWcompatOpts , Opt_WarnCompatUnqualifiedImports , Opt_WarnTypeEqualityOutOfScope , Opt_WarnImplicitRhsQuantification + , Opt_WarnDeprecatedTypeAbstractions ] -- | Things you get with -Wunused-binds diff --git a/compiler/GHC/Driver/Session.hs b/compiler/GHC/Driver/Session.hs index 4b5ec2216cdc4c31399401006f98227de9ab632e..161eadb9cae0c015afc8f5e374675e40c5178411 100644 --- a/compiler/GHC/Driver/Session.hs +++ b/compiler/GHC/Driver/Session.hs @@ -2285,6 +2285,7 @@ wWarningFlagsDeps = [minBound..maxBound] >>= \x -> case x of Opt_WarnIncompleteExportWarnings -> warnSpec x Opt_WarnIncompleteRecordSelectors -> warnSpec x Opt_WarnDataKindsTC -> warnSpec x + Opt_WarnDeprecatedTypeAbstractions -> warnSpec x warningGroupsDeps :: [(Deprecation, FlagSpec WarningGroup)] warningGroupsDeps = map mk warningGroups diff --git a/compiler/GHC/Rename/Pat.hs b/compiler/GHC/Rename/Pat.hs index b563a3ebf4fd0fbd39e8c32ddc64582c4dda778b..5951da22b74a01306d5e4ef8aaa4b4d4b1198c63 100644 --- a/compiler/GHC/Rename/Pat.hs +++ b/compiler/GHC/Rename/Pat.hs @@ -663,21 +663,10 @@ rnConPatAndThen mk con (PrefixCon tyargs pats) do { type_abs <- xoptM LangExt.TypeAbstractions ; type_app <- xoptM LangExt.TypeApplications ; scoped_tvs <- xoptM LangExt.ScopedTypeVariables - ; if | type_abs - -> return () - - -- As per [GHC Proposal 604](https://github.com/ghc-proposals/ghc-proposals/pull/604/), - -- we allow type applications in constructor patterns when -XTypeApplications and - -- -XScopedTypeVariables are both enabled, but we emit a warning when doing so. - -- - -- This warning is scheduled to become an error in GHC 9.12, in - -- which case we will get the usual error (below), - -- which suggests enabling -XTypeAbstractions. - | type_app && scoped_tvs - -> addDiagnostic TcRnDeprecatedInvisTyArgInConPat - - | otherwise - -> addErrTc $ TcRnTypeApplicationsDisabled (TypeApplicationInPattern arg) + -- See Note [Deprecated type abstractions in constructor patterns] + ; if | type_abs -> return () + | type_app && scoped_tvs -> addDiagnostic TcRnDeprecatedInvisTyArgInConPat + | otherwise -> addErrTc $ TcRnTypeApplicationsDisabled (TypeApplicationInPattern arg) } rnConPatTyArg (HsConPatTyArg at t) = do @@ -701,6 +690,29 @@ rnConPatAndThen mk con (RecCon rpats) } } +{- Note [Deprecated type abstractions in constructor patterns] +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +Type abstractions in constructor patterns allow the user to bind +existential type variables: + + import Type.Reflection (Typeable, typeRep) + data Ex = forall e. (Typeable e, Show e) => MkEx e + showEx (MkEx @e a) = show a ++ " :: " ++ show (typeRep @e) + +Note the pattern `MkEx @e a`, and specifically the `@e` binder. + +For historical reasons, using this feature only required TypeApplications +and ScopedTypeVariables to be enabled. As per GHC Proposal #448 (and especially +its amendment #604) we are now transitioning towards guarding this feature +behind TypeAbstractions instead. + +As a compatibility measure, we continue to support old programs that use +TypeApplications with ScopedTypeVariables instead of TypeAbstractions, +but emit the appropriate compatibility warning, -Wdeprecated-type-abstractions. +This warning is scheduled to become an error in GHC 9.14, at which point +we can simply require TypeAbstractions. +-} + checkUnusedRecordWildcardCps :: SrcSpan -> Maybe [ImplicitFieldBinders] -> CpsRn () diff --git a/compiler/GHC/Tc/Errors/Ppr.hs b/compiler/GHC/Tc/Errors/Ppr.hs index 87bed258dfe2c0568efec84dfc8bfae1de020e71..196a1909a422eefa4d2486dbe0ea049830076ce3 100644 --- a/compiler/GHC/Tc/Errors/Ppr.hs +++ b/compiler/GHC/Tc/Errors/Ppr.hs @@ -1865,7 +1865,7 @@ instance Diagnostic TcRnMessage where TcRnDeprecatedInvisTyArgInConPat -> mkSimpleDecorated $ cat [ text "Type applications in constructor patterns will require" - , text "the TypeAbstractions extension starting from GHC 9.12." ] + , text "the TypeAbstractions extension starting from GHC 9.14." ] TcRnInvisBndrWithoutSig _ hs_bndr -> mkSimpleDecorated $ @@ -2502,7 +2502,7 @@ instance Diagnostic TcRnMessage where TcRnIllegalInvisTyVarBndr{} -> ErrorWithoutFlag TcRnDeprecatedInvisTyArgInConPat {} - -> WarningWithoutFlag + -> WarningWithFlag Opt_WarnDeprecatedTypeAbstractions TcRnInvalidInvisTyVarBndr{} -> ErrorWithoutFlag TcRnInvisBndrWithoutSig{} diff --git a/docs/users_guide/9.10.1-notes.rst b/docs/users_guide/9.10.1-notes.rst index 08b9d740b1b2165a9c677e49cd64c4563455bec8..8fd0c11c2227e272d651a3508139cd4abbcc2353 100644 --- a/docs/users_guide/9.10.1-notes.rst +++ b/docs/users_guide/9.10.1-notes.rst @@ -83,6 +83,10 @@ Compiler This makes core optimizations more consistent between 32bit and 64bit platforms at the cost of slightly worse 32bit performance in edge cases. +- Type abstractions in constructor patterns that were previously admitted without enabling the :extension:`TypeAbstractions` + extension now trigger a warning, :ghc-flag:`-Wdeprecated-type-abstractions`. + This new warning is part of the :ghc-flag:`-Wcompat` warning group and will become an error in a future GHC release. + GHCi ~~~~ diff --git a/docs/users_guide/using-warnings.rst b/docs/users_guide/using-warnings.rst index 1cd7db47018088e3465ec8d72c740964290068fa..cb2fbc8812fdb26ed69d2cfc528a0643eef0ca6d 100644 --- a/docs/users_guide/using-warnings.rst +++ b/docs/users_guide/using-warnings.rst @@ -171,6 +171,7 @@ as ``-Wno-...`` for every individual warning in the group. * :ghc-flag:`-Wcompat-unqualified-imports` * :ghc-flag:`-Wtype-equality-out-of-scope` * :ghc-flag:`-Wimplicit-rhs-quantification` + * :ghc-flag:`-Wdeprecated-type-abstractions` .. ghc-flag:: -w :shortdesc: disable all warnings @@ -2501,6 +2502,36 @@ of ``-W(no-)*``. This warning detects code that will be affected by this breaking change. +.. ghc-flag:: -Wdeprecated-type-abstractions + :shortdesc: warn when type abstractions in constructor patterns are used without enabling :extension:`TypeApplications` + :type: dynamic + :reverse: -Wno-deprecated-type-abstractions + :category: + + :since: 9.10.1 + :default: off + + Type abstractions in constructor patterns allow binding existential type variables: :: + + import Type.Reflection (Typeable, typeRep) + data Ex = forall e. (Typeable e, Show e) => MkEx e + showEx (MkEx @e a) = show a ++ " :: " ++ show (typeRep @e) + + Note the pattern ``MkEx @e a``, and specifically the ``@e`` binder. + + Support for this feature was added to GHC in version 9.2, but instead of getting + its own language extension the feature was enabled by a combination of + :extension:`TypeApplications` and :extension:`ScopedTypeVariables`. + As per `GHC Proposal #448 + <https://github.com/ghc-proposals/ghc-proposals/blob/master/proposals/0448-type-variable-scoping.rst>`__ + and its amendment `#604 <https://github.com/ghc-proposals/ghc-proposals/pull/604>`__ + we are now transitioning towards guarding this feature behind :extension:`TypeAbstractions` instead. + + As a compatibility measure, GHC continues to support old programs that use type abstractions + in constructor patterns without enabling the appropriate extension :extension:`TypeAbstractions`, + but it will stop doing so in a future release. + + This warning detects code that will be affected by this breaking change. .. ghc-flag:: -Wincomplete-export-warnings :shortdesc: warn when some but not all of exports for a name are warned about diff --git a/testsuite/tests/ghci/scripts/ghci024.stdout b/testsuite/tests/ghci/scripts/ghci024.stdout index a3ceebd9d85e089ffdd7c7bc88f3b394bb5a2e89..e82226aff141c55eb9bd76bd635db0d62b72d7e4 100644 --- a/testsuite/tests/ghci/scripts/ghci024.stdout +++ b/testsuite/tests/ghci/scripts/ghci024.stdout @@ -20,6 +20,7 @@ warning settings: -Wcompat-unqualified-imports -Wtype-equality-out-of-scope -Wimplicit-rhs-quantification + -Wdeprecated-type-abstractions ~~~~~~~~~~ Testing :set -a options currently set: none. base language is: GHC2021 diff --git a/testsuite/tests/ghci/scripts/ghci024.stdout-mingw32 b/testsuite/tests/ghci/scripts/ghci024.stdout-mingw32 index 2563472740ff6df9f8b2377b52fefdb3ecffa724..c7658f986c2f80fbeb3831f63e99838512b7bcc3 100644 --- a/testsuite/tests/ghci/scripts/ghci024.stdout-mingw32 +++ b/testsuite/tests/ghci/scripts/ghci024.stdout-mingw32 @@ -19,6 +19,7 @@ warning settings: -Wcompat-unqualified-imports -Wtype-equality-out-of-scope -Wimplicit-rhs-quantification + -Wdeprecated-type-abstractions ~~~~~~~~~~ Testing :set -a options currently set: none. base language is: GHC2021 diff --git a/testsuite/tests/typecheck/should_fail/T23776.stderr b/testsuite/tests/typecheck/should_fail/T23776.stderr index 1379bfaff8495c91d653e9ce970a750ade081bd8..cd734bc880a05b8989befb0ac9bf7b58dde31e5f 100644 --- a/testsuite/tests/typecheck/should_fail/T23776.stderr +++ b/testsuite/tests/typecheck/should_fail/T23776.stderr @@ -1,5 +1,5 @@ -T23776.hs:8:6: warning: [GHC-69797] +T23776.hs:8:6: error: [GHC-69797] [-Wdeprecated-type-abstractions (in -Wcompat), Werror=deprecated-type-abstractions] Type applications in constructor patterns will require - the TypeAbstractions extension starting from GHC 9.12. + the TypeAbstractions extension starting from GHC 9.14. Suggested fix: Perhaps you intended to use TypeAbstractions diff --git a/testsuite/tests/typecheck/should_fail/all.T b/testsuite/tests/typecheck/should_fail/all.T index 19f331f357738b3734c80f2d9e15fb8dc203d54f..82661689a1d4f3f7e31b4974e6ac89ca2a3fa0ae 100644 --- a/testsuite/tests/typecheck/should_fail/all.T +++ b/testsuite/tests/typecheck/should_fail/all.T @@ -706,7 +706,7 @@ test('VisFlag5', normal, compile_fail, ['']) test('T22684', normal, compile_fail, ['']) test('T23514a', normal, compile_fail, ['']) test('T22478c', normal, compile_fail, ['']) -test('T23776', normal, compile, ['']) # to become an error in GHC 9.12 +test('T23776', normal, compile_fail, ['']) # error due to -Werror=compat, scheduled to become an actual error in GHC 9.14 test('T17940', normal, compile_fail, ['']) test('ErrorIndexLinks', normal, compile_fail, ['-fprint-error-index-links=always']) test('T24064', normal, compile_fail, [''])