From 4435a8e0b74337fe5faddb9c46691f0f5bf9e653 Mon Sep 17 00:00:00 2001 From: Ben Gamari Date: Sun, 29 Dec 2019 15:16:24 -0500 Subject: 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 --- compiler/GHC/Rename/Names.hs | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) (limited to 'compiler/GHC') 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" -- cgit v1.2.1