Skip to content

Commit 0abcf8d

Browse files
committed
[Concurrency] Infer @Sendable for globally isolated function types.
Since globally isolated functions may never be called concurrently, they are implicitly Sendable.
1 parent 826515f commit 0abcf8d

File tree

4 files changed

+23
-4
lines changed

4 files changed

+23
-4
lines changed

include/swift/AST/Types.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3634,9 +3634,7 @@ class AnyFunctionType : public TypeBase {
36343634
return getExtInfo().isNoEscape();
36353635
}
36363636

3637-
bool isSendable() const {
3638-
return getExtInfo().isSendable();
3639-
}
3637+
bool isSendable() const;
36403638

36413639
bool isAsync() const { return getExtInfo().isAsync(); }
36423640

lib/AST/Type.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3895,6 +3895,15 @@ Type AnyFunctionType::getThrownError() const {
38953895
}
38963896
}
38973897

3898+
bool AnyFunctionType::isSendable() const {
3899+
auto &ctx = getASTContext();
3900+
if (ctx.LangOpts.hasFeature(Feature::GlobalActorIsolatedTypesUsability)) {
3901+
// Global-actor-isolated function types are implicitly Sendable.
3902+
return getExtInfo().isSendable() || getIsolation().isGlobalActor();
3903+
}
3904+
return getExtInfo().isSendable();
3905+
}
3906+
38983907
Type AnyFunctionType::getGlobalActor() const {
38993908
switch (getKind()) {
39003909
case TypeKind::Function:

test/Concurrency/actor_isolation.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -559,7 +559,7 @@ extension MyActor {
559559

560560
func testBadImplicitGlobalActorClosureCall() async {
561561
{ @MainActor in }() // expected-error{{expression is 'async' but is not marked with 'await'}}
562-
// expected-note@-1{{calls function of type '@MainActor () -> ()' from outside of its actor context are implicitly asynchronous}}
562+
// expected-note@-1{{calls function of type '@MainActor @Sendable () -> ()' from outside of its actor context are implicitly asynchronous}}
563563
}
564564

565565

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// RUN: %target-typecheck-verify-swift -strict-concurrency=complete -disable-availability-checking -enable-upcoming-feature RegionBasedIsolation -enable-experimental-feature GlobalActorIsolatedTypesUsability
2+
3+
// REQUIRES: concurrency
4+
// REQUIRES: asserts
5+
6+
func inferSendableFunctionType() {
7+
let closure: @MainActor () -> Void = {}
8+
9+
Task {
10+
await closure() // okay
11+
}
12+
}

0 commit comments

Comments
 (0)