Skip to content

Commit 773d521

Browse files
committed
fix rdar://25271859 QoI: Misleading error message when expression result can't be inferred from closure
We previously produced the error message: rdar25271859.swift:14:11: error: value of tuple type '(Float, Int)' has no member '0' a.map { $0.0 } ^~ ~ We now produce: rdar25271859.swift:15:5: error: generic parameter 'U' could not be inferred .andThen { dataResult in ^ which is at least is correct, if not yet helpful.
1 parent 71de3c9 commit 773d521

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

lib/Sema/CSDiag.cpp

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2211,7 +2211,21 @@ bool FailureDiagnosis::diagnoseGeneralMemberFailure(Constraint *constraint) {
22112211
}
22122212
}
22132213

2214-
if (baseObjTy->is<TupleType>()) {
2214+
// If this is a tuple, then the index needs to be valid.
2215+
if (auto tuple = baseObjTy->getAs<TupleType>()) {
2216+
StringRef nameStr = memberName.getBaseName().str();
2217+
int fieldIdx = -1;
2218+
// Resolve a number reference into the tuple type.
2219+
unsigned Value = 0;
2220+
if (!nameStr.getAsInteger(10, Value) && Value < tuple->getNumElements()) {
2221+
fieldIdx = Value;
2222+
} else {
2223+
fieldIdx = tuple->getNamedElementId(memberName.getBaseName());
2224+
}
2225+
2226+
if (fieldIdx != -1)
2227+
return false; // Lookup is valid.
2228+
22152229
diagnose(anchor->getLoc(), diag::could_not_find_tuple_member,
22162230
baseObjTy, memberName)
22172231
.highlight(anchor->getSourceRange()).highlight(memberRange);

test/Constraints/tuple.swift

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,4 +179,23 @@ func makeRequest() {
179179
}
180180
}
181181

182+
// <rdar://problem/25271859> QoI: Misleading error message when expression result can't be inferred from closure
183+
struct r25271859<T> {
184+
}
185+
186+
extension r25271859 {
187+
func map<U>(f: (T) -> U) -> r25271859<U> {
188+
}
189+
190+
func andThen<U>(f: (T)->r25271859<U>) {
191+
}
192+
}
193+
194+
func f(a : r25271859<(Float, Int)>) {
195+
a.map { $0.0 }
196+
.andThen { _ in // expected-error {{generic parameter 'U' could not be inferred}}
197+
print("hello") // comment this out and it runs, leave any form of print in and it doesn't
198+
return Task<String>()
199+
}
200+
}
182201

0 commit comments

Comments
 (0)