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 /compiler/GHC | |
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
Diffstat (limited to 'compiler/GHC')
-rw-r--r-- | compiler/GHC/Rename/Names.hs | 37 |
1 files changed, 37 insertions, 0 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" |