Skip to content

Commit 2dcd174

Browse files
committed
[sending] Fix type inference for sending results of closures.
The previous commit fixed things like: ```swift let x: () -> sending String = { "" } ``` This commit fixes this test case: ```swift let x = { () -> sending String in "" } ``` (cherry picked from commit 81100ad)
1 parent 4d18a2c commit 2dcd174

File tree

4 files changed

+13
-7
lines changed

4 files changed

+13
-7
lines changed

lib/Sema/CSGen.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1636,10 +1636,9 @@ namespace {
16361636
}
16371637

16381638
Type
1639-
resolveTypeReferenceInExpression(TypeRepr *repr, TypeResolverContext resCtx,
1639+
resolveTypeReferenceInExpression(TypeRepr *repr,
1640+
TypeResolutionOptions options,
16401641
const ConstraintLocatorBuilder &locator) {
1641-
TypeResolutionOptions options(resCtx);
1642-
16431642
// Introduce type variables for unbound generics.
16441643
const auto genericOpener = OpenUnboundGenericType(CS, locator);
16451644
const auto placeholderHandler = HandlePlaceholderType(CS, locator);
@@ -2528,9 +2527,11 @@ namespace {
25282527
return declaredTy;
25292528
}
25302529

2530+
auto options =
2531+
TypeResolutionOptions(TypeResolverContext::InExpression);
2532+
options.setContext(TypeResolverContext::ClosureExpr);
25312533
const auto resolvedTy = resolveTypeReferenceInExpression(
2532-
closure->getExplicitResultTypeRepr(),
2533-
TypeResolverContext::InExpression, resultLocator);
2534+
closure->getExplicitResultTypeRepr(), options, resultLocator);
25342535
if (resolvedTy)
25352536
return resolvedTy;
25362537
}

lib/Sema/TypeCheckType.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4971,7 +4971,8 @@ TypeResolver::resolveSendingTypeRepr(SendingTypeRepr *repr,
49714971
return ErrorType::get(getASTContext());
49724972
}
49734973

4974-
if (!options.is(TypeResolverContext::FunctionResult) &&
4974+
if (!options.is(TypeResolverContext::ClosureExpr) &&
4975+
!options.is(TypeResolverContext::FunctionResult) &&
49754976
(!options.is(TypeResolverContext::FunctionInput) ||
49764977
options.hasBase(TypeResolverContext::EnumElementDecl))) {
49774978
diagnoseInvalid(repr, repr->getSpecifierLoc(),

lib/Sema/TypeCheckType.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ enum class TypeResolverContext : uint8_t {
118118
/// Whether we are checking the parameter list of a subscript.
119119
SubscriptDecl,
120120

121-
/// Whether we are checking the parameter list of a closure.
121+
/// Whether we are checking the parameter list or result of a closure.
122122
ClosureExpr,
123123

124124
/// Whether we are in the input type of a function, or under one level of

test/Concurrency/sending_closure_inference.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,7 @@ func testNamedTypeParameterSendingInference() {
4242
func testSendingResultInference() {
4343
let _: () -> sending String = { "" }
4444
}
45+
46+
func testSendingResultOnClosure() {
47+
let _ = { (x: String) -> sending String in x }
48+
}

0 commit comments

Comments
 (0)