Skip to content

Suggestion for as_mut_ref method on *mut T is <*const T>::as_ref rather than <*mut T>::as_mut #83695

@glandium

Description

@glandium
Contributor

Given the following code:

fn main() {
    let p: *mut () = std::ptr::null_mut();
    p.as_mut_ref();
}

(I don't know why to this day, I still type as_mut_ret... it's beyond me...)

The current output is:

error[E0599]: no method named `as_mut_ref` found for raw pointer `*mut ()` in the current scope
 --> src/main.rs:3:7
  |
3 |     p.as_mut_ref();
  |       ^^^^^^^^^^ method not found in `*mut ()`
  |
  = note: try using `<*const T>::as_ref()` to get a reference to the type behind the pointer: https://doc.rust-lang.org/std/primitive.pointer.html#method.as_ref
  = note: using `<*const T>::as_ref()` on a pointer which is unaligned or points to invalid or uninitialized memory is undefined behavior

The note is misleading in two ways:

  • it's suggesting a method on *const T, when it should be a method on *mut T.
  • it's suggesting as_ref while it should be as_mut.

Activity

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 Mar 31, 2021
added
A-raw-pointersArea: raw pointers, MaybeUninit, NonNull
C-bugCategory: This is a bug.
on Mar 31, 2021
added 2 commits that reference this issue on Apr 11, 2024
e1fa18f
f13f37f
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, NonNullC-bugCategory: This is a bug.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

      Participants

      @glandium@jyn514

      Issue actions

        Suggestion for as_mut_ref method on *mut T is <*const T>::as_ref rather than <*mut T>::as_mut · Issue #83695 · rust-lang/rust