Merge pull request #38104 from xedin/drop-optionality-for-contextual-closure-type

[CSSimplify] Look through optionals in contextual type while resolving a closure
This commit is contained in:
Pavel Yaskevich
2021-06-28 10:04:08 -07:00
committed by GitHub
2 changed files with 98 additions and 0 deletions

View File

@@ -8699,6 +8699,22 @@ bool ConstraintSystem::resolveClosure(TypeVariableType *typeVar,
auto *closure = castToExpr<ClosureExpr>(closureLocator->getAnchor());
auto *inferredClosureType = getClosureType(closure);
// Let's look through all optionals associated with contextual
// type to make it possible to infer parameter/result type of
// the closure faster e.g.:
//
// func test(_: ((Int) -> Void)?) {
// ...
// }
//
// test { $0 + ... }
//
// In this case dropping optionality from contextual type
// `((Int) -> Void)?` allows `resolveClosure` to infer type
// of `$0` directly (via `getContextualParamAt`) instead of
// having to use type variable inference mechanism.
contextualType = contextualType->lookThroughAllOptionalTypes();
auto getContextualParamAt =
[&contextualType, &inferredClosureType](
unsigned index) -> Optional<AnyFunctionType::Param> {