Skip to content

Mutating data with Deref to raw pointer is reported as an error #15799

Closed
@neivv

Description

@neivv

The following program compiles and runs fine, but rust-analyzer shows an error
at **wrap = 6; line.

cannot mutate immutable variable `wrap`
struct WrapPtr(*mut u32);

impl std::ops::Deref for WrapPtr {
    type Target = *mut u32;
    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

fn main() {
    let mut x = 0u32;
    {
        let wrap = WrapPtr(&mut x);
        unsafe {
            **wrap = 6;
        }
    }
    assert_eq!(x, 6);
}

rust-analyzer version: VS Code: rust-analyzer version: 0.3.1705-standalone (10872952c 2023-10-22)

rustc version: rustc 1.75.0-nightly (cd674d617 2023-10-24)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions