Skip to content

Commit f43bd72

Browse files
committed
Handle module selectors in UDRE lookups
1 parent 2869b33 commit f43bd72

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

lib/Sema/TypeCheckNameLookup.cpp

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ namespace {
6060
class LookupResultBuilder {
6161
LookupResult &Result;
6262
DeclContext *DC;
63+
Identifier ModuleSelector;
6364
NameLookupOptions Options;
6465

6566
/// The vector of found declarations.
@@ -72,8 +73,9 @@ namespace {
7273

7374
public:
7475
LookupResultBuilder(LookupResult &result, DeclContext *dc,
75-
NameLookupOptions options)
76-
: Result(result), DC(dc), Options(options) {
76+
Identifier moduleSelector, NameLookupOptions options)
77+
: Result(result), DC(dc), ModuleSelector(moduleSelector), Options(options)
78+
{
7779
if (dc->getASTContext().isAccessControlDisabled())
7880
Options |= NameLookupFlags::IgnoreAccessControl;
7981
}
@@ -83,6 +85,11 @@ namespace {
8385
removeOverriddenDecls(FoundDecls);
8486
removeOverriddenDecls(FoundOuterDecls);
8587

88+
// Remove any declarations excluded by the module selector from the
89+
// found-declarations set.
90+
removeOutOfModuleDecls(FoundDecls, ModuleSelector, DC);
91+
removeOutOfModuleDecls(FoundOuterDecls, ModuleSelector, DC);
92+
8693
// Remove any shadowed declarations from the found-declarations set.
8794
removeShadowedDecls(FoundDecls, DC);
8895
removeShadowedDecls(FoundOuterDecls, DC);
@@ -290,7 +297,10 @@ LookupResult TypeChecker::lookupUnqualified(DeclContext *dc, DeclNameRef name,
290297
UnqualifiedLookupRequest{descriptor}, {});
291298

292299
LookupResult result;
293-
LookupResultBuilder builder(result, dc, options);
300+
// Disable module selector filtering--UnqualifiedLookupRequest should have
301+
// done it.
302+
LookupResultBuilder builder(result, dc, /*moduleSelector=*/Identifier(),
303+
options);
294304
for (auto idx : indices(lookup.allResults())) {
295305
const auto &found = lookup[idx];
296306
// Determine which type we looked through to find this result.
@@ -381,7 +391,7 @@ LookupResult TypeChecker::lookupMember(DeclContext *dc,
381391
// Make sure we've resolved implicit members, if we need them.
382392
namelookup::installSemanticMembersIfNeeded(type, name);
383393

384-
LookupResultBuilder builder(result, dc, options);
394+
LookupResultBuilder builder(result, dc, name.getModuleSelector(), options);
385395
SmallVector<ValueDecl *, 4> lookupResults;
386396
dc->lookupQualified(type, name, loc, subOptions, lookupResults);
387397

test/NameLookup/module_selector.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@ extension B: @retroactive main::Equatable {
111111
// expected-error@-2 {{cannot infer contextual base in reference to member 'main::min'}}
112112

113113
self = B.main::init(value: .min)
114+
// FIXME improve: expected-error@-1 {{'B' cannot be constructed because it has no accessible initializers}}
115+
// expected-error@-2 {{cannot infer contextual base in reference to member 'min'}}
114116
}
115117

116118
self.main::myNegate()
@@ -119,7 +121,9 @@ extension B: @retroactive main::Equatable {
119121
// FIXME improve: expected-error@-1 {{cannot find 'main::fatalError' in scope}}
120122

121123
_ = \main::A.magnitude
124+
// FIXME improve: expected-error@-1 {{'main::A' in scope}} -- different diagnostic wording for legacy parser vs. ASTGen
122125
_ = \A.main::magnitude
126+
// FIXME improve: expected-error@-1 {{value of type 'A' has no member 'main::magnitude'}}
123127
}
124128

125129
// FIXME: Can we test @convention(witness_method:)?
@@ -167,11 +171,13 @@ extension C: @retroactive ModuleSelectorTestingKit::Equatable {
167171
}
168172
else {
169173
self = ModuleSelectorTestingKit::C(value: .ModuleSelectorTestingKit::min)
174+
// FIXME improve: expected-error@-1 {{type 'Int' has no member 'ModuleSelectorTestingKit::min'}}
170175

171176
self = C.ModuleSelectorTestingKit::init(value: .min)
172177
}
173178

174179
self.ModuleSelectorTestingKit::myNegate()
180+
// FIXME improve: expected-error@-1 {{value of type 'C' has no member 'ModuleSelectorTestingKit::myNegate'}}
175181

176182
ModuleSelectorTestingKit::fatalError()
177183
// FIXME improve: expected-error@-1 {{cannot find 'ModuleSelectorTestingKit::fatalError' in scope}}
@@ -225,14 +231,19 @@ extension D: @retroactive Swift::Equatable {
225231
// expected-error@-2 {{cannot infer contextual base in reference to member 'Swift::min'}}
226232

227233
self = D.Swift::init(value: .min)
234+
// FIXME improve: expected-error@-1 {{'D' cannot be constructed because it has no accessible initializers}}
235+
// expected-error@-2 {{cannot infer contextual base in reference to member 'min'}}
228236
}
229237

230238
self.Swift::myNegate()
239+
// FIXME improve: expected-error@-1 {{value of type 'D' has no member 'Swift::myNegate'}}
231240

232241
Swift::fatalError()
233242

234243
_ = \Swift::A.magnitude
244+
// FIXME improve: expected-error@-1 {{'Swift::A' in scope}} -- different diagnostic wording for legacy parser vs. ASTGen
235245
_ = \A.Swift::magnitude
246+
// FIXME improve: expected-error@-1 {{value of type 'A' has no member 'Swift::magnitude'}}
236247
}
237248

238249
// FIXME: Can we test @convention(witness_method:)?
@@ -309,6 +320,8 @@ func badModuleNames() {
309320
// expected-error@-1 {{cannot find 'NonexistentModule::print' in scope}}
310321

311322
_ = "foo".NonexistentModule::count
323+
// FIXME improve: expected-error@-1 {{value of type 'String' has no member 'NonexistentModule::count'}}
324+
// FIXME: expected-EVENTUALLY-note@-2 {{did you mean module 'Swift'?}} {{13-30=Swift}}
312325

313326
let x: NonexistentModule::MyType = NonexistentModule::MyType()
314327
// expected-error@-1 {{cannot find type 'NonexistentModule::MyType' in scope}}

0 commit comments

Comments
 (0)