Skip to content

Commit 4d18a2c

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. (cherry picked from commit 2ac874e)
1 parent e80f9ba commit 4d18a2c

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
@@ -11783,10 +11783,10 @@ bool ConstraintSystem::resolveClosure(TypeVariableType *typeVar,
1178311783
if (contextualParam->isIsolated() && !flags.isIsolated() && paramDecl)
1178411784
isolatedParams.insert(paramDecl);
1178511785

11786-
param =
11787-
param.withFlags(flags.withInOut(contextualParam->isInOut())
11788-
.withVariadic(contextualParam->isVariadic())
11789-
.withIsolated(contextualParam->isIsolated()));
11786+
param = param.withFlags(flags.withInOut(contextualParam->isInOut())
11787+
.withVariadic(contextualParam->isVariadic())
11788+
.withIsolated(contextualParam->isIsolated())
11789+
.withSending(contextualParam->isSending()));
1179011790
}
1179111791
}
1179211792

@@ -11913,6 +11913,12 @@ bool ConstraintSystem::resolveClosure(TypeVariableType *typeVar,
1191311913
closureExtInfo = closureExtInfo.withSendable();
1191411914
}
1191511915

11916+
// Propagate sending result from the contextual type to the closure.
11917+
if (auto contextualFnType = contextualType->getAs<FunctionType>()) {
11918+
if (contextualFnType->hasExtInfo() && contextualFnType->hasSendingResult())
11919+
closureExtInfo = closureExtInfo.withSendingResult();
11920+
}
11921+
1191611922
// Isolated parameters override any other kind of isolation we might infer.
1191711923
if (hasIsolatedParam) {
1191811924
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)