mirror of
https://github.com/apple/swift.git
synced 2025-12-21 12:14:44 +01:00
[ConstraintSystem] Fix a case of too eager force optional unwrap with optional contextual type
If return of optional chaining is assigned to an optional type or discarded there should be no force optional unwrap attempt on it because type variable associated with context can be bound to optional of any depth. Resolves: rdar://problem/57668873
This commit is contained in:
@@ -2662,6 +2662,17 @@ repairViaOptionalUnwrap(ConstraintSystem &cs, Type fromType, Type toType,
|
||||
if (fromUnwraps <= toUnwraps && !fromObjectType->is<TypeVariableType>())
|
||||
return false;
|
||||
|
||||
// If the result of optional chaining is converted to
|
||||
// an optional contextual type represented by a type
|
||||
// variable e.g. `T?`, there can be no optional mismatch
|
||||
// because `T` could be bound to an optional of any depth.
|
||||
if (isa<OptionalEvaluationExpr>(anchor) && toUnwraps > 0) {
|
||||
auto last = locator.last();
|
||||
if (last && last->is<LocatorPathElt::ContextualType>() &&
|
||||
toObjectType->is<TypeVariableType>())
|
||||
return false;
|
||||
}
|
||||
|
||||
auto result =
|
||||
cs.matchTypes(fromObjectType, toObjectType, matchKind,
|
||||
ConstraintSystem::TypeMatchFlags::TMF_ApplyingFix, locator);
|
||||
|
||||
Reference in New Issue
Block a user