Skip to content

Commit 0562c31

Browse files
committed
[CSSimplify] Look through optionals in contextual type while resolving a 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.: ```swift 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.
1 parent 0572b35 commit 0562c31

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

lib/Sema/CSSimplify.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8681,6 +8681,22 @@ bool ConstraintSystem::resolveClosure(TypeVariableType *typeVar,
86818681
auto *closure = castToExpr<ClosureExpr>(closureLocator->getAnchor());
86828682
auto *inferredClosureType = getClosureType(closure);
86838683

8684+
// Let's look through all optionals associated with contextual
8685+
// type to make it possible to infer parameter/result type of
8686+
// the closure faster e.g.:
8687+
//
8688+
// func test(_: ((Int) -> Void)?) {
8689+
// ...
8690+
// }
8691+
//
8692+
// test { $0 + ... }
8693+
//
8694+
// In this case dropping optionality from contextual type
8695+
// `((Int) -> Void)?` allows `resolveClosure` to infer type
8696+
// of `$0` directly (via `getContextualParamAt`) instead of
8697+
// having to use type variable inference mechanism.
8698+
contextualType = contextualType->lookThroughAllOptionalTypes();
8699+
86848700
auto getContextualParamAt =
86858701
[&contextualType, &inferredClosureType](
86868702
unsigned index) -> Optional<AnyFunctionType::Param> {

0 commit comments

Comments
 (0)