Skip to content

Commit ba2187a

Browse files
committed
[Diagnostics] Add the opaque type fix-it for failed associated type
inference from the element type of a subscript value witness.
1 parent c75178e commit ba2187a

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

lib/Sema/TypeCheckProtocolInference.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1768,6 +1768,9 @@ bool AssociatedTypeInference::diagnoseNoSolutions(
17681768
} else if (auto *func = dyn_cast<FuncDecl>(failed.Witness)) {
17691769
resultType = func->getResultInterfaceType();
17701770
typeRange = func->getResultTypeSourceRange();
1771+
} else if (auto *subscript = dyn_cast<SubscriptDecl>(failed.Witness)) {
1772+
resultType = subscript->getElementInterfaceType();
1773+
typeRange = subscript->getElementTypeSourceRange();
17711774
}
17721775

17731776
// If the type witness was inferred from an existential

test/decl/protocol/req/missing_conformance.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,15 @@ struct S4: P13 { // expected-error {{type 'S4' does not conform to protocol 'P13
119119
// expected-note@-1 {{cannot infer 'A' = 'P11' because 'P11' as a type cannot conform to protocols; did you mean to use an opaque result type?}}{{12-12=some }}
120120
}
121121

122+
protocol P14 {
123+
associatedtype A : P11 // expected-note {{unable to infer associated type 'A' for protocol 'P14'}}
124+
subscript(i: Int) -> A { get }
125+
}
126+
struct S5: P14 { // expected-error {{type 'S5' does not conform to protocol 'P14'}}
127+
subscript(i: Int) -> P11 { return i }
128+
// expected-note@-1 {{cannot infer 'A' = 'P11' because 'P11' as a type cannot conform to protocols; did you mean to use an opaque result type?}}{{24-24=some }}
129+
}
130+
122131
// SR-12759
123132
struct CountSteps1<T> : Collection {
124133
init(count: Int) { self.count = count }

0 commit comments

Comments
 (0)