Skip to content

[Concurrency] Downgrade non-Sendable type captures to warnings if clo… #80737

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions lib/Sema/TypeCheckConcurrency.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2924,8 +2924,10 @@ namespace {
auto *explicitClosure = dyn_cast_or_null<ClosureExpr>(closure);

bool preconcurrency = false;
if (explicitClosure) {
preconcurrency = explicitClosure->isIsolatedByPreconcurrency();
if (closure) {
preconcurrency =
getActorIsolationOfContext(closure, getClosureActorIsolation)
.preconcurrency();
}

for (const auto &capture : localFunc.getCaptureInfo().getCaptures()) {
Expand Down
28 changes: 28 additions & 0 deletions test/Concurrency/sendable_checking_captures_swift6.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,31 @@ do {
}
}
}

func use(_ closure: @autoclosure () -> Any) {
}

do {
class C {
@preconcurrency static func f(_: @escaping @Sendable () -> Void) {}
}

class SelfCapture { // expected-note 5 {{class 'SelfCapture' does not conform to the 'Sendable' protocol}}
func fooDirect() {
C.f {
use(self)
// expected-warning@-1 {{capture of 'self' with non-sendable type 'SelfCapture' in a '@Sendable' closure}}
// expected-warning@-2 {{implicit capture of 'self' requires that 'SelfCapture' conforms to 'Sendable'}}
}
}

func fooThroughClosure() {
C.f {
{ use(self) }()
// expected-warning@-1 {{capture of 'self' with non-sendable type 'SelfCapture' in a '@Sendable' closure}}
// expected-warning@-2 {{capture of 'self' with non-sendable type 'SelfCapture' in an isolated closure}}
// expected-warning@-3 {{implicit capture of 'self' requires that 'SelfCapture' conforms to 'Sendable'}}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ void doSomethingConcurrently(__attribute__((noescape)) void SWIFT_SENDABLE (^blo
-(void) testWithCompletion: (void (^)(void)) completion;
@end

@interface TestSelfCapture : NSObject
+ (void)doWithCompletion:(void(^)(void)) completion;
@end

#pragma clang assume_nonnull end

//--- main.swift
Expand Down Expand Up @@ -219,3 +223,25 @@ extension TestDR {
@_dynamicReplacement(for: test(completion:))
func __replaceObjCFunc(_: @escaping () -> Void) {} // Ok
}

class SelfCapture { // expected-note 5 {{class 'SelfCapture' does not conform to the 'Sendable' protocol}}
static func use(_ closure: @autoclosure () -> Any) {
}

func testDirect() {
TestSelfCapture.do {
Self.use(self)
// expected-warning@-1 {{capture of 'self' with non-sendable type 'SelfCapture' in a '@Sendable' closure}}
// expected-warning@-2 {{implicit capture of 'self' requires that 'SelfCapture' conforms to 'Sendable'}}
}
}

func testThroughClosure() {
TestSelfCapture.do {
let _ = { Self.use(self) }()
// expected-warning@-1 {{capture of 'self' with non-sendable type 'SelfCapture' in a '@Sendable' closure}}
// expected-warning@-2 {{capture of 'self' with non-sendable type 'SelfCapture' in an isolated closure}}
// expected-warning@-3 {{implicit capture of 'self' requires that 'SelfCapture' conforms to 'Sendable'}}
}
}
}