Skip to content

[Concurrency] Don't strip @Sendable when determining the actor isolation of a closure in a @preconcurrency context. #72437

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
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
15 changes: 7 additions & 8 deletions lib/Sema/TypeCheckConcurrency.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -675,10 +675,6 @@ static bool isSendableClosure(
if (forActorIsolation && explicitClosure->inheritsActorContext()) {
return false;
}

if (explicitClosure->isIsolatedByPreconcurrency() &&
!shouldDiagnoseExistingDataRaces(closure->getParent()))
return false;
}

if (auto type = closure->getType()) {
Expand Down Expand Up @@ -3692,13 +3688,16 @@ namespace {
bool checkLocalCapture(
ConcreteDeclRef valueRef, SourceLoc loc, DeclRefExpr *declRefExpr) {
auto value = valueRef.getDecl();
auto *dc = getDeclContext();

// Check whether we are in a context that will not execute concurrently
// with the context of 'self'. If not, it's safe.
if (!mayExecuteConcurrentlyWith(
getDeclContext(), findCapturedDeclContext(value)))
if (!mayExecuteConcurrentlyWith(dc, findCapturedDeclContext(value)))
return false;

SendableCheckContext sendableBehavior(dc);
auto limit = sendableBehavior.defaultDiagnosticBehavior();

// Check whether this is a local variable, in which case we can
// determine whether it was safe to access concurrently.
if (auto var = dyn_cast<VarDecl>(value)) {
Expand Down Expand Up @@ -3738,7 +3737,7 @@ namespace {
loc, diag::concurrent_access_of_local_capture,
parent.dyn_cast<LoadExpr *>(),
var)
.warnUntilSwiftVersion(6);
.limitBehaviorUntilSwiftVersion(limit, 6);
return true;
}

Expand All @@ -3748,7 +3747,7 @@ namespace {

func->diagnose(diag::local_function_executed_concurrently, func)
.fixItInsert(func->getAttributeInsertionLoc(false), "@Sendable ")
.warnUntilSwiftVersion(6);
.limitBehaviorUntilSwiftVersion(limit, 6);

// Add the @Sendable attribute implicitly, so we don't diagnose
// again.
Expand Down
35 changes: 35 additions & 0 deletions test/Concurrency/actor_data_race_checks_minimal.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// RUN: %target-run-simple-swift(-Xfrontend -disable-availability-checking -enable-actor-data-race-checks -swift-version 5 -strict-concurrency=minimal) | %FileCheck %s

// REQUIRES: executable_test
// REQUIRES: concurrency
// REQUIRES: concurrency_runtime

@preconcurrency @MainActor
protocol P {
func requirement()
}

class C: P {
func requirement() {
call {
print("don't crash!")
}
}

var task: Task<Void, Never>?

@preconcurrency func call(closure: @escaping @Sendable () -> Void) {
task = Task.detached {
closure()
}
}

func wait() async {
await task?.value
}
}

// CHECK: don't crash!
let c = C()
c.requirement()
await c.wait()