Skip to content

Commit 80560da

Browse files
committed
CSGen: Infer generic arguments in explicit closure result types
1 parent 07fdb2e commit 80560da

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

lib/Sema/CSGen.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2073,12 +2073,14 @@ namespace {
20732073

20742074
return resolveTypeReferenceInExpression(
20752075
closure->getExplicitResultTypeRepr(),
2076-
TypeResolverContext::InExpression, nullptr);
2076+
TypeResolverContext::InExpression,
2077+
// Introduce type variables for unbound generics.
2078+
OpenUnboundGenericType(
2079+
CS, CS.getConstraintLocator(closure,
2080+
ConstraintLocator::ClosureResult)));
20772081
};
20782082

20792083
Type resultTy;
2080-
auto *resultLoc =
2081-
CS.getConstraintLocator(closure, ConstraintLocator::ClosureResult);
20822084
if (auto explicityTy = getExplicitResultType()) {
20832085
resultTy = explicityTy;
20842086
} else {
@@ -2099,9 +2101,11 @@ namespace {
20992101
// If this is a multi-statement closure, let's mark result
21002102
// as potential hole right away.
21012103
resultTy = CS.createTypeVariable(
2102-
resultLoc,
2104+
CS.getConstraintLocator(closure,
2105+
ConstraintLocator::ClosureResult),
21032106
shouldTypeCheckInEnclosingExpression(closure)
2104-
? 0 : TVO_CanBindToHole);
2107+
? 0
2108+
: TVO_CanBindToHole);
21052109
}
21062110
}
21072111

test/Constraints/closures.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1030,3 +1030,15 @@ func sr12815() {
10301030
.doesntExist2() { $0 }
10311031
}
10321032
}
1033+
1034+
// Make sure we can infer generic arguments in an explicit result type.
1035+
let explicitUnboundResult1 = { () -> Array in [0] }
1036+
let explicitUnboundResult2: (Array<Bool>) -> Array<Int> = {
1037+
(arr: Array) -> Array in [0]
1038+
}
1039+
// FIXME: Should we prioritize the contextual result type and infer Array<Int>
1040+
// rather than using a type variable in these cases?
1041+
// expected-error@+1 {{unable to infer closure type in the current context}}
1042+
let explicitUnboundResult3: (Array<Bool>) -> Array<Int> = {
1043+
(arr: Array) -> Array in [true]
1044+
}

0 commit comments

Comments
 (0)