Skip to content

Commit 2fd247a

Browse files
committed
[QoI] Improve contextual mismatch diagnostics in calls with trailing closures
Resolves: rdar://problem/28012273
1 parent 2c7c743 commit 2fd247a

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

lib/Sema/CSDiag.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6098,10 +6098,17 @@ bool FailureDiagnosis::diagnoseTrailingClosureErrors(ApplyExpr *callExpr) {
60986098
// let's try to re-typecheck it to see if we can get some
60996099
// more information about what is going on.
61006100
if (currentType->hasTypeVariable() || currentType->hasUnresolvedType()) {
6101-
CallResultListener listener(CS->getContextualType());
6101+
auto contextualType = CS->getContextualType();
6102+
CallResultListener listener(contextualType);
61026103
CS->TC.getPossibleTypesOfExpressionWithoutApplying(
61036104
fnExpr, CS->DC, possibleTypes, FreeTypeVariableBinding::UnresolvedType,
61046105
&listener);
6106+
6107+
// Looks like there is there a contextual mismatch
6108+
// related to function type, let's try to diagnose it.
6109+
if (possibleTypes.empty() && contextualType &&
6110+
!contextualType->hasUnresolvedType())
6111+
return diagnoseContextualConversionError();
61056112
} else {
61066113
possibleTypes.push_back(currentType);
61076114
}

lib/Sema/ConstraintSystem.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -752,7 +752,7 @@ enum class ConstraintSystemFlags {
752752
PreferForceUnwrapToOptional = 0x02,
753753

754754
/// If set, this is going to prevent constraint system from erasing all
755-
/// discovered solutions expect the best one.
755+
/// discovered solutions except the best one.
756756
ReturnAllDiscoveredSolutions = 0x04,
757757
};
758758

test/Constraints/trailing_closures_objc.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,12 @@ func rdar28004686(a: [IndexPath]) {
1818
_ = a.sorted { (lhs: NSIndexPath, rhs: NSIndexPath) -> Bool in true }
1919
// expected-error@-1 {{'NSIndexPath' is not convertible to 'IndexPath'}}
2020
}
21+
22+
class Test: NSObject {
23+
var categories : NSArray?
24+
func rdar28012273() {
25+
let categories = ["hello", "world"]
26+
self.categories = categories.sorted { $0.localizedCaseInsensitiveCompare($1) == ComparisonResult.orderedDescending }
27+
// expected-error@-1 {{cannot assign value of type '[String]' to type 'NSArray?'}} {{121-121= as NSArray}}
28+
}
29+
}

0 commit comments

Comments
 (0)