# Illegal access a stack object via raw pointer?

**URL:** <https://internals.rust-lang.org/t/illegal-access-a-stack-object-via-raw-pointer/23581>\
**Category:** Uncategorized\
**Created:** [September 30, 2025, 5:04am UTC](https://internals.rust-lang.org/t/illegal-access-a-stack-object-via-raw-pointer/23581 "2025-09-30T05:04:51Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![zhuyt](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/zhuyt/32/10381_2.png) [@zhuyt](https://internals.rust-lang.org/u/zhuyt)\
**Post date:** [September 30, 2025, 5:04am UTC](https://internals.rust-lang.org/t/illegal-access-a-stack-object-via-raw-pointer/23581/1 "2025-09-30T05:04:51Z")

</div>

```rs
struct Node(i32, Option<Box<Node>>);

fn main() {
    let nod = Node(0, None);
    let ptr = unsafe {
        &nod as *const Node
    };
    let x = nod; // value moved
    let y = unsafe {
        (*ptr).0 // invalid dereference?
    };
}

```

In the above code, `ptr` holds the address of a stack object `nod`. However, since `ptr` bypasses the borrow checker, we can assess a `nod` after its value moves.

The memory of `nod` is allocated in stack and keeps alive until `main` finishes. And the move of `nod` only does a bitwise copy. My question is, how Rust think about this issue? If we obey the ownership rule, the dereference of `ptr` is invalid. Otherwise, it is safe.

---

<div class="post-metadata">

**Author:** ![theemathas](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/theemathas/32/11817_2.png) [@theemathas](https://internals.rust-lang.org/u/theemathas)\
**Post date:** [September 30, 2025, 7:31am UTC](https://internals.rust-lang.org/t/illegal-access-a-stack-object-via-raw-pointer/23581/2 "2025-09-30T07:31:01Z")

</div>

I believe that is an unsettled question. See [What about: use-after-move and (maybe) use-after-drop · Issue #188 · rust-lang/unsafe-code-guidelines · GitHub](https://github.com/rust-lang/unsafe-code-guidelines/issues/188)

---

<div class="post-metadata">

**Author:** ![dlight](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/dlight/32/8462_2.png) [@dlight](https://internals.rust-lang.org/u/dlight)\
**Post date:** [September 30, 2025, 8:39am UTC](https://internals.rust-lang.org/t/illegal-access-a-stack-object-via-raw-pointer/23581/3 "2025-09-30T08:39:39Z")

</div>

If you move a variable somewhere else and it's in a register, the compiler might reuse the register for something else. But since one is taking the address the variable must be spilled to the stack.

Isn't the compiler allowed to reuse stack space to another variable after the current variable there were unconditionally moved out? Before the end of scope

---

<div class="post-metadata">

**Author:** ![theemathas](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/theemathas/32/11817_2.png) [@theemathas](https://internals.rust-lang.org/u/theemathas)\
**Post date:** [September 30, 2025, 10:14am UTC](https://internals.rust-lang.org/t/illegal-access-a-stack-object-via-raw-pointer/23581/4 "2025-09-30T10:14:43Z")

</div>

> [@dlight](#):
>
> But since one is taking the address the variable must be spilled to the stack.

Not necessarily. It could be optimized into something that doesn’t use the address.

> [@dlight](#):
>
> Isn't the compiler allowed to reuse stack space to another variable after the current variable there were unconditionally moved out? Before the end of scope

It is currently unclear whether the compiler should be allowed to do that or not (in the case where the compiler can’t prove that nothing is holding a pointer to that variable). See the discussion I linked. One of the difficulties is: How to handle partial moves?
