Skip to content

Commit 9f0dda5

Browse files
committed
Add a diagnostic group for the diagnostic about non-Sendable metatypes.
This is a new restriction that folks are sure to run into, so provide it with some actionable documentation. Fixes rdar://152450956.
1 parent 7472a0c commit 9f0dda5

File tree

3 files changed

+64
-1
lines changed

3 files changed

+64
-1
lines changed

include/swift/AST/DiagnosticGroups.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ GROUP(PropertyWrappers, "property-wrapper-requirements")
6060
GROUP(ProtocolTypeNonConformance, "protocol-type-non-conformance")
6161
GROUP(ResultBuilderMethods, "result-builder-methods")
6262
GROUP(SendableClosureCaptures, "sendable-closure-captures")
63+
GROUP(SendableMetatypes, "sendable-metatypes")
6364
GROUP(SendingRisksDataRace, "sending-risks-data-race")
6465
GROUP(StrictLanguageFeatures, "strict-language-features")
6566
GROUP(StrictMemorySafety, "strict-memory-safety")

include/swift/AST/DiagnosticsSema.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5717,7 +5717,7 @@ ERROR(non_sendable_isolated_capture,none,
57175717
"capture of %1 with non-Sendable type %0 in an isolated "
57185718
"%select{local function|closure}2",
57195719
(Type, DeclName, bool))
5720-
ERROR(non_sendable_metatype_capture,none,
5720+
GROUPED_ERROR(non_sendable_metatype_capture,SendableMetatypes,none,
57215721
"capture of non-Sendable type %0 in an isolated "
57225722
"%select{local function|closure}1",
57235723
(Type, bool))
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# Sendable metatypes
2+
3+
Types that are shared in concurrent code generally need to conform to `Sendable`. The same is true in generic code when sharing parameters of a generic parameter `T`. For example, the given code will produce an error under strict concurrency checking
4+
5+
```swift
6+
func doSomethingElsewhere<T>(_ value: T) {
7+
Task { @concurrent in
8+
print(value) // warning: capture of non-Sendable type 'T'
9+
}
10+
}
11+
```
12+
13+
because `value` can have a non-Sendable type that is not safe to share. To address this potential data race, the type `T` can be marked as `Sendable`:
14+
15+
```swift
16+
func doSomethingElsewhere<T: Sendable>(_ value: T) {
17+
Task { @concurrent in
18+
print(value)
19+
}
20+
}
21+
```
22+
23+
The same issue can occur when passing the type `T` itself, rather than a value of type `T`. The compiler will indicate such problems by noting that the metatype of `T`, spelled `T.Type`, is not `Sendable`:
24+
25+
```swift
26+
protocol P {
27+
static func doSomething()
28+
}
29+
30+
func doSomethingStatic<T: P>(_: T.Type) {
31+
Task { @concurrent in
32+
T.doSomething() // warning: capture of non-Sendable type 'T.Type' in an isolated closure
33+
}
34+
}
35+
```
36+
37+
In these cases, the type parameter should be required to conform to the `SendableMetatype` protocol, e.g.,
38+
39+
```swift
40+
func doSomethingStatic<T: P & SendableMetatype>(_: T.Type) {
41+
Task { @concurrent in
42+
T.doSomething()
43+
}
44+
}
45+
```
46+
47+
The `SendableMetatype` requirement allows the function to share the type `T` in concurrent code. To maintain data race safety, it prevents callers from using isolated conformances in the call. For example, the following code will be rejected due to a data race:
48+
49+
```swift
50+
@MainActor
51+
class C: @MainActor P {
52+
static func doSomething() { }
53+
}
54+
55+
@MainActor
56+
func test(c: C) {
57+
doSomethingStatic(C.self) // error: main actor-isolated conformance of 'C' to 'P' cannot satisfy conformance requirement for a 'Sendable' type parameter
58+
}
59+
60+
```
61+
62+
The conformance of `C` to `P` can only be used on the main actor, so it cannot be provided to `doSomethingStatic`, which calls the conformance from a different concurrent task.

0 commit comments

Comments
 (0)