rustc_hir_typeck/method/
probe.rs

1use std::assert_matches::debug_assert_matches;
2use std::cell::{Cell, RefCell};
3use std::cmp::max;
4use std::ops::Deref;
5
6use rustc_attr_parsing::is_doc_alias_attrs_contain_symbol;
7use rustc_data_structures::fx::FxHashSet;
8use rustc_data_structures::sso::SsoHashSet;
9use rustc_errors::Applicability;
10use rustc_hir::def::DefKind;
11use rustc_hir::{self as hir, ExprKind, HirId, Node};
12use rustc_hir_analysis::autoderef::{self, Autoderef};
13use rustc_infer::infer::canonical::{Canonical, OriginalQueryValues, QueryResponse};
14use rustc_infer::infer::{BoundRegionConversionTime, DefineOpaqueTypes, InferOk, TyCtxtInferExt};
15use rustc_infer::traits::{ObligationCauseCode, PredicateObligation, query};
16use rustc_middle::middle::stability;
17use rustc_middle::ty::elaborate::supertrait_def_ids;
18use rustc_middle::ty::fast_reject::{DeepRejectCtxt, TreatParams, simplify_type};
19use rustc_middle::ty::{
20    self, AssocContainer, AssocItem, GenericArgs, GenericArgsRef, GenericParamDefKind, ParamEnvAnd,
21    Ty, TyCtxt, TypeVisitableExt, Upcast,
22};
23use rustc_middle::{bug, span_bug};
24use rustc_session::lint;
25use rustc_span::def_id::{DefId, LocalDefId};
26use rustc_span::edit_distance::{
27    edit_distance_with_substrings, find_best_match_for_name_with_substrings,
28};
29use rustc_span::{DUMMY_SP, Ident, Span, Symbol, sym};
30use rustc_trait_selection::error_reporting::infer::need_type_info::TypeAnnotationNeeded;
31use rustc_trait_selection::infer::InferCtxtExt as _;
32use rustc_trait_selection::solve::Goal;
33use rustc_trait_selection::traits::query::CanonicalMethodAutoderefStepsGoal;
34use rustc_trait_selection::traits::query::evaluate_obligation::InferCtxtExt;
35use rustc_trait_selection::traits::query::method_autoderef::{
36    CandidateStep, MethodAutoderefBadTy, MethodAutoderefStepsResult,
37};
38use rustc_trait_selection::traits::{self, ObligationCause, ObligationCtxt};
39use smallvec::{SmallVec, smallvec};
40use tracing::{debug, instrument};
41
42use self::CandidateKind::*;
43pub(crate) use self::PickKind::*;
44use super::{CandidateSource, MethodError, NoMatchData, suggest};
45use crate::FnCtxt;
46
47/// Boolean flag used to indicate if this search is for a suggestion
48/// or not. If true, we can allow ambiguity and so forth.
49#[derive(Clone, Copy, Debug)]
50pub(crate) struct IsSuggestion(pub bool);
51
52pub(crate) struct ProbeContext<'a, 'tcx> {
53    fcx: &'a FnCtxt<'a, 'tcx>,
54    span: Span,
55    mode: Mode,
56    method_name: Option<Ident>,
57    return_type: Option<Ty<'tcx>>,
58
59    /// This is the OriginalQueryValues for the steps queries
60    /// that are answered in steps.
61    orig_steps_var_values: &'a OriginalQueryValues<'tcx>,
62    steps: &'tcx [CandidateStep<'tcx>],
63
64    inherent_candidates: Vec<Candidate<'tcx>>,
65    extension_candidates: Vec<Candidate<'tcx>>,
66    impl_dups: FxHashSet<DefId>,
67
68    /// When probing for names, include names that are close to the
69    /// requested name (by edit distance)
70    allow_similar_names: bool,
71
72    /// List of potential private candidates. Will be trimmed to ones that
73    /// actually apply and then the result inserted into `private_candidate`
74    private_candidates: Vec<Candidate<'tcx>>,
75
76    /// Some(candidate) if there is a private candidate
77    private_candidate: Cell<Option<(DefKind, DefId)>>,
78
79    /// Collects near misses when the candidate functions are missing a `self` keyword and is only
80    /// used for error reporting
81    static_candidates: RefCell<Vec<CandidateSource>>,
82
83    scope_expr_id: HirId,
84
85    /// Is this probe being done for a diagnostic? This will skip some error reporting
86    /// machinery, since we don't particularly care about, for example, similarly named
87    /// candidates if we're *reporting* similarly named candidates.
88    is_suggestion: IsSuggestion,
89}
90
91impl<'a, 'tcx> Deref for ProbeContext<'a, 'tcx> {
92    type Target = FnCtxt<'a, 'tcx>;
93    fn deref(&self) -> &Self::Target {
94        self.fcx
95    }
96}
97
98#[derive(Debug, Clone)]
99pub(crate) struct Candidate<'tcx> {
100    pub(crate) item: ty::AssocItem,
101    pub(crate) kind: CandidateKind<'tcx>,
102    pub(crate) import_ids: SmallVec<[LocalDefId; 1]>,
103}
104
105#[derive(Debug, Clone)]
106pub(crate) enum CandidateKind<'tcx> {
107    InherentImplCandidate { impl_def_id: DefId, receiver_steps: usize },
108    ObjectCandidate(ty::PolyTraitRef<'tcx>),
109    TraitCandidate(ty::PolyTraitRef<'tcx>),
110    WhereClauseCandidate(ty::PolyTraitRef<'tcx>),
111}
112
113#[derive(Debug, PartialEq, Eq, Copy, Clone)]
114enum ProbeResult {
115    NoMatch,
116    BadReturnType,
117    Match,
118}
119
120/// When adjusting a receiver we often want to do one of
121///
122/// - Add a `&` (or `&mut`), converting the receiver from `T` to `&T` (or `&mut T`)
123/// - If the receiver has type `*mut T`, convert it to `*const T`
124///
125/// This type tells us which one to do.
126///
127/// Note that in principle we could do both at the same time. For example, when the receiver has
128/// type `T`, we could autoref it to `&T`, then convert to `*const T`. Or, when it has type `*mut
129/// T`, we could convert it to `*const T`, then autoref to `&*const T`. However, currently we do
130/// (at most) one of these. Either the receiver has type `T` and we convert it to `&T` (or with
131/// `mut`), or it has type `*mut T` and we convert it to `*const T`.
132#[derive(Debug, PartialEq, Copy, Clone)]
133pub(crate) enum AutorefOrPtrAdjustment {
134    /// Receiver has type `T`, add `&` or `&mut` (if `T` is `mut`), and maybe also "unsize" it.
135    /// Unsizing is used to convert a `[T; N]` to `[T]`, which only makes sense when autorefing.
136    Autoref {
137        mutbl: hir::Mutability,
138
139        /// Indicates that the source expression should be "unsized" to a target type.
140        /// This is special-cased for just arrays unsizing to slices.
141        unsize: bool,
142    },
143    /// Receiver has type `*mut T`, convert to `*const T`
144    ToConstPtr,
145
146    /// Reborrow a `Pin<&mut T>` or `Pin<&T>`.
147    ReborrowPin(hir::Mutability),
148}
149
150impl AutorefOrPtrAdjustment {
151    fn get_unsize(&self) -> bool {
152        match self {
153            AutorefOrPtrAdjustment::Autoref { mutbl: _, unsize } => *unsize,
154            AutorefOrPtrAdjustment::ToConstPtr => false,
155            AutorefOrPtrAdjustment::ReborrowPin(_) => false,
156        }
157    }
158}
159
160/// Extra information required only for error reporting.
161#[derive(Debug)]
162struct PickDiagHints<'a, 'tcx> {
163    /// Unstable candidates alongside the stable ones.
164    unstable_candidates: Option<Vec<(Candidate<'tcx>, Symbol)>>,
165
166    /// Collects near misses when trait bounds for type parameters are unsatisfied and is only used
167    /// for error reporting
168    unsatisfied_predicates: &'a mut UnsatisfiedPredicates<'tcx>,
169}
170
171pub(crate) type UnsatisfiedPredicates<'tcx> =
172    Vec<(ty::Predicate<'tcx>, Option<ty::Predicate<'tcx>>, Option<ObligationCause<'tcx>>)>;
173
174/// Criteria to apply when searching for a given Pick. This is used during
175/// the search for potentially shadowed methods to ensure we don't search
176/// more candidates than strictly necessary.
177#[derive(Debug)]
178struct PickConstraintsForShadowed {
179    autoderefs: usize,
180    receiver_steps: Option<usize>,
181    def_id: DefId,
182}
183
184impl PickConstraintsForShadowed {
185    fn may_shadow_based_on_autoderefs(&self, autoderefs: usize) -> bool {
186        autoderefs == self.autoderefs
187    }
188
189    fn candidate_may_shadow(&self, candidate: &Candidate<'_>) -> bool {
190        // An item never shadows itself
191        candidate.item.def_id != self.def_id
192            // and we're only concerned about inherent impls doing the shadowing.
193            // Shadowing can only occur if the shadowed is further along
194            // the Receiver dereferencing chain than the shadowed.
195            && match candidate.kind {
196                CandidateKind::InherentImplCandidate { receiver_steps, .. } => match self.receiver_steps {
197                    Some(shadowed_receiver_steps) => receiver_steps > shadowed_receiver_steps,
198                    _ => false
199                },
200                _ => false
201            }
202    }
203}
204
205#[derive(Debug, Clone)]
206pub(crate) struct Pick<'tcx> {
207    pub item: ty::AssocItem,
208    pub kind: PickKind<'tcx>,
209    pub import_ids: SmallVec<[LocalDefId; 1]>,
210
211    /// Indicates that the source expression should be autoderef'd N times
212    /// ```ignore (not-rust)
213    /// A = expr | *expr | **expr | ...
214    /// ```
215    pub autoderefs: usize,
216
217    /// Indicates that we want to add an autoref (and maybe also unsize it), or if the receiver is
218    /// `*mut T`, convert it to `*const T`.
219    pub autoref_or_ptr_adjustment: Option<AutorefOrPtrAdjustment>,
220    pub self_ty: Ty<'tcx>,
221
222    /// Unstable candidates alongside the stable ones.
223    unstable_candidates: Vec<(Candidate<'tcx>, Symbol)>,
224
225    /// Number of jumps along the `Receiver::Target` chain we followed
226    /// to identify this method. Used only for deshadowing errors.
227    /// Only applies for inherent impls.
228    pub receiver_steps: Option<usize>,
229
230    /// Candidates that were shadowed by supertraits.
231    pub shadowed_candidates: Vec<ty::AssocItem>,
232}
233
234#[derive(Clone, Debug, PartialEq, Eq)]
235pub(crate) enum PickKind<'tcx> {
236    InherentImplPick,
237    ObjectPick,
238    TraitPick,
239    WhereClausePick(
240        // Trait
241        ty::PolyTraitRef<'tcx>,
242    ),
243}
244
245pub(crate) type PickResult<'tcx> = Result<Pick<'tcx>, MethodError<'tcx>>;
246
247#[derive(PartialEq, Eq, Copy, Clone, Debug)]
248pub(crate) enum Mode {
249    // An expression of the form `receiver.method_name(...)`.
250    // Autoderefs are performed on `receiver`, lookup is done based on the
251    // `self` argument of the method, and static methods aren't considered.
252    MethodCall,
253    // An expression of the form `Type::item` or `<T>::item`.
254    // No autoderefs are performed, lookup is done based on the type each
255    // implementation is for, and static methods are included.
256    Path,
257}
258
259#[derive(PartialEq, Eq, Copy, Clone, Debug)]
260pub(crate) enum ProbeScope {
261    // Single candidate coming from pre-resolved delegation method.
262    Single(DefId),
263
264    // Assemble candidates coming only from traits in scope.
265    TraitsInScope,
266
267    // Assemble candidates coming from all traits.
268    AllTraits,
269}
270
271impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
272    /// This is used to offer suggestions to users. It returns methods
273    /// that could have been called which have the desired return
274    /// type. Some effort is made to rule out methods that, if called,
275    /// would result in an error (basically, the same criteria we
276    /// would use to decide if a method is a plausible fit for
277    /// ambiguity purposes).
278    #[instrument(level = "debug", skip(self, candidate_filter))]
279    pub(crate) fn probe_for_return_type_for_diagnostic(
280        &self,
281        span: Span,
282        mode: Mode,
283        return_type: Ty<'tcx>,
284        self_ty: Ty<'tcx>,
285        scope_expr_id: HirId,
286        candidate_filter: impl Fn(&ty::AssocItem) -> bool,
287    ) -> Vec<ty::AssocItem> {
288        let method_names = self
289            .probe_op(
290                span,
291                mode,
292                None,
293                Some(return_type),
294                IsSuggestion(true),
295                self_ty,
296                scope_expr_id,
297                ProbeScope::AllTraits,
298                |probe_cx| Ok(probe_cx.candidate_method_names(candidate_filter)),
299            )
300            .unwrap_or_default();
301        method_names
302            .iter()
303            .flat_map(|&method_name| {
304                self.probe_op(
305                    span,
306                    mode,
307                    Some(method_name),
308                    Some(return_type),
309                    IsSuggestion(true),
310                    self_ty,
311                    scope_expr_id,
312                    ProbeScope::AllTraits,
313                    |probe_cx| probe_cx.pick(),
314                )
315                .ok()
316                .map(|pick| pick.item)
317            })
318            .collect()
319    }
320
321    #[instrument(level = "debug", skip(self))]
322    pub(crate) fn probe_for_name(
323        &self,
324        mode: Mode,
325        item_name: Ident,
326        return_type: Option<Ty<'tcx>>,
327        is_suggestion: IsSuggestion,
328        self_ty: Ty<'tcx>,
329        scope_expr_id: HirId,
330        scope: ProbeScope,
331    ) -> PickResult<'tcx> {
332        self.probe_op(
333            item_name.span,
334            mode,
335            Some(item_name),
336            return_type,
337            is_suggestion,
338            self_ty,
339            scope_expr_id,
340            scope,
341            |probe_cx| probe_cx.pick(),
342        )
343    }
344
345    #[instrument(level = "debug", skip(self))]
346    pub(crate) fn probe_for_name_many(
347        &self,
348        mode: Mode,
349        item_name: Ident,
350        return_type: Option<Ty<'tcx>>,
351        is_suggestion: IsSuggestion,
352        self_ty: Ty<'tcx>,
353        scope_expr_id: HirId,
354        scope: ProbeScope,
355    ) -> Result<Vec<Candidate<'tcx>>, MethodError<'tcx>> {
356        self.probe_op(
357            item_name.span,
358            mode,
359            Some(item_name),
360            return_type,
361            is_suggestion,
362            self_ty,
363            scope_expr_id,
364            scope,
365            |probe_cx| {
366                Ok(probe_cx
367                    .inherent_candidates
368                    .into_iter()
369                    .chain(probe_cx.extension_candidates)
370                    .collect())
371            },
372        )
373    }
374
375    pub(crate) fn probe_op<OP, R>(
376        &'a self,
377        span: Span,
378        mode: Mode,
379        method_name: Option<Ident>,
380        return_type: Option<Ty<'tcx>>,
381        is_suggestion: IsSuggestion,
382        self_ty: Ty<'tcx>,
383        scope_expr_id: HirId,
384        scope: ProbeScope,
385        op: OP,
386    ) -> Result<R, MethodError<'tcx>>
387    where
388        OP: FnOnce(ProbeContext<'_, 'tcx>) -> Result<R, MethodError<'tcx>>,
389    {
390        let mut orig_values = OriginalQueryValues::default();
391        let predefined_opaques_in_body = if self.next_trait_solver() {
392            self.tcx.mk_predefined_opaques_in_body_from_iter(
393                self.inner.borrow_mut().opaque_types().iter_opaque_types().map(|(k, v)| (k, v.ty)),
394            )
395        } else {
396            ty::List::empty()
397        };
398        let value = query::MethodAutoderefSteps { predefined_opaques_in_body, self_ty };
399        let query_input = self
400            .canonicalize_query(ParamEnvAnd { param_env: self.param_env, value }, &mut orig_values);
401
402        let steps = match mode {
403            Mode::MethodCall => self.tcx.method_autoderef_steps(query_input),
404            Mode::Path => self.probe(|_| {
405                // Mode::Path - the deref steps is "trivial". This turns
406                // our CanonicalQuery into a "trivial" QueryResponse. This
407                // is a bit inefficient, but I don't think that writing
408                // special handling for this "trivial case" is a good idea.
409
410                let infcx = &self.infcx;
411                let (ParamEnvAnd { param_env: _, value }, var_values) =
412                    infcx.instantiate_canonical(span, &query_input.canonical);
413                let query::MethodAutoderefSteps { predefined_opaques_in_body: _, self_ty } = value;
414                debug!(?self_ty, ?query_input, "probe_op: Mode::Path");
415                MethodAutoderefStepsResult {
416                    steps: infcx.tcx.arena.alloc_from_iter([CandidateStep {
417                        self_ty: self
418                            .make_query_response_ignoring_pending_obligations(var_values, self_ty),
419                        self_ty_is_opaque: false,
420                        autoderefs: 0,
421                        from_unsafe_deref: false,
422                        unsize: false,
423                        reachable_via_deref: true,
424                    }]),
425                    opt_bad_ty: None,
426                    reached_recursion_limit: false,
427                }
428            }),
429        };
430
431        // If our autoderef loop had reached the recursion limit,
432        // report an overflow error, but continue going on with
433        // the truncated autoderef list.
434        if steps.reached_recursion_limit && !is_suggestion.0 {
435            self.probe(|_| {
436                let ty = &steps
437                    .steps
438                    .last()
439                    .unwrap_or_else(|| span_bug!(span, "reached the recursion limit in 0 steps?"))
440                    .self_ty;
441                let ty = self
442                    .probe_instantiate_query_response(span, &orig_values, ty)
443                    .unwrap_or_else(|_| span_bug!(span, "instantiating {:?} failed?", ty));
444                autoderef::report_autoderef_recursion_limit_error(self.tcx, span, ty.value);
445            });
446        }
447
448        // If we encountered an `_` type or an error type during autoderef, this is
449        // ambiguous.
450        if let Some(bad_ty) = &steps.opt_bad_ty {
451            if is_suggestion.0 {
452                // Ambiguity was encountered during a suggestion. There's really
453                // not much use in suggesting methods in this case.
454                return Err(MethodError::NoMatch(NoMatchData {
455                    static_candidates: Vec::new(),
456                    unsatisfied_predicates: Vec::new(),
457                    out_of_scope_traits: Vec::new(),
458                    similar_candidate: None,
459                    mode,
460                }));
461            } else if bad_ty.reached_raw_pointer
462                && !self.tcx.features().arbitrary_self_types_pointers()
463                && !self.tcx.sess.at_least_rust_2018()
464            {
465                // this case used to be allowed by the compiler,
466                // so we do a future-compat lint here for the 2015 edition
467                // (see https://github.com/rust-lang/rust/issues/46906)
468                self.tcx.node_span_lint(
469                    lint::builtin::TYVAR_BEHIND_RAW_POINTER,
470                    scope_expr_id,
471                    span,
472                    |lint| {
473                        lint.primary_message("type annotations needed");
474                    },
475                );
476            } else {
477                // Ended up encountering a type variable when doing autoderef,
478                // but it may not be a type variable after processing obligations
479                // in our local `FnCtxt`, so don't call `structurally_resolve_type`.
480                let ty = &bad_ty.ty;
481                let ty = self
482                    .probe_instantiate_query_response(span, &orig_values, ty)
483                    .unwrap_or_else(|_| span_bug!(span, "instantiating {:?} failed?", ty));
484                let ty = self.resolve_vars_if_possible(ty.value);
485                let guar = match *ty.kind() {
486                    ty::Infer(ty::TyVar(_)) => {
487                        // We want to get the variable name that the method
488                        // is being called on. If it is a method call.
489                        let err_span = match (mode, self.tcx.hir_node(scope_expr_id)) {
490                            (
491                                Mode::MethodCall,
492                                Node::Expr(hir::Expr {
493                                    kind: ExprKind::MethodCall(_, recv, ..),
494                                    ..
495                                }),
496                            ) => recv.span,
497                            _ => span,
498                        };
499
500                        let raw_ptr_call = bad_ty.reached_raw_pointer
501                            && !self.tcx.features().arbitrary_self_types();
502
503                        let mut err = self.err_ctxt().emit_inference_failure_err(
504                            self.body_id,
505                            err_span,
506                            ty.into(),
507                            TypeAnnotationNeeded::E0282,
508                            !raw_ptr_call,
509                        );
510                        if raw_ptr_call {
511                            err.span_label(span, "cannot call a method on a raw pointer with an unknown pointee type");
512                        }
513                        err.emit()
514                    }
515                    ty::Error(guar) => guar,
516                    _ => bug!("unexpected bad final type in method autoderef"),
517                };
518                self.demand_eqtype(span, ty, Ty::new_error(self.tcx, guar));
519                return Err(MethodError::ErrorReported(guar));
520            }
521        }
522
523        debug!("ProbeContext: steps for self_ty={:?} are {:?}", self_ty, steps);
524
525        // this creates one big transaction so that all type variables etc
526        // that we create during the probe process are removed later
527        self.probe(|_| {
528            let mut probe_cx = ProbeContext::new(
529                self,
530                span,
531                mode,
532                method_name,
533                return_type,
534                &orig_values,
535                steps.steps,
536                scope_expr_id,
537                is_suggestion,
538            );
539
540            match scope {
541                ProbeScope::TraitsInScope => {
542                    probe_cx.assemble_inherent_candidates();
543                    probe_cx.assemble_extension_candidates_for_traits_in_scope();
544                }
545                ProbeScope::AllTraits => {
546                    probe_cx.assemble_inherent_candidates();
547                    probe_cx.assemble_extension_candidates_for_all_traits();
548                }
549                ProbeScope::Single(def_id) => {
550                    let item = self.tcx.associated_item(def_id);
551                    // FIXME(fn_delegation): Delegation to inherent methods is not yet supported.
552                    assert_eq!(item.container, AssocContainer::Trait);
553
554                    let trait_def_id = self.tcx.parent(def_id);
555                    let trait_span = self.tcx.def_span(trait_def_id);
556
557                    let trait_args = self.fresh_args_for_item(trait_span, trait_def_id);
558                    let trait_ref = ty::TraitRef::new_from_args(self.tcx, trait_def_id, trait_args);
559
560                    probe_cx.push_candidate(
561                        Candidate {
562                            item,
563                            kind: CandidateKind::TraitCandidate(ty::Binder::dummy(trait_ref)),
564                            import_ids: smallvec![],
565                        },
566                        false,
567                    );
568                }
569            };
570            op(probe_cx)
571        })
572    }
573}
574
575pub(crate) fn method_autoderef_steps<'tcx>(
576    tcx: TyCtxt<'tcx>,
577    goal: CanonicalMethodAutoderefStepsGoal<'tcx>,
578) -> MethodAutoderefStepsResult<'tcx> {
579    debug!("method_autoderef_steps({:?})", goal);
580
581    let (ref infcx, goal, inference_vars) = tcx.infer_ctxt().build_with_canonical(DUMMY_SP, &goal);
582    let ParamEnvAnd {
583        param_env,
584        value: query::MethodAutoderefSteps { predefined_opaques_in_body, self_ty },
585    } = goal;
586    for (key, ty) in predefined_opaques_in_body {
587        let prev = infcx
588            .register_hidden_type_in_storage(key, ty::ProvisionalHiddenType { span: DUMMY_SP, ty });
589        // It may be possible that two entries in the opaque type storage end up
590        // with the same key after resolving contained inference variables.
591        //
592        // We could put them in the duplicate list but don't have to. The opaques we
593        // encounter here are already tracked in the caller, so there's no need to
594        // also store them here. We'd take them out when computing the query response
595        // and then discard them, as they're already present in the input.
596        //
597        // Ideally we'd drop duplicate opaque type definitions when computing
598        // the canonical input. This is more annoying to implement and may cause a
599        // perf regression, so we do it inside of the query for now.
600        if let Some(prev) = prev {
601            debug!(?key, ?ty, ?prev, "ignore duplicate in `opaque_types_storage`");
602        }
603    }
604
605    // We accept not-yet-defined opaque types in the autoderef
606    // chain to support recursive calls. We do error if the final
607    // infer var is not an opaque.
608    let self_ty_is_opaque = |ty: Ty<'_>| {
609        if let &ty::Infer(ty::TyVar(vid)) = ty.kind() {
610            infcx.has_opaques_with_sub_unified_hidden_type(vid)
611        } else {
612            false
613        }
614    };
615
616    // If arbitrary self types is not enabled, we follow the chain of
617    // `Deref<Target=T>`. If arbitrary self types is enabled, we instead
618    // follow the chain of `Receiver<Target=T>`, but we also record whether
619    // such types are reachable by following the (potentially shorter)
620    // chain of `Deref<Target=T>`. We will use the first list when finding
621    // potentially relevant function implementations (e.g. relevant impl blocks)
622    // but the second list when determining types that the receiver may be
623    // converted to, in order to find out which of those methods might actually
624    // be callable.
625    let mut autoderef_via_deref =
626        Autoderef::new(infcx, param_env, hir::def_id::CRATE_DEF_ID, DUMMY_SP, self_ty)
627            .include_raw_pointers()
628            .silence_errors();
629
630    let mut reached_raw_pointer = false;
631    let arbitrary_self_types_enabled =
632        tcx.features().arbitrary_self_types() || tcx.features().arbitrary_self_types_pointers();
633    let (mut steps, reached_recursion_limit): (Vec<_>, bool) = if arbitrary_self_types_enabled {
634        let reachable_via_deref =
635            autoderef_via_deref.by_ref().map(|_| true).chain(std::iter::repeat(false));
636
637        let mut autoderef_via_receiver =
638            Autoderef::new(infcx, param_env, hir::def_id::CRATE_DEF_ID, DUMMY_SP, self_ty)
639                .include_raw_pointers()
640                .use_receiver_trait()
641                .silence_errors();
642        let steps = autoderef_via_receiver
643            .by_ref()
644            .zip(reachable_via_deref)
645            .map(|((ty, d), reachable_via_deref)| {
646                let step = CandidateStep {
647                    self_ty: infcx
648                        .make_query_response_ignoring_pending_obligations(inference_vars, ty),
649                    self_ty_is_opaque: self_ty_is_opaque(ty),
650                    autoderefs: d,
651                    from_unsafe_deref: reached_raw_pointer,
652                    unsize: false,
653                    reachable_via_deref,
654                };
655                if ty.is_raw_ptr() {
656                    // all the subsequent steps will be from_unsafe_deref
657                    reached_raw_pointer = true;
658                }
659                step
660            })
661            .collect();
662        (steps, autoderef_via_receiver.reached_recursion_limit())
663    } else {
664        let steps = autoderef_via_deref
665            .by_ref()
666            .map(|(ty, d)| {
667                let step = CandidateStep {
668                    self_ty: infcx
669                        .make_query_response_ignoring_pending_obligations(inference_vars, ty),
670                    self_ty_is_opaque: self_ty_is_opaque(ty),
671                    autoderefs: d,
672                    from_unsafe_deref: reached_raw_pointer,
673                    unsize: false,
674                    reachable_via_deref: true,
675                };
676                if ty.is_raw_ptr() {
677                    // all the subsequent steps will be from_unsafe_deref
678                    reached_raw_pointer = true;
679                }
680                step
681            })
682            .collect();
683        (steps, autoderef_via_deref.reached_recursion_limit())
684    };
685    let final_ty = autoderef_via_deref.final_ty();
686    let opt_bad_ty = match final_ty.kind() {
687        ty::Infer(ty::TyVar(_)) if !self_ty_is_opaque(final_ty) => Some(MethodAutoderefBadTy {
688            reached_raw_pointer,
689            ty: infcx.make_query_response_ignoring_pending_obligations(inference_vars, final_ty),
690        }),
691        ty::Error(_) => Some(MethodAutoderefBadTy {
692            reached_raw_pointer,
693            ty: infcx.make_query_response_ignoring_pending_obligations(inference_vars, final_ty),
694        }),
695        ty::Array(elem_ty, _) => {
696            let autoderefs = steps.iter().filter(|s| s.reachable_via_deref).count() - 1;
697            steps.push(CandidateStep {
698                self_ty: infcx.make_query_response_ignoring_pending_obligations(
699                    inference_vars,
700                    Ty::new_slice(infcx.tcx, *elem_ty),
701                ),
702                self_ty_is_opaque: false,
703                autoderefs,
704                // this could be from an unsafe deref if we had
705                // a *mut/const [T; N]
706                from_unsafe_deref: reached_raw_pointer,
707                unsize: true,
708                reachable_via_deref: true, // this is always the final type from
709                                           // autoderef_via_deref
710            });
711
712            None
713        }
714        _ => None,
715    };
716
717    debug!("method_autoderef_steps: steps={:?} opt_bad_ty={:?}", steps, opt_bad_ty);
718    // Need to empty the opaque types storage before it gets dropped.
719    let _ = infcx.take_opaque_types();
720    MethodAutoderefStepsResult {
721        steps: tcx.arena.alloc_from_iter(steps),
722        opt_bad_ty: opt_bad_ty.map(|ty| &*tcx.arena.alloc(ty)),
723        reached_recursion_limit,
724    }
725}
726
727impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
728    fn new(
729        fcx: &'a FnCtxt<'a, 'tcx>,
730        span: Span,
731        mode: Mode,
732        method_name: Option<Ident>,
733        return_type: Option<Ty<'tcx>>,
734        orig_steps_var_values: &'a OriginalQueryValues<'tcx>,
735        steps: &'tcx [CandidateStep<'tcx>],
736        scope_expr_id: HirId,
737        is_suggestion: IsSuggestion,
738    ) -> ProbeContext<'a, 'tcx> {
739        ProbeContext {
740            fcx,
741            span,
742            mode,
743            method_name,
744            return_type,
745            inherent_candidates: Vec::new(),
746            extension_candidates: Vec::new(),
747            impl_dups: FxHashSet::default(),
748            orig_steps_var_values,
749            steps,
750            allow_similar_names: false,
751            private_candidates: Vec::new(),
752            private_candidate: Cell::new(None),
753            static_candidates: RefCell::new(Vec::new()),
754            scope_expr_id,
755            is_suggestion,
756        }
757    }
758
759    fn reset(&mut self) {
760        self.inherent_candidates.clear();
761        self.extension_candidates.clear();
762        self.impl_dups.clear();
763        self.private_candidates.clear();
764        self.private_candidate.set(None);
765        self.static_candidates.borrow_mut().clear();
766    }
767
768    /// When we're looking up a method by path (UFCS), we relate the receiver
769    /// types invariantly. When we are looking up a method by the `.` operator,
770    /// we relate them covariantly.
771    fn variance(&self) -> ty::Variance {
772        match self.mode {
773            Mode::MethodCall => ty::Covariant,
774            Mode::Path => ty::Invariant,
775        }
776    }
777
778    ///////////////////////////////////////////////////////////////////////////
779    // CANDIDATE ASSEMBLY
780
781    fn push_candidate(&mut self, candidate: Candidate<'tcx>, is_inherent: bool) {
782        let is_accessible = if let Some(name) = self.method_name {
783            let item = candidate.item;
784            let hir_id = self.tcx.local_def_id_to_hir_id(self.body_id);
785            let def_scope =
786                self.tcx.adjust_ident_and_get_scope(name, item.container_id(self.tcx), hir_id).1;
787            item.visibility(self.tcx).is_accessible_from(def_scope, self.tcx)
788        } else {
789            true
790        };
791        if is_accessible {
792            if is_inherent {
793                self.inherent_candidates.push(candidate);
794            } else {
795                self.extension_candidates.push(candidate);
796            }
797        } else {
798            self.private_candidates.push(candidate);
799        }
800    }
801
802    fn assemble_inherent_candidates(&mut self) {
803        for step in self.steps.iter() {
804            self.assemble_probe(&step.self_ty, step.autoderefs);
805        }
806    }
807
808    #[instrument(level = "debug", skip(self))]
809    fn assemble_probe(
810        &mut self,
811        self_ty: &Canonical<'tcx, QueryResponse<'tcx, Ty<'tcx>>>,
812        receiver_steps: usize,
813    ) {
814        let raw_self_ty = self_ty.value.value;
815        match *raw_self_ty.kind() {
816            ty::Dynamic(data, ..) if let Some(p) = data.principal() => {
817                // Subtle: we can't use `instantiate_query_response` here: using it will
818                // commit to all of the type equalities assumed by inference going through
819                // autoderef (see the `method-probe-no-guessing` test).
820                //
821                // However, in this code, it is OK if we end up with an object type that is
822                // "more general" than the object type that we are evaluating. For *every*
823                // object type `MY_OBJECT`, a function call that goes through a trait-ref
824                // of the form `<MY_OBJECT as SuperTraitOf(MY_OBJECT)>::func` is a valid
825                // `ObjectCandidate`, and it should be discoverable "exactly" through one
826                // of the iterations in the autoderef loop, so there is no problem with it
827                // being discoverable in another one of these iterations.
828                //
829                // Using `instantiate_canonical` on our
830                // `Canonical<QueryResponse<Ty<'tcx>>>` and then *throwing away* the
831                // `CanonicalVarValues` will exactly give us such a generalization - it
832                // will still match the original object type, but it won't pollute our
833                // type variables in any form, so just do that!
834                let (QueryResponse { value: generalized_self_ty, .. }, _ignored_var_values) =
835                    self.fcx.instantiate_canonical(self.span, self_ty);
836
837                self.assemble_inherent_candidates_from_object(generalized_self_ty);
838                self.assemble_inherent_impl_candidates_for_type(p.def_id(), receiver_steps);
839                self.assemble_inherent_candidates_for_incoherent_ty(raw_self_ty, receiver_steps);
840            }
841            ty::Adt(def, _) => {
842                let def_id = def.did();
843                self.assemble_inherent_impl_candidates_for_type(def_id, receiver_steps);
844                self.assemble_inherent_candidates_for_incoherent_ty(raw_self_ty, receiver_steps);
845            }
846            ty::Foreign(did) => {
847                self.assemble_inherent_impl_candidates_for_type(did, receiver_steps);
848                self.assemble_inherent_candidates_for_incoherent_ty(raw_self_ty, receiver_steps);
849            }
850            ty::Param(_) => {
851                self.assemble_inherent_candidates_from_param(raw_self_ty);
852            }
853            ty::Bool
854            | ty::Char
855            | ty::Int(_)
856            | ty::Uint(_)
857            | ty::Float(_)
858            | ty::Str
859            | ty::Array(..)
860            | ty::Slice(_)
861            | ty::RawPtr(_, _)
862            | ty::Ref(..)
863            | ty::Never
864            | ty::Tuple(..) => {
865                self.assemble_inherent_candidates_for_incoherent_ty(raw_self_ty, receiver_steps)
866            }
867            _ => {}
868        }
869    }
870
871    fn assemble_inherent_candidates_for_incoherent_ty(
872        &mut self,
873        self_ty: Ty<'tcx>,
874        receiver_steps: usize,
875    ) {
876        let Some(simp) = simplify_type(self.tcx, self_ty, TreatParams::InstantiateWithInfer) else {
877            bug!("unexpected incoherent type: {:?}", self_ty)
878        };
879        for &impl_def_id in self.tcx.incoherent_impls(simp).into_iter() {
880            self.assemble_inherent_impl_probe(impl_def_id, receiver_steps);
881        }
882    }
883
884    fn assemble_inherent_impl_candidates_for_type(&mut self, def_id: DefId, receiver_steps: usize) {
885        let impl_def_ids = self.tcx.at(self.span).inherent_impls(def_id).into_iter();
886        for &impl_def_id in impl_def_ids {
887            self.assemble_inherent_impl_probe(impl_def_id, receiver_steps);
888        }
889    }
890
891    #[instrument(level = "debug", skip(self))]
892    fn assemble_inherent_impl_probe(&mut self, impl_def_id: DefId, receiver_steps: usize) {
893        if !self.impl_dups.insert(impl_def_id) {
894            return; // already visited
895        }
896
897        for item in self.impl_or_trait_item(impl_def_id) {
898            if !self.has_applicable_self(&item) {
899                // No receiver declared. Not a candidate.
900                self.record_static_candidate(CandidateSource::Impl(impl_def_id));
901                continue;
902            }
903            self.push_candidate(
904                Candidate {
905                    item,
906                    kind: InherentImplCandidate { impl_def_id, receiver_steps },
907                    import_ids: smallvec![],
908                },
909                true,
910            );
911        }
912    }
913
914    #[instrument(level = "debug", skip(self))]
915    fn assemble_inherent_candidates_from_object(&mut self, self_ty: Ty<'tcx>) {
916        let principal = match self_ty.kind() {
917            ty::Dynamic(data, ..) => Some(data),
918            _ => None,
919        }
920        .and_then(|data| data.principal())
921        .unwrap_or_else(|| {
922            span_bug!(
923                self.span,
924                "non-object {:?} in assemble_inherent_candidates_from_object",
925                self_ty
926            )
927        });
928
929        // It is illegal to invoke a method on a trait instance that refers to
930        // the `Self` type. An [`DynCompatibilityViolation::SupertraitSelf`] error
931        // will be reported by `dyn_compatibility.rs` if the method refers to the
932        // `Self` type anywhere other than the receiver. Here, we use a
933        // instantiation that replaces `Self` with the object type itself. Hence,
934        // a `&self` method will wind up with an argument type like `&dyn Trait`.
935        let trait_ref = principal.with_self_ty(self.tcx, self_ty);
936        self.assemble_candidates_for_bounds(
937            traits::supertraits(self.tcx, trait_ref),
938            |this, new_trait_ref, item| {
939                this.push_candidate(
940                    Candidate {
941                        item,
942                        kind: ObjectCandidate(new_trait_ref),
943                        import_ids: smallvec![],
944                    },
945                    true,
946                );
947            },
948        );
949    }
950
951    #[instrument(level = "debug", skip(self))]
952    fn assemble_inherent_candidates_from_param(&mut self, param_ty: Ty<'tcx>) {
953        debug_assert_matches!(param_ty.kind(), ty::Param(_));
954
955        let tcx = self.tcx;
956
957        // We use `DeepRejectCtxt` here which may return false positive on where clauses
958        // with alias self types. We need to later on reject these as inherent candidates
959        // in `consider_probe`.
960        let bounds = self.param_env.caller_bounds().iter().filter_map(|predicate| {
961            let bound_predicate = predicate.kind();
962            match bound_predicate.skip_binder() {
963                ty::ClauseKind::Trait(trait_predicate) => DeepRejectCtxt::relate_rigid_rigid(tcx)
964                    .types_may_unify(param_ty, trait_predicate.trait_ref.self_ty())
965                    .then(|| bound_predicate.rebind(trait_predicate.trait_ref)),
966                ty::ClauseKind::RegionOutlives(_)
967                | ty::ClauseKind::TypeOutlives(_)
968                | ty::ClauseKind::Projection(_)
969                | ty::ClauseKind::ConstArgHasType(_, _)
970                | ty::ClauseKind::WellFormed(_)
971                | ty::ClauseKind::ConstEvaluatable(_)
972                | ty::ClauseKind::UnstableFeature(_)
973                | ty::ClauseKind::HostEffect(..) => None,
974            }
975        });
976
977        self.assemble_candidates_for_bounds(bounds, |this, poly_trait_ref, item| {
978            this.push_candidate(
979                Candidate {
980                    item,
981                    kind: WhereClauseCandidate(poly_trait_ref),
982                    import_ids: smallvec![],
983                },
984                true,
985            );
986        });
987    }
988
989    // Do a search through a list of bounds, using a callback to actually
990    // create the candidates.
991    fn assemble_candidates_for_bounds<F>(
992        &mut self,
993        bounds: impl Iterator<Item = ty::PolyTraitRef<'tcx>>,
994        mut mk_cand: F,
995    ) where
996        F: for<'b> FnMut(&mut ProbeContext<'b, 'tcx>, ty::PolyTraitRef<'tcx>, ty::AssocItem),
997    {
998        for bound_trait_ref in bounds {
999            debug!("elaborate_bounds(bound_trait_ref={:?})", bound_trait_ref);
1000            for item in self.impl_or_trait_item(bound_trait_ref.def_id()) {
1001                if !self.has_applicable_self(&item) {
1002                    self.record_static_candidate(CandidateSource::Trait(bound_trait_ref.def_id()));
1003                } else {
1004                    mk_cand(self, bound_trait_ref, item);
1005                }
1006            }
1007        }
1008    }
1009
1010    #[instrument(level = "debug", skip(self))]
1011    fn assemble_extension_candidates_for_traits_in_scope(&mut self) {
1012        let mut duplicates = FxHashSet::default();
1013        let opt_applicable_traits = self.tcx.in_scope_traits(self.scope_expr_id);
1014        if let Some(applicable_traits) = opt_applicable_traits {
1015            for trait_candidate in applicable_traits.iter() {
1016                let trait_did = trait_candidate.def_id;
1017                if duplicates.insert(trait_did) {
1018                    self.assemble_extension_candidates_for_trait(
1019                        &trait_candidate.import_ids,
1020                        trait_did,
1021                    );
1022                }
1023            }
1024        }
1025    }
1026
1027    #[instrument(level = "debug", skip(self))]
1028    fn assemble_extension_candidates_for_all_traits(&mut self) {
1029        let mut duplicates = FxHashSet::default();
1030        for trait_info in suggest::all_traits(self.tcx) {
1031            if duplicates.insert(trait_info.def_id) {
1032                self.assemble_extension_candidates_for_trait(&smallvec![], trait_info.def_id);
1033            }
1034        }
1035    }
1036
1037    fn matches_return_type(&self, method: ty::AssocItem, expected: Ty<'tcx>) -> bool {
1038        match method.kind {
1039            ty::AssocKind::Fn { .. } => self.probe(|_| {
1040                let args = self.fresh_args_for_item(self.span, method.def_id);
1041                let fty = self.tcx.fn_sig(method.def_id).instantiate(self.tcx, args);
1042                let fty = self.instantiate_binder_with_fresh_vars(
1043                    self.span,
1044                    BoundRegionConversionTime::FnCall,
1045                    fty,
1046                );
1047                self.can_eq(self.param_env, fty.output(), expected)
1048            }),
1049            _ => false,
1050        }
1051    }
1052
1053    #[instrument(level = "debug", skip(self))]
1054    fn assemble_extension_candidates_for_trait(
1055        &mut self,
1056        import_ids: &SmallVec<[LocalDefId; 1]>,
1057        trait_def_id: DefId,
1058    ) {
1059        let trait_args = self.fresh_args_for_item(self.span, trait_def_id);
1060        let trait_ref = ty::TraitRef::new_from_args(self.tcx, trait_def_id, trait_args);
1061
1062        if self.tcx.is_trait_alias(trait_def_id) {
1063            // For trait aliases, recursively assume all explicitly named traits are relevant
1064            for (bound_trait_pred, _) in
1065                traits::expand_trait_aliases(self.tcx, [(trait_ref.upcast(self.tcx), self.span)]).0
1066            {
1067                assert_eq!(bound_trait_pred.polarity(), ty::PredicatePolarity::Positive);
1068                let bound_trait_ref = bound_trait_pred.map_bound(|pred| pred.trait_ref);
1069                for item in self.impl_or_trait_item(bound_trait_ref.def_id()) {
1070                    if !self.has_applicable_self(&item) {
1071                        self.record_static_candidate(CandidateSource::Trait(
1072                            bound_trait_ref.def_id(),
1073                        ));
1074                    } else {
1075                        self.push_candidate(
1076                            Candidate {
1077                                item,
1078                                import_ids: import_ids.clone(),
1079                                kind: TraitCandidate(bound_trait_ref),
1080                            },
1081                            false,
1082                        );
1083                    }
1084                }
1085            }
1086        } else {
1087            debug_assert!(self.tcx.is_trait(trait_def_id));
1088            if self.tcx.trait_is_auto(trait_def_id) {
1089                return;
1090            }
1091            for item in self.impl_or_trait_item(trait_def_id) {
1092                // Check whether `trait_def_id` defines a method with suitable name.
1093                if !self.has_applicable_self(&item) {
1094                    debug!("method has inapplicable self");
1095                    self.record_static_candidate(CandidateSource::Trait(trait_def_id));
1096                    continue;
1097                }
1098                self.push_candidate(
1099                    Candidate {
1100                        item,
1101                        import_ids: import_ids.clone(),
1102                        kind: TraitCandidate(ty::Binder::dummy(trait_ref)),
1103                    },
1104                    false,
1105                );
1106            }
1107        }
1108    }
1109
1110    fn candidate_method_names(
1111        &self,
1112        candidate_filter: impl Fn(&ty::AssocItem) -> bool,
1113    ) -> Vec<Ident> {
1114        let mut set = FxHashSet::default();
1115        let mut names: Vec<_> = self
1116            .inherent_candidates
1117            .iter()
1118            .chain(&self.extension_candidates)
1119            .filter(|candidate| candidate_filter(&candidate.item))
1120            .filter(|candidate| {
1121                if let Some(return_ty) = self.return_type {
1122                    self.matches_return_type(candidate.item, return_ty)
1123                } else {
1124                    true
1125                }
1126            })
1127            // ensure that we don't suggest unstable methods
1128            .filter(|candidate| {
1129                // note that `DUMMY_SP` is ok here because it is only used for
1130                // suggestions and macro stuff which isn't applicable here.
1131                !matches!(
1132                    self.tcx.eval_stability(candidate.item.def_id, None, DUMMY_SP, None),
1133                    stability::EvalResult::Deny { .. }
1134                )
1135            })
1136            .map(|candidate| candidate.item.ident(self.tcx))
1137            .filter(|&name| set.insert(name))
1138            .collect();
1139
1140        // Sort them by the name so we have a stable result.
1141        names.sort_by(|a, b| a.as_str().cmp(b.as_str()));
1142        names
1143    }
1144
1145    ///////////////////////////////////////////////////////////////////////////
1146    // THE ACTUAL SEARCH
1147
1148    #[instrument(level = "debug", skip(self))]
1149    fn pick(mut self) -> PickResult<'tcx> {
1150        assert!(self.method_name.is_some());
1151
1152        let mut unsatisfied_predicates = Vec::new();
1153
1154        if let Some(r) = self.pick_core(&mut unsatisfied_predicates) {
1155            return r;
1156        }
1157
1158        // If it's a `lookup_probe_for_diagnostic`, then quit early. No need to
1159        // probe for other candidates.
1160        if self.is_suggestion.0 {
1161            return Err(MethodError::NoMatch(NoMatchData {
1162                static_candidates: vec![],
1163                unsatisfied_predicates: vec![],
1164                out_of_scope_traits: vec![],
1165                similar_candidate: None,
1166                mode: self.mode,
1167            }));
1168        }
1169
1170        debug!("pick: actual search failed, assemble diagnostics");
1171
1172        let static_candidates = std::mem::take(self.static_candidates.get_mut());
1173        let private_candidate = self.private_candidate.take();
1174
1175        // things failed, so lets look at all traits, for diagnostic purposes now:
1176        self.reset();
1177
1178        self.assemble_extension_candidates_for_all_traits();
1179
1180        let out_of_scope_traits = match self.pick_core(&mut Vec::new()) {
1181            Some(Ok(p)) => vec![p.item.container_id(self.tcx)],
1182            Some(Err(MethodError::Ambiguity(v))) => v
1183                .into_iter()
1184                .map(|source| match source {
1185                    CandidateSource::Trait(id) => id,
1186                    CandidateSource::Impl(impl_id) => self.tcx.impl_trait_id(impl_id),
1187                })
1188                .collect(),
1189            Some(Err(MethodError::NoMatch(NoMatchData {
1190                out_of_scope_traits: others, ..
1191            }))) => {
1192                assert!(others.is_empty());
1193                vec![]
1194            }
1195            _ => vec![],
1196        };
1197
1198        if let Some((kind, def_id)) = private_candidate {
1199            return Err(MethodError::PrivateMatch(kind, def_id, out_of_scope_traits));
1200        }
1201        let similar_candidate = self.probe_for_similar_candidate()?;
1202
1203        Err(MethodError::NoMatch(NoMatchData {
1204            static_candidates,
1205            unsatisfied_predicates,
1206            out_of_scope_traits,
1207            similar_candidate,
1208            mode: self.mode,
1209        }))
1210    }
1211
1212    fn pick_core(
1213        &self,
1214        unsatisfied_predicates: &mut UnsatisfiedPredicates<'tcx>,
1215    ) -> Option<PickResult<'tcx>> {
1216        // Pick stable methods only first, and consider unstable candidates if not found.
1217        self.pick_all_method(&mut PickDiagHints {
1218            // This first cycle, maintain a list of unstable candidates which
1219            // we encounter. This will end up in the Pick for diagnostics.
1220            unstable_candidates: Some(Vec::new()),
1221            // Contribute to the list of unsatisfied predicates which may
1222            // also be used for diagnostics.
1223            unsatisfied_predicates,
1224        })
1225        .or_else(|| {
1226            self.pick_all_method(&mut PickDiagHints {
1227                // On the second search, don't provide a special list of unstable
1228                // candidates. This indicates to the picking code that it should
1229                // in fact include such unstable candidates in the actual
1230                // search.
1231                unstable_candidates: None,
1232                // And there's no need to duplicate ourselves in the
1233                // unsatisifed predicates list. Provide a throwaway list.
1234                unsatisfied_predicates: &mut Vec::new(),
1235            })
1236        })
1237    }
1238
1239    fn pick_all_method<'b>(
1240        &self,
1241        pick_diag_hints: &mut PickDiagHints<'b, 'tcx>,
1242    ) -> Option<PickResult<'tcx>> {
1243        let track_unstable_candidates = pick_diag_hints.unstable_candidates.is_some();
1244        self.steps
1245            .iter()
1246            // At this point we're considering the types to which the receiver can be converted,
1247            // so we want to follow the `Deref` chain not the `Receiver` chain. Filter out
1248            // steps which can only be reached by following the (longer) `Receiver` chain.
1249            .filter(|step| step.reachable_via_deref)
1250            .filter(|step| {
1251                debug!("pick_all_method: step={:?}", step);
1252                // skip types that are from a type error or that would require dereferencing
1253                // a raw pointer
1254                !step.self_ty.value.references_error() && !step.from_unsafe_deref
1255            })
1256            .find_map(|step| {
1257                let InferOk { value: self_ty, obligations: instantiate_self_ty_obligations } = self
1258                    .fcx
1259                    .probe_instantiate_query_response(
1260                        self.span,
1261                        self.orig_steps_var_values,
1262                        &step.self_ty,
1263                    )
1264                    .unwrap_or_else(|_| {
1265                        span_bug!(self.span, "{:?} was applicable but now isn't?", step.self_ty)
1266                    });
1267
1268                let by_value_pick = self.pick_by_value_method(
1269                    step,
1270                    self_ty,
1271                    &instantiate_self_ty_obligations,
1272                    pick_diag_hints,
1273                );
1274
1275                // Check for shadowing of a by-reference method by a by-value method (see comments on check_for_shadowing)
1276                if let Some(by_value_pick) = by_value_pick {
1277                    if let Ok(by_value_pick) = by_value_pick.as_ref() {
1278                        if by_value_pick.kind == PickKind::InherentImplPick {
1279                            for mutbl in [hir::Mutability::Not, hir::Mutability::Mut] {
1280                                if let Err(e) = self.check_for_shadowed_autorefd_method(
1281                                    by_value_pick,
1282                                    step,
1283                                    self_ty,
1284                                    &instantiate_self_ty_obligations,
1285                                    mutbl,
1286                                    track_unstable_candidates,
1287                                ) {
1288                                    return Some(Err(e));
1289                                }
1290                            }
1291                        }
1292                    }
1293                    return Some(by_value_pick);
1294                }
1295
1296                let autoref_pick = self.pick_autorefd_method(
1297                    step,
1298                    self_ty,
1299                    &instantiate_self_ty_obligations,
1300                    hir::Mutability::Not,
1301                    pick_diag_hints,
1302                    None,
1303                );
1304                // Check for shadowing of a by-mut-ref method by a by-reference method (see comments on check_for_shadowing)
1305                if let Some(autoref_pick) = autoref_pick {
1306                    if let Ok(autoref_pick) = autoref_pick.as_ref() {
1307                        // Check we're not shadowing others
1308                        if autoref_pick.kind == PickKind::InherentImplPick {
1309                            if let Err(e) = self.check_for_shadowed_autorefd_method(
1310                                autoref_pick,
1311                                step,
1312                                self_ty,
1313                                &instantiate_self_ty_obligations,
1314                                hir::Mutability::Mut,
1315                                track_unstable_candidates,
1316                            ) {
1317                                return Some(Err(e));
1318                            }
1319                        }
1320                    }
1321                    return Some(autoref_pick);
1322                }
1323
1324                // Note that no shadowing errors are produced from here on,
1325                // as we consider const ptr methods.
1326                // We allow new methods that take *mut T to shadow
1327                // methods which took *const T, so there is no entry in
1328                // this list for the results of `pick_const_ptr_method`.
1329                // The reason is that the standard pointer cast method
1330                // (on a mutable pointer) always already shadows the
1331                // cast method (on a const pointer). So, if we added
1332                // `pick_const_ptr_method` to this method, the anti-
1333                // shadowing algorithm would always complain about
1334                // the conflict between *const::cast and *mut::cast.
1335                // In practice therefore this does constrain us:
1336                // we cannot add new
1337                //   self: *mut Self
1338                // methods to types such as NonNull or anything else
1339                // which implements Receiver, because this might in future
1340                // shadow existing methods taking
1341                //   self: *const NonNull<Self>
1342                // in the pointee. In practice, methods taking raw pointers
1343                // are rare, and it seems that it should be easily possible
1344                // to avoid such compatibility breaks.
1345                // We also don't check for reborrowed pin methods which
1346                // may be shadowed; these also seem unlikely to occur.
1347                self.pick_autorefd_method(
1348                    step,
1349                    self_ty,
1350                    &instantiate_self_ty_obligations,
1351                    hir::Mutability::Mut,
1352                    pick_diag_hints,
1353                    None,
1354                )
1355                .or_else(|| {
1356                    self.pick_const_ptr_method(
1357                        step,
1358                        self_ty,
1359                        &instantiate_self_ty_obligations,
1360                        pick_diag_hints,
1361                    )
1362                })
1363                .or_else(|| {
1364                    self.pick_reborrow_pin_method(
1365                        step,
1366                        self_ty,
1367                        &instantiate_self_ty_obligations,
1368                        pick_diag_hints,
1369                    )
1370                })
1371            })
1372    }
1373
1374    /// Check for cases where arbitrary self types allows shadowing
1375    /// of methods that might be a compatibility break. Specifically,
1376    /// we have something like:
1377    /// ```ignore (illustrative)
1378    /// struct A;
1379    /// impl A {
1380    ///   fn foo(self: &NonNull<A>) {}
1381    ///      // note this is by reference
1382    /// }
1383    /// ```
1384    /// then we've come along and added this method to `NonNull`:
1385    /// ```ignore (illustrative)
1386    ///   fn foo(self)  // note this is by value
1387    /// ```
1388    /// Report an error in this case.
1389    fn check_for_shadowed_autorefd_method(
1390        &self,
1391        possible_shadower: &Pick<'tcx>,
1392        step: &CandidateStep<'tcx>,
1393        self_ty: Ty<'tcx>,
1394        instantiate_self_ty_obligations: &[PredicateObligation<'tcx>],
1395        mutbl: hir::Mutability,
1396        track_unstable_candidates: bool,
1397    ) -> Result<(), MethodError<'tcx>> {
1398        // The errors emitted by this function are part of
1399        // the arbitrary self types work, and should not impact
1400        // other users.
1401        if !self.tcx.features().arbitrary_self_types()
1402            && !self.tcx.features().arbitrary_self_types_pointers()
1403        {
1404            return Ok(());
1405        }
1406
1407        // We don't want to remember any of the diagnostic hints from this
1408        // shadow search, but we do need to provide Some/None for the
1409        // unstable_candidates in order to reflect the behavior of the
1410        // main search.
1411        let mut pick_diag_hints = PickDiagHints {
1412            unstable_candidates: if track_unstable_candidates { Some(Vec::new()) } else { None },
1413            unsatisfied_predicates: &mut Vec::new(),
1414        };
1415        // Set criteria for how we find methods possibly shadowed by 'possible_shadower'
1416        let pick_constraints = PickConstraintsForShadowed {
1417            // It's the same `self` type...
1418            autoderefs: possible_shadower.autoderefs,
1419            // ... but the method was found in an impl block determined
1420            // by searching further along the Receiver chain than the other,
1421            // showing that it's a smart pointer type causing the problem...
1422            receiver_steps: possible_shadower.receiver_steps,
1423            // ... and they don't end up pointing to the same item in the
1424            // first place (could happen with things like blanket impls for T)
1425            def_id: possible_shadower.item.def_id,
1426        };
1427        // A note on the autoderefs above. Within pick_by_value_method, an extra
1428        // autoderef may be applied in order to reborrow a reference with
1429        // a different lifetime. That seems as though it would break the
1430        // logic of these constraints, since the number of autoderefs could
1431        // no longer be used to identify the fundamental type of the receiver.
1432        // However, this extra autoderef is applied only to by-value calls
1433        // where the receiver is already a reference. So this situation would
1434        // only occur in cases where the shadowing looks like this:
1435        // ```
1436        // struct A;
1437        // impl A {
1438        //   fn foo(self: &&NonNull<A>) {}
1439        //      // note this is by DOUBLE reference
1440        // }
1441        // ```
1442        // then we've come along and added this method to `NonNull`:
1443        // ```
1444        //   fn foo(&self)  // note this is by single reference
1445        // ```
1446        // and the call is:
1447        // ```
1448        // let bar = NonNull<Foo>;
1449        // let bar = &foo;
1450        // bar.foo();
1451        // ```
1452        // In these circumstances, the logic is wrong, and we wouldn't spot
1453        // the shadowing, because the autoderef-based maths wouldn't line up.
1454        // This is a niche case and we can live without generating an error
1455        // in the case of such shadowing.
1456        let potentially_shadowed_pick = self.pick_autorefd_method(
1457            step,
1458            self_ty,
1459            instantiate_self_ty_obligations,
1460            mutbl,
1461            &mut pick_diag_hints,
1462            Some(&pick_constraints),
1463        );
1464        // Look for actual pairs of shadower/shadowed which are
1465        // the sort of shadowing case we want to avoid. Specifically...
1466        if let Some(Ok(possible_shadowed)) = potentially_shadowed_pick.as_ref() {
1467            let sources = [possible_shadower, possible_shadowed]
1468                .into_iter()
1469                .map(|p| self.candidate_source_from_pick(p))
1470                .collect();
1471            return Err(MethodError::Ambiguity(sources));
1472        }
1473        Ok(())
1474    }
1475
1476    /// For each type `T` in the step list, this attempts to find a method where
1477    /// the (transformed) self type is exactly `T`. We do however do one
1478    /// transformation on the adjustment: if we are passing a region pointer in,
1479    /// we will potentially *reborrow* it to a shorter lifetime. This allows us
1480    /// to transparently pass `&mut` pointers, in particular, without consuming
1481    /// them for their entire lifetime.
1482    fn pick_by_value_method(
1483        &self,
1484        step: &CandidateStep<'tcx>,
1485        self_ty: Ty<'tcx>,
1486        instantiate_self_ty_obligations: &[PredicateObligation<'tcx>],
1487        pick_diag_hints: &mut PickDiagHints<'_, 'tcx>,
1488    ) -> Option<PickResult<'tcx>> {
1489        if step.unsize {
1490            return None;
1491        }
1492
1493        self.pick_method(self_ty, instantiate_self_ty_obligations, pick_diag_hints, None).map(|r| {
1494            r.map(|mut pick| {
1495                pick.autoderefs = step.autoderefs;
1496
1497                match *step.self_ty.value.value.kind() {
1498                    // Insert a `&*` or `&mut *` if this is a reference type:
1499                    ty::Ref(_, _, mutbl) => {
1500                        pick.autoderefs += 1;
1501                        pick.autoref_or_ptr_adjustment = Some(AutorefOrPtrAdjustment::Autoref {
1502                            mutbl,
1503                            unsize: pick.autoref_or_ptr_adjustment.is_some_and(|a| a.get_unsize()),
1504                        })
1505                    }
1506
1507                    ty::Adt(def, args)
1508                        if self.tcx.features().pin_ergonomics()
1509                            && self.tcx.is_lang_item(def.did(), hir::LangItem::Pin) =>
1510                    {
1511                        // make sure this is a pinned reference (and not a `Pin<Box>` or something)
1512                        if let ty::Ref(_, _, mutbl) = args[0].expect_ty().kind() {
1513                            pick.autoref_or_ptr_adjustment =
1514                                Some(AutorefOrPtrAdjustment::ReborrowPin(*mutbl));
1515                        }
1516                    }
1517
1518                    _ => (),
1519                }
1520
1521                pick
1522            })
1523        })
1524    }
1525
1526    fn pick_autorefd_method(
1527        &self,
1528        step: &CandidateStep<'tcx>,
1529        self_ty: Ty<'tcx>,
1530        instantiate_self_ty_obligations: &[PredicateObligation<'tcx>],
1531        mutbl: hir::Mutability,
1532        pick_diag_hints: &mut PickDiagHints<'_, 'tcx>,
1533        pick_constraints: Option<&PickConstraintsForShadowed>,
1534    ) -> Option<PickResult<'tcx>> {
1535        let tcx = self.tcx;
1536
1537        if let Some(pick_constraints) = pick_constraints {
1538            if !pick_constraints.may_shadow_based_on_autoderefs(step.autoderefs) {
1539                return None;
1540            }
1541        }
1542
1543        // In general, during probing we erase regions.
1544        let region = tcx.lifetimes.re_erased;
1545
1546        let autoref_ty = Ty::new_ref(tcx, region, self_ty, mutbl);
1547        self.pick_method(
1548            autoref_ty,
1549            instantiate_self_ty_obligations,
1550            pick_diag_hints,
1551            pick_constraints,
1552        )
1553        .map(|r| {
1554            r.map(|mut pick| {
1555                pick.autoderefs = step.autoderefs;
1556                pick.autoref_or_ptr_adjustment =
1557                    Some(AutorefOrPtrAdjustment::Autoref { mutbl, unsize: step.unsize });
1558                pick
1559            })
1560        })
1561    }
1562
1563    /// Looks for applicable methods if we reborrow a `Pin<&mut T>` as a `Pin<&T>`.
1564    #[instrument(level = "debug", skip(self, step, pick_diag_hints))]
1565    fn pick_reborrow_pin_method(
1566        &self,
1567        step: &CandidateStep<'tcx>,
1568        self_ty: Ty<'tcx>,
1569        instantiate_self_ty_obligations: &[PredicateObligation<'tcx>],
1570        pick_diag_hints: &mut PickDiagHints<'_, 'tcx>,
1571    ) -> Option<PickResult<'tcx>> {
1572        if !self.tcx.features().pin_ergonomics() {
1573            return None;
1574        }
1575
1576        // make sure self is a Pin<&mut T>
1577        let inner_ty = match self_ty.kind() {
1578            ty::Adt(def, args) if self.tcx.is_lang_item(def.did(), hir::LangItem::Pin) => {
1579                match args[0].expect_ty().kind() {
1580                    ty::Ref(_, ty, hir::Mutability::Mut) => *ty,
1581                    _ => {
1582                        return None;
1583                    }
1584                }
1585            }
1586            _ => return None,
1587        };
1588
1589        let region = self.tcx.lifetimes.re_erased;
1590        let autopin_ty = Ty::new_pinned_ref(self.tcx, region, inner_ty, hir::Mutability::Not);
1591        self.pick_method(autopin_ty, instantiate_self_ty_obligations, pick_diag_hints, None).map(
1592            |r| {
1593                r.map(|mut pick| {
1594                    pick.autoderefs = step.autoderefs;
1595                    pick.autoref_or_ptr_adjustment =
1596                        Some(AutorefOrPtrAdjustment::ReborrowPin(hir::Mutability::Not));
1597                    pick
1598                })
1599            },
1600        )
1601    }
1602
1603    /// If `self_ty` is `*mut T` then this picks `*const T` methods. The reason why we have a
1604    /// special case for this is because going from `*mut T` to `*const T` with autoderefs and
1605    /// autorefs would require dereferencing the pointer, which is not safe.
1606    fn pick_const_ptr_method(
1607        &self,
1608        step: &CandidateStep<'tcx>,
1609        self_ty: Ty<'tcx>,
1610        instantiate_self_ty_obligations: &[PredicateObligation<'tcx>],
1611        pick_diag_hints: &mut PickDiagHints<'_, 'tcx>,
1612    ) -> Option<PickResult<'tcx>> {
1613        // Don't convert an unsized reference to ptr
1614        if step.unsize {
1615            return None;
1616        }
1617
1618        let &ty::RawPtr(ty, hir::Mutability::Mut) = self_ty.kind() else {
1619            return None;
1620        };
1621
1622        let const_ptr_ty = Ty::new_imm_ptr(self.tcx, ty);
1623        self.pick_method(const_ptr_ty, instantiate_self_ty_obligations, pick_diag_hints, None).map(
1624            |r| {
1625                r.map(|mut pick| {
1626                    pick.autoderefs = step.autoderefs;
1627                    pick.autoref_or_ptr_adjustment = Some(AutorefOrPtrAdjustment::ToConstPtr);
1628                    pick
1629                })
1630            },
1631        )
1632    }
1633
1634    fn pick_method(
1635        &self,
1636        self_ty: Ty<'tcx>,
1637        instantiate_self_ty_obligations: &[PredicateObligation<'tcx>],
1638        pick_diag_hints: &mut PickDiagHints<'_, 'tcx>,
1639        pick_constraints: Option<&PickConstraintsForShadowed>,
1640    ) -> Option<PickResult<'tcx>> {
1641        debug!("pick_method(self_ty={})", self.ty_to_string(self_ty));
1642
1643        for (kind, candidates) in
1644            [("inherent", &self.inherent_candidates), ("extension", &self.extension_candidates)]
1645        {
1646            debug!("searching {} candidates", kind);
1647            let res = self.consider_candidates(
1648                self_ty,
1649                instantiate_self_ty_obligations,
1650                candidates,
1651                pick_diag_hints,
1652                pick_constraints,
1653            );
1654            if let Some(pick) = res {
1655                return Some(pick);
1656            }
1657        }
1658
1659        if self.private_candidate.get().is_none() {
1660            if let Some(Ok(pick)) = self.consider_candidates(
1661                self_ty,
1662                instantiate_self_ty_obligations,
1663                &self.private_candidates,
1664                &mut PickDiagHints {
1665                    unstable_candidates: None,
1666                    unsatisfied_predicates: &mut vec![],
1667                },
1668                None,
1669            ) {
1670                self.private_candidate.set(Some((pick.item.as_def_kind(), pick.item.def_id)));
1671            }
1672        }
1673        None
1674    }
1675
1676    fn consider_candidates(
1677        &self,
1678        self_ty: Ty<'tcx>,
1679        instantiate_self_ty_obligations: &[PredicateObligation<'tcx>],
1680        candidates: &[Candidate<'tcx>],
1681        pick_diag_hints: &mut PickDiagHints<'_, 'tcx>,
1682        pick_constraints: Option<&PickConstraintsForShadowed>,
1683    ) -> Option<PickResult<'tcx>> {
1684        let mut applicable_candidates: Vec<_> = candidates
1685            .iter()
1686            .filter(|candidate| {
1687                pick_constraints
1688                    .map(|pick_constraints| pick_constraints.candidate_may_shadow(&candidate))
1689                    .unwrap_or(true)
1690            })
1691            .map(|probe| {
1692                (
1693                    probe,
1694                    self.consider_probe(
1695                        self_ty,
1696                        instantiate_self_ty_obligations,
1697                        probe,
1698                        &mut pick_diag_hints.unsatisfied_predicates,
1699                    ),
1700                )
1701            })
1702            .filter(|&(_, status)| status != ProbeResult::NoMatch)
1703            .collect();
1704
1705        debug!("applicable_candidates: {:?}", applicable_candidates);
1706
1707        if applicable_candidates.len() > 1 {
1708            if let Some(pick) =
1709                self.collapse_candidates_to_trait_pick(self_ty, &applicable_candidates)
1710            {
1711                return Some(Ok(pick));
1712            }
1713        }
1714
1715        if let Some(uc) = &mut pick_diag_hints.unstable_candidates {
1716            applicable_candidates.retain(|&(candidate, _)| {
1717                if let stability::EvalResult::Deny { feature, .. } =
1718                    self.tcx.eval_stability(candidate.item.def_id, None, self.span, None)
1719                {
1720                    uc.push((candidate.clone(), feature));
1721                    return false;
1722                }
1723                true
1724            });
1725        }
1726
1727        if applicable_candidates.len() > 1 {
1728            // We collapse to a subtrait pick *after* filtering unstable candidates
1729            // to make sure we don't prefer a unstable subtrait method over a stable
1730            // supertrait method.
1731            if self.tcx.features().supertrait_item_shadowing() {
1732                if let Some(pick) =
1733                    self.collapse_candidates_to_subtrait_pick(self_ty, &applicable_candidates)
1734                {
1735                    return Some(Ok(pick));
1736                }
1737            }
1738
1739            let sources =
1740                applicable_candidates.iter().map(|p| self.candidate_source(p.0, self_ty)).collect();
1741            return Some(Err(MethodError::Ambiguity(sources)));
1742        }
1743
1744        applicable_candidates.pop().map(|(probe, status)| match status {
1745            ProbeResult::Match => Ok(probe.to_unadjusted_pick(
1746                self_ty,
1747                pick_diag_hints.unstable_candidates.clone().unwrap_or_default(),
1748            )),
1749            ProbeResult::NoMatch | ProbeResult::BadReturnType => Err(MethodError::BadReturnType),
1750        })
1751    }
1752}
1753
1754impl<'tcx> Pick<'tcx> {
1755    /// In case there were unstable name collisions, emit them as a lint.
1756    /// Checks whether two picks do not refer to the same trait item for the same `Self` type.
1757    /// Only useful for comparisons of picks in order to improve diagnostics.
1758    /// Do not use for type checking.
1759    pub(crate) fn differs_from(&self, other: &Self) -> bool {
1760        let Self {
1761            item: AssocItem { def_id, kind: _, container: _ },
1762            kind: _,
1763            import_ids: _,
1764            autoderefs: _,
1765            autoref_or_ptr_adjustment: _,
1766            self_ty,
1767            unstable_candidates: _,
1768            receiver_steps: _,
1769            shadowed_candidates: _,
1770        } = *self;
1771        self_ty != other.self_ty || def_id != other.item.def_id
1772    }
1773
1774    /// In case there were unstable name collisions, emit them as a lint.
1775    pub(crate) fn maybe_emit_unstable_name_collision_hint(
1776        &self,
1777        tcx: TyCtxt<'tcx>,
1778        span: Span,
1779        scope_expr_id: HirId,
1780    ) {
1781        if self.unstable_candidates.is_empty() {
1782            return;
1783        }
1784        let def_kind = self.item.as_def_kind();
1785        tcx.node_span_lint(lint::builtin::UNSTABLE_NAME_COLLISIONS, scope_expr_id, span, |lint| {
1786            lint.primary_message(format!(
1787                "{} {} with this name may be added to the standard library in the future",
1788                tcx.def_kind_descr_article(def_kind, self.item.def_id),
1789                tcx.def_kind_descr(def_kind, self.item.def_id),
1790            ));
1791
1792            match (self.item.kind, self.item.container) {
1793                (ty::AssocKind::Fn { .. }, _) => {
1794                    // FIXME: This should be a `span_suggestion` instead of `help`
1795                    // However `self.span` only
1796                    // highlights the method name, so we can't use it. Also consider reusing
1797                    // the code from `report_method_error()`.
1798                    lint.help(format!(
1799                        "call with fully qualified syntax `{}(...)` to keep using the current \
1800                             method",
1801                        tcx.def_path_str(self.item.def_id),
1802                    ));
1803                }
1804                (ty::AssocKind::Const { name }, ty::AssocContainer::Trait) => {
1805                    let def_id = self.item.container_id(tcx);
1806                    lint.span_suggestion(
1807                        span,
1808                        "use the fully qualified path to the associated const",
1809                        format!("<{} as {}>::{}", self.self_ty, tcx.def_path_str(def_id), name),
1810                        Applicability::MachineApplicable,
1811                    );
1812                }
1813                _ => {}
1814            }
1815            tcx.disabled_nightly_features(
1816                lint,
1817                self.unstable_candidates.iter().map(|(candidate, feature)| {
1818                    (format!(" `{}`", tcx.def_path_str(candidate.item.def_id)), *feature)
1819                }),
1820            );
1821        });
1822    }
1823}
1824
1825impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
1826    fn select_trait_candidate(
1827        &self,
1828        trait_ref: ty::TraitRef<'tcx>,
1829    ) -> traits::SelectionResult<'tcx, traits::Selection<'tcx>> {
1830        let obligation =
1831            traits::Obligation::new(self.tcx, self.misc(self.span), self.param_env, trait_ref);
1832        traits::SelectionContext::new(self).select(&obligation)
1833    }
1834
1835    /// Used for ambiguous method call error reporting. Uses probing that throws away the result internally,
1836    /// so do not use to make a decision that may lead to a successful compilation.
1837    fn candidate_source(&self, candidate: &Candidate<'tcx>, self_ty: Ty<'tcx>) -> CandidateSource {
1838        match candidate.kind {
1839            InherentImplCandidate { .. } => {
1840                CandidateSource::Impl(candidate.item.container_id(self.tcx))
1841            }
1842            ObjectCandidate(_) | WhereClauseCandidate(_) => {
1843                CandidateSource::Trait(candidate.item.container_id(self.tcx))
1844            }
1845            TraitCandidate(trait_ref) => self.probe(|_| {
1846                let trait_ref = self.instantiate_binder_with_fresh_vars(
1847                    self.span,
1848                    BoundRegionConversionTime::FnCall,
1849                    trait_ref,
1850                );
1851                let (xform_self_ty, _) =
1852                    self.xform_self_ty(candidate.item, trait_ref.self_ty(), trait_ref.args);
1853                // Guide the trait selection to show impls that have methods whose type matches
1854                // up with the `self` parameter of the method.
1855                let _ = self.at(&ObligationCause::dummy(), self.param_env).sup(
1856                    DefineOpaqueTypes::Yes,
1857                    xform_self_ty,
1858                    self_ty,
1859                );
1860                match self.select_trait_candidate(trait_ref) {
1861                    Ok(Some(traits::ImplSource::UserDefined(ref impl_data))) => {
1862                        // If only a single impl matches, make the error message point
1863                        // to that impl.
1864                        CandidateSource::Impl(impl_data.impl_def_id)
1865                    }
1866                    _ => CandidateSource::Trait(candidate.item.container_id(self.tcx)),
1867                }
1868            }),
1869        }
1870    }
1871
1872    fn candidate_source_from_pick(&self, pick: &Pick<'tcx>) -> CandidateSource {
1873        match pick.kind {
1874            InherentImplPick => CandidateSource::Impl(pick.item.container_id(self.tcx)),
1875            ObjectPick | WhereClausePick(_) | TraitPick => {
1876                CandidateSource::Trait(pick.item.container_id(self.tcx))
1877            }
1878        }
1879    }
1880
1881    #[instrument(level = "debug", skip(self, possibly_unsatisfied_predicates), ret)]
1882    fn consider_probe(
1883        &self,
1884        self_ty: Ty<'tcx>,
1885        instantiate_self_ty_obligations: &[PredicateObligation<'tcx>],
1886        probe: &Candidate<'tcx>,
1887        possibly_unsatisfied_predicates: &mut UnsatisfiedPredicates<'tcx>,
1888    ) -> ProbeResult {
1889        self.probe(|snapshot| {
1890            let outer_universe = self.universe();
1891
1892            let mut result = ProbeResult::Match;
1893            let cause = &self.misc(self.span);
1894            let ocx = ObligationCtxt::new_with_diagnostics(self);
1895
1896            // Subtle: we're not *really* instantiating the current self type while
1897            // probing, but instead fully recompute the autoderef steps once we've got
1898            // a final `Pick`. We can't nicely handle these obligations outside of a probe.
1899            //
1900            // We simply handle them for each candidate here for now. That's kinda scuffed
1901            // and ideally we just put them into the `FnCtxt` right away. We need to consider
1902            // them to deal with defining uses in `method_autoderef_steps`.
1903            if self.next_trait_solver() {
1904                ocx.register_obligations(instantiate_self_ty_obligations.iter().cloned());
1905                let errors = ocx.try_evaluate_obligations();
1906                if !errors.is_empty() {
1907                    unreachable!("unexpected autoderef error {errors:?}");
1908                }
1909            }
1910
1911            let mut trait_predicate = None;
1912            let (mut xform_self_ty, mut xform_ret_ty);
1913
1914            match probe.kind {
1915                InherentImplCandidate { impl_def_id, .. } => {
1916                    let impl_args = self.fresh_args_for_item(self.span, impl_def_id);
1917                    let impl_ty = self.tcx.type_of(impl_def_id).instantiate(self.tcx, impl_args);
1918                    (xform_self_ty, xform_ret_ty) =
1919                        self.xform_self_ty(probe.item, impl_ty, impl_args);
1920                    xform_self_ty = ocx.normalize(cause, self.param_env, xform_self_ty);
1921                    match ocx.relate(cause, self.param_env, self.variance(), self_ty, xform_self_ty)
1922                    {
1923                        Ok(()) => {}
1924                        Err(err) => {
1925                            debug!("--> cannot relate self-types {:?}", err);
1926                            return ProbeResult::NoMatch;
1927                        }
1928                    }
1929                    // FIXME: Weirdly, we normalize the ret ty in this candidate, but no other candidates.
1930                    xform_ret_ty = ocx.normalize(cause, self.param_env, xform_ret_ty);
1931                    // Check whether the impl imposes obligations we have to worry about.
1932                    let impl_def_id = probe.item.container_id(self.tcx);
1933                    let impl_bounds =
1934                        self.tcx.predicates_of(impl_def_id).instantiate(self.tcx, impl_args);
1935                    let impl_bounds = ocx.normalize(cause, self.param_env, impl_bounds);
1936                    // Convert the bounds into obligations.
1937                    ocx.register_obligations(traits::predicates_for_generics(
1938                        |idx, span| {
1939                            let code = ObligationCauseCode::WhereClauseInExpr(
1940                                impl_def_id,
1941                                span,
1942                                self.scope_expr_id,
1943                                idx,
1944                            );
1945                            self.cause(self.span, code)
1946                        },
1947                        self.param_env,
1948                        impl_bounds,
1949                    ));
1950                }
1951                TraitCandidate(poly_trait_ref) => {
1952                    // Some trait methods are excluded for arrays before 2021.
1953                    // (`array.into_iter()` wants a slice iterator for compatibility.)
1954                    if let Some(method_name) = self.method_name {
1955                        if self_ty.is_array() && !method_name.span.at_least_rust_2021() {
1956                            let trait_def = self.tcx.trait_def(poly_trait_ref.def_id());
1957                            if trait_def.skip_array_during_method_dispatch {
1958                                return ProbeResult::NoMatch;
1959                            }
1960                        }
1961
1962                        // Some trait methods are excluded for boxed slices before 2024.
1963                        // (`boxed_slice.into_iter()` wants a slice iterator for compatibility.)
1964                        if self_ty.boxed_ty().is_some_and(Ty::is_slice)
1965                            && !method_name.span.at_least_rust_2024()
1966                        {
1967                            let trait_def = self.tcx.trait_def(poly_trait_ref.def_id());
1968                            if trait_def.skip_boxed_slice_during_method_dispatch {
1969                                return ProbeResult::NoMatch;
1970                            }
1971                        }
1972                    }
1973
1974                    let trait_ref = self.instantiate_binder_with_fresh_vars(
1975                        self.span,
1976                        BoundRegionConversionTime::FnCall,
1977                        poly_trait_ref,
1978                    );
1979                    let trait_ref = ocx.normalize(cause, self.param_env, trait_ref);
1980                    (xform_self_ty, xform_ret_ty) =
1981                        self.xform_self_ty(probe.item, trait_ref.self_ty(), trait_ref.args);
1982                    xform_self_ty = ocx.normalize(cause, self.param_env, xform_self_ty);
1983                    match self_ty.kind() {
1984                        // HACK: opaque types will match anything for which their bounds hold.
1985                        // Thus we need to prevent them from trying to match the `&_` autoref
1986                        // candidates that get created for `&self` trait methods.
1987                        ty::Alias(ty::Opaque, alias_ty)
1988                            if !self.next_trait_solver()
1989                                && self.infcx.can_define_opaque_ty(alias_ty.def_id)
1990                                && !xform_self_ty.is_ty_var() =>
1991                        {
1992                            return ProbeResult::NoMatch;
1993                        }
1994                        _ => match ocx.relate(
1995                            cause,
1996                            self.param_env,
1997                            self.variance(),
1998                            self_ty,
1999                            xform_self_ty,
2000                        ) {
2001                            Ok(()) => {}
2002                            Err(err) => {
2003                                debug!("--> cannot relate self-types {:?}", err);
2004                                return ProbeResult::NoMatch;
2005                            }
2006                        },
2007                    }
2008                    let obligation = traits::Obligation::new(
2009                        self.tcx,
2010                        cause.clone(),
2011                        self.param_env,
2012                        ty::Binder::dummy(trait_ref),
2013                    );
2014
2015                    // We only need this hack to deal with fatal overflow in the old solver.
2016                    if self.infcx.next_trait_solver() || self.infcx.predicate_may_hold(&obligation)
2017                    {
2018                        ocx.register_obligation(obligation);
2019                    } else {
2020                        result = ProbeResult::NoMatch;
2021                        if let Ok(Some(candidate)) = self.select_trait_candidate(trait_ref) {
2022                            for nested_obligation in candidate.nested_obligations() {
2023                                if !self.infcx.predicate_may_hold(&nested_obligation) {
2024                                    possibly_unsatisfied_predicates.push((
2025                                        self.resolve_vars_if_possible(nested_obligation.predicate),
2026                                        Some(self.resolve_vars_if_possible(obligation.predicate)),
2027                                        Some(nested_obligation.cause),
2028                                    ));
2029                                }
2030                            }
2031                        }
2032                    }
2033
2034                    trait_predicate = Some(trait_ref.upcast(self.tcx));
2035                }
2036                ObjectCandidate(poly_trait_ref) | WhereClauseCandidate(poly_trait_ref) => {
2037                    let trait_ref = self.instantiate_binder_with_fresh_vars(
2038                        self.span,
2039                        BoundRegionConversionTime::FnCall,
2040                        poly_trait_ref,
2041                    );
2042                    (xform_self_ty, xform_ret_ty) =
2043                        self.xform_self_ty(probe.item, trait_ref.self_ty(), trait_ref.args);
2044
2045                    if matches!(probe.kind, WhereClauseCandidate(_)) {
2046                        // `WhereClauseCandidate` requires that the self type is a param,
2047                        // because it has special behavior with candidate preference as an
2048                        // inherent pick.
2049                        match ocx.structurally_normalize_ty(
2050                            cause,
2051                            self.param_env,
2052                            trait_ref.self_ty(),
2053                        ) {
2054                            Ok(ty) => {
2055                                if !matches!(ty.kind(), ty::Param(_)) {
2056                                    debug!("--> not a param ty: {xform_self_ty:?}");
2057                                    return ProbeResult::NoMatch;
2058                                }
2059                            }
2060                            Err(errors) => {
2061                                debug!("--> cannot relate self-types {:?}", errors);
2062                                return ProbeResult::NoMatch;
2063                            }
2064                        }
2065                    }
2066
2067                    xform_self_ty = ocx.normalize(cause, self.param_env, xform_self_ty);
2068                    match ocx.relate(cause, self.param_env, self.variance(), self_ty, xform_self_ty)
2069                    {
2070                        Ok(()) => {}
2071                        Err(err) => {
2072                            debug!("--> cannot relate self-types {:?}", err);
2073                            return ProbeResult::NoMatch;
2074                        }
2075                    }
2076                }
2077            }
2078
2079            // See <https://github.com/rust-lang/trait-system-refactor-initiative/issues/134>.
2080            //
2081            // In the new solver, check the well-formedness of the return type.
2082            // This emulates, in a way, the predicates that fall out of
2083            // normalizing the return type in the old solver.
2084            //
2085            // FIXME(-Znext-solver): We alternatively could check the predicates of
2086            // the method itself hold, but we intentionally do not do this in the old
2087            // solver b/c of cycles, and doing it in the new solver would be stronger.
2088            // This should be fixed in the future, since it likely leads to much better
2089            // method winnowing.
2090            if let Some(xform_ret_ty) = xform_ret_ty
2091                && self.infcx.next_trait_solver()
2092            {
2093                ocx.register_obligation(traits::Obligation::new(
2094                    self.tcx,
2095                    cause.clone(),
2096                    self.param_env,
2097                    ty::ClauseKind::WellFormed(xform_ret_ty.into()),
2098                ));
2099            }
2100
2101            // Evaluate those obligations to see if they might possibly hold.
2102            for error in ocx.try_evaluate_obligations() {
2103                result = ProbeResult::NoMatch;
2104                let nested_predicate = self.resolve_vars_if_possible(error.obligation.predicate);
2105                if let Some(trait_predicate) = trait_predicate
2106                    && nested_predicate == self.resolve_vars_if_possible(trait_predicate)
2107                {
2108                    // Don't report possibly unsatisfied predicates if the root
2109                    // trait obligation from a `TraitCandidate` is unsatisfied.
2110                    // That just means the candidate doesn't hold.
2111                } else {
2112                    possibly_unsatisfied_predicates.push((
2113                        nested_predicate,
2114                        Some(self.resolve_vars_if_possible(error.root_obligation.predicate))
2115                            .filter(|root_predicate| *root_predicate != nested_predicate),
2116                        Some(error.obligation.cause),
2117                    ));
2118                }
2119            }
2120
2121            if let ProbeResult::Match = result
2122                && let Some(return_ty) = self.return_type
2123                && let Some(mut xform_ret_ty) = xform_ret_ty
2124            {
2125                // `xform_ret_ty` has only been normalized for `InherentImplCandidate`.
2126                // We don't normalize the other candidates for perf/backwards-compat reasons...
2127                // but `self.return_type` is only set on the diagnostic-path, so we
2128                // should be okay doing it here.
2129                if !matches!(probe.kind, InherentImplCandidate { .. }) {
2130                    xform_ret_ty = ocx.normalize(&cause, self.param_env, xform_ret_ty);
2131                }
2132
2133                debug!("comparing return_ty {:?} with xform ret ty {:?}", return_ty, xform_ret_ty);
2134                match ocx.relate(cause, self.param_env, self.variance(), xform_ret_ty, return_ty) {
2135                    Ok(()) => {}
2136                    Err(_) => {
2137                        result = ProbeResult::BadReturnType;
2138                    }
2139                }
2140
2141                // Evaluate those obligations to see if they might possibly hold.
2142                for error in ocx.try_evaluate_obligations() {
2143                    result = ProbeResult::NoMatch;
2144                    possibly_unsatisfied_predicates.push((
2145                        error.obligation.predicate,
2146                        Some(error.root_obligation.predicate)
2147                            .filter(|predicate| *predicate != error.obligation.predicate),
2148                        Some(error.root_obligation.cause),
2149                    ));
2150                }
2151            }
2152
2153            if self.infcx.next_trait_solver() {
2154                if self.should_reject_candidate_due_to_opaque_treated_as_rigid(trait_predicate) {
2155                    result = ProbeResult::NoMatch;
2156                }
2157            }
2158
2159            // Previously, method probe used `evaluate_predicate` to determine if a predicate
2160            // was impossible to satisfy. This did a leak check, so we must also do a leak
2161            // check here to prevent backwards-incompatible ambiguity being introduced. See
2162            // `tests/ui/methods/leak-check-disquality.rs` for a simple example of when this
2163            // may happen.
2164            if let Err(_) = self.leak_check(outer_universe, Some(snapshot)) {
2165                result = ProbeResult::NoMatch;
2166            }
2167
2168            result
2169        })
2170    }
2171
2172    /// Trait candidates for not-yet-defined opaque types are a somewhat hacky.
2173    ///
2174    /// We want to only accept trait methods if they were hold even if the
2175    /// opaque types were rigid. To handle this, we both check that for trait
2176    /// candidates the goal were to hold even when treating opaques as rigid,
2177    /// see [OpaqueTypesJank](rustc_trait_selection::solve::OpaqueTypesJank).
2178    ///
2179    /// We also check that all opaque types encountered as self types in the
2180    /// autoderef chain don't get constrained when applying the candidate.
2181    /// Importantly, this also handles calling methods taking `&self` on
2182    /// `impl Trait` to reject the "by-self" candidate.
2183    ///
2184    /// This needs to happen at the end of `consider_probe` as we need to take
2185    /// all the constraints from that into account.
2186    #[instrument(level = "debug", skip(self), ret)]
2187    fn should_reject_candidate_due_to_opaque_treated_as_rigid(
2188        &self,
2189        trait_predicate: Option<ty::Predicate<'tcx>>,
2190    ) -> bool {
2191        // This function is what hacky and doesn't perfectly do what we want it to.
2192        // It's not soundness critical and we should be able to freely improve this
2193        // in the future.
2194        //
2195        // Some concrete edge cases include the fact that `goal_may_hold_opaque_types_jank`
2196        // also fails if there are any constraints opaques which are never used as a self
2197        // type. We also allow where-bounds which are currently ambiguous but end up
2198        // constraining an opaque later on.
2199
2200        // Check whether the trait candidate would not be applicable if the
2201        // opaque type were rigid.
2202        if let Some(predicate) = trait_predicate {
2203            let goal = Goal { param_env: self.param_env, predicate };
2204            if !self.infcx.goal_may_hold_opaque_types_jank(goal) {
2205                return true;
2206            }
2207        }
2208
2209        // Check whether any opaque types in the autoderef chain have been
2210        // constrained.
2211        for step in self.steps {
2212            if step.self_ty_is_opaque {
2213                debug!(?step.autoderefs, ?step.self_ty, "self_type_is_opaque");
2214                let constrained_opaque = self.probe(|_| {
2215                    // If we fail to instantiate the self type of this
2216                    // step, this part of the deref-chain is no longer
2217                    // reachable. In this case we don't care about opaque
2218                    // types there.
2219                    let Ok(ok) = self.fcx.probe_instantiate_query_response(
2220                        self.span,
2221                        self.orig_steps_var_values,
2222                        &step.self_ty,
2223                    ) else {
2224                        debug!("failed to instantiate self_ty");
2225                        return false;
2226                    };
2227                    let ocx = ObligationCtxt::new(self);
2228                    let self_ty = ocx.register_infer_ok_obligations(ok);
2229                    if !ocx.try_evaluate_obligations().is_empty() {
2230                        debug!("failed to prove instantiate self_ty obligations");
2231                        return false;
2232                    }
2233
2234                    !self.resolve_vars_if_possible(self_ty).is_ty_var()
2235                });
2236                if constrained_opaque {
2237                    debug!("opaque type has been constrained");
2238                    return true;
2239                }
2240            }
2241        }
2242
2243        false
2244    }
2245
2246    /// Sometimes we get in a situation where we have multiple probes that are all impls of the
2247    /// same trait, but we don't know which impl to use. In this case, since in all cases the
2248    /// external interface of the method can be determined from the trait, it's ok not to decide.
2249    /// We can basically just collapse all of the probes for various impls into one where-clause
2250    /// probe. This will result in a pending obligation so when more type-info is available we can
2251    /// make the final decision.
2252    ///
2253    /// Example (`tests/ui/methods/method-two-trait-defer-resolution-1.rs`):
2254    ///
2255    /// ```ignore (illustrative)
2256    /// trait Foo { ... }
2257    /// impl Foo for Vec<i32> { ... }
2258    /// impl Foo for Vec<usize> { ... }
2259    /// ```
2260    ///
2261    /// Now imagine the receiver is `Vec<_>`. It doesn't really matter at this time which impl we
2262    /// use, so it's ok to just commit to "using the method from the trait Foo".
2263    fn collapse_candidates_to_trait_pick(
2264        &self,
2265        self_ty: Ty<'tcx>,
2266        probes: &[(&Candidate<'tcx>, ProbeResult)],
2267    ) -> Option<Pick<'tcx>> {
2268        // Do all probes correspond to the same trait?
2269        let container = probes[0].0.item.trait_container(self.tcx)?;
2270        for (p, _) in &probes[1..] {
2271            let p_container = p.item.trait_container(self.tcx)?;
2272            if p_container != container {
2273                return None;
2274            }
2275        }
2276
2277        // FIXME: check the return type here somehow.
2278        // If so, just use this trait and call it a day.
2279        Some(Pick {
2280            item: probes[0].0.item,
2281            kind: TraitPick,
2282            import_ids: probes[0].0.import_ids.clone(),
2283            autoderefs: 0,
2284            autoref_or_ptr_adjustment: None,
2285            self_ty,
2286            unstable_candidates: vec![],
2287            receiver_steps: None,
2288            shadowed_candidates: vec![],
2289        })
2290    }
2291
2292    /// Much like `collapse_candidates_to_trait_pick`, this method allows us to collapse
2293    /// multiple conflicting picks if there is one pick whose trait container is a subtrait
2294    /// of the trait containers of all of the other picks.
2295    ///
2296    /// This implements RFC #3624.
2297    fn collapse_candidates_to_subtrait_pick(
2298        &self,
2299        self_ty: Ty<'tcx>,
2300        probes: &[(&Candidate<'tcx>, ProbeResult)],
2301    ) -> Option<Pick<'tcx>> {
2302        let mut child_candidate = probes[0].0;
2303        let mut child_trait = child_candidate.item.trait_container(self.tcx)?;
2304        let mut supertraits: SsoHashSet<_> = supertrait_def_ids(self.tcx, child_trait).collect();
2305
2306        let mut remaining_candidates: Vec<_> = probes[1..].iter().map(|&(p, _)| p).collect();
2307        while !remaining_candidates.is_empty() {
2308            let mut made_progress = false;
2309            let mut next_round = vec![];
2310
2311            for remaining_candidate in remaining_candidates {
2312                let remaining_trait = remaining_candidate.item.trait_container(self.tcx)?;
2313                if supertraits.contains(&remaining_trait) {
2314                    made_progress = true;
2315                    continue;
2316                }
2317
2318                // This pick is not a supertrait of the `child_pick`.
2319                // Check if it's a subtrait of the `child_pick`, instead.
2320                // If it is, then it must have been a subtrait of every
2321                // other pick we've eliminated at this point. It will
2322                // take over at this point.
2323                let remaining_trait_supertraits: SsoHashSet<_> =
2324                    supertrait_def_ids(self.tcx, remaining_trait).collect();
2325                if remaining_trait_supertraits.contains(&child_trait) {
2326                    child_candidate = remaining_candidate;
2327                    child_trait = remaining_trait;
2328                    supertraits = remaining_trait_supertraits;
2329                    made_progress = true;
2330                    continue;
2331                }
2332
2333                // `child_pick` is not a supertrait of this pick.
2334                // Don't bail here, since we may be comparing two supertraits
2335                // of a common subtrait. These two supertraits won't be related
2336                // at all, but we will pick them up next round when we find their
2337                // child as we continue iterating in this round.
2338                next_round.push(remaining_candidate);
2339            }
2340
2341            if made_progress {
2342                // If we've made progress, iterate again.
2343                remaining_candidates = next_round;
2344            } else {
2345                // Otherwise, we must have at least two candidates which
2346                // are not related to each other at all.
2347                return None;
2348            }
2349        }
2350
2351        Some(Pick {
2352            item: child_candidate.item,
2353            kind: TraitPick,
2354            import_ids: child_candidate.import_ids.clone(),
2355            autoderefs: 0,
2356            autoref_or_ptr_adjustment: None,
2357            self_ty,
2358            unstable_candidates: vec![],
2359            shadowed_candidates: probes
2360                .iter()
2361                .map(|(c, _)| c.item)
2362                .filter(|item| item.def_id != child_candidate.item.def_id)
2363                .collect(),
2364            receiver_steps: None,
2365        })
2366    }
2367
2368    /// Similarly to `probe_for_return_type`, this method attempts to find the best matching
2369    /// candidate method where the method name may have been misspelled. Similarly to other
2370    /// edit distance based suggestions, we provide at most one such suggestion.
2371    #[instrument(level = "debug", skip(self))]
2372    pub(crate) fn probe_for_similar_candidate(
2373        &mut self,
2374    ) -> Result<Option<ty::AssocItem>, MethodError<'tcx>> {
2375        debug!("probing for method names similar to {:?}", self.method_name);
2376
2377        self.probe(|_| {
2378            let mut pcx = ProbeContext::new(
2379                self.fcx,
2380                self.span,
2381                self.mode,
2382                self.method_name,
2383                self.return_type,
2384                self.orig_steps_var_values,
2385                self.steps,
2386                self.scope_expr_id,
2387                IsSuggestion(true),
2388            );
2389            pcx.allow_similar_names = true;
2390            pcx.assemble_inherent_candidates();
2391            pcx.assemble_extension_candidates_for_all_traits();
2392
2393            let method_names = pcx.candidate_method_names(|_| true);
2394            pcx.allow_similar_names = false;
2395            let applicable_close_candidates: Vec<ty::AssocItem> = method_names
2396                .iter()
2397                .filter_map(|&method_name| {
2398                    pcx.reset();
2399                    pcx.method_name = Some(method_name);
2400                    pcx.assemble_inherent_candidates();
2401                    pcx.assemble_extension_candidates_for_all_traits();
2402                    pcx.pick_core(&mut Vec::new()).and_then(|pick| pick.ok()).map(|pick| pick.item)
2403                })
2404                .collect();
2405
2406            if applicable_close_candidates.is_empty() {
2407                Ok(None)
2408            } else {
2409                let best_name = {
2410                    let names = applicable_close_candidates
2411                        .iter()
2412                        .map(|cand| cand.name())
2413                        .collect::<Vec<Symbol>>();
2414                    find_best_match_for_name_with_substrings(
2415                        &names,
2416                        self.method_name.unwrap().name,
2417                        None,
2418                    )
2419                }
2420                .or_else(|| {
2421                    applicable_close_candidates
2422                        .iter()
2423                        .find(|cand| self.matches_by_doc_alias(cand.def_id))
2424                        .map(|cand| cand.name())
2425                });
2426                Ok(best_name.and_then(|best_name| {
2427                    applicable_close_candidates
2428                        .into_iter()
2429                        .find(|method| method.name() == best_name)
2430                }))
2431            }
2432        })
2433    }
2434
2435    ///////////////////////////////////////////////////////////////////////////
2436    // MISCELLANY
2437    fn has_applicable_self(&self, item: &ty::AssocItem) -> bool {
2438        // "Fast track" -- check for usage of sugar when in method call
2439        // mode.
2440        //
2441        // In Path mode (i.e., resolving a value like `T::next`), consider any
2442        // associated value (i.e., methods, constants) but not types.
2443        match self.mode {
2444            Mode::MethodCall => item.is_method(),
2445            Mode::Path => match item.kind {
2446                ty::AssocKind::Type { .. } => false,
2447                ty::AssocKind::Fn { .. } | ty::AssocKind::Const { .. } => true,
2448            },
2449        }
2450        // FIXME -- check for types that deref to `Self`,
2451        // like `Rc<Self>` and so on.
2452        //
2453        // Note also that the current code will break if this type
2454        // includes any of the type parameters defined on the method
2455        // -- but this could be overcome.
2456    }
2457
2458    fn record_static_candidate(&self, source: CandidateSource) {
2459        self.static_candidates.borrow_mut().push(source);
2460    }
2461
2462    #[instrument(level = "debug", skip(self))]
2463    fn xform_self_ty(
2464        &self,
2465        item: ty::AssocItem,
2466        impl_ty: Ty<'tcx>,
2467        args: GenericArgsRef<'tcx>,
2468    ) -> (Ty<'tcx>, Option<Ty<'tcx>>) {
2469        if item.is_fn() && self.mode == Mode::MethodCall {
2470            let sig = self.xform_method_sig(item.def_id, args);
2471            (sig.inputs()[0], Some(sig.output()))
2472        } else {
2473            (impl_ty, None)
2474        }
2475    }
2476
2477    #[instrument(level = "debug", skip(self))]
2478    fn xform_method_sig(&self, method: DefId, args: GenericArgsRef<'tcx>) -> ty::FnSig<'tcx> {
2479        let fn_sig = self.tcx.fn_sig(method);
2480        debug!(?fn_sig);
2481
2482        assert!(!args.has_escaping_bound_vars());
2483
2484        // It is possible for type parameters or early-bound lifetimes
2485        // to appear in the signature of `self`. The generic parameters
2486        // we are given do not include type/lifetime parameters for the
2487        // method yet. So create fresh variables here for those too,
2488        // if there are any.
2489        let generics = self.tcx.generics_of(method);
2490        assert_eq!(args.len(), generics.parent_count);
2491
2492        let xform_fn_sig = if generics.is_own_empty() {
2493            fn_sig.instantiate(self.tcx, args)
2494        } else {
2495            let args = GenericArgs::for_item(self.tcx, method, |param, _| {
2496                let i = param.index as usize;
2497                if i < args.len() {
2498                    args[i]
2499                } else {
2500                    match param.kind {
2501                        GenericParamDefKind::Lifetime => {
2502                            // In general, during probe we erase regions.
2503                            self.tcx.lifetimes.re_erased.into()
2504                        }
2505                        GenericParamDefKind::Type { .. } | GenericParamDefKind::Const { .. } => {
2506                            self.var_for_def(self.span, param)
2507                        }
2508                    }
2509                }
2510            });
2511            fn_sig.instantiate(self.tcx, args)
2512        };
2513
2514        self.tcx.instantiate_bound_regions_with_erased(xform_fn_sig)
2515    }
2516
2517    /// Determine if the given associated item type is relevant in the current context.
2518    fn is_relevant_kind_for_mode(&self, kind: ty::AssocKind) -> bool {
2519        match (self.mode, kind) {
2520            (Mode::MethodCall, ty::AssocKind::Fn { .. }) => true,
2521            (Mode::Path, ty::AssocKind::Const { .. } | ty::AssocKind::Fn { .. }) => true,
2522            _ => false,
2523        }
2524    }
2525
2526    /// Determine if the associated item with the given DefId matches
2527    /// the desired name via a doc alias.
2528    fn matches_by_doc_alias(&self, def_id: DefId) -> bool {
2529        let Some(method) = self.method_name else {
2530            return false;
2531        };
2532        let Some(local_def_id) = def_id.as_local() else {
2533            return false;
2534        };
2535        let hir_id = self.fcx.tcx.local_def_id_to_hir_id(local_def_id);
2536        let attrs = self.fcx.tcx.hir_attrs(hir_id);
2537
2538        if is_doc_alias_attrs_contain_symbol(attrs.into_iter(), method.name) {
2539            return true;
2540        }
2541
2542        for attr in attrs {
2543            if attr.has_name(sym::rustc_confusables) {
2544                let Some(confusables) = attr.meta_item_list() else {
2545                    continue;
2546                };
2547                // #[rustc_confusables("foo", "bar"))]
2548                for n in confusables {
2549                    if let Some(lit) = n.lit()
2550                        && method.name == lit.symbol
2551                    {
2552                        return true;
2553                    }
2554                }
2555            }
2556        }
2557        false
2558    }
2559
2560    /// Finds the method with the appropriate name (or return type, as the case may be). If
2561    /// `allow_similar_names` is set, find methods with close-matching names.
2562    // The length of the returned iterator is nearly always 0 or 1 and this
2563    // method is fairly hot.
2564    fn impl_or_trait_item(&self, def_id: DefId) -> SmallVec<[ty::AssocItem; 1]> {
2565        if let Some(name) = self.method_name {
2566            if self.allow_similar_names {
2567                let max_dist = max(name.as_str().len(), 3) / 3;
2568                self.tcx
2569                    .associated_items(def_id)
2570                    .in_definition_order()
2571                    .filter(|x| {
2572                        if !self.is_relevant_kind_for_mode(x.kind) {
2573                            return false;
2574                        }
2575                        if let Some(d) = edit_distance_with_substrings(
2576                            name.as_str(),
2577                            x.name().as_str(),
2578                            max_dist,
2579                        ) {
2580                            return d > 0;
2581                        }
2582                        self.matches_by_doc_alias(x.def_id)
2583                    })
2584                    .copied()
2585                    .collect()
2586            } else {
2587                self.fcx
2588                    .associated_value(def_id, name)
2589                    .filter(|x| self.is_relevant_kind_for_mode(x.kind))
2590                    .map_or_else(SmallVec::new, |x| SmallVec::from_buf([x]))
2591            }
2592        } else {
2593            self.tcx
2594                .associated_items(def_id)
2595                .in_definition_order()
2596                .filter(|x| self.is_relevant_kind_for_mode(x.kind))
2597                .copied()
2598                .collect()
2599        }
2600    }
2601}
2602
2603impl<'tcx> Candidate<'tcx> {
2604    fn to_unadjusted_pick(
2605        &self,
2606        self_ty: Ty<'tcx>,
2607        unstable_candidates: Vec<(Candidate<'tcx>, Symbol)>,
2608    ) -> Pick<'tcx> {
2609        Pick {
2610            item: self.item,
2611            kind: match self.kind {
2612                InherentImplCandidate { .. } => InherentImplPick,
2613                ObjectCandidate(_) => ObjectPick,
2614                TraitCandidate(_) => TraitPick,
2615                WhereClauseCandidate(trait_ref) => {
2616                    // Only trait derived from where-clauses should
2617                    // appear here, so they should not contain any
2618                    // inference variables or other artifacts. This
2619                    // means they are safe to put into the
2620                    // `WhereClausePick`.
2621                    assert!(
2622                        !trait_ref.skip_binder().args.has_infer()
2623                            && !trait_ref.skip_binder().args.has_placeholders()
2624                    );
2625
2626                    WhereClausePick(trait_ref)
2627                }
2628            },
2629            import_ids: self.import_ids.clone(),
2630            autoderefs: 0,
2631            autoref_or_ptr_adjustment: None,
2632            self_ty,
2633            unstable_candidates,
2634            receiver_steps: match self.kind {
2635                InherentImplCandidate { receiver_steps, .. } => Some(receiver_steps),
2636                _ => None,
2637            },
2638            shadowed_candidates: vec![],
2639        }
2640    }
2641}