[commit: ghc] ghc-7.10: Test earlier for self-import (Trac #9032) (9e87d8c)

git at git.haskell.org git at git.haskell.org
Sat Dec 27 12:12:07 UTC 2014


Repository : ssh://git@git.haskell.org/ghc

On branch  : ghc-7.10
Link       : http://ghc.haskell.org/trac/ghc/changeset/9e87d8c9d5121db3492f7d8f26b2a7edbba7f03f/ghc

>---------------------------------------------------------------

commit 9e87d8c9d5121db3492f7d8f26b2a7edbba7f03f
Author: Simon Peyton Jones <simonpj at microsoft.com>
Date:   Tue Dec 23 15:59:30 2014 +0000

    Test earlier for self-import (Trac #9032)
    
    This patch makes the renamer check for self-import, especially when
    dependencies change, because the typechecker can fall over if that
    happens.
    
    I'm still uneasy about *indirect* self-import, but I'll leave that for
    another day
    
    (cherry picked from commit edd233acc19d269385c1a870829e0916a3df8e88,
    7a2c9dde24b72fe53216881867d5543e5a6f756c, and
    add6a30317ca7307a91563afbbf102c3771ee246)


>---------------------------------------------------------------

9e87d8c9d5121db3492f7d8f26b2a7edbba7f03f
 compiler/rename/RnNames.hs                      | 14 +++++++++++---
 testsuite/tests/rename/should_fail/Makefile     |  5 +++++
 testsuite/tests/rename/should_fail/T9032.hs     | 12 ++++++++++++
 testsuite/tests/rename/should_fail/T9032.stderr |  2 ++
 testsuite/tests/rename/should_fail/all.T        |  5 +++++
 5 files changed, 35 insertions(+), 3 deletions(-)

diff --git a/compiler/rename/RnNames.hs b/compiler/rename/RnNames.hs
index bff2ed0..145d6fc 100644
--- a/compiler/rename/RnNames.hs
+++ b/compiler/rename/RnNames.hs
@@ -181,6 +181,14 @@ rnImportDecl this_mod
     let imp_mod_name = unLoc loc_imp_mod_name
         doc = ppr imp_mod_name <+> ptext (sLit "is directly imported")
 
+    -- Check for self-import, which confuses the typechecker (Trac #9032)
+    -- ghc --make rejects self-import cycles already, but batch-mode may not
+    -- at least not until TcIface.tcHiBootIface, which is too late to avoid
+    -- typechecker crashes.  ToDo: what about indirect self-import?
+    -- But 'import {-# SOURCE #-} M' is ok, even if a bit odd
+    when (not want_boot && imp_mod_name == moduleName this_mod)
+         (addErr (ptext (sLit "A module cannot import itself:") <+> ppr imp_mod_name))
+
     -- Check for a missing import list (Opt_WarnMissingImportList also
     -- checks for T(..) items but that is done in checkDodgyImport below)
     case imp_details of
@@ -212,9 +220,9 @@ rnImportDecl this_mod
     warnIf (want_boot && any (not.mi_boot) ifaces && isOneShot (ghcMode dflags))
            (warnRedundantSourceImport imp_mod_name)
     when (mod_safe && not (safeImportsOn dflags)) $
-        addErrAt loc (ptext (sLit "safe import can't be used as Safe Haskell isn't on!")
-                  $+$ ptext (sLit $ "please enable Safe Haskell through either "
-                                 ++ "Safe, Trustworthy or Unsafe"))
+        addErr (ptext (sLit "safe import can't be used as Safe Haskell isn't on!")
+                $+$ ptext (sLit $ "please enable Safe Haskell through either "
+                                   ++ "Safe, Trustworthy or Unsafe"))
 
     let
         qual_mod_name = as_mod `orElse` imp_mod_name
diff --git a/testsuite/tests/rename/should_fail/Makefile b/testsuite/tests/rename/should_fail/Makefile
index 9101fbd..b2393f6 100644
--- a/testsuite/tests/rename/should_fail/Makefile
+++ b/testsuite/tests/rename/should_fail/Makefile
@@ -1,3 +1,8 @@
 TOP=../../..
 include $(TOP)/mk/boilerplate.mk
 include $(TOP)/mk/test.mk
+
+T9032:
+	'$(TEST_HC)' $(TEST_HC_OPTS) -c -fforce-recomp T9032.hs
+	-'$(TEST_HC)' $(TEST_HC_OPTS) -c -fforce-recomp -DERR T9032.hs
+
diff --git a/testsuite/tests/rename/should_fail/T9032.hs b/testsuite/tests/rename/should_fail/T9032.hs
new file mode 100644
index 0000000..0a00ba3
--- /dev/null
+++ b/testsuite/tests/rename/should_fail/T9032.hs
@@ -0,0 +1,12 @@
+{-# LANGUAGE CPP #-}
+
+module T9032 where
+
+#ifdef ERR
+import T9032
+#endif
+
+f x = x
+
+
+
diff --git a/testsuite/tests/rename/should_fail/T9032.stderr b/testsuite/tests/rename/should_fail/T9032.stderr
new file mode 100644
index 0000000..21af9ac
--- /dev/null
+++ b/testsuite/tests/rename/should_fail/T9032.stderr
@@ -0,0 +1,2 @@
+
+T9032.hs:6:1: A module cannot import itself: T9032
diff --git a/testsuite/tests/rename/should_fail/all.T b/testsuite/tests/rename/should_fail/all.T
index 2798fe9..2aeee2f 100644
--- a/testsuite/tests/rename/should_fail/all.T
+++ b/testsuite/tests/rename/should_fail/all.T
@@ -127,3 +127,8 @@ test('T9436', normal, compile_fail, [''])
 test('T9437', normal, compile_fail, [''])
 test('T9077', normal, compile_fail, [''])
 test('T9815', normal, compile_fail, [''])
+
+test('T9032',
+     normal,
+     run_command,
+     ['$MAKE -s --no-print-directory T9032'])



More information about the ghc-commits mailing list