Skip to content

Commit d72afad

Browse files
committed
Handle module selectors in qualified lookup
1 parent caa328a commit d72afad

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,
@@ -2471,14 +2507,17 @@ bool namelookup::isInABIAttr(SourceFile *sourceFile, SourceLoc loc) {
24712507
}
24722508

24732509
void namelookup::pruneLookupResultSet(const DeclContext *dc, NLOptions options,
2510+
Identifier moduleSelector,
24742511
SmallVectorImpl<ValueDecl *> &decls) {
24752512
// If we're supposed to remove overridden declarations, do so now.
24762513
if (options & NL_RemoveOverridden)
24772514
removeOverriddenDecls(decls);
24782515

24792516
// If we're supposed to remove shadowed/hidden declarations, do so now.
2480-
if (options & NL_RemoveNonVisible)
2517+
if (options & NL_RemoveNonVisible) {
2518+
removeOutOfModuleDecls(decls, moduleSelector, dc);
24812519
removeShadowedDecls(decls, dc);
2520+
}
24822521

24832522
ModuleDecl *M = dc->getParentModule();
24842523
filterForDiscriminator(decls, M->getDebugClient());
@@ -2790,7 +2829,7 @@ QualifiedLookupRequest::evaluate(Evaluator &eval, const DeclContext *DC,
27902829
}
27912830
}
27922831

2793-
pruneLookupResultSet(DC, options, decls);
2832+
pruneLookupResultSet(DC, options, member.getModuleSelector(), decls);
27942833
if (auto *debugClient = DC->getParentModule()->getDebugClient()) {
27952834
debugClient->finishLookupInNominals(DC, typeDecls, member.getFullName(),
27962835
options, decls);
@@ -2842,7 +2881,7 @@ ModuleQualifiedLookupRequest::evaluate(Evaluator &eval, const DeclContext *DC,
28422881
}
28432882
}
28442883

2845-
pruneLookupResultSet(DC, options, decls);
2884+
pruneLookupResultSet(DC, options, member.getModuleSelector(), decls);
28462885

28472886
if (auto *debugClient = DC->getParentModule()->getDebugClient()) {
28482887
debugClient->finishLookupInModule(DC, module, member.getFullName(),
@@ -2910,7 +2949,7 @@ AnyObjectLookupRequest::evaluate(Evaluator &evaluator, const DeclContext *dc,
29102949
decls.push_back(decl);
29112950
}
29122951

2913-
pruneLookupResultSet(dc, options, decls);
2952+
pruneLookupResultSet(dc, options, member.getModuleSelector(), decls);
29142953
if (auto *debugClient = dc->getParentModule()->getDebugClient()) {
29152954
debugClient->finishLookupInAnyObject(dc, member.getFullName(), options,
29162955
decls);

test/NameLookup/module_selector.swift

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

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

4546
_ = (fn, magnitude)
4647

@@ -76,18 +77,21 @@ extension B: @retroactive main::Equatable {
7677
// @_derivative(of:)
7778

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

8082
mutating func myNegate() {
8183
let fn: (main::Int, main::Int) -> main::Int =
8284
(main::+)
8385

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

8689
_ = (fn, magnitude)
8790

8891
if main::Bool.main::random() {
8992

9093
main::negate()
94+
// FIXME improve: expected-error@-1 {{cannot find 'main::negate' in scope}}
9195
}
9296
else {
9397
self = main::B(value: .main::min)
@@ -125,6 +129,7 @@ extension C: @retroactive ModuleSelectorTestingKit::Equatable {
125129
(ModuleSelectorTestingKit::+)
126130

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

129134
_ = (fn, magnitude)
130135

@@ -161,19 +166,22 @@ extension D: @retroactive Swift::Equatable {
161166
// @_derivative(of:)
162167

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

165171
mutating func myNegate() {
166172

167173
let fn: (Swift::Int, Swift::Int) -> Swift::Int =
168174
(Swift::+)
169175

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

172179
_ = (fn, magnitude)
173180

174181
if Swift::Bool.Swift::random() {
175182

176183
Swift::negate()
184+
// FIXME improve: expected-error@-1 {{cannot find 'Swift::negate' in scope}}
177185
}
178186
else {
179187
self = Swift::D(value: .Swift::min)

0 commit comments

Comments
 (0)