# \*const pointer not warning about use after modify?

**URL:** <https://internals.rust-lang.org/t/const-pointer-not-warning-about-use-after-modify/23736>\
**Category:** Unsafe Code Guidelines\
**Created:** [November 20, 2025, 11:05am UTC](https://internals.rust-lang.org/t/const-pointer-not-warning-about-use-after-modify/23736 "2025-11-20T11:05:43Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![SciMind2460](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/scimind2460/32/13125_2.png) [@SciMind2460](https://internals.rust-lang.org/u/SciMind2460)\
**Post date:** [November 20, 2025, 11:05am UTC](https://internals.rust-lang.org/t/const-pointer-not-warning-about-use-after-modify/23736/1 "2025-11-20T11:05:43Z")

</div>

For some reason, this compiles without warning (only one that a is not used for the nitpickers)

```rust
fn main() {
  let mut a = 0;
  let b = &raw const a;
  a = 1;
  unsafe {
    println!("{}", *b);
  }
}

```

Output:

```rust
1

```

This is definitely not intended behavior, and I think there should at least be a warning if a const pointer's pointed value changes and then the pointer is referenced.

---

<div class="post-metadata">

**Author:** ![Noratrieb](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/noratrieb/32/12440_2.png) [@Noratrieb](https://internals.rust-lang.org/u/Noratrieb)\
**Post date:** [November 20, 2025, 11:54am UTC](https://internals.rust-lang.org/t/const-pointer-not-warning-about-use-after-modify/23736/2 "2025-11-20T11:54:13Z")

</div>

`*const` vs `*mut` only changes the variance over T and prevents direct writes through it, nothing else. In fact, because of the variance effects, people use `*const` to represent owned values that are very much written through. The entire point of raw pointers is that they are not checked by a borrow checker. So this is intended behavior and good.

---

<div class="post-metadata">

**Author:** ![Noratrieb](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/noratrieb/32/12440_2.png) [@Noratrieb](https://internals.rust-lang.org/u/Noratrieb)\
**Post date:** [November 20, 2025, 11:58am UTC](https://internals.rust-lang.org/t/const-pointer-not-warning-about-use-after-modify/23736/3 "2025-11-20T11:58:07Z")

</div>

That said, this is undefined behavior under stacked borrows, not because it's `*const`, but because writing to the value directly invalidates all pointers to the value. Miri will flag this. So a lint for the "pointer used after invalidation" case wouldn't be a bad idea, but that's unrelated to the mutability of the pointer.

---

<div class="post-metadata">

**Author:** ![SciMind2460](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/scimind2460/32/13125_2.png) [@SciMind2460](https://internals.rust-lang.org/u/SciMind2460)\
**Post date:** [November 20, 2025, 12:09pm UTC](https://internals.rust-lang.org/t/const-pointer-not-warning-about-use-after-modify/23736/4 "2025-11-20T12:09:03Z")

</div>

Yep, that's what I was mainly warning against. Edited my post to make it clear.

---

<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:** [November 20, 2025, 7:03pm UTC](https://internals.rust-lang.org/t/const-pointer-not-warning-about-use-after-modify/23736/5 "2025-11-20T19:03:29Z")

</div>

> [@Noratrieb](#):
>
> writing to the value directly invalidates all pointers to the value.

Wait, why? Is there some piece of documentation or reference that spells out this?

Is this just syntactic restriction? (That is, specifically writes through `a` aren't allowed, all others are). I mean, surely we can in general have other code changing memory that a raw pointer points to.

---

<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:** [November 20, 2025, 7:20pm UTC](https://internals.rust-lang.org/t/const-pointer-not-warning-about-use-after-modify/23736/6 "2025-11-20T19:20:58Z")

</div>

Whether or not that code is allowed is an [unresolved question](https://github.com/rust-lang/unsafe-code-guidelines/issues/257). So this code is in the gray area. AFAIK we don't usually lint for that.

Also, there are so many lints we could write for raw pointer misuse... I feel like that needs a somewhat holistic approach, not picking random ad-hoc examples.

---

<div class="post-metadata">

**Author:** ![ais523](https://avatars.discourse-cdn.com/v4/letter/a/a183cd/32.png) [@ais523](https://internals.rust-lang.org/u/ais523)\
**Post date:** [November 24, 2025, 5:36pm UTC](https://internals.rust-lang.org/t/const-pointer-not-warning-about-use-after-modify/23736/7 "2025-11-24T17:36:59Z")

</div>

I think you have linked the wrong unresolved question? Doing this is disallowed under Stacked Borrows regardless of whether the pointer to `b` is initially `*const` or `*mut` (because the `a = 1;` line conceptually creates a new mutable borrow of `a` which conflicts with the pointer, even if the pointer is mutable).

It is still an unresolved question, but a different one, and I haven't immediately been able to tell which one (although it's mentioned in [unsafe-code-guidelines issue 573](https://github.com/rust-lang/unsafe-code-guidelines/issues/573) it isn't the main purpose of that post.

---

<div class="post-metadata">

**Author:** ![scottmcm](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/scottmcm/32/2355_2.png) [@scottmcm](https://internals.rust-lang.org/u/scottmcm)\
**Post date:** [November 24, 2025, 6:49pm UTC](https://internals.rust-lang.org/t/const-pointer-not-warning-about-use-after-modify/23736/8 "2025-11-24T18:49:23Z")

</div>

> [@SciMind2460](#):
>
> and I think there should at least be a warning if [...]

TBH, I'm always skeptical about warnings like this around raw pointers. The whole point of using raw pointers in Rust is doing things that the compiler doesn't understand well, and thus trying to make more and more complicated lints just makes the compiler slower for questionable gain.

It is realistic that people are writing this code instead of using references that _do_ give good diagnostics for things like this? If someone wrote this _because_ the reference-using code didn't compile, a warning won't make them stop either.

---

<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:** [November 24, 2025, 7:30pm UTC](https://internals.rust-lang.org/t/const-pointer-not-warning-about-use-after-modify/23736/9 "2025-11-24T19:30:43Z")

</div>

> [@ais523](#):
>
> I think you have linked the wrong unresolved question? Doing this is disallowed under Stacked Borrows regardless of whether the pointer to `b` is initially `*const` or `*mut` (because the `a = 1;` line conceptually creates a new mutable borrow of `a` which conflicts with the pointer, even if the pointer is mutable).
> 
> It is still an unresolved question, but a different one, and I haven't immediately been able to tell which one (although it's mentioned in [unsafe-code-guidelines issue 573](https://github.com/rust-lang/unsafe-code-guidelines/issues/573) it isn't the main purpose of that post.

The underlying unresolved question is whether raw pointers always get the tag of the place they are created from, or whether they sometimes get their own tag. The issue I linked is a direct consequence of raw pointers getting their own tag in Stacked Borrows. So in that sense it is the same question, though I admit the issue description and discussion doesn't really reflect that. Maybe we should have a separate issue making this more clear.

EDIT: [issue created](https://github.com/rust-lang/unsafe-code-guidelines/issues/591)
