Skip to content

Commit 8b1debf

Browse files
committed
[ConstraintSystem] Detect contextual type mismatches associated with optional evaluation
1 parent 918ff24 commit 8b1debf

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

lib/Sema/CSSimplify.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2846,6 +2846,22 @@ bool ConstraintSystem::repairFailures(
28462846
}
28472847
}
28482848

2849+
if (auto *OEE = dyn_cast<OptionalEvaluationExpr>(anchor)) {
2850+
// If concrete type of the sub-expression can't be converted to the
2851+
// type associated with optional evaluation result it could only be
2852+
// contextual mismatch where type of the top-level expression
2853+
// comes from contextual type or its parent expression.
2854+
//
2855+
// Because result type of the optional evaluation is supposed to
2856+
// represent the type of its sub-expression with added level of
2857+
// optionality if needed.
2858+
if (!lhs->getOptionalObjectType() && !lhs->hasTypeVariable()) {
2859+
conversionsOrFixes.push_back(IgnoreContextualType::create(
2860+
*this, lhs, rhs, getConstraintLocator(OEE->getSubExpr())));
2861+
return true;
2862+
}
2863+
}
2864+
28492865
if (auto *AE = dyn_cast<AssignExpr>(anchor)) {
28502866
if (repairByInsertingExplicitCall(lhs, rhs))
28512867
return true;

test/Constraints/diagnostics.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -920,13 +920,13 @@ class NSCache<K, V> {
920920
}
921921

922922
class CacheValue {
923-
func value(x: Int) -> Int {} // expected-note {{found this candidate}}
924-
func value(y: String) -> String {} // expected-note {{found this candidate}}
923+
func value(x: Int) -> Int {} // expected-note {{found candidate with type '(Int) -> Int'}}
924+
func value(y: String) -> String {} // expected-note {{found candidate with type '(String) -> String'}}
925925
}
926926

927927
func valueForKey<K>(_ key: K) -> CacheValue? {
928928
let cache = NSCache<K, CacheValue>()
929-
return cache.object(forKey: key)?.value // expected-error {{ambiguous reference to member 'value(x:)'}}
929+
return cache.object(forKey: key)?.value // expected-error {{no exact matches in call to instance method 'value'}}
930930
}
931931

932932
// SR-2242: poor diagnostic when argument label is omitted

0 commit comments

Comments
 (0)