[ConstraintSystem] Fix closure parameter destructuring

Detect and fix closure parameter destructuring where
it's not currently allowed e.g. free standing closures
with contextual type `let _: ((Int, Int)) -> Void = { $0 + $1 }`
This commit is contained in:
Pavel Yaskevich
2019-03-04 11:02:40 -08:00
parent c8343525c9
commit 8f85b848cc
3 changed files with 53 additions and 2 deletions

View File

@@ -1288,8 +1288,8 @@ ConstraintSystem::matchFunctionTypes(FunctionType *func1, FunctionType *func2,
return elt.getKind() != ConstraintLocator::OptionalPayload;
});
auto &ctx = getASTContext();
if (last != path.rend()) {
auto &ctx = getASTContext();
if (last->getKind() == ConstraintLocator::ApplyArgToParam) {
if (isSingleTupleParam(ctx, func2Params) &&
canImplodeParams(func1Params)) {
@@ -1310,6 +1310,20 @@ ConstraintSystem::matchFunctionTypes(FunctionType *func1, FunctionType *func2,
}
}
}
if (shouldAttemptFixes()) {
auto *anchor = locator.trySimplifyToExpr();
if (anchor && isa<ClosureExpr>(anchor) &&
isSingleTupleParam(ctx, func2Params) &&
canImplodeParams(func1Params)) {
auto *fix = AllowClosureParamDestructuring::create(
*this, func2, getConstraintLocator(anchor));
if (recordFix(fix))
return getTypeMatchFailure(argumentLocator);
implodeParams(func1Params);
}
}
}
// https://bugs.swift.org/browse/SR-6796
@@ -5779,6 +5793,7 @@ ConstraintSystem::SolutionKind ConstraintSystem::simplifyFixConstraint(
case FixKind::AllowTypeOrInstanceMember:
case FixKind::AllowInvalidPartialApplication:
case FixKind::AllowInvalidInitRef:
case FixKind::AllowClosureParameterDestructuring:
llvm_unreachable("handled elsewhere");
}