Skip to content

Commit 646a317

Browse files
committed
Support module selectors in unqualified lookup
# Conflicts: # lib/AST/UnqualifiedLookup.cpp
1 parent 52d1805 commit 646a317

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
@@ -385,10 +385,17 @@ void UnqualifiedLookupFactory::addImportedResults(const DeclContext *const dc) {
385385
SmallVector<ValueDecl *, 8> CurModuleResults;
386386
auto resolutionKind = isOriginallyTypeLookup ? ResolutionKind::TypesOnly
387387
: ResolutionKind::Overloadable;
388+
auto moduleToLookIn = dc;
389+
if (Name.hasModuleSelector())
390+
// FIXME: Should we look this up relative to dc?
391+
// We'd need a new ResolutionKind.
392+
moduleToLookIn =
393+
dc->getASTContext().getLoadedModule(Name.getModuleSelector());
394+
388395
auto nlOptions = NL_UnqualifiedDefault;
389396
if (options.contains(Flags::IncludeUsableFromInline))
390397
nlOptions |= NL_IncludeUsableFromInline;
391-
lookupInModule(dc, Name.getFullName(), CurModuleResults,
398+
lookupInModule(moduleToLookIn, Name.getFullName(), CurModuleResults,
392399
NLKind::UnqualifiedLookup, resolutionKind, dc, nlOptions);
393400

394401
// Always perform name shadowing for type lookup.

test/NameLookup/module_selector.swift

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

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

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

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

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

59+
extension C {}
60+
5661
extension ModuleSelectorTestingKit::C: ModuleSelectorTestingKit::Equatable {
62+
// FIXME improve: expected-error@-1 {{use of undeclared type 'ModuleSelectorTestingKit::Equatable'}}
5763
@_implements(ModuleSelectorTestingKit::Equatable, ModuleSelectorTestingKit::==(_:_:))
58-
// expected-error@-1 {{name cannot be qualified with module selector here}} {{52-77=}}
64+
// FIXME improve: expected-error@-1 {{use of undeclared type 'ModuleSelectorTestingKit::Equatable'}}
65+
// expected-error@-2 {{name cannot be qualified with module selector here}} {{52-77=}}
5966
public static func equals(_: ModuleSelectorTestingKit::C, _: ModuleSelectorTestingKit::C) -> ModuleSelectorTestingKit::Bool { ModuleSelectorTestingKit::fatalError() }
60-
67+
// FIXME improve: expected-error@-1 {{use of undeclared type 'ModuleSelectorTestingKit::Bool'}}
68+
6169
// FIXME: Add tests with autodiff @_differentiable(jvp:vjp:) and
6270
// @_derivative(of:)
6371

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

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

132144
@main::available() var use2
145+
// FIXME improve: expected-error@-1 {{unknown attribute 'available'}}
146+
// FIXME suppress: expected-error@-2 {{type annotation missing in pattern}}
147+
133148
@ModuleSelectorTestingKit::available() var use4
149+
// no-error
150+
134151
@Swift::available() var use5
135-
// expected-EVENTUALLY-error@-1 {{can't find because not in 'Swift'}}
152+
// FIXME improve: expected-error@-1 {{unknown attribute 'available'}}
153+
// FIXME suppress: expected-error@-2 {{type annotation missing in pattern}}
136154
}
137155

138156
func builderUser2(@main::MyBuilder fn: () -> Void) {}
157+
// FIXME improve: expected-error@-1 {{unknown attribute 'MyBuilder'}}
158+
139159
func builderUser3(@ModuleSelectorTestingKit::MyBuilder fn: () -> Void) {}
160+
// no-error
161+
140162
func builderUser4(@Swift::MyBuilder fn: () -> Void) {}
141-
// expected-EVENTUALLY-error@-1 {{can't find because not in 'Swift'}}
163+
// FIXME improve: expected-error@-1 {{unknown attribute 'MyBuilder'}}
142164

143165
func whitespace() {
144166
Swift::print
@@ -181,10 +203,13 @@ func main::decl1(
181203
// expected-error@-1 {{name of function declaration cannot be qualified with module selector}}
182204
main::p1: main::A,
183205
// expected-error@-1 {{argument label cannot be qualified with module selector}}
206+
// FIXME access path: expected-error@-2 {{use of undeclared type 'main::A'}}
184207
main::label p2: main::A,
185208
// expected-error@-1 {{argument label cannot be qualified with module selector}}
209+
// FIXME access path: expected-error@-2 {{use of undeclared type 'main::A'}}
186210
label main::p3: main::A
187211
// expected-error@-1 {{name of parameter declaration cannot be qualified with module selector}}
212+
// FIXME access path: expected-error@-2 {{use of undeclared type 'main::A'}}
188213
) {
189214
let main::decl1a = "a"
190215
// expected-error@-1 {{name of constant declaration cannot be qualified with module selector}}
@@ -268,6 +293,7 @@ class main::decl4<main::T> {}
268293

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

272298
protocol main::decl6 {
273299
// expected-error@-1 {{name of protocol declaration cannot be qualified with module selector}}
@@ -308,6 +334,7 @@ struct Parent {
308334

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

313340
@_swift_native_objc_runtime_base(main::BaseClass)

0 commit comments

Comments
 (0)