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