From b6698360c4d663cd3597975a68d4790005cd17e0 Mon Sep 17 00:00:00 2001 From: Kari Pahula <kari.pahula@vincit.fi> Date: Wed, 11 Sep 2019 14:00:23 +0300 Subject: [PATCH] Add -Wderiving-defaults (#15839) Combining DeriveAnyClass and GeneralizedNewtypeDeriving can cause warnings about defaulting with a deriving strategy. This change adds an enable/suppress flag for it. --- compiler/main/DynFlags.hs | 3 +++ compiler/typecheck/TcDeriv.hs | 6 ++++-- docs/users_guide/using-warnings.rst | 15 +++++++++++++++ 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/compiler/main/DynFlags.hs b/compiler/main/DynFlags.hs index c2d0322cd9e5..24d527e56fe7 100644 --- a/compiler/main/DynFlags.hs +++ b/compiler/main/DynFlags.hs @@ -919,6 +919,7 @@ data WarningFlag = | Opt_WarnUnusedPackages -- Since 8.10 | Opt_WarnInferredSafeImports -- Since 8.10 | Opt_WarnMissingSafeHaskellMode -- Since 8.10 + | Opt_WarnDerivingDefaults deriving (Eq, Show, Enum) data Language = Haskell98 | Haskell2010 @@ -4025,6 +4026,7 @@ wWarningFlagsDeps = [ Opt_WarnDeferredOutOfScopeVariables, flagSpec "deprecations" Opt_WarnWarningsDeprecations, flagSpec "deprecated-flags" Opt_WarnDeprecatedFlags, + flagSpec "deriving-defaults" Opt_WarnDerivingDefaults, flagSpec "deriving-typeable" Opt_WarnDerivingTypeable, flagSpec "dodgy-exports" Opt_WarnDodgyExports, flagSpec "dodgy-foreign-imports" Opt_WarnDodgyForeignImports, @@ -4822,6 +4824,7 @@ standardWarnings -- see Note [Documenting warning flags] Opt_WarnPartialTypeSignatures, Opt_WarnUnrecognisedPragmas, Opt_WarnDuplicateExports, + Opt_WarnDerivingDefaults, Opt_WarnOverflowedLiterals, Opt_WarnEmptyEnumerations, Opt_WarnMissingFields, diff --git a/compiler/typecheck/TcDeriv.hs b/compiler/typecheck/TcDeriv.hs index 0863e22cb9c0..52417fbe8203 100644 --- a/compiler/typecheck/TcDeriv.hs +++ b/compiler/typecheck/TcDeriv.hs @@ -1755,8 +1755,10 @@ mkNewTypeEqn -- enabled, we take the diplomatic approach of defaulting to -- DeriveAnyClass, but emitting a warning about the choice. -- See Note [Deriving strategies] - when (newtype_deriving && deriveAnyClass) $ - lift $ addWarnTc NoReason $ sep + when (newtype_deriving && deriveAnyClass) $ do + dyn_flags <- getDynFlags + when (wopt Opt_WarnDerivingDefaults dyn_flags) $ + lift $ addWarnTc NoReason $ sep [ text "Both DeriveAnyClass and" <+> text "GeneralizedNewtypeDeriving are enabled" , text "Defaulting to the DeriveAnyClass strategy" diff --git a/docs/users_guide/using-warnings.rst b/docs/users_guide/using-warnings.rst index dda7bb656c8d..6271f5c8924a 100644 --- a/docs/users_guide/using-warnings.rst +++ b/docs/users_guide/using-warnings.rst @@ -26,6 +26,7 @@ generally likely to indicate bugs in your program. These are: * :ghc-flag:`-Wdeprecated-flags` * :ghc-flag:`-Wunrecognised-pragmas` * :ghc-flag:`-Wduplicate-exports` + * :ghc-flag:`-Wderiving-defaults` * :ghc-flag:`-Woverflowed-literals` * :ghc-flag:`-Wempty-enumerations` * :ghc-flag:`-Wmissing-fields` @@ -640,6 +641,20 @@ of ``-W(no-)*``. Causes a warning to be emitted if an enumeration is empty, e.g. ``[5 .. 3]``. +.. ghc-flag:: -Wderiving-defaults + :shortdesc: warn about default deriving when using both + DeriveAnyClass and GeneralizedNewtypeDeriving + :type: dynamic + :reverse: -Wno-deriving-defaults + :category: + + Causes a warning when using both :ref:`DeriveAnyClass` and + :ref:`GeneralizedNewtypeDeriving` and the compiler defaults to + using the first one of them. For example :: + + class C a + newtype T a = MkT a deriving C + .. ghc-flag:: -Wduplicate-constraints :shortdesc: warn when a constraint appears duplicated in a type signature :type: dynamic -- GitLab