Noncopyable Existentials and Casting

After talking with some people who were trying to use Noncopyable existentials for various purposes, I set out about a month ago to see what was possible here. I have since implemented an experimental feature "NoncopyableCasting" that enables many kinds of common operations involving noncopyables. I'm still exploring what's feasible, but here's a partial list of what you can do now with this feature:

  • type(of:) borrows its argument instead of consuming.
  • Most forms of is borrow the subject instead of consuming.
  • if let, guard let, while let, if case, and consuming switch generally work for noncopyable types. In all of these, a failing cast leaves the original available, so you can chain if let or use the original value in an else or default: clause.

There are some limitations to the above. I'm not certain I'll be able to address all of these, at least not in the short term:

  • New runtime support is required for some of these cases, especially the is handling and some switch constructs that are based on it. So some cases may not back-deploy on ABI-stable platforms until the runtime support can be back-deployed.
  • Borrowing switch is complicated. In essence, Swift's casting allows you to do things like create or project out of existentials or optionals, and those are hard to do without destroying the original. Borrowed function arguments have the same challenge.
  • Optional noncopyable existentials (e.g. case x as (any P & ~Copyable)?: ) are also proving troublesome.
  • There are a number of other edge cases that I hope to clean up, many involving interactions with closures and general noncopyable support.

Some of these limitations I might be able to lift, others will get left as diagnostics suggesting workarounds.

I hope to put together a proper proposal in the next couple of weeks with more details about all of this.

Cheers,

Tim

11 Likes

Does this make it possible to write:

func f(_ x: borrowing some ~Copyable) {
  if let x = x as? any P & ~Copyable {
    p.requirement()
  }
}

If so, it would allow us to fully support various core protocols like CustomStringConvertible on noncopyable values without needing to plumb a generic overload of everything through every layer of the system.

1 Like

Not with a borrowing argument, no. It would work with a consuming argument.

Swift's casting runtime support always consumes the original value on a successful cast. So that means we can support structures like this:

if let result = source as? any P & ~Copyable {
   // `source` is consumed here, `result` is valid
} else if let result2 = source as? any Q & ~Copyable {
   // `source` is consumed here, `result2` is valid
} else {
   // `source` is valid here
}
// `source` is consumed here

(As well as similar shapes for consuming switch statements, etc.) Note that the source is always consumed at the end, which precludes using it with a borrowed argument.

There's nothing theoretically wrong with the code you wrote, but it requires non-trivial additional runtime and compiler machinery beyond what I'll be able to achieve in this effort.

1 Like

Am sad. Okay, thanks for clarifying!

Here's a draft Swift Evolution Proposal for this work:

https://github.com/tbkka/swift-evolution/blob/c271626276f1e483eb55508423e3b0faf566a494/proposals/NNNN-dynamic-casting-of-noncopyable-values.md

Any feedback is appreciated.

1 Like