summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorÖmer Sinan Ağacan <omeragacan@gmail.com>2015-10-13 00:47:28 -0500
committerAustin Seipp <austin@well-typed.com>2015-10-13 00:47:35 -0500
commit1818b48e420fd0f689105da76721895aadee7fd6 (patch)
tree686d13d1cc2264dc734b845d811870fb9907ff40
parentd2f9972a35ce05ceb8a78893e433ef1df06f73ef (diff)
downloadhaskell-1818b48e420fd0f689105da76721895aadee7fd6.tar.gz
Fix incorrect import warnings when methods with identical names are imported
Currently, GHC's warning generation code is assuming that a name (`RdrName`) can be imported at most once. This is a correct assumption, because 1) it's OK to import same names as long as we don't use any of them 2) when we use one of them, GHC generates an error because it doesn't disambiguate it automatically. But apparently the story is different with typeclass methods. If I import two methods with same names, it's OK to use them in typeclass instance declarations, because the context specifies which one to use. For example, this is OK (where modules A and B define typeclasses A and B, both with a function has), import A import B data Blah = Blah instance A Blah where has = Blah instance B Blah where has = Blah But GHC's warning generator is not taking this into account, and so if I change import list of this program to: import A (A (has)) import B (B (has)) GHC is printing these warnings: Main.hs:5:1: Warning: The import of ‘A.has’ from module ‘A’ is redundant Main.hs:6:1: Warning: The import of ‘B.has’ from module ‘B’ is redundant Why? Because warning generation code is _silently_ ignoring multiple symbols with same names. With this patch, GHC takes this into account. If there's only one name, then this patch reduces to the previous version, that is, it works exactly the same as current GHC (thanks goes to @quchen for realizing this). Reviewed By: austin Differential Revision: https://phabricator.haskell.org/D1257 GHC Trac Issues: #10890
-rw-r--r--compiler/rename/RnNames.hs10
-rw-r--r--testsuite/tests/warnings/should_compile/T10890/A.hs4
-rw-r--r--testsuite/tests/warnings/should_compile/T10890/B.hs4
-rw-r--r--testsuite/tests/warnings/should_compile/T10890/Base.hs6
-rw-r--r--testsuite/tests/warnings/should_compile/T10890/Extends.hs4
-rw-r--r--testsuite/tests/warnings/should_compile/T10890/Makefile3
-rw-r--r--testsuite/tests/warnings/should_compile/T10890/T10890.hs23
-rw-r--r--testsuite/tests/warnings/should_compile/T10890/T10890_1.hs22
-rw-r--r--testsuite/tests/warnings/should_compile/T10890/all.T7
9 files changed, 77 insertions, 6 deletions
diff --git a/compiler/rename/RnNames.hs b/compiler/rename/RnNames.hs
index 7b067de701..0cdf38c693 100644
--- a/compiler/rename/RnNames.hs
+++ b/compiler/rename/RnNames.hs
@@ -1461,13 +1461,11 @@ extendImportMap :: GlobalRdrEnv -> RdrName -> ImportMap -> ImportMap
-- it into scope; choose one of them (bestImport), and record
-- the RdrName in that import decl's entry in the ImportMap
extendImportMap rdr_env rdr imp_map
- | [gre] <- lookupGRE_RdrName rdr rdr_env
- , GRE { gre_lcl = lcl, gre_imp = imps } <- gre
- , not lcl
- = add_imp gre (bestImport imps) imp_map
- | otherwise
- = imp_map
+ = foldr recordRdrName imp_map nonLocalGREs
where
+ recordRdrName gre m = add_imp gre (bestImport (gre_imp gre)) m
+ nonLocalGREs = filter (not . gre_lcl) (lookupGRE_RdrName rdr rdr_env)
+
add_imp :: GlobalRdrElt -> ImportSpec -> ImportMap -> ImportMap
add_imp gre (ImpSpec { is_decl = imp_decl_spec }) imp_map
= Map.insertWith add decl_loc [avail] imp_map
diff --git a/testsuite/tests/warnings/should_compile/T10890/A.hs b/testsuite/tests/warnings/should_compile/T10890/A.hs
new file mode 100644
index 0000000000..c88ccc5b68
--- /dev/null
+++ b/testsuite/tests/warnings/should_compile/T10890/A.hs
@@ -0,0 +1,4 @@
+module A where
+
+class A a where
+ has :: a
diff --git a/testsuite/tests/warnings/should_compile/T10890/B.hs b/testsuite/tests/warnings/should_compile/T10890/B.hs
new file mode 100644
index 0000000000..cfc279019a
--- /dev/null
+++ b/testsuite/tests/warnings/should_compile/T10890/B.hs
@@ -0,0 +1,4 @@
+module B where
+
+class B a where
+ has :: a
diff --git a/testsuite/tests/warnings/should_compile/T10890/Base.hs b/testsuite/tests/warnings/should_compile/T10890/Base.hs
new file mode 100644
index 0000000000..f56bd9ad60
--- /dev/null
+++ b/testsuite/tests/warnings/should_compile/T10890/Base.hs
@@ -0,0 +1,6 @@
+module Base (AClass(..), BClass()) where
+
+import Extends (BClass ())
+
+class AClass a where
+ has :: a
diff --git a/testsuite/tests/warnings/should_compile/T10890/Extends.hs b/testsuite/tests/warnings/should_compile/T10890/Extends.hs
new file mode 100644
index 0000000000..a81013e490
--- /dev/null
+++ b/testsuite/tests/warnings/should_compile/T10890/Extends.hs
@@ -0,0 +1,4 @@
+module Extends where
+
+class BClass b where
+ has :: b
diff --git a/testsuite/tests/warnings/should_compile/T10890/Makefile b/testsuite/tests/warnings/should_compile/T10890/Makefile
new file mode 100644
index 0000000000..1c39d1c1fe
--- /dev/null
+++ b/testsuite/tests/warnings/should_compile/T10890/Makefile
@@ -0,0 +1,3 @@
+TOP=../../../..
+include $(TOP)/mk/boilerplate.mk
+include $(TOP)/mk/test.mk
diff --git a/testsuite/tests/warnings/should_compile/T10890/T10890.hs b/testsuite/tests/warnings/should_compile/T10890/T10890.hs
new file mode 100644
index 0000000000..a6c0751927
--- /dev/null
+++ b/testsuite/tests/warnings/should_compile/T10890/T10890.hs
@@ -0,0 +1,23 @@
+module Main where
+
+-- Previously GHC was printing this warning:
+--
+-- Main.hs:5:1: Warning:
+-- The import of ‘A.has’ from module ‘A’ is redundant
+--
+-- Main.hs:6:1: Warning:
+-- The import of ‘B.has’ from module ‘B’ is redundant
+
+import A (A (has))
+import B (B (has))
+
+data Blah = Blah
+
+instance A Blah where
+ has = Blah
+
+instance B Blah where
+ has = Blah
+
+main :: IO ()
+main = return ()
diff --git a/testsuite/tests/warnings/should_compile/T10890/T10890_1.hs b/testsuite/tests/warnings/should_compile/T10890/T10890_1.hs
new file mode 100644
index 0000000000..7614ee3fa5
--- /dev/null
+++ b/testsuite/tests/warnings/should_compile/T10890/T10890_1.hs
@@ -0,0 +1,22 @@
+module Main where
+
+import Base
+import Extends
+
+-- Previously GHC was giving this false positive:
+--
+-- T10890_1.hs:4:1: Warning:
+-- The import of ‘Extends’ is redundant
+-- except perhaps to import instances from ‘Extends’
+-- To import instances alone, use: import Extends()
+
+data Bar = Bar
+
+instance AClass Bar where
+ has = Bar
+
+instance BClass Bar where
+ has = Bar
+
+main :: IO ()
+main = return ()
diff --git a/testsuite/tests/warnings/should_compile/T10890/all.T b/testsuite/tests/warnings/should_compile/T10890/all.T
new file mode 100644
index 0000000000..3e323f0ed0
--- /dev/null
+++ b/testsuite/tests/warnings/should_compile/T10890/all.T
@@ -0,0 +1,7 @@
+test('T10890',
+ extra_clean(['A.o', 'A.hi', 'B.o', 'B.hi']),
+ multimod_compile, ['T10890', '-v0 -fwarn-unused-imports'])
+
+test('T10890_1',
+ extra_clean(['Base.o', 'Base.hi', 'Extends.o', 'Extends.hi']),
+ multimod_compile, ['T10890_1', '-v0 -fwarn-unused-imports'])