Skip to content

Commit 81ca8e3

Browse files
committed
Turn a delayed bug back into a normal bug by winnowing private method candidates instead of assuming any candidate of the right name will apply.
1 parent 51a6dde commit 81ca8e3

File tree

2 files changed

+23
-9
lines changed

2 files changed

+23
-9
lines changed

compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1071,7 +1071,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
10711071
ty::ImplContainer => {
10721072
if segments.len() == 1 {
10731073
// `<T>::assoc` will end up here, and so
1074-
// can `T::assoc`. It this came from an
1074+
// can `T::assoc`. If this came from an
10751075
// inherent impl, we need to record the
10761076
// `T` for posterity (see `UserSelfTy` for
10771077
// details).
@@ -1411,7 +1411,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
14111411
) {
14121412
Ok(ok) => self.register_infer_ok_obligations(ok),
14131413
Err(_) => {
1414-
self.dcx().span_delayed_bug(
1414+
self.dcx().span_bug(
14151415
span,
14161416
format!(
14171417
"instantiate_value_path: (UFCS) {self_ty:?} was a subtype of {impl_ty:?} but now is not?",

compiler/rustc_hir_typeck/src/method/probe.rs

+21-7
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ use rustc_trait_selection::traits::query::method_autoderef::{
4141
use rustc_trait_selection::traits::query::CanonicalTyGoal;
4242
use rustc_trait_selection::traits::ObligationCtxt;
4343
use rustc_trait_selection::traits::{self, ObligationCause};
44+
use std::cell::Cell;
4445
use std::cell::RefCell;
4546
use std::cmp::max;
4647
use std::iter;
@@ -76,8 +77,12 @@ pub(crate) struct ProbeContext<'a, 'tcx> {
7677
/// requested name (by edit distance)
7778
allow_similar_names: bool,
7879

80+
/// List of potential private candidates. Will be trimmed to ones that
81+
/// actually apply and then the result inserted into `private_candidate`
82+
private_candidates: Vec<Candidate<'tcx>>,
83+
7984
/// Some(candidate) if there is a private candidate
80-
private_candidate: Option<(DefKind, DefId)>,
85+
private_candidate: Cell<Option<(DefKind, DefId)>>,
8186

8287
/// Collects near misses when the candidate functions are missing a `self` keyword and is only
8388
/// used for error reporting
@@ -581,7 +586,8 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
581586
orig_steps_var_values,
582587
steps,
583588
allow_similar_names: false,
584-
private_candidate: None,
589+
private_candidates: Vec::new(),
590+
private_candidate: Cell::new(None),
585591
static_candidates: RefCell::new(Vec::new()),
586592
unsatisfied_predicates: RefCell::new(Vec::new()),
587593
scope_expr_id,
@@ -593,7 +599,8 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
593599
self.inherent_candidates.clear();
594600
self.extension_candidates.clear();
595601
self.impl_dups.clear();
596-
self.private_candidate = None;
602+
self.private_candidates.clear();
603+
self.private_candidate.set(None);
597604
self.static_candidates.borrow_mut().clear();
598605
self.unsatisfied_predicates.borrow_mut().clear();
599606
}
@@ -617,9 +624,8 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
617624
} else {
618625
self.extension_candidates.push(candidate);
619626
}
620-
} else if self.private_candidate.is_none() {
621-
self.private_candidate =
622-
Some((candidate.item.kind.as_def_kind(), candidate.item.def_id));
627+
} else {
628+
self.private_candidates.push(candidate);
623629
}
624630
}
625631

@@ -1171,7 +1177,7 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
11711177
let mut possibly_unsatisfied_predicates = Vec::new();
11721178

11731179
for (kind, candidates) in
1174-
&[("inherent", &self.inherent_candidates), ("extension", &self.extension_candidates)]
1180+
[("inherent", &self.inherent_candidates), ("extension", &self.extension_candidates)]
11751181
{
11761182
debug!("searching {} candidates", kind);
11771183
let res = self.consider_candidates(
@@ -1185,6 +1191,14 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
11851191
}
11861192
}
11871193

1194+
if self.private_candidate.get().is_none() {
1195+
if let Some(Ok(pick)) =
1196+
self.consider_candidates(self_ty, &self.private_candidates, &mut vec![], None)
1197+
{
1198+
self.private_candidate.set(Some((pick.item.kind.as_def_kind(), pick.item.def_id)));
1199+
}
1200+
}
1201+
11881202
// `pick_method` may be called twice for the same self_ty if no stable methods
11891203
// match. Only extend once.
11901204
if unstable_candidates.is_some() {

0 commit comments

Comments
 (0)