# SE-0446: Nonescapable Types

**URL:** <https://forums.swift.org/t/se-0446-nonescapable-types/74666>\
**Category:** Proposal Reviews\
**Created:** [September 17, 2024, 9:23pm UTC](https://forums.swift.org/t/se-0446-nonescapable-types/74666 "2024-09-17T21:23:43Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Joe\_Groff](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/joe_groff/32/1929_2.png) [@Joe\_Groff](https://forums.swift.org/u/Joe_Groff)\
**Post date:** [September 17, 2024, 9:23pm UTC](https://forums.swift.org/t/se-0446-nonescapable-types/74666/1 "2024-09-17T21:23:44Z")

</div>

Hi everyone. The review of [SE-0446: Nonescapable Types](https://github.com/swiftlang/swift-evolution/blob/main/proposals/0446-non-escapable.md) begins now and runs through **October 1, 2024**.

In order to try this feature out, the proposal authors have provided toolchains:

- Toolchain for macOS: [https://ci.swift.org/job/swift-PR-toolchain-macos/1498/artifact/branch-main/swift-PR-76406-1498-osx.tar.gz](https://ci.swift.org/job/swift-PR-toolchain-macos/1498/artifact/branch-main/swift-PR-76406-1498-osx.tar.gz)
- Toolchain for Linux (Ubuntu): [https://download.swift.org/tmp/pull-request/76406/1039/ubuntu2004/PR-ubuntu2004.tar.gz](https://download.swift.org/tmp/pull-request/76406/1039/ubuntu2004/PR-ubuntu2004.tar.gz)
- Toolchain for Windows: [https://ci-external.swift.org/job/swift-PR-build-toolchain-windows/5477/artifact/\*zip\*/archive.zip](https://ci-external.swift.org/job/swift-PR-build-toolchain-windows/5477/artifact/*zip*/archive.zip)

This proposal covers the first stage of support for nonescapable types; it covers the basic semantics of nonescapable types which are confined to their current scope. However, the proposal on its own provides no facility for expressing lifetime dependencies beyond that, which means that in particular it provides no safe way to initialize or return values of `~Escapable` type. In order to facilitate experimentation with this feature, the above toolchains also provide some support for the `@lifetime` attribute, which must be used to express the lifetime dependency of an `~Escapable` value produced by an initializer or return value. The exact behavior of this attribute is however not covered by this review, and may be subject to change in the future until it is separately reviewed and accepted.

Reviews are an important part of the Swift evolution process. All review feedback should be either on this forum thread or, if you would like to keep your feedback private, directly to the review manager via the forum messaging feature. When contacting the review manager directly, please keep the proposal link at the top of the message.

#### What goes into a review?

The goal of the review process is to improve the proposal under review through constructive criticism and, eventually, determine the direction of Swift. When writing your review, here are some questions you might want to answer in your review:

- What is your evaluation of the proposal?
- Is the problem being addressed significant enough to warrant a change to Swift?
- Does this proposal fit well with the feel and direction of Swift?
- If you have used other languages or libraries with a similar feature, how do you feel that this proposal compares to those?
- How much effort did you put into your review? A glance, a quick reading, or an in-depth study?

More information about the Swift evolution process is available at [https://github.com/apple/swift-evolution/blob/main/process.md](https://github.com/apple/swift-evolution/blob/main/process.md).

Thanks for contributing to Swift!

Joe Groff  
Review Manager

---

<div class="post-metadata">

**Author:** ![grynspan](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/grynspan/32/15315_2.png) [@grynspan](https://forums.swift.org/u/grynspan)\
**Post date:** [September 17, 2024, 9:54pm UTC](https://forums.swift.org/t/se-0446-nonescapable-types/74666/2 "2024-09-17T21:54:34Z")

</div>

I look forward to having non-escapable types in the language!

A bit of a niche question, but…

If I need to refer to "any metatype", I can no longer say `Any.Type` since the addition of move-only types, and must instead say `any ~Copyable.Type`. With the addition of `~Escapable`, "any metatype" becomes `any (~Copyable & ~Escapable).Type`… I think? This is only going to get more complicated—should we take a moment to consider some improved syntax for "any metatype" (since a metatype in itself is not subject to move-only or no-escape rules)?

---

<div class="post-metadata">

**Author:** ![Joe\_Groff](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/joe_groff/32/1929_2.png) [@Joe\_Groff](https://forums.swift.org/u/Joe_Groff)\
**Post date:** [September 17, 2024, 9:57pm UTC](https://forums.swift.org/t/se-0446-nonescapable-types/74666/3 "2024-09-17T21:57:49Z")

</div>

> [@grynspan](#):
>
> A bit of a niche question, but…
> 
> If I need to refer to "any metatype", I can no longer say `Any.Type` since the addition of move-only types, and must instead say `any ~Copyable.Type`. With the addition of `~Escapable`, "any metatype" becomes `any (~Copyable & ~Escapable).Type`… I think? This is only going to get more complicated—should we take a moment to consider some improved syntax for "any metatype" (since a metatype in itself is not subject to move-only or no-escape rules)?

It's tricky to do that without recreating the same problem next time we introduce some suppressible capability. A hypothetical `LiterallyAny.Type` would have to either change meaning when we introduce a new `~Capability`, which would be source breaking, or it would have to keep the meaning it had when it was introduced, meaning we need another `AbsolutelyLiterallyAny.Type` to include the new thing.

---

<div class="post-metadata">

**Author:** ![xwu](https://avatars.discourse-cdn.com/v4/letter/x/b487fb/32.png) [@xwu](https://forums.swift.org/u/xwu)\
**Post date:** [September 17, 2024, 10:11pm UTC](https://forums.swift.org/t/se-0446-nonescapable-types/74666/4 "2024-09-17T22:11:05Z")

</div>

In SE-0427, the steering group concluded, regarding the handling of then-_future_ potentially suppressible constraints (such as `Escapable`):

> that the current implementation on `main` when enabling the experimental `Escapable` feature was potentially inconsistent. However, this will only become an issue once `Escapable` (or another defaulted-but-supressible constraint) is actually introduced into the language, so discussion of that behavior can be had during the proposal review of that feature in a later Swift version.

We will need to reckon with that behavior here. On a quick scan, this proposal text as put forward for review does not include explicit language on this point, but any design will have to adhere to the key principle that existing generic constraints which _don't_ use any suppressible constraints must not change their meaning when this or another future suppressible constraint is introduced.

Additionally, we want to—if possible—avoid possibly surprising inferred combinations of suppression and non-suppression while also avoiding unnecessary incantations of an ever-longer list of `~Copyable & ~Escapable & ~SomeOtherThing`, etc.

---

<div class="post-metadata">

**Author:** ![Karl](https://avatars.discourse-cdn.com/v4/letter/k/34f0e0/32.png) [@Karl](https://forums.swift.org/u/Karl)\
**Post date:** [September 18, 2024, 12:50am UTC](https://forums.swift.org/t/se-0446-nonescapable-types/74666/5 "2024-09-18T00:50:41Z")

</div>

Two questions:

1. Will `@escaping` closures conform to the `Escapable` protocol?

2. Can we mark arbitrary function parameters as `~Escapable`?

---

<div class="post-metadata">

**Author:** ![ksluder](https://avatars.discourse-cdn.com/v4/letter/k/eada6e/32.png) [@ksluder](https://forums.swift.org/u/ksluder)\
**Post date:** [September 18, 2024, 1:00am UTC](https://forums.swift.org/t/se-0446-nonescapable-types/74666/6 "2024-09-18T01:00:47Z")

</div>

> [@xwu](#):
>
> We will need to reckon with that behavior here. On a quick scan, this proposal text as put forward for review does not include explicit language on this point, but any design will have to adhere to the key principle that existing generic constraints which _don't_ use any suppressible constraints must not change their meaning when this or another future suppressible constraint is introduced.

Does it really make sense for a function to be generic over all suppressible constraints that now or may ever exist in the future? If it were, wouldn’t `Any` have been made to imply `Any & ~Copyable` in Swift 6?

In other words, should we consider the need to specify yet another `~Constraint` to be a price that new constraint proposals have to be worth paying?

---

<div class="post-metadata">

**Author:** ![FranzBusch](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/franzbusch/32/19447_2.png) [@FranzBusch](https://forums.swift.org/u/FranzBusch)\
**Post date:** [September 18, 2024, 9:37am UTC](https://forums.swift.org/t/se-0446-nonescapable-types/74666/7 "2024-09-18T09:37:26Z")

</div>

+1 on the overall proposal. I am looking forward to how we can leverage this to build really high performance parsers and asynchronous message streams.

> The closures used in `Task.init` , `Task.detached` , or `TaskGroup.addTask` are escaping closures and therefore cannot capture nonescapable values.

I understand the constraints for `Task.init` and `Task.detached` but it is unfortunate that the same applies to `TaskGroup.addTask`. Is that really escaping? Similarly, what about `async let`s?

I can see quite a few use-cases where one might get a `Span` and wants to process independent chunks of that span in child tasks.

---

<div class="post-metadata">

**Author:** ![vns](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/vns/32/37396_2.png) [@vns](https://forums.swift.org/u/vns)\
**Post date:** [September 18, 2024, 9:44am UTC](https://forums.swift.org/t/se-0446-nonescapable-types/74666/8 "2024-09-18T09:44:54Z")

</div>

Great to see that this proposal doesn’t introduce lifetime dependencies altogether and leave it to a separate proposal.

+1 on the change, I like this direction of control Swift gives to the (especially, value-) types system.

---

<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:** [September 18, 2024, 12:29pm UTC](https://forums.swift.org/t/se-0446-nonescapable-types/74666/9 "2024-09-18T12:29:32Z")

</div>

Hello, I'd be interested in hearing thoughts about the interaction of non-`Escapable` Types and Region-Based Isolation. I known this escapes the focus of the proposal, but, well, I ask anyway.

According to [SE-0414, Rules for Merging Isolation Regions](https://github.com/swiftlang/swift-evolution/blob/main/proposals/0414-region-based-isolation.md):

> Given a function `f` with arguments `ai` and result that is assigned to  
> variable `y`:
> 
> ```swift
> y = f(a0, ..., an)
> 
> ```
> 
> 1. All regions of non-`Sendable` arguments `ai` are merged into one larger  
> region after `f` executes.
> 2. If any of `ai` are non-`Sendable` and `y` is non-`Sendable`, then `y` is in  
> the same merged region as `ai`. If all of the `ai` are `Sendable`,  
> then `y` is within a new disconnected region that consists only of `y`.
> 3. [...]

Should the rule 2 still hold if an argument is non-`Sendable`, and non-`Escapable`?

The reason why I ask is that it is currently impossible to write the following method when `Resource` is not `Sendable`:

```swift
func accessResource<T>(
  _ work: sending @escaping (Resource) -> sending T
) async throw -> sending T

```

The reason why it is not possible is that at use site, all values grabbed from the "resource" are in its Region, and can not be sent out:

```swift
accessResource { rez in
    let value = makeValue(resource: rez)
    return value // warning or error
}

```

The only way this API must be written today if full of Sendable constraints:

```swift
func accessResource<T: Sendable>(
  _ work: @escaping @Sendable (Resource) -> T
) async throw -> T

```

We know "sending" was invented in order to help dealing with non-Sendable types, so this version is _strictly_ inferior to the previous one that uses `sending` (but can't be made to work when `Resource` is not `Sendable`).

If the `Resource` type was made non-`Escapable` as well, would it be a sufficient guarantee that it can't "pollute" the value according to Region-Based Isolation, and could thus be "sent" without compiler warning or error?

---

<div class="post-metadata">

**Author:** ![Slava\_Pestov](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/slava_pestov/32/30204_2.png) [@Slava\_Pestov](https://forums.swift.org/u/Slava_Pestov)\
**Post date:** [September 18, 2024, 1:22pm UTC](https://forums.swift.org/t/se-0446-nonescapable-types/74666/10 "2024-09-18T13:22:23Z")

</div>

> [@Karl](#):
>
> Will `@escaping` closures conform to the `Escapable` protocol?

Trivially, yes, because as with Copyable all existing types now magically conform as soon as the new protocol is introduced.

---

<div class="post-metadata">

**Author:** ![Slava\_Pestov](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/slava_pestov/32/30204_2.png) [@Slava\_Pestov](https://forums.swift.org/u/Slava_Pestov)\
**Post date:** [September 18, 2024, 1:33pm UTC](https://forums.swift.org/t/se-0446-nonescapable-types/74666/11 "2024-09-18T13:33:59Z")

</div>

> [@Joe\_Groff](#):
>
> It's tricky to do that without recreating the same problem next time we introduce some suppressible capability.

With today’s compiler architecture, every new suppressible protocol introduces non-trivial compile-time overhead, because every type parameter that does _not_ suppress conformance gains extra requirements. So perhaps we should draw a line at two suppressible protocols, or at least require a very high bar to be met before we consider introducing more.

---

<div class="post-metadata">

**Author:** ![cal](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/cal/32/35557_2.png) [@cal](https://forums.swift.org/u/cal)\
**Post date:** [September 18, 2024, 1:47pm UTC](https://forums.swift.org/t/se-0446-nonescapable-types/74666/12 "2024-09-18T13:47:23Z")

</div>

> Classes cannot be declared `~Escapable` .

Why does this proposal exclude classes? I don't see any discussion about that in the proposal beyond a passing mention.

---

<div class="post-metadata">

**Author:** ![Slava\_Pestov](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/slava_pestov/32/30204_2.png) [@Slava\_Pestov](https://forums.swift.org/u/Slava_Pestov)\
**Post date:** [September 18, 2024, 3:01pm UTC](https://forums.swift.org/t/se-0446-nonescapable-types/74666/13 "2024-09-18T15:01:44Z")

</div>

> [@cal](#):
>
> Why does this proposal exclude classes? I don't see any discussion about that in the proposal beyond a passing mention.

Classes cannot be ~Copyable yet either. Noncopyable and nonescaping classes are a straightforward generalization at the type checker level, but the representation raises some interesting design questions that I don’t think have been worked out yet. For example, a noncopyable class instance doesn’t need a reference count, and noncopyable class metadata doesn’t need a virtual destructor. For a nonescaping class instance you might be able to get away with not instantiating any metadata at all, etc. Once we enable these features (and indirect cases in noncopyable enums) we’ll need to commit to an ABI, and ideally it should not impose any fixed costs due to unnecessary indirection.

---

<div class="post-metadata">

**Author:** ![Joe\_Groff](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/joe_groff/32/1929_2.png) [@Joe\_Groff](https://forums.swift.org/u/Joe_Groff)\
**Post date:** [September 18, 2024, 3:04pm UTC](https://forums.swift.org/t/se-0446-nonescapable-types/74666/14 "2024-09-18T15:04:58Z")

</div>

> [@Slava\_Pestov](#):
>
> With today’s compiler architecture, every new suppressible protocol introduces non-trivial compile-time overhead, because every type parameter that does _not_ suppress conformance gains extra requirements. So perhaps we should draw a line at two suppressible protocols, or at least require a very high bar to be met before we consider introducing more.

While we shouldn't keep adding new suppressible protocols forever with abandon, I think it's still a good idea to avoid baking in assumptions (again) that we will _never_ make more basic type functionality optional in the future.

---

<div class="post-metadata">

**Author:** ![Gabardone](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/gabardone/32/3312_2.png) [@Gabardone](https://forums.swift.org/u/Gabardone)\
**Post date:** [September 18, 2024, 7:34pm UTC](https://forums.swift.org/t/se-0446-nonescapable-types/74666/15 "2024-09-18T19:34:15Z")

</div>

Do we have an allowance here to have nonescaping function properties within `~Escapable` types? It would allow a lot of fun, performant control flow experiments.

Apologies if it's in the proposal, if so I blame my fried brain for skipping it.

---

<div class="post-metadata">

**Author:** ![grynspan](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/grynspan/32/15315_2.png) [@grynspan](https://forums.swift.org/u/grynspan)\
**Post date:** [September 19, 2024, 1:09am UTC](https://forums.swift.org/t/se-0446-nonescapable-types/74666/16 "2024-09-19T01:09:54Z")

</div>

This is 3. `BitwiseCopyable`.

---

<div class="post-metadata">

**Author:** ![James\_Dempsey](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/james_dempsey/32/14336_2.png) [@James\_Dempsey](https://forums.swift.org/u/James_Dempsey)\
**Post date:** [September 19, 2024, 1:33am UTC](https://forums.swift.org/t/se-0446-nonescapable-types/74666/17 "2024-09-19T01:33:17Z")

</div>

The proposal includes an upcoming feature flag `NonescapableTypes`.

The _Source compatibility_ section of the proposal seems to indicate that adopting the proposal would introduce no source breakage for existing code.

What behavior or syntax is gated by the `NonescapableTypes` upcoming feature flag?

Or was this supposed to be an experimental feature flag?

---

<div class="post-metadata">

**Author:** ![ibex](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/ibex/32/30439_2.png) [@ibex](https://forums.swift.org/u/ibex)\
**Post date:** [September 19, 2024, 2:27am UTC](https://forums.swift.org/t/se-0446-nonescapable-types/74666/18 "2024-09-19T02:27:00Z")

</div>

> Specifically, we will add this declaration to the standard library:
> 
> ```swift
> // An Escapable type may or may not be Copyable
> protocol Escapable: ~Copyable {}
> 
> ```

This is probably a silly question, but how do I reconcile the comment with the decl? 😕

---

<div class="post-metadata">

**Author:** ![Nobody1707](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/nobody1707/32/2169_2.png) [@Nobody1707](https://forums.swift.org/u/Nobody1707)\
**Post date:** [September 19, 2024, 2:31am UTC](https://forums.swift.org/t/se-0446-nonescapable-types/74666/19 "2024-09-19T02:31:06Z")

</div>

The `~Copyable` constraint on `Escapable` doesn't mean that the protocol requires non-copyability. It merely says that types that conform to it are not required to be `Copyable`.

---

<div class="post-metadata">

**Author:** ![Slava\_Pestov](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/slava_pestov/32/30204_2.png) [@Slava\_Pestov](https://forums.swift.org/u/Slava_Pestov)\
**Post date:** [September 19, 2024, 12:01pm UTC](https://forums.swift.org/t/se-0446-nonescapable-types/74666/20 "2024-09-19T12:01:40Z")

</div>

That uses the ~ syntax in a different way, to disable the default synthesized conformance or something IIRC. But we’re not generating a `T: BitwiseCopyable` requirement for each generic parameter T by default.

[Next page](https://forums.swift.org/t/se-0446-nonescapable-types/74666.md?page=2)
