You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Using &raw const instead of &raw mut does not offer a suggestion fix #127562
fnmain(){let val = 2;let ptr = &rawconst 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
However, when I add that code as a regression tests in tests/ui directory, the diagnostic suddenly changed to
error[E0594]: cannot assign to `*ptr`, which is behind a `*const` pointer
--> $DIR/dont_suggest_raw_pointer_syntax-issue-127562.rs:9:9
|
LL | *ptr = 3;
| ^^^^^^^^ `ptr` is a `*const` pointer, so the data it refers to cannot be written
|
help: consider specifying this binding's type
|
LL | let ptr:*muti32 = std::ptr::addr_of!(val);
| ++++++++++
This is completely invalid syntax so just suggesting nothing instead as a first step would already be a good idea.
Suggesting anything that involves references is a really bad idea -- code that uses raw pointers should keep using raw pointers throughout with no intermediate references.
changed the title [-]Using `ptr::addr_of!` instead of `ptr::addr_of_mut!` produces invalid help message[/-][+]Using `&raw const` instead of `&raw mut` produces invalid help message[/+]on Oct 21, 2024
changed the title [-]Using `&raw const` instead of `&raw mut` produces invalid help message[/-][+]Using `&raw const` instead of `&raw mut` does not offer a suggestion fix[/+]on Dec 15, 2024
Activity
chenyukang commentedon Jul 13, 2024
I think the current suggestion is suggesting the code like this:
chenyukang commentedon Jul 13, 2024
oh, the suggestion's code comes from here:
https://github.com/chenyukang/rust/blob/c8ed5c50edfd1b721c0541fa614864179251aaac/library/core/src/ptr/mod.rs#L2208
I think we should not suggest to change the library code here.
tesuji commentedon Jul 13, 2024
I have playground link that represents the problem above: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=fee632ddd4f51a0390ac6918ef4eff96
However, when I add that code as a regression tests in
tests/uidirectory, the diagnostic suddenly changed toI don't have any ideas about this differences.
chenyukang commentedon Jul 13, 2024
This is because when run the test UI, the span here is remaped
$SRC_DIR/core/src/ptr/mod.rs:LL:COL (#4):https://github.com/chenyukang/rust/blob/b44a48416d06524da2ea247f1d1973d85ef514f8/compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs#L1452
so the here we get an error and didn't continue the code branch:
https://github.com/chenyukang/rust/blob/b44a48416d06524da2ea247f1d1973d85ef514f8/compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs#L1453
it's weird, maybe another issue need to be fixed.
cyrgani commentedon Jul 14, 2024
Given that
&rawsyntax might get stabilized soon by #127679, suggesting the correct&raw mutsyntax would become a more feasible option.RalfJung commentedon Jul 14, 2024
This is completely invalid syntax so just suggesting nothing instead as a first step would already be a good idea.
Suggesting anything that involves references is a really bad idea -- code that uses raw pointers should keep using raw pointers throughout with no intermediate references.
Rollup merge of rust-lang#127675 - chenyukang:yukang-fix-127562-addr,…
Rollup merge of rust-lang#127675 - chenyukang:yukang-fix-127562-addr,…
Unrolled build for rust-lang#127675
[-]Using `ptr::addr_of!` instead of `ptr::addr_of_mut!` produces invalid help message[/-][+]Using `&raw const` instead of `&raw mut` produces invalid help message[/+]Rollup merge of rust-lang#134244 - Enselic:no-mut-hint-for-raw-ref, r…
Rollup merge of rust-lang#134244 - Enselic:no-mut-hint-for-raw-ref, r…
Rollup merge of rust-lang#134244 - Enselic:no-mut-hint-for-raw-ref, r…
Unrolled build for rust-lang#134244
Enselic commentedon Dec 14, 2024
With #134244 we now at least don't suggest invalid syntax.
But it would be even nicer to suggest changing
&raw const valto&raw mut val(which when applied will then suggest changing tolet mut val).[-]Using `&raw const` instead of `&raw mut` produces invalid help message[/-][+]Using `&raw const` instead of `&raw mut` does not offer a suggestion fix[/+]cyrgani commentedon Dec 15, 2024
@rustbot label: -D-invalid-suggestion
Rollup merge of rust-lang#134397 - Enselic:raw-mut, r=compiler-errors
Rollup merge of rust-lang#134397 - Enselic:raw-mut, r=compiler-errors
Unrolled build for rust-lang#134397