[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:
Pavel Yaskevich
2019-12-06 15:01:52 -08:00
parent d8997c6223
commit 08baf67677
2 changed files with 29 additions and 0 deletions

View File

@@ -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);