Skip to content

Commit 4d4a228

Browse files
authored
Merge pull request #41368 from DougGregor/disable-sendable-warnings
Disable implicit Sendable warnings.
2 parents db6eeb9 + 9886063 commit 4d4a228

8 files changed

+31
-37
lines changed

CHANGELOG.md

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -103,26 +103,6 @@ Swift 5.6
103103
}
104104
```
105105

106-
* [SE-0302][]:
107-
108-
Swift will now produce warnings to indicate potential data races when
109-
non-`Sendable` types are passed across actor or task boundaries. For
110-
example:
111-
112-
```swift
113-
class MyCounter {
114-
var value = 0
115-
}
116-
117-
func f() -> MyCounter {
118-
let counter = MyCounter()
119-
Task {
120-
counter.value += 1 // warning: capture of non-Sendable type 'MyCounter'
121-
}
122-
return counter
123-
}
124-
```
125-
126106
* [SE-0331][]:
127107

128108
The conformance of the unsafe pointer types (e.g., `UnsafePointer`,

lib/Sema/TypeCheckConcurrency.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -750,6 +750,14 @@ DiagnosticBehavior SendableCheckContext::diagnosticBehavior(
750750

751751
auto defaultBehavior = defaultDiagnosticBehavior();
752752

753+
// If we're in Swift 5 without -warn-concurrency, suppress the diagnostic
754+
// entirely. This is the partial rollback of SE-0302
755+
ASTContext &ctx = fromDC->getASTContext();
756+
if (!ctx.isSwiftVersionAtLeast(6) && !ctx.LangOpts.WarnConcurrency &&
757+
(!conformanceCheck ||
758+
*conformanceCheck == SendableCheck::ImpliedByStandardProtocol))
759+
defaultBehavior = DiagnosticBehavior::Ignore;
760+
753761
// If we are checking an implicit Sendable conformance, don't suppress
754762
// diagnostics for declarations in the same module. We want them so make
755763
// enclosing inferred types non-Sendable.
@@ -776,6 +784,15 @@ static bool diagnoseSingleNonSendableType(
776784
behavior = fromContext.diagnosticBehavior(nominal);
777785
} else {
778786
behavior = fromContext.defaultDiagnosticBehavior();
787+
788+
// If we're in Swift 5 without -warn-concurrency, suppress the diagnostic
789+
// entirely. This is the partial rollback of SE-0302
790+
ASTContext &ctx = fromContext.fromDC->getASTContext();
791+
if (!ctx.isSwiftVersionAtLeast(6) && !ctx.LangOpts.WarnConcurrency &&
792+
(!fromContext.conformanceCheck ||
793+
*fromContext.conformanceCheck ==
794+
SendableCheck::ImpliedByStandardProtocol))
795+
behavior = DiagnosticBehavior::Ignore;
779796
}
780797

781798
bool wasSuppressed = diagnose(type, behavior);

test/Concurrency/async_tasks.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,6 @@ func test_unsafeThrowingContinuations() async throws {
8989

9090
// ==== Sendability ------------------------------------------------------------
9191
class NotSendable { }
92-
// expected-note@-1{{class 'NotSendable' does not conform to the 'Sendable' protocol}}
9392

9493
@available(SwiftStdlib 5.1, *)
9594
func test_nonsendableContinuation() async throws {
@@ -99,7 +98,7 @@ func test_nonsendableContinuation() async throws {
9998

10099
let _: NotSendable = try await withUnsafeThrowingContinuation { continuation in
101100
Task {
102-
continuation.resume(returning: NotSendable()) // expected-warning{{capture of 'continuation' with non-sendable type 'UnsafeContinuation<NotSendable, Error>' in a `@Sendable` closure}}
101+
continuation.resume(returning: NotSendable())
103102
}
104103
}
105104
}

test/Concurrency/sendable_checking.swift

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ extension NS1: Sendable { }
1111
// expected-note@-1 4{{conformance of 'NS1' to 'Sendable' has been explicitly marked unavailable here}}
1212

1313
@available(SwiftStdlib 5.1, *)
14-
struct NS2 { // expected-note{{consider making struct 'NS2' conform to the 'Sendable' protocol}}
14+
struct NS2 {
1515
var ns1: NS1
1616
}
1717

@@ -22,7 +22,7 @@ struct NS3 { }
2222
extension NS3: Sendable { }
2323

2424
@available(SwiftStdlib 5.1, *)
25-
class NS4 { } // expected-note{{class 'NS4' does not conform to the 'Sendable' protocol}}
25+
class NS4 { }
2626

2727
@available(SwiftStdlib 5.1, *)
2828
func acceptCV<T: Sendable>(_: T) { }
@@ -53,12 +53,11 @@ func testCV(
5353
) async {
5454
acceptCV(ns1) // expected-warning{{conformance of 'NS1' to 'Sendable' is unavailable}}
5555
acceptCV(ns1array) // expected-warning{{conformance of 'NS1' to 'Sendable' is unavailable}}
56-
acceptCV(ns2) // expected-warning{{type 'NS2' does not conform to the 'Sendable' protocol}}
56+
acceptCV(ns2)
5757
acceptCV(ns3) // expected-warning{{conformance of 'NS3' to 'Sendable' is only available in macOS 11.0 or newer}}
5858
// expected-note@-1{{add 'if #available' version check}}
59-
acceptCV(ns4) // expected-warning{{type 'NS4' does not conform to the 'Sendable' protocol}}
60-
acceptCV(fn) // expected-warning{{type '() -> Void' does not conform to the 'Sendable' protocol}}
61-
// expected-note@-1{{a function type must be marked '@Sendable' to conform to 'Sendable'}}
59+
acceptCV(ns4)
60+
acceptCV(fn)
6261
acceptSendableFn(fn) // expected-error{{passing non-sendable parameter 'fn' to function expecting a @Sendable closure}}
6362
}
6463

test/Concurrency/sendable_module_checking.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ actor A {
1515
func testA(a: A) async {
1616
_ = await a.f() // CHECK: warning: cannot call function returning non-sendable type '[StrictStruct : NonStrictClass]' across actors}}
1717
// CHECK: note: struct 'StrictStruct' does not conform to the 'Sendable' protocol
18-
// CHECK: note: class 'NonStrictClass' does not conform to the 'Sendable' protocol
1918
}
2019

2120
extension NonStrictStruct: @unchecked Sendable { }

test/Concurrency/sendable_preconcurrency.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ actor A {
1212
func f() -> [StrictStruct: NonStrictClass] { [:] }
1313
}
1414

15-
class NS { } // expected-note 2{{class 'NS' does not conform to the 'Sendable' protocol}}
15+
class NS { } // expected-note {{class 'NS' does not conform to the 'Sendable' protocol}}
1616

1717
struct MyType {
1818
var nsc: NonStrictClass
@@ -29,7 +29,7 @@ struct MyType3 {
2929

3030
func testA(ns: NS, mt: MyType, mt2: MyType2, mt3: MyType3) async {
3131
Task {
32-
print(ns) // expected-warning{{capture of 'ns' with non-sendable type 'NS' in a `@Sendable` closure}}
32+
print(ns)
3333
print(mt) // no warning: MyType is Sendable because we suppressed NonStrictClass's warning
3434
print(mt2)
3535
print(mt3)

test/Concurrency/sendable_without_preconcurrency.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,22 @@ actor A {
1212
func f() -> [StrictStruct: NonStrictClass] { [:] }
1313
}
1414

15-
class NS { } // expected-note{{class 'NS' does not conform to the 'Sendable' protocol}}
15+
class NS { }
1616

1717
struct MyType {
1818
var nsc: NonStrictClass
1919
}
2020

21-
struct MyType2 { // expected-note{{consider making struct 'MyType2' conform to the 'Sendable' protocol}}
21+
struct MyType2 {
2222
var nsc: NonStrictClass
2323
var ns: NS
2424
}
2525

2626
func testA(ns: NS, mt: MyType, mt2: MyType2) async {
2727
Task {
28-
print(ns) // expected-warning{{capture of 'ns' with non-sendable type 'NS' in a `@Sendable` closure}}
28+
print(ns)
2929
print(mt) // no warning: MyType is Sendable because we suppressed NonStrictClass's warning
30-
print(mt2) // expected-warning{{capture of 'mt2' with non-sendable type 'MyType2' in a `@Sendable` closure}}
30+
print(mt2)
3131
}
3232
}
3333

test/Concurrency/sendable_without_preconcurrency_2.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ actor A {
1212
func f() -> [StrictStruct: NonStrictClass] { [:] }
1313
}
1414

15-
class NS { } // expected-note 2{{class 'NS' does not conform to the 'Sendable' protocol}}
15+
class NS { } // expected-note{{class 'NS' does not conform to the 'Sendable' protocol}}
1616

1717
struct MyType {
1818
var nsc: NonStrictClass
@@ -25,7 +25,7 @@ struct MyType2: Sendable {
2525

2626
func testA(ns: NS, mt: MyType, mt2: MyType2) async {
2727
Task {
28-
print(ns) // expected-warning{{capture of 'ns' with non-sendable type 'NS' in a `@Sendable` closure}}
28+
print(ns)
2929
print(mt) // no warning: MyType is Sendable because we suppressed NonStrictClass's warning
3030
print(mt2)
3131
}

0 commit comments

Comments
 (0)