Skip to content

Using &raw const instead of &raw mut produces invalid help message #127562

Open

Description

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
  |
help: consider changing this to be a mutable pointer
  |
3 |     let ptr = &mut raw const val;
  |                +++

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 help message is wrong:

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

gives

error: expected one of `!`, `.`, `::`, `;`, `?`, `else`, `{`, or an operator, found keyword `const`
 --> a.rs:5:24
  |
5 |     let ptr = &mut raw const val;
  |                        ^^^^^ expected one of 8 possible tokens

whereas the correct syntax would be &raw mut val.

Other cases

No response

Rust Version

rustc 1.84.0-nightly (2024-10-20 662180b34d95f72d05b7)

Anything else?

EDIT: updated since addr_of! and addr_of_mut! diagnostics are not incorrect anymore due to #127675

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.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