Skip to content

Commit abe2805

Browse files
committed
Handle module selectors in qualified lookup
1 parent 0b5ca9a commit abe2805

File tree

3 files changed

+60
-4
lines changed

3 files changed

+60
-4
lines changed

include/swift/AST/NameLookup.h

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

489+
/// Remove any declarations in the given set that do not match the
490+
/// module selector, if it is not empty.
491+
///
492+
/// \returns true if any declarations were removed, false otherwise.
493+
bool removeOutOfModuleDecls(SmallVectorImpl<ValueDecl*> &decls,
494+
Identifier moduleSelector,
495+
const DeclContext *dc);
496+
489497
/// Remove any declarations in the given set that are shadowed by
490498
/// other declarations in that set.
491499
///
@@ -561,6 +569,7 @@ void tryExtractDirectlyReferencedNominalTypes(
561569
/// Once name lookup has gathered a set of results, perform any necessary
562570
/// steps to prune the result set before returning it to the caller.
563571
void pruneLookupResultSet(const DeclContext *dc, NLOptions options,
572+
Identifier moduleSelector,
564573
SmallVectorImpl<ValueDecl *> &decls);
565574

566575
/// Do nothing if debugClient is null.

lib/AST/NameLookup.cpp

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,42 @@ enum class ConstructorComparison {
361361
Better,
362362
};
363363

364+
bool swift::removeOutOfModuleDecls(SmallVectorImpl<ValueDecl*> &decls,
365+
Identifier moduleSelector,
366+
const DeclContext *dc) {
367+
if (moduleSelector.empty())
368+
return false;
369+
370+
ASTContext &ctx = dc->getASTContext();
371+
372+
// FIXME: Should we look this up relative to dc?
373+
// We'd need a new ResolutionKind.
374+
// FIXME: How can we diagnose this?
375+
ModuleDecl *visibleFrom = ctx.getLoadedModule(moduleSelector);
376+
if (!visibleFrom) {
377+
LLVM_DEBUG(llvm::dbgs() << "no module " << moduleSelector << "\n");
378+
decls.clear();
379+
return true;
380+
}
381+
382+
bool initialCount = decls.size();
383+
decls.erase(
384+
std::remove_if(decls.begin(), decls.end(), [&](ValueDecl *decl) -> bool {
385+
bool inScope = ctx.getImportCache().isImportedBy(decl->getModuleContext(),
386+
visibleFrom);
387+
388+
LLVM_DEBUG(decl->dumpRef(llvm::dbgs()));
389+
LLVM_DEBUG(llvm::dbgs() << ": " << decl->getModuleContext()->getName()
390+
<< (inScope ? " is " : " is NOT ")
391+
<< "selected by " << visibleFrom->getName()
392+
<< "\n");
393+
394+
return !inScope;
395+
}),
396+
decls.end());
397+
return initialCount != decls.size();
398+
}
399+
364400
/// Determines whether \p ctor1 is a "better" initializer than \p ctor2.
365401
static ConstructorComparison compareConstructors(ConstructorDecl *ctor1,
366402
ConstructorDecl *ctor2,
@@ -2466,14 +2502,17 @@ bool namelookup::isInABIAttr(SourceFile *sourceFile, SourceLoc loc) {
24662502
}
24672503

24682504
void namelookup::pruneLookupResultSet(const DeclContext *dc, NLOptions options,
2505+
Identifier moduleSelector,
24692506
SmallVectorImpl<ValueDecl *> &decls) {
24702507
// If we're supposed to remove overridden declarations, do so now.
24712508
if (options & NL_RemoveOverridden)
24722509
removeOverriddenDecls(decls);
24732510

24742511
// If we're supposed to remove shadowed/hidden declarations, do so now.
2475-
if (options & NL_RemoveNonVisible)
2512+
if (options & NL_RemoveNonVisible) {
2513+
removeOutOfModuleDecls(decls, moduleSelector, dc);
24762514
removeShadowedDecls(decls, dc);
2515+
}
24772516

24782517
ModuleDecl *M = dc->getParentModule();
24792518
filterForDiscriminator(decls, M->getDebugClient());
@@ -2785,7 +2824,7 @@ QualifiedLookupRequest::evaluate(Evaluator &eval, const DeclContext *DC,
27852824
}
27862825
}
27872826

2788-
pruneLookupResultSet(DC, options, decls);
2827+
pruneLookupResultSet(DC, options, member.getModuleSelector(), decls);
27892828
if (auto *debugClient = DC->getParentModule()->getDebugClient()) {
27902829
debugClient->finishLookupInNominals(DC, typeDecls, member.getFullName(),
27912830
options, decls);
@@ -2837,7 +2876,7 @@ ModuleQualifiedLookupRequest::evaluate(Evaluator &eval, const DeclContext *DC,
28372876
}
28382877
}
28392878

2840-
pruneLookupResultSet(DC, options, decls);
2879+
pruneLookupResultSet(DC, options, member.getModuleSelector(), decls);
28412880

28422881
if (auto *debugClient = DC->getParentModule()->getDebugClient()) {
28432882
debugClient->finishLookupInModule(DC, module, member.getFullName(),
@@ -2905,7 +2944,7 @@ AnyObjectLookupRequest::evaluate(Evaluator &evaluator, const DeclContext *dc,
29052944
decls.push_back(decl);
29062945
}
29072946

2908-
pruneLookupResultSet(dc, options, decls);
2947+
pruneLookupResultSet(dc, options, member.getModuleSelector(), decls);
29092948
if (auto *debugClient = dc->getParentModule()->getDebugClient()) {
29102949
debugClient->finishLookupInAnyObject(dc, member.getFullName(), options,
29112950
decls);

test/NameLookup/module_selector.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ extension A: @retroactive Swift::Equatable {
4343
let fn: (Swift::Int, Swift::Int) -> Swift::Int = (Swift::+)
4444

4545
let magnitude: Int.Swift::Magnitude = main::magnitude
46+
// expected-error@-1 {{cannot convert value of type 'Never' to specified type 'Int.Magnitude' (aka 'UInt')}}
4647

4748
_ = (fn, magnitude)
4849

@@ -79,18 +80,21 @@ extension B: @retroactive main::Equatable {
7980
// @_derivative(of:)
8081

8182
@_dynamicReplacement(for: main::negate())
83+
// FIXME improve: expected-error@-1 {{replaced function 'main::negate()' could not be found}}
8284

8385
mutating func myNegate() {
8486
let fn: (main::Int, main::Int) -> main::Int =
8587
(main::+)
8688

8789
let magnitude: Int.main::Magnitude = main::magnitude
90+
// FIXME improve: expected-error@-1 {{'main::Magnitude' is not a member type of struct 'Swift.Int'}}
8891

8992
_ = (fn, magnitude)
9093

9194
if main::Bool.main::random() {
9295

9396
main::negate()
97+
// FIXME improve: expected-error@-1 {{cannot find 'main::negate' in scope}}
9498
}
9599
else {
96100
self = main::B(value: .main::min)
@@ -129,6 +133,7 @@ extension C: @retroactive ModuleSelectorTestingKit::Equatable {
129133
(ModuleSelectorTestingKit::+)
130134

131135
let magnitude: Int.ModuleSelectorTestingKit::Magnitude = ModuleSelectorTestingKit::magnitude
136+
// FIXME improve: expected-error@-1 {{'ModuleSelectorTestingKit::Magnitude' is not a member type of struct 'Swift.Int'}}
132137

133138
_ = (fn, magnitude)
134139

@@ -167,19 +172,22 @@ extension D: @retroactive Swift::Equatable {
167172
// @_derivative(of:)
168173

169174
@_dynamicReplacement(for: Swift::negate())
175+
// FIXME improve: expected-error@-1 {{replaced function 'Swift::negate()' could not be found}}
170176

171177
mutating func myNegate() {
172178

173179
let fn: (Swift::Int, Swift::Int) -> Swift::Int =
174180
(Swift::+)
175181

176182
let magnitude: Int.Swift::Magnitude = Swift::magnitude
183+
// expected-error@-1 {{cannot convert value of type 'Never' to specified type 'Int.Magnitude' (aka 'UInt')}}
177184

178185
_ = (fn, magnitude)
179186

180187
if Swift::Bool.Swift::random() {
181188

182189
Swift::negate()
190+
// FIXME improve: expected-error@-1 {{cannot find 'Swift::negate' in scope}}
183191
}
184192
else {
185193
self = Swift::D(value: .Swift::min)

0 commit comments

Comments
 (0)