Improve the warning message of qualified unused imports.
authorHE, Tao <sighingnow@gmail.com>
Mon, 19 Mar 2018 15:58:26 +0000 (11:58 -0400)
committerBen Gamari <ben@smart-cactus.org>
Mon, 19 Mar 2018 16:05:11 +0000 (12:05 -0400)
Pretty-print unused imported names unqualified unconditionally to
make the warning message consistent for ambiguous/unambiguous
identifiers.

Signed-off-by: HE, Tao <sighingnow@gmail.com>
Test Plan: make test TEST="T14881"

Reviewers: bgamari, simonpj

Reviewed By: simonpj

Subscribers: simonpj, rwbarton, thomie, carter

GHC Trac Issues: #14881

Differential Revision: https://phabricator.haskell.org/D4461

compiler/basicTypes/Name.hs
compiler/rename/RnNames.hs
testsuite/tests/rename/should_compile/T14881.hs [new file with mode: 0644]
testsuite/tests/rename/should_compile/T14881.stderr [new file with mode: 0644]
testsuite/tests/rename/should_compile/T14881Aux.hs [new file with mode: 0644]
testsuite/tests/rename/should_compile/all.T

index 02eb067..6941dd9 100644 (file)
@@ -70,7 +70,7 @@ module Name (
         NamedThing(..),
         getSrcLoc, getSrcSpan, getOccString, getOccFS,
 
-        pprInfixName, pprPrefixName, pprModulePrefix,
+        pprInfixName, pprPrefixName, pprModulePrefix, pprNameUnqualified,
         nameStableString,
 
         -- Re-export the OccName stuff
@@ -535,6 +535,10 @@ pprName (Name {n_sort = sort, n_uniq = uniq, n_occ = occ})
       System                  -> pprSystem sty uniq occ
       Internal                -> pprInternal sty uniq occ
 
+-- | Print the string of Name unqualifiedly directly.
+pprNameUnqualified :: Name -> SDoc
+pprNameUnqualified Name { n_occ = occ } = ppr_occ_name occ
+
 pprExternal :: PprStyle -> Unique -> Module -> OccName -> Bool -> BuiltInSyntax -> SDoc
 pprExternal sty uniq mod occ is_wired is_builtin
   | codeStyle sty = ppr mod <> char '_' <> ppr_z_occ_name occ
index 769b34e..af00056 100644 (file)
@@ -1401,9 +1401,12 @@ warnUnusedImport flag fld_env (L loc decl, used, unused)
     pp_mod      = ppr (unLoc (ideclName decl))
     pp_not_used = text "is redundant"
 
+    -- In warning message, pretty-print identifiers unqualified unconditionally
+    -- to improve the consistent for ambiguous/unambiguous identifiers.
+    -- See trac#14881.
     ppr_possible_field n = case lookupNameEnv fld_env n of
-                               Just (fld, p) -> ppr p <> parens (ppr fld)
-                               Nothing  -> ppr n
+                               Just (fld, p) -> pprNameUnqualified p <> parens (ppr fld)
+                               Nothing  -> pprNameUnqualified n
 
     -- Print unused names in a deterministic (lexicographic) order
     sort_unused = pprWithCommas ppr_possible_field $
diff --git a/testsuite/tests/rename/should_compile/T14881.hs b/testsuite/tests/rename/should_compile/T14881.hs
new file mode 100644 (file)
index 0000000..c1b955c
--- /dev/null
@@ -0,0 +1,5 @@
+module T14881 where
+
+import qualified T14881Aux as Aux (L(Cons), x, tail, adjust, length)
+
+x = Aux.Cons
diff --git a/testsuite/tests/rename/should_compile/T14881.stderr b/testsuite/tests/rename/should_compile/T14881.stderr
new file mode 100644 (file)
index 0000000..bfb6ca9
--- /dev/null
@@ -0,0 +1,6 @@
+[1 of 2] Compiling T14881Aux        ( T14881Aux.hs, T14881Aux.o )
+[2 of 2] Compiling T14881           ( T14881.hs, T14881.o )
+
+T14881.hs:3:1: warning: [-Wunused-imports (in -Wextra)]
+    The qualified import of ‘adjust, length, L(tail), L(x)’
+    from module ‘T14881Aux’ is redundant
diff --git a/testsuite/tests/rename/should_compile/T14881Aux.hs b/testsuite/tests/rename/should_compile/T14881Aux.hs
new file mode 100644 (file)
index 0000000..13b8f31
--- /dev/null
@@ -0,0 +1,13 @@
+module T14881Aux where
+
+-- unambiguous function name.
+adjust :: ()
+adjust = undefined
+
+-- ambiguous function name.
+length :: ()
+length = undefined
+
+data L = Cons { x :: Int      -- unambiguous field selector
+              , tail :: [Int] -- ambiguous field selector
+              }
index 4eb584f..80bcb09 100644 (file)
@@ -153,3 +153,4 @@ test('T12548', normal, compile, [''])
 test('T13132', normal, compile, [''])
 test('T13646', normal, compile, [''])
 test('LookupSub', [], multimod_compile, ['LookupSub', '-v0'])
+test('T14881', [], multimod_compile, ['T14881', '-W'])