Skip to content

Commit 2869b33

Browse files
committed
Handle module selectors in unqualified lookup
1 parent d30b46e commit 2869b33

File tree

2 files changed

+55
-7
lines changed

2 files changed

+55
-7
lines changed

lib/AST/UnqualifiedLookup.cpp

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

474474
void UnqualifiedLookupFactory::addImportedResults(const DeclContext *const dc) {
475+
ASSERT(dc && "unqualified lookup in null 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;
@@ -487,7 +500,7 @@ void UnqualifiedLookupFactory::addImportedResults(const DeclContext *const dc) {
487500
if (options.contains(Flags::IgnoreAccessControl))
488501
nlOptions |= NL_IgnoreAccessControl;
489502

490-
lookupInModule(dc, Name.getFullName(), CurModuleResults,
503+
lookupInModule(moduleToLookIn, Name.getFullName(), CurModuleResults,
491504
NLKind::UnqualifiedLookup, resolutionKind, dc,
492505
Loc, nlOptions);
493506

test/NameLookup/module_selector.swift

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,20 @@ extension A: @retroactive Swift::Equatable {
6666
// Test resolution of main:: using `B`
6767

6868
extension main::B {}
69+
// FIXME improve: expected-error@-1 {{cannot find type 'main::B' in scope}}
6970

7071
extension B: @retroactive main::Equatable {
72+
// FIXME improve: expected-error@-1 {{cannot find type 'main::Equatable' in scope}}
7173

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

7477
public static func equals(_: main::B, _: main::B) -> main::Bool {
78+
// FIXME improve: expected-error@-1 {{cannot find type 'main::B' in scope}}
79+
// FIXME improve: expected-error@-2 {{cannot find type 'main::B' in scope}}
80+
// FIXME improve: expected-error@-3 {{cannot find type 'main::Bool' in scope}}
7581
main::fatalError()
82+
// FIXME improve: expected-error@-1 {{cannot find 'main::fatalError' in scope}}
7683
}
7784

7885
// FIXME: Add tests with autodiff @_differentiable(jvp:vjp:) and
@@ -83,27 +90,33 @@ extension B: @retroactive main::Equatable {
8390

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

8897
let magnitude: Int.main::Magnitude = main::magnitude
8998
// FIXME improve: expected-error@-1 {{'main::Magnitude' is not a member type of struct 'Swift.Int'}}
9099

91100
_ = (fn, magnitude)
92101

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

95105
main::negate()
96106
// FIXME improve: expected-error@-1 {{cannot find 'main::negate' in scope}}
97107
}
98108
else {
99109
self = main::B(value: .main::min)
110+
// FIXME improve: expected-error@-1 {{cannot find 'main::B' in scope}}
111+
// expected-error@-2 {{cannot infer contextual base in reference to member 'main::min'}}
100112

101113
self = B.main::init(value: .min)
102114
}
103115

104116
self.main::myNegate()
105117

106118
main::fatalError()
119+
// FIXME improve: expected-error@-1 {{cannot find 'main::fatalError' in scope}}
107120

108121
_ = \main::A.magnitude
109122
_ = \A.main::magnitude
@@ -117,11 +130,16 @@ extension B: @retroactive main::Equatable {
117130
extension ModuleSelectorTestingKit::C {}
118131

119132
extension C: @retroactive ModuleSelectorTestingKit::Equatable {
133+
// FIXME improve: expected-error@-1 {{cannot find type 'ModuleSelectorTestingKit::Equatable' in scope}}
120134

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

123138
public static func equals(_: ModuleSelectorTestingKit::C, _: ModuleSelectorTestingKit::C) -> ModuleSelectorTestingKit::Bool {
139+
// FIXME improve: expected-error@-1 {{cannot find type 'ModuleSelectorTestingKit::Bool' in scope}}
140+
124141
ModuleSelectorTestingKit::fatalError()
142+
// FIXME improve: expected-error@-1 {{cannot find 'ModuleSelectorTestingKit::fatalError' in scope}}
125143
}
126144

127145
// FIXME: Add tests with autodiff @_differentiable(jvp:vjp:) and
@@ -131,14 +149,18 @@ extension C: @retroactive ModuleSelectorTestingKit::Equatable {
131149

132150
mutating func myNegate() {
133151
let fn: (ModuleSelectorTestingKit::Int, ModuleSelectorTestingKit::Int) -> ModuleSelectorTestingKit::Int =
152+
// FIXME improve: expected-error@-1 3{{cannot find type 'ModuleSelectorTestingKit::Int' in scope}}
134153
(ModuleSelectorTestingKit::+)
154+
// FIXME improve: expected-error@-1 {{cannot find operator 'ModuleSelectorTestingKit::+' in scope}}
135155

136156
let magnitude: Int.ModuleSelectorTestingKit::Magnitude = ModuleSelectorTestingKit::magnitude
137157
// FIXME improve: expected-error@-1 {{'ModuleSelectorTestingKit::Magnitude' is not a member type of struct 'Swift.Int'}}
158+
// FIXME improve: expected-error@-2 {{cannot find 'ModuleSelectorTestingKit::magnitude' in scope}}
138159

139160
_ = (fn, magnitude)
140161

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

143165
ModuleSelectorTestingKit::negate()
144166
// expected-error@-1 {{cannot find 'ModuleSelectorTestingKit::negate' in scope}}
@@ -152,6 +174,7 @@ extension C: @retroactive ModuleSelectorTestingKit::Equatable {
152174
self.ModuleSelectorTestingKit::myNegate()
153175

154176
ModuleSelectorTestingKit::fatalError()
177+
// FIXME improve: expected-error@-1 {{cannot find 'ModuleSelectorTestingKit::fatalError' in scope}}
155178

156179
_ = \ModuleSelectorTestingKit::A.magnitude
157180
_ = \A.ModuleSelectorTestingKit::magnitude
@@ -163,12 +186,16 @@ extension C: @retroactive ModuleSelectorTestingKit::Equatable {
163186
// Test resolution of Swift:: using `D`
164187

165188
extension Swift::D {}
189+
// FIXME improve: expected-error@-1 {{cannot find type 'Swift::D' in scope}}
166190

167191
extension D: @retroactive Swift::Equatable {
192+
// 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}}
168193

169194
@_implements(Swift::Equatable, ==(_:_:))
170195
public static func equals(_: Swift::D, _: Swift::D) -> Swift::Bool {
171-
Swift::fatalError()
196+
// expected-error@-1 {{cannot find type 'Swift::D' in scope}}
197+
// expected-error@-2 {{cannot find type 'Swift::D' in scope}}
198+
Swift::fatalError() // no-error -- not typechecking function bodies
172199
}
173200

174201
// FIXME: Add tests with autodiff @_differentiable(jvp:vjp:) and
@@ -183,7 +210,7 @@ extension D: @retroactive Swift::Equatable {
183210
(Swift::+)
184211

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

188215
_ = (fn, magnitude)
189216

@@ -194,6 +221,8 @@ extension D: @retroactive Swift::Equatable {
194221
}
195222
else {
196223
self = Swift::D(value: .Swift::min)
224+
// FIXME improve: expected-error@-1 {{cannot find 'Swift::D' in scope}}
225+
// expected-error@-2 {{cannot infer contextual base in reference to member 'Swift::min'}}
197226

198227
self = D.Swift::init(value: .min)
199228
}
@@ -210,7 +239,6 @@ extension D: @retroactive Swift::Equatable {
210239
}
211240

212241
let mog: Never = fatalError()
213-
// expected-note@-1 {{did you mean 'mog'?}}
214242

215243
func localVarsCantBeAccessedByModuleSelector() {
216244
let mag: Int.Swift::Magnitude = main::mag
@@ -223,55 +251,62 @@ struct AvailableUser {
223251
@available(macOS 10.15, *) var use1: String { "foo" }
224252

225253
@main::available() var use2
254+
// FIXME improve: expected-error@-1 {{unknown attribute 'available'}}
255+
// FIXME suppress: expected-error@-2 {{type annotation missing in pattern}}
226256

227257
@ModuleSelectorTestingKit::available() var use4
228258
// no-error
229259

230260
@Swift::available() var use5
261+
// FIXME improve: expected-error@-1 {{unknown attribute 'available'}}
262+
// FIXME suppress: expected-error@-2 {{type annotation missing in pattern}}
231263
}
232264

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

235268
func builderUser3(@ModuleSelectorTestingKit::MyBuilder fn: () -> Void) {}
269+
// no-error
236270

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

239274
// Error cases
240275

241276
func decl1(
242277
p1: main::A,
278+
// FIXME: expected-error@-1 {{cannot find type 'main::A' in scope}}
243279
label p2: inout A,
244280
label p3: @escaping () -> A
245281
) {
246282
switch Optional(main::p2) {
247283
case Optional.some(let decl1i):
248-
// expected-warning@-1 {{immutable value 'decl1i' was never used; consider replacing with '_' or removing it}}
249284
break
250285
case .none:
251286
break
252287
}
253288

254289
switch Optional(main::p2) {
255290
case let Optional.some(decl1j):
256-
// expected-warning@-1 {{immutable value 'decl1j' was never used; consider replacing with '_' or removing it}}
257291
break
258292
case .none:
259293
break
260294
}
261295

262296
switch Optional(main::p2) {
263297
case let decl1k?:
264-
// expected-warning@-1 {{immutable value 'decl1k' was never used; consider replacing with '_' or removing it}}
265298
break
266299
case .none:
267300
break
268301
}
269302
}
270303

271304
typealias decl5 = main::Bool
305+
// FIXME improve: expected-error@-1 {{cannot find type 'main::Bool' in scope}}
272306

273307
func badModuleNames() {
274308
NonexistentModule::print()
309+
// expected-error@-1 {{cannot find 'NonexistentModule::print' in scope}}
275310

276311
_ = "foo".NonexistentModule::count
277312

0 commit comments

Comments
 (0)