# \[Pitch\] Staging in \`Sendable\` checking

**URL:** <https://forums.swift.org/t/pitch-staging-in-sendable-checking/51341>\
**Category:** Pitches\
**Created:** [August 19, 2021, 7:01pm UTC](https://forums.swift.org/t/pitch-staging-in-sendable-checking/51341 "2021-08-19T19:01:44Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![Douglas\_Gregor](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/douglas_gregor/32/1362_2.png) [@Douglas\_Gregor](https://forums.swift.org/u/Douglas_Gregor)\
**Post date:** [August 19, 2021, 7:01pm UTC](https://forums.swift.org/t/pitch-staging-in-sendable-checking/51341/1 "2021-08-19T19:01:44Z")

</div>

Hi all!

[SE-0302](https://github.com/apple/swift-evolution/blob/main/proposals/0302-concurrent-value-and-concurrent-closures.md) introduced the `Sendable` protocol, which is used to indicate which types have values that can safely be copied across actors or, more generally, into any context where a copy of the value might be used concurrently with the original. Applied uniformly to all Swift code, `Sendable` checking eliminates a large class of data races caused by shared mutable state. Swift 5.5 does not perform complete checking for `Sendable` because doing so resulted in so many compiler errors and diagnostics that it undermined the usability of the feature.

I think we can stage in `Sendable` checking to improve data-race safety over time without. We propose two principles to guide the design of staged `Sendable` checking:

- Incremental adoption of concurrency should introduce incrementally more `Sendable` checking.
- `Sendable` problems outside of the user's module should not block progress nor produce an undue annotation burden.

Here is a [proposal](https://github.com/DougGregor/swift-evolution/blob/sendable-staging/proposals/nnnn-sendable-staging.md) formalizing this staging mechanism. Thoughts?

Doug

---

<div class="post-metadata">

**Author:** ![Jumhyn](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jumhyn/32/3891_2.png) [@Jumhyn](https://forums.swift.org/u/Jumhyn)\
**Post date:** [August 19, 2021, 8:27pm UTC](https://forums.swift.org/t/pitch-staging-in-sendable-checking/51341/2 "2021-08-19T20:27:31Z")

</div>

Overall this looks pretty good to me. I'm somewhat bothered by the situation in **Module-sensitive `Sendable` checking** , though:

```swift
// module A (has not adopted concurrency)
public struct Point3D {
  var x, y, z: Double
}

// module B
import A

struct Sphere { // okay: we allow Sphere to be inferred as Sendable
  var center: Point3D // ... because Point3D is from a module that hasn't adopted concurrency yet
  var radius: Double
}

let sphere = getSphere()
Task {
  print(sphere) // okay: Sphere is Sendable
}

```

First, would the same apply if `Point3D` were a class (perhaps with all its visible properties being `Sendable` and immutable)?

But even beyond that question, it feels weird to me that we would infer a completely implicit conformance to `Sendable` that we have _no way of knowing_ is correct. I have a couple alternatives in mind, which hopefully mitigate the issues with `@unchecked Sendable` noted in the **Motivation** section:

1. Allow types such as `Sphere` to conform to `Sendable` explicitly, but without requiring `@unchecked`. I.e., the snippet above would be an error, but it would be fine to declare `Sphere` as:

2. If annotating types like `Sphere` is considered too burdensome, we could move the same solution one level up—allow the user to declare `extension Point3D: Sendable {}` without `@unchecked` when `Point3D` is from a module that has not yet adopted concurrency. If module A did adopt concurrency without marking `Point3D` as `Sendable`, then `extension Point3D: Sendable` would become an error—module B would have to declare the conformance `@unchecked` or remove the conformance.

---

<div class="post-metadata">

**Author:** ![Douglas\_Gregor](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/douglas_gregor/32/1362_2.png) [@Douglas\_Gregor](https://forums.swift.org/u/Douglas_Gregor)\
**Post date:** [August 20, 2021, 11:04pm UTC](https://forums.swift.org/t/pitch-staging-in-sendable-checking/51341/3 "2021-08-20T23:04:30Z")

</div>

> [@Jumhyn](#):
>
> First, would the same apply if `Point3D` were a class (perhaps with all its visible properties being `Sendable` and immutable)?

Yes. I'm suggesting uniform rules for struct/enum/class. We could consider something more heuristic-y: struct/enum types are more likely to be okay than class types, so we could be more strict about class types. But that punishes correctly-synchronized or immutable classes in modules that haven't adopted concurrency yet, so it isn't a clear win.

> [@Jumhyn](#):
>
> Allow types such as `Sphere` to conform to `Sendable` explicitly, but without requiring `@unchecked` . I.e., the snippet above would be an error, but it would be fine to declare `Sphere` as:
> 
> ```swift
> struct Sphere: Sendable { 
> var center: Point3D
> var radius: Double
> }
> 
> ```
> 
> I'm guessing this was considered and rejected under the principle that " `Sendable` problems outside of the user's module should not [...] produce an undue annotation burden," but I'd be curious to know if there's a more concrete idea in mind of what the threshold for "undue" is.

Interesting idea! It nudges you to think about whether `Point3D` itself is likely to be `Sendable`. My main concern here is that you will not need the `Sendable` annotation when `Point3D`'s module gets updated for Concurrency, so we have an intermediate step that requires more annotation than the final step. We've created poor experiences for developers before where we made their code worse (more annotations) that later became unnecessary, and it didn't go well. [SE-0160](https://github.com/apple/swift-evolution/blob/main/proposals/0160-objc-inference.md) and [SE-0025](https://github.com/apple/swift-evolution/blob/main/proposals/0025-scoped-access-level.md) both ended up like this, and I'm not sure we should do that again.

FWIW, I don't have an idea for a concrete threshold for "undue". It may come down to experimenting with apps a bit to see how many errors and warnings we get in an app when we flip the switch.

> [@Jumhyn](#):
>
> If annotating types like `Sphere` is considered too burdensome, we could move the same solution one level up—allow the user to declare `extension Point3D: Sendable {}` without `@unchecked` when `Point3D` is from a module that has not yet adopted concurrency. If module A did adopt concurrency without marking `Point3D` as `Sendable` , then `extension Point3D: Sendable` would become an error—module B would have to declare the conformance `@unchecked` or remove the conformance.

Also interesting! My concern from above applies, because the `extension Point3D: Sendable { }` will be extraneous after its module is updated, but I like that this documents the assumption and provides us with a place that we can diagnose when the assumption is known to be wrong.

If a user wanted to get diagnostics about types from modules that haven't adopted concurrency, this would be a way to silence them.

Doug

---

<div class="post-metadata">

**Author:** ![Jumhyn](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jumhyn/32/3891_2.png) [@Jumhyn](https://forums.swift.org/u/Jumhyn)\
**Post date:** [August 21, 2021, 12:25am UTC](https://forums.swift.org/t/pitch-staging-in-sendable-checking/51341/4 "2021-08-21T00:25:49Z")

</div>

> [@Douglas\_Gregor](#):
>
> But that punishes correctly-synchronized or immutable classes in modules that haven't adopted concurrency yet, so it isn't a clear win.

My entirely non-empirical hypothesis is that this doesn't apply to the vast majority of classes in existing Swift codebases, and among the libraries which vend classes that _have_ gone to the trouble of properly self-synchronizing, most of them will be diligent about adopting concurrency. So, that's to say that if we decide to go the annotation-free route, I'd definitely lean toward this "heuristic-y" strategy. Of course, I strongly agree with this for informing our approach 🙂:

> [@Douglas\_Gregor](#):
>
> It may come down to experimenting with apps a bit to see how many errors and warnings we get in an app when we flip the switch.

I don't love the idea an app which has adopted concurrency and is compiling in the Swift 6 language mode won't receive _any_ indication that the type they're using may or may not be concurrency safe. IMO it undermines the whole idea that in Swift 6, code is "safe by default" from data races. When importing a arbitrary library, I may or may not know whether it has adopted concurrency. Suppose we have a setup such as the one called out in the proposal:

```swift
// module A, no concurrency yet
public struct Point3D {
  var x, y, z: Double
  private let changeCounter = Counter() // shared mutable state!
}

// module B
import A

struct Sphere { // OK, Sphere is Sendable
  var center: Point3D
  var radius: Double
}

```

Now, suppose that the author of module B writes some code that depends on the (completely implicit!) `Sendable` conformance of `Sphere` while also interacting with module A in a way that makes `Point3D` concurrency-unsafe, and they run into a data race bug.

Who in the chain has committed an error? Certainly not the author module A—they wrote their code before concurrency features were available, and did the best they could at the time. I also think the author module B can't really be said to have committed any error—they're using Swift 6, which is "safe by default" from data races, and their code compiled without any errors or ignored warnings. I think it would be a reasonable assumption on their part that their code, as written, _was_ safe from data races.

The only other option for where the error lies, in my view, is in the language/documentation, either for promising "safe by default" or for failing to deliver on that promise.

* * *

> [@Douglas\_Gregor](#):
>
> My main concern here is that you will not need the `Sendable` annotation when `Point3D` 's module gets updated for Concurrency, so we have an intermediate step that requires more annotation than the final step.

This is definitely a worthwhile concern. The solution requiring the user to extend `Point3D` explicitly at least has the advantage that all such conformances that module A requires could be confined to a single file (that later gets deleted), while requiring the annotation of types like `Sphere` would result in those extraneous annotations being distributed throughout the codebase.

But in either situation, the impact here of the extraneous annotations (if I'm understanding correctly) is notably more limited than the situations resulting from either SE-0160 or SE-0025: both of these potentially resulted in extraneous annotations on a potentially unbounded number of _members_ within each affected type. The extraneous annotations here will be strictly limited to one per type.

"One per type" is, IMO, an acceptably low burden to be able to properly deliver on the "safe by default" promise in Swift 6.

I realize that for most people this transition step will be entirely ephemeral, but it's also worth considering that it's likely the case that there are apps in the wild depending on libraries/frameworks that have been totally abandoned, and might _never_ adopt concurrency. Clients of these libraries, then, would _never_ be able to get the "safe by default" behavior, unless we wanted to adopt even more aggressive rules in Swift 7.

---

<div class="post-metadata">

**Author:** ![Jumhyn](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jumhyn/32/3891_2.png) [@Jumhyn](https://forums.swift.org/u/Jumhyn)\
**Post date:** [August 21, 2021, 12:48am UTC](https://forums.swift.org/t/pitch-staging-in-sendable-checking/51341/5 "2021-08-21T00:48:44Z")

</div>

Missed this on the first pass:

> [@Douglas\_Gregor](#):
>
> If a user wanted to get diagnostics about types from modules that haven't adopted concurrency, this would be a way to silence them.

Yeah, I like that idea too. I'd be totally fine with a regime where the Swift 6 default was "warn about `Sendable` assumptions for pre-concurrency modules, silence by declaring an explicit conformance."

---

<div class="post-metadata">

**Author:** ![samsonjs](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/samsonjs/32/36989_2.png) [@samsonjs](https://forums.swift.org/u/samsonjs)\
**Post date:** [August 21, 2021, 6:15pm UTC](https://forums.swift.org/t/pitch-staging-in-sendable-checking/51341/6 "2021-08-21T18:15:07Z")

</div>

As an app developer this seems like a great proposal to me.

---

<div class="post-metadata">

**Author:** ![langford](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/langford/32/5411_2.png) [@langford](https://forums.swift.org/u/langford)\
**Post date:** [August 21, 2021, 6:51pm UTC](https://forums.swift.org/t/pitch-staging-in-sendable-checking/51341/7 "2021-08-21T18:51:32Z")

</div>

I believe what you have is a “proposal to beat”.

A couple questions on the complexities of transitive dependencies and builds:

Does this multiple annotation approach work the way you intend for libraries which are clients to programs which are also clients of libraries also used by that program?

Does it durably do that across the many ways that happens (vendoring from large corp APIs, cocoapods including ones used in multiple target projects including watch and tvOS, and less esoteric ones as well)?

Eg:

iOS App Timr007:  
Dep-\> LibJ  
Dep-\> LibP

WatchOS StandAlone Timr007Wristpanion:  
Dep-\> LibJ  
Dep-\> LibP

LibP:  
Dep-\> LibJ

Like will this all just come out with all of LibP and LibJ’s type as @\_unsafeSendable until they eventually drop their pre-concurrency clients?

Will unsafeSendable be hobbled for must have analysis tools, effectively shortening its useful lifespan so much this whole approach becomes relatively expensive for the benefit?

I worry “which compiler flags should we use” is going to be fairly painful for companies using a mix of dependencies. Systems like cocoapods will end up having to figure out how to specify a lot of those, and not just for the “treat warnings as errors” crowd. I also worry about autocomplete’s  
ability to handle this kind of issue.

At runtime/other compile times: I am also worried about managing this across test and development targets which have differences in their dependency lists. Think like when they sometimes add analytics or debug tools, or mock libraries, and things like that.

Worst case I could see is some testing dependency inscrutably making behavior semantically different in an app’s test’s behavior due to the fact it included or did not include some pre-concurrency library, and all the tests being useless then, which the team could perhaps not even notice.

Testing with other deps could also have us having to make a bunch of extra declarations that are hand written copies of a bunch of interfaces, AND fight the compiler about it having “heisensendable” protocol conformances in the worst case.

If this proposal does fail to produce good types for the “apps that use libraries which are dependencies of their own libraries”, are there some more user configurable/half measures to get those libs and apps across the gap here?

Can a small amount of code-gen or hand-edited protocol conformances simplify the overall proposal to avoid compiler flags for libraries? Or is there at least a way a person can promote a type from @unsafeSendable to Sendable in client code and have that honored?

Your comment about how you can’t make non-sendable objects sendable makes me worry. I fear we’ll get backed into a corner of dependency hell we cannot program our way out of, or 3rd party APIs that are incredibly hard to make “play nice” for years after we are otherwise ready to live the Swift6 life.

---

<div class="post-metadata">

**Author:** ![jrose](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jrose/32/408_2.png) [@jrose](https://forums.swift.org/u/jrose)\
**Post date:** [August 23, 2021, 3:42am UTC](https://forums.swift.org/t/pitch-staging-in-sendable-checking/51341/8 "2021-08-23T03:42:24Z")

</div>

This mostly seems good, the same sort of cross-language cross-module reasoning that normally goes with swift-versions. This part didn’t make sense to me, though:

> The `@unsafeSendable` attribute indicates that the corresponding parameter (of function type) is intended to be `@Sendable` going forward. For a client that has not yet adopted concurrency, it will be treated as a non-`@Sendable` function type.

Why would a library author not _always_ use this annotation? And if they would, can that just be the behavior of plain old Sendable?

---

<div class="post-metadata">

**Author:** ![Douglas\_Gregor](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/douglas_gregor/32/1362_2.png) [@Douglas\_Gregor](https://forums.swift.org/u/Douglas_Gregor)\
**Post date:** [August 23, 2021, 5:35am UTC](https://forums.swift.org/t/pitch-staging-in-sendable-checking/51341/9 "2021-08-23T05:35:42Z")

</div>

> [@Jumhyn](#):
>
> My entirely non-empirical hypothesis is that this doesn't apply to the vast majority of classes in existing Swift codebases, and among the libraries which vend classes that _have_ gone to the trouble of properly self-synchronizing, most of them will be diligent about adopting concurrency. So, that's to say that if we decide to go the annotation-free route, I'd definitely lean toward this "heuristic-y" strategy.

Understood. And it's likely that a class is probably an order of magnitude more likely to have non-`Sendable` semantics, so the heuristic is probably well-justified.

> [@Jumhyn](#):
>
> I don't love the idea an app which has adopted concurrency and is compiling in the Swift 6 language mode won't receive _any_ indication that the type they're using may or may not be concurrency safe. IMO it undermines the whole idea that in Swift 6, code is "safe by default" from data races.

I agree that Swift 6 should be more noisy about missing `Sendable` conformances. Perhaps Swift 5.x ignores missing `Sendable` conformances from Swift 5.x code that has adopted concurrency, but Swift 6 warns about such conformances.

> [@Jumhyn](#):
>
> "One per type" is, IMO, an acceptably low burden to be able to properly deliver on the "safe by default" promise in Swift 6.

People have a _lot_ of types, though. [swift-driver](https://github.com/apple/swift-driver) is not very big, but it has over a hundred structs. SwiftUI has almost 600 public structs. I think it's necessary to do these annotations for Swift 6, but there is a cost here and we need to be wary of that.

> [@Jumhyn](#):
>
> Yeah, I like that idea too. I'd be totally fine with a regime where the Swift 6 default was "warn about `Sendable` assumptions for pre-concurrency modules, silence by declaring an explicit conformance."

Agreed. One additional tweak we could make is to produce an error when a module declares a `Sendable` conformance for a type from another module, e.g,:

```swift
import A

// Point3D is from module A
extension Point3D: @unchecked Sendable { }

```

when that module `A` adopts concurrency, if `Point3D` is not declared `Sendable` there, we should error. That way, if we were wrong about our assumption that this type should be `Sendable`, we'll learn about it.

Doug

---

<div class="post-metadata">

**Author:** ![Douglas\_Gregor](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/douglas_gregor/32/1362_2.png) [@Douglas\_Gregor](https://forums.swift.org/u/Douglas_Gregor)\
**Post date:** [August 23, 2021, 5:46am UTC](https://forums.swift.org/t/pitch-staging-in-sendable-checking/51341/10 "2021-08-23T05:46:27Z")

</div>

> [@langford](#):
>
> Does this multiple annotation approach work the way you intend for libraries which are clients to programs which are also clients of libraries also used by that program?

It generally only considers two modules at a time: the module doing the `Sendable` check, and the module that declared the type that doesn't conform to `Sendable`, so I think it scales.

> [@langford](#):
>
> Like will this all just come out with all of LibP and LibJ’s type as @\_unsafeSendable until they eventually drop their pre-concurrency clients?

`@unsafeSendable` is generally for closure parameters that need to be `@Sendable`, but yes---if there are pre-concurrency clients, one should use it.

> [@langford](#):
>
> Will unsafeSendable be hobbled for must have analysis tools, effectively shortening its useful lifespan so much this whole approach becomes relatively expensive for the benefit?

It'll be treated as `@Sendable` in any code that adopts concurrency, so I don't think it's lifespan is that short. `@unsafeSendable` can become `@Sendable` when pre-concurrency clients go away (or if they can be updated at the same time).

> [@langford](#):
>
> I worry “which compiler flags should we use” is going to be fairly painful for companies using a mix of dependencies. Systems like cocoapods will end up having to figure out how to specify a lot of those, and not just for the “treat warnings as errors” crowd. I also worry about autocomplete’s  
> ability to handle this kind of issue.

I don't see a reason to worry about autocomplete specifically. However, what I'm proposing is effectively a new dialect in between Swift 5.5 and Swift 6, which _does_ worry me. We've tried fairly hard not to have dialects in Swift other than the language base version, which has been at 5 for ~2.5 years, and we don't have a whole lot of opt-in warning flags. This would be a big one, and I have some concerns that a significant part of the ecosystem could be stuck in this middle dialect.

> [@langford](#):
>
> Can a small amount of code-gen or hand-edited protocol conformances simplify the overall proposal to avoid compiler flags for libraries? Or is there at least a way a person can promote a type from @unsafeSendable to Sendable in client code and have that honored?
> 
> Your comment about how you can’t make non-sendable objects sendable makes me worry.

I think there might be a misunderstanding of `Sendable` conformances here. You can make a type `Sendable` even if it has instance data that isn't `Sendable` using `@unchecked`, e.g.,

```swift
class StoredData {
  // ...
}

struct Data: @unchecked Sendable { // okay! we told the compiler to trust us, it's Sendable
  var stored: StoredData // not Sendable
}

```

The above can generalize to an `UnsafeSendable<T>` type (or even property wrapper) that lets you box up a non-`Sendable` type in an unsafe `Sendable` shell to work around issues when you're stuck or you have some other kind of synchronization.

Doug

> [@jrose](#):
>
> > The `@unsafeSendable` attribute indicates that the corresponding parameter (of function type) is intended to be `@Sendable` going forward. For a client that has not yet adopted concurrency, it will be treated as a non- `@Sendable` function type.
> 
> Why would a library author not _always_ use this annotation? And if they would, can that just be the behavior of plain old Sendable?

You would only use this annotation for old APIs that have pre-concurrency clients. If you're introducing new API, you should start your clients out right with the concurrency checking.

I'm beginning to dislike `@unsafeSendable`, because I see it's causing confusion. Instead, I'm starting to prefer that declaration-level `@predatesConcurrency` approach that will strip out concurrency-related constraints (`T: Sendable`, `@Sendable`, `@MainActor`, etc.) when the API is used in a pre-concurrency client.

Doug

---

<div class="post-metadata">

**Author:** ![gwendal.roue](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/gwendal.roue/32/13992_2.png) [@gwendal.roue](https://forums.swift.org/u/gwendal.roue)\
**Post date:** [August 23, 2021, 6:06am UTC](https://forums.swift.org/t/pitch-staging-in-sendable-checking/51341/11 "2021-08-23T06:06:37Z")

</div>

Hello,

The proposal distinguishes two kinds of module:

> We propose to phase in checking for `Sendable` by distinguishing modules that have fully adopted concurrency from those that have not. [...]

Importing a module that does not adopt concurrency introduces some unsafely:

> When using a type from a module that has not adopted concurrency, the lack of a `Sendable` conformance does not produce an error. Instead, the compiler will produce a warning, or even suppress the diagnostic entirely.

The above paragraph is augmented with:

> [@Douglas\_Gregor](#):
>
> Yes. I'm suggesting uniform rules for struct/enum/class.

In this context, I can imagine a third stage: the library author wants to delay concurrency adoption in a module (for some good reason), but wants to prevent incorrect `Sendable` inference.

Is it a scenario worth considering? What about letting the author of such a module annotate types that are known to be non-sendable, in order to prevent the compiler from inferring this conformance?

---

<div class="post-metadata">

**Author:** ![Jumhyn](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jumhyn/32/3891_2.png) [@Jumhyn](https://forums.swift.org/u/Jumhyn)\
**Post date:** [August 23, 2021, 2:29pm UTC](https://forums.swift.org/t/pitch-staging-in-sendable-checking/51341/12 "2021-08-23T14:29:46Z")

</div>

> [@Douglas\_Gregor](#):
>
> Understood. And it's likely that a class is probably an order of magnitude more likely to have non- `Sendable` semantics, so the heuristic is probably well-justified.

Yep this is my expectation as well. 👍

> [@Douglas\_Gregor](#):
>
> Perhaps Swift 5.x ignores missing `Sendable` conformances from Swift 5.x code that has adopted concurrency, but Swift 6 warns about such conformances.

That seems reasonable to me. There's already the expectation that Swift 5.x is _not_ safe by default especially when it comes to `Sendable` checking.

> [@Douglas\_Gregor](#):
>
> People have a _lot_ of types, though. [swift-driver](https://github.com/apple/swift-driver) is not very big, but it has over a hundred structs. SwiftUI has almost 600 public structs. I think it's necessary to do these annotations for Swift 6, but there is a cost here and we need to be wary of that.

Ah, yeah, I really only meant this as a comparison to the extraneous annotation situations for `@objc` and access control modifiers. For `Sendable` checking, I agree that if the typical annotation burden really was one for _every_ type in either the imported module or the client module, it would be quite large. But since this situation only arises where a type from the client module directly includes a type from the imported module, I expect that the typical project will _not_ need to annotate each and every type.

> [@Douglas\_Gregor](#):
>
> One additional tweak we could make is to produce an error when a module declares a `Sendable` conformance for a type from another module

Hm, so the Swift 6 rule would be "don't conform types you don't own to `Sendable`"? I don't think that causes major issues for inferring `Sendable` for aggregates (since the aggregate itself can just be tagged `@unchecked Sendable`), but it would prevent the _direct_ usage of `Point3D` as `Sendable` in the case where I somehow _know_ that it is concurrency-safe and that the author of module A perhaps just forgot to add the conformance.

I suppose you could make a simple `@unchecked Sendable` wrapper around `Point3D` in that case though, and it won't always be possible for a client to tell the difference between "non-`Sendable` because author forgot" and "non-`Sendable` because it may become concurrency-unsafe in the future," even with access to the source.

> [@gwendal.roue](#):
>
> What about letting the author of such a module annotate types that are known to be non-sendable, in order to prevent the compiler from inferring this conformance?

I think this came up during the discussion of SE-0302, but the implicit conformance only applies to non-public or frozen types, since non-public types aren't part of the module API and frozen types can't validly add/change members in a way that would break `Sendable` inference.

---

<div class="post-metadata">

**Author:** ![Douglas\_Gregor](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/douglas_gregor/32/1362_2.png) [@Douglas\_Gregor](https://forums.swift.org/u/Douglas_Gregor)\
**Post date:** [August 23, 2021, 7:53pm UTC](https://forums.swift.org/t/pitch-staging-in-sendable-checking/51341/13 "2021-08-23T19:53:15Z")

</div>

> [@gwendal.roue](#):
>
> In this context, I can imagine a third stage: the library author wants to delay concurrency adoption in a module (for some good reason), but wants to prevent incorrect `Sendable` inference.
> 
> Is it a scenario worth considering? What about letting the author of such a module annotate types that are known to be non-sendable, in order to prevent the compiler from inferring this conformance?

This came up in the review of SE-0302, because `Sendable` is inferred structurally for non-public struct and enum types, and there is currently no way to disable the inference. I'm inclined toward using (un)availability on the conformance to spell "this is intentionally not `Sendable`", rather than invent something new, e.g.,

```swift
public struct MyType : @available(unavailable, *) Sendable {
  var x, y: Double
  // some day we may add some shared state here
}

```

To your use case, if a module explicitly declares an unavailable `Sendable` conformance, importing modules should complain if they need `MyType` to be `Sendable`.

If every type is explicitly marked as `Sendable` or having unavailable `Sendable` conformance, then there's no observable difference in importing modules as to whether the module has adopted concurrency or not.

> [@Jumhyn](#):
>
> > [@Douglas\_Gregor](#):
> >
> > One additional tweak we could make is to produce an error when a module declares a `Sendable` conformance for a type from another module
> 
> Hm, so the Swift 6 rule would be "don't conform types you don't own to `Sendable` "? I don't think that causes major issues for inferring `Sendable` for aggregates (since the aggregate itself can just be tagged `@unchecked Sendable` ), but it would prevent the _direct_ usage of `Point3D` as `Sendable` in the case where I somehow _know_ that it is concurrency-safe and that the author of module A perhaps just forgot to add the conformance.

Oh, that's not what I meant. I don't want to ban this:

```swift
import A

// Point3D is from module A
extension Point3D: @unchecked Sendable { }

```

I want to turn it into an error if `A` adopts concurrency and doesn't make `Point3D` `Sendable`:

```swift
// module A adopts concurrency
public struct Point3D { ... } // not Sendable

```

Doug

---

<div class="post-metadata">

**Author:** ![Jumhyn](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jumhyn/32/3891_2.png) [@Jumhyn](https://forums.swift.org/u/Jumhyn)\
**Post date:** [August 23, 2021, 8:11pm UTC](https://forums.swift.org/t/pitch-staging-in-sendable-checking/51341/14 "2021-08-23T20:11:19Z")

</div>

> [@Douglas\_Gregor](#):
>
> Oh, that's not what I meant. I don't want to ban this:
> 
> ```swift
> import A
> 
> // Point3D is from module A
> extension Point3D: @unchecked Sendable { }
> 
> ```
> 
> I want to turn it into an error if `A` adopts concurrency and doesn't make `Point3D` `Sendable` :
> 
> ```swift
> // module A adopts concurrency
> public struct Point3D { ... } // not Sendable
> 
> ```

But after the concurrency transition period, wouldn't we _eventually_ end up in a world where the rule is "don't conform types you don't own to `Sendable`"?

Once ~all libraries have adopted concurrency, an arbitrary public type `X` will either be declared to conform to `Sendable` in its own module, in which case `extension X: @unchecked Sendable` should give a "redundant conformance" error, or `X` _won't_ conform to `Sendable` in which case you're proposing `extension X: @unchecked Sendable` _still_ be an error (unless I'm still misunderstanding?).

---

<div class="post-metadata">

**Author:** ![Douglas\_Gregor](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/douglas_gregor/32/1362_2.png) [@Douglas\_Gregor](https://forums.swift.org/u/Douglas_Gregor)\
**Post date:** [August 23, 2021, 10:05pm UTC](https://forums.swift.org/t/pitch-staging-in-sendable-checking/51341/15 "2021-08-23T22:05:12Z")

</div>

> [@Jumhyn](#):
>
> But after the concurrency transition period, wouldn't we _eventually_ end up in a world where the rule is "don't conform types you don't own to `Sendable` "?
> 
> Once ~all libraries have adopted concurrency, an arbitrary public type `X` will either be declared to conform to `Sendable` in its own module, in which case `extension X: @unchecked Sendable` should give a "redundant conformance" error, or `X` _won't_ conform to `Sendable` in which case you're proposing `extension X: @unchecked Sendable` _still_ be an error (unless I'm still misunderstanding?).

Yes to all of that.

Doug

---

<div class="post-metadata">

**Author:** ![Philippe\_Hausler](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/philippe_hausler/32/22569_2.png) [@Philippe\_Hausler](https://forums.swift.org/u/Philippe_Hausler)\
**Post date:** [August 23, 2021, 11:40pm UTC](https://forums.swift.org/t/pitch-staging-in-sendable-checking/51341/16 "2021-08-23T23:40:57Z")

</div>

So for protocols and generics of existing types, how can we stage in the requirements on existing things to require sendable. E.g. all Elements passed in AsyncSequence should be Sendable; but we couldn't put that in due to the fact that hardly any type was Sendable at that point of intro. I am sure this is true for a number of other APIs.

---

<div class="post-metadata">

**Author:** ![langford](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/langford/32/5411_2.png) [@langford](https://forums.swift.org/u/langford)\
**Post date:** [August 24, 2021, 9:43pm UTC](https://forums.swift.org/t/pitch-staging-in-sendable-checking/51341/17 "2021-08-24T21:43:34Z")

</div>

Thank you for clarifying. I'm still personally worried about the pairwise module based checking. Modules are a fine abstraction, but I fear actual library mechanics for how and when code is compiled and by whom will make a hash of this all come transitive dependencies.

> [@Douglas\_Gregor](#):
>
> > [@langford](#):
> >
> > I worry “which compiler flags should we use” is going to be fairly painful for companies using a mix of dependencies. Systems like cocoapods will end up having to figure out how to specify a lot of those, and not just for the “treat warnings as errors” crowd. I also worry about autocomplete’s
> > 
> > > ability to handle this kind of issue.
> 
> I don't see a reason to worry about autocomplete specifically.

Autocomplete has historically had high fragility in and around closures, just giving up. As a good deal of the sendable stuff is related to closure params, I worry about a sendable and non-sendable double entry in an autocomplete data structure gumming up closure autocomplete. It can also get a bit frisky around the combination of generics + closures. Besides auto-complete, I worry about the compiler itself getting bothered by these definitions that may conflict.

> [@Douglas\_Gregor](#):
>
> > [@langford](#):
> >
> > Does this multiple annotation approach work the way you intend for libraries which are clients to programs which are also clients of libraries also used by that program?
> 
> It generally only considers two modules at a time: the module doing the `Sendable` check, and the module that declared the type that doesn't conform to `Sendable` , so I think it scales.

If we look at only the context of the "checker" and the "checked", I see deep hierarchies coming out possibly still with both sendable and unsafeSendable interfaces about the same types/closure signatures.

AppGamma:  
LibB  
LibC  
LibD  
LibF

LibB:  
LibD  
LibE  
LibC

LibC: // This is preconcurrent code  
LibE  
LibQ

LibF: // This is a library which explicitly tries to work for preConcurreny code and swift 6.  
LibE  
LibQ

Given the above situation here is where it seems to be trouble. If LibE had closure taking APIs, or even types used in closures a lot, specific ways of packaging and compiling LibB, LibC, LibF and LibE could all result in "duplicate but slightly different" signatures with respect to the Sendable interface. This seems possible given even the same LibE codebase in each.

Like:

```swift
let cc = LibC.Cicle()
let fc = LibF.FracturedCircle() // this of type circle
let bc = LibB.BumpyCircle()

let arrayOfCircles = [cc, fc, bc] //I'm pretty sure y'all would catch it if it didn't compile here during development of the feature

//I'm also pretty sure this situation is going to be handled alright
libF.funcFThatRequiresArrayOfCircles(arrayOfCircles)
libC.funcCThatRequiresArrayOfCircles(arrayOfCircles)
libB.funcBThatRequiresArrayOfCircles(arrayOfCircles)

```

but when we get to stuff more like this:

```swift
//in AppGamma
struct Peg<T:Round>{
 ...
}

SomeVaguelyConcurrencyRelatedApp.doStuff{ 

    fc.checkArea(cc) { peg1
       cc.checkArea(fc) { peg2
         peg1 == peg2
       }
    }

}

```

This is where I greatly fear we hit weird compilation error messages like "Circle is not of type Circle" compiler errors, even if generics aren't involved.

If you take AppGamma and LibC, LibF or LibE are removed, say in a testing situation, or just because it's no longer needed, do the types change at the AppGamma level?

This all seems like a very large testing matrix for the implementation of your proposal, with a lot of guessing at how various providers of Swift libraries could slightly blow up everything. I'm not talking about falsely marking non-concurrent code here; I'm just talking about the code compiling.

Apologies if I really am missing something there, but this seems like a very real world composition problem that I'm not sure the changes to Error + the function/closure attribute + the type inference + compiler flags would all sail through necessarily. I feel it could strand some apps pre-concurrency due to a hairy library slightly doing something wrong.

--

On another note, of sincere feedback about the parts that are confusing iffy feeling, and in the spirit of frank feedback on what I could clearly understand from the document, not second guessing your decisions:

The slight difference in Sendable the protocol and @Sendable functions is also tricky. They maybe feel like they mean something different. Perhaps this is just the "new taste" of marker protocols, and they are really "protocols that act more like closure parameter attributes". Perhaps it's just calling marker protocols "protocols" is the issue, time will tell probably. Not trying to bike shed, but tell what it feels like from the outside.

Like if you look at @noescape vs @escaping, the second one marks code that "isn't safe" to flexibly use however. One of the reasons in the [original proposal](https://github.com/apple/swift-evolution/blob/main/proposals/0103-make-noescape-default.md) to assume @noescape was that humans are likely to fail to notice that they made something escape. This allows the compiler to go "hey person, you are not typing right, and this extra thingy can go up here" via a fixit. You can't as easily do that with @sendable or @unsafeSendable or @predatesConcurrency, for instance. If you said why the "no escape" variant is chosen as the default but not in the escaping situation perhaps it would have just all made sense, but I'm not sure.

Is "Sendibility" the right property to be annotating closures/functions with? I don't know.

It's "dual" doesn't have an obviously better name. I am not sure I'd love the name "@nonSendableAllowed", but it's what I mean. Concurrency is pretty hard for humans, and I'd imagine it's even harder for y'all to not only model concurrency, but how we all will fail repeatedly while trying to do it right. Would the dual better focus us on "how we messed up" so we could better write concurrent code? Or specifically having to mark things "@doesNotCareIfConcurrent", "@acceptsJanky"?

@unsafeSendable feels like it might be okay. @preConcurrency may work but isn't super clear on "why we care", while possibly not accurate from the perspective of a person writing a library at some time in the future. Sorry to be both unhelpful on this front and still confused.

@libraryTryingToDoBothConcurrencyAndHandleOldStuffWellSoPleaseGetOfItsBack is a little wordy, but probably closer to what this is all about. It's really weird to deal with varying answers to "Is this library guaranteeing safe, concurrent types", "Is this library trying to use safe, concurrent types" and "Is this library only able to provide safe concurrent types if it is also provided them".

I do not envy you in both having to figure out a technical answer to all of this and then teach us all how and what to think. Best of luck, this feels most of the way there.

---

<div class="post-metadata">

**Author:** ![Douglas\_Gregor](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/douglas_gregor/32/1362_2.png) [@Douglas\_Gregor](https://forums.swift.org/u/Douglas_Gregor)\
**Post date:** [September 8, 2021, 5:41am UTC](https://forums.swift.org/t/pitch-staging-in-sendable-checking/51341/18 "2021-09-08T05:41:47Z")

</div>

> [@Philippe\_Hausler](#):
>
> So for protocols and generics of existing types, how can we stage in the requirements on existing things to require sendable. E.g. all Elements passed in AsyncSequence should be Sendable; but we couldn't put that in due to the fact that hardly any type was Sendable at that point of intro. I am sure this is true for a number of other APIs.

Normally, one could not go back and retroactively add constraints to an associated type, protocol, or generic type. However, marker protocols like `Sendable` have no ABI impact in those positions, so adding a constraint to (say) make the `Element` generic parameter of `AsyncStream` `Sendable` could break source compatibility, but it won't break binary compatibility. And with the approach to staging in `Sendable` checking described by this proposal, it won't break source compatibility either until "Swift 6". You can see where we recently added [`Sendable` to the Task APIs](https://github.com/apple/swift/pull/38866). It sounds to me like we need to follow up with at least `AsyncStream` to make its `Element` type `Sendable` (because the closure is `@Sendable`). I don't think `AsyncSequence` wants to force its element type to be `Sendable`, though, because you could certainly have sequences that are asynchronous but never transfer values across actors/concurrency domains.

> [@langford](#):
>
> If you take AppGamma and LibC, LibF or LibE are removed, say in a testing situation, or just because it's no longer needed, do the types change at the AppGamma level?

They shouldn't, no. The notion of "adopts concurrency" is on a module-by-module basis, and does not interact with modules other than the importing module and the imported module.

> [@langford](#):
>
> Apologies if I really am missing something there, but this seems like a very real world composition problem that I'm not sure the changes to Error + the function/closure attribute + the type inference + compiler flags would all sail through necessarily. I feel it could strand some apps pre-concurrency due to a hairy library slightly doing something wrong.

It's certainly not a trivial undertaking, but we've already been using this general approach in Swift 5.5 in a more narrow way, and it seems to work. For example, the iOS 15 SDK annotates `UIView` as being on the main actor with the equivalent of "predates concurrency". Let's get the compiler to complain about the type of `UIView.addConstraint`, like this:

```swift
let _: Int = UIView.addConstraint

```

In existing code, the error will be that a value of type `(UIView) -> (NSLayoutConstraint) -> Void` is not convertible to `Int`. However, if you put that into code that uses concurrency, like this:

```swift
func g() async {
  let _: Int = UIView.addConstraint
}

```

the error will be that a value of type `(UIView) -> @MainActor (NSLayoutConstraint) -> Void` cannot be converted to `Int`. So, we're already doing a "predates concurrency" type translation similar to what is described in this proposal, for all code out there that's using UIKit and AppKit with the iOS 15/macOS 12/etc. SDKs.

> [@langford](#):
>
> The slight difference in Sendable the protocol and @Sendable functions is also tricky. They maybe feel like they mean something different.

SE-0302 covers this, but a `@Sendable` function type conforms to `Sendable`. There are some semantic rules applied to `@Sendable` functions/closures to ensure that they meet the semantic requirements of `Sendable`. That's why we use the same name in both places.

> [@langford](#):
>
> Like if you look at @noescape vs @escaping, the second one marks code that "isn't safe" to flexibly use however. One of the reasons in the [original proposal](https://github.com/apple/swift-evolution/blob/main/proposals/0103-make-noescape-default.md) to assume @noescape was that humans are likely to fail to notice that they made something escape. This allows the compiler to go "hey person, you are not typing right, and this extra thingy can go up here" via a fixit.

`@Sendable` is designed in a similar manner. The default is that a function is non-`@Sendable`, which means that you'll get a compiler error or warning if you try to use that function in a manner that requires it to be `Sendable`, e.g.,

```swift
func f(fn: @escaping () -> Void ) {
  Task {
    fn() // warning/error: cannot use parameter 'fn' with a non-sendable type '() -> Void' from concurrently-executed code
         // note: a function type must be marked '@Sendable' to conform to 'Sendable'
  }
}

```

So, you'll be guided to the right solution by the compiler.

`@predatesConcurrency` and `@unsafeSendable` are harder, but they're also more niche features for folks who are providing source compatibility with pre-concurrency clients.

**EDIT** : we'll have an updated proposal coming in a week or so that will extend the "staging" to also cover global actors (like my `@MainActor` example above) and work through some additional compatibility issues. Thanks for all of your comments so far, folks.

Doug
