1
Fork 0

Auto merge of #132961 - adetaylor:arbitrary-self-types-the-big-bit, r=compiler-errors,wesleywiser

Arbitrary self types v2: main compiler changes

This is the main PR in a series of PRs related to Arbitrary Self Types v2, tracked in #44874. Specifically this is step 7 of the plan [described here](https://github.com/rust-lang/rust/issues/44874#issuecomment-2122179688), for [RFC 3519](https://github.com/rust-lang/rfcs/pull/3519).

Overall this PR:
* Switches from the `Deref` trait to the new `Receiver` trait when the unstable `arbitrary_self_types` feature is enabled (the simple bit)
* Introduces new algorithms to spot "shadowing"; that is, the case where a newly-added method in an outer smart pointer might end up overriding a pre-existing method in the pointee (the complex bit). Most of this bit was explored in [this earlier perf-testing PR](https://github.com/rust-lang/rust/pull/127812#issuecomment-2236911900).
* Lots of tests

This should not break compatibility for:
* Stable users, where it should have no effect
* Users of the existing `arbitrary_self_types` feature (because we implement `Receiver` for `T: Deref`) _unless_ those folks have added methods which may shadow methods in inner types, which we no longer want to allow

Subsequent PRs will add better diagnostics.

It's probably easiest to review this commit-by-commit.

r? `@wesleywiser`
This commit is contained in:
bors 2024-12-12 21:40:39 +00:00
commit 915e7eb9b9
50 changed files with 1746 additions and 199 deletions

View file

@ -1821,13 +1821,18 @@ fn receiver_is_valid<'tcx>(
let mut autoderef = Autoderef::new(infcx, wfcx.param_env, wfcx.body_def_id, span, receiver_ty);
// The `arbitrary_self_types` feature allows custom smart pointer
// types to be method receivers, as identified by following the Receiver<Target=T>
// chain.
if arbitrary_self_types_enabled.is_some() {
autoderef = autoderef.use_receiver_trait();
}
// The `arbitrary_self_types_pointers` feature allows raw pointer receivers like `self: *const Self`.
if arbitrary_self_types_enabled == Some(ArbitrarySelfTypesLevel::WithPointers) {
autoderef = autoderef.include_raw_pointers();
}
let receiver_trait_def_id = tcx.require_lang_item(LangItem::LegacyReceiver, Some(span));
// Keep dereferencing `receiver_ty` until we get to `self_ty`.
while let Some((potential_self_ty, _)) = autoderef.next() {
debug!(
@ -1849,11 +1854,13 @@ fn receiver_is_valid<'tcx>(
}
// Without `feature(arbitrary_self_types)`, we require that each step in the
// deref chain implement `receiver`.
// deref chain implement `LegacyReceiver`.
if arbitrary_self_types_enabled.is_none() {
if !receiver_is_implemented(
let legacy_receiver_trait_def_id =
tcx.require_lang_item(LangItem::LegacyReceiver, Some(span));
if !legacy_receiver_is_implemented(
wfcx,
receiver_trait_def_id,
legacy_receiver_trait_def_id,
cause.clone(),
potential_self_ty,
) {
@ -1866,7 +1873,7 @@ fn receiver_is_valid<'tcx>(
cause.clone(),
wfcx.param_env,
potential_self_ty,
receiver_trait_def_id,
legacy_receiver_trait_def_id,
);
}
}
@ -1875,14 +1882,14 @@ fn receiver_is_valid<'tcx>(
Err(ReceiverValidityError::DoesNotDeref)
}
fn receiver_is_implemented<'tcx>(
fn legacy_receiver_is_implemented<'tcx>(
wfcx: &WfCheckingCtxt<'_, 'tcx>,
receiver_trait_def_id: DefId,
legacy_receiver_trait_def_id: DefId,
cause: ObligationCause<'tcx>,
receiver_ty: Ty<'tcx>,
) -> bool {
let tcx = wfcx.tcx();
let trait_ref = ty::TraitRef::new(tcx, receiver_trait_def_id, [receiver_ty]);
let trait_ref = ty::TraitRef::new(tcx, legacy_receiver_trait_def_id, [receiver_ty]);
let obligation = Obligation::new(tcx, cause, wfcx.param_env, trait_ref);
@ -1890,7 +1897,7 @@ fn receiver_is_implemented<'tcx>(
true
} else {
debug!(
"receiver_is_implemented: type `{:?}` does not implement `Receiver` trait",
"receiver_is_implemented: type `{:?}` does not implement `LegacyReceiver` trait",
receiver_ty
);
false