Thinking more about it, I think ~Discardable is a better name, it fits in better with ~Escapable and ~Copyable. I had some discussions about what this could look like (motivated by @called(once)) and I feel like there are interesting ideas that I want to spell out fully. This isn't really a "design" so much as some thoughts.
I'll talk about the notion of what 'unsafe' code can assume about 'safe' code. Unsafe code usually has the aim of doing things the compiler can't verify (like working with uninitialized memory) and encapsulating them in a way that means safe code is never exposed to that UB, no matter how safe code uses it. Things we can add to the language that allow unsafe code to make more assumptions of safe code increase the kinds of unsafe code that can be safely encapsulated, which can be desirable. "Linear" types are interesting from a safety perspective for allowing unsafe code to assume that safe code either ran some desired logic with a mutating dependence on another value before graceful exits, or has been prevented from accessing a value due to exclusivity.
I'll also talk about graceful exits, which excludes fatalError, killing the process, unplugging the computer, things of that nature. We won't be able to invent types that ensure we do work in these cases. Rust unwinding is an interesting comparison, I think of Swift as always being in panic = "abort" which means that it is impossible to "catch" or attempt to recover from fatalError, so we don't need to leave the program in a good state when that happens.
Note that I'll talk about consuming things more than once in the context of Copyable. This describes copying the value, then consuming one of the copies. Thinking about consumption like this allows some ideas to generalize.
We can define ~Discardable as types which don't have implicit destruction or programmer defined deinit. These types are interesting even when they aren't paired with ~Escapable or ~Copyable! Such a type is required to be consumed anywhere it might normally have been released. Members that are also ~Discardable need to be consumed when that happens. If you were holding such a value, you'd need to consume it before throwing, unless you can store it in a global or are throwing the value. This means it needs to be passed to a function as a consuming parameter, or have a consuming method called. During separate checking, the body of that function with the consuming parameter had to consume it or pass the obligation further! Because ~Discardable does not require ~Escapable, this isn't a very strong requirement; it does not tell us that the value will be consumed before graceful program exits. You can store an instance of this type in a reference type, and then involve the reference type in a cycle. In Rust, this would be something like mem::forget, which is safe (after the leakpocalypse) to make it clear that the compiler cannot prevent safe code from doing this. Unsafe code needs to assume safe code may "forget" to consume these values, so unsafe code cannot rely on consumption to uphold correctness.
There is a design fork regarding if ~Discardable values can be stored in Discardable values. There is a way to make it work which is equivalently safe, but it's easier to decide this isn't allowed, and provide DeinitConsume<T: ~Discardable & ~Copyable> (calls a provided consuming method during deinit) and DeinitBomb<T: ~Discardable & ~Copyable> (fatalError if the ~Discardable wasn't consumed out) with conditional Copyable conformances. These will have an unsafe internal implementation, but they don't do anything safe code shouldn't do, we are just restricting storing ~Discardable in Discardable to make this mistake harder and avoid complexity about always consume in Discardable types (the alternative just requires consuming ~Discardable members in deinit + rules about what you do before you consume yourself). Note that DeinitConsume is restrictive, it can't use an async consuming method...
If you see where this is going and object to the existence of DeinitConsume, DeinitBomb, see if you can find a definition for ~Discardable which makes it unsafe for them to exist, but does not lead to ~Discardable implies ~Escapable. I believe this is not really possible.
Thinking through avoiding these Deinit types...
If you forbid ~Discardable from being stored in Discardable, but allow it to escape, then you can store it in a global. Jumping ahead of the forget we will define, instead we will use a global, which stores a 'recursive forgetter'; it stores some ~Discardable T and another instance of the forgetter. It has a consume, takes T which conform to some protocol for consumption, and uses that to consume them (except, it won't do that). Any time we want to forget a ~Discardable, we store it in a forgetter, put the existing forgetter inside it, and then store it in the global. This seems equivalent from safety perspective to forgetting a DeinitBomb, but makes it harder to do reasonable things (storing Escapable, ~Discardable in a reference type). If we also forbid storing in a global, how does this differ from ~Escapable which depends on a local value?
Now we can write our own, safe, forget method in Swift:
class Cyclic<T: ~Discardable & ~Copyable> {
let bomb: DeinitBomb<T>
var cycle: Cyclic?
init(_ t: consuming T) {
self.bomb = DeinitBomb(t)
}
}
/// Consume a ~Discardable value without calling a consuming method on it.
func forget<T: ~Discardable & ~Copyable>(_ t: consuming T) {
let v = Cyclic(t)
// close the cycle
v.cycle = v
// release our reference to v, refcount remains > 0
}
So, unsafe code clearly can't assume safe code won't forget to consume ~Discardable values before graceful exits, so far as upholding safety goes. When I say 'forget', I mean that you can forget to actually consume the value in the intended way. But we can also see that this forget implementation didn't violate the requirements of ~Discardable, it won't discard t without consuming it (the bomb would go off) yet this will not happen before a graceful exit. As I understand it, this is not sufficient for linearity, linearity requires consumption "eventually" for graceful exits, but this will happen never. You could also say this is sufficient for "static" linearity but not "dynamic" linearity. The type is used exactly once... for safety we are interested in knowing which values end up with exactly one consuming method use. I'll call this static linearity "weakly" linear.
But we have other protocols! What do these all mean together? Let's write a weird sort of truth table to think about it. Our names aren't going to take into account how "long" you get before you must do things, but our analysis of consumption will, since "forever" allows you to forget.
| Name |
Discardable |
Copyable |
Escapable |
How many consumptions? |
| Unrestricted |
 |
 |
 |
0..N |
| Unrestricted |
 |
 |
 |
0..N |
| Affine |
 |
 |
 |
0..1 |
| Affine |
 |
 |
 |
0..1 |
| Weakly Relevant |
 |
 |
 |
1..N OR forget |
| Relevant |
 |
 |
 |
1..N |
| Weakly Linear |
 |
 |
 |
1 OR forget |
| Linear |
 |
 |
 |
1 |
Our "unrestricted" type is pretty normal, you can consume it as many times as you want, and you don't need to consume it. These are most types in Swift. It might be ~Escapable, such that you lose access to it after some scope, but that doesn't affect how many times you can consume it before then. "Affine" types can be consumed up to once; our existing ~Copyable types, @called(once) proposal is affine, since you can call it up to once. "Relevant" types can be consumed as many times as you want... but you must consume it before graceful exit. And "linear" types must be consumed exactly once.
We have "weakly" variants of relevant and linear types, what are these? This is maybe the wrong word from a theory perspective (since it doesn't refer to weakening) but I like it. They can be conceptualized as really powerful lints, but they don't increase the kinds of unsafe operations that can be safely encapsulated. For a weakly linear type, you can safely discharge your obligation via forget, so unsafe code can't rely on the linear property. But these weak variants are still very useful; in exchange for not being "strong", they can be stored in reference types and handled more ergonomically. If you've ever used @discardableResult, a weakly relevant type is the extremely "infectious" inverse of that; short of forgetting it, a weakly relevant result must be consumed at least once. A programmer cannot just store your returned value in a variable to discharge the obligation to do something with it, it is an error to not consume it somehow. Non @discardableResult (pretend it only considers consuming use) is easy to escape, weakly relevant / linear is "harder", but for all of them it is safe and expected to forget to always consume.
It might seem wrong from a theory perspective that we have this "weak" distinction, there shouldn't be multiple kinds of linear types. You can argue our "weakly linear" is linear up to leak / forget, and our "linear" is linear up to ungraceful exit, and that linear up to ungraceful exit is the important property. I think languages with linear types normally wouldn't permit cycles... debating the name distracts from the safety motivation to have these types, which is to prove that something must have happened.
Note that we already have some of these! We have the first 4 today. Affine types (especially ~Escapable) can already do a lot. If all you want is to ensure some operation happens, ~Copyable, ~Escapable ensures your deinit will run before graceful exits, unless you called a consuming method that used discard self, or deadlock / loop infinitely / similar. You can write code that assumes the deinit of a ~Copyable, ~Escapable value has run if the programmer is allowed to access the value it had a mutating dependence on. However, your deinit is not invoked by code, so it can't take parameters, return a value, throw an error that must be handled, perform async operations, and so on. In this way, a weakly linear type (which is Escapable) is an expressivity gain over this, although it is a correctness loss since it is now safe to never consume it, whereas it was impossible to forget to deinit something ~Copyable, ~Escapable when the lifetime of its dependency ends. Our "true" linear type is both an expressivity gain and a correctness gain (at a high ergonomic cost).
Although this table has the "fundamental" types, we can invent some more kinds by composing them. For exactly N consuming operations, we can use linear types which return more linear types, instantiating N variants which each return the next. We can also achieve exactly N, in any order, by returning a tuple of linear types, or at least N with a tuple of relevant types. We could also define relevant types using consuming operations on linear types that return more linear types... This might be easier to work with than the relevant types we'd get naively, since Swift may insert copies in surprising places.
You might object that ~Escapable still allows you to defer consumption "for a long time", but the important distinction is that the compiler is aware of this. forget is problematic because under separate checking, calling it appears to discharge the relevant or linear requirement. This is because the kinds of things we want to do with linear and relevant types use exclusive dependency to uphold safety. They are linear, and use their dependency on some other value to ensure that until the consuming operation is done, the value they depend on cannot be accessed. As long as you can keep both around, that is fine, and wherever the dependency "runs out" you'll need to discharge your obligation to the linear type. This is also how you make sense of holding a linear type over an await, or before going into an infinite loop; if the await blocks forever swift concurrency forward progress isn't happening, and we aren't getting a graceful exit or won't be able to access the depended on value for the rest of the program. The property we are chasing is a value which is consumed before the dependency can be accessed again OR graceful exit.
I have a lot more I want to say about these concepts, but I want to highlight that while it can be dynamically safe to go from weakly linear or weakly relevant to affine (this is what DeinitBomb and friends do) it can never be safe to go from relevant or linear to escapable affine via a type and enforce the requirement via deinit, if you think it is desirable to increase what safe code can be encapsulated. No "internal" dynamic check can detect forget and so no assumption of "eventually" can be made. An "external" check could, however; if all consumptions flipped a shared bit, then at the point where you assume the value has been consumed you could assert the bit has been flipped.