Skip to content

Commit bd7491a

Browse files
committed
Support module selectors in qualified lookup
# Conflicts: # lib/ClangImporter/ImportDecl.cpp
1 parent 1e14446 commit bd7491a

File tree

3 files changed

+61
-9
lines changed

3 files changed

+61
-9
lines changed

include/swift/AST/NameLookup.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,14 @@ class UsableFilteringDeclConsumer final : public VisibleDeclConsumer {
438438
/// \returns true if any declarations were removed, false otherwise.
439439
bool removeOverriddenDecls(SmallVectorImpl<ValueDecl*> &decls);
440440

441+
/// Remove any declarations in the given set that do not match the
442+
/// module selector, if it is not empty.
443+
///
444+
/// \returns true if any declarations were removed, false otherwise.
445+
bool removeOutOfModuleDecls(SmallVectorImpl<ValueDecl*> &decls,
446+
Identifier moduleSelector,
447+
const DeclContext *dc);
448+
441449
/// Remove any declarations in the given set that are shadowed by
442450
/// other declarations in that set.
443451
///
@@ -494,6 +502,7 @@ namespace namelookup {
494502
/// Once name lookup has gathered a set of results, perform any necessary
495503
/// steps to prune the result set before returning it to the caller.
496504
void pruneLookupResultSet(const DeclContext *dc, NLOptions options,
505+
Identifier moduleSelector,
497506
SmallVectorImpl<ValueDecl *> &decls);
498507

499508
/// Do nothing if debugClient is null.

lib/AST/NameLookup.cpp

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,42 @@ enum class ConstructorComparison {
257257
Better,
258258
};
259259

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+
260296
/// Determines whether \p ctor1 is a "better" initializer than \p ctor2.
261297
static ConstructorComparison compareConstructors(ConstructorDecl *ctor1,
262298
ConstructorDecl *ctor2,
@@ -1577,14 +1613,17 @@ static bool isAcceptableLookupResult(const DeclContext *dc,
15771613
}
15781614

15791615
void namelookup::pruneLookupResultSet(const DeclContext *dc, NLOptions options,
1616+
Identifier moduleSelector,
15801617
SmallVectorImpl<ValueDecl *> &decls) {
15811618
// If we're supposed to remove overridden declarations, do so now.
15821619
if (options & NL_RemoveOverridden)
15831620
removeOverriddenDecls(decls);
15841621

15851622
// 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);
15871625
removeShadowedDecls(decls, dc);
1626+
}
15881627

15891628
ModuleDecl *M = dc->getParentModule();
15901629
filterForDiscriminator(decls, M->getDebugClient());
@@ -1818,7 +1857,7 @@ QualifiedLookupRequest::evaluate(Evaluator &eval, const DeclContext *DC,
18181857
}
18191858
}
18201859

1821-
pruneLookupResultSet(DC, options, decls);
1860+
pruneLookupResultSet(DC, options, member.getModuleSelector(), decls);
18221861
if (auto *debugClient = DC->getParentModule()->getDebugClient()) {
18231862
debugClient->finishLookupInNominals(DC, typeDecls, member.getFullName(),
18241863
options, decls);
@@ -1870,7 +1909,7 @@ ModuleQualifiedLookupRequest::evaluate(Evaluator &eval, const DeclContext *DC,
18701909
}
18711910
}
18721911

1873-
pruneLookupResultSet(DC, options, decls);
1912+
pruneLookupResultSet(DC, options, member.getModuleSelector(), decls);
18741913

18751914
if (auto *debugClient = DC->getParentModule()->getDebugClient()) {
18761915
debugClient->finishLookupInModule(DC, module, member.getFullName(),
@@ -1938,7 +1977,7 @@ AnyObjectLookupRequest::evaluate(Evaluator &evaluator, const DeclContext *dc,
19381977
decls.push_back(decl);
19391978
}
19401979

1941-
pruneLookupResultSet(dc, options, decls);
1980+
pruneLookupResultSet(dc, options, member.getModuleSelector(), decls);
19421981
if (auto *debugClient = dc->getParentModule()->getDebugClient()) {
19431982
debugClient->finishLookupInAnyObject(dc, member.getFullName(), options,
19441983
decls);

test/NameLookup/module_selector.swift

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ extension main::B: main::Equatable {
2121
// @_derivative(of:)
2222

2323
@_dynamicReplacement(for: main::negate())
24+
// FIXME improve: expected-error@-1 {{replaced function 'main::negate()' could not be found}}
2425
mutating func myNegate() {
2526
let fn: (main::Int, main::Int) -> main::Int =
2627
// FIXME:
@@ -30,11 +31,12 @@ extension main::B: main::Equatable {
3031
// expected-error@-3 {{expected expression after operator}}
3132

3233
let magnitude: main::Int.main::Magnitude = main::magnitude
33-
// expected-EVENTUALLY-error@-1 {{a type mismatch with 'Never'}}
34-
// FIXME: expected-error@-2 {{variable used within its own initial value}}
34+
// expected-EVENTUALLY-error@-1 {{can't find 'Int'}}
35+
// FIXME improve: expected-error@-2 {{type alias 'Magnitude' is not a member type of 'Int'}}
36+
// FIXME: expected-error@-3 {{variable used within its own initial value}}
3537
if main::Bool.main::random() {
3638
main::negate()
37-
// expected-EVENTUALLY-error@-1 {{something about not finding 'negate' because we didn't look in self}}
39+
// FIXME improve: expected-error@-1 {{use of unresolved identifier 'main::negate'}}
3840
}
3941
else {
4042
self = main::B(value: .main::min)
@@ -66,7 +68,8 @@ extension ModuleSelectorTestingKit::C: ModuleSelectorTestingKit::Equatable {
6668
// expected-error@-3 {{expected expression after operator}}
6769
let magnitude: ModuleSelectorTestingKit::Int.ModuleSelectorTestingKit::Magnitude = ModuleSelectorTestingKit::magnitude
6870
// expected-EVENTUALLY-error@-1 {{something about not finding 'magnitude' because we didn't look in self}}
69-
// FIXME: expected-error@-2 {{variable used within its own initial value}}
71+
// FIXME improve: expected-error@-2 {{type alias 'Magnitude' is not a member type of 'Int'}}
72+
// FIXME: expected-error@-3 {{variable used within its own initial value}}
7073
if ModuleSelectorTestingKit::Bool.ModuleSelectorTestingKit::random() {
7174
ModuleSelectorTestingKit::negate()
7275
// expected-EVENTUALLY-error@-1 {{something about not finding 'negate' because we didn't look in self}}
@@ -94,6 +97,7 @@ extension Swift::D: Swift::Equatable {
9497
// @_derivative(of:)
9598

9699
@_dynamicReplacement(for: Swift::negate())
100+
// FIXME improve: expected-error@-1 {{replaced function 'Swift::negate()' could not be found}}
97101
mutating func myNegate() {
98102
let fn: (Swift::Int, Swift::Int) -> Swift::Int =
99103
// FIXME:
@@ -106,7 +110,7 @@ extension Swift::D: Swift::Equatable {
106110
// FIXME: expected-error@-2 {{variable used within its own initial value}}
107111
if Swift::Bool.Swift::random() {
108112
Swift::negate()
109-
// expected-EVENTUALLY-error@-1 {{something about not finding 'negate' because we didn't look in self}}
113+
// FIXME improve: expected-error@-1 {{use of unresolved identifier 'Swift::negate'}}
110114
}
111115
else {
112116
self = Swift::D(value: .ModuleSelectorTestingKit::min)

0 commit comments

Comments
 (0)