# Rc and internal mutability

**URL:** <https://internals.rust-lang.org/t/rc-and-internal-mutability/11463>\
**Category:** libs\
**Created:** [December 10, 2019, 5:56pm UTC](https://internals.rust-lang.org/t/rc-and-internal-mutability/11463 "2019-12-10T17:56:07Z")\
**Posts on this page:** 3\
**Page:** 2

<div class="post-metadata">

**Author:** ![RalfJung](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/ralfjung/32/2415_2.png) [@RalfJung](https://internals.rust-lang.org/u/RalfJung)\
**Post date:** [December 22, 2019, 4:46pm UTC](https://internals.rust-lang.org/t/rc-and-internal-mutability/11463/21 "2019-12-22T16:46:52Z")

</div>

> [@CAD97](#):
>
> Does this mean that my [( `A` ) `RcBorrow::upgrade`](https://cad97.github.io/rowan/rc_borrow/struct.ArcBorrow.html#method.upgrade) is unsound? It's a not _too_ uncommon pattern to allow upgrading from `&T` where the `T` is statically known to be behind an ( `A` ) `Rc` to an ( `A` ) `Rc<T>` , and that's what that library is providing a type for.

Well, at least Stacked Borrows won't like that code. (Currently there are likely enough raw pointers around to camouflage the fault, but once we make a better effort at tracking those, I think this code would be ruled out). This reminds me a bit of [`container_of`'s unsoundess](https://github.com/Gilnaa/memoffset/pull/21), though I think your code here can be fixed if you find a better way to create `RcBorrow` -- something that's actually meant to be used with `from_raw`.

But this makes me a bit worried about how much other code like this is out there, that might be incompatible with any attempt at properly restricting the aliasing of references.

> [@bill\_myers](#):
>
> As for optimizations, if a pointer is just passed around but never read/written from, then aliasing is irrelevant, so it should be fine to have arbitrary aliasing information for it (although LLVM might make assumptions that invalidate this).
> 
> It's kind of an intuitive principle: if you never dereference a pointer, then it's equivalent to an integer.

That is not at all a given truth. We could decide for this, but it will severely limit optimizations. Being able to make assumptions even about "dead" values is extremely useful for a compiler.

---

<div class="post-metadata">

**Author:** ![CAD97](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/cad97/32/3460_2.png) [@CAD97](https://internals.rust-lang.org/u/CAD97)\
**Post date:** [December 22, 2019, 5:22pm UTC](https://internals.rust-lang.org/t/rc-and-internal-mutability/11463/22 "2019-12-22T17:22:21Z")

</div>

After I get `Arc::into_raw` fixed I'm planning to add an `Arc::as_raw` and use that for `ArcBorrow`, so at least that resource will be correct. (Temporarily, I should try and see if I can get a clone+`into_raw`+drop to optimize away the ref count manipulation to sidestep the issue until `as_raw` is stable. I'll probably publish the crate as soon as I do that.)

But yes, "reverse pointer projection" is used in the wild by my experience. Typically, it involves raw pointers for at-rest storage, but because we don't have `&raw`, the "forward projection" always requires going through references (and pointer offsetting in the "forward" direction feels unneeded at first approximation).

And any type that uses a pointer not to the start of the managed allocation is at risk of falling afoul of something like this.

Now the rest of this post is musing on the subject:

- Restricting arbitrary movement of `&mut` within an allocation makes complete and total sense. It has never even slightly been legal to go from `&mut struct.field` to `&mut struct`.
- But what about shared references? That's a lot murkier: if you _know_ that the outer bit is shared, why _can't_ you access it?
- The problem is when internal mutability comes into the the picture. Even with `UnsafeCell`, going from an internal reference to an external reference is definitely unsound.
- People who want to do tricky things with `unsafe` do typically stick to working with raw pointers (or wrappers thereof) rather than try to lie with at-rest references. But temporarily manifesting references is hard to avoid. (And that temporarily may be in a local variable...)
- So the solution seems to be make sticking with raw pointers and avoiding references easier, as quickly as possible.
- (How expensive would a miri cargobomb be? Probably prohibitive, even if "impossible" crates (e.g. ffi) were filtered out.)

* * *

EDIT: tried to write a "correct" version of `Arc::as_raw` in user code. [As it turns out, even a simple `drop(arc.clone())` cannot be optimized out](https://rust.godbolt.org/z/jkjBVQ), likely due to atomic operation requirements. [Rc seems to almost but not quite optimize out completely](https://rust.godbolt.org/z/i5YVGq).

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/system/32/14092_2.png) [@system](https://internals.rust-lang.org/u/system)\
**Post date:** [March 21, 2020, 5:22pm UTC](https://internals.rust-lang.org/t/rc-and-internal-mutability/11463/23 "2020-03-21T17:22:28Z")

</div>

Closed on March 21, 2020, 5:22pm UTC

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.

[Previous page](https://internals.rust-lang.org/t/rc-and-internal-mutability/11463.md?page=1)
