1use std::cell::{Cell, RefCell};
2use std::cmp::max;
3use std::ops::Deref;
4
5use rustc_data_structures::fx::FxHashSet;
6use rustc_data_structures::sso::SsoHashSet;
7use rustc_errors::Applicability;
8use rustc_hir as hir;
9use rustc_hir::HirId;
10use rustc_hir::def::DefKind;
11use rustc_hir_analysis::autoderef::{self, Autoderef};
12use rustc_infer::infer::canonical::{Canonical, OriginalQueryValues, QueryResponse};
13use rustc_infer::infer::{self, DefineOpaqueTypes, InferOk, TyCtxtInferExt};
14use rustc_infer::traits::ObligationCauseCode;
15use rustc_middle::middle::stability;
16use rustc_middle::query::Providers;
17use rustc_middle::ty::fast_reject::{TreatParams, simplify_type};
18use rustc_middle::ty::{
19 self, AssocItem, AssocItemContainer, GenericArgs, GenericArgsRef, GenericParamDefKind,
20 ParamEnvAnd, Ty, TyCtxt, TypeVisitableExt, Upcast,
21};
22use rustc_middle::{bug, span_bug};
23use rustc_session::lint;
24use rustc_span::def_id::{DefId, LocalDefId};
25use rustc_span::edit_distance::{
26 edit_distance_with_substrings, find_best_match_for_name_with_substrings,
27};
28use rustc_span::{DUMMY_SP, Ident, Span, Symbol, sym};
29use rustc_trait_selection::error_reporting::infer::need_type_info::TypeAnnotationNeeded;
30use rustc_trait_selection::infer::InferCtxtExt as _;
31use rustc_trait_selection::traits::query::CanonicalTyGoal;
32use rustc_trait_selection::traits::query::evaluate_obligation::InferCtxtExt;
33use rustc_trait_selection::traits::query::method_autoderef::{
34 CandidateStep, MethodAutoderefBadTy, MethodAutoderefStepsResult,
35};
36use rustc_trait_selection::traits::{self, ObligationCause, ObligationCtxt};
37use rustc_type_ir::elaborate::supertrait_def_ids;
38use smallvec::{SmallVec, smallvec};
39use tracing::{debug, instrument};
40
41use self::CandidateKind::*;
42pub(crate) use self::PickKind::*;
43use super::{CandidateSource, MethodError, NoMatchData, suggest};
44use crate::FnCtxt;
45
46#[derive(Clone, Copy, Debug)]
49pub(crate) struct IsSuggestion(pub bool);
50
51pub(crate) struct ProbeContext<'a, 'tcx> {
52 fcx: &'a FnCtxt<'a, 'tcx>,
53 span: Span,
54 mode: Mode,
55 method_name: Option<Ident>,
56 return_type: Option<Ty<'tcx>>,
57
58 orig_steps_var_values: &'a OriginalQueryValues<'tcx>,
61 steps: &'tcx [CandidateStep<'tcx>],
62
63 inherent_candidates: Vec<Candidate<'tcx>>,
64 extension_candidates: Vec<Candidate<'tcx>>,
65 impl_dups: FxHashSet<DefId>,
66
67 allow_similar_names: bool,
70
71 private_candidates: Vec<Candidate<'tcx>>,
74
75 private_candidate: Cell<Option<(DefKind, DefId)>>,
77
78 static_candidates: RefCell<Vec<CandidateSource>>,
81
82 scope_expr_id: HirId,
83
84 is_suggestion: IsSuggestion,
88}
89
90impl<'a, 'tcx> Deref for ProbeContext<'a, 'tcx> {
91 type Target = FnCtxt<'a, 'tcx>;
92 fn deref(&self) -> &Self::Target {
93 self.fcx
94 }
95}
96
97#[derive(Debug, Clone)]
98pub(crate) struct Candidate<'tcx> {
99 pub(crate) item: ty::AssocItem,
100 pub(crate) kind: CandidateKind<'tcx>,
101 pub(crate) import_ids: SmallVec<[LocalDefId; 1]>,
102}
103
104#[derive(Debug, Clone)]
105pub(crate) enum CandidateKind<'tcx> {
106 InherentImplCandidate { impl_def_id: DefId, receiver_steps: usize },
107 ObjectCandidate(ty::PolyTraitRef<'tcx>),
108 TraitCandidate(ty::PolyTraitRef<'tcx>),
109 WhereClauseCandidate(ty::PolyTraitRef<'tcx>),
110}
111
112#[derive(Debug, PartialEq, Eq, Copy, Clone)]
113enum ProbeResult {
114 NoMatch,
115 BadReturnType,
116 Match,
117}
118
119#[derive(Debug, PartialEq, Copy, Clone)]
132pub(crate) enum AutorefOrPtrAdjustment {
133 Autoref {
136 mutbl: hir::Mutability,
137
138 unsize: bool,
141 },
142 ToConstPtr,
144
145 ReborrowPin(hir::Mutability),
147}
148
149impl AutorefOrPtrAdjustment {
150 fn get_unsize(&self) -> bool {
151 match self {
152 AutorefOrPtrAdjustment::Autoref { mutbl: _, unsize } => *unsize,
153 AutorefOrPtrAdjustment::ToConstPtr => false,
154 AutorefOrPtrAdjustment::ReborrowPin(_) => false,
155 }
156 }
157}
158
159#[derive(Debug)]
161struct PickDiagHints<'a, 'tcx> {
162 unstable_candidates: Option<Vec<(Candidate<'tcx>, Symbol)>>,
164
165 unsatisfied_predicates: &'a mut Vec<(
168 ty::Predicate<'tcx>,
169 Option<ty::Predicate<'tcx>>,
170 Option<ObligationCause<'tcx>>,
171 )>,
172}
173
174#[derive(Debug)]
178struct PickConstraintsForShadowed {
179 autoderefs: usize,
180 receiver_steps: Option<usize>,
181 def_id: DefId,
182}
183
184impl PickConstraintsForShadowed {
185 fn may_shadow_based_on_autoderefs(&self, autoderefs: usize) -> bool {
186 autoderefs == self.autoderefs
187 }
188
189 fn candidate_may_shadow(&self, candidate: &Candidate<'_>) -> bool {
190 candidate.item.def_id != self.def_id
192 && match candidate.kind {
196 CandidateKind::InherentImplCandidate { receiver_steps, .. } => match self.receiver_steps {
197 Some(shadowed_receiver_steps) => receiver_steps > shadowed_receiver_steps,
198 _ => false
199 },
200 _ => false
201 }
202 }
203}
204
205#[derive(Debug, Clone)]
206pub(crate) struct Pick<'tcx> {
207 pub item: ty::AssocItem,
208 pub kind: PickKind<'tcx>,
209 pub import_ids: SmallVec<[LocalDefId; 1]>,
210
211 pub autoderefs: usize,
216
217 pub autoref_or_ptr_adjustment: Option<AutorefOrPtrAdjustment>,
220 pub self_ty: Ty<'tcx>,
221
222 unstable_candidates: Vec<(Candidate<'tcx>, Symbol)>,
224
225 pub receiver_steps: Option<usize>,
229
230 pub shadowed_candidates: Vec<ty::AssocItem>,
232}
233
234#[derive(Clone, Debug, PartialEq, Eq)]
235pub(crate) enum PickKind<'tcx> {
236 InherentImplPick,
237 ObjectPick,
238 TraitPick,
239 WhereClausePick(
240 ty::PolyTraitRef<'tcx>,
242 ),
243}
244
245pub(crate) type PickResult<'tcx> = Result<Pick<'tcx>, MethodError<'tcx>>;
246
247#[derive(PartialEq, Eq, Copy, Clone, Debug)]
248pub(crate) enum Mode {
249 MethodCall,
253 Path,
257}
258
259#[derive(PartialEq, Eq, Copy, Clone, Debug)]
260pub(crate) enum ProbeScope {
261 Single(DefId),
263
264 TraitsInScope,
266
267 AllTraits,
269}
270
271impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
272 #[instrument(level = "debug", skip(self, candidate_filter))]
279 pub(crate) fn probe_for_return_type_for_diagnostic(
280 &self,
281 span: Span,
282 mode: Mode,
283 return_type: Ty<'tcx>,
284 self_ty: Ty<'tcx>,
285 scope_expr_id: HirId,
286 candidate_filter: impl Fn(&ty::AssocItem) -> bool,
287 ) -> Vec<ty::AssocItem> {
288 let method_names = self
289 .probe_op(
290 span,
291 mode,
292 None,
293 Some(return_type),
294 IsSuggestion(true),
295 self_ty,
296 scope_expr_id,
297 ProbeScope::AllTraits,
298 |probe_cx| Ok(probe_cx.candidate_method_names(candidate_filter)),
299 )
300 .unwrap_or_default();
301 method_names
302 .iter()
303 .flat_map(|&method_name| {
304 self.probe_op(
305 span,
306 mode,
307 Some(method_name),
308 Some(return_type),
309 IsSuggestion(true),
310 self_ty,
311 scope_expr_id,
312 ProbeScope::AllTraits,
313 |probe_cx| probe_cx.pick(),
314 )
315 .ok()
316 .map(|pick| pick.item)
317 })
318 .collect()
319 }
320
321 #[instrument(level = "debug", skip(self))]
322 pub(crate) fn probe_for_name(
323 &self,
324 mode: Mode,
325 item_name: Ident,
326 return_type: Option<Ty<'tcx>>,
327 is_suggestion: IsSuggestion,
328 self_ty: Ty<'tcx>,
329 scope_expr_id: HirId,
330 scope: ProbeScope,
331 ) -> PickResult<'tcx> {
332 self.probe_op(
333 item_name.span,
334 mode,
335 Some(item_name),
336 return_type,
337 is_suggestion,
338 self_ty,
339 scope_expr_id,
340 scope,
341 |probe_cx| probe_cx.pick(),
342 )
343 }
344
345 #[instrument(level = "debug", skip(self))]
346 pub(crate) fn probe_for_name_many(
347 &self,
348 mode: Mode,
349 item_name: Ident,
350 return_type: Option<Ty<'tcx>>,
351 is_suggestion: IsSuggestion,
352 self_ty: Ty<'tcx>,
353 scope_expr_id: HirId,
354 scope: ProbeScope,
355 ) -> Result<Vec<Candidate<'tcx>>, MethodError<'tcx>> {
356 self.probe_op(
357 item_name.span,
358 mode,
359 Some(item_name),
360 return_type,
361 is_suggestion,
362 self_ty,
363 scope_expr_id,
364 scope,
365 |probe_cx| {
366 Ok(probe_cx
367 .inherent_candidates
368 .into_iter()
369 .chain(probe_cx.extension_candidates)
370 .collect())
371 },
372 )
373 }
374
375 pub(crate) fn probe_op<OP, R>(
376 &'a self,
377 span: Span,
378 mode: Mode,
379 method_name: Option<Ident>,
380 return_type: Option<Ty<'tcx>>,
381 is_suggestion: IsSuggestion,
382 self_ty: Ty<'tcx>,
383 scope_expr_id: HirId,
384 scope: ProbeScope,
385 op: OP,
386 ) -> Result<R, MethodError<'tcx>>
387 where
388 OP: FnOnce(ProbeContext<'_, 'tcx>) -> Result<R, MethodError<'tcx>>,
389 {
390 let mut orig_values = OriginalQueryValues::default();
391 let query_input = self.canonicalize_query(
392 ParamEnvAnd { param_env: self.param_env, value: self_ty },
393 &mut orig_values,
394 );
395
396 let steps = match mode {
397 Mode::MethodCall => self.tcx.method_autoderef_steps(query_input),
398 Mode::Path => self.probe(|_| {
399 let infcx = &self.infcx;
405 let (ParamEnvAnd { param_env: _, value: self_ty }, canonical_inference_vars) =
406 infcx.instantiate_canonical(span, &query_input.canonical);
407 debug!(?self_ty, ?query_input, "probe_op: Mode::Path");
408 MethodAutoderefStepsResult {
409 steps: infcx.tcx.arena.alloc_from_iter([CandidateStep {
410 self_ty: self.make_query_response_ignoring_pending_obligations(
411 canonical_inference_vars,
412 self_ty,
413 ),
414 autoderefs: 0,
415 from_unsafe_deref: false,
416 unsize: false,
417 reachable_via_deref: true,
418 }]),
419 opt_bad_ty: None,
420 reached_recursion_limit: false,
421 }
422 }),
423 };
424
425 if steps.reached_recursion_limit && !is_suggestion.0 {
429 self.probe(|_| {
430 let ty = &steps
431 .steps
432 .last()
433 .unwrap_or_else(|| span_bug!(span, "reached the recursion limit in 0 steps?"))
434 .self_ty;
435 let ty = self
436 .probe_instantiate_query_response(span, &orig_values, ty)
437 .unwrap_or_else(|_| span_bug!(span, "instantiating {:?} failed?", ty));
438 autoderef::report_autoderef_recursion_limit_error(self.tcx, span, ty.value);
439 });
440 }
441
442 if let Some(bad_ty) = &steps.opt_bad_ty {
445 if is_suggestion.0 {
446 return Err(MethodError::NoMatch(NoMatchData {
449 static_candidates: Vec::new(),
450 unsatisfied_predicates: Vec::new(),
451 out_of_scope_traits: Vec::new(),
452 similar_candidate: None,
453 mode,
454 }));
455 } else if bad_ty.reached_raw_pointer
456 && !self.tcx.features().arbitrary_self_types_pointers()
457 && !self.tcx.sess.at_least_rust_2018()
458 {
459 self.tcx.node_span_lint(
463 lint::builtin::TYVAR_BEHIND_RAW_POINTER,
464 scope_expr_id,
465 span,
466 |lint| {
467 lint.primary_message("type annotations needed");
468 },
469 );
470 } else {
471 let ty = &bad_ty.ty;
475 let ty = self
476 .probe_instantiate_query_response(span, &orig_values, ty)
477 .unwrap_or_else(|_| span_bug!(span, "instantiating {:?} failed?", ty));
478 let ty = self.resolve_vars_if_possible(ty.value);
479 let guar = match *ty.kind() {
480 ty::Infer(ty::TyVar(_)) => {
481 let raw_ptr_call = bad_ty.reached_raw_pointer
482 && !self.tcx.features().arbitrary_self_types();
483 let mut err = self.err_ctxt().emit_inference_failure_err(
484 self.body_id,
485 span,
486 ty.into(),
487 TypeAnnotationNeeded::E0282,
488 !raw_ptr_call,
489 );
490 if raw_ptr_call {
491 err.span_label(span, "cannot call a method on a raw pointer with an unknown pointee type");
492 }
493 err.emit()
494 }
495 ty::Error(guar) => guar,
496 _ => bug!("unexpected bad final type in method autoderef"),
497 };
498 self.demand_eqtype(span, ty, Ty::new_error(self.tcx, guar));
499 return Err(MethodError::ErrorReported(guar));
500 }
501 }
502
503 debug!("ProbeContext: steps for self_ty={:?} are {:?}", self_ty, steps);
504
505 self.probe(|_| {
508 let mut probe_cx = ProbeContext::new(
509 self,
510 span,
511 mode,
512 method_name,
513 return_type,
514 &orig_values,
515 steps.steps,
516 scope_expr_id,
517 is_suggestion,
518 );
519
520 match scope {
521 ProbeScope::TraitsInScope => {
522 probe_cx.assemble_inherent_candidates();
523 probe_cx.assemble_extension_candidates_for_traits_in_scope();
524 }
525 ProbeScope::AllTraits => {
526 probe_cx.assemble_inherent_candidates();
527 probe_cx.assemble_extension_candidates_for_all_traits();
528 }
529 ProbeScope::Single(def_id) => {
530 let item = self.tcx.associated_item(def_id);
531 assert_eq!(item.container, AssocItemContainer::Trait);
533
534 let trait_def_id = self.tcx.parent(def_id);
535 let trait_span = self.tcx.def_span(trait_def_id);
536
537 let trait_args = self.fresh_args_for_item(trait_span, trait_def_id);
538 let trait_ref = ty::TraitRef::new_from_args(self.tcx, trait_def_id, trait_args);
539
540 probe_cx.push_candidate(
541 Candidate {
542 item,
543 kind: CandidateKind::TraitCandidate(ty::Binder::dummy(trait_ref)),
544 import_ids: smallvec![],
545 },
546 false,
547 );
548 }
549 };
550 op(probe_cx)
551 })
552 }
553}
554
555pub(crate) fn provide(providers: &mut Providers) {
556 providers.method_autoderef_steps = method_autoderef_steps;
557}
558
559fn method_autoderef_steps<'tcx>(
560 tcx: TyCtxt<'tcx>,
561 goal: CanonicalTyGoal<'tcx>,
562) -> MethodAutoderefStepsResult<'tcx> {
563 debug!("method_autoderef_steps({:?})", goal);
564
565 let (ref infcx, goal, inference_vars) = tcx.infer_ctxt().build_with_canonical(DUMMY_SP, &goal);
566 let ParamEnvAnd { param_env, value: self_ty } = goal;
567
568 let mut autoderef_via_deref =
578 Autoderef::new(infcx, param_env, hir::def_id::CRATE_DEF_ID, DUMMY_SP, self_ty)
579 .include_raw_pointers()
580 .silence_errors();
581
582 let mut reached_raw_pointer = false;
583 let arbitrary_self_types_enabled =
584 tcx.features().arbitrary_self_types() || tcx.features().arbitrary_self_types_pointers();
585 let (mut steps, reached_recursion_limit): (Vec<_>, bool) = if arbitrary_self_types_enabled {
586 let reachable_via_deref =
587 autoderef_via_deref.by_ref().map(|_| true).chain(std::iter::repeat(false));
588
589 let mut autoderef_via_receiver =
590 Autoderef::new(infcx, param_env, hir::def_id::CRATE_DEF_ID, DUMMY_SP, self_ty)
591 .include_raw_pointers()
592 .use_receiver_trait()
593 .silence_errors();
594 let steps = autoderef_via_receiver
595 .by_ref()
596 .zip(reachable_via_deref)
597 .map(|((ty, d), reachable_via_deref)| {
598 let step = CandidateStep {
599 self_ty: infcx
600 .make_query_response_ignoring_pending_obligations(inference_vars, ty),
601 autoderefs: d,
602 from_unsafe_deref: reached_raw_pointer,
603 unsize: false,
604 reachable_via_deref,
605 };
606 if ty.is_raw_ptr() {
607 reached_raw_pointer = true;
609 }
610 step
611 })
612 .collect();
613 (steps, autoderef_via_receiver.reached_recursion_limit())
614 } else {
615 let steps = autoderef_via_deref
616 .by_ref()
617 .map(|(ty, d)| {
618 let step = CandidateStep {
619 self_ty: infcx
620 .make_query_response_ignoring_pending_obligations(inference_vars, ty),
621 autoderefs: d,
622 from_unsafe_deref: reached_raw_pointer,
623 unsize: false,
624 reachable_via_deref: true,
625 };
626 if ty.is_raw_ptr() {
627 reached_raw_pointer = true;
629 }
630 step
631 })
632 .collect();
633 (steps, autoderef_via_deref.reached_recursion_limit())
634 };
635 let final_ty = autoderef_via_deref.final_ty(true);
636 let opt_bad_ty = match final_ty.kind() {
637 ty::Infer(ty::TyVar(_)) | ty::Error(_) => Some(MethodAutoderefBadTy {
638 reached_raw_pointer,
639 ty: infcx.make_query_response_ignoring_pending_obligations(inference_vars, final_ty),
640 }),
641 ty::Array(elem_ty, _) => {
642 let autoderefs = steps.iter().filter(|s| s.reachable_via_deref).count() - 1;
643 steps.push(CandidateStep {
644 self_ty: infcx.make_query_response_ignoring_pending_obligations(
645 inference_vars,
646 Ty::new_slice(infcx.tcx, *elem_ty),
647 ),
648 autoderefs,
649 from_unsafe_deref: reached_raw_pointer,
652 unsize: true,
653 reachable_via_deref: true, });
656
657 None
658 }
659 _ => None,
660 };
661
662 debug!("method_autoderef_steps: steps={:?} opt_bad_ty={:?}", steps, opt_bad_ty);
663
664 MethodAutoderefStepsResult {
665 steps: tcx.arena.alloc_from_iter(steps),
666 opt_bad_ty: opt_bad_ty.map(|ty| &*tcx.arena.alloc(ty)),
667 reached_recursion_limit,
668 }
669}
670
671impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
672 fn new(
673 fcx: &'a FnCtxt<'a, 'tcx>,
674 span: Span,
675 mode: Mode,
676 method_name: Option<Ident>,
677 return_type: Option<Ty<'tcx>>,
678 orig_steps_var_values: &'a OriginalQueryValues<'tcx>,
679 steps: &'tcx [CandidateStep<'tcx>],
680 scope_expr_id: HirId,
681 is_suggestion: IsSuggestion,
682 ) -> ProbeContext<'a, 'tcx> {
683 ProbeContext {
684 fcx,
685 span,
686 mode,
687 method_name,
688 return_type,
689 inherent_candidates: Vec::new(),
690 extension_candidates: Vec::new(),
691 impl_dups: FxHashSet::default(),
692 orig_steps_var_values,
693 steps,
694 allow_similar_names: false,
695 private_candidates: Vec::new(),
696 private_candidate: Cell::new(None),
697 static_candidates: RefCell::new(Vec::new()),
698 scope_expr_id,
699 is_suggestion,
700 }
701 }
702
703 fn reset(&mut self) {
704 self.inherent_candidates.clear();
705 self.extension_candidates.clear();
706 self.impl_dups.clear();
707 self.private_candidates.clear();
708 self.private_candidate.set(None);
709 self.static_candidates.borrow_mut().clear();
710 }
711
712 fn variance(&self) -> ty::Variance {
716 match self.mode {
717 Mode::MethodCall => ty::Covariant,
718 Mode::Path => ty::Invariant,
719 }
720 }
721
722 fn push_candidate(&mut self, candidate: Candidate<'tcx>, is_inherent: bool) {
726 let is_accessible = if let Some(name) = self.method_name {
727 let item = candidate.item;
728 let hir_id = self.tcx.local_def_id_to_hir_id(self.body_id);
729 let def_scope =
730 self.tcx.adjust_ident_and_get_scope(name, item.container_id(self.tcx), hir_id).1;
731 item.visibility(self.tcx).is_accessible_from(def_scope, self.tcx)
732 } else {
733 true
734 };
735 if is_accessible {
736 if is_inherent {
737 self.inherent_candidates.push(candidate);
738 } else {
739 self.extension_candidates.push(candidate);
740 }
741 } else {
742 self.private_candidates.push(candidate);
743 }
744 }
745
746 fn assemble_inherent_candidates(&mut self) {
747 for step in self.steps.iter() {
748 self.assemble_probe(&step.self_ty, step.autoderefs);
749 }
750 }
751
752 #[instrument(level = "debug", skip(self))]
753 fn assemble_probe(
754 &mut self,
755 self_ty: &Canonical<'tcx, QueryResponse<'tcx, Ty<'tcx>>>,
756 receiver_steps: usize,
757 ) {
758 let raw_self_ty = self_ty.value.value;
759 match *raw_self_ty.kind() {
760 ty::Dynamic(data, ..) if let Some(p) = data.principal() => {
761 let (QueryResponse { value: generalized_self_ty, .. }, _ignored_var_values) =
779 self.fcx.instantiate_canonical(self.span, self_ty);
780
781 self.assemble_inherent_candidates_from_object(generalized_self_ty);
782 self.assemble_inherent_impl_candidates_for_type(p.def_id(), receiver_steps);
783 if self.tcx.has_attr(p.def_id(), sym::rustc_has_incoherent_inherent_impls) {
784 self.assemble_inherent_candidates_for_incoherent_ty(
785 raw_self_ty,
786 receiver_steps,
787 );
788 }
789 }
790 ty::Adt(def, _) => {
791 let def_id = def.did();
792 self.assemble_inherent_impl_candidates_for_type(def_id, receiver_steps);
793 if self.tcx.has_attr(def_id, sym::rustc_has_incoherent_inherent_impls) {
794 self.assemble_inherent_candidates_for_incoherent_ty(
795 raw_self_ty,
796 receiver_steps,
797 );
798 }
799 }
800 ty::Foreign(did) => {
801 self.assemble_inherent_impl_candidates_for_type(did, receiver_steps);
802 if self.tcx.has_attr(did, sym::rustc_has_incoherent_inherent_impls) {
803 self.assemble_inherent_candidates_for_incoherent_ty(
804 raw_self_ty,
805 receiver_steps,
806 );
807 }
808 }
809 ty::Param(p) => {
810 self.assemble_inherent_candidates_from_param(p);
811 }
812 ty::Bool
813 | ty::Char
814 | ty::Int(_)
815 | ty::Uint(_)
816 | ty::Float(_)
817 | ty::Str
818 | ty::Array(..)
819 | ty::Slice(_)
820 | ty::RawPtr(_, _)
821 | ty::Ref(..)
822 | ty::Never
823 | ty::Tuple(..) => {
824 self.assemble_inherent_candidates_for_incoherent_ty(raw_self_ty, receiver_steps)
825 }
826 _ => {}
827 }
828 }
829
830 fn assemble_inherent_candidates_for_incoherent_ty(
831 &mut self,
832 self_ty: Ty<'tcx>,
833 receiver_steps: usize,
834 ) {
835 let Some(simp) = simplify_type(self.tcx, self_ty, TreatParams::InstantiateWithInfer) else {
836 bug!("unexpected incoherent type: {:?}", self_ty)
837 };
838 for &impl_def_id in self.tcx.incoherent_impls(simp).into_iter() {
839 self.assemble_inherent_impl_probe(impl_def_id, receiver_steps);
840 }
841 }
842
843 fn assemble_inherent_impl_candidates_for_type(&mut self, def_id: DefId, receiver_steps: usize) {
844 let impl_def_ids = self.tcx.at(self.span).inherent_impls(def_id).into_iter();
845 for &impl_def_id in impl_def_ids {
846 self.assemble_inherent_impl_probe(impl_def_id, receiver_steps);
847 }
848 }
849
850 #[instrument(level = "debug", skip(self))]
851 fn assemble_inherent_impl_probe(&mut self, impl_def_id: DefId, receiver_steps: usize) {
852 if !self.impl_dups.insert(impl_def_id) {
853 return; }
855
856 for item in self.impl_or_trait_item(impl_def_id) {
857 if !self.has_applicable_self(&item) {
858 self.record_static_candidate(CandidateSource::Impl(impl_def_id));
860 continue;
861 }
862 self.push_candidate(
863 Candidate {
864 item,
865 kind: InherentImplCandidate { impl_def_id, receiver_steps },
866 import_ids: smallvec![],
867 },
868 true,
869 );
870 }
871 }
872
873 #[instrument(level = "debug", skip(self))]
874 fn assemble_inherent_candidates_from_object(&mut self, self_ty: Ty<'tcx>) {
875 let principal = match self_ty.kind() {
876 ty::Dynamic(ref data, ..) => Some(data),
877 _ => None,
878 }
879 .and_then(|data| data.principal())
880 .unwrap_or_else(|| {
881 span_bug!(
882 self.span,
883 "non-object {:?} in assemble_inherent_candidates_from_object",
884 self_ty
885 )
886 });
887
888 let trait_ref = principal.with_self_ty(self.tcx, self_ty);
895 self.assemble_candidates_for_bounds(
896 traits::supertraits(self.tcx, trait_ref),
897 |this, new_trait_ref, item| {
898 this.push_candidate(
899 Candidate {
900 item,
901 kind: ObjectCandidate(new_trait_ref),
902 import_ids: smallvec![],
903 },
904 true,
905 );
906 },
907 );
908 }
909
910 #[instrument(level = "debug", skip(self))]
911 fn assemble_inherent_candidates_from_param(&mut self, param_ty: ty::ParamTy) {
912 let bounds = self.param_env.caller_bounds().iter().filter_map(|predicate| {
913 let bound_predicate = predicate.kind();
914 match bound_predicate.skip_binder() {
915 ty::ClauseKind::Trait(trait_predicate) => {
916 match *trait_predicate.trait_ref.self_ty().kind() {
917 ty::Param(p) if p == param_ty => {
918 Some(bound_predicate.rebind(trait_predicate.trait_ref))
919 }
920 _ => None,
921 }
922 }
923 ty::ClauseKind::RegionOutlives(_)
924 | ty::ClauseKind::TypeOutlives(_)
925 | ty::ClauseKind::Projection(_)
926 | ty::ClauseKind::ConstArgHasType(_, _)
927 | ty::ClauseKind::WellFormed(_)
928 | ty::ClauseKind::ConstEvaluatable(_)
929 | ty::ClauseKind::HostEffect(..) => None,
930 }
931 });
932
933 self.assemble_candidates_for_bounds(bounds, |this, poly_trait_ref, item| {
934 this.push_candidate(
935 Candidate {
936 item,
937 kind: WhereClauseCandidate(poly_trait_ref),
938 import_ids: smallvec![],
939 },
940 true,
941 );
942 });
943 }
944
945 fn assemble_candidates_for_bounds<F>(
948 &mut self,
949 bounds: impl Iterator<Item = ty::PolyTraitRef<'tcx>>,
950 mut mk_cand: F,
951 ) where
952 F: for<'b> FnMut(&mut ProbeContext<'b, 'tcx>, ty::PolyTraitRef<'tcx>, ty::AssocItem),
953 {
954 for bound_trait_ref in bounds {
955 debug!("elaborate_bounds(bound_trait_ref={:?})", bound_trait_ref);
956 for item in self.impl_or_trait_item(bound_trait_ref.def_id()) {
957 if !self.has_applicable_self(&item) {
958 self.record_static_candidate(CandidateSource::Trait(bound_trait_ref.def_id()));
959 } else {
960 mk_cand(self, bound_trait_ref, item);
961 }
962 }
963 }
964 }
965
966 #[instrument(level = "debug", skip(self))]
967 fn assemble_extension_candidates_for_traits_in_scope(&mut self) {
968 let mut duplicates = FxHashSet::default();
969 let opt_applicable_traits = self.tcx.in_scope_traits(self.scope_expr_id);
970 if let Some(applicable_traits) = opt_applicable_traits {
971 for trait_candidate in applicable_traits.iter() {
972 let trait_did = trait_candidate.def_id;
973 if duplicates.insert(trait_did) {
974 self.assemble_extension_candidates_for_trait(
975 &trait_candidate.import_ids,
976 trait_did,
977 );
978 }
979 }
980 }
981 }
982
983 #[instrument(level = "debug", skip(self))]
984 fn assemble_extension_candidates_for_all_traits(&mut self) {
985 let mut duplicates = FxHashSet::default();
986 for trait_info in suggest::all_traits(self.tcx) {
987 if duplicates.insert(trait_info.def_id) {
988 self.assemble_extension_candidates_for_trait(&smallvec![], trait_info.def_id);
989 }
990 }
991 }
992
993 fn matches_return_type(&self, method: ty::AssocItem, expected: Ty<'tcx>) -> bool {
994 match method.kind {
995 ty::AssocKind::Fn => self.probe(|_| {
996 let args = self.fresh_args_for_item(self.span, method.def_id);
997 let fty = self.tcx.fn_sig(method.def_id).instantiate(self.tcx, args);
998 let fty = self.instantiate_binder_with_fresh_vars(self.span, infer::FnCall, fty);
999 self.can_eq(self.param_env, fty.output(), expected)
1000 }),
1001 _ => false,
1002 }
1003 }
1004
1005 #[instrument(level = "debug", skip(self))]
1006 fn assemble_extension_candidates_for_trait(
1007 &mut self,
1008 import_ids: &SmallVec<[LocalDefId; 1]>,
1009 trait_def_id: DefId,
1010 ) {
1011 let trait_args = self.fresh_args_for_item(self.span, trait_def_id);
1012 let trait_ref = ty::TraitRef::new_from_args(self.tcx, trait_def_id, trait_args);
1013
1014 if self.tcx.is_trait_alias(trait_def_id) {
1015 for (bound_trait_pred, _) in
1017 traits::expand_trait_aliases(self.tcx, [(trait_ref.upcast(self.tcx), self.span)]).0
1018 {
1019 assert_eq!(bound_trait_pred.polarity(), ty::PredicatePolarity::Positive);
1020 let bound_trait_ref = bound_trait_pred.map_bound(|pred| pred.trait_ref);
1021 for item in self.impl_or_trait_item(bound_trait_ref.def_id()) {
1022 if !self.has_applicable_self(&item) {
1023 self.record_static_candidate(CandidateSource::Trait(
1024 bound_trait_ref.def_id(),
1025 ));
1026 } else {
1027 self.push_candidate(
1028 Candidate {
1029 item,
1030 import_ids: import_ids.clone(),
1031 kind: TraitCandidate(bound_trait_ref),
1032 },
1033 false,
1034 );
1035 }
1036 }
1037 }
1038 } else {
1039 debug_assert!(self.tcx.is_trait(trait_def_id));
1040 if self.tcx.trait_is_auto(trait_def_id) {
1041 return;
1042 }
1043 for item in self.impl_or_trait_item(trait_def_id) {
1044 if !self.has_applicable_self(&item) {
1046 debug!("method has inapplicable self");
1047 self.record_static_candidate(CandidateSource::Trait(trait_def_id));
1048 continue;
1049 }
1050 self.push_candidate(
1051 Candidate {
1052 item,
1053 import_ids: import_ids.clone(),
1054 kind: TraitCandidate(ty::Binder::dummy(trait_ref)),
1055 },
1056 false,
1057 );
1058 }
1059 }
1060 }
1061
1062 fn candidate_method_names(
1063 &self,
1064 candidate_filter: impl Fn(&ty::AssocItem) -> bool,
1065 ) -> Vec<Ident> {
1066 let mut set = FxHashSet::default();
1067 let mut names: Vec<_> = self
1068 .inherent_candidates
1069 .iter()
1070 .chain(&self.extension_candidates)
1071 .filter(|candidate| candidate_filter(&candidate.item))
1072 .filter(|candidate| {
1073 if let Some(return_ty) = self.return_type {
1074 self.matches_return_type(candidate.item, return_ty)
1075 } else {
1076 true
1077 }
1078 })
1079 .filter(|candidate| {
1081 !matches!(
1084 self.tcx.eval_stability(candidate.item.def_id, None, DUMMY_SP, None),
1085 stability::EvalResult::Deny { .. }
1086 )
1087 })
1088 .map(|candidate| candidate.item.ident(self.tcx))
1089 .filter(|&name| set.insert(name))
1090 .collect();
1091
1092 names.sort_by(|a, b| a.as_str().cmp(b.as_str()));
1094 names
1095 }
1096
1097 #[instrument(level = "debug", skip(self))]
1101 fn pick(mut self) -> PickResult<'tcx> {
1102 assert!(self.method_name.is_some());
1103
1104 let mut unsatisfied_predicates = Vec::new();
1105
1106 if let Some(r) = self.pick_core(&mut unsatisfied_predicates) {
1107 return r;
1108 }
1109
1110 if self.is_suggestion.0 {
1113 return Err(MethodError::NoMatch(NoMatchData {
1114 static_candidates: vec![],
1115 unsatisfied_predicates: vec![],
1116 out_of_scope_traits: vec![],
1117 similar_candidate: None,
1118 mode: self.mode,
1119 }));
1120 }
1121
1122 debug!("pick: actual search failed, assemble diagnostics");
1123
1124 let static_candidates = std::mem::take(self.static_candidates.get_mut());
1125 let private_candidate = self.private_candidate.take();
1126
1127 self.reset();
1129
1130 let span = self.span;
1131 let tcx = self.tcx;
1132
1133 self.assemble_extension_candidates_for_all_traits();
1134
1135 let out_of_scope_traits = match self.pick_core(&mut Vec::new()) {
1136 Some(Ok(p)) => vec![p.item.container_id(self.tcx)],
1137 Some(Err(MethodError::Ambiguity(v))) => v
1138 .into_iter()
1139 .map(|source| match source {
1140 CandidateSource::Trait(id) => id,
1141 CandidateSource::Impl(impl_id) => match tcx.trait_id_of_impl(impl_id) {
1142 Some(id) => id,
1143 None => span_bug!(span, "found inherent method when looking at traits"),
1144 },
1145 })
1146 .collect(),
1147 Some(Err(MethodError::NoMatch(NoMatchData {
1148 out_of_scope_traits: others, ..
1149 }))) => {
1150 assert!(others.is_empty());
1151 vec![]
1152 }
1153 _ => vec![],
1154 };
1155
1156 if let Some((kind, def_id)) = private_candidate {
1157 return Err(MethodError::PrivateMatch(kind, def_id, out_of_scope_traits));
1158 }
1159 let similar_candidate = self.probe_for_similar_candidate()?;
1160
1161 Err(MethodError::NoMatch(NoMatchData {
1162 static_candidates,
1163 unsatisfied_predicates,
1164 out_of_scope_traits,
1165 similar_candidate,
1166 mode: self.mode,
1167 }))
1168 }
1169
1170 fn pick_core(
1171 &self,
1172 unsatisfied_predicates: &mut Vec<(
1173 ty::Predicate<'tcx>,
1174 Option<ty::Predicate<'tcx>>,
1175 Option<ObligationCause<'tcx>>,
1176 )>,
1177 ) -> Option<PickResult<'tcx>> {
1178 self.pick_all_method(&mut PickDiagHints {
1180 unstable_candidates: Some(Vec::new()),
1183 unsatisfied_predicates,
1186 })
1187 .or_else(|| {
1188 self.pick_all_method(&mut PickDiagHints {
1189 unstable_candidates: None,
1194 unsatisfied_predicates: &mut Vec::new(),
1197 })
1198 })
1199 }
1200
1201 fn pick_all_method<'b>(
1202 &self,
1203 pick_diag_hints: &mut PickDiagHints<'b, 'tcx>,
1204 ) -> Option<PickResult<'tcx>> {
1205 let track_unstable_candidates = pick_diag_hints.unstable_candidates.is_some();
1206 self.steps
1207 .iter()
1208 .filter(|step| step.reachable_via_deref)
1212 .filter(|step| {
1213 debug!("pick_all_method: step={:?}", step);
1214 !step.self_ty.references_error() && !step.from_unsafe_deref
1217 })
1218 .find_map(|step| {
1219 let InferOk { value: self_ty, obligations: _ } = self
1220 .fcx
1221 .probe_instantiate_query_response(
1222 self.span,
1223 self.orig_steps_var_values,
1224 &step.self_ty,
1225 )
1226 .unwrap_or_else(|_| {
1227 span_bug!(self.span, "{:?} was applicable but now isn't?", step.self_ty)
1228 });
1229
1230 let by_value_pick = self.pick_by_value_method(step, self_ty, pick_diag_hints);
1231
1232 if let Some(by_value_pick) = by_value_pick {
1234 if let Ok(by_value_pick) = by_value_pick.as_ref() {
1235 if by_value_pick.kind == PickKind::InherentImplPick {
1236 for mutbl in [hir::Mutability::Not, hir::Mutability::Mut] {
1237 if let Err(e) = self.check_for_shadowed_autorefd_method(
1238 by_value_pick,
1239 step,
1240 self_ty,
1241 mutbl,
1242 track_unstable_candidates,
1243 ) {
1244 return Some(Err(e));
1245 }
1246 }
1247 }
1248 }
1249 return Some(by_value_pick);
1250 }
1251
1252 let autoref_pick = self.pick_autorefd_method(
1253 step,
1254 self_ty,
1255 hir::Mutability::Not,
1256 pick_diag_hints,
1257 None,
1258 );
1259 if let Some(autoref_pick) = autoref_pick {
1261 if let Ok(autoref_pick) = autoref_pick.as_ref() {
1262 if autoref_pick.kind == PickKind::InherentImplPick {
1264 if let Err(e) = self.check_for_shadowed_autorefd_method(
1265 autoref_pick,
1266 step,
1267 self_ty,
1268 hir::Mutability::Mut,
1269 track_unstable_candidates,
1270 ) {
1271 return Some(Err(e));
1272 }
1273 }
1274 }
1275 return Some(autoref_pick);
1276 }
1277
1278 self.pick_autorefd_method(
1302 step,
1303 self_ty,
1304 hir::Mutability::Mut,
1305 pick_diag_hints,
1306 None,
1307 )
1308 .or_else(|| self.pick_const_ptr_method(step, self_ty, pick_diag_hints))
1309 .or_else(|| self.pick_reborrow_pin_method(step, self_ty, pick_diag_hints))
1310 })
1311 }
1312
1313 fn check_for_shadowed_autorefd_method(
1329 &self,
1330 possible_shadower: &Pick<'tcx>,
1331 step: &CandidateStep<'tcx>,
1332 self_ty: Ty<'tcx>,
1333 mutbl: hir::Mutability,
1334 track_unstable_candidates: bool,
1335 ) -> Result<(), MethodError<'tcx>> {
1336 if !self.tcx.features().arbitrary_self_types()
1340 && !self.tcx.features().arbitrary_self_types_pointers()
1341 {
1342 return Ok(());
1343 }
1344
1345 let mut pick_diag_hints = PickDiagHints {
1350 unstable_candidates: if track_unstable_candidates { Some(Vec::new()) } else { None },
1351 unsatisfied_predicates: &mut Vec::new(),
1352 };
1353 let pick_constraints = PickConstraintsForShadowed {
1355 autoderefs: possible_shadower.autoderefs,
1357 receiver_steps: possible_shadower.receiver_steps,
1361 def_id: possible_shadower.item.def_id,
1364 };
1365 let potentially_shadowed_pick = self.pick_autorefd_method(
1395 step,
1396 self_ty,
1397 mutbl,
1398 &mut pick_diag_hints,
1399 Some(&pick_constraints),
1400 );
1401 if let Some(Ok(possible_shadowed)) = potentially_shadowed_pick.as_ref() {
1404 let sources = [possible_shadower, possible_shadowed]
1405 .into_iter()
1406 .map(|p| self.candidate_source_from_pick(p))
1407 .collect();
1408 return Err(MethodError::Ambiguity(sources));
1409 }
1410 Ok(())
1411 }
1412
1413 fn pick_by_value_method(
1420 &self,
1421 step: &CandidateStep<'tcx>,
1422 self_ty: Ty<'tcx>,
1423 pick_diag_hints: &mut PickDiagHints<'_, 'tcx>,
1424 ) -> Option<PickResult<'tcx>> {
1425 if step.unsize {
1426 return None;
1427 }
1428
1429 self.pick_method(self_ty, pick_diag_hints, None).map(|r| {
1430 r.map(|mut pick| {
1431 pick.autoderefs = step.autoderefs;
1432
1433 match *step.self_ty.value.value.kind() {
1434 ty::Ref(_, _, mutbl) => {
1436 pick.autoderefs += 1;
1437 pick.autoref_or_ptr_adjustment = Some(AutorefOrPtrAdjustment::Autoref {
1438 mutbl,
1439 unsize: pick.autoref_or_ptr_adjustment.is_some_and(|a| a.get_unsize()),
1440 })
1441 }
1442
1443 ty::Adt(def, args)
1444 if self.tcx.features().pin_ergonomics()
1445 && self.tcx.is_lang_item(def.did(), hir::LangItem::Pin) =>
1446 {
1447 if let ty::Ref(_, _, mutbl) = args[0].expect_ty().kind() {
1449 pick.autoref_or_ptr_adjustment =
1450 Some(AutorefOrPtrAdjustment::ReborrowPin(*mutbl));
1451 }
1452 }
1453
1454 _ => (),
1455 }
1456
1457 pick
1458 })
1459 })
1460 }
1461
1462 fn pick_autorefd_method(
1463 &self,
1464 step: &CandidateStep<'tcx>,
1465 self_ty: Ty<'tcx>,
1466 mutbl: hir::Mutability,
1467 pick_diag_hints: &mut PickDiagHints<'_, 'tcx>,
1468 pick_constraints: Option<&PickConstraintsForShadowed>,
1469 ) -> Option<PickResult<'tcx>> {
1470 let tcx = self.tcx;
1471
1472 if let Some(pick_constraints) = pick_constraints {
1473 if !pick_constraints.may_shadow_based_on_autoderefs(step.autoderefs) {
1474 return None;
1475 }
1476 }
1477
1478 let region = tcx.lifetimes.re_erased;
1480
1481 let autoref_ty = Ty::new_ref(tcx, region, self_ty, mutbl);
1482 self.pick_method(autoref_ty, pick_diag_hints, pick_constraints).map(|r| {
1483 r.map(|mut pick| {
1484 pick.autoderefs = step.autoderefs;
1485 pick.autoref_or_ptr_adjustment =
1486 Some(AutorefOrPtrAdjustment::Autoref { mutbl, unsize: step.unsize });
1487 pick
1488 })
1489 })
1490 }
1491
1492 #[instrument(level = "debug", skip(self, step, pick_diag_hints))]
1494 fn pick_reborrow_pin_method(
1495 &self,
1496 step: &CandidateStep<'tcx>,
1497 self_ty: Ty<'tcx>,
1498 pick_diag_hints: &mut PickDiagHints<'_, 'tcx>,
1499 ) -> Option<PickResult<'tcx>> {
1500 if !self.tcx.features().pin_ergonomics() {
1501 return None;
1502 }
1503
1504 let inner_ty = match self_ty.kind() {
1506 ty::Adt(def, args) if self.tcx.is_lang_item(def.did(), hir::LangItem::Pin) => {
1507 match args[0].expect_ty().kind() {
1508 ty::Ref(_, ty, hir::Mutability::Mut) => *ty,
1509 _ => {
1510 return None;
1511 }
1512 }
1513 }
1514 _ => return None,
1515 };
1516
1517 let region = self.tcx.lifetimes.re_erased;
1518 let autopin_ty = Ty::new_pinned_ref(self.tcx, region, inner_ty, hir::Mutability::Not);
1519 self.pick_method(autopin_ty, pick_diag_hints, None).map(|r| {
1520 r.map(|mut pick| {
1521 pick.autoderefs = step.autoderefs;
1522 pick.autoref_or_ptr_adjustment =
1523 Some(AutorefOrPtrAdjustment::ReborrowPin(hir::Mutability::Not));
1524 pick
1525 })
1526 })
1527 }
1528
1529 fn pick_const_ptr_method(
1533 &self,
1534 step: &CandidateStep<'tcx>,
1535 self_ty: Ty<'tcx>,
1536 pick_diag_hints: &mut PickDiagHints<'_, 'tcx>,
1537 ) -> Option<PickResult<'tcx>> {
1538 if step.unsize {
1540 return None;
1541 }
1542
1543 let &ty::RawPtr(ty, hir::Mutability::Mut) = self_ty.kind() else {
1544 return None;
1545 };
1546
1547 let const_ptr_ty = Ty::new_imm_ptr(self.tcx, ty);
1548 self.pick_method(const_ptr_ty, pick_diag_hints, None).map(|r| {
1549 r.map(|mut pick| {
1550 pick.autoderefs = step.autoderefs;
1551 pick.autoref_or_ptr_adjustment = Some(AutorefOrPtrAdjustment::ToConstPtr);
1552 pick
1553 })
1554 })
1555 }
1556
1557 fn pick_method(
1558 &self,
1559 self_ty: Ty<'tcx>,
1560 pick_diag_hints: &mut PickDiagHints<'_, 'tcx>,
1561 pick_constraints: Option<&PickConstraintsForShadowed>,
1562 ) -> Option<PickResult<'tcx>> {
1563 debug!("pick_method(self_ty={})", self.ty_to_string(self_ty));
1564
1565 for (kind, candidates) in
1566 [("inherent", &self.inherent_candidates), ("extension", &self.extension_candidates)]
1567 {
1568 debug!("searching {} candidates", kind);
1569 let res =
1570 self.consider_candidates(self_ty, candidates, pick_diag_hints, pick_constraints);
1571 if let Some(pick) = res {
1572 return Some(pick);
1573 }
1574 }
1575
1576 if self.private_candidate.get().is_none() {
1577 if let Some(Ok(pick)) = self.consider_candidates(
1578 self_ty,
1579 &self.private_candidates,
1580 &mut PickDiagHints {
1581 unstable_candidates: None,
1582 unsatisfied_predicates: &mut vec![],
1583 },
1584 None,
1585 ) {
1586 self.private_candidate.set(Some((pick.item.kind.as_def_kind(), pick.item.def_id)));
1587 }
1588 }
1589 None
1590 }
1591
1592 fn consider_candidates(
1593 &self,
1594 self_ty: Ty<'tcx>,
1595 candidates: &[Candidate<'tcx>],
1596 pick_diag_hints: &mut PickDiagHints<'_, 'tcx>,
1597 pick_constraints: Option<&PickConstraintsForShadowed>,
1598 ) -> Option<PickResult<'tcx>> {
1599 let mut applicable_candidates: Vec<_> = candidates
1600 .iter()
1601 .filter(|candidate| {
1602 pick_constraints
1603 .map(|pick_constraints| pick_constraints.candidate_may_shadow(&candidate))
1604 .unwrap_or(true)
1605 })
1606 .map(|probe| {
1607 (
1608 probe,
1609 self.consider_probe(
1610 self_ty,
1611 probe,
1612 &mut pick_diag_hints.unsatisfied_predicates,
1613 ),
1614 )
1615 })
1616 .filter(|&(_, status)| status != ProbeResult::NoMatch)
1617 .collect();
1618
1619 debug!("applicable_candidates: {:?}", applicable_candidates);
1620
1621 if applicable_candidates.len() > 1 {
1622 if let Some(pick) =
1623 self.collapse_candidates_to_trait_pick(self_ty, &applicable_candidates)
1624 {
1625 return Some(Ok(pick));
1626 }
1627 }
1628
1629 if let Some(uc) = &mut pick_diag_hints.unstable_candidates {
1630 applicable_candidates.retain(|&(candidate, _)| {
1631 if let stability::EvalResult::Deny { feature, .. } =
1632 self.tcx.eval_stability(candidate.item.def_id, None, self.span, None)
1633 {
1634 uc.push((candidate.clone(), feature));
1635 return false;
1636 }
1637 true
1638 });
1639 }
1640
1641 if applicable_candidates.len() > 1 {
1642 if self.tcx.features().supertrait_item_shadowing() {
1646 if let Some(pick) =
1647 self.collapse_candidates_to_subtrait_pick(self_ty, &applicable_candidates)
1648 {
1649 return Some(Ok(pick));
1650 }
1651 }
1652
1653 let sources = candidates.iter().map(|p| self.candidate_source(p, self_ty)).collect();
1654 return Some(Err(MethodError::Ambiguity(sources)));
1655 }
1656
1657 applicable_candidates.pop().map(|(probe, status)| match status {
1658 ProbeResult::Match => Ok(probe.to_unadjusted_pick(
1659 self_ty,
1660 pick_diag_hints.unstable_candidates.clone().unwrap_or_default(),
1661 )),
1662 ProbeResult::NoMatch | ProbeResult::BadReturnType => Err(MethodError::BadReturnType),
1663 })
1664 }
1665}
1666
1667impl<'tcx> Pick<'tcx> {
1668 pub(crate) fn differs_from(&self, other: &Self) -> bool {
1673 let Self {
1674 item:
1675 AssocItem {
1676 def_id,
1677 name: _,
1678 kind: _,
1679 container: _,
1680 trait_item_def_id: _,
1681 fn_has_self_parameter: _,
1682 opt_rpitit_info: _,
1683 },
1684 kind: _,
1685 import_ids: _,
1686 autoderefs: _,
1687 autoref_or_ptr_adjustment: _,
1688 self_ty,
1689 unstable_candidates: _,
1690 receiver_steps: _,
1691 shadowed_candidates: _,
1692 } = *self;
1693 self_ty != other.self_ty || def_id != other.item.def_id
1694 }
1695
1696 pub(crate) fn maybe_emit_unstable_name_collision_hint(
1698 &self,
1699 tcx: TyCtxt<'tcx>,
1700 span: Span,
1701 scope_expr_id: HirId,
1702 ) {
1703 if self.unstable_candidates.is_empty() {
1704 return;
1705 }
1706 let def_kind = self.item.kind.as_def_kind();
1707 tcx.node_span_lint(lint::builtin::UNSTABLE_NAME_COLLISIONS, scope_expr_id, span, |lint| {
1708 lint.primary_message(format!(
1709 "{} {} with this name may be added to the standard library in the future",
1710 tcx.def_kind_descr_article(def_kind, self.item.def_id),
1711 tcx.def_kind_descr(def_kind, self.item.def_id),
1712 ));
1713
1714 match (self.item.kind, self.item.container) {
1715 (ty::AssocKind::Fn, _) => {
1716 lint.help(format!(
1721 "call with fully qualified syntax `{}(...)` to keep using the current \
1722 method",
1723 tcx.def_path_str(self.item.def_id),
1724 ));
1725 }
1726 (ty::AssocKind::Const, ty::AssocItemContainer::Trait) => {
1727 let def_id = self.item.container_id(tcx);
1728 lint.span_suggestion(
1729 span,
1730 "use the fully qualified path to the associated const",
1731 format!(
1732 "<{} as {}>::{}",
1733 self.self_ty,
1734 tcx.def_path_str(def_id),
1735 self.item.name
1736 ),
1737 Applicability::MachineApplicable,
1738 );
1739 }
1740 _ => {}
1741 }
1742 tcx.disabled_nightly_features(
1743 lint,
1744 Some(scope_expr_id),
1745 self.unstable_candidates.iter().map(|(candidate, feature)| {
1746 (format!(" `{}`", tcx.def_path_str(candidate.item.def_id)), *feature)
1747 }),
1748 );
1749 });
1750 }
1751}
1752
1753impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
1754 fn select_trait_candidate(
1755 &self,
1756 trait_ref: ty::TraitRef<'tcx>,
1757 ) -> traits::SelectionResult<'tcx, traits::Selection<'tcx>> {
1758 let obligation =
1759 traits::Obligation::new(self.tcx, self.misc(self.span), self.param_env, trait_ref);
1760 traits::SelectionContext::new(self).select(&obligation)
1761 }
1762
1763 fn candidate_source(&self, candidate: &Candidate<'tcx>, self_ty: Ty<'tcx>) -> CandidateSource {
1766 match candidate.kind {
1767 InherentImplCandidate { .. } => {
1768 CandidateSource::Impl(candidate.item.container_id(self.tcx))
1769 }
1770 ObjectCandidate(_) | WhereClauseCandidate(_) => {
1771 CandidateSource::Trait(candidate.item.container_id(self.tcx))
1772 }
1773 TraitCandidate(trait_ref) => self.probe(|_| {
1774 let trait_ref =
1775 self.instantiate_binder_with_fresh_vars(self.span, infer::FnCall, trait_ref);
1776 let (xform_self_ty, _) =
1777 self.xform_self_ty(candidate.item, trait_ref.self_ty(), trait_ref.args);
1778 let _ = self.at(&ObligationCause::dummy(), self.param_env).sup(
1781 DefineOpaqueTypes::Yes,
1782 xform_self_ty,
1783 self_ty,
1784 );
1785 match self.select_trait_candidate(trait_ref) {
1786 Ok(Some(traits::ImplSource::UserDefined(ref impl_data))) => {
1787 CandidateSource::Impl(impl_data.impl_def_id)
1790 }
1791 _ => CandidateSource::Trait(candidate.item.container_id(self.tcx)),
1792 }
1793 }),
1794 }
1795 }
1796
1797 fn candidate_source_from_pick(&self, pick: &Pick<'tcx>) -> CandidateSource {
1798 match pick.kind {
1799 InherentImplPick => CandidateSource::Impl(pick.item.container_id(self.tcx)),
1800 ObjectPick | WhereClausePick(_) | TraitPick => {
1801 CandidateSource::Trait(pick.item.container_id(self.tcx))
1802 }
1803 }
1804 }
1805
1806 #[instrument(level = "trace", skip(self, possibly_unsatisfied_predicates), ret)]
1807 fn consider_probe(
1808 &self,
1809 self_ty: Ty<'tcx>,
1810 probe: &Candidate<'tcx>,
1811 possibly_unsatisfied_predicates: &mut Vec<(
1812 ty::Predicate<'tcx>,
1813 Option<ty::Predicate<'tcx>>,
1814 Option<ObligationCause<'tcx>>,
1815 )>,
1816 ) -> ProbeResult {
1817 debug!("consider_probe: self_ty={:?} probe={:?}", self_ty, probe);
1818
1819 self.probe(|snapshot| {
1820 let outer_universe = self.universe();
1821
1822 let mut result = ProbeResult::Match;
1823 let cause = &self.misc(self.span);
1824 let ocx = ObligationCtxt::new_with_diagnostics(self);
1825
1826 let mut trait_predicate = None;
1827 let (mut xform_self_ty, mut xform_ret_ty);
1828
1829 match probe.kind {
1830 InherentImplCandidate { impl_def_id, .. } => {
1831 let impl_args = self.fresh_args_for_item(self.span, impl_def_id);
1832 let impl_ty = self.tcx.type_of(impl_def_id).instantiate(self.tcx, impl_args);
1833 (xform_self_ty, xform_ret_ty) =
1834 self.xform_self_ty(probe.item, impl_ty, impl_args);
1835 xform_self_ty = ocx.normalize(cause, self.param_env, xform_self_ty);
1836 match ocx.relate(cause, self.param_env, self.variance(), self_ty, xform_self_ty)
1837 {
1838 Ok(()) => {}
1839 Err(err) => {
1840 debug!("--> cannot relate self-types {:?}", err);
1841 return ProbeResult::NoMatch;
1842 }
1843 }
1844 xform_ret_ty = ocx.normalize(cause, self.param_env, xform_ret_ty);
1846 let impl_def_id = probe.item.container_id(self.tcx);
1848 let impl_bounds =
1849 self.tcx.predicates_of(impl_def_id).instantiate(self.tcx, impl_args);
1850 let impl_bounds = ocx.normalize(cause, self.param_env, impl_bounds);
1851 ocx.register_obligations(traits::predicates_for_generics(
1853 |idx, span| {
1854 let code = ObligationCauseCode::WhereClauseInExpr(
1855 impl_def_id,
1856 span,
1857 self.scope_expr_id,
1858 idx,
1859 );
1860 self.cause(self.span, code)
1861 },
1862 self.param_env,
1863 impl_bounds,
1864 ));
1865 }
1866 TraitCandidate(poly_trait_ref) => {
1867 if let Some(method_name) = self.method_name {
1870 if self_ty.is_array() && !method_name.span.at_least_rust_2021() {
1871 let trait_def = self.tcx.trait_def(poly_trait_ref.def_id());
1872 if trait_def.skip_array_during_method_dispatch {
1873 return ProbeResult::NoMatch;
1874 }
1875 }
1876
1877 if self_ty.boxed_ty().is_some_and(Ty::is_slice)
1880 && !method_name.span.at_least_rust_2024()
1881 {
1882 let trait_def = self.tcx.trait_def(poly_trait_ref.def_id());
1883 if trait_def.skip_boxed_slice_during_method_dispatch {
1884 return ProbeResult::NoMatch;
1885 }
1886 }
1887 }
1888
1889 let trait_ref = self.instantiate_binder_with_fresh_vars(
1890 self.span,
1891 infer::FnCall,
1892 poly_trait_ref,
1893 );
1894 let trait_ref = ocx.normalize(cause, self.param_env, trait_ref);
1895 (xform_self_ty, xform_ret_ty) =
1896 self.xform_self_ty(probe.item, trait_ref.self_ty(), trait_ref.args);
1897 xform_self_ty = ocx.normalize(cause, self.param_env, xform_self_ty);
1898 match self_ty.kind() {
1899 ty::Alias(ty::Opaque, alias_ty)
1903 if !self.next_trait_solver()
1904 && self.infcx.can_define_opaque_ty(alias_ty.def_id)
1905 && !xform_self_ty.is_ty_var() =>
1906 {
1907 return ProbeResult::NoMatch;
1908 }
1909 _ => match ocx.relate(
1910 cause,
1911 self.param_env,
1912 self.variance(),
1913 self_ty,
1914 xform_self_ty,
1915 ) {
1916 Ok(()) => {}
1917 Err(err) => {
1918 debug!("--> cannot relate self-types {:?}", err);
1919 return ProbeResult::NoMatch;
1920 }
1921 },
1922 }
1923 let obligation = traits::Obligation::new(
1924 self.tcx,
1925 cause.clone(),
1926 self.param_env,
1927 ty::Binder::dummy(trait_ref),
1928 );
1929
1930 if self.infcx.next_trait_solver() || self.infcx.predicate_may_hold(&obligation)
1933 {
1934 ocx.register_obligation(obligation);
1935 } else {
1936 result = ProbeResult::NoMatch;
1937 if let Ok(Some(candidate)) = self.select_trait_candidate(trait_ref) {
1938 for nested_obligation in candidate.nested_obligations() {
1939 if !self.infcx.predicate_may_hold(&nested_obligation) {
1940 possibly_unsatisfied_predicates.push((
1941 self.resolve_vars_if_possible(nested_obligation.predicate),
1942 Some(self.resolve_vars_if_possible(obligation.predicate)),
1943 Some(nested_obligation.cause),
1944 ));
1945 }
1946 }
1947 }
1948 }
1949
1950 trait_predicate = Some(trait_ref.upcast(self.tcx));
1951 }
1952 ObjectCandidate(poly_trait_ref) | WhereClauseCandidate(poly_trait_ref) => {
1953 let trait_ref = self.instantiate_binder_with_fresh_vars(
1954 self.span,
1955 infer::FnCall,
1956 poly_trait_ref,
1957 );
1958 (xform_self_ty, xform_ret_ty) =
1959 self.xform_self_ty(probe.item, trait_ref.self_ty(), trait_ref.args);
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 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 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 } 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 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 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 if let Err(_) = self.leak_check(outer_universe, Some(snapshot)) {
2052 result = ProbeResult::NoMatch;
2053 }
2054
2055 result
2056 })
2057 }
2058
2059 fn collapse_candidates_to_trait_pick(
2077 &self,
2078 self_ty: Ty<'tcx>,
2079 probes: &[(&Candidate<'tcx>, ProbeResult)],
2080 ) -> Option<Pick<'tcx>> {
2081 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 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 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 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 next_round.push(remaining_candidate);
2152 }
2153
2154 if made_progress {
2155 remaining_candidates = next_round;
2157 } else {
2158 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 #[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.into_iter().find(|method| method.name == best_name)
2241 }))
2242 }
2243 })
2244 }
2245
2246 fn has_applicable_self(&self, item: &ty::AssocItem) -> bool {
2249 match self.mode {
2255 Mode::MethodCall => item.fn_has_self_parameter,
2256 Mode::Path => match item.kind {
2257 ty::AssocKind::Type => false,
2258 ty::AssocKind::Fn | ty::AssocKind::Const => true,
2259 },
2260 }
2261 }
2268
2269 fn record_static_candidate(&self, source: CandidateSource) {
2270 self.static_candidates.borrow_mut().push(source);
2271 }
2272
2273 #[instrument(level = "debug", skip(self))]
2274 fn xform_self_ty(
2275 &self,
2276 item: ty::AssocItem,
2277 impl_ty: Ty<'tcx>,
2278 args: GenericArgsRef<'tcx>,
2279 ) -> (Ty<'tcx>, Option<Ty<'tcx>>) {
2280 if item.kind == ty::AssocKind::Fn && self.mode == Mode::MethodCall {
2281 let sig = self.xform_method_sig(item.def_id, args);
2282 (sig.inputs()[0], Some(sig.output()))
2283 } else {
2284 (impl_ty, None)
2285 }
2286 }
2287
2288 #[instrument(level = "debug", skip(self))]
2289 fn xform_method_sig(&self, method: DefId, args: GenericArgsRef<'tcx>) -> ty::FnSig<'tcx> {
2290 let fn_sig = self.tcx.fn_sig(method);
2291 debug!(?fn_sig);
2292
2293 assert!(!args.has_escaping_bound_vars());
2294
2295 let generics = self.tcx.generics_of(method);
2301 assert_eq!(args.len(), generics.parent_count);
2302
2303 let xform_fn_sig = if generics.is_own_empty() {
2304 fn_sig.instantiate(self.tcx, args)
2305 } else {
2306 let args = GenericArgs::for_item(self.tcx, method, |param, _| {
2307 let i = param.index as usize;
2308 if i < args.len() {
2309 args[i]
2310 } else {
2311 match param.kind {
2312 GenericParamDefKind::Lifetime => {
2313 self.tcx.lifetimes.re_erased.into()
2315 }
2316 GenericParamDefKind::Type { .. } | GenericParamDefKind::Const { .. } => {
2317 self.var_for_def(self.span, param)
2318 }
2319 }
2320 }
2321 });
2322 fn_sig.instantiate(self.tcx, args)
2323 };
2324
2325 self.tcx.instantiate_bound_regions_with_erased(xform_fn_sig)
2326 }
2327
2328 fn is_relevant_kind_for_mode(&self, kind: ty::AssocKind) -> bool {
2330 match (self.mode, kind) {
2331 (Mode::MethodCall, ty::AssocKind::Fn) => true,
2332 (Mode::Path, ty::AssocKind::Const | ty::AssocKind::Fn) => true,
2333 _ => false,
2334 }
2335 }
2336
2337 fn matches_by_doc_alias(&self, def_id: DefId) -> bool {
2340 let Some(method) = self.method_name else {
2341 return false;
2342 };
2343 let Some(local_def_id) = def_id.as_local() else {
2344 return false;
2345 };
2346 let hir_id = self.fcx.tcx.local_def_id_to_hir_id(local_def_id);
2347 let attrs = self.fcx.tcx.hir().attrs(hir_id);
2348 for attr in attrs {
2349 if sym::doc == attr.name_or_empty() {
2350 } else if sym::rustc_confusables == attr.name_or_empty() {
2351 let Some(confusables) = attr.meta_item_list() else {
2352 continue;
2353 };
2354 for n in confusables {
2356 if let Some(lit) = n.lit()
2357 && method.name == lit.symbol
2358 {
2359 return true;
2360 }
2361 }
2362 continue;
2363 } else {
2364 continue;
2365 };
2366 let Some(values) = attr.meta_item_list() else {
2367 continue;
2368 };
2369 for v in values {
2370 if v.name_or_empty() != sym::alias {
2371 continue;
2372 }
2373 if let Some(nested) = v.meta_item_list() {
2374 for n in nested {
2376 if let Some(lit) = n.lit()
2377 && method.name == lit.symbol
2378 {
2379 return true;
2380 }
2381 }
2382 } else if let Some(meta) = v.meta_item()
2383 && let Some(lit) = meta.name_value_literal()
2384 && method.name == lit.symbol
2385 {
2386 return true;
2388 }
2389 }
2390 }
2391 false
2392 }
2393
2394 fn impl_or_trait_item(&self, def_id: DefId) -> SmallVec<[ty::AssocItem; 1]> {
2399 if let Some(name) = self.method_name {
2400 if self.allow_similar_names {
2401 let max_dist = max(name.as_str().len(), 3) / 3;
2402 self.tcx
2403 .associated_items(def_id)
2404 .in_definition_order()
2405 .filter(|x| {
2406 if !self.is_relevant_kind_for_mode(x.kind) {
2407 return false;
2408 }
2409 if self.matches_by_doc_alias(x.def_id) {
2410 return true;
2411 }
2412 match edit_distance_with_substrings(
2413 name.as_str(),
2414 x.name.as_str(),
2415 max_dist,
2416 ) {
2417 Some(d) => d > 0,
2418 None => false,
2419 }
2420 })
2421 .copied()
2422 .collect()
2423 } else {
2424 self.fcx
2425 .associated_value(def_id, name)
2426 .filter(|x| self.is_relevant_kind_for_mode(x.kind))
2427 .map_or_else(SmallVec::new, |x| SmallVec::from_buf([x]))
2428 }
2429 } else {
2430 self.tcx
2431 .associated_items(def_id)
2432 .in_definition_order()
2433 .filter(|x| self.is_relevant_kind_for_mode(x.kind))
2434 .copied()
2435 .collect()
2436 }
2437 }
2438}
2439
2440impl<'tcx> Candidate<'tcx> {
2441 fn to_unadjusted_pick(
2442 &self,
2443 self_ty: Ty<'tcx>,
2444 unstable_candidates: Vec<(Candidate<'tcx>, Symbol)>,
2445 ) -> Pick<'tcx> {
2446 Pick {
2447 item: self.item,
2448 kind: match self.kind {
2449 InherentImplCandidate { .. } => InherentImplPick,
2450 ObjectCandidate(_) => ObjectPick,
2451 TraitCandidate(_) => TraitPick,
2452 WhereClauseCandidate(trait_ref) => {
2453 assert!(
2459 !trait_ref.skip_binder().args.has_infer()
2460 && !trait_ref.skip_binder().args.has_placeholders()
2461 );
2462
2463 WhereClausePick(trait_ref)
2464 }
2465 },
2466 import_ids: self.import_ids.clone(),
2467 autoderefs: 0,
2468 autoref_or_ptr_adjustment: None,
2469 self_ty,
2470 unstable_candidates,
2471 receiver_steps: match self.kind {
2472 InherentImplCandidate { receiver_steps, .. } => Some(receiver_steps),
2473 _ => None,
2474 },
2475 shadowed_candidates: vec![],
2476 }
2477 }
2478}