Skip to content

Commit

Permalink
Arbitrary self types v2: no deshadowing on traits
Browse files Browse the repository at this point in the history
This is a hacky temporary "fix" for problems encountered where our
deshadowing algorithm is blocking legitimate code. This will need to be
reworked, but pushing to see if we get through CI.

The solution here involves only applying the deshadowing algorithm in
cases where both methods are inherent rather than in traits. That's
probably what we want to do? - but it needs discussion, and the code
needs a bit of restructuring.
  • Loading branch information
adetaylor committed Nov 14, 2024
1 parent d1c8cbb commit adea5bc
Showing 1 changed file with 18 additions and 11 deletions.
29 changes: 18 additions & 11 deletions compiler/rustc_hir_typeck/src/method/probe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,9 @@ pub(crate) struct ProbeContext<'a, 'tcx> {

/// Number of times we've hopped along the chain of `Receiver::Target`.
/// Used to spot cases where an "outer" method in a smart pointer might
/// "shadow" a pre-existing method in the pointee.
receiver_trait_derefs: usize,
/// "shadow" a pre-existing method in the pointee. Only applies for inherent
/// methods.
receiver_trait_derefs: Option<usize>,
}

impl<'a, 'tcx> Deref for ProbeContext<'a, 'tcx> {
Expand All @@ -104,7 +105,7 @@ pub(crate) struct Candidate<'tcx> {
pub(crate) item: ty::AssocItem,
pub(crate) kind: CandidateKind<'tcx>,
pub(crate) import_ids: SmallVec<[LocalDefId; 1]>,
receiver_trait_derefs: usize,
receiver_trait_derefs: Option<usize>,
}

#[derive(Debug, Clone)]
Expand Down Expand Up @@ -183,7 +184,7 @@ struct PickDiagHints<'a, 'tcx> {
#[derive(Debug)]
struct PickConstraintsForShadowed {
autoderefs: usize,
receiver_trait_derefs: usize,
receiver_trait_derefs: Option<usize>,
def_id: DefId,
}

Expand All @@ -192,8 +193,11 @@ impl PickConstraintsForShadowed {
autoderefs == self.autoderefs
}

fn may_shadow_based_on_receiver_trait_derefs(&self, receiver_trait_derefs: usize) -> bool {
receiver_trait_derefs != self.receiver_trait_derefs
fn may_shadow_based_on_receiver_trait_derefs(
&self,
receiver_trait_derefs: Option<usize>,
) -> bool {
receiver_trait_derefs != self.receiver_trait_derefs && receiver_trait_derefs.is_some()
}

fn may_shadow_based_on_defid(&self, def_id: DefId) -> bool {
Expand Down Expand Up @@ -223,7 +227,8 @@ pub(crate) struct Pick<'tcx> {

/// Number of jumps along the `Receiver::Target` chain we followed
/// to identify this method. Used only for deshadowing errors.
pub receiver_trait_derefs: usize,
/// Only applies for inherent impls.
pub receiver_trait_derefs: Option<usize>,
}

#[derive(Clone, Debug, PartialEq, Eq)]
Expand Down Expand Up @@ -708,7 +713,7 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
static_candidates: RefCell::new(Vec::new()),
scope_expr_id,
is_suggestion,
receiver_trait_derefs: 0usize,
receiver_trait_derefs: None,
}
}

Expand Down Expand Up @@ -765,9 +770,11 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {

fn assemble_inherent_candidates(&mut self) {
for step in self.steps.iter() {
self.receiver_trait_derefs = step.autoderefs;
self.receiver_trait_derefs = Some(step.autoderefs);
self.assemble_probe(&step.self_ty);
}
// FIXME pass on stack again probably
self.receiver_trait_derefs = None;
}

#[instrument(level = "debug", skip(self))]
Expand Down Expand Up @@ -2039,11 +2046,11 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
item: probes[0].0.item,
kind: TraitPick,
import_ids: probes[0].0.import_ids.clone(),
autoderefs: probes[0].0.receiver_trait_derefs,
autoderefs: 0,
autoref_or_ptr_adjustment: None,
self_ty,
unstable_candidates: vec![],
receiver_trait_derefs: 0,
receiver_trait_derefs: probes[0].0.receiver_trait_derefs,
})
}

Expand Down

0 comments on commit adea5bc

Please sign in to comment.