Skip to content

Is mutation of a *const casted to *mut UB? Differing behavior for Stacked vs Tree borrows #4356

Closed
@olekgierczak

Description

@olekgierczak

Is mutating a *const T by casting it to a *mut T and then performing the mutation UB? I wasn't able to find any mentions of this in past issues or any explicit documentation in the rust unsafe guidelines on this. Here's an example that does such a mutation:

fn main() {
  let mut x = 0;
  unsafe {
      let p = &mut x as *const i32;
      let p2 = p as *mut i32;
      *p2 = 1;
  }
  println!("{}", x);
}

Under Tree Borrows, this succeeds, but under Stacked Borrows, it gives an error at the mutation

attempting a write access using <590> at alloc260[0x0], but that tag only grants SharedReadOnly permission for this location

Is it the case that casting through a *const doesn't give a Frozen permission? And if so, is that intentional?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions