Commit 01cdc738 authored by Ben Gamari's avatar Ben Gamari 🐢

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`.

Bumps hpc submodule.

Fixes #17244.

[discussion]: https://groups.google.com/d/msg/haskell-core-libraries/q3zHLmzBa5E/PmlAs_kYAQAJ
parent c8c15e0d
......@@ -923,6 +923,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)
......@@ -4141,7 +4142,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\>@
......@@ -4923,6 +4925,7 @@ minusWcompatOpts
, Opt_WarnSemigroup
, Opt_WarnNonCanonicalMonoidInstances
, Opt_WarnStarIsType
, Opt_WarnCompatUnqualifiedImports
]
enableUnusedBinds :: DynP ()
......
......@@ -501,7 +501,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,
......@@ -533,6 +533,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")
......
......@@ -377,6 +377,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 })
......@@ -484,6 +487,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"
......
......@@ -125,6 +125,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`.
......@@ -220,12 +221,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
......@@ -233,14 +253,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
......
{-# OPTIONS_GHC -Wcompat-unqualified-imports #-}
module T17244A (hello) where
-- This should warn with -Wcompat-unqualified-imports.
import Data.List
hello :: [Int] -> Int
hello = sum
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.
{-# 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
{-# 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
......@@ -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, [''])
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment