# SE-0556: Cell, ConstCell, and Volatile

**URL:** <https://forums.swift.org/t/se-0556-cell-constcell-and-volatile/89988>\
**Category:** Proposal Reviews\
**Created:** [October 9, 2026, 10:00pm UTC](https://forums.swift.org/t/se-0556-cell-constcell-and-volatile/89988 "2026-10-09T22:00:51Z")\
**Posts on this page:** 20\
**Page:** 2

<div class="post-metadata">

**Author:** ![j-f1](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/j-f1/32/12287_2.png) [@j-f1](https://forums.swift.org/u/j-f1)\
**Post date:** [October 14, 2026, 4:47am UTC](https://forums.swift.org/t/se-0556-cell-constcell-and-volatile/89988/21 "2026-10-14T04:47:33Z")

</div>

> [@ellie20](#):
>
> It has some weird implications; for example, if we have a `MutableSpan<Volatile<T>>`, and we use a bulk update operation such as `MutableSpan.updateElements`, it becomes significant how many times, and in what order, the method's implementation writes and reads each individual element, unlike with any non-`Volatile` type.

This makes me wonder if a better shape for `Volatile<T>` would be for it to wrap an `UnsafeMutablePointer<T>`. That would make it trivially bitwise copyable, and hopefully it would still be reasonably easy to import a `volatile` C constant by constructing `Volatile(&foo)` at the bridging layer.

This would, of course, have the downside of not being able to easily write `Span<Volatile<T>>` for larger buffers of volatile values. But given the behavior you’ve mentioned, it seems like a `VolatileSpan<T>` sort of type would be better here because it would allow you to more clearly document the expected ordering/behavior of bulk reads and writes.

---

<div class="post-metadata">

**Author:** ![ellie20](https://avatars.discourse-cdn.com/v4/letter/e/6f9a4e/32.png) [@ellie20](https://forums.swift.org/u/ellie20)\
**Post date:** [October 15, 2026, 3:49am UTC](https://forums.swift.org/t/se-0556-cell-constcell-and-volatile/89988/22 "2026-10-15T03:49:27Z")

</div>

Yeah, something like `VolatileRef<T>` and `VolatileSpan<T>` would be one approach to fix the issues I brought up. Another possible approach is to have types like `VolatileCell<T>` and `ConstVolatileCell<T>` that (like `Cell` and `ConstCell`) only have special semantics when borrowed, and behave like normal values otherwise.

---

<div class="post-metadata">

**Author:** ![scoates](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/scoates/32/35687_2.png) [@scoates](https://forums.swift.org/u/scoates)\
**Post date:** [October 16, 2026, 3:10pm UTC](https://forums.swift.org/t/se-0556-cell-constcell-and-volatile/89988/23 "2026-10-16T15:10:07Z")

</div>

I haven't had a chance to read this whole thread, but as a person who [hacks up dumb stuff](https://scoat.es/@sean/117420250673023324) with Embedded Swift, `Volatile` sounds like it would definitely help me with MMIO (without needing [swift-mmio](https://github.com/apple/swift-mmio) or a build hack to access it from C-space).

This is not just "running a safe-ish subset of operations on memory that another core is concurrently modifying without using synchronization primitives"; sometimes the actual bus lines are wired up to not-CPU-controlled RAM.

(I recognize this is a niche, but I was happy to see `Volatile` show up in my Swift Forums Summary email.)

---

<div class="post-metadata">

**Author:** ![Douglas\_Gregor](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/douglas_gregor/32/1362_2.png) [@Douglas\_Gregor](https://forums.swift.org/u/Douglas_Gregor)\
**Post date:** [October 21, 2026, 6:18pm UTC](https://forums.swift.org/t/se-0556-cell-constcell-and-volatile/89988/24 "2026-10-21T18:18:05Z")

</div>

## On `Volatile`...

> [@fclout](#):
>
> I don't really agree that `Volatile` goes in `Synchronization` because it's only the right tool for synchronization in extremely specific circumstances (running a safe-ish subset of operations on memory that another core is concurrently modifying without using synchronization primitives), and its only other use (making sure you see the latest value of a variable that a signal handler can change) is a single-threaded use case.

I agree with this. `Volatile` isn't really a synchronization mechanism, it's a memory access semantic. I'd prefer that it go into the standard library, partly for that reason, and partly because it's how we want to interoperate with `volatile`-qualified values from C(++)... and I don't want interoperability to have different behavior/visibility depending on whether one imports `Synchronization`.

I do wonder whether we should call this type `VolatileCell` to put it in the same family of the other `Cell` types: an aliasable location with specific access semantics.

## On `ConstCell`...

> [@xwu](#):
>
> A slight tangent from the overall point that @fclout is making here, but I find it interesting to observe that, as proposed, `Cell` and `ConstCell` deliberately departs from our Foo/MutableFoo convention.

It is deliberate, because the "non-mutation-permitting" version of the cell doesn't mean immutable the way that `Span` or `Ref` do. All it means that _you_ can't modify the data; it might still change under you. For that reason, I think `ConstCell` isn't the right name for this type, because it's not "constant" in any sense that applies in Swift. It's basically the C notion of `const`, but that's not something that we should use for naming. Personally, I'd prefer `ReadOnlyCell`: it's wordy but clear.

`Unsafe(Buffer)Pointer` is the primary counter-argument to my reasoning above. The pointee can change out from underneath you, as with `Cell`. I consider that to be part of the fundamental unsafety of the type rather than a pattern to emulate.

And then, the aesthetic argument: outside of interoperability with C, I expect uses of the `ConstCell`/`ReadOnlyCell` to be exceedingly rare. It would be unfortunate to burn the short name (`Cell`) on that.

Naming aside, I do wish it were easier to convert from a `Span<Cell<T>>` to a `Span<ConstCell<T>>` (ditto for `Ref`), because I expect that's going to be common in interop scenarios. How awful would this be?

```swift
extension Span {
  func constCells<T>() -> Span<ConstCell<T>> where Element == Cell<T> { ... }
}

```

## On the conversions...

> [@ellie20](#):
>
> I think the semantics of `Span.unsafeCells()` should be clarified.

I'd rather we remove the API. One can do this with the unsafe pointer APIs. We don't need to make it easy.

## About `unsafeBorrow`...

> [@ellie20](#):
>
> `Array.withUnsafeMutableBufferPointer` does not make existing operations on the array unsafe, because the compiler knows that the array is being mutated for the duration of the call.

This statement is only true for APIs directly on the array, within the closure passed to `Array.withUnsafeMutableBufferPointer`, and assuming that buffer isn't used in a way that corrupts the data. For example, one can corrupt the contents of the buffer by breaking memory exclusivity, or escape the buffer outside of the closure, each of which will cause later array operations to be broken.

> [@ellie20](#):
>
> One could argue that the non-usage of otherwise-safe `Cell` APIs is just part of the contract of `Cell.unsafeBorrow`—part of what it means to "use it correctly"—but I think that would make the contract of `Cell.unsafeBorrow` non-local and more difficult to check than the contracts of other unsafe APIs like `Range.init(uncheckedBounds:)`. It could require checking all code that is transitively called during the duration of the borrow, even if such code otherwise appears safe.

I think `Range.init(uncheckedBounds:)` isn't a good comparison here because it's so trivial to reason about the safety invariants.

`Span.withUnsafeMutableBufferPointer` is an interesting comparison, because `Span` doesn't (and can't) "know" about the mutation in the way that `Array` does, so it's more similar to `Cell`.

> [@ellie20](#):
>
> To put it another way, `Cell.unsafeBorrow` would become the first unsafe API whose contract depends on which _safe_ APIs are used after the fact.

I don't agree with this statement. I think `Array`'s unsafe buffer pointer API is an example, and `Span`'s equivalent API is a more relevant one.

## On `RefCell` and runtime exclusivity checking

> [@ellie20](#):
>
> So, it might be possible for `Cell` to use this approach to have runtime exclusivity checking, while still being zero-cost if the runtime exclusivity checking is not used.

This is an interesting idea. One could provide yielding accessors that call into the Swift runtime's `swift_beginAccess` / `swift_endAccess`, which is what provides dynamic exclusivity checking.

> [@ellie20](#):
>
> What's stopping `Cell` from having runtime exclusivity checking, though, is that it is possible for C code to read or write the inner value _without_ updating the access set.

Right.

> [@ellie20](#):
>
> Perhaps a `Cell`-like type with runtime exclusivity checking could be "default" for pure Swift code, and a version without runtime exclusivity checking would be used only for C interoperability?

It would have to be a separate type, because the normal get/set operations on the value would also have to trigger exclusivity checks. Part of the intent of `Cell` as it is today is that it doesn't do dynamic exclusivity checking.

## On `Sendable`...

> [@fclout](#):
>
> On the other hand, Cell and MutableCell are no longer the tools to use to share FullyInhabited values on multiple threads, and that was one of the things I (and several other people who cannot represent their interests here) was the most looking forward to. This is not necessarily an argument against Cell if there are other good reasons to have it, but I am very much looking forward to that functionality.

I'd actually like to bring the `Sendable` conformance back when the underlying element type is `Sendable` and `FullyInhabited`. The `FullyInhabited` constraint is extremely strong: you can use `Span` APIs to scribble on individual bytes of a `FullyInhabited` type, and it's memory-safe to do so. Concurrency doesn't change that, it just adds more sources where individual-byte-scribbling can happen if you're doing this kind of mutation. For many of the named use cases of `Cell`---shared memory, C interoperability---those memory-scribbling sources are already quite plentiful.

On the other hand, the workarounds for not having `Sendable` on `Cell` are pretty annoying, because it means that you can't (for example) share a `Span<Cell<Double>>` across isolation domains without wrapping up that whole type in an `@unchecked Sendable`. This scatters a lot of apparent (but not new) unsafety throughout a code base, for not much benefit.

> [@aviva](#):
>
> `ConstCell` being `Sendable` but only for `FullyInhabited` makes me uncomfortable although I can't find a safety issue with it.

I think we should either have the `Sendable` conformance on both (my preference) or neither.

Doug

---

<div class="post-metadata">

**Author:** ![aviva](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/aviva/32/44327_2.png) [@aviva](https://forums.swift.org/u/aviva)\
**Post date:** [October 21, 2026, 6:38pm UTC](https://forums.swift.org/t/se-0556-cell-constcell-and-volatile/89988/25 "2026-10-21T18:38:19Z")

</div>

I agree with both or neither! From a semantics perspective, you convinced me offline that it wouldn't be so bad to let `Cell` be `Sendable` for `FullyInhabited`\[1\]. I can only think of it leading to safety issues in cases like where `Cell<Int>` is used to do unchecked indexing... but one shouldn't do that! Store it in some larger structure and mutate it normally, or create a wrapper for `Int` (you should do this anyway to enforce its only created after bounds / invariant checking...) which won't be `FullyInhabited`. I think it's been brought up before but we could consider an even stronger protocol (`SingleWord`?) for types which are always updated as a single operation (and so won't tear) but I don't think this is super necessary and it would be platform dependent which is weird.

> [@ellie20](#):
>
> If I understand correctly, unlike in Java, where a data race can cause tearing but can't cause undefined behavior, or Go, where a data race can cause tearing but can only cause undefined behavior if pointers are involved, in C and presumably also in Swift, a data race _always_ causes undefined behavior, which tearing is merely a possible symptom of. If so, then the `FullyInhabited` requirement doesn't actually improve safety; it is still unsound for `ConstCell` to _ever_ conform to `Sendable`, because reading from a `ConstCell` while C code is writing to it from another thread causes a data race, even if the inner value is `FullyInhabited`.

I think we need to figure out the answer to this before we can let people do "unconcerning" data races in safe code. The worst you can do in safe code today is copy out and overwrite without actually doing a CAS, which is not great (it's sort of a "high level" data race) but not the same as a data race from a UB perspective AFAIK.

* * *

1. assuming this doesn't allow safe code to introduce what LLVM considers UB

---

<div class="post-metadata">

**Author:** ![ellie20](https://avatars.discourse-cdn.com/v4/letter/e/6f9a4e/32.png) [@ellie20](https://forums.swift.org/u/ellie20)\
**Post date:** [October 21, 2026, 8:13pm UTC](https://forums.swift.org/t/se-0556-cell-constcell-and-volatile/89988/26 "2026-10-21T20:13:59Z")

</div>

> [@Douglas\_Gregor](#):
>
> For example, one can corrupt the contents of the buffer by breaking memory exclusivity, or escape the buffer outside of the closure, each of which will cause later array operations to be broken.

Those examples require the buffer pointer to be _unsafely_ dereferenced, such as by using `UnsafeBufferPointer.subscript(_:)`. (If I understand correctly, escaping the buffer pointer is only problematic if the buffer pointer is subsequently unsafely dereferenced; it isn't inherently undefined behavior as long as the buffer pointer is unused after being escaped.\[1\]) And they are incorrect regardless of what safe code does afterwards.

> [@Douglas\_Gregor](#):
>
> > [@ellie20](#):
> >
> > To put it another way, `Cell.unsafeBorrow` would become the first unsafe API whose contract depends on which _safe_ APIs are used after the fact.
> 
> I don't agree with this statement. I think `Array`'s unsafe buffer pointer API is an example, and `Span`'s equivalent API is a more relevant one.

The contract of both `Array.withUnsafeBufferPointer` and `Span.withUnsafeBufferPointer` is that the buffer pointer passed to the closure is not used incorrectly. The only way to use the buffer pointer incorrectly is to perform some _unsafe_ operation on it, namely unsafely dereferencing it. So the contract of `withUnsafeBufferPointer` only depends on the behavior of subsequent _unsafe_ code, not the behavior of subsequent _safe_ code.

The contract of `Cell.unsafeBorrow` is that otherwise-safe operations of the `Cell` (such as `Cell.value`) are not used for the duration of the borrow. If we see `Cell.unsafeBorrow` being used, we can't know if we're upholding its contract unless we look at the behavior of subsequent _safe_ code; that's what I think makes it different from `withUnsafeBufferPointer`.

To clarify why I think that's bad: it compromises the ability of types to communicate invariants in safe code. If we have a `Span` and we're currently writing _safe_ code, we can rely on its invariants, even if we've also used `withUnsafeBufferPointer` (as long as our _unsafe_ code is correct, e.g. we don't dereference the buffer pointer after escaping it). If we have a `Cell` and we're currently writing safe code, we can't be fully confident that its invariants are upheld, because we might have previously used `Cell.unsafeBorrow`; if so, then all it takes to break the contract is a subsequent _safe_ operation like `Cell.value`.

However, I now realize that `Cell.unsafeBorrow` wouldn't be the _only_ example, because `Span.unsafeCells()` would be another.

* * *

1. That is, this would be okay: `let danglingPtr = withUnsafePointer(x) { ptr in ptr }` as long as `danglingPtr` itself is unused.

---

<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:** [October 21, 2026, 10:47pm UTC](https://forums.swift.org/t/se-0556-cell-constcell-and-volatile/89988/27 "2026-10-21T22:47:12Z")

</div>

> [@aviva](#):
>
> I think we need to figure out the answer to this before we can let people do "unconcerning" data races in safe code. The worst you can do in safe code today is copy out and overwrite without actually doing a CAS, which is not great (it's sort of a "high level" data race) but not the same as a data race from a UB perspective AFAIK.

One thing that I think we'd have to be wary of to avoid arbitrary UB is not so much tearing loads, but redundant loads. Given an ordinary load in LLVM IR, the compiler might choose to actually load multiple times (say because it would have to spill a register to the stack anyway), expecting the same value to be available if there are no apparent writes to the memory location on the same thread. If a `Cell` is `Sendable`, we might have to implement the `value` getter using a volatile and/or atomic load to rule that out.

---

<div class="post-metadata">

**Author:** ![aviva](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/aviva/32/44327_2.png) [@aviva](https://forums.swift.org/u/aviva)\
**Post date:** [October 21, 2026, 10:49pm UTC](https://forums.swift.org/t/se-0556-cell-constcell-and-volatile/89988/28 "2026-10-21T22:49:06Z")

</div>

> [@Joe\_Groff](#):
>
> If a `Cell` is `Sendable`, we might have to implement the `value` getter using a volatile and/or atomic load to rule that out.

This seems undesirable unless we can make it conditional on the `Sendable` conformance, although I figure we can...

---

<div class="post-metadata">

**Author:** ![ellie20](https://avatars.discourse-cdn.com/v4/letter/e/6f9a4e/32.png) [@ellie20](https://forums.swift.org/u/ellie20)\
**Post date:** [October 22, 2026, 12:53am UTC](https://forums.swift.org/t/se-0556-cell-constcell-and-volatile/89988/29 "2026-10-22T00:53:47Z")

</div>

> [@Joe\_Groff](#):
>
> One thing that I think we'd have to be wary of to avoid arbitrary UB is not so much tearing loads, but redundant loads. Given an ordinary load in LLVM IR, the compiler might choose to actually load multiple times (say because it would have to spill a register to the stack anyway), expecting the same value to be available if there are no apparent writes to the memory location on the same thread. If a `Cell` is `Sendable`, we might have to implement the `value` getter using a volatile and/or atomic load to rule that out.

I believe that, in C, and presumably in LLVM as well, it is formally UB to atomically read a variable if another thread simultaneously non-atomically writes to it; for it to not UB, the write must be atomic as well. (I'm not sure if the same would apply to a volatile read.) Admittedly, I can't think of an optimization that would exploit this UB, but if one hypothetically existed, even an atomic (or volatile) read would be dangerous.\[1\] Personally, I'd find it uncomfortable if Swift emits LLVM IR that formally exhibits UB, just because there currently happens to not be an optimization exploiting that UB.

* * *

1. Perhaps such an optimization is more likely if cross-language LLVM LTO is used.

---

<div class="post-metadata">

**Author:** ![fclout](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/fclout/32/36155_2.png) [@fclout](https://forums.swift.org/u/fclout)\
**Post date:** [October 22, 2026, 1:04am UTC](https://forums.swift.org/t/se-0556-cell-constcell-and-volatile/89988/30 "2026-10-22T01:04:07Z")

</div>

Yes, on the AliasedSpan I asked about exactly what it means for load/store semantics and we would probably have to answer these questions to bring the Sendable conformance back:

> [@\[Pitch\] Aliased Span and Ref types](https://forums.swift.org/t/pitch-aliased-span-and-ref-types/88529/14):
>
> - If the optimizer sees two consecutive loads of the same index, can they be combined?
> - If the optimizer sees two adjacent accesses, can it combine them into one?
> - If the optimizer needs to stash a value it loaded from an Aliased\*, instead of spilling to the stack and reload from there, can it choose to reload from the Aliased\*?

The answers are probably no, yes, extremely no.

---

<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:** [October 22, 2026, 3:24am UTC](https://forums.swift.org/t/se-0556-cell-constcell-and-volatile/89988/31 "2026-10-22T03:24:02Z")

</div>

> [@\[Pitch\] Aliased Span and Ref types](https://forums.swift.org/t/pitch-aliased-span-and-ref-types/88529/14):
>
> - If the optimizer sees two consecutive loads of the same index, can they be combined?

If our threshold of acceptable slop is "you get some arbitrary but well-behaved value", I think we could get away with combining loads. It would be as if both loads observed the same value, but as long as those loaded values behave coherently afterward I think we're OK. But yeah, we definitely wouldn't be able to allow redundant reloads, since that would give us potentially different representations of the "same" value.

---

<div class="post-metadata">

**Author:** ![Alejandro](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/alejandro/32/46197_2.png) [@Alejandro](https://forums.swift.org/u/Alejandro)\
**Post date:** [October 27, 2026, 8:03pm UTC](https://forums.swift.org/t/se-0556-cell-constcell-and-volatile/89988/32 "2026-10-27T20:03:58Z")

</div>

> [@benlings](#):
>
> What will the availability of this type be on ABI-stable platforms? Can it be back deployed? How does this relate to the `_Cell` struct used by `Mutex`?

The availability will be whatever Swift version this releases in. This is the same type as `_Cell` used by `Mutex`, but public.

---

<div class="post-metadata">

**Author:** ![aviva](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/aviva/32/44327_2.png) [@aviva](https://forums.swift.org/u/aviva)\
**Post date:** [October 27, 2026, 8:45pm UTC](https://forums.swift.org/t/se-0556-cell-constcell-and-volatile/89988/33 "2026-10-27T20:45:12Z")

</div>

> [@Alejandro](#):
>
> This is the same type as `_Cell` used by `Mutex`, but public.

Except (potentially) with different `Sendable` conformance, right?

---

<div class="post-metadata">

**Author:** ![benlings](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/benlings/32/13011_2.png) [@benlings](https://forums.swift.org/u/benlings)\
**Post date:** [October 27, 2026, 8:46pm UTC](https://forums.swift.org/t/se-0556-cell-constcell-and-volatile/89988/34 "2026-10-27T20:46:06Z")

</div>

Thanks @Alejandro . The reason for asking about both `_Cell` and back deployment was in case it’s possible to rename `_Cell` and therefore make this deployable back to when `_Cell` was introduced (Swift 6.0)?

---

<div class="post-metadata">

**Author:** ![Alejandro](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/alejandro/32/46197_2.png) [@Alejandro](https://forums.swift.org/u/Alejandro)\
**Post date:** [October 27, 2026, 8:47pm UTC](https://forums.swift.org/t/se-0556-cell-constcell-and-volatile/89988/35 "2026-10-27T20:47:17Z")

</div>

Well `_Cell` is not `Sendable` because it's just an implementation detail for `Mutex` which declares its own `Sendable` conformance, but if we decide that `Cell` and `ConstCell` should be `Sendable where T: Sendable & FullyInhabited` then yes it would be different.

---

<div class="post-metadata">

**Author:** ![Alejandro](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/alejandro/32/46197_2.png) [@Alejandro](https://forums.swift.org/u/Alejandro)\
**Post date:** [October 27, 2026, 8:49pm UTC](https://forums.swift.org/t/se-0556-cell-constcell-and-volatile/89988/36 "2026-10-27T20:49:56Z")

</div>

We could, but `_Cell` is in `Synchronization` which means we'd need to sink it into the stdlib (possible, but more grunt work). We may be able to back deploy types soon though with some work that Doug has been doing here: [Extend @backDeployed to @frozen structs and enums with non-resilient layouts by DougGregor · Pull Request #93430 · swiftlang/swift · GitHub](https://github.com/swiftlang/swift/pull/93430)

---

<div class="post-metadata">

**Author:** ![John\_McCall](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/john_mccall/32/1351_2.png) [@John\_McCall](https://forums.swift.org/u/John_McCall)\
**Post date:** [October 31, 2026, 4:29pm UTC](https://forums.swift.org/t/se-0556-cell-constcell-and-volatile/89988/37 "2026-10-31T16:29:07Z")

</div>

I know this proposal is just introducing the library types, but I think it could be clearer about the wider thinking for imported types. For example, `ConstCell` is necessary _because_ we want to make `Cell` support `nonmutating` mutation _because_ we want the safe import of non-`const` C/C++ pointers and arrays to still use non-`Mutable` `Ref` and `Span` so that they aren’t burdened with non-copyability.

(You might think that, on a more basic level, we also don’t want the exclusivity restrictions of a `mutating` access. However, on that basic level, we _also_ do not want the exclusivity restrictions of a `nonmutating` access. I am fairly convinced that we have to model cells as involving new basic kinds of aliased access. Once you accept that, it also gives us clearer directions for a lot of other related problems. I’ll talk through this in a separate reply.)

(The non-import uses of `Cell` also want this property of `nonmutating` mutation, but if the import uses didn’t, we would clearly just want to use different types to model them.)

---

<div class="post-metadata">

**Author:** ![John\_McCall](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/john_mccall/32/1351_2.png) [@John\_McCall](https://forums.swift.org/u/John_McCall)\
**Post date:** [October 31, 2026, 5:58pm UTC](https://forums.swift.org/t/se-0556-cell-constcell-and-volatile/89988/38 "2026-10-31T17:58:30Z")

</div>

On the access front, the current proposal is making a valiant attempt at a purely type-based modeling of aliases, but I think this effort would be more successful if we acknowledged that aliased and non-aliased accesses need to be distinguished in the language.

That is, we can currently classify accesses into various basic kinds: initialization, consumption/deinitialization, reading/borrowing, mutation, and assignment. Of these, the first two really must be non-aliased, at least for arbitrary types: there’s no way to allow the construction or destruction of an object to overlap with uses of it without allowing all sorts of initialization unsafety. And assignment is always “instantaneous”, in the sense that there isn’t generally any way to synchronously overlap the assignment: we produce the new value, then assign it in as an uninterrupted step. (_Concurrent_ code can overlap the assignment, but this is inherently problematic: it’s a data race and has an undefined effect even if the language somehow limits it from causing any other damage.)

But with reads and modifications we can usefully talk about what might happen if we allow them to overlap. And while we don’t want that to infect the standard model of Swift programming, we can still allow people to opt in to it in specific places. For that, we need two things: the idea, above, of aliased read and modification _accesses_; and an idea of aliased _storage_.

The basic rule here is that something about the storage declaration determines how you access it:

- Ordinary storage in Swift is not aliased, which means we will enforce exclusivity for accesses to it, _even aliased accesses_. If something asks for aliased read access to non-aliased storage, semantically Swift will perform a non-aliased read, and the client will just ignore the exclusivity guarantees we gave it.\[1\] This is still safe as long as we only do memory-safe things during the access, which should be true even for these aliased accesses.
- Aliased storage would not enforce exclusivity and would only offer aliased accesses at an implementation level. Non-aliased accesses can be emulated for copyable types by copying into temporary, non-aliased storage and then accessing that. But of course this has semantic consequences: a non-aliased access (of either kind) will not observe changes in the original storage that happen after the read begins, and a non-aliased modification will assign the temporary back in its entirety and therefore will silently overwrite any overlapping changes to the original storage. We may want to limit these emulations or make the user acknowledge them somehow.

So, for example, we could have `borrowing(aliased)` parameters. In the caller, the access done to the argument when passing to such a parameter is naturally an aliased read. In the callee, the parameter is aliased storage. If you pass the parameter as the argument to another aliased parameter, the argument can be safely forwarded without introducing copies and therefore without changing its lifetime.

The implication for `Cell` is two-fold. The first is that the functions for creating aliased `Ref`s and `Span`s — I can’t seem to find these in the proposal, but surely they must be coming — can take `borrowing(aliased)` or `inout(aliased)` parameters, weakening the requirements on the code that produces them. And the second is that, rather than forcing a copy on every access to the safe property `value` by forcing the access through get/set, we instead give `value` _aliased_ `borrow` and `mutate` accessors, reflecting the weaker guarantees they offer. A copy is then required in the caller if it needs non-aliased access — but if it _doesn’t_, we can safely forward without one.

Finally, C and C++ functions taking borrowed or `inout` parameters would generally allow them to be aliased,\[2\] and things like fields of foreign reference types would generally be considered aliased as well.

All of this has a number of immediate beneficial consequences:

- Safe accesses to cell values (i.e. not using `unsafeBorrow`) should not need a copy if an aliased access is okay. In particular, passing them to C++ methods will generally be doable without a copy. In _particular_ particular, this means _non-copyable_ values in aliased storage can be worked with totally safely unless you need to use something from Swift that requires exclusive access; if you’re just e.g. calling imported methods on an imported non-copyable type, you should be totally fine.
- We have a language mechanism for projecting members of aliased storage, e.g. to get an aliased `Ref` to a member of a different aliased `Ref`. Otherwise we’d need to figure out some way to, I guess, project a key path.
- We should significantly reduce the number of `Cell`s we need in imported APIs, especially in foreign reference types. They should only be necessary when we’re bringing something in as a `Ref` or `Span`. This should reduce the clutter of the resulting code a lot.

* * *

1. We could also weaken the exclusivity checking to permitting overlapping aliased accesses. We still have to _do_ exclusivity checking, however, to establish that these accesses don’t overlap non-aliased accesses that they would conflict with. For example, we can have simultaneous aliased and non-aliased reads, and we can have simultaneous aliased reads and modifications, but we cannot allow modifications of either kind to overlap with non-aliased reads, and we cannot allow any other access to overlap with non-aliased modifications.

2. unless they’re marked `restrict`, I suppose

---

<div class="post-metadata">

**Author:** ![Andrew\_Trick](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/andrew_trick/32/323_2.png) [@Andrew\_Trick](https://forums.swift.org/u/Andrew_Trick)\
**Post date:** [November 2, 2026, 5:23pm UTC](https://forums.swift.org/t/se-0556-cell-constcell-and-volatile/89988/39 "2026-11-02T17:23:02Z")

</div>

Regarding aliasing and mutation: this is an implementation aspect of `Cell` I’d like to bring up. We’re faced with two options:

A. Make all access to `Cell`, `ConstCell`, or any raw-layout type opaque to aliasing and mutation analysis throughout the compiler.

B. Assume any Swift variable is subject to aliasing and mutation at any time.

We should go with option A because the aliasing and mutation guarantees provided by Swift’s exclusivity rules are one of the most important memory optimization strategies that we have.

In practice, this means the value in a `Cell` can only be accessed through `Builtin.addressOfRawLayout`, that builtin must be preserved throughout compilation, and the optimizer can’t reason about the provenance of the address it gets from the builtin. Lowering to LLVM needs to preserve the builtin's guarantees because `noalias` can potentially be applied to raw-layout values--the implementation doesn’t handle this yet.

I think it’s worth mentioning this in the proposal because implementers need to understand and agree on a common strategy. It will be difficult to maintain without bugs creeping in because there’s a strong incentive for optimizations to work around any barriers we put up for correctness, and there's no way to generally verify safety in this respect. If the rules are written down in an authoritative place, we can refer people to it whenever touching related code.

---

<div class="post-metadata">

**Author:** ![Andrew\_Trick](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/andrew_trick/32/323_2.png) [@Andrew\_Trick](https://forums.swift.org/u/Andrew_Trick)\
**Post date:** [November 2, 2026, 5:27pm UTC](https://forums.swift.org/t/se-0556-cell-constcell-and-volatile/89988/40 "2026-11-02T17:27:48Z")

</div>

On the concurrency side...

`ConstCell` should not be Sendable, even with type constraints. Sending a `ConstCell` across threads is equivalent to passing an `inout` across threads from a correctness perspective. The concurrency model is about more than memory safety. It also protects against data corruption caused by race conditions. Those issues are incredibly difficult to reproduce and diagnose. `Atomic` is meant to be `Sendable`. It has all the benefits of `Cell` and comes with concurrency semantics.

Let's find an alternative to `VolatileCell`. It would be a shame to add the concept of volatile in the Swift language at the level of data types. `Atomic` should be used instead signal the intention to share values across threads, it has meaningful concurrency semantics, and lets you express the desired memory ordering.

Any true need for volatile behavior (who knows what the actual semantics are) is so niche that it makes more sense to call into volatile load/store primitives in those cases. It seems reasonable for those primitives to be in the standard library.

[Previous page](https://forums.swift.org/t/se-0556-cell-constcell-and-volatile/89988.md?page=1)

[Next page](https://forums.swift.org/t/se-0556-cell-constcell-and-volatile/89988.md?page=3)
