Skip to content

[6.0][Concurrency] Diagnose captures of self in a task created in deinit. #75227

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
Jul 15, 2024
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
3 changes: 3 additions & 0 deletions include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -5520,6 +5520,9 @@ ERROR(non_sendable_isolated_capture,none,
ERROR(implicit_async_let_non_sendable_capture,none,
"capture of %1 with non-sendable type %0 in 'async let' binding",
(Type, DeclName))
ERROR(self_capture_deinit_task,none,
"capture of 'self' in a closure that outlives deinit",
())
ERROR(implicit_non_sendable_capture,none,
"implicit capture of %1 requires that %0 conforms to `Sendable`",
(Type, DeclName))
Expand Down
20 changes: 19 additions & 1 deletion lib/Sema/TypeCheckConcurrency.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2793,6 +2793,25 @@ namespace {
if (capture.isOpaqueValue())
continue;

auto *closure = localFunc.getAbstractClosureExpr();

// Diagnose a `self` capture inside an escaping `sending`
// `@Sendable` closure in a deinit, which almost certainly
// means `self` would escape deinit at runtime.
auto *explicitClosure = dyn_cast_or_null<ClosureExpr>(closure);
auto *dc = getDeclContext();
if (explicitClosure && isa<DestructorDecl>(dc) &&
!explicitClosure->getType()->isNoEscape() &&
(explicitClosure->isPassedToSendingParameter() ||
explicitClosure->isSendable())) {
auto var = dyn_cast_or_null<VarDecl>(capture.getDecl());
if (var && var->isSelfParameter()) {
ctx.Diags.diagnose(explicitClosure->getLoc(),
diag::self_capture_deinit_task)
.warnUntilSwiftVersion(6);
}
}

// If the closure won't execute concurrently with the context in
// which the declaration occurred, it's okay.
auto decl = capture.getDecl();
Expand All @@ -2817,7 +2836,6 @@ namespace {
if (type->hasError())
continue;

auto *closure = localFunc.getAbstractClosureExpr();
if (closure && closure->isImplicit()) {
auto *patternBindingDecl = getTopPatternBindingDecl();
if (patternBindingDecl && patternBindingDecl->isAsyncLet()) {
Expand Down
37 changes: 37 additions & 0 deletions test/Concurrency/self_escapes_deinit.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// RUN: %target-typecheck-verify-swift -strict-concurrency=complete -disable-availability-checking

@MainActor
class C {
let x: Int = 0

deinit {
// expected-warning@+1 {{capture of 'self' in a closure that outlives deinit; this is an error in the Swift 6 language mode}}
Task { @MainActor in
_ = self
}

// expected-warning@+1 {{capture of 'self' in a closure that outlives deinit; this is an error in the Swift 6 language mode}}
Task {
_ = x
}
}
}

func enqueueSomewhereElse(_ closure: @escaping @Sendable () -> Void) {}

@MainActor
class C2 {
let x: Int = 0

deinit {
// expected-warning@+1 {{capture of 'self' in a closure that outlives deinit; this is an error in the Swift 6 language mode}}
enqueueSomewhereElse {
_ = self
}

// expected-warning@+1 {{capture of 'self' in a closure that outlives deinit; this is an error in the Swift 6 language mode}}
enqueueSomewhereElse {
_ = self.x
}
}
}