Skip to content

Commit 8a3a5bb

Browse files
committed
[Concurrency] Improve diagnostic for inout params with Sendable closures.
Error should point out any operation with mutable params is not permitted in a Sendable closure Fixes rdar://89641516
1 parent b3bc295 commit 8a3a5bb

File tree

3 files changed

+20
-0
lines changed

3 files changed

+20
-0
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4566,6 +4566,10 @@ ERROR(concurrent_access_of_local_capture,none,
45664566
"%select{mutation of|reference to}0 captured %1 %2 in "
45674567
"concurrently-executing code",
45684568
(bool, DescriptiveDeclKind, DeclName))
4569+
ERROR(concurrent_access_of_inout_param,none,
4570+
"mutable capture of 'inout' parameter %0 is not allowed in "
4571+
"concurrently-executing code",
4572+
(DeclName))
45694573
ERROR(non_sendable_capture,none,
45704574
"capture of %1 with non-sendable type %0 in a `@Sendable` closure",
45714575
(Type, DeclName))

lib/Sema/TypeCheckConcurrency.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2553,6 +2553,13 @@ namespace {
25532553
return false;
25542554
}
25552555

2556+
if (auto param = dyn_cast<ParamDecl>(value)){
2557+
if(param->isInOut()){
2558+
ctx.Diags.diagnose(loc, diag::concurrent_access_of_inout_param, param->getName());
2559+
}
2560+
return true;
2561+
}
2562+
25562563
// Otherwise, we have concurrent access. Complain.
25572564
ctx.Diags.diagnose(
25582565
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+
}

0 commit comments

Comments
 (0)