Skip to content

Commit 82d21ef

Browse files
committed
Improve module selector type lookup diagnostics
1 parent 6bf9312 commit 82d21ef

File tree

3 files changed

+97
-19
lines changed

3 files changed

+97
-19
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1159,6 +1159,10 @@ ERROR(cannot_find_type_in_cast_expression,none,
11591159
"type-casting operator expects a type on its right-hand side (got: %kind0)", (const ValueDecl *))
11601160
ERROR(cannot_find_type_in_scope_did_you_mean,none,
11611161
"cannot find type %0 in scope; did you mean to use '%1'?", (DeclNameRef, StringRef))
1162+
ERROR(type_not_in_module,none,
1163+
"type %0 is not imported through module %1", (DeclName, Identifier))
1164+
NOTE(note_change_module_selector,none,
1165+
"did you mean module %0?", (Identifier))
11621166
NOTE(note_typo_candidate_implicit_member,none,
11631167
"did you mean the implicitly-synthesized %kindbase0?", (const ValueDecl *))
11641168
NOTE(note_remapped_type,none,

lib/Sema/TypeCheckType.cpp

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1493,6 +1493,33 @@ static Type diagnoseUnknownType(const TypeResolution &resolution,
14931493
return ErrorType::get(ctx);
14941494
}
14951495

1496+
// Is there an incorrect module selector?
1497+
if (repr->getNameRef().hasModuleSelector()) {
1498+
auto anyModuleName = DeclNameRef(repr->getNameRef().getFullName());
1499+
auto anyModuleResults =
1500+
TypeChecker::lookupUnqualifiedType(dc, anyModuleName,
1501+
repr->getLoc(), lookupOptions);
1502+
if (!anyModuleResults.empty()) {
1503+
diags.diagnose(repr->getNameLoc(), diag::type_not_in_module,
1504+
repr->getNameRef().getFullName(),
1505+
repr->getNameRef().getModuleSelector());
1506+
1507+
SourceLoc moduleSelectorLoc = repr->getNameLoc().getModuleSelectorLoc();
1508+
1509+
for (auto result : anyModuleResults) {
1510+
TypeDecl * decl = static_cast<TypeDecl*>(result.getValueDecl());
1511+
Identifier moduleName = decl->getModuleContext()->getName();
1512+
1513+
diags.diagnose(moduleSelectorLoc, diag::note_change_module_selector,
1514+
moduleName)
1515+
.fixItReplace(moduleSelectorLoc, moduleName.str());
1516+
}
1517+
1518+
// FIXME: Can we recover by assuming the first/best result is correct?
1519+
return ErrorType::get(ctx);
1520+
}
1521+
}
1522+
14961523
// Try ignoring access control.
14971524
NameLookupOptions relookupOptions = lookupOptions;
14981525
relookupOptions |= NameLookupFlags::IgnoreAccessControl;
@@ -1584,6 +1611,32 @@ static Type diagnoseUnknownType(const TypeResolution &resolution,
15841611
return ErrorType::get(ctx);
15851612
}
15861613

1614+
// Is there an incorrect module selector?
1615+
if (repr->getNameRef().hasModuleSelector()) {
1616+
auto anyModuleName = DeclNameRef(repr->getNameRef().getFullName());
1617+
auto anyModuleResults = TypeChecker::lookupMemberType(
1618+
dc, parentType, anyModuleName, repr->getLoc(), lookupOptions);
1619+
if (anyModuleResults) {
1620+
diags.diagnose(repr->getNameLoc(), diag::type_not_in_module,
1621+
repr->getNameRef().getFullName(),
1622+
repr->getNameRef().getModuleSelector());
1623+
1624+
SourceLoc moduleSelectorLoc = repr->getNameLoc().getModuleSelectorLoc();
1625+
1626+
for (auto result : anyModuleResults) {
1627+
TypeDecl * decl = result.Member;
1628+
Identifier moduleName = decl->getModuleContext()->getName();
1629+
1630+
diags.diagnose(moduleSelectorLoc, diag::note_change_module_selector,
1631+
moduleName)
1632+
.fixItReplace(moduleSelectorLoc, moduleName.str());
1633+
}
1634+
1635+
// FIXME: Can we recover by assuming the first/best result is correct?
1636+
return ErrorType::get(ctx);
1637+
}
1638+
}
1639+
15871640
// Try ignoring access control.
15881641
NameLookupOptions relookupOptions = lookupOptions;
15891642
relookupOptions |= NameLookupFlags::IgnoreAccessControl;

test/NameLookup/module_selector.swift

Lines changed: 40 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -66,19 +66,24 @@ 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}}
69+
// expected-error@-1 {{type 'B' is not imported through module 'main'}}
70+
// expected-note@-2 {{did you mean module 'ModuleSelectorTestingKit'?}} {{11-15=ModuleSelectorTestingKit}}
7071

7172
extension B: @retroactive main::Equatable {
72-
// FIXME improve: expected-error@-1 {{cannot find type 'main::Equatable' in scope}}
73+
// expected-error@-1 {{type 'Equatable' is not imported through module 'main'}}
74+
// expected-note@-2 {{did you mean module 'Swift'?}} {{27-31=Swift}}
7375

7476
@_implements(main::Equatable, main::==(_:_:))
7577
// 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}}
78+
// expected-error@-2 {{type 'Equatable' is not imported through module 'main'}}
79+
// expected-note@-3 {{did you mean module 'Swift'?}} {{16-20=Swift}}
7780

7881
public static func equals(_: main::B, _: main::B) -> main::Bool {
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+
// expected-error@-1 2{{type 'B' is not imported through module 'main'}}
83+
// expected-note@-2 {{did you mean module 'ModuleSelectorTestingKit'?}} {{32-36=ModuleSelectorTestingKit}}
84+
// expected-note@-3 {{did you mean module 'ModuleSelectorTestingKit'?}} {{44-48=ModuleSelectorTestingKit}}
85+
// expected-error@-4 {{type 'Bool' is not imported through module 'main'}}
86+
// expected-note@-5 {{did you mean module 'Swift'?}} {{56-60=Swift}}
8287
main::fatalError() // no-error -- not typechecking function bodies
8388
}
8489

@@ -90,12 +95,16 @@ extension B: @retroactive main::Equatable {
9095

9196
mutating func myNegate() {
9297
let fn: (main::Int, main::Int) -> main::Int =
93-
// FIXME improve: expected-error@-1 3{{cannot find type 'main::Int' in scope}}
98+
// expected-error@-1 3{{type 'Int' is not imported through module 'main'}}
99+
// expected-note@-2 {{did you mean module 'Swift'?}} {{14-18=Swift}}
100+
// expected-note@-3 {{did you mean module 'Swift'?}} {{25-29=Swift}}
101+
// expected-note@-4 {{did you mean module 'Swift'?}} {{39-43=Swift}}
94102
(main::+)
95103
// FIXME: should fail????
96104

97105
let magnitude: Int.main::Magnitude = main::magnitude
98-
// FIXME improve: expected-error@-1 {{'main::Magnitude' is not a member type of struct 'Swift.Int'}}
106+
// expected-error@-1 {{type 'Magnitude' is not imported through module 'main'}}
107+
// expected-note@-2 {{did you mean module 'Swift'?}} {{24-28=Swift}}
99108

100109
_ = (fn, magnitude)
101110

@@ -129,14 +138,17 @@ extension B: @retroactive main::Equatable {
129138
extension ModuleSelectorTestingKit::C {}
130139

131140
extension C: @retroactive ModuleSelectorTestingKit::Equatable {
132-
// FIXME improve: expected-error@-1 {{cannot find type 'ModuleSelectorTestingKit::Equatable' in scope}}
141+
// expected-error@-1 {{type 'Equatable' is not imported through module 'ModuleSelectorTestingKit'}}
142+
// expected-note@-2 {{did you mean module 'Swift'?}} {{27-51=Swift}}
133143

134144
@_implements(ModuleSelectorTestingKit::Equatable, ModuleSelectorTestingKit::==(_:_:))
135145
// 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=}}
136-
// FIXME improve: expected-error@-2 {{cannot find type 'ModuleSelectorTestingKit::Equatable' in scope}}
146+
// expected-error@-2 {{type 'Equatable' is not imported through module 'ModuleSelectorTestingKit'}}
147+
// expected-note@-3 {{did you mean module 'Swift'?}} {{16-40=Swift}}
137148

138149
public static func equals(_: ModuleSelectorTestingKit::C, _: ModuleSelectorTestingKit::C) -> ModuleSelectorTestingKit::Bool {
139-
// FIXME improve: expected-error@-1 {{cannot find type 'ModuleSelectorTestingKit::Bool' in scope}}
150+
// expected-error@-1 {{type 'Bool' is not imported through module 'ModuleSelectorTestingKit'}}
151+
// expected-note@-2 {{did you mean module 'Swift'?}} {{96-120=Swift}}
140152

141153
ModuleSelectorTestingKit::fatalError() // no-error -- not typechecking function bodies
142154
}
@@ -150,12 +162,16 @@ extension C: @retroactive ModuleSelectorTestingKit::Equatable {
150162
// FIXME improve: expected-note@-1 {{did you mean 'myNegate'?}}
151163

152164
let fn: (ModuleSelectorTestingKit::Int, ModuleSelectorTestingKit::Int) -> ModuleSelectorTestingKit::Int =
153-
// FIXME improve: expected-error@-1 3{{cannot find type 'ModuleSelectorTestingKit::Int' in scope}}
165+
// expected-error@-1 3{{type 'Int' is not imported through module 'ModuleSelectorTestingKit'}}
166+
// expected-note@-2 {{did you mean module 'Swift'?}} {{14-38=Swift}}
167+
// expected-note@-3 {{did you mean module 'Swift'?}} {{45-69=Swift}}
168+
// expected-note@-4 {{did you mean module 'Swift'?}} {{79-103=Swift}}
154169
(ModuleSelectorTestingKit::+)
155170
// FIXME: should fail????
156171

157172
let magnitude: Int.ModuleSelectorTestingKit::Magnitude = ModuleSelectorTestingKit::magnitude
158-
// FIXME improve: expected-error@-1 {{'ModuleSelectorTestingKit::Magnitude' is not a member type of struct 'Swift.Int'}}
173+
// expected-error@-1 {{type 'Magnitude' is not imported through module 'ModuleSelectorTestingKit'}}
174+
// expected-note@-2 {{did you mean module 'Swift'?}} {{24-48=Swift}}
159175

160176
_ = (fn, magnitude)
161177

@@ -185,7 +201,8 @@ extension C: @retroactive ModuleSelectorTestingKit::Equatable {
185201
// Test resolution of Swift:: using `D`
186202

187203
extension Swift::D {}
188-
// FIXME improve: expected-error@-1 {{cannot find type 'Swift::D' in scope}}
204+
// expected-error@-1 {{type 'D' is not imported through module 'Swift'}}
205+
// expected-note@-2 {{did you mean module 'ModuleSelectorTestingKit'?}} {{11-16=ModuleSelectorTestingKit}}
189206

190207
extension D: @retroactive Swift::Equatable {
191208
// 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}}
@@ -194,8 +211,9 @@ extension D: @retroactive Swift::Equatable {
194211
// 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=}}
195212

196213
public static func equals(_: Swift::D, _: Swift::D) -> Swift::Bool {
197-
// expected-error@-1 {{cannot find type 'Swift::D' in scope}}
198-
// expected-error@-2 {{cannot find type 'Swift::D' in scope}}
214+
// expected-error@-1 2{{type 'D' is not imported through module 'Swift'}}
215+
// expected-note@-2 {{did you mean module 'ModuleSelectorTestingKit'?}} {{32-37=ModuleSelectorTestingKit}}
216+
// expected-note@-3 {{did you mean module 'ModuleSelectorTestingKit'?}} {{45-50=ModuleSelectorTestingKit}}
199217
Swift::fatalError() // no-error -- not typechecking function bodies
200218
}
201219

@@ -301,7 +319,8 @@ func main::decl1(
301319
// 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=}}
302320
main::p1: main::A,
303321
// expected-error@-1 {{argument label cannot be qualified with a module selector}} expected-note@-1 {{remove module selector from this name}} {{3-9=}}
304-
// FIXME: expected-error@-2 {{cannot find type 'main::A' in scope}}
322+
// expected-error@-2 {{type 'A' is not imported through module 'main'}}
323+
// expected-note@-3 {{did you mean module 'ModuleSelectorTestingKit'?}} {{13-17=ModuleSelectorTestingKit}}
305324
main::label p2: main::inout A,
306325
// expected-error@-1 {{argument label cannot be qualified with a module selector}} expected-note@-1 {{remove module selector from this name}} {{3-9=}}
307326
// FIXME: expected-error@-2 {{expected identifier in dotted type}} should be something like {{type 'inout' is not imported through module 'main'}}
@@ -391,7 +410,8 @@ class main::decl4<main::T> {}
391410

392411
typealias main::decl5 = main::Bool
393412
// 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=}}
394-
// FIXME improve: expected-error@-2 {{cannot find type 'main::Bool' in scope}}
413+
// expected-error@-2 {{type 'Bool' is not imported through module 'main'}}
414+
// expected-note@-3 {{did you mean module 'Swift'?}} {{25-29=Swift}}
395415

396416
protocol main::decl6 {
397417
// 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=}}
@@ -432,7 +452,8 @@ struct Parent {
432452

433453
typealias main::decl5 = main::Bool
434454
// 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=}}
435-
// FIXME improve: expected-error@-2 {{cannot find type 'main::Bool' in scope}}
455+
// expected-error@-2 {{type 'Bool' is not imported through module 'main'}}
456+
// expected-note@-3 {{did you mean module 'Swift'?}} {{27-31=Swift}}
436457
}
437458

438459
@_swift_native_objc_runtime_base(main::BaseClass)

0 commit comments

Comments
 (0)