Skip to content

Commit 45d3c88

Browse files
committed
[Clang importer] Import __swift_attr__("@sendable") as Sendable on parameters.
1 parent 5f71e52 commit 45d3c88

File tree

4 files changed

+26
-0
lines changed

4 files changed

+26
-0
lines changed

lib/ClangImporter/ImportType.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1739,6 +1739,15 @@ Type ClangImporter::Implementation::applyParamAttributes(
17391739

17401740
continue;
17411741
}
1742+
1743+
// Map @Sendable.
1744+
if (swiftAttr->getAttribute() == "@Sendable") {
1745+
type = applyToFunctionType(type, [](ASTExtInfo extInfo) {
1746+
return extInfo.withConcurrent();
1747+
});
1748+
1749+
continue;
1750+
}
17421751
}
17431752

17441753
return type;

test/ClangImporter/objc_async.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,18 @@ func testSlowServerOldSchool(slowServer: SlowServer) {
8282
_ = slowServer.allOperations
8383
}
8484

85+
func testSendable(fn: () -> Void) { // expected-note{{parameter 'fn' is implicitly non-concurrent}}
86+
doSomethingConcurrently(fn)
87+
// expected-error@-1{{passing non-concurrent parameter 'fn' to function expecting a @Sendable closure}}
88+
89+
var x = 17
90+
doSomethingConcurrently {
91+
print(x) // expected-error{{reference to captured var 'x' in concurrently-executing code}}
92+
x = x + 1 // expected-error{{mutation of captured var 'x' in concurrently-executing code}}
93+
// expected-error@-1{{reference to captured var 'x' in concurrently-executing code}}
94+
}
95+
}
96+
8597
// Check import of attributes
8698
func globalAsync() async { }
8799

test/IDE/print_clang_objc_async.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,5 @@ import _Concurrency
4747
// CHECK-NEXT: {{^[}]$}}
4848

4949
// CHECK: {{^}}var MAGIC_NUMBER: Int32 { get }
50+
51+
// CHECK: func doSomethingConcurrently(_ block: @Sendable () -> Void)

test/Inputs/clang-importer-sdk/usr/include/ObjCConcurrency.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,4 +144,7 @@ __attribute__((__swift_attr__("@MainActor(unsafe)")))
144144
-(void)onButtonPress;
145145
@end
146146

147+
// Do something concurrently, but without escaping.
148+
void doSomethingConcurrently(__attribute__((noescape)) __attribute__((swift_attr("@Sendable"))) void (^block)(void));
149+
147150
#pragma clang assume_nonnull end

0 commit comments

Comments
 (0)