Skip to content

Commit 3f37fc0

Browse files
authored
Merge pull request #42016 from angela-laar/async_inout_capture
[Concurrency] Improve diagnostic for inout params with Sendable closures
2 parents ff5f1b9 + e1e008c commit 3f37fc0

File tree

4 files changed

+22
-1
lines changed

4 files changed

+22
-1
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4587,6 +4587,10 @@ ERROR(concurrent_access_of_local_capture,none,
45874587
"%select{mutation of|reference to}0 captured %1 %2 in "
45884588
"concurrently-executing code",
45894589
(bool, DescriptiveDeclKind, DeclName))
4590+
ERROR(concurrent_access_of_inout_param,none,
4591+
"mutable capture of 'inout' parameter %0 is not allowed in "
4592+
"concurrently-executing code",
4593+
(DeclName))
45904594
ERROR(non_sendable_capture,none,
45914595
"capture of %1 with non-sendable type %0 in a `@Sendable` closure",
45924596
(Type, DeclName))

lib/Sema/TypeCheckConcurrency.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2567,6 +2567,13 @@ namespace {
25672567
return false;
25682568
}
25692569

2570+
if (auto param = dyn_cast<ParamDecl>(value)){
2571+
if(param->isInOut()){
2572+
ctx.Diags.diagnose(loc, diag::concurrent_access_of_inout_param, param->getName());
2573+
return true;
2574+
}
2575+
}
2576+
25702577
// Otherwise, we have concurrent access. Complain.
25712578
ctx.Diags.diagnose(
25722579
loc, diag::concurrent_access_of_local_capture,

test/Concurrency/async_tasks.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,3 +128,12 @@ func test_detached_throwing() async -> String {
128128
print("caught: \(error)")
129129
}
130130
}
131+
132+
// ==== Detached Tasks with inout Params---------------------------------------
133+
@available(SwiftStdlib 5.1, *)
134+
func printOrderNumber(n: inout Int) async {
135+
Task.detached {
136+
n+=1 //expected-error {{mutable capture of 'inout' parameter 'n' is not allowed in concurrently-executing code}}
137+
print(n) //expected-error {{mutable capture of 'inout' parameter 'n' is not allowed in concurrently-executing code}}
138+
}
139+
}

test/Concurrency/taskgroup_cancelAll_from_child.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ func test_taskGroup_cancelAll() async {
1313

1414
group.spawn {
1515
group.cancelAll() //expected-warning{{capture of 'group' with non-sendable type 'TaskGroup<Int>' in a `@Sendable` closure}}
16-
//expected-error@-1{{reference to captured parameter 'group' in concurrently-executing code}}
16+
//expected-error@-1{{mutable capture of 'inout' parameter 'group' is not allowed in concurrently-executing code}}
17+
1718
return 0
1819
}
1920
group.spawn { [group] in

0 commit comments

Comments
 (0)