Open
Description
openedon Jul 10, 2024
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
Labels
Area: Messages for errors, warnings, and lintsArea: raw pointers, MaybeUninit, NonNullArea: Suggestions generated by the compiler applied by `cargo fix`Diagnostics: A structured suggestion resulting in incorrect code.Diagnostics: An error or lint that needs small tweaks.Relevant to the compiler team, which will review and decide on the PR/issue.