summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorAndreas Rumpf <rumpf_a@web.de>2014-10-20 01:20:39 +0200
committerAndreas Rumpf <rumpf_a@web.de>2014-10-20 01:20:39 +0200
commitb29c0da7b5f450cff50a7d9830d5bb449042f94f (patch)
tree6401af8e3f8539103dd22dd3ce36783fedade468
parent60a7532bdac51e7be03461d5d402d440794d79a8 (diff)
parent8b70e2c0e7ccbca799634f8c3c25dbb244a141e8 (diff)
downloadNim-b29c0da7b5f450cff50a7d9830d5bb449042f94f.tar.gz
Merge pull request #1574 from trustable-code/PR3
Do not allow self import
-rw-r--r--compiler/importer.nim6
-rw-r--r--tests/modules/tselfimport.nim8
2 files changed, 12 insertions, 2 deletions
diff --git a/compiler/importer.nim b/compiler/importer.nim
index b4cae017e..9e327c8f7 100644
--- a/compiler/importer.nim
+++ b/compiler/importer.nim
@@ -92,7 +92,7 @@ proc rawImportSymbol(c: PContext, s: PSym) =
     if s.kind == skConverter: addConverter(c, s)
     if hasPattern(s): addPattern(c, s)
 
-proc importSymbol(c: PContext, n: PNode, fromMod: PSym) = 
+proc importSymbol(c: PContext, n: PNode, fromMod: PSym) =        
   let ident = lookups.considerQuotedIdent(n)
   let s = strTableGet(fromMod.tab, ident)
   if s == nil:
@@ -155,10 +155,12 @@ proc importModuleAs(n: PNode, realModule: PSym): PSym =
     # some misguided guy will write 'import abc.foo as foo' ...
     result = createModuleAlias(realModule, n.sons[1].ident, n.sons[1].info)
 
-proc myImportModule(c: PContext, n: PNode): PSym =
+proc myImportModule(c: PContext, n: PNode): PSym = 
   var f = checkModuleName(n)
   if f != InvalidFileIDX:
     result = importModuleAs(n, gImportModule(c.module, f))
+    if 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)
 
diff --git a/tests/modules/tselfimport.nim b/tests/modules/tselfimport.nim
new file mode 100644
index 000000000..f20a40407
--- /dev/null
+++ b/tests/modules/tselfimport.nim
@@ -0,0 +1,8 @@
+discard """
+  file: "tselfimport.nim"
+  line: 6
+  errormsg: "A module cannot import itself"
+"""
+import tselfimport #ERROR
+echo("Hello World")
+