Skip to content

Commit bc0f92f

Browse files
committed
Support module selectors in unqualified lookup
1 parent e7a0997 commit bc0f92f

File tree

2 files changed

+47
-13
lines changed

2 files changed

+47
-13
lines changed

lib/AST/UnqualifiedLookup.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -477,14 +477,21 @@ void UnqualifiedLookupFactory::addImportedResults(const DeclContext *const dc) {
477477
auto resolutionKind = isOriginallyTypeLookup ? ResolutionKind::TypesOnly
478478
: isOriginallyMacroLookup ? ResolutionKind::MacrosOnly
479479
: ResolutionKind::Overloadable;
480+
auto moduleToLookIn = dc;
481+
if (Name.hasModuleSelector())
482+
// FIXME: Should we look this up relative to dc?
483+
// We'd need a new ResolutionKind.
484+
moduleToLookIn =
485+
dc->getASTContext().getLoadedModule(Name.getModuleSelector());
486+
480487
auto nlOptions = NL_UnqualifiedDefault;
481488
if (options.contains(Flags::IncludeUsableFromInline))
482489
nlOptions |= NL_IncludeUsableFromInline;
483490
if (options.contains(Flags::ExcludeMacroExpansions))
484491
nlOptions |= NL_ExcludeMacroExpansions;
485492
if (options.contains(Flags::ABIProviding))
486493
nlOptions |= NL_ABIProviding;
487-
lookupInModule(dc, Name.getFullName(), CurModuleResults,
494+
lookupInModule(moduleToLookIn, Name.getFullName(), CurModuleResults,
488495
NLKind::UnqualifiedLookup, resolutionKind, dc,
489496
Loc, nlOptions);
490497

test/NameLookup/module_selector.swift

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@ let magnitude: Never = fatalError()
1414

1515
// Test resolution of main:: using `B`
1616

17-
extension main::B: main::Equatable {
17+
extension main::B {}
18+
// FIXME improve: expected-error@-1 {{use of undeclared type 'main::B'}}
19+
20+
extension B: main::Equatable {
1821
@_implements(main::Equatable, main::==(_:_:))
1922
// expected-error@-1 {{name cannot be qualified with module selector here}} {{33-39=}}
2023
public static func equals(_: main::B, _: main::B) -> main::Bool { main::fatalError() }
@@ -32,7 +35,7 @@ extension main::B: main::Equatable {
3235
// expected-error@-2 {{expected expression}}
3336
// expected-error@-3 {{expected expression after operator}}
3437

35-
let magnitude: main::Int.main::Magnitude = main::magnitude
38+
let magnitude: Int.main::Magnitude = main::magnitude
3639
// expected-EVENTUALLY-error@-1 {{can't find 'Int'}}
3740
// FIXME improve: expected-error@-2 {{type alias 'Magnitude' is not a member type of 'Int'}}
3841
// FIXME: expected-error@-3 {{variable used within its own initial value}}
@@ -52,27 +55,33 @@ extension main::B: main::Equatable {
5255

5356
// Test resolution of ModuleSelectorTestingKit:: using `C`
5457

58+
extension C {}
59+
5560
extension ModuleSelectorTestingKit::C: ModuleSelectorTestingKit::Equatable {
61+
// FIXME improve: expected-error@-1 {{use of undeclared type 'ModuleSelectorTestingKit::Equatable'}}
5662
@_implements(ModuleSelectorTestingKit::Equatable, ModuleSelectorTestingKit::==(_:_:))
57-
// expected-error@-1 {{name cannot be qualified with module selector here}} {{52-77=}}
63+
// FIXME improve: expected-error@-1 {{use of undeclared type 'ModuleSelectorTestingKit::Equatable'}}
64+
// expected-error@-2 {{name cannot be qualified with module selector here}} {{52-77=}}
5865
public static func equals(_: ModuleSelectorTestingKit::C, _: ModuleSelectorTestingKit::C) -> ModuleSelectorTestingKit::Bool { ModuleSelectorTestingKit::fatalError() }
59-
66+
// FIXME improve: expected-error@-1 {{use of undeclared type 'ModuleSelectorTestingKit::Bool'}}
67+
6068
// FIXME: Add tests with autodiff @_differentiable(jvp:vjp:) and
6169
// @_derivative(of:)
6270

6371
@_dynamicReplacement(for: ModuleSelectorTestingKit::negate())
6472
mutating func myNegate() {
6573
let fn: (ModuleSelectorTestingKit::Int, ModuleSelectorTestingKit::Int) -> ModuleSelectorTestingKit::Int =
74+
// FIXME improve: expected-error@-1 3{{use of undeclared type 'ModuleSelectorTestingKit::Int'}}
6675
// FIXME:
6776
(ModuleSelectorTestingKit::+)
68-
// expected-error@-1 {{cannot convert value of type '()' to specified type '(Int, Int) -> Int'}}
69-
// expected-error@-2 {{expected expression}}
70-
// expected-error@-3 {{expected expression after operator}}
71-
let magnitude: ModuleSelectorTestingKit::Int.ModuleSelectorTestingKit::Magnitude = ModuleSelectorTestingKit::magnitude
77+
// expected-error@-1 {{expected expression}}
78+
// expected-error@-2 {{expected expression after operator}}
79+
let magnitude: Int.ModuleSelectorTestingKit::Magnitude = ModuleSelectorTestingKit::magnitude
7280
// expected-EVENTUALLY-error@-1 {{something about not finding 'magnitude' because we didn't look in self}}
7381
// FIXME improve: expected-error@-2 {{type alias 'Magnitude' is not a member type of 'Int'}}
7482
// FIXME: expected-error@-3 {{variable used within its own initial value}}
7583
if ModuleSelectorTestingKit::Bool.ModuleSelectorTestingKit::random() {
84+
// FIXME improve: expected-error@-1 {{use of unresolved identifier 'ModuleSelectorTestingKit::Bool'}}
7685
ModuleSelectorTestingKit::negate()
7786
// FIXME improve, suggest adding 'self.': expected-error@-1 {{use of unresolved identifier 'ModuleSelectorTestingKit::negate'}}
7887
}
@@ -90,7 +99,10 @@ extension ModuleSelectorTestingKit::C: ModuleSelectorTestingKit::Equatable {
9099
// Test resolution of Swift:: using `D`
91100
// FIXME: Many more of these should fail once the feature is actually implemented.
92101

93-
extension Swift::D: @retroactive Swift::Equatable {
102+
extension Swift::D {}
103+
// FIXME improve: expected-error@-1 {{use of undeclared type 'Swift::D'}}
104+
105+
extension D: @retroactive Swift::Equatable {
94106
@_implements(Swift::Equatable, Swift::==(_:_:))
95107
// expected-error@-1 {{name cannot be qualified with module selector here}} {{34-41=}}
96108
public static func equals(_: Swift::D, _: Swift::D) -> Swift::Bool { Swift::fatalError() }
@@ -107,7 +119,7 @@ extension Swift::D: @retroactive Swift::Equatable {
107119
// expected-error@-1 {{cannot convert value of type '()' to specified type '(Int, Int) -> Int'}}
108120
// expected-error@-2 {{expected expression}}
109121
// expected-error@-3 {{expected expression after operator}}
110-
let magnitude: Swift::Int.Swift::Magnitude = Swift::magnitude
122+
let magnitude: Int.Swift::Magnitude = Swift::magnitude
111123
// expected-EVENTUALLY-error@-1 {{something about not finding 'magnitude' because we didn't look in self}}
112124
// FIXME: expected-error@-2 {{variable used within its own initial value}}
113125
if Swift::Bool.Swift::random() {
@@ -129,15 +141,25 @@ struct AvailableUser {
129141
@available(macOS 10.15, *) var use1: String { "foo" }
130142

131143
@main::available() var use2
144+
// FIXME improve: expected-error@-1 {{unknown attribute 'available'}}
145+
// FIXME suppress: expected-error@-2 {{type annotation missing in pattern}}
146+
132147
@ModuleSelectorTestingKit::available() var use4
148+
// no-error
149+
133150
@Swift::available() var use5
134-
// expected-EVENTUALLY-error@-1 {{can't find because not in 'Swift'}}
151+
// FIXME improve: expected-error@-1 {{unknown attribute 'available'}}
152+
// FIXME suppress: expected-error@-2 {{type annotation missing in pattern}}
135153
}
136154

137155
func builderUser2(@main::MyBuilder fn: () -> Void) {}
156+
// FIXME improve: expected-error@-1 {{unknown attribute 'MyBuilder'}}
157+
138158
func builderUser3(@ModuleSelectorTestingKit::MyBuilder fn: () -> Void) {}
159+
// no-error
160+
139161
func builderUser4(@Swift::MyBuilder fn: () -> Void) {}
140-
// expected-EVENTUALLY-error@-1 {{can't find because not in 'Swift'}}
162+
// FIXME improve: expected-error@-1 {{unknown attribute 'MyBuilder'}}
141163

142164
func whitespace() {
143165
Swift::print
@@ -180,10 +202,13 @@ func main::decl1(
180202
// expected-error@-1 {{name of function declaration cannot be qualified with module selector}}
181203
main::p1: main::A,
182204
// expected-error@-1 {{argument label cannot be qualified with module selector}}
205+
// FIXME access path: expected-error@-2 {{use of undeclared type 'main::A'}}
183206
main::label p2: main::A,
184207
// expected-error@-1 {{argument label cannot be qualified with module selector}}
208+
// FIXME access path: expected-error@-2 {{use of undeclared type 'main::A'}}
185209
label main::p3: main::A
186210
// expected-error@-1 {{name of parameter declaration cannot be qualified with module selector}}
211+
// FIXME access path: expected-error@-2 {{use of undeclared type 'main::A'}}
187212
) {
188213
let main::decl1a = "a"
189214
// expected-error@-1 {{name of constant declaration cannot be qualified with module selector}}
@@ -267,6 +292,7 @@ class main::decl4<main::T> {}
267292

268293
typealias main::decl5 = main::Bool
269294
// expected-error@-1 {{name of typealias declaration cannot be qualified with module selector}}
295+
// FIXME improve: expected-error@-2 {{use of undeclared type 'main::Bool'}}
270296

271297
protocol main::decl6 {
272298
// expected-error@-1 {{name of protocol declaration cannot be qualified with module selector}}
@@ -307,6 +333,7 @@ struct Parent {
307333

308334
typealias main::decl5 = main::Bool
309335
// expected-error@-1 {{name of typealias declaration cannot be qualified with module selector}}
336+
// FIXME improve: expected-error@-2 {{use of undeclared type 'main::Bool'}}
310337
}
311338

312339
@_swift_native_objc_runtime_base(main::BaseClass)

0 commit comments

Comments
 (0)