Skip to content

Commit 2ac874e

Browse files
committed
[sending] Fix a few bugs around closure inference of sending parameters and results.
I found this while writing tests for the earlier part of this work. Since this is also type checking work, I am just folding this work into that work.
1 parent ba6c8af commit 2ac874e

File tree

4 files changed

+59
-4
lines changed

4 files changed

+59
-4
lines changed

include/swift/AST/Types.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3237,6 +3237,9 @@ class AnyFunctionType : public TypeBase {
32373237
/// Whether the parameter is 'isolated'.
32383238
bool isIsolated() const { return Flags.isIsolated(); }
32393239

3240+
/// Whether or not the parameter is 'sending'.
3241+
bool isSending() const { return Flags.isSending(); }
3242+
32403243
/// Whether the parameter is 'isCompileTimeConst'.
32413244
bool isCompileTimeConst() const { return Flags.isCompileTimeConst(); }
32423245

lib/Parse/ParseType.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1555,6 +1555,8 @@ bool Parser::canParseType() {
15551555
consumeToken();
15561556
} else if (Tok.isContextualKeyword("each")) {
15571557
consumeToken();
1558+
} else if (Tok.isContextualKeyword("sending")) {
1559+
consumeToken();
15581560
}
15591561

15601562
switch (Tok.getKind()) {

lib/Sema/CSSimplify.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11810,10 +11810,10 @@ bool ConstraintSystem::resolveClosure(TypeVariableType *typeVar,
1181011810
if (contextualParam->isIsolated() && !flags.isIsolated() && paramDecl)
1181111811
isolatedParams.insert(paramDecl);
1181211812

11813-
param =
11814-
param.withFlags(flags.withInOut(contextualParam->isInOut())
11815-
.withVariadic(contextualParam->isVariadic())
11816-
.withIsolated(contextualParam->isIsolated()));
11813+
param = param.withFlags(flags.withInOut(contextualParam->isInOut())
11814+
.withVariadic(contextualParam->isVariadic())
11815+
.withIsolated(contextualParam->isIsolated())
11816+
.withSending(contextualParam->isSending()));
1181711817
}
1181811818
}
1181911819

@@ -11940,6 +11940,12 @@ bool ConstraintSystem::resolveClosure(TypeVariableType *typeVar,
1194011940
closureExtInfo = closureExtInfo.withSendable();
1194111941
}
1194211942

11943+
// Propagate sending result from the contextual type to the closure.
11944+
if (auto contextualFnType = contextualType->getAs<FunctionType>()) {
11945+
if (contextualFnType->hasExtInfo() && contextualFnType->hasSendingResult())
11946+
closureExtInfo = closureExtInfo.withSendingResult();
11947+
}
11948+
1194311949
// Isolated parameters override any other kind of isolation we might infer.
1194411950
if (hasIsolatedParam) {
1194511951
closureExtInfo = closureExtInfo.withIsolation(
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// RUN: %target-swift-frontend -swift-version 6 %s -emit-silgen | %FileCheck %s
2+
3+
// READ THIS! This file only contains tests that validate that the relevant
4+
// function subtyping rules for sending work. Please do not put other tests in
5+
// the file!
6+
7+
// REQUIRES: concurrency
8+
// REQUIRES: asserts
9+
10+
////////////////////////
11+
// MARK: Declarations //
12+
////////////////////////
13+
14+
class NonSendableKlass {}
15+
16+
/////////////////
17+
// MARK: Tests //
18+
/////////////////
19+
20+
// CHECK: sil private [ossa] @$s25sending_closure_inference38testAnonymousParameterSendingInferenceyyFySSYucfU_ : $@convention(thin) (@sil_sending @guaranteed String) -> () {
21+
func testAnonymousParameterSendingInference() {
22+
let _: (sending String) -> () = {
23+
print($0)
24+
}
25+
}
26+
27+
// CHECK: sil private [ossa] @$s25sending_closure_inference38testNamedOnlyParameterSendingInferenceyyFySSYucfU_ : $@convention(thin) (@sil_sending @guaranteed String) -> () {
28+
func testNamedOnlyParameterSendingInference() {
29+
let _: (sending String) -> () = { x in
30+
print(x)
31+
}
32+
}
33+
34+
// CHECK: sil private [ossa] @$s25sending_closure_inference38testNamedTypeParameterSendingInferenceyyFySSnYucfU_ : $@convention(thin) (@sil_sending @owned String) -> () {
35+
func testNamedTypeParameterSendingInference() {
36+
let _: (sending String) -> () = { (x: sending String) in
37+
print(x)
38+
}
39+
}
40+
41+
// CHECK: sil private [ossa] @$s25sending_closure_inference26testSendingResultInferenceyyFSSyYTcfU_ : $@convention(thin) () -> @sil_sending @owned String {
42+
func testSendingResultInference() {
43+
let _: () -> sending String = { "" }
44+
}

0 commit comments

Comments
 (0)