Skip to content

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

Closed
@glandium

Description

@glandium

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.

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

    No branches or pull requests

    Issue actions