# 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:** 20\
**Page:** 1

<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 10, 2019, 5:56pm UTC](https://internals.rust-lang.org/t/rc-and-internal-mutability/11463/1 "2019-12-10T17:56:07Z")

</div>

> > [@CAD97](#):
> >
> > Just to note: this [[(`A`)`Rc::get_mut_unchecked`](https://doc.rust-lang.org/nightly/std/rc/struct.Rc.html#method.get_mut_unchecked)] exposes shared internal mutability to ( `A` ) `Rc` where the stable API only exposes (runtime checked) unshared internal mutability (of the payload) [[(`A`)`Rc::get_mut`](https://doc.rust-lang.org/nightly/std/rc/struct.Rc.html#method.get_mutd)].
> 
> > [@RalfJung](#):
> >
> > @CAD97 I am not entirely sure what you mean... I do not see any interior mutability here. That term is specifically tied to `UnsafeCell` in Rust, and there is none here.
> > 
> > Would you say that the fact that we can create a raw pointer into a `Box` "exposes shared internal mutability"? Because this operation is very similar.
> 
> > [@SimonSapin](#):
> >
> > I think another way to phrase @CAD97’s comment/concern is: given `let some_box: Box<u32> = /*…*/; let ptr = &mut *some_box;` , under what conditions if any is it safe to write through `ptr` while `some_box` still exists (and therefore "shares" the same `u32` value by having another pointer to it).
> > 
> > Compare with `Arc::get_mut` which ensures there’s only one reference to the value and therefore none of this kind of "sharing".
> 
> > [@RalfJung](#):
> >
> > > [@SimonSapin](#):
> > >
> > > under what conditions if any is it safe to write through ptr while some\_box still exists (and therefore "shares" the same u32 value by having another pointer to it).
> > 
> > That goes deep into the aliasing model -- and is not specific to `Box` or `Arc` at all, I would say.
> > 
> > Your example uses a safe reference for `ptr` so you won't be able to do anything illegal, but assuming you meant `&mut *some_box as *mut _` , then the answer that Stacked Borrows gives is that you may use `ptr` until you do _anything_ with `some_box` the next time -- including things like moving it or borrowing from it. But of course Stacked Borrows is just an experiment, not the final answer.
> 
> > [@SimonSapin](#):
> >
> > > [@RalfJung](#):
> > >
> > > not specific to `Box` or `Arc` at all
> > 
> > Yeah a code sample just a way to express something without terminology
> > 
> > > [@RalfJung](#):
> > >
> > > assuming you meant `&mut *some_box as *mut _`
> > 
> > Oops, yes I did.
> > 
> > * * *
> > 
> > Ok I understand you might not have a final answer yet for some cases of aliasing rules. So the question is: does documenting `Arc::get_mut_unchecked` as safe to use while other `Arc` references to the same value exist (just not deref’ed) constrain a future decision we might want to make for aliasing rules? As opposed to documenting that it may only be used if only one `Arc` and no `Weak` exist.
> 
> > [@RalfJung](#):
> >
> > Oh, now I see what you are getting at. Good question.
> > 
> > This can only become a problem if `Arc` / `Rc` is ever understood by the optimizer to mean something. With `Box` , it is basically a primitive type and also uses `Unique` , hence the strict rules; for `Arc` that seems unlikely.
> > 
> > > [@SimonSapin](#):
> > >
> > > while other Arc references to the same value exist (just not deref’ed)
> > 
> > Deref would certainly be bad, but even cloning creates a reference to the `ArcInner` , right? None of these should be allowed.
> > 
> > I think we'd be on the safe side if we said that other `Arc` might exist but nothing may be done with them (not even a move), similar to `Box` .

-- @CAD97, @SimonSapin, @RalfJung [rust-lang/pull#6251 (discussion)](https://github.com/rust-lang/rust/pull/62451#discussion_r349833751)

> [@RalfJung](#):
>
> > [@CAD97](#):
> >
> > In fact `_.as_ptr() as *mut _` occurs a lot, correctly, in a couple pointer-heavy libraries I wrote recently. The reason is for (`A`)`Rc` to `ptr::NonNull` conversion: they only stably expose `into_raw() -> *const _` currently, and `into_raw_non_null() -> ptr::NonNull` is not yet stable.
> 
> Notice that `Rc::into_raw` returns a ptr with "shared ref" provenance. So actually writing to that pointer is UB. (In fact, this likely makes `Rc::from_raw(Rc::into_raw(rc)).get_mut()` unsound .)

-- @RalfJung [rust-lang/rust-clippy#4774 (comment)](https://github.com/rust-lang/rust-clippy/issues/4774#issuecomment-563915004)

> ```rust
> struct RcBox<T: ?Sized> {
> strong: Cell<usize>,
> weak: Cell<usize>,
> value: T,
> }
> 
> pub struct Rc<T: ?Sized> {
> ptr: NonNull<RcBox<T>>,
> phantom: PhantomData<RcBox<T>>,
> }
> impl<T: ?Sized> Rc<T> {
> pub fn into_raw(this: Self) -> *const T {
> let ptr: *const T = &*this;
> mem::forget(this);
> ptr
> }
> 
> pub unsafe fn from_raw(ptr: *const T) -> Self {
> let offset = data_offset(ptr);
> 
> // Reverse the offset to find the original RcBox.
> let fake_ptr = ptr as *mut RcBox<T>;
> let rc_ptr = set_data_ptr(fake_ptr, (ptr as *mut u8).offset(-offset));
> 
> Self::from_ptr(rc_ptr)
> }
> 
> #[inline]
> pub fn into_raw_non_null(this: Self) -> NonNull<T> {
> // safe because Rc guarantees its pointer is non-null
> unsafe { NonNull::new_unchecked(Rc::into_raw(this) as *mut _) }
> }
> 
> ```

-- liballoc/rc.rs ([`8960acf`](https://github.com/rust-lang/rust/blob/8960acf/src/liballoc/rc.rs))

- (`A`)`Rc` are shared pointers. That part is obvious.
- (`A`)`Rc` expose mutability when they are runtime-checked unique. That part is stable (`get_mut`).
- (`A`)`Rc` expose shared mutability when `unsafe`-guaranteed no other pointers are "used" during the mutation. That part is unstable (`get_mut_unchecked`).
- (`A`)`Rc` are represented as `ptr::NonNull<{ strong: UnsafeCell<usize>, weak: UnsafeCell<usize>, value: T }>`.

There are three potential solutions to the immediate issue I see here (that `Rc::from_raw(Rc::into_raw(_))` gives "immutable/shared provenance" to the `RcBox` pointer, making `get_mut` unsound):

- The aliasing model is wrong, liballoc is correct.
- Fix the implementation of `into_raw` to not use shared references. Unfortunately, I think this would require `&raw` or some other way of offsetting pointers without manifesting a reference.
- Admit (`A`)`Rc` is an internal mutability type and store the value as `UnsafeCell<T>` in `RcBox`/`ArcInner`. (We really should unify those naming schemes.)

I think the third solution is the best here. The correctness of (`A`)`Rc::get_mut`(`_unchecked`) is really subtle without the use of `UnsafeCell` and interacts with minutia of the not-yet-specified aliasing model. If we do use `UnsafeCell`, it's trivial what the requirements of these functions are, rather than being scattered around the module to never accidentally change the provenance of the pointer to the inner blob.

---

<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 10, 2019, 6:23pm UTC](https://internals.rust-lang.org/t/rc-and-internal-mutability/11463/2 "2019-12-10T18:23:17Z")

</div>

> [@CAD97](#):
>
> Admit ( `A` ) `Rc` is an internal mutability type and store the value as `UnsafeCell<T>` in `RcBox` / `ArcInner` . (We really should unify those naming schemes.)

I don't think that helps -- assuming the implementation of `into_raw` stays as it. It goes through `deref`, which returns `&T`, which is read-only.

So you'll need to adjust `into_raw` to not go through `deref`. And if you do that, I think there is no reason to add an `UnsafeCell`.

---

<div class="post-metadata">

**Author:** ![withoutboats](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/withoutboats/32/4560_2.png) [@withoutboats](https://internals.rust-lang.org/u/withoutboats)\
**Post date:** [December 10, 2019, 6:31pm UTC](https://internals.rust-lang.org/t/rc-and-internal-mutability/11463/3 "2019-12-10T18:31:56Z")

</div>

> [@CAD97](#):
>
> Fix the implementation of `into_raw` to not use shared references. Unfortunately, I think this would require `&raw` or some other way of offsetting pointers without manifesting a reference.

Like just using `ptr::offset` by the size\_of the header value?

---

<div class="post-metadata">

**Author:** ![RustyYato](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/rustyyato/32/13627_2.png) [@RustyYato](https://internals.rust-lang.org/u/RustyYato)\
**Post date:** [December 10, 2019, 7:01pm UTC](https://internals.rust-lang.org/t/rc-and-internal-mutability/11463/4 "2019-12-10T19:01:17Z")

</div>

And also aligning the result of the offset. This relies on the layout of `RcBox`/`ArcInner`.

Something like this should work.

```rust
let ptr = inner_ptr.add(size_of::<usize>());
let align_offset = ptr.align_offset(align_of::<T>());
let ptr: *const u8 = ptr.add(align_offset);

```

---

<div class="post-metadata">

**Author:** ![SimonSapin](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/simonsapin/32/3158_2.png) [@SimonSapin](https://internals.rust-lang.org/u/SimonSapin)\
**Post date:** [December 10, 2019, 7:04pm UTC](https://internals.rust-lang.org/t/rc-and-internal-mutability/11463/5 "2019-12-10T19:04:16Z")

</div>

Would using the offset from the return value of `core::alloc::Layout::extend` be less error-prone?

---

<div class="post-metadata">

**Author:** ![197g](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/197g/32/7276_2.png) [@197g](https://internals.rust-lang.org/u/197g)\
**Post date:** [December 10, 2019, 7:17pm UTC](https://internals.rust-lang.org/t/rc-and-internal-mutability/11463/6 "2019-12-10T19:17:12Z")

</div>

`Arc` and `Rc` already manually offset their pointers as part of their `from_raw` implementations and as part of this calculate the offset of the data from the `RcBox` itself. Since `liballoc` can control the exact layout of the inner `RcBox` that is not a problem. What's stopping it from using the same logic in reverse (or rather forward in memory) to perform the initial offsetting?

```rust
// For Rc:
pub fn into_raw(this: Self) -> *const T {
    // Re-use the existing method of calculating the offset.
    // The deref is only temporary and does not create the final ptr.
    let offset = data_offset(&*this);
    let raw = NonNull::into_raw(this.ptr);
    unsafe {
        (raw as *const u8).offset(offset) as *const T
    }
}

```

Or am I missing something here? (Except that the code will be slightly more complicated for offsetting the pointer in the case of `T: !Sized`).

---

<div class="post-metadata">

**Author:** ![RustyYato](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/rustyyato/32/13627_2.png) [@RustyYato](https://internals.rust-lang.org/u/RustyYato)\
**Post date:** [December 10, 2019, 8:35pm UTC](https://internals.rust-lang.org/t/rc-and-internal-mutability/11463/7 "2019-12-10T20:35:02Z")

</div>

Yes, I didn't realize that `Layout` had that! Cool.

---

<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 10, 2019, 8:56pm UTC](https://internals.rust-lang.org/t/rc-and-internal-mutability/11463/8 "2019-12-10T20:56:08Z")

</div>

Specifically, here are the computations that are used for offsetting:

```rust
unsafe fn data_offset<T: ?Sized>(ptr: *const T) -> isize {
    // Align the unsized value to the end of the `RcBox`.
    // Because it is ?Sized, it will always be the last field in memory.
    data_offset_align(align_of_val(&*ptr))
}

/// Computes the offset of the data field within `RcBox`.
///
/// Unlike [`data_offset`], this doesn't need the pointer, but it works only on `T: Sized`.
fn data_offset_sized<T>() -> isize {
    data_offset_align(align_of::<T>())
}

#[inline]
fn data_offset_align(align: usize) -> isize {
    let layout = Layout::new::<RcBox<()>>();
    (layout.size() + layout.padding_needed_for(align)) as isize
}

```

Current `from_raw`:

```rust
    pub unsafe fn from_raw(ptr: *const T) -> Self {
        let offset = data_offset(ptr);

        // Reverse the offset to find the original RcBox.
        let fake_ptr = ptr as *mut RcBox<T>;
        let rc_ptr = set_data_ptr(fake_ptr, (ptr as *mut u8).offset(-offset));

        Self::from_ptr(rc_ptr)
    }

```

I agree with @197g here and I think we can just re-implement `into_raw` to use a manual offset as well, implemented something like

```rust
    pub fn into_raw(this: Self) -> *const T {
        let ptr: *mut RcBox<T> = NonNull::as_ptr(this.ptr);
        mem::forget(this);

        let offset = data_offset(&*ptr.value);
        (ptr as *mut u8).offset(offset) as *const u8 as *const T
    }

```

---

<div class="post-metadata">

**Author:** ![bill\_myers](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/bill_myers/32/4085_2.png) [@bill\_myers](https://internals.rust-lang.org/u/bill_myers)\
**Post date:** [December 10, 2019, 9:57pm UTC](https://internals.rust-lang.org/t/rc-and-internal-mutability/11463/9 "2019-12-10T21:57:04Z")

</div>

Has the idea of changing Stacked Borrows so that creating a reference only has an effect when it is first used (and thus never does if converted to a raw pointer immediately) been decided against?

---

<div class="post-metadata">

**Author:** ![Ixrec](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/ixrec/32/6754_2.png) [@Ixrec](https://internals.rust-lang.org/u/Ixrec)\
**Post date:** [December 10, 2019, 10:02pm UTC](https://internals.rust-lang.org/t/rc-and-internal-mutability/11463/10 "2019-12-10T22:02:34Z")

</div>

I haven't heard of any suggestions or explicitly rejected alternatives along these lines. I would guess that's because you can't know when a reference is "used" without analyzing all transitively called functions, and making all reference optimizations depend on that would be bad. And I don't see how that would help with the issues in this thread anyway.

(at least, I assume by "used" you mean something more interesting than just passing the reference to another function, which is already a semantically significant operation in Stacked Borrows)

---

<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 14, 2019, 8:11pm UTC](https://internals.rust-lang.org/t/rc-and-internal-mutability/11463/11 "2019-12-14T20:11:27Z")

</div>

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.

The implementation currently uses

```rust
pub struct $RcBorrow<'a, T: ?Sized> {
    raw: ptr::NonNull<T>,
    marker: PhantomData<&'a $Rc<T>>
}

impl<'a, T: ?Sized> From<&'a $Rc<T>> for $RcBorrow<'a, T> {
     fn from(v: &'a $Rc<T>) -> $RcBorrow<'a, T> {
        $RcBorrow {
            raw: (&**v).into(),
            marker: PhantomData,
        }
    }
}

impl<'a, T: ?Sized> $RcBorrow<'a, T> {
    /// Convert this borrowed pointer into an owned pointer.
    pub fn upgrade(this: Self) -> $Rc<T> {
        unsafe { $Rc::clone(&ManuallyDrop::new($Rc::from_raw(this.raw.as_ptr()))) }
    }

    /// Convert this borrowed pointer into a standard reference.
    pub fn downgrade(this: Self) -> &'a T {
        unsafe { &*this.raw.as_ptr() }
    }
}

```

As I understand it, we've determined that this way of implementing `into_raw` is incorrect when using the current Stacked Borrows rules, so my "`as_raw`" implementation would also be unsound to turn into a `&$Rc<T>` to clone, as it's lost write provenance over the location of `T`. (And a unique `$Rc<T>` can be used to write.)

If I understand correctly, the way to make this sound would be to

- Provide a `$Rc::<T>::as_raw(&self) -> *const T` function that uses pointer manipulation instead of `Deref`,
- Use that in the implementation of `$RcBorrow<'_, T>`, and
- Say that the implicit case of `&T` -\> `$Rc<T>` where `T` is known statically to only be allocated behind `$Rc` is actually unsound (unsafe when used to write).

I'm ok with that for this library, but I know `&T` -\> `$Rc<T>` is used in the wild.

---

<div class="post-metadata">

**Author:** ![Tom-Phinney](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/tom-phinney/32/3299_2.png) [@Tom-Phinney](https://internals.rust-lang.org/u/Tom-Phinney)\
**Post date:** [December 14, 2019, 8:18pm UTC](https://internals.rust-lang.org/t/rc-and-internal-mutability/11463/12 "2019-12-14T20:18:19Z")

</div>

cc @RalfJung

---

<div class="post-metadata">

**Author:** ![xfix](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/xfix/32/2726_2.png) [@xfix](https://internals.rust-lang.org/u/xfix)\
**Post date:** [December 15, 2019, 12:22am UTC](https://internals.rust-lang.org/t/rc-and-internal-mutability/11463/13 "2019-12-15T00:22:01Z")

</div>

`Rc::get_mut_unchecked` probably should require no other `Rc` and `Weak` pointers to the same value. It's possible to cause unsafety without violating "Any other `Rc` or `Weak` pointers to the same value must not be dereferenced for the duration of the returned borrow" requirement.

```rust
#![feature(get_mut_unchecked)]

use std::rc::Rc;

unsafe fn f<'a>(mut r: Rc<&'a str>, s: &'a str) {
    *Rc::get_mut_unchecked(&mut r) = s;
}

fn main() {
    let x = Rc::new("Hello, world!");
    {
        let s = String::from("Replaced");
        unsafe { f(Rc::clone(&x), &s) };
    }
    println!("{}", x);
}

```

`Rc::into_raw` probably should return a pointer without involving deref however (similarly to how `Weak` already does it in its `as_raw` method).

I don't think `Rc` should be modified to use `UnsafeCell`, as this changes variance, when the current variance is fine, and this is not necessary when `Rc::into_raw` could be changed instead.

---

<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 15, 2019, 11:12pm UTC](https://internals.rust-lang.org/t/rc-and-internal-mutability/11463/14 "2019-12-15T23:12:04Z")

</div>

I [opened a PR to adjust the implementation of `into_raw`](https://github.com/rust-lang/rust/pull/67339). The "`as_raw` issue" remains. I also [raised the `as_mut_unchecked` concern on the tracking issue](https://github.com/rust-lang/rust/issues/63292#issuecomment-565766453).

---

<div class="post-metadata">

**Author:** ![dhm](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/dhm/32/4879_2.png) [@dhm](https://internals.rust-lang.org/u/dhm)\
**Post date:** [December 16, 2019, 11:46am UTC](https://internals.rust-lang.org/t/rc-and-internal-mutability/11463/15 "2019-12-16T11:46:59Z")

</div>

Now that we are at it, do use `ManuallyDrop` rather than `forget`, then.

---

<div class="post-metadata">

**Author:** ![bill\_myers](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/bill_myers/32/4085_2.png) [@bill\_myers](https://internals.rust-lang.org/u/bill_myers)\
**Post date:** [December 18, 2019, 1:42pm UTC](https://internals.rust-lang.org/t/rc-and-internal-mutability/11463/16 "2019-12-18T13:42:04Z")

</div>

> [@Ixrec](#):
>
> I haven't heard of any suggestions or explicitly rejected alternatives along these lines. I would guess that's because you can't know when a reference is "used" without analyzing all transitively called functions, and making all reference optimizations depend on that would be bad. And I don't see how that would help with the issues in this thread anyway.
> 
> (at least, I assume by "used" you mean something more interesting than just passing the reference to another function, which is already a semantically significant operation in Stacked Borrows)

The miri implementation can just execute code to find uses (i.e. actual pointer reads and writes).

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 said, stricter rules might be better to reduce accidental bugs later (i.e. someone incorrectly changing code to use a previously unused invalid reference).

---

<div class="post-metadata">

**Author:** ![Ixrec](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/ixrec/32/6754_2.png) [@Ixrec](https://internals.rust-lang.org/u/Ixrec)\
**Post date:** [December 18, 2019, 1:47pm UTC](https://internals.rust-lang.org/t/rc-and-internal-mutability/11463/17 "2019-12-18T13:47:20Z")

</div>

> [@bill\_myers](#):
>
> The miri implementation can just execute code to find uses (i.e. actual pointer reads and writes).
> 
> As for optimizations, if a pointer is just passed around but never read/written from, then aliasing is irrelevant ...

It's not just about what miri can execute, it's also about what rustc can assume without whole-program analysis. Maybe I don't understand what you're suggesting, but afaik most of the optimizations described in the Stacked Borrows blog posts become invalid if the model has to start caring about whether certain kinds of "uses" occurred somewhere in a transitively called function. Unless you bake those "uses" into function types with an effect system or something.

---

<div class="post-metadata">

**Author:** ![bill\_myers](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/bill_myers/32/4085_2.png) [@bill\_myers](https://internals.rust-lang.org/u/bill_myers)\
**Post date:** [December 18, 2019, 1:51pm UTC](https://internals.rust-lang.org/t/rc-and-internal-mutability/11463/18 "2019-12-18T13:51:27Z")

</div>

rustc can just do optimizations as normal, and it should not cause problems because while the reference has incorrect aliasing assumptions, it is never read/written in practice so it effectively does not exist.

Obviously if a transitively called function started to read/write the reference, then it would be UB, but it's the unsafe code's responsibility to ensure this doesn't happen.

---

<div class="post-metadata">

**Author:** ![Ixrec](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/ixrec/32/6754_2.png) [@Ixrec](https://internals.rust-lang.org/u/Ixrec)\
**Post date:** [December 18, 2019, 1:54pm UTC](https://internals.rust-lang.org/t/rc-and-internal-mutability/11463/19 "2019-12-18T13:54:16Z")

</div>

Yeah I don't understand the suggestion at all. How can rustc "do optimizations as normal" on a reference without knowing whether it's a "real" reference or a reference that "effectively does not exist"?

---

<div class="post-metadata">

**Author:** ![bill\_myers](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/bill_myers/32/4085_2.png) [@bill\_myers](https://internals.rust-lang.org/u/bill_myers)\
**Post date:** [December 18, 2019, 2:03pm UTC](https://internals.rust-lang.org/t/rc-and-internal-mutability/11463/20 "2019-12-18T14:03:12Z")

</div>

rustc just assumes that it's a real reference. Since the reference is never read/written to, the fact that it's a reference with incorrect aliasing assumptions does not matter (or more precisely, you can only do optimizations for which this statement is true, but this should be all optimizations).

That's what the current compiler does and why the code in the standard library works.

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