# Inconsistency between stacked borrows and my mental model

**URL:** <https://users.rust-lang.org/t/inconsistency-between-stacked-borrows-and-my-mental-model/47178>\
**Category:** uncategorized\
**Created:** [August 11, 2020, 6:09pm UTC](https://users.rust-lang.org/t/inconsistency-between-stacked-borrows-and-my-mental-model/47178 "2020-08-11T18:09:38Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![alice](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/alice/32/9777_2.png) [@alice](https://users.rust-lang.org/u/alice)\
**Post date:** [August 11, 2020, 6:09pm UTC](https://users.rust-lang.org/t/inconsistency-between-stacked-borrows-and-my-mental-model/47178/1 "2020-08-11T18:09:39Z")

</div>

So I was reading the [Stacked Borrows Implemented](https://www.ralfj.de/blog/2018/11/16/stacked-borrows-implementation.html) blog post, and realized that the following is not considered UB by stacked borrows:

```rust
fn main() {
    let x = &mut 1u8;

    // Create raw pointer
    let y = x as *mut u8;

    // Assert uniqueness of x
    *x = 7;

    // Create another raw pointer
    x as *mut u8;

    // Read x through y, which was created before we asserted uniqueness.
    let _val = unsafe { *y };
}

```

[playground](https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=687065c5b7395f26c9b6855975e4a58f)

I found it really surprising that reading through `y` is not considered UB here, because in my mental model, `y` would have been invalidated by asserting uniqueness with the write.

Note that if you remove the immediately-ignored raw pointer, miri will complain. I don't have any questions, I just found this surprising and wanted to share it.

---

<div class="post-metadata">

**Author:** ![mbrubeck](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/mbrubeck/32/648_2.png) [@mbrubeck](https://users.rust-lang.org/u/mbrubeck)\
**Post date:** [August 11, 2020, 6:29pm UTC](https://users.rust-lang.org/t/inconsistency-between-stacked-borrows-and-my-mental-model/47178/2 "2020-08-11T18:29:20Z")

</div>

I asked about a vaguely similar case in [this Zulip thread](https://rust-lang.zulipchat.com/#narrow/stream/136281-t-lang.2Fwg-unsafe-code-guidelines/topic/borrows.20invalidating.20raw.20pointers), where @RustyYato and @RalfJung replied:

> Yato: Yes, all raw pointers have the same tag (which is untagged) right now, so making a new raw pointer to the same location revives old raw pointers incorrectly.
> 
> RalfJ: note however that to match what LLVM does with `noalias` , we need to eventually also track raw pointers better and then that kind of code would stop working

So I believe this is a known limitation of the current Stacked Borrows model which might be changed in future revisions.

---

<div class="post-metadata">

**Author:** ![RalfJung](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/ralfjung/32/4469_2.png) [@RalfJung](https://users.rust-lang.org/u/RalfJung)\
**Post date:** [August 12, 2020, 12:16pm UTC](https://users.rust-lang.org/t/inconsistency-between-stacked-borrows-and-my-mental-model/47178/3 "2020-08-12T12:16:09Z")

</div>

> [@mbrubeck](#):
>
> So I believe this is a known limitation of the current Stacked Borrows model which might be changed in future revisions.

Exactly. 🙂

---

<div class="post-metadata">

**Author:** ![alice](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/alice/32/9777_2.png) [@alice](https://users.rust-lang.org/u/alice)\
**Post date:** [November 10, 2020, 12:16pm UTC](https://users.rust-lang.org/t/inconsistency-between-stacked-borrows-and-my-mental-model/47178/4 "2020-11-10T12:16:16Z")

</div>

Closed on November 10, 2020, 12:16pm UTC

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.
