Skip to content

Commit 363b66a

Browse files
committed
Fully Qualify the Parent Type When Diagnosing Missing Member Types
Use the FullyQualified<Type> abstraction from the prior commit plus DescriptiveDeclKind to give a bit more information when issuing a missing member type diagnostic during type resolution.
1 parent b3843af commit 363b66a

19 files changed

+63
-46
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -802,7 +802,8 @@ NOTE(invalid_redecl_implicit_wrapper,none,
802802
ERROR(ambiguous_type_base,none,
803803
"%0 is ambiguous for type lookup in this context", (DeclNameRef))
804804
ERROR(invalid_member_type,none,
805-
"%0 is not a member type of %1", (DeclNameRef, Type))
805+
"%0 is not a member type of %1 %2",
806+
(DeclNameRef, DescriptiveDeclKind, FullyQualified<Type>))
806807
ERROR(invalid_member_type_suggest,none,
807808
"%0 does not have a member type named %1; did you mean %2?",
808809
(Type, DeclNameRef, DeclName))

lib/Sema/TypeCheckType.cpp

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,17 @@ Type TypeResolution::mapTypeIntoContext(Type type) const {
153153
llvm_unreachable("unhandled stage");
154154
}
155155

156+
// FIXME: It would be nice to have a 'DescriptiveTypeKind' abstraction instead.
157+
static DescriptiveDeclKind describeDeclOfType(Type t) {
158+
if (!t) {
159+
return DescriptiveDeclKind::Type;
160+
}
161+
if (auto *NTD = t->getAnyNominal()) {
162+
return NTD->getDescriptiveKind();
163+
}
164+
return DescriptiveDeclKind::Type;
165+
}
166+
156167
Type TypeResolution::resolveDependentMemberType(
157168
Type baseTy, DeclContext *DC,
158169
SourceRange baseRange,
@@ -211,8 +222,9 @@ Type TypeResolution::resolveDependentMemberType(
211222
if (!singleType) {
212223
auto name = ref->getNameRef();
213224
auto nameLoc = ref->getNameLoc();
214-
ctx.Diags.diagnose(nameLoc, diag::invalid_member_type, name, baseTy)
215-
.highlight(baseRange);
225+
const auto kind = describeDeclOfType(baseTy);
226+
ctx.Diags.diagnose(nameLoc, diag::invalid_member_type, name, kind, baseTy)
227+
.highlight(baseRange);
216228
corrections.noteAllCandidates();
217229

218230
return ErrorType::get(ctx);
@@ -1160,8 +1172,10 @@ static Type diagnoseUnknownType(TypeResolution resolution,
11601172

11611173
// Qualified lookup case.
11621174
if (!parentType->mayHaveMembers()) {
1163-
diags.diagnose(comp->getNameLoc(), diag::invalid_member_type,
1164-
comp->getNameRef(), parentType)
1175+
const auto kind = describeDeclOfType(parentType);
1176+
diags
1177+
.diagnose(comp->getNameLoc(), diag::invalid_member_type,
1178+
comp->getNameRef(), kind, parentType)
11651179
.highlight(parentRange);
11661180
return ErrorType::get(ctx);
11671181
}
@@ -1214,9 +1228,11 @@ static Type diagnoseUnknownType(TypeResolution resolution,
12141228
parentType)
12151229
.highlight(parentRange);
12161230
} else {
1217-
diags.diagnose(comp->getNameLoc(), diag::invalid_member_type,
1218-
comp->getNameRef(), parentType)
1219-
.highlight(parentRange);
1231+
const auto kind = describeDeclOfType(parentType);
1232+
diags
1233+
.diagnose(comp->getNameLoc(), diag::invalid_member_type,
1234+
comp->getNameRef(), kind, parentType)
1235+
.highlight(parentRange);
12201236
// Note where the type was defined, this can help diagnose if the user
12211237
// expected name lookup to find a module when there's a conflicting type.
12221238
if (auto typeDecl = parentType->getNominalOrBoundGenericNominal()) {

test/AutoDiff/Sema/differentiable_attr_type_checking.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -632,7 +632,7 @@ class Super: Differentiable {
632632
// TODO(TF-632): Fix "'TangentVector' is not a member type of 'Self'" diagnostic.
633633
// The underlying error should appear instead:
634634
// "covariant 'Self' can only appear at the top level of method result type".
635-
// expected-error @+1 2 {{'TangentVector' is not a member type of 'Self'}}
635+
// expected-error @+1 2 {{'TangentVector' is not a member type of type 'Self'}}
636636
func vjpDynamicSelfResult() -> (Self, (Self.TangentVector) -> Self.TangentVector) {
637637
return (self, { $0 })
638638
}

test/Constraints/invalid_archetype_constraint.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,6 @@ struct A2<T> : P {
1919
typealias Element = T
2020
}
2121

22-
func toA<S: Empty, AT:P>(_ s: S) -> AT where AT.Element == S.Generator.Element { // expected-error{{'Generator' is not a member type of 'S'}}
22+
func toA<S: Empty, AT:P>(_ s: S) -> AT where AT.Element == S.Generator.Element { // expected-error{{'Generator' is not a member type of type 'S'}}
2323
return AT()
2424
}

test/Constraints/invalid_constraint_lookup.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,6 @@ protocol _Collection {
2525
protocol Collection : _Collection, Sequence {
2626
subscript(i: Index) -> Iterator.Element {get set }
2727
}
28-
func insertionSort<C: Mutable> (_ elements: inout C, i: C.Index) { // expected-error {{cannot find type 'Mutable' in scope}} expected-error {{'Index' is not a member type of 'C'}}
29-
var x: C.Iterator.Element = elements[i] // expected-error {{'Iterator' is not a member type of 'C'}}
28+
func insertionSort<C: Mutable> (_ elements: inout C, i: C.Index) { // expected-error {{cannot find type 'Mutable' in scope}} expected-error {{'Index' is not a member type of type 'C'}}
29+
var x: C.Iterator.Element = elements[i] // expected-error {{'Iterator' is not a member type of type 'C'}}
3030
}

test/Constraints/same_types.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ func test8b<T: Barrable, U: Barrable>(_ t: T, u: U)
141141
}
142142

143143
// rdar://problem/19137463
144-
func rdar19137463<T>(_ t: T) where T.a == T {} // expected-error{{'a' is not a member type of 'T'}}
144+
func rdar19137463<T>(_ t: T) where T.a == T {} // expected-error{{'a' is not a member type of type 'T'}}
145145
rdar19137463(1)
146146

147147

test/Generics/associated_type_typo.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,4 @@ class D {
7979
typealias BElement = Int // expected-note{{did you mean 'BElement'?}}
8080
}
8181

82-
func typoSuperclass2<T : D>(_: T, _: T.Element) { } // expected-error{{'Element' is not a member type of 'T'}}
82+
func typoSuperclass2<T : D>(_: T, _: T.Element) { } // expected-error{{'Element' is not a member type of type 'T'}}

test/Generics/function_defs.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ func sameTypeEq<T>(_: T) where T = T {} // expected-error{{use '==' for same-typ
290290

291291
func badTypeConformance1<T>(_: T) where Int : EqualComparable {} // expected-error{{type 'Int' in conformance requirement does not refer to a generic parameter or associated type}}
292292

293-
func badTypeConformance2<T>(_: T) where T.Blarg : EqualComparable { } // expected-error{{'Blarg' is not a member type of 'T'}}
293+
func badTypeConformance2<T>(_: T) where T.Blarg : EqualComparable { } // expected-error{{'Blarg' is not a member type of type 'T'}}
294294

295295
func badTypeConformance3<T>(_: T) where (T) -> () : EqualComparable { }
296296
// expected-error@-1{{type '(T) -> ()' in conformance requirement does not refer to a generic parameter or associated type}}

test/Generics/generic_types.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ struct XParam<T> { // expected-note{{'XParam' declared here}}
199199
}
200200
}
201201

202-
var xp : XParam<Int>.T = Int() // expected-error{{'T' is not a member type of 'XParam<Int>'}}
202+
var xp : XParam<Int>.T = Int() // expected-error{{'T' is not a member type of generic struct 'generic_types.XParam<Swift.Int>'}}
203203

204204
// Diagnose failure to meet a superclass requirement.
205205
class X1 { }
@@ -237,11 +237,11 @@ class Bottom<T : Bottom<Top>> {}
237237
// Invalid inheritance clause
238238

239239
struct UnsolvableInheritance1<T : T.A> {}
240-
// expected-error@-1 {{'A' is not a member type of 'T'}}
240+
// expected-error@-1 {{'A' is not a member type of type 'T'}}
241241

242242
struct UnsolvableInheritance2<T : U.A, U : T.A> {}
243-
// expected-error@-1 {{'A' is not a member type of 'U'}}
244-
// expected-error@-2 {{'A' is not a member type of 'T'}}
243+
// expected-error@-1 {{'A' is not a member type of type 'U'}}
244+
// expected-error@-2 {{'A' is not a member type of type 'T'}}
245245

246246
enum X7<T> where X7.X : G { case X } // expected-error{{enum case 'X' is not a member type of 'X7<T>'}}
247247
// expected-error@-1{{cannot find type 'G' in scope}}

test/Generics/invalid.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ func badDiagnostic3() {
7979
// Crash with missing nested type inside concrete type
8080
class OuterGeneric<T> {
8181
class InnerGeneric<U> where U:OuterGeneric<T.NoSuchType> {
82-
// expected-error@-1 {{'NoSuchType' is not a member type of 'T'}}
82+
// expected-error@-1 {{'NoSuchType' is not a member type of type 'T'}}
8383
func method() {
8484
_ = method
8585
}
@@ -107,10 +107,10 @@ class P<N> {
107107

108108
// SR-5579
109109
protocol Foo {
110-
associatedtype Bar where Bar.Nonsense == Int // expected-error{{'Nonsense' is not a member type of 'Self.Bar'}}
110+
associatedtype Bar where Bar.Nonsense == Int // expected-error{{'Nonsense' is not a member type of type 'Self.Bar'}}
111111
}
112112

113-
protocol Wibble : Foo where Bar.EvenMoreNonsense == Int { } // expected-error{{'EvenMoreNonsense' is not a member type of 'Self.Bar'}}
113+
protocol Wibble : Foo where Bar.EvenMoreNonsense == Int { } // expected-error{{'EvenMoreNonsense' is not a member type of type 'Self.Bar'}}
114114

115115
// rdar://45271500 - failure to emit a diagnostic
116116
enum Cat<A> {}

0 commit comments

Comments
 (0)