Skip to content

Commit 7cb3117

Browse files
committed
Handle module selectors in unqualified lookup
1 parent 53446b5 commit 7cb3117

File tree

2 files changed

+52
-9
lines changed

2 files changed

+52
-9
lines changed

lib/AST/UnqualifiedLookup.cpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -472,19 +472,32 @@ void UnqualifiedLookupFactory::setAsideUnavailableResults(
472472
}
473473

474474
void UnqualifiedLookupFactory::addImportedResults(const DeclContext *const dc) {
475+
assert(dc);
476+
475477
using namespace namelookup;
476478
SmallVector<ValueDecl *, 8> CurModuleResults;
477479
auto resolutionKind = isOriginallyTypeLookup ? ResolutionKind::TypesOnly
478480
: isOriginallyMacroLookup ? ResolutionKind::MacrosOnly
479481
: ResolutionKind::Overloadable;
482+
auto moduleToLookIn = dc;
483+
if (Name.hasModuleSelector())
484+
// FIXME: Should we look this up relative to dc?
485+
// We'd need a new ResolutionKind.
486+
moduleToLookIn =
487+
dc->getASTContext().getLoadedModule(Name.getModuleSelector());
488+
489+
// If we didn't find the module, it obviously can't have any results.
490+
if (!moduleToLookIn)
491+
return;
492+
480493
auto nlOptions = NL_UnqualifiedDefault;
481494
if (options.contains(Flags::IncludeUsableFromInline))
482495
nlOptions |= NL_IncludeUsableFromInline;
483496
if (options.contains(Flags::ExcludeMacroExpansions))
484497
nlOptions |= NL_ExcludeMacroExpansions;
485498
if (options.contains(Flags::ABIProviding))
486499
nlOptions |= NL_ABIProviding;
487-
lookupInModule(dc, Name.getFullName(), CurModuleResults,
500+
lookupInModule(moduleToLookIn, Name.getFullName(), CurModuleResults,
488501
NLKind::UnqualifiedLookup, resolutionKind, dc,
489502
Loc, nlOptions);
490503

test/NameLookup/module_selector.swift

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,19 @@ extension A: @retroactive Swift::Equatable {
6464
// Test resolution of main:: using `B`
6565

6666
extension main::B {}
67+
// FIXME improve: expected-error@-1 {{cannot find type 'main::B' in scope}}
6768

6869
extension B: @retroactive main::Equatable {
70+
// FIXME improve: expected-error@-1 {{cannot find type 'main::Equatable' in scope}}
6971

7072
@_implements(main::Equatable, ==(_:_:))
73+
// FIXME improve: expected-error@-1 {{cannot find type 'main::Equatable' in scope}}
7174

7275
public static func equals(_: main::B, _: main::B) -> main::Bool {
73-
main::fatalError()
76+
// FIXME improve: expected-error@-1 {{cannot find type 'main::B' in scope}}
77+
// FIXME improve: expected-error@-2 {{cannot find type 'main::B' in scope}}
78+
// FIXME improve: expected-error@-3 {{cannot find type 'main::Bool' in scope}}
79+
main::fatalError() // no-error -- not typechecking function bodies
7480
}
7581

7682
// FIXME: Add tests with autodiff @_differentiable(jvp:vjp:) and
@@ -81,6 +87,7 @@ extension B: @retroactive main::Equatable {
8187

8288
mutating func myNegate() {
8389
let fn: (main::Int, main::Int) -> main::Int =
90+
// FIXME improve: expected-error@-1 3{{cannot find type 'main::Int' in scope}}
8491
(main::+)
8592

8693
let magnitude: Int.main::Magnitude = main::magnitude
@@ -89,19 +96,23 @@ extension B: @retroactive main::Equatable {
8996
_ = (fn, magnitude)
9097

9198
if main::Bool.main::random() {
99+
// FIXME improve: expected-error@-1 {{cannot find 'main::Bool' in scope}}
92100

93101
main::negate()
94102
// FIXME improve: expected-error@-1 {{cannot find 'main::negate' in scope}}
95103
}
96104
else {
97105
self = main::B(value: .main::min)
106+
// FIXME improve: expected-error@-1 {{cannot find 'main::B' in scope}}
107+
// expected-error@-2 {{cannot infer contextual base in reference to member 'main::min'}}
98108

99109
self = B.main::init(value: .min)
100110
}
101111

102112
self.main::myNegate()
103113

104114
main::fatalError()
115+
// FIXME improve: expected-error@-1 {{cannot find 'main::fatalError' in scope}}
105116
}
106117

107118
// FIXME: Can we test @convention(witness_method:)?
@@ -112,11 +123,15 @@ extension B: @retroactive main::Equatable {
112123
extension ModuleSelectorTestingKit::C {}
113124

114125
extension C: @retroactive ModuleSelectorTestingKit::Equatable {
126+
// FIXME improve: expected-error@-1 {{cannot find type 'ModuleSelectorTestingKit::Equatable' in scope}}
115127

116128
@_implements(ModuleSelectorTestingKit::Equatable, ==(_:_:))
129+
// FIXME improve: expected-error@-1 {{cannot find type 'ModuleSelectorTestingKit::Equatable' in scope}}
117130

118131
public static func equals(_: ModuleSelectorTestingKit::C, _: ModuleSelectorTestingKit::C) -> ModuleSelectorTestingKit::Bool {
119-
ModuleSelectorTestingKit::fatalError()
132+
// FIXME improve: expected-error@-1 {{cannot find type 'ModuleSelectorTestingKit::Bool' in scope}}
133+
134+
ModuleSelectorTestingKit::fatalError() // no-error -- not typechecking function bodies
120135
}
121136

122137
// FIXME: Add tests with autodiff @_differentiable(jvp:vjp:) and
@@ -126,6 +141,7 @@ extension C: @retroactive ModuleSelectorTestingKit::Equatable {
126141

127142
mutating func myNegate() {
128143
let fn: (ModuleSelectorTestingKit::Int, ModuleSelectorTestingKit::Int) -> ModuleSelectorTestingKit::Int =
144+
// FIXME improve: expected-error@-1 3{{cannot find type 'ModuleSelectorTestingKit::Int' in scope}}
129145
(ModuleSelectorTestingKit::+)
130146

131147
let magnitude: Int.ModuleSelectorTestingKit::Magnitude = ModuleSelectorTestingKit::magnitude
@@ -134,6 +150,7 @@ extension C: @retroactive ModuleSelectorTestingKit::Equatable {
134150
_ = (fn, magnitude)
135151

136152
if ModuleSelectorTestingKit::Bool.ModuleSelectorTestingKit::random() {
153+
// FIXME improve: expected-error@-1 {{cannot find 'ModuleSelectorTestingKit::Bool' in scope}}
137154

138155
ModuleSelectorTestingKit::negate()
139156
// expected-error@-1 {{cannot find 'ModuleSelectorTestingKit::negate' in scope}}
@@ -147,6 +164,7 @@ extension C: @retroactive ModuleSelectorTestingKit::Equatable {
147164
self.ModuleSelectorTestingKit::myNegate()
148165

149166
ModuleSelectorTestingKit::fatalError()
167+
// FIXME improve: expected-error@-1 {{cannot find 'ModuleSelectorTestingKit::fatalError' in scope}}
150168
}
151169

152170
// FIXME: Can we test @convention(witness_method:)?
@@ -155,12 +173,16 @@ extension C: @retroactive ModuleSelectorTestingKit::Equatable {
155173
// Test resolution of Swift:: using `D`
156174

157175
extension Swift::D {}
176+
// FIXME improve: expected-error@-1 {{cannot find type 'Swift::D' in scope}}
158177

159178
extension D: @retroactive Swift::Equatable {
179+
// Caused by Swift::D failing to typecheck in `equals(_:_:)`: expected-error@-1 *{{extension outside of file declaring struct 'D' prevents automatic synthesis of '==' for protocol 'Equatable'}} expected-note@-1 *{{add stubs for conformance}}
160180

161181
@_implements(Swift::Equatable, ==(_:_:))
162182
public static func equals(_: Swift::D, _: Swift::D) -> Swift::Bool {
163-
Swift::fatalError()
183+
// expected-error@-1 {{cannot find type 'Swift::D' in scope}}
184+
// expected-error@-2 {{cannot find type 'Swift::D' in scope}}
185+
Swift::fatalError() // no-error -- not typechecking function bodies
164186
}
165187

166188
// FIXME: Add tests with autodiff @_differentiable(jvp:vjp:) and
@@ -175,7 +197,7 @@ extension D: @retroactive Swift::Equatable {
175197
(Swift::+)
176198

177199
let magnitude: Int.Swift::Magnitude = Swift::magnitude
178-
// expected-error@-1 {{cannot convert value of type 'Never' to specified type 'Int.Magnitude' (aka 'UInt')}}
200+
// expected-error@-1 {{cannot find 'Swift::magnitude' in scope}}
179201

180202
_ = (fn, magnitude)
181203

@@ -186,6 +208,8 @@ extension D: @retroactive Swift::Equatable {
186208
}
187209
else {
188210
self = Swift::D(value: .Swift::min)
211+
// FIXME improve: expected-error@-1 {{cannot find 'Swift::D' in scope}}
212+
// expected-error@-2 {{cannot infer contextual base in reference to member 'Swift::min'}}
189213

190214
self = D.Swift::init(value: .min)
191215
}
@@ -199,7 +223,6 @@ extension D: @retroactive Swift::Equatable {
199223
}
200224

201225
let mog: Never = fatalError()
202-
// expected-note@-1 {{did you mean 'mog'?}}
203226

204227
func localVarsCantBeAccessedByModuleSelector() {
205228
let mag: Int.Swift::Magnitude = main::mag
@@ -212,55 +235,62 @@ struct AvailableUser {
212235
@available(macOS 10.15, *) var use1: String { "foo" }
213236

214237
@main::available() var use2
238+
// FIXME improve: expected-error@-1 {{unknown attribute 'available'}}
239+
// FIXME suppress: expected-error@-2 {{type annotation missing in pattern}}
215240

216241
@ModuleSelectorTestingKit::available() var use4
217242
// no-error
218243

219244
@Swift::available() var use5
245+
// FIXME improve: expected-error@-1 {{unknown attribute 'available'}}
246+
// FIXME suppress: expected-error@-2 {{type annotation missing in pattern}}
220247
}
221248

222249
func builderUser2(@main::MyBuilder fn: () -> Void) {}
250+
// FIXME improve: expected-error@-1 {{unknown attribute 'MyBuilder'}}
223251

224252
func builderUser3(@ModuleSelectorTestingKit::MyBuilder fn: () -> Void) {}
253+
// no-error
225254

226255
func builderUser4(@Swift::MyBuilder fn: () -> Void) {}
256+
// FIXME improve: expected-error@-1 {{unknown attribute 'MyBuilder'}}
227257

228258
// Error cases
229259

230260
func decl1(
231261
p1: main::A,
262+
// FIXME: expected-error@-1 {{cannot find type 'main::A' in scope}}
232263
label p2: inout A,
233264
label p3: @escaping () -> A
234265
) {
235266
switch Optional(main::p2) {
236267
case Optional.some(let decl1i):
237-
// expected-warning@-1 {{immutable value 'decl1i' was never used; consider replacing with '_' or removing it}}
238268
break
239269
case .none:
240270
break
241271
}
242272

243273
switch Optional(main::p2) {
244274
case let Optional.some(decl1j):
245-
// expected-warning@-1 {{immutable value 'decl1j' was never used; consider replacing with '_' or removing it}}
246275
break
247276
case .none:
248277
break
249278
}
250279

251280
switch Optional(main::p2) {
252281
case let decl1k?:
253-
// expected-warning@-1 {{immutable value 'decl1k' was never used; consider replacing with '_' or removing it}}
254282
break
255283
case .none:
256284
break
257285
}
258286
}
259287

260288
typealias decl5 = main::Bool
289+
// FIXME improve: expected-error@-1 {{cannot find type 'main::Bool' in scope}}
261290

262291
func badModuleNames() {
263292
NonexistentModule::print()
293+
// expected-error@-1 {{cannot find 'NonexistentModule::print' in scope}}
264294

265295
_ = "foo".NonexistentModule::count
266296

0 commit comments

Comments
 (0)