Skip to content

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

Closed

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

Activity

  1. added
    A-diagnosticsArea: Messages for errors, warnings, and lints
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    on Jul 10, 2024
  2. chenyukang commented on Jul 13, 2024

    @chenyukang
    Member

    I think the current suggestion is suggesting the code like this:

        let ptr = &mut val;
  3. chenyukang commented on Jul 13, 2024

    @chenyukang
    Member

    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.

  4. tesuji commented on Jul 13, 2024

    @tesuji
    Contributor

    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/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: *mut i32 = std::ptr::addr_of!(val);
       |            ++++++++++

    I don't have any ideas about this differences.

  5. chenyukang commented on Jul 13, 2024

    @chenyukang
    Member
    binding's type

    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.

  6. cyrgani commented on Jul 14, 2024

    @cyrgani
    ContributorAuthor

    Given that &raw syntax might get stabilized soon by #127679, suggesting the correct &raw mut syntax would become a more feasible option.

  7. RalfJung commented on Jul 14, 2024

    @RalfJung
    Member

    &mut raw const $place

    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.

  8. added a commit that references this issue on Jul 15, 2024
  9. added a commit that references this issue on Oct 19, 2024
  10. added a commit that references this issue on Oct 20, 2024
  11. 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
  12. added
    A-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`
    D-papercutDiagnostics: An error or lint that needs small tweaks.
    D-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.
    A-raw-pointersArea: raw pointers, MaybeUninit, NonNull
    on Oct 22, 2024
  13. added 3 commits that reference this issue on Dec 13, 2024
  14. added a commit that references this issue on Dec 14, 2024
  15. Enselic commented on Dec 14, 2024

    @Enselic
    Member

    With #134244 we now at least don't suggest invalid syntax.

    But it would be even nicer to suggest changing &raw const val to &raw mut val (which when applied will then suggest changing to let mut val).

  16. 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
  17. cyrgani commented on Dec 15, 2024

    @cyrgani
    ContributorAuthor

    @rustbot label: -D-invalid-suggestion

  18. removed
    D-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.
    on Dec 15, 2024
  19. added 2 commits that reference this issue on Dec 17, 2024
    70aa10a
    40ce4e0
  20. added a commit that references this issue on Dec 17, 2024
    8ac0c0f
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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