Skip to content

*const Self method can't be called with *mut Self (arbitrary_self_types)  #80258

Closed
@osa1

Description

@osa1

Using nightly 2020-12-03, this doesn't build: (play link)

#![feature(arbitrary_self_types)]

struct Test {}

impl Test {
    fn x(self: *const Self) {}
}

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

Error:

error[E0599]: no method named `x` found for raw pointer `*mut Test` in the current scope
  --> src/main.rs:11:9
   |
11 |     ptr.x();
   |         ^ method not found in `*mut Test`
   |
   = 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

error: aborting due to previous error

I think similar to how &self methods can be called on &mut references, *const Self methods should be allowed to be called on *mut pointers.

Metadata

Metadata

Assignees

Labels

A-raw-pointersArea: raw pointers, MaybeUninit, NonNullC-bugCategory: This is a bug.F-arbitrary_self_types`#![feature(arbitrary_self_types)]`T-langRelevant to the language team, which will review and decide on the PR/issue.requires-nightlyThis issue requires a nightly compiler in some way.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions