Skip to content

Commit 81100ad

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 "" } ```
1 parent 2ac874e commit 81100ad

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
@@ -1649,10 +1649,9 @@ namespace {
16491649
}
16501650

16511651
Type
1652-
resolveTypeReferenceInExpression(TypeRepr *repr, TypeResolverContext resCtx,
1652+
resolveTypeReferenceInExpression(TypeRepr *repr,
1653+
TypeResolutionOptions options,
16531654
const ConstraintLocatorBuilder &locator) {
1654-
TypeResolutionOptions options(resCtx);
1655-
16561655
// Introduce type variables for unbound generics.
16571656
const auto genericOpener = OpenUnboundGenericType(CS, locator);
16581657
const auto placeholderHandler = HandlePlaceholderType(CS, locator);
@@ -2541,9 +2540,11 @@ namespace {
25412540
return declaredTy;
25422541
}
25432542

2543+
auto options =
2544+
TypeResolutionOptions(TypeResolverContext::InExpression);
2545+
options.setContext(TypeResolverContext::ClosureExpr);
25442546
const auto resolvedTy = resolveTypeReferenceInExpression(
2545-
closure->getExplicitResultTypeRepr(),
2546-
TypeResolverContext::InExpression, resultLocator);
2547+
closure->getExplicitResultTypeRepr(), options, resultLocator);
25472548
if (resolvedTy)
25482549
return resolvedTy;
25492550
}

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)