Skip to content

Commit 5fab496

Browse files
committed
Handle module selectors in unqualified lookup
1 parent 279b433 commit 5fab496

File tree

2 files changed

+53
-6
lines changed

2 files changed

+53
-6
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: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,14 +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, main::==(_:_:))
7375
// expected-error@-1 {{name of sibling declaration cannot be qualified with a module selector}} expected-note@-1 {{remove module selector from this name}} {{33-39=}}
76+
// FIXME improve: expected-error@-2 {{cannot find type 'main::Equatable' in scope}}
7477

7578
public static func equals(_: main::B, _: main::B) -> main::Bool {
76-
main::fatalError()
79+
// FIXME improve: expected-error@-1 {{cannot find type 'main::B' in scope}}
80+
// FIXME improve: expected-error@-2 {{cannot find type 'main::B' in scope}}
81+
// FIXME improve: expected-error@-3 {{cannot find type 'main::Bool' in scope}}
82+
main::fatalError() // no-error -- not typechecking function bodies
7783
}
7884

7985
// FIXME: Add tests with autodiff @_differentiable(jvp:vjp:) and
@@ -84,6 +90,7 @@ extension B: @retroactive main::Equatable {
8490

8591
mutating func myNegate() {
8692
let fn: (main::Int, main::Int) -> main::Int =
93+
// FIXME improve: expected-error@-1 3{{cannot find type 'main::Int' in scope}}
8794
(main::+)
8895

8996
let magnitude: Int.main::Magnitude = main::magnitude
@@ -92,19 +99,23 @@ extension B: @retroactive main::Equatable {
9299
_ = (fn, magnitude)
93100

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

96104
main::negate()
97105
// FIXME improve: expected-error@-1 {{cannot find 'main::negate' in scope}}
98106
}
99107
else {
100108
self = main::B(value: .main::min)
109+
// FIXME improve: expected-error@-1 {{cannot find 'main::B' in scope}}
110+
// expected-error@-2 {{cannot infer contextual base in reference to member 'main::min'}}
101111

102112
self = B.main::init(value: .min)
103113
}
104114

105115
self.main::myNegate()
106116

107117
main::fatalError()
118+
// FIXME improve: expected-error@-1 {{cannot find 'main::fatalError' in scope}}
108119
}
109120

110121
// FIXME: Can we test @convention(witness_method:)?
@@ -115,12 +126,16 @@ extension B: @retroactive main::Equatable {
115126
extension ModuleSelectorTestingKit::C {}
116127

117128
extension C: @retroactive ModuleSelectorTestingKit::Equatable {
129+
// FIXME improve: expected-error@-1 {{cannot find type 'ModuleSelectorTestingKit::Equatable' in scope}}
118130

119131
@_implements(ModuleSelectorTestingKit::Equatable, ModuleSelectorTestingKit::==(_:_:))
120132
// expected-error@-1 {{name of sibling declaration cannot be qualified with a module selector}} expected-note@-1 {{remove module selector from this name}} {{53-79=}}
133+
// FIXME improve: expected-error@-2 {{cannot find type 'ModuleSelectorTestingKit::Equatable' in scope}}
121134

122135
public static func equals(_: ModuleSelectorTestingKit::C, _: ModuleSelectorTestingKit::C) -> ModuleSelectorTestingKit::Bool {
123-
ModuleSelectorTestingKit::fatalError()
136+
// FIXME improve: expected-error@-1 {{cannot find type 'ModuleSelectorTestingKit::Bool' in scope}}
137+
138+
ModuleSelectorTestingKit::fatalError() // no-error -- not typechecking function bodies
124139
}
125140

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

131146
mutating func myNegate() {
132147
let fn: (ModuleSelectorTestingKit::Int, ModuleSelectorTestingKit::Int) -> ModuleSelectorTestingKit::Int =
148+
// FIXME improve: expected-error@-1 3{{cannot find type 'ModuleSelectorTestingKit::Int' in scope}}
133149
(ModuleSelectorTestingKit::+)
134150

135151
let magnitude: Int.ModuleSelectorTestingKit::Magnitude = ModuleSelectorTestingKit::magnitude
@@ -138,6 +154,7 @@ extension C: @retroactive ModuleSelectorTestingKit::Equatable {
138154
_ = (fn, magnitude)
139155

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

142159
ModuleSelectorTestingKit::negate()
143160
// expected-error@-1 {{cannot find 'ModuleSelectorTestingKit::negate' in scope}}
@@ -151,6 +168,7 @@ extension C: @retroactive ModuleSelectorTestingKit::Equatable {
151168
self.ModuleSelectorTestingKit::myNegate()
152169

153170
ModuleSelectorTestingKit::fatalError()
171+
// FIXME improve: expected-error@-1 {{cannot find 'ModuleSelectorTestingKit::fatalError' in scope}}
154172
}
155173

156174
// FIXME: Can we test @convention(witness_method:)?
@@ -159,14 +177,18 @@ extension C: @retroactive ModuleSelectorTestingKit::Equatable {
159177
// Test resolution of Swift:: using `D`
160178

161179
extension Swift::D {}
180+
// FIXME improve: expected-error@-1 {{cannot find type 'Swift::D' in scope}}
162181

163182
extension D: @retroactive Swift::Equatable {
183+
// 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}}
164184

165185
@_implements(Swift::Equatable, Swift::==(_:_:))
166186
// expected-error@-1 {{name of sibling declaration cannot be qualified with a module selector}} expected-note@-1 {{remove module selector from this name}} {{34-41=}}
167187

168188
public static func equals(_: Swift::D, _: Swift::D) -> Swift::Bool {
169-
Swift::fatalError()
189+
// expected-error@-1 {{cannot find type 'Swift::D' in scope}}
190+
// expected-error@-2 {{cannot find type 'Swift::D' in scope}}
191+
Swift::fatalError() // no-error -- not typechecking function bodies
170192
}
171193

172194
// FIXME: Add tests with autodiff @_differentiable(jvp:vjp:) and
@@ -181,7 +203,7 @@ extension D: @retroactive Swift::Equatable {
181203
(Swift::+)
182204

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

186208
_ = (fn, magnitude)
187209

@@ -192,6 +214,8 @@ extension D: @retroactive Swift::Equatable {
192214
}
193215
else {
194216
self = Swift::D(value: .Swift::min)
217+
// FIXME improve: expected-error@-1 {{cannot find 'Swift::D' in scope}}
218+
// expected-error@-2 {{cannot infer contextual base in reference to member 'Swift::min'}}
195219

196220
self = D.Swift::init(value: .min)
197221
}
@@ -205,7 +229,6 @@ extension D: @retroactive Swift::Equatable {
205229
}
206230

207231
let mog: Never = fatalError()
208-
// expected-note@-1 {{did you mean 'mog'?}}
209232

210233
func localVarsCantBeAccessedByModuleSelector() {
211234
let mag: Int.Swift::Magnitude = main::mag
@@ -218,18 +241,25 @@ struct AvailableUser {
218241
@available(macOS 10.15, *) var use1: String { "foo" }
219242

220243
@main::available() var use2
244+
// FIXME improve: expected-error@-1 {{unknown attribute 'available'}}
245+
// FIXME suppress: expected-error@-2 {{type annotation missing in pattern}}
221246

222247
@ModuleSelectorTestingKit::available() var use4
223248
// no-error
224249

225250
@Swift::available() var use5
251+
// FIXME improve: expected-error@-1 {{unknown attribute 'available'}}
252+
// FIXME suppress: expected-error@-2 {{type annotation missing in pattern}}
226253
}
227254

228255
func builderUser2(@main::MyBuilder fn: () -> Void) {}
256+
// FIXME improve: expected-error@-1 {{unknown attribute 'MyBuilder'}}
229257
230258
func builderUser3(@ModuleSelectorTestingKit::MyBuilder fn: () -> Void) {}
259+
// no-error
231260
232261
func builderUser4(@Swift::MyBuilder fn: () -> Void) {}
262+
// FIXME improve: expected-error@-1 {{unknown attribute 'MyBuilder'}}
233263
234264
func whitespace() {
235265
_ = Swift::print
@@ -260,6 +290,7 @@ func main::decl1(
260290
// expected-error@-1 {{name in function declaration cannot be qualified with a module selector}} expected-note@-1 {{remove module selector from this name}} {{6-12=}}
261291
main::p1: main::A,
262292
// expected-error@-1 {{argument label cannot be qualified with a module selector}} expected-note@-1 {{remove module selector from this name}} {{3-9=}}
293+
// FIXME: expected-error@-2 {{cannot find type 'main::A' in scope}}
263294
main::label p2: main::inout A,
264295
// expected-error@-1 {{argument label cannot be qualified with a module selector}} expected-note@-1 {{remove module selector from this name}} {{3-9=}}
265296
// FIXME: expected-error@-2 {{expected identifier in dotted type}} should be something like {{type 'inout' is not imported through module 'main'}}
@@ -346,6 +377,7 @@ class main::decl4<main::T> {}
346377

347378
typealias main::decl5 = main::Bool
348379
// expected-error@-1 {{name in typealias declaration cannot be qualified with a module selector}} expected-note@-1 {{remove module selector from this name}} {{11-17=}}
380+
// FIXME improve: expected-error@-2 {{cannot find type 'main::Bool' in scope}}
349381

350382
protocol main::decl6 {
351383
// expected-error@-1 {{name in protocol declaration cannot be qualified with a module selector}} expected-note@-1 {{remove module selector from this name}} {{10-16=}}
@@ -386,6 +418,7 @@ struct Parent {
386418

387419
typealias main::decl5 = main::Bool
388420
// expected-error@-1 {{name in typealias declaration cannot be qualified with a module selector}} expected-note@-1 {{remove module selector from this name}} {{13-19=}}
421+
// FIXME improve: expected-error@-2 {{cannot find type 'main::Bool' in scope}}
389422
}
390423

391424
@_swift_native_objc_runtime_base(main::BaseClass)
@@ -404,6 +437,7 @@ precedencegroup main::PG1 {
404437

405438
func badModuleNames() {
406439
NonexistentModule::print()
440+
// expected-error@-1 {{cannot find 'NonexistentModule::print' in scope}}
407441

408442
_ = "foo".NonexistentModule::count
409443

0 commit comments

Comments
 (0)