One issue with the proposal wording is that it’s very light on the tradeoff you’re accepting with Cell and I would love it if it was clearer on what you are giving up on:
what you are giving up when you use Cell safely
what you get in return
what precautions you need to take if you use Cell’s unsafe pointer accessors
So I’m glad to see you bring that up and I think it’s very important to flesh it out.
I'd like to briefly post something John discussed with me so it can be on the forums; if we are taking the route of borrowing(aliased) style access, is it possible we should have an Aliased<T> type[1] which has aliasing semantics, but not interior mutability? Interior mutability is "morally" equivalent to casting away const, which is bad[2]. Since Cell as proposed has aliasing and interior mutability, we need a ConstCell for use with Span, Ref etc to avoid allowing this cast in safe Swift when importing C++ code.
If our primary interop type didn't have interior mutability, and only aliasing semantics, then mutability would propagate from the container like other types in Swift; Span<Aliased<T>> is immutable because you cannot call mutating methods on Aliased through Span, while MutableSpan<Aliased<T>> is mutable. We can still have a Cell or maybe InnerMutAliased or some other named type for when you really do need interior mutability, if we think that is motivated. Based on how many people seem to think this type is only for interop, it may not be needed at all. If it is, it's something that can be built without special support via Aliased. This choice would also eliminate the need for the rather strange ConstCell[3], since C++ "const" uses can be behind Ref and Span which you can't mutate through in safe Swift.
or some other name, this is just a strawman to align with the access syntax John mentions ↩︎
There are times it's ok to cast away const, but I feel like it's usually misused ↩︎
which seems to only exist to protect non mutable references from interior mutability ↩︎
My posts above came out of those same conversations (and a few others). However, I did realize as I was writing it up that we probably do want the aliased Span and Ref types to be copyable even when mutable. We can do by using non-MutableSpan and Ref and then preserving the correct interior mutability with the Cell vs. ConstCell distinction.
However, the more I think about it, the more concerned I get about Span<Cell>. Here's the problem as I see it:
We want to have (and already do have) optimizations around "long-term" non-aliased borrows, like those done for Refs and Spans. In particular, I think we want to aggressively strength-reduce copies to borrows (only when you don't go on to mutate your copy, of course), and that should generally be semantically okay because of the strength of Swift borrows. And this optimization would generally apply to borrows of "subvalues" of the borrowed value, of course.
We also want to have optimizations around aliased accesses. In particular, if we can (1) recognize that we're doing an aliased access to non-aliased storage and (2) prove that that access can't conflict with other aliased accesses to the storage,[1] we can promote those accesses to be non-aliased accesses and then get back to aggressively strength-reducing copies to borrows.
These rules seem to interact really badly. They basically force us into a world where we have to pretend that the value storage of a Cell is not really a sub-value of the cell, as it if it were stored completely separately. That's the only thing that would break the inference that a non-aliased borrow of a Cell does not permit reasoning about the aliased-ness of its value. I get why we need that for Atomic and Mutex, but it seems really conservative for these more composable use cases. It just feels like we have the tiniest bit of a lie here, but it's going to lead us to need some kind of semantic hack to treat Span<Cell> and Ref<Cell> as aliased all the way up.
The fix would be that we really do need the full set of Aliased{|Mutable}{Span|Ref} types. That feels like a less composable design except that, if we bless the idea of directly modeling aliased storage instead of wrapping it all in Cell, I'm actually not sure that we need more than Span and Ref of Cell anyway. And if we need a semantic hack for accesses to Span and Ref of Cell, we don't have correct composability anyway.
This might sound difficult, but it's likely to be quite easy. Recall that aliased accesses to non-aliased storage still have to follow the basic rules of non-aliased accesses so that we can correctly check the interaction between aliased and non-aliased accesses. ↩︎
Maybe it would help to distinguish between two possible aliasing semantics:
Even if we have possibly-aliased/borrowing access to the outer value, we can assume that the inner value is non-aliased/mutating for some lifetime. (Which is unsafe, unless the mutation is instantaneous.) However, if the outer value is non-aliased/mutating, the inner value is still guaranteed to be non-aliased/mutating.
The reverse direction: even if we have possibly-aliased/borrowing access to the inner value, we can assume that the outer value is non-aliased/mutating. This means, even if the outer value is non-aliased/mutating, the inner value might still be aliased.
The first kind seems relatively straightforward. It corresponds to Rust's UnsafeCell type. I think it might already exist for weak references, because even if a struct is immutably borrowed, its weak reference values can still be mutated. If Cell only has this property, it's still safe to assume that a non-aliased Cell has a non-aliased inner value.
The second kind seems a lot more vexed. It corresponds to Rust's UnsafePinned type, which is still experimental, and to my knowledge, still has unresolved questions surrounding it. I think it might be this kind of aliasing semantics, not the first kind, that significantly prevents optimizations.
This seems to imply that Aliased would have the second kind of aliasing semantics, instead of (or in addition to) the first kind; because MutableSpan<Aliased<T>> would undo the normal non-aliasing guarantees of mutating access.
I wonder to what extent these are consequences of specifically the second kind of aliasing semantics, not the first kind.
Maybe I missed something but I would expect ConstCell to represent immutable data, and so not generally be subject to those sorts of data races? Is the concern that it could be mutated outside Swift? It's trivial to construct a value that's immutable and Sendable in Swift but which some C function will then mutate when we're not looking; as a rule, I don't think we can or should try to prevent that from happening beyond calling it "undefined behaviour".
I was under the impression that ConstCells primary purpose was to represent the contents of a C T const* to a potentially non-const value. So that, for instance, a Swift implementation of a C function that takes a T* dst, T const* src, size_t len triple can import the parameters as dst: Span<Cell<T>>, src: Span<ConstCell<T>> and merely get tearing instead of undefined behavior if someone mutates src out from under you. Or to protect you from UB if the function in question is akin to memmove and dst and src overlap.
There's been good discussion on this proposal in the past two weeks that the Language Steering Group would like to see continue—as such, the review period for this proposal has been extended through November 10th, 2026.
In addition to Cell, there's a ConstCell type that provides the same guarantees as Cell, but disallow writes from Swift. The memory may still be [safely] altered from outside of Swift.
Yes, my read of the proposal is that the outer value is non-aliased and immutable while the inner value is aliased and mutable. Span<Cell<T>> means that the cell is unique and immutable but the value it contains is aliased and mutable. I think this only works if there's no way to detect aliasing/mutation of the cell itself--it's a stateless wrapper that magically materializes its inner value.
One of the proposed conversions doesn't look safe:
extension Span where Element: ~Copyable {
public func constCells() -> Span<ConstCell<Element>>
}
Basically, for the same reason Rust doesn't allow &T to &Cell<T>. The result of constCell() can be "safely" passed to C++. The compiler can, however, continue to assume that the original borrowed value is unaliased and immutable after it has escaped to C++. (Or do we never allow ConstCell to be passed to C++?).
The conversions from a MutableSpan are safe because they consume the MutableSpan which precludes any subsequent access from asusming uniquenes.
It's not just Span.constCells() that has this problem. It's also possible for C++ code to return a Span<ConstCell>/std::span<const T> pointing to a C++ array that was originally declared as const (such as const int a[10]), which is also memory that the compiler can assume is really immutable.