SE-0556: Cell, ConstCell, and Volatile

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.

1 Like

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.


  1. or some other name, this is just a strawman to align with the access syntax John mentions ↩︎

  2. There are times it's ok to cast away const, but I feel like it's usually misused ↩︎

  3. which seems to only exist to protect non mutable references from interior mutability ↩︎

1 Like

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-Mutable Span 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.


  1. 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:

  1. 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.
  2. 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.

1 Like

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.

1 Like

The implementation does handle this now with: [IRGen] Don't mark raw-layout parameters noalias by drexin · Pull Request #93127 · swiftlang/swift · GitHub.

2 Likes

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.

2 Likes

My reading of the proposal:

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.

See SE-0556: Cell, ConstCell, and Volatile - #46 by Nobody1707

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.

1 Like

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>. Although the result of constCell() can be "safely" passed to C++, the compiler can continue to assume that the original borrowed value is unaliased and immutable after it has escaped to C++ which would be UB. (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 assuming uniquenes.

2 Likes

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.

Hopefully Swift can never import a C++ const int a[10] as something truly immutable in Swift. It could be imported as ConstCell, but the proposal says ConstCell is mutably aliased even though it doesn't give the Swift programmer control over mutation.

We do allow ConstCell to be passed to C(++) as const T*. There is a required lifetime annotation on the C++ side that promises not to escape the value (noescape). I think the additional promise C(++) code makes is that, if it is given a const value, it doesn't make use of const_cast or mutable or similar nonsense to modify the value. I consider those to be reasonable restrictions on the C(++) side, and we don't need to be defend Swift's safety model against violations of them.

Doug

3 Likes

The more I think about it, the more necessary I think this general model of aliased storage, borrows, and mutations is for delivering the feature set we really want. Strictly speaking, though, I don't think it needs to block accepting this proposal. We could definitely come back later and rebase the currently-proposed semantics of Cell and ConstCell onto that general model.

However, it might block adopting this proposal, especially in C/ObjC/C++ interop. In a world without that model, we presumably need to import a lot of things as Cell, like:

  • pointee types of pointers and C++ references (at least when we can import them as Refs and Spans),
  • mutable global variables, and
  • stored properties of foreign reference types.

The aliased-storage model wouldn't help with pointers, but the others could just be modeled as aliased storage. And we presumably would not want to introduce a million intermediate Cells, with all the usability pains that would entail, only to remove them when we import the storage as aliased.

In this world with direct modeling of aliased storage, I think people would only really use Cell for two sorts of things:

  • pointee types of aliased Refs and Spans, and
  • implementing things like Mutex.

The second of these is a pretty uncommon use case, and I don't think we need to be overly concerned about the annoyance of working with Cell there. However, I am really worried about the usability of aliased pointers because of the requirement to drill through the Cell on every access. We've hand-waved about possible solutions to that, but if aliased storage means that this is really going to be the only place we expect to see Cell in normal use, maybe we should just not try to use Cell generically this way.[1] We would need to go back to what we had in an early draft of this proposal, where we added new AliasedRef, AliasedMutableRef, AliasedSpan, and AliasedMutableSpan types. These types wouldn't have the .value-stacking problems that Cell does. They would just offer safe get/set access, to be rebased onto aliased borrow/mutate later, plus of course unsafe variants that directly borrow and mutate.

We can still add Cell as an official way of getting the interior-mutability semantics required in the implementation of types like Mutex, but we wouldn't expect to use it outside of that. And we probably wouldn't need ConstCell at all.


  1. This also conveniently side-steps my concerns above about whether Ref<Cell> and Span<Cell> are really proper generic applications of Ref and Span rather than special cases. ↩︎

3 Likes

I was wondering how the aliased-storage model would work with function types, protocol requirements, and unspecialised generics (especially with T: ~Copyable).

Parameter ownership is generally type-driven in Swift already. I would expect that we would model aliased borrows and mutations as new kinds of parameter ownership that would distinguished in function types, probably written something like borrowing(aliased) and inout(aliased) (or nonmutating(aliased) and mutating(aliased) on methods).

Having an abstract reference to non-aliased storage (e.g. a non-aliased borrowing parameter) is a guarantee that nothing externally aliases that storage. It does not forbid a function from locally forming its own aliases to the storage.[1] Such local aliases are fine because the model for them preserves memory safety (in the absence of concurrency) by only allowing instantaneous accesses to the actual memory: copies and overwrites.[2] The compiler just needs to ensure that the lifetime of such aliases does not overlap any accesses that would conflict with them.[3]

Therefore, a non-aliased borrow or mutation can safely be "downgraded" to an aliased borrow or mutation as long as nothing else is happening to the storage during that time. In particular, this means there is a natural subtype relationship in which:

  • (borrowing(aliased) T) -> R is a subtype of (borrowing T) -> R and
  • (inout(aliased) T) -> R is a subtype of (inout T) -> R.

The reverse is not true: you cannot safely "upgrade" an aliased reference to a non-aliased reference. It is possible to emulate this by copying the value, but of course this requires the type to be copyable, and it has performance and semantic consequences. We would not have a function subtyping relation in this direction, both because of the copyability restriction and because it would create a new cycle in the subtyping relation, which is highly undesirable. However, I do think we should probably allow this emulation to be done implicitly during calls.[4]

For C++, I would expect that we'd import this and reference parameters as "value" parameters with these new kinds of ownership, at least when the parameter is known to not escape.

For protocol requirements and unspecialized generics, this is all fairly straightforward. We have no mechanism in the language for generically abstracting over parameter ownership; the ownership of a specific parameter is always known statically. For protocol requirements, this comes from the declaration of the requirement. An unadorned protocol requirement like mutating func foo() declares that it takes self as an unaliased mutation. We can reasonably choose to allow an implementation of the requirement to take self as an aliased mutation, which ultimately simply does the subtyping downgrade discussed above; but when we call an unknown function through the protocol requirement, we must assume that the caller requires an unaliased mutation, and so such a function cannot be called on aliased storage[5].

So, for example, Iterable's makeBorrowingIterator requirement takes an unaliased borrow of self, and the iterator it returns is valid for this duration. A witness to this requirement may therefore assume that self is not aliased during the iteration. In particular, C++ types like std::vector can directly vend their element span from the iterator and be confident that it will not be invalidated. The flip side of this is that makeBorrowingIterator() cannot be called at all on a vector that is held in aliased storage (unless we can somehow prove a lack of aliasing, or create it by a temporary copy).


  1. Of course, such aliases may not escape the lifetime of the abstract reference (e.g. the call to the function with the borrowing parameter). ↩︎

  2. Members can also be projected as aliased references. ↩︎

  3. For example, a function with a non-aliased inout parameter may form an aliased reference (mutable or not) to the parameter as long as it does not overlap with any calls it makes on that parameter that require a non-aliased reference. ↩︎

  4. This would of course count as an implicit copy for the purposes of any diagnostic that cares about that. For example, if we had a language mode or a type attribute that forbade implicit copies, we would expect it to forbid this emulation as well. ↩︎

  5. except by emulating unaliased access with an implicit copy, as discussed above ↩︎

1 Like

On that last point, one benefit of a type-driven concept of aliasing, like Cell, over the direct modeling of aliased storage is that it would give us space to talk about different conformances for, say, Cell<std::vector<char>> vs std::vector<char>. The former might need to conform to Iterable by copying elements individually rather than vending its internal storage. However, this does not really fit into Swift’s protocol model at all, since different specializations of Cell would require totally unrelated conformances.

Alright, so the thinking here is that Cell can use get/set, with whatever optimization barrier we need to put in place to make sure we deal with aliasing. That's conservatively correct. As a more general model of aliased storage comes along, we replace the get/set with your proposed borrow(aliased)/mutate(aliased). This lets the optimizer reason more about the effects of aliasing, but doesn't make any existing code worse.

This doesn't really mean that we can avoid reasoning about how Ref<Cell> or Span<Cell> works, though. Generically, we end up where Ref<T> can't assume non-aliasing of the pointee when we don't know whether T is a Cell or not.

This is primarily an ergonomics argument, which I understand---having to .value everything when accessing a Ref or a Span is pretty painful---and I don't know that we are happy about any kind of implicit .value or conversions. I wonder if there are also arguments around performance for those places we know we're dealing with aliasing. A loop over a Span<Cell<UInt8>> to copy or zero the bytes may end up being a whole lot worse than memcpy or memset, but maybe we can do better when we don't have Cell in the way.

Right, ConstCell was only ever interesting for C interoperability. If we're not getting C interoperability out of it (because we're pushing those uses over to AliasedRef/AliasedSpan), there's no reason to have it.

Doug

2 Likes

I have been relatively quiet on this proposal because I usually review features that either have clear security implications or features that I think I would use; but now that AliasedRawSpan is no longer proposed, it falls outside my usual scope. (There's a good amount of room to get the implementation of Cell wrong in a way that leads to memory safety issues, but I think that there exists a safe implementation.)

With that said, it's been pointed out to me that Span<Cell<UInt8>> could be the replacement for AliasedRawSpan, and I think that's a mistake. I suspect we will either have another proposal for a true AliasedRawSpan later or that one will be created independently. I don't want to expound too much on features that are not proposed, but my experience is that combining small loads and stores into wider ones through Span is unreliable, yet that's what we would need to work with Span<Cell<UInt8>> as a RawSpan. I think that we should also explore what that says about RawSpan.

Whether Span<Cell<UInt8>> is actually the base type for aliased raw spans, however, does have implications on whether Cell is Sendable when its contents is Sendable & FullyInhabited. That is more or less required to make Span<Cell<UInt8>> usable in multiple threads. This is very convenient to implement, for instance, tiled image decoders. However, making Cell Sendable for that use case alone, if there is push back that maybe this is not a good idea for concurrency correctness in general, may be a hint that Cell maybe should never be Sendable on its own and the basis for an aliased raw span should not be Span<Cell<UInt8>> as a result. I think it makes sense to ask what anybody else would use that conformance for if it existed.

1 Like