From f6cb66feab3439f41ca87cb307f69b49654883ab Mon Sep 17 00:00:00 2001 From: Tim Foley Date: Mon, 26 Jun 2017 10:52:31 -0700 Subject: Check for re-import at translation-unit level Previously the code checked for a duplicate `#import` using a data structure attached to the compile request, but this would fail for nested imports. It also wouldn't work for a combination of `#import` and `__import`. This change makes it so that we instead track a set of already-imported modules in the semantic checking visitor, which is instantiated once per translation unit. We also key this set on the actual module (AST) imported, rather than on path/name/whatever, so hopefully it will be robust to the same thing getting imported multiple ways. --- source/slang/check.cpp | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'source/slang/check.cpp') diff --git a/source/slang/check.cpp b/source/slang/check.cpp index 463052bc8..582f19448 100644 --- a/source/slang/check.cpp +++ b/source/slang/check.cpp @@ -52,6 +52,14 @@ namespace Slang // lexical outer statements List outerStmts; + + // We need to track what has been `import`ed, + // to avoid importing the same thing more than once + // + // TODO: a smarter approach might be to filter + // out duplicate references during lookup. + HashSet importedModules; + public: SemanticsVisitor( DiagnosticSink * pErr, @@ -4909,6 +4917,15 @@ namespace Slang // it later during code generation. decl->importedModuleDecl = importedModuleDecl; + // If we've imported this one already, then + // skip the step where we modify the current scope. + if (importedModules.Contains(importedModuleDecl.Ptr())) + { + return; + } + importedModules.Add(importedModuleDecl.Ptr()); + + // Create a new sub-scope to wire the module // into our lookup chain. auto subScope = new Scope(); -- cgit v1.2.3