You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[rbi] Remove code that caused us to misidentify certain captured parameters as sending.
Specifically, this code was added because otherwise we would in swift 5 +
strict-concurrency mode emit two warnings, one at the AST level and one at the
SIL level. Once we are in swift-6 mode, this does not happen since we stop
compiling at the AST level since we will emit the AST level diagnostic as an
error.
To do this, we tried to pattern match what the AST was erroring upon and treat
the parameter as disconnected instead of being isolated. Sadly, this resulted in
us treating certain closure cases incorrectly and not emit a diagnostic
(creating a concurrency hole).
Given that this behavior results in a bad diagnostic only to avoid emitting two
diagnostics in a mode which is not going to last forever... it really doesn't
make sense to keep it. We really need a better way to handle these sorts of
issues. Perhaps a special semantic parameter put on the function that squelches
certain errors. But that is something for another day. The specific case it
messes up is:
```
class NonSendable {
func action() async {}
}
@mainactor
final class Foo {
let value = NonSendable()
func perform() {
Task { [value] in
await value.action() // Should emit error but do not.
}
}
}
```
In this case, we think that value is sending... when it isnt and we should emit
an error.
rdar://146378329
Task{ // expected-tns-warning {{passing closure as a 'sending' parameter risks causing data races between code in the current task and concurrent execution of the closure}}
467
+
completion() // expected-tns-note {{closure captures 'completion' which is accessible to code in the current task}}
468
468
// expected-warning@-1 {{capture of 'completion' with non-Sendable type '@MainActor () -> Void' in a '@Sendable' closure}}
469
469
// expected-warning@-2 {{capture of 'completion' with non-Sendable type '@MainActor () -> Void' in an isolated closure}}
470
470
// expected-note@-3 2 {{a function type must be marked '@Sendable' to conform to 'Sendable'}}
471
471
// expected-warning@-4 {{expression is 'async' but is not marked with 'await'; this is an error in the Swift 6 language mode}}
472
472
// expected-note@-5 {{calls to parameter 'completion' from outside of its actor context are implicitly asynchronous}}
473
+
// expected-complete-and-tns-warning @-7 {{passing closure as a 'sending' parameter risks causing data races between code in the current task and concurrent execution of the closure}}
474
+
// expected-complete-and-tns-note @-7 {{closure captures 'completion' which is accessible to code in the current task}}
Copy file name to clipboardExpand all lines: test/Concurrency/transfernonsendable.swift
+23-5Lines changed: 23 additions & 5 deletions
Original file line number
Diff line number
Diff line change
@@ -1671,13 +1671,12 @@ extension MyActor {
1671
1671
_ =self
1672
1672
_ = sc
1673
1673
1674
-
Task{ // expected-warning {{sending value of non-Sendable type '() async -> ()' risks causing data races}}
1675
-
// expected-note @-1 {{Passing value of non-Sendable type '() async -> ()' as a 'sending' argument to initializer 'init(name:priority:operation:)' risks causing races in between local and caller code}}
1676
-
_ = sc
1674
+
Task{ // expected-warning {{passing closure as a 'sending' parameter risks causing data races between 'self'-isolated code and concurrent execution of the closure}}
Task{ // expected-note {{access can happen concurrently}}
1680
-
_ = sc
1678
+
Task{ // expected-warning {{passing closure as a 'sending' parameter risks causing data races between 'self'-isolated code and concurrent execution of the closure}}
// expected-ni-note @-1 {{sending nonisolated(nonsending) task-isolated 'x' to nonisolated global function 'useValueAsyncConcurrent' risks causing data races between nonisolated and nonisolated(nonsending) task-isolated uses}}
2002
2001
// expected-ni-ns-note @-2 {{sending task-isolated 'x' to @concurrent global function 'useValueAsyncConcurrent' risks causing data races between @concurrent and task-isolated uses}}
2003
2002
}
2003
+
2004
+
func avoidThinkingClosureParameterIsSending(){
2005
+
@MainActor
2006
+
finalclassFoo{
2007
+
letvalue=NonSendableKlass()
2008
+
2009
+
func perform(){
2010
+
Task{[value]in
2011
+
await value.asyncCall() // expected-ni-warning {{sending 'value' risks causing data races}}
2012
+
// expected-ni-note @-1 {{sending main actor-isolated 'value' to nonisolated instance method 'asyncCall()' risks causing data races between nonisolated and main actor-isolated uses}}
2013
+
}
2014
+
2015
+
Task{
2016
+
await value.asyncCall() // expected-ni-warning {{sending 'self.value' risks causing data races}}
2017
+
// expected-ni-note @-1 {{sending main actor-isolated 'self.value' to nonisolated instance method 'asyncCall()' risks causing data races between nonisolated and main actor-isolated uses}}
Copy file name to clipboardExpand all lines: test/Concurrency/transfernonsendable_typed_errors.swift
+4-5Lines changed: 4 additions & 5 deletions
Original file line number
Diff line number
Diff line change
@@ -61,13 +61,12 @@ extension MyActor {
61
61
_ =self
62
62
_ = sc
63
63
64
-
Task{ // expected-error {{sending value of non-Sendable type '() async -> ()' risks causing data races}}
65
-
// expected-note @-1 {{Passing value of non-Sendable type '() async -> ()' as a 'sending' argument to initializer 'init(name:priority:operation:)' risks causing races in between local and caller code}}
66
-
_ = sc
64
+
Task{ // expected-error {{passing closure as a 'sending' parameter risks causing data races between 'self'-isolated code and concurrent execution of the closure}}
Task{ // expected-note {{access can happen concurrently}}
70
-
_ = sc
68
+
Task{ // expected-error {{passing closure as a 'sending' parameter risks causing data races between 'self'-isolated code and concurrent execution of the closure}}
0 commit comments