Skip to content

[Concurrency] Downgrade actor_isolated_mutating_func to a warning in a narrow case that was previously accepted. #69330

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 23, 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
11 changes: 9 additions & 2 deletions lib/Sema/TypeCheckConcurrency.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2760,7 +2760,8 @@ namespace {
return false;

bool result = false;
auto diagnoseIsolatedInoutState = [this, call, isPartialApply, &result](
bool downgradeToWarning = false;
auto diagnoseIsolatedInoutState = [&](
ConcreteDeclRef declRef, SourceLoc argLoc) {
auto decl = declRef.getDecl();
auto isolation = getActorIsolationForReference(decl, getDeclContext());
Expand All @@ -2777,7 +2778,8 @@ namespace {
ValueDecl *fnDecl = declRef->getDecl();
ctx.Diags.diagnose(apply->getLoc(),
diag::actor_isolated_mutating_func,
fnDecl->getName(), decl);
fnDecl->getName(), decl)
.warnUntilSwiftVersionIf(downgradeToWarning, 6);
result = true;
return;
}
Expand All @@ -2800,6 +2802,11 @@ namespace {
};

auto findIsolatedState = [&](Expr *expr) -> Expr * {
// This code used to not walk into InOutExpr, which allowed
// some invalid code to slip by in compilers <=5.9.
if (isa<InOutExpr>(expr))
downgradeToWarning = true;

if (LookupExpr *lookup = dyn_cast<LookupExpr>(expr)) {
if (isa<DeclRefExpr>(lookup->getBase())) {
diagnoseIsolatedInoutState(lookup->getMember().getDecl(),
Expand Down
15 changes: 15 additions & 0 deletions test/Concurrency/actor_inout_isolation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -307,3 +307,18 @@ actor ProtectArray {
}
}
}

extension Optional {
mutating func mutate() async {}
}

@available(SwiftStdlib 5.1, *)
actor ProtectDictionary {
var dict: [Int: Int] = [:]

func invalid() async {
await dict[0].mutate()
// expected-warning@-1 {{cannot call mutating async function 'mutate()' on actor-isolated property 'dict'; this is an error in Swift 6}}
// expected-targeted-complete-warning@-2 {{passing argument of non-sendable type 'inout Optional<Int>' outside of actor-isolated context may introduce data races}}
}
}