Skip to content

Commit adea5bc

Browse files
committed
Arbitrary self types v2: no deshadowing on traits
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.
1 parent d1c8cbb commit adea5bc

File tree

1 file changed

+18
-11
lines changed
  • compiler/rustc_hir_typeck/src/method

1 file changed

+18
-11
lines changed

compiler/rustc_hir_typeck/src/method/probe.rs

+18-11
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,9 @@ pub(crate) struct ProbeContext<'a, 'tcx> {
8888

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

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

110111
#[derive(Debug, Clone)]
@@ -183,7 +184,7 @@ struct PickDiagHints<'a, 'tcx> {
183184
#[derive(Debug)]
184185
struct PickConstraintsForShadowed {
185186
autoderefs: usize,
186-
receiver_trait_derefs: usize,
187+
receiver_trait_derefs: Option<usize>,
187188
def_id: DefId,
188189
}
189190

@@ -192,8 +193,11 @@ impl PickConstraintsForShadowed {
192193
autoderefs == self.autoderefs
193194
}
194195

195-
fn may_shadow_based_on_receiver_trait_derefs(&self, receiver_trait_derefs: usize) -> bool {
196-
receiver_trait_derefs != self.receiver_trait_derefs
196+
fn may_shadow_based_on_receiver_trait_derefs(
197+
&self,
198+
receiver_trait_derefs: Option<usize>,
199+
) -> bool {
200+
receiver_trait_derefs != self.receiver_trait_derefs && receiver_trait_derefs.is_some()
197201
}
198202

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

224228
/// Number of jumps along the `Receiver::Target` chain we followed
225229
/// to identify this method. Used only for deshadowing errors.
226-
pub receiver_trait_derefs: usize,
230+
/// Only applies for inherent impls.
231+
pub receiver_trait_derefs: Option<usize>,
227232
}
228233

229234
#[derive(Clone, Debug, PartialEq, Eq)]
@@ -708,7 +713,7 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
708713
static_candidates: RefCell::new(Vec::new()),
709714
scope_expr_id,
710715
is_suggestion,
711-
receiver_trait_derefs: 0usize,
716+
receiver_trait_derefs: None,
712717
}
713718
}
714719

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

766771
fn assemble_inherent_candidates(&mut self) {
767772
for step in self.steps.iter() {
768-
self.receiver_trait_derefs = step.autoderefs;
773+
self.receiver_trait_derefs = Some(step.autoderefs);
769774
self.assemble_probe(&step.self_ty);
770775
}
776+
// FIXME pass on stack again probably
777+
self.receiver_trait_derefs = None;
771778
}
772779

773780
#[instrument(level = "debug", skip(self))]
@@ -2039,11 +2046,11 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
20392046
item: probes[0].0.item,
20402047
kind: TraitPick,
20412048
import_ids: probes[0].0.import_ids.clone(),
2042-
autoderefs: probes[0].0.receiver_trait_derefs,
2049+
autoderefs: 0,
20432050
autoref_or_ptr_adjustment: None,
20442051
self_ty,
20452052
unstable_candidates: vec![],
2046-
receiver_trait_derefs: 0,
2053+
receiver_trait_derefs: probes[0].0.receiver_trait_derefs,
20472054
})
20482055
}
20492056

0 commit comments

Comments
 (0)