@@ -257,6 +257,42 @@ enum class ConstructorComparison {
257
257
Better,
258
258
};
259
259
260
+ bool swift::removeOutOfModuleDecls (SmallVectorImpl<ValueDecl*> &decls,
261
+ Identifier moduleSelector,
262
+ const DeclContext *dc) {
263
+ if (moduleSelector.empty ())
264
+ return false ;
265
+
266
+ ASTContext &ctx = dc->getASTContext ();
267
+
268
+ // FIXME: Should we look this up relative to dc?
269
+ // We'd need a new ResolutionKind.
270
+ // FIXME: How can we diagnose this?
271
+ ModuleDecl *visibleFrom = ctx.getLoadedModule (moduleSelector);
272
+ if (!visibleFrom) {
273
+ LLVM_DEBUG (llvm::dbgs () << " no module " << moduleSelector << " \n " );
274
+ decls.clear ();
275
+ return true ;
276
+ }
277
+
278
+ bool initialCount = decls.size ();
279
+ decls.erase (
280
+ std::remove_if (decls.begin (), decls.end (), [&](ValueDecl *decl) -> bool {
281
+ bool inScope = ctx.getImportCache ().isImportedBy (decl->getModuleContext (),
282
+ visibleFrom);
283
+
284
+ LLVM_DEBUG (decl->dumpRef (llvm::dbgs ()));
285
+ LLVM_DEBUG (llvm::dbgs () << " : " << decl->getModuleContext ()->getName ()
286
+ << (inScope ? " is " : " is NOT " )
287
+ << " selected by " << visibleFrom->getName ()
288
+ << " \n " );
289
+
290
+ return !inScope;
291
+ }),
292
+ decls.end ());
293
+ return initialCount != decls.size ();
294
+ }
295
+
260
296
// / Determines whether \p ctor1 is a "better" initializer than \p ctor2.
261
297
static ConstructorComparison compareConstructors (ConstructorDecl *ctor1,
262
298
ConstructorDecl *ctor2,
@@ -1577,14 +1613,17 @@ static bool isAcceptableLookupResult(const DeclContext *dc,
1577
1613
}
1578
1614
1579
1615
void namelookup::pruneLookupResultSet (const DeclContext *dc, NLOptions options,
1616
+ Identifier moduleSelector,
1580
1617
SmallVectorImpl<ValueDecl *> &decls) {
1581
1618
// If we're supposed to remove overridden declarations, do so now.
1582
1619
if (options & NL_RemoveOverridden)
1583
1620
removeOverriddenDecls (decls);
1584
1621
1585
1622
// If we're supposed to remove shadowed/hidden declarations, do so now.
1586
- if (options & NL_RemoveNonVisible)
1623
+ if (options & NL_RemoveNonVisible) {
1624
+ removeOutOfModuleDecls (decls, moduleSelector, dc);
1587
1625
removeShadowedDecls (decls, dc);
1626
+ }
1588
1627
1589
1628
ModuleDecl *M = dc->getParentModule ();
1590
1629
filterForDiscriminator (decls, M->getDebugClient ());
@@ -1818,7 +1857,7 @@ QualifiedLookupRequest::evaluate(Evaluator &eval, const DeclContext *DC,
1818
1857
}
1819
1858
}
1820
1859
1821
- pruneLookupResultSet (DC, options, decls);
1860
+ pruneLookupResultSet (DC, options, member. getModuleSelector (), decls);
1822
1861
if (auto *debugClient = DC->getParentModule ()->getDebugClient ()) {
1823
1862
debugClient->finishLookupInNominals (DC, typeDecls, member.getFullName (),
1824
1863
options, decls);
@@ -1870,7 +1909,7 @@ ModuleQualifiedLookupRequest::evaluate(Evaluator &eval, const DeclContext *DC,
1870
1909
}
1871
1910
}
1872
1911
1873
- pruneLookupResultSet (DC, options, decls);
1912
+ pruneLookupResultSet (DC, options, member. getModuleSelector (), decls);
1874
1913
1875
1914
if (auto *debugClient = DC->getParentModule ()->getDebugClient ()) {
1876
1915
debugClient->finishLookupInModule (DC, module , member.getFullName (),
@@ -1938,7 +1977,7 @@ AnyObjectLookupRequest::evaluate(Evaluator &evaluator, const DeclContext *dc,
1938
1977
decls.push_back (decl);
1939
1978
}
1940
1979
1941
- pruneLookupResultSet (dc, options, decls);
1980
+ pruneLookupResultSet (dc, options, member. getModuleSelector (), decls);
1942
1981
if (auto *debugClient = dc->getParentModule ()->getDebugClient ()) {
1943
1982
debugClient->finishLookupInAnyObject (dc, member.getFullName (), options,
1944
1983
decls);
0 commit comments