summary refs log tree commit diff stats
path: root/compiler/importer.nim
diff options
context:
space:
mode:
authorAraq <rumpf_a@web.de>2016-11-23 23:23:31 +0100
committerAraq <rumpf_a@web.de>2016-11-23 23:23:31 +0100
commit074f276c8a753bbb85788777b7c58a074f41329f (patch)
treed5078972eb1cbd23f7ce59b63e13bb31f02196d6 /compiler/importer.nim
parent204838b3585d13ea88d3b8ac8e7f0fc19e55f3e9 (diff)
downloadNim-074f276c8a753bbb85788777b7c58a074f41329f.tar.gz
disallow recursive module dependencies
Diffstat (limited to 'compiler/importer.nim')
-rw-r--r--compiler/importer.nim20
1 files changed, 17 insertions, 3 deletions
diff --git a/compiler/importer.nim b/compiler/importer.nim
index ce365c4dc..46e4c159f 100644
--- a/compiler/importer.nim
+++ b/compiler/importer.nim
@@ -162,12 +162,26 @@ proc importModuleAs(n: PNode, realModule: PSym): PSym =
 proc myImportModule(c: PContext, n: PNode): PSym =
   var f = checkModuleName(n)
   if f != InvalidFileIDX:
+    let L = c.graph.importStack.len
+    let recursion = c.graph.importStack.find(f)
+    c.graph.importStack.add f
+    #echo "adding ", toFullPath(f), " at ", L+1
+    if recursion >= 0:
+      var err = ""
+      for i in countup(recursion, L-1):
+        if i > 0: err.add "\n"
+        err.add toFullPath(c.graph.importStack[i]) & " imports " &
+                toFullPath(c.graph.importStack[i+1])
+      localError(n.info, "recursive module dependency detected:\n" & err)
     result = importModuleAs(n, gImportModule(c.graph, c.module, f, c.cache))
+    #echo "set back to ", L
+    c.graph.importStack.setLen(L)
     # we cannot perform this check reliably because of
     # test: modules/import_in_config)
-    if result.info.fileIndex == c.module.info.fileIndex and
-        result.info.fileIndex == n.info.fileIndex:
-      localError(n.info, errGenerated, "A module cannot import itself")
+    when false:
+      if result.info.fileIndex == c.module.info.fileIndex and
+          result.info.fileIndex == n.info.fileIndex:
+        localError(n.info, errGenerated, "A module cannot import itself")
     if sfDeprecated in result.flags:
       message(n.info, warnDeprecated, result.name.s)
     #suggestSym(n.info, result, false)