Skip to content

Using &raw const instead of &raw mut does not offer a suggestion fix #127562

Closed
@cyrgani

Description

@cyrgani

Code

fn main() {
    let val = 2;
    let ptr = &raw const val;
    unsafe { *ptr = 3; }
}

Current output

error[E0594]: cannot assign to `*ptr`, which is behind a `*const` pointer
 --> src/main.rs:4:14
  |
4 |     unsafe { *ptr = 3; }
  |              ^^^^^^^^ `ptr` is a `*const` pointer, so the data it refers to cannot be written

For more information about this error, try `rustc --explain E0594`.

Desired output

error[E0594]: cannot assign to `*ptr`, which is behind a `*const` pointer
 --> src/main.rs:4:14
  |
4 |     unsafe { *ptr = 3; }
  |              ^^^^^^^^ `ptr` is a `*const` pointer, so the data it refers to cannot be written
  |
help: consider changing this to be a mutable pointer
  |
3 |     let ptr = &raw mut val;
  |                    ~~~

For more information about this error, try `rustc --explain E0594`.

Rationale and extra context

The current error message does not include a suggestion for how to fix it.

Other cases

No response

Rust Version

1.85.0-nightly (2024-12-14 0aeaa5eb22180fdf12a8)

Anything else?

EDIT 1: updated since addr_of! and addr_of_mut! diagnostics are not incorrect anymore due to #127675
EDIT 2: updated as #134224 removed the invalid suggestion

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-raw-pointersArea: raw pointers, MaybeUninit, NonNullA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`D-papercutDiagnostics: An error or lint that needs small tweaks.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions