diff options
author | Ben Gamari <ben@smart-cactus.org> | 2019-12-29 15:16:24 -0500 |
---|---|---|
committer | Marge Bot <ben+marge-bot@smart-cactus.org> | 2020-02-08 10:17:55 -0500 |
commit | 4435a8e0b74337fe5faddb9c46691f0f5bf9e653 (patch) | |
tree | 1d8ac7f540770c017f5e26f51d2788666203fbed | |
parent | aede171a59f9b7b8022548c385a1cb8c4589f905 (diff) | |
download | haskell-4435a8e0b74337fe5faddb9c46691f0f5bf9e653.tar.gz |
Introduce -Wcompat-unqualified-imports
This implements the warning proposed in option (B) of the
Data.List.singleton CLC [discussion][].
This warning, which is included in `-Wcompat` is intended to help users
identify imports of modules that will change incompatibly in future GHC
releases. This currently only includes `Data.List` due to the expected
specialisation and addition of `Data.List.singleton`.
Fixes #17244.
[discussion]: https://groups.google.com/d/msg/haskell-core-libraries/q3zHLmzBa5E/PmlAs_kYAQAJ
-rw-r--r-- | compiler/GHC/Rename/Names.hs | 37 | ||||
-rw-r--r-- | compiler/main/DynFlags.hs | 5 | ||||
-rw-r--r-- | compiler/prelude/PrelNames.hs | 3 | ||||
-rw-r--r-- | docs/users_guide/using-warnings.rst | 25 | ||||
-rw-r--r-- | testsuite/tests/rename/should_compile/T17244A.hs | 9 | ||||
-rw-r--r-- | testsuite/tests/rename/should_compile/T17244A.stderr | 5 | ||||
-rw-r--r-- | testsuite/tests/rename/should_compile/T17244B.hs | 10 | ||||
-rw-r--r-- | testsuite/tests/rename/should_compile/T17244B.stderr | 0 | ||||
-rw-r--r-- | testsuite/tests/rename/should_compile/T17244C.hs | 10 | ||||
-rw-r--r-- | testsuite/tests/rename/should_compile/T17244C.stderr | 0 | ||||
-rw-r--r-- | testsuite/tests/rename/should_compile/all.T | 3 |
11 files changed, 102 insertions, 5 deletions
diff --git a/compiler/GHC/Rename/Names.hs b/compiler/GHC/Rename/Names.hs index ecf82fffa0..b666b89875 100644 --- a/compiler/GHC/Rename/Names.hs +++ b/compiler/GHC/Rename/Names.hs @@ -380,6 +380,9 @@ rnImportDecl this_mod _ -> return () ) + -- Complain about -Wcompat-unqualified-imports violations. + warnUnqualifiedImport decl iface + let new_imp_decl = L loc (decl { ideclExt = noExtField, ideclSafe = mod_safe' , ideclHiding = new_imp_details }) @@ -487,6 +490,40 @@ calculateAvails dflags iface mod_safe' want_boot imported_by = } +-- | Issue a warning if the user imports Data.List without either an import +-- list or `qualified`. This is part of the migration plan for the +-- `Data.List.singleton` proposal. See #17244. +warnUnqualifiedImport :: ImportDecl GhcPs -> ModIface -> RnM () +warnUnqualifiedImport decl iface = + whenWOptM Opt_WarnCompatUnqualifiedImports + $ when bad_import + $ addWarnAt (Reason Opt_WarnCompatUnqualifiedImports) loc warning + where + mod = mi_module iface + loc = getLoc $ ideclName decl + + is_qual = isImportDeclQualified (ideclQualified decl) + has_import_list = + -- We treat a `hiding` clause as not having an import list although + -- it's not entirely clear this is the right choice. + case ideclHiding decl of + Just (False, _) -> True + _ -> False + bad_import = + mod `elemModuleSet` qualifiedMods + && not is_qual + && not has_import_list + + warning = vcat + [ text "To ensure compatibility with future core libraries changes" + , text "imports to" <+> ppr (ideclName decl) <+> text "should be" + , text "either qualified or have an explicit import list." + ] + + -- Modules for which we warn if we see unqualified imports + qualifiedMods = mkModuleSet [ dATA_LIST ] + + warnRedundantSourceImport :: ModuleName -> SDoc warnRedundantSourceImport mod_name = text "Unnecessary {-# SOURCE #-} in the import of module" diff --git a/compiler/main/DynFlags.hs b/compiler/main/DynFlags.hs index be40ff9e2e..3064c1991d 100644 --- a/compiler/main/DynFlags.hs +++ b/compiler/main/DynFlags.hs @@ -926,6 +926,7 @@ data WarningFlag = | Opt_WarnUnusedPackages -- Since 8.10 | Opt_WarnInferredSafeImports -- Since 8.10 | Opt_WarnMissingSafeHaskellMode -- Since 8.10 + | Opt_WarnCompatUnqualifiedImports -- Since 8.10 | Opt_WarnDerivingDefaults deriving (Eq, Show, Enum) @@ -4163,7 +4164,8 @@ wWarningFlagsDeps = [ flagSpec "partial-fields" Opt_WarnPartialFields, flagSpec "prepositive-qualified-module" Opt_WarnPrepositiveQualifiedModule, - flagSpec "unused-packages" Opt_WarnUnusedPackages + flagSpec "unused-packages" Opt_WarnUnusedPackages, + flagSpec "compat-unqualified-imports" Opt_WarnCompatUnqualifiedImports ] -- | These @-\<blah\>@ flags can all be reversed with @-no-\<blah\>@ @@ -4931,6 +4933,7 @@ minusWcompatOpts , Opt_WarnSemigroup , Opt_WarnNonCanonicalMonoidInstances , Opt_WarnStarIsType + , Opt_WarnCompatUnqualifiedImports ] enableUnusedBinds :: DynP () diff --git a/compiler/prelude/PrelNames.hs b/compiler/prelude/PrelNames.hs index 17aee23fab..095b853927 100644 --- a/compiler/prelude/PrelNames.hs +++ b/compiler/prelude/PrelNames.hs @@ -502,7 +502,7 @@ gHC_PRIM, gHC_TYPES, gHC_GENERICS, gHC_MAGIC, gHC_CLASSES, gHC_PRIMOPWRAPPERS, gHC_BASE, gHC_ENUM, gHC_GHCI, gHC_GHCI_HELPERS, gHC_CSTRING, gHC_SHOW, gHC_READ, gHC_NUM, gHC_MAYBE, gHC_INTEGER_TYPE, gHC_NATURAL, - gHC_LIST, gHC_TUPLE, dATA_TUPLE, dATA_EITHER, dATA_STRING, + gHC_LIST, gHC_TUPLE, dATA_TUPLE, dATA_EITHER, dATA_LIST, dATA_STRING, dATA_FOLDABLE, dATA_TRAVERSABLE, gHC_CONC, gHC_IO, gHC_IO_Exception, gHC_ST, gHC_IX, gHC_STABLE, gHC_PTR, gHC_ERR, gHC_REAL, @@ -534,6 +534,7 @@ gHC_LIST = mkBaseModule (fsLit "GHC.List") gHC_TUPLE = mkPrimModule (fsLit "GHC.Tuple") dATA_TUPLE = mkBaseModule (fsLit "Data.Tuple") dATA_EITHER = mkBaseModule (fsLit "Data.Either") +dATA_LIST = mkBaseModule (fsLit "Data.List") dATA_STRING = mkBaseModule (fsLit "Data.String") dATA_FOLDABLE = mkBaseModule (fsLit "Data.Foldable") dATA_TRAVERSABLE= mkBaseModule (fsLit "Data.Traversable") diff --git a/docs/users_guide/using-warnings.rst b/docs/users_guide/using-warnings.rst index 586af57136..aab74d36c2 100644 --- a/docs/users_guide/using-warnings.rst +++ b/docs/users_guide/using-warnings.rst @@ -124,6 +124,7 @@ The following flags are simple ways to select standard "packages" of warnings: * :ghc-flag:`-Wsemigroup` * :ghc-flag:`-Wnoncanonical-monoid-instances` * :ghc-flag:`-Wstar-is-type` + * :ghc-flag:`-Wcompat-unqualified-imports` .. ghc-flag:: -Wno-compat :shortdesc: Disables all warnings enabled by :ghc-flag:`-Wcompat`. @@ -219,12 +220,31 @@ of ``-W(no-)*``. encountered on the command line. :type: dynamic :reverse: -Wno-unrecognised-warning-flags + :default: on :category: Enables warnings when the compiler encounters a ``-W...`` flag that is not recognised. - This warning is on by default. +.. ghc-flag:: -Wcompat-unqualified-imports + :shortdesc: Report unqualified imports of core libraries which are expected + to cause compatibility problems in future releases. + :type: dynamic + :reverse: -Wno-compat-unqualified-imports + :category: + + Warns on qualified imports of core library modules which are subject to + change in future GHC releases. Currently the following modules are covered + by this warning: + + - ``Data.List`` due to the future addition of ``Data.List.singleton`` and + specialisation of exports to the ``[]`` type. See the + :ref:`mailing list + <https://groups.google.com/forum/#!topic/haskell-core-libraries/q3zHLmzBa5E>` + for details. + + This warning can be addressed by either adding an explicit import list or + using a ``qualified`` import. .. ghc-flag:: -Wtyped-holes :shortdesc: Report warnings when :ref:`typed hole <typed-holes>` errors are @@ -232,14 +252,13 @@ of ``-W(no-)*``. :ghc-flag:`-fdefer-typed-holes`. :type: dynamic :reverse: -Wno-typed-holes + :default: on :category: Determines whether the compiler reports typed holes warnings. Has no effect unless typed holes errors are deferred until runtime. See :ref:`typed-holes` and :ref:`defer-type-errors` - This warning is on by default. - .. ghc-flag:: -Wdeferred-type-errors :shortdesc: Report warnings when :ref:`deferred type errors <defer-type-errors>` are enabled. This option is enabled by diff --git a/testsuite/tests/rename/should_compile/T17244A.hs b/testsuite/tests/rename/should_compile/T17244A.hs new file mode 100644 index 0000000000..290120affd --- /dev/null +++ b/testsuite/tests/rename/should_compile/T17244A.hs @@ -0,0 +1,9 @@ +{-# OPTIONS_GHC -Wcompat-unqualified-imports #-} + +module T17244A (hello) where + +-- This should warn with -Wcompat-unqualified-imports. +import Data.List + +hello :: [Int] -> Int +hello = sum diff --git a/testsuite/tests/rename/should_compile/T17244A.stderr b/testsuite/tests/rename/should_compile/T17244A.stderr new file mode 100644 index 0000000000..621e9439f1 --- /dev/null +++ b/testsuite/tests/rename/should_compile/T17244A.stderr @@ -0,0 +1,5 @@ + +T17244A.hs:6:8: warning: [-Wcompat-unqualified-imports (in -Wcompat)] + To ensure compatibility with future core libraries changes + imports to Data.List should be + either qualified or have an explicit import list. diff --git a/testsuite/tests/rename/should_compile/T17244B.hs b/testsuite/tests/rename/should_compile/T17244B.hs new file mode 100644 index 0000000000..61cbde3bee --- /dev/null +++ b/testsuite/tests/rename/should_compile/T17244B.hs @@ -0,0 +1,10 @@ +{-# OPTIONS_GHC -Wcompat-unqualified-imports #-} + +module T17244B (hello) where + +-- This should not warn with -Wcompat-unqualified-imports. +import qualified Data.List as List + +hello :: [Int] -> Int +hello = List.sum + diff --git a/testsuite/tests/rename/should_compile/T17244B.stderr b/testsuite/tests/rename/should_compile/T17244B.stderr new file mode 100644 index 0000000000..e69de29bb2 --- /dev/null +++ b/testsuite/tests/rename/should_compile/T17244B.stderr diff --git a/testsuite/tests/rename/should_compile/T17244C.hs b/testsuite/tests/rename/should_compile/T17244C.hs new file mode 100644 index 0000000000..3da92dddd6 --- /dev/null +++ b/testsuite/tests/rename/should_compile/T17244C.hs @@ -0,0 +1,10 @@ +{-# OPTIONS_GHC -Wcompat-unqualified-imports #-} + +module T17244C (hello) where + +-- This should not warn with -Wcompat-unqualified-imports. +import Data.List (sum) + +hello :: [Int] -> Int +hello = sum + diff --git a/testsuite/tests/rename/should_compile/T17244C.stderr b/testsuite/tests/rename/should_compile/T17244C.stderr new file mode 100644 index 0000000000..e69de29bb2 --- /dev/null +++ b/testsuite/tests/rename/should_compile/T17244C.stderr diff --git a/testsuite/tests/rename/should_compile/all.T b/testsuite/tests/rename/should_compile/all.T index 51684f1eb3..b461d09076 100644 --- a/testsuite/tests/rename/should_compile/all.T +++ b/testsuite/tests/rename/should_compile/all.T @@ -169,3 +169,6 @@ test('T15798b', normal, compile, ['']) test('T15798c', normal, compile, ['']) test('T16116a', normal, compile, ['']) test('T15957', normal, compile, ['-Werror -Wredundant-record-wildcards -Wunused-record-wildcards']) +test('T17244A', normal, compile, ['-Wno-error=compat-unqualified-imports']) +test('T17244B', normal, compile, ['']) +test('T17244C', normal, compile, ['']) |