From 849484f9202c66567a8e6937198498ef8a75f558 Mon Sep 17 00:00:00 2001 From: Simon Krauter Date: Tue, 14 Oct 2014 23:38:50 +0200 Subject: Do not allow self import --- compiler/importer.nim | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/compiler/importer.nim b/compiler/importer.nim index b4cae017e..4a0d83c66 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.name.s == fileInfos[n.info.fileIndex].shortName: + localError(n.info, errGenerated, "A module cannot import itself") if sfDeprecated in result.flags: message(n.info, warnDeprecated, result.name.s) -- cgit 1.4.1-2-gfad0 From 27585ee6ecd0dd2f7673b800e921ec2b3850e608 Mon Sep 17 00:00:00 2001 From: Simon Krauter Date: Wed, 15 Oct 2014 00:20:08 +0200 Subject: Compare fileIndexes instead of file names --- compiler/importer.nim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/importer.nim b/compiler/importer.nim index 4a0d83c66..9e327c8f7 100644 --- a/compiler/importer.nim +++ b/compiler/importer.nim @@ -159,7 +159,7 @@ proc myImportModule(c: PContext, n: PNode): PSym = var f = checkModuleName(n) if f != InvalidFileIDX: result = importModuleAs(n, gImportModule(c.module, f)) - if result.name.s == fileInfos[n.info.fileIndex].shortName: + 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) -- cgit 1.4.1-2-gfad0 From 8b70e2c0e7ccbca799634f8c3c25dbb244a141e8 Mon Sep 17 00:00:00 2001 From: Simon Krauter Date: Wed, 15 Oct 2014 00:31:55 +0200 Subject: Added test case --- tests/modules/tselfimport.nim | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 tests/modules/tselfimport.nim 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") + -- cgit 1.4.1-2-gfad0