Skip to content

[Concurrency] Improve diagnostic for inout params with Sendable closures #42016

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 2 commits into from
Mar 25, 2022
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
4 changes: 4 additions & 0 deletions include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -4566,6 +4566,10 @@ ERROR(concurrent_access_of_local_capture,none,
"%select{mutation of|reference to}0 captured %1 %2 in "
"concurrently-executing code",
(bool, DescriptiveDeclKind, DeclName))
ERROR(concurrent_access_of_inout_param,none,
"mutable capture of 'inout' parameter %0 is not allowed in "
"concurrently-executing code",
(DeclName))
ERROR(non_sendable_capture,none,
"capture of %1 with non-sendable type %0 in a `@Sendable` closure",
(Type, DeclName))
Expand Down
7 changes: 7 additions & 0 deletions lib/Sema/TypeCheckConcurrency.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2553,6 +2553,13 @@ namespace {
return false;
}

if (auto param = dyn_cast<ParamDecl>(value)){
if(param->isInOut()){
ctx.Diags.diagnose(loc, diag::concurrent_access_of_inout_param, param->getName());
return true;
}
}

// Otherwise, we have concurrent access. Complain.
ctx.Diags.diagnose(
loc, diag::concurrent_access_of_local_capture,
Expand Down
9 changes: 9 additions & 0 deletions test/Concurrency/async_tasks.swift
Original file line number Diff line number Diff line change
Expand Up @@ -128,3 +128,12 @@ func test_detached_throwing() async -> String {
print("caught: \(error)")
}
}

// ==== Detached Tasks with inout Params---------------------------------------
@available(SwiftStdlib 5.1, *)
func printOrderNumber(n: inout Int) async {
Task.detached {
n+=1 //expected-error {{mutable capture of 'inout' parameter 'n' is not allowed in concurrently-executing code}}
print(n) //expected-error {{mutable capture of 'inout' parameter 'n' is not allowed in concurrently-executing code}}
}
}
3 changes: 2 additions & 1 deletion test/Concurrency/taskgroup_cancelAll_from_child.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ func test_taskGroup_cancelAll() async {

group.spawn {
group.cancelAll() //expected-warning{{capture of 'group' with non-sendable type 'TaskGroup<Int>' in a `@Sendable` closure}}
//expected-error@-1{{reference to captured parameter 'group' in concurrently-executing code}}
//expected-error@-1{{mutable capture of 'inout' parameter 'group' is not allowed in concurrently-executing code}}

return 0
}
group.spawn { [group] in
Expand Down