1
Fork 0

Arbitrary self types v2: use Receiver trait

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.
This commit is contained in:
Adrian Taylor 2024-10-25 11:08:58 +00:00
parent 5a6036a180
commit e75660dad3
39 changed files with 737 additions and 97 deletions

View file

@ -149,11 +149,21 @@ pub struct CandidateStep<'tcx> {
/// `foo.by_raw_ptr()` will work and `foo.by_ref()` won't.
pub from_unsafe_deref: bool,
pub unsize: bool,
/// We will generate CandidateSteps which are reachable via a chain
/// of following `Receiver`. The first 'n' of those will be reachable
/// by following a chain of 'Deref' instead (since there's a blanket
/// implementation of Receiver for Deref).
/// We use the entire set of steps when identifying method candidates
/// (e.g. identifying relevant `impl` blocks) but only those that are
/// reachable via Deref when examining what the receiver type can
/// be converted into by autodereffing.
pub reachable_via_deref: bool,
}
#[derive(Copy, Clone, Debug, HashStable)]
pub struct MethodAutoderefStepsResult<'tcx> {
/// The valid autoderef steps that could be found.
/// The valid autoderef steps that could be found by following a chain
/// of `Receiver<Target=T>` or `Deref<Target=T>` trait implementations.
pub steps: &'tcx [CandidateStep<'tcx>],
/// If Some(T), a type autoderef reported an error on.
pub opt_bad_ty: Option<&'tcx MethodAutoderefBadTy<'tcx>>,