Skip to content

[Concurrency] Disallow Sendable annotation on methods of non-Sendable types #69177

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
Oct 17, 2023
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 @@ -5302,6 +5302,9 @@ ERROR(local_function_executed_concurrently,none,
ERROR(sendable_isolated_sync_function,none,
"%0 synchronous %kind1 cannot be marked as '@Sendable'",
(ActorIsolation, const ValueDecl *))
ERROR(nonsendable_instance_method,none,
"instance methods of non-Sendable types cannot be marked as '@Sendable'",
())
ERROR(concurrent_access_of_local_capture,none,
"%select{mutation of|reference to}0 captured %kind1 in "
"concurrently-executing code",
Expand Down
11 changes: 11 additions & 0 deletions lib/Sema/TypeCheckAttr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6614,6 +6614,8 @@ void AttributeChecker::visitKnownToBeLocalAttr(KnownToBeLocalAttr *attr) {

void AttributeChecker::visitSendableAttr(SendableAttr *attr) {

auto dc = D->getDeclContext();

if ((isa<AbstractFunctionDecl>(D) || isa<AbstractStorageDecl>(D)) &&
!isAsyncDecl(cast<ValueDecl>(D))) {
auto value = cast<ValueDecl>(D);
Expand All @@ -6625,6 +6627,15 @@ void AttributeChecker::visitSendableAttr(SendableAttr *attr) {
.warnUntilSwiftVersion(6);
}
}
// Prevent Sendable Attr from being added to methods of non-sendable types
if (auto *funcDecl = dyn_cast<AbstractFunctionDecl>(D)) {
if (auto selfdecl = funcDecl->getImplicitSelfDecl()) {
if (!isSendableType(dc->getParentModule(), selfdecl->getTypeInContext())) {
diagnose(attr->getLocation(), diag::nonsendable_instance_method)
.warnUntilSwiftVersion(6);
}
}
}
}

void AttributeChecker::visitNonisolatedAttr(NonisolatedAttr *attr) {
Expand Down
18 changes: 18 additions & 0 deletions test/Concurrency/sendable_functions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,24 @@ actor A {
}
}

class NonSendableC {
var x: Int = 0

@Sendable func inc() { // expected-warning{{instance methods of non-Sendable types cannot be marked as '@Sendable'}}
x += 1
}
}

struct S<T> {
let t: T

@Sendable func test() {} // expected-warning{{instance methods of non-Sendable types cannot be marked as '@Sendable'}}
}

extension S: Sendable where T: Sendable {
@Sendable func test2() {}
}

@available(SwiftStdlib 5.1, *)
@MainActor @Sendable func globalActorFunc() { } // expected-warning{{main actor-isolated synchronous global function 'globalActorFunc()' cannot be marked as '@Sendable'}}

Expand Down