mirror of
https://github.com/rust-lang/rust.git
synced 2025-04-29 19:47:38 +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.
25 lines
528 B
Rust
25 lines
528 B
Rust
//@ edition: 2018
|
|
//@ run-pass
|
|
|
|
#![feature(arbitrary_self_types)]
|
|
|
|
// tests that the referent type of a reference must be known to call methods on it
|
|
|
|
struct SmartPtr<T>(T);
|
|
|
|
impl<T> core::ops::Receiver for SmartPtr<T> {
|
|
type Target = T;
|
|
}
|
|
|
|
impl<T> SmartPtr<T> {
|
|
fn foo(&self) -> usize { 3 }
|
|
}
|
|
|
|
fn main() {
|
|
let val = 1_u32;
|
|
let ptr = SmartPtr(val);
|
|
// Ensure calls to outer methods work even if inner methods can't be
|
|
// resolved due to the type variable
|
|
assert_eq!((ptr as SmartPtr<_>).foo(), 3);
|
|
}
|