[commit: ghc] master: Test earlier for self-import (Trac #9032) (edd233a)

git at git.haskell.org git at git.haskell.org
Tue Dec 23 16:29:46 UTC 2014


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

On branch  : master
Link       : http://ghc.haskell.org/trac/ghc/changeset/edd233acc19d269385c1a870829e0916a3df8e88/ghc

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

commit edd233acc19d269385c1a870829e0916a3df8e88
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


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

edd233acc19d269385c1a870829e0916a3df8e88
 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 |  3 +++
 testsuite/tests/rename/should_fail/all.T        |  5 +++++
 5 files changed, 36 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..037694c 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..56b9158
--- /dev/null
+++ b/testsuite/tests/rename/should_fail/T9032.stderr
@@ -0,0 +1,3 @@
+
+T9032.hs:6:1: A module cannot import itself: T9032
+make[2]: *** [T9032] Error 1
diff --git a/testsuite/tests/rename/should_fail/all.T b/testsuite/tests/rename/should_fail/all.T
index 2798fe9..8d60ef3 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',
+     exit_code(2),
+     run_command,
+     ['$MAKE -s --no-print-directory T9032'])



More information about the ghc-commits mailing list