mirror of
https://github.com/rust-lang/rust.git
synced 2025-05-01 12:37:37 +00:00

In this new version of Arbitrary Self Types, we no longer use the Deref trait exclusively when working out which self types are valid. Instead, we follow a chain of Receiver traits. This enables methods to be called on smart pointer types which fundamentally cannot support Deref (for instance because they are wrappers for pointers that don't follow Rust's aliasing rules). This includes: * Changes to tests appropriately * New tests for: * The basics of the feature * Ensuring lifetime elision works properly * Generic Receivers * A copy of the method subst test enhanced with Receiver This is really the heart of the 'arbitrary self types v2' feature, and is the most critical commit in the current PR. Subsequent commits are focused on: * Detecting "shadowing" problems, where a smart pointer type can hide methods in the pointee. * Diagnostics and cleanup. Naming: in this commit, the "Autoderef" type is modified so that it no longer solely focuses on the "Deref" trait, but can now consider the "Receiver" trait instead. Should it be renamed, to something like "TraitFollower"? This was considered, but rejected, because * even in the Receiver case, it still considers built-in derefs * the name Autoderef is short and snappy.
65 lines
1.7 KiB
Rust
65 lines
1.7 KiB
Rust
#![feature(arbitrary_self_types)]
|
|
|
|
use std::ops::{Receiver, Deref};
|
|
|
|
struct SmartPtr<'a, T: ?Sized>(&'a T);
|
|
|
|
impl<'a, T: ?Sized> Deref for SmartPtr<'a, T> {
|
|
type Target = T;
|
|
fn deref(&self) -> &Self::Target {
|
|
self.0
|
|
}
|
|
}
|
|
|
|
impl<'a, T: ?Sized> Clone for SmartPtr<'a, T> {
|
|
fn clone(&self) -> Self {
|
|
Self(self.0)
|
|
}
|
|
}
|
|
|
|
impl<'a, T: ?Sized> Copy for SmartPtr<'a, T> {
|
|
}
|
|
|
|
struct Foo(u32);
|
|
impl Foo {
|
|
fn a<R: Receiver<Target=Self>>(self: R) -> u32 {
|
|
//~^ ERROR invalid generic `self` parameter type: `R`
|
|
2
|
|
}
|
|
fn b<R: Deref<Target=Self>>(self: R) -> u32 {
|
|
//~^ ERROR invalid generic `self` parameter type: `R`
|
|
self.0
|
|
}
|
|
fn c(self: impl Receiver<Target=Self>) -> u32 {
|
|
//~^ ERROR invalid generic `self` parameter type: `impl Receiver<Target = Self>`
|
|
3
|
|
}
|
|
fn d(self: impl Deref<Target=Self>) -> u32 {
|
|
//~^ ERROR invalid generic `self` parameter type: `impl Deref<Target = Self>`
|
|
self.0
|
|
}
|
|
}
|
|
|
|
fn main() {
|
|
let foo = Foo(1);
|
|
assert_eq!((&foo).a::<&Foo>(), 2);
|
|
assert_eq!((&foo).b::<&Foo>(), 1);
|
|
assert_eq!((&foo).a(), 2);
|
|
assert_eq!((&foo).b(), 1);
|
|
assert_eq!((&foo).c(), 3);
|
|
assert_eq!((&foo).d(), 1);
|
|
assert_eq!(foo.a::<&Foo>(), 2);
|
|
//~^ ERROR mismatched types
|
|
assert_eq!(foo.b::<&Foo>(), 1);
|
|
//~^ ERROR mismatched types
|
|
let smart_ptr = SmartPtr(&foo);
|
|
assert_eq!(smart_ptr.a(), 2);
|
|
assert_eq!(smart_ptr.b(), 1);
|
|
assert_eq!(smart_ptr.c(), 3);
|
|
assert_eq!(smart_ptr.d(), 1);
|
|
assert_eq!(smart_ptr.a::<&Foo>(), 2);
|
|
//~^ ERROR mismatched types
|
|
assert_eq!(smart_ptr.b::<&Foo>(), 1);
|
|
//~^ ERROR mismatched types
|
|
}
|