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::query::Providers;
19use rustc_middle::ty::elaborate::supertrait_def_ids;
20use rustc_middle::ty::fast_reject::{DeepRejectCtxt, TreatParams, simplify_type};
21use rustc_middle::ty::{
22 self, AssocItem, AssocItemContainer, GenericArgs, GenericArgsRef, GenericParamDefKind,
23 ParamEnvAnd, Ty, TyCtxt, TypeVisitableExt, Upcast,
24};
25use rustc_middle::{bug, span_bug};
26use rustc_session::lint;
27use rustc_span::def_id::{DefId, LocalDefId};
28use rustc_span::edit_distance::{
29 edit_distance_with_substrings, find_best_match_for_name_with_substrings,
30};
31use rustc_span::{DUMMY_SP, Ident, Span, Symbol, sym};
32use rustc_trait_selection::error_reporting::infer::need_type_info::TypeAnnotationNeeded;
33use rustc_trait_selection::infer::InferCtxtExt as _;
34use rustc_trait_selection::traits::query::CanonicalTyGoal;
35use rustc_trait_selection::traits::query::evaluate_obligation::InferCtxtExt;
36use rustc_trait_selection::traits::query::method_autoderef::{
37 CandidateStep, MethodAutoderefBadTy, MethodAutoderefStepsResult,
38};
39use rustc_trait_selection::traits::{self, ObligationCause, ObligationCtxt};
40use smallvec::{SmallVec, smallvec};
41use tracing::{debug, instrument};
42
43use self::CandidateKind::*;
44pub(crate) use self::PickKind::*;
45use super::{CandidateSource, MethodError, NoMatchData, suggest};
46use crate::FnCtxt;
47
48#[derive(Clone, Copy, Debug)]
51pub(crate) struct IsSuggestion(pub bool);
52
53pub(crate) struct ProbeContext<'a, 'tcx> {
54 fcx: &'a FnCtxt<'a, 'tcx>,
55 span: Span,
56 mode: Mode,
57 method_name: Option<Ident>,
58 return_type: Option<Ty<'tcx>>,
59
60 orig_steps_var_values: &'a OriginalQueryValues<'tcx>,
63 steps: &'tcx [CandidateStep<'tcx>],
64
65 inherent_candidates: Vec<Candidate<'tcx>>,
66 extension_candidates: Vec<Candidate<'tcx>>,
67 impl_dups: FxHashSet<DefId>,
68
69 allow_similar_names: bool,
72
73 private_candidates: Vec<Candidate<'tcx>>,
76
77 private_candidate: Cell<Option<(DefKind, DefId)>>,
79
80 static_candidates: RefCell<Vec<CandidateSource>>,
83
84 scope_expr_id: HirId,
85
86 is_suggestion: IsSuggestion,
90}
91
92impl<'a, 'tcx> Deref for ProbeContext<'a, 'tcx> {
93 type Target = FnCtxt<'a, 'tcx>;
94 fn deref(&self) -> &Self::Target {
95 self.fcx
96 }
97}
98
99#[derive(Debug, Clone)]
100pub(crate) struct Candidate<'tcx> {
101 pub(crate) item: ty::AssocItem,
102 pub(crate) kind: CandidateKind<'tcx>,
103 pub(crate) import_ids: SmallVec<[LocalDefId; 1]>,
104}
105
106#[derive(Debug, Clone)]
107pub(crate) enum CandidateKind<'tcx> {
108 InherentImplCandidate { impl_def_id: DefId, receiver_steps: usize },
109 ObjectCandidate(ty::PolyTraitRef<'tcx>),
110 TraitCandidate(ty::PolyTraitRef<'tcx>),
111 WhereClauseCandidate(ty::PolyTraitRef<'tcx>),
112}
113
114#[derive(Debug, PartialEq, Eq, Copy, Clone)]
115enum ProbeResult {
116 NoMatch,
117 BadReturnType,
118 Match,
119}
120
121#[derive(Debug, PartialEq, Copy, Clone)]
134pub(crate) enum AutorefOrPtrAdjustment {
135 Autoref {
138 mutbl: hir::Mutability,
139
140 unsize: bool,
143 },
144 ToConstPtr,
146
147 ReborrowPin(hir::Mutability),
149}
150
151impl AutorefOrPtrAdjustment {
152 fn get_unsize(&self) -> bool {
153 match self {
154 AutorefOrPtrAdjustment::Autoref { mutbl: _, unsize } => *unsize,
155 AutorefOrPtrAdjustment::ToConstPtr => false,
156 AutorefOrPtrAdjustment::ReborrowPin(_) => false,
157 }
158 }
159}
160
161#[derive(Debug)]
163struct PickDiagHints<'a, 'tcx> {
164 unstable_candidates: Option<Vec<(Candidate<'tcx>, Symbol)>>,
166
167 unsatisfied_predicates: &'a mut Vec<(
170 ty::Predicate<'tcx>,
171 Option<ty::Predicate<'tcx>>,
172 Option<ObligationCause<'tcx>>,
173 )>,
174}
175
176#[derive(Debug)]
180struct PickConstraintsForShadowed {
181 autoderefs: usize,
182 receiver_steps: Option<usize>,
183 def_id: DefId,
184}
185
186impl PickConstraintsForShadowed {
187 fn may_shadow_based_on_autoderefs(&self, autoderefs: usize) -> bool {
188 autoderefs == self.autoderefs
189 }
190
191 fn candidate_may_shadow(&self, candidate: &Candidate<'_>) -> bool {
192 candidate.item.def_id != self.def_id
194 && match candidate.kind {
198 CandidateKind::InherentImplCandidate { receiver_steps, .. } => match self.receiver_steps {
199 Some(shadowed_receiver_steps) => receiver_steps > shadowed_receiver_steps,
200 _ => false
201 },
202 _ => false
203 }
204 }
205}
206
207#[derive(Debug, Clone)]
208pub(crate) struct Pick<'tcx> {
209 pub item: ty::AssocItem,
210 pub kind: PickKind<'tcx>,
211 pub import_ids: SmallVec<[LocalDefId; 1]>,
212
213 pub autoderefs: usize,
218
219 pub autoref_or_ptr_adjustment: Option<AutorefOrPtrAdjustment>,
222 pub self_ty: Ty<'tcx>,
223
224 unstable_candidates: Vec<(Candidate<'tcx>, Symbol)>,
226
227 pub receiver_steps: Option<usize>,
231
232 pub shadowed_candidates: Vec<ty::AssocItem>,
234}
235
236#[derive(Clone, Debug, PartialEq, Eq)]
237pub(crate) enum PickKind<'tcx> {
238 InherentImplPick,
239 ObjectPick,
240 TraitPick,
241 WhereClausePick(
242 ty::PolyTraitRef<'tcx>,
244 ),
245}
246
247pub(crate) type PickResult<'tcx> = Result<Pick<'tcx>, MethodError<'tcx>>;
248
249#[derive(PartialEq, Eq, Copy, Clone, Debug)]
250pub(crate) enum Mode {
251 MethodCall,
255 Path,
259}
260
261#[derive(PartialEq, Eq, Copy, Clone, Debug)]
262pub(crate) enum ProbeScope {
263 Single(DefId),
265
266 TraitsInScope,
268
269 AllTraits,
271}
272
273impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
274 #[instrument(level = "debug", skip(self, candidate_filter))]
281 pub(crate) fn probe_for_return_type_for_diagnostic(
282 &self,
283 span: Span,
284 mode: Mode,
285 return_type: Ty<'tcx>,
286 self_ty: Ty<'tcx>,
287 scope_expr_id: HirId,
288 candidate_filter: impl Fn(&ty::AssocItem) -> bool,
289 ) -> Vec<ty::AssocItem> {
290 let method_names = self
291 .probe_op(
292 span,
293 mode,
294 None,
295 Some(return_type),
296 IsSuggestion(true),
297 self_ty,
298 scope_expr_id,
299 ProbeScope::AllTraits,
300 |probe_cx| Ok(probe_cx.candidate_method_names(candidate_filter)),
301 )
302 .unwrap_or_default();
303 method_names
304 .iter()
305 .flat_map(|&method_name| {
306 self.probe_op(
307 span,
308 mode,
309 Some(method_name),
310 Some(return_type),
311 IsSuggestion(true),
312 self_ty,
313 scope_expr_id,
314 ProbeScope::AllTraits,
315 |probe_cx| probe_cx.pick(),
316 )
317 .ok()
318 .map(|pick| pick.item)
319 })
320 .collect()
321 }
322
323 #[instrument(level = "debug", skip(self))]
324 pub(crate) fn probe_for_name(
325 &self,
326 mode: Mode,
327 item_name: Ident,
328 return_type: Option<Ty<'tcx>>,
329 is_suggestion: IsSuggestion,
330 self_ty: Ty<'tcx>,
331 scope_expr_id: HirId,
332 scope: ProbeScope,
333 ) -> PickResult<'tcx> {
334 self.probe_op(
335 item_name.span,
336 mode,
337 Some(item_name),
338 return_type,
339 is_suggestion,
340 self_ty,
341 scope_expr_id,
342 scope,
343 |probe_cx| probe_cx.pick(),
344 )
345 }
346
347 #[instrument(level = "debug", skip(self))]
348 pub(crate) fn probe_for_name_many(
349 &self,
350 mode: Mode,
351 item_name: Ident,
352 return_type: Option<Ty<'tcx>>,
353 is_suggestion: IsSuggestion,
354 self_ty: Ty<'tcx>,
355 scope_expr_id: HirId,
356 scope: ProbeScope,
357 ) -> Result<Vec<Candidate<'tcx>>, MethodError<'tcx>> {
358 self.probe_op(
359 item_name.span,
360 mode,
361 Some(item_name),
362 return_type,
363 is_suggestion,
364 self_ty,
365 scope_expr_id,
366 scope,
367 |probe_cx| {
368 Ok(probe_cx
369 .inherent_candidates
370 .into_iter()
371 .chain(probe_cx.extension_candidates)
372 .collect())
373 },
374 )
375 }
376
377 pub(crate) fn probe_op<OP, R>(
378 &'a self,
379 span: Span,
380 mode: Mode,
381 method_name: Option<Ident>,
382 return_type: Option<Ty<'tcx>>,
383 is_suggestion: IsSuggestion,
384 self_ty: Ty<'tcx>,
385 scope_expr_id: HirId,
386 scope: ProbeScope,
387 op: OP,
388 ) -> Result<R, MethodError<'tcx>>
389 where
390 OP: FnOnce(ProbeContext<'_, 'tcx>) -> Result<R, MethodError<'tcx>>,
391 {
392 let mut orig_values = OriginalQueryValues::default();
393 let query_input = self.canonicalize_query(
394 ParamEnvAnd { param_env: self.param_env, value: self_ty },
395 &mut orig_values,
396 );
397
398 let steps = match mode {
399 Mode::MethodCall => self.tcx.method_autoderef_steps(query_input),
400 Mode::Path => self.probe(|_| {
401 let infcx = &self.infcx;
407 let (ParamEnvAnd { param_env: _, value: self_ty }, canonical_inference_vars) =
408 infcx.instantiate_canonical(span, &query_input.canonical);
409 debug!(?self_ty, ?query_input, "probe_op: Mode::Path");
410 MethodAutoderefStepsResult {
411 steps: infcx.tcx.arena.alloc_from_iter([CandidateStep {
412 self_ty: self.make_query_response_ignoring_pending_obligations(
413 canonical_inference_vars,
414 self_ty,
415 ),
416 autoderefs: 0,
417 from_unsafe_deref: false,
418 unsize: false,
419 reachable_via_deref: true,
420 }]),
421 opt_bad_ty: None,
422 reached_recursion_limit: false,
423 }
424 }),
425 };
426
427 if steps.reached_recursion_limit && !is_suggestion.0 {
431 self.probe(|_| {
432 let ty = &steps
433 .steps
434 .last()
435 .unwrap_or_else(|| span_bug!(span, "reached the recursion limit in 0 steps?"))
436 .self_ty;
437 let ty = self
438 .probe_instantiate_query_response(span, &orig_values, ty)
439 .unwrap_or_else(|_| span_bug!(span, "instantiating {:?} failed?", ty));
440 autoderef::report_autoderef_recursion_limit_error(self.tcx, span, ty.value);
441 });
442 }
443
444 if let Some(bad_ty) = &steps.opt_bad_ty {
447 if is_suggestion.0 {
448 return Err(MethodError::NoMatch(NoMatchData {
451 static_candidates: Vec::new(),
452 unsatisfied_predicates: Vec::new(),
453 out_of_scope_traits: Vec::new(),
454 similar_candidate: None,
455 mode,
456 }));
457 } else if bad_ty.reached_raw_pointer
458 && !self.tcx.features().arbitrary_self_types_pointers()
459 && !self.tcx.sess.at_least_rust_2018()
460 {
461 self.tcx.node_span_lint(
465 lint::builtin::TYVAR_BEHIND_RAW_POINTER,
466 scope_expr_id,
467 span,
468 |lint| {
469 lint.primary_message("type annotations needed");
470 },
471 );
472 } else {
473 let ty = &bad_ty.ty;
477 let ty = self
478 .probe_instantiate_query_response(span, &orig_values, ty)
479 .unwrap_or_else(|_| span_bug!(span, "instantiating {:?} failed?", ty));
480 let ty = self.resolve_vars_if_possible(ty.value);
481 let guar = match *ty.kind() {
482 ty::Infer(ty::TyVar(_)) => {
483 let raw_ptr_call = bad_ty.reached_raw_pointer
484 && !self.tcx.features().arbitrary_self_types();
485 let mut err = self.err_ctxt().emit_inference_failure_err(
486 self.body_id,
487 span,
488 ty.into(),
489 TypeAnnotationNeeded::E0282,
490 !raw_ptr_call,
491 );
492 if raw_ptr_call {
493 err.span_label(span, "cannot call a method on a raw pointer with an unknown pointee type");
494 }
495 err.emit()
496 }
497 ty::Error(guar) => guar,
498 _ => bug!("unexpected bad final type in method autoderef"),
499 };
500 self.demand_eqtype(span, ty, Ty::new_error(self.tcx, guar));
501 return Err(MethodError::ErrorReported(guar));
502 }
503 }
504
505 debug!("ProbeContext: steps for self_ty={:?} are {:?}", self_ty, steps);
506
507 self.probe(|_| {
510 let mut probe_cx = ProbeContext::new(
511 self,
512 span,
513 mode,
514 method_name,
515 return_type,
516 &orig_values,
517 steps.steps,
518 scope_expr_id,
519 is_suggestion,
520 );
521
522 match scope {
523 ProbeScope::TraitsInScope => {
524 probe_cx.assemble_inherent_candidates();
525 probe_cx.assemble_extension_candidates_for_traits_in_scope();
526 }
527 ProbeScope::AllTraits => {
528 probe_cx.assemble_inherent_candidates();
529 probe_cx.assemble_extension_candidates_for_all_traits();
530 }
531 ProbeScope::Single(def_id) => {
532 let item = self.tcx.associated_item(def_id);
533 assert_eq!(item.container, AssocItemContainer::Trait);
535
536 let trait_def_id = self.tcx.parent(def_id);
537 let trait_span = self.tcx.def_span(trait_def_id);
538
539 let trait_args = self.fresh_args_for_item(trait_span, trait_def_id);
540 let trait_ref = ty::TraitRef::new_from_args(self.tcx, trait_def_id, trait_args);
541
542 probe_cx.push_candidate(
543 Candidate {
544 item,
545 kind: CandidateKind::TraitCandidate(ty::Binder::dummy(trait_ref)),
546 import_ids: smallvec![],
547 },
548 false,
549 );
550 }
551 };
552 op(probe_cx)
553 })
554 }
555}
556
557pub(crate) fn provide(providers: &mut Providers) {
558 providers.method_autoderef_steps = method_autoderef_steps;
559}
560
561fn method_autoderef_steps<'tcx>(
562 tcx: TyCtxt<'tcx>,
563 goal: CanonicalTyGoal<'tcx>,
564) -> MethodAutoderefStepsResult<'tcx> {
565 debug!("method_autoderef_steps({:?})", goal);
566
567 let (ref infcx, goal, inference_vars) = tcx.infer_ctxt().build_with_canonical(DUMMY_SP, &goal);
568 let ParamEnvAnd { param_env, value: self_ty } = goal;
569
570 let mut autoderef_via_deref =
580 Autoderef::new(infcx, param_env, hir::def_id::CRATE_DEF_ID, DUMMY_SP, self_ty)
581 .include_raw_pointers()
582 .silence_errors();
583
584 let mut reached_raw_pointer = false;
585 let arbitrary_self_types_enabled =
586 tcx.features().arbitrary_self_types() || tcx.features().arbitrary_self_types_pointers();
587 let (mut steps, reached_recursion_limit): (Vec<_>, bool) = if arbitrary_self_types_enabled {
588 let reachable_via_deref =
589 autoderef_via_deref.by_ref().map(|_| true).chain(std::iter::repeat(false));
590
591 let mut autoderef_via_receiver =
592 Autoderef::new(infcx, param_env, hir::def_id::CRATE_DEF_ID, DUMMY_SP, self_ty)
593 .include_raw_pointers()
594 .use_receiver_trait()
595 .silence_errors();
596 let steps = autoderef_via_receiver
597 .by_ref()
598 .zip(reachable_via_deref)
599 .map(|((ty, d), reachable_via_deref)| {
600 let step = CandidateStep {
601 self_ty: infcx
602 .make_query_response_ignoring_pending_obligations(inference_vars, ty),
603 autoderefs: d,
604 from_unsafe_deref: reached_raw_pointer,
605 unsize: false,
606 reachable_via_deref,
607 };
608 if ty.is_raw_ptr() {
609 reached_raw_pointer = true;
611 }
612 step
613 })
614 .collect();
615 (steps, autoderef_via_receiver.reached_recursion_limit())
616 } else {
617 let steps = autoderef_via_deref
618 .by_ref()
619 .map(|(ty, d)| {
620 let step = CandidateStep {
621 self_ty: infcx
622 .make_query_response_ignoring_pending_obligations(inference_vars, ty),
623 autoderefs: d,
624 from_unsafe_deref: reached_raw_pointer,
625 unsize: false,
626 reachable_via_deref: true,
627 };
628 if ty.is_raw_ptr() {
629 reached_raw_pointer = true;
631 }
632 step
633 })
634 .collect();
635 (steps, autoderef_via_deref.reached_recursion_limit())
636 };
637 let final_ty = autoderef_via_deref.final_ty(true);
638 let opt_bad_ty = match final_ty.kind() {
639 ty::Infer(ty::TyVar(_)) | ty::Error(_) => Some(MethodAutoderefBadTy {
640 reached_raw_pointer,
641 ty: infcx.make_query_response_ignoring_pending_obligations(inference_vars, final_ty),
642 }),
643 ty::Array(elem_ty, _) => {
644 let autoderefs = steps.iter().filter(|s| s.reachable_via_deref).count() - 1;
645 steps.push(CandidateStep {
646 self_ty: infcx.make_query_response_ignoring_pending_obligations(
647 inference_vars,
648 Ty::new_slice(infcx.tcx, *elem_ty),
649 ),
650 autoderefs,
651 from_unsafe_deref: reached_raw_pointer,
654 unsize: true,
655 reachable_via_deref: true, });
658
659 None
660 }
661 _ => None,
662 };
663
664 debug!("method_autoderef_steps: steps={:?} opt_bad_ty={:?}", steps, opt_bad_ty);
665
666 MethodAutoderefStepsResult {
667 steps: tcx.arena.alloc_from_iter(steps),
668 opt_bad_ty: opt_bad_ty.map(|ty| &*tcx.arena.alloc(ty)),
669 reached_recursion_limit,
670 }
671}
672
673impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
674 fn new(
675 fcx: &'a FnCtxt<'a, 'tcx>,
676 span: Span,
677 mode: Mode,
678 method_name: Option<Ident>,
679 return_type: Option<Ty<'tcx>>,
680 orig_steps_var_values: &'a OriginalQueryValues<'tcx>,
681 steps: &'tcx [CandidateStep<'tcx>],
682 scope_expr_id: HirId,
683 is_suggestion: IsSuggestion,
684 ) -> ProbeContext<'a, 'tcx> {
685 ProbeContext {
686 fcx,
687 span,
688 mode,
689 method_name,
690 return_type,
691 inherent_candidates: Vec::new(),
692 extension_candidates: Vec::new(),
693 impl_dups: FxHashSet::default(),
694 orig_steps_var_values,
695 steps,
696 allow_similar_names: false,
697 private_candidates: Vec::new(),
698 private_candidate: Cell::new(None),
699 static_candidates: RefCell::new(Vec::new()),
700 scope_expr_id,
701 is_suggestion,
702 }
703 }
704
705 fn reset(&mut self) {
706 self.inherent_candidates.clear();
707 self.extension_candidates.clear();
708 self.impl_dups.clear();
709 self.private_candidates.clear();
710 self.private_candidate.set(None);
711 self.static_candidates.borrow_mut().clear();
712 }
713
714 fn variance(&self) -> ty::Variance {
718 match self.mode {
719 Mode::MethodCall => ty::Covariant,
720 Mode::Path => ty::Invariant,
721 }
722 }
723
724 fn push_candidate(&mut self, candidate: Candidate<'tcx>, is_inherent: bool) {
728 let is_accessible = if let Some(name) = self.method_name {
729 let item = candidate.item;
730 let hir_id = self.tcx.local_def_id_to_hir_id(self.body_id);
731 let def_scope =
732 self.tcx.adjust_ident_and_get_scope(name, item.container_id(self.tcx), hir_id).1;
733 item.visibility(self.tcx).is_accessible_from(def_scope, self.tcx)
734 } else {
735 true
736 };
737 if is_accessible {
738 if is_inherent {
739 self.inherent_candidates.push(candidate);
740 } else {
741 self.extension_candidates.push(candidate);
742 }
743 } else {
744 self.private_candidates.push(candidate);
745 }
746 }
747
748 fn assemble_inherent_candidates(&mut self) {
749 for step in self.steps.iter() {
750 self.assemble_probe(&step.self_ty, step.autoderefs);
751 }
752 }
753
754 #[instrument(level = "debug", skip(self))]
755 fn assemble_probe(
756 &mut self,
757 self_ty: &Canonical<'tcx, QueryResponse<'tcx, Ty<'tcx>>>,
758 receiver_steps: usize,
759 ) {
760 let raw_self_ty = self_ty.value.value;
761 match *raw_self_ty.kind() {
762 ty::Dynamic(data, ..) if let Some(p) = data.principal() => {
763 let (QueryResponse { value: generalized_self_ty, .. }, _ignored_var_values) =
781 self.fcx.instantiate_canonical(self.span, self_ty);
782
783 self.assemble_inherent_candidates_from_object(generalized_self_ty);
784 self.assemble_inherent_impl_candidates_for_type(p.def_id(), receiver_steps);
785 if self.tcx.has_attr(p.def_id(), sym::rustc_has_incoherent_inherent_impls) {
786 self.assemble_inherent_candidates_for_incoherent_ty(
787 raw_self_ty,
788 receiver_steps,
789 );
790 }
791 }
792 ty::Adt(def, _) => {
793 let def_id = def.did();
794 self.assemble_inherent_impl_candidates_for_type(def_id, receiver_steps);
795 if self.tcx.has_attr(def_id, sym::rustc_has_incoherent_inherent_impls) {
796 self.assemble_inherent_candidates_for_incoherent_ty(
797 raw_self_ty,
798 receiver_steps,
799 );
800 }
801 }
802 ty::Foreign(did) => {
803 self.assemble_inherent_impl_candidates_for_type(did, receiver_steps);
804 if self.tcx.has_attr(did, sym::rustc_has_incoherent_inherent_impls) {
805 self.assemble_inherent_candidates_for_incoherent_ty(
806 raw_self_ty,
807 receiver_steps,
808 );
809 }
810 }
811 ty::Param(_) => {
812 self.assemble_inherent_candidates_from_param(raw_self_ty);
813 }
814 ty::Bool
815 | ty::Char
816 | ty::Int(_)
817 | ty::Uint(_)
818 | ty::Float(_)
819 | ty::Str
820 | ty::Array(..)
821 | ty::Slice(_)
822 | ty::RawPtr(_, _)
823 | ty::Ref(..)
824 | ty::Never
825 | ty::Tuple(..) => {
826 self.assemble_inherent_candidates_for_incoherent_ty(raw_self_ty, receiver_steps)
827 }
828 _ => {}
829 }
830 }
831
832 fn assemble_inherent_candidates_for_incoherent_ty(
833 &mut self,
834 self_ty: Ty<'tcx>,
835 receiver_steps: usize,
836 ) {
837 let Some(simp) = simplify_type(self.tcx, self_ty, TreatParams::InstantiateWithInfer) else {
838 bug!("unexpected incoherent type: {:?}", self_ty)
839 };
840 for &impl_def_id in self.tcx.incoherent_impls(simp).into_iter() {
841 self.assemble_inherent_impl_probe(impl_def_id, receiver_steps);
842 }
843 }
844
845 fn assemble_inherent_impl_candidates_for_type(&mut self, def_id: DefId, receiver_steps: usize) {
846 let impl_def_ids = self.tcx.at(self.span).inherent_impls(def_id).into_iter();
847 for &impl_def_id in impl_def_ids {
848 self.assemble_inherent_impl_probe(impl_def_id, receiver_steps);
849 }
850 }
851
852 #[instrument(level = "debug", skip(self))]
853 fn assemble_inherent_impl_probe(&mut self, impl_def_id: DefId, receiver_steps: usize) {
854 if !self.impl_dups.insert(impl_def_id) {
855 return; }
857
858 for item in self.impl_or_trait_item(impl_def_id) {
859 if !self.has_applicable_self(&item) {
860 self.record_static_candidate(CandidateSource::Impl(impl_def_id));
862 continue;
863 }
864 self.push_candidate(
865 Candidate {
866 item,
867 kind: InherentImplCandidate { impl_def_id, receiver_steps },
868 import_ids: smallvec![],
869 },
870 true,
871 );
872 }
873 }
874
875 #[instrument(level = "debug", skip(self))]
876 fn assemble_inherent_candidates_from_object(&mut self, self_ty: Ty<'tcx>) {
877 let principal = match self_ty.kind() {
878 ty::Dynamic(data, ..) => Some(data),
879 _ => None,
880 }
881 .and_then(|data| data.principal())
882 .unwrap_or_else(|| {
883 span_bug!(
884 self.span,
885 "non-object {:?} in assemble_inherent_candidates_from_object",
886 self_ty
887 )
888 });
889
890 let trait_ref = principal.with_self_ty(self.tcx, self_ty);
897 self.assemble_candidates_for_bounds(
898 traits::supertraits(self.tcx, trait_ref),
899 |this, new_trait_ref, item| {
900 this.push_candidate(
901 Candidate {
902 item,
903 kind: ObjectCandidate(new_trait_ref),
904 import_ids: smallvec![],
905 },
906 true,
907 );
908 },
909 );
910 }
911
912 #[instrument(level = "debug", skip(self))]
913 fn assemble_inherent_candidates_from_param(&mut self, param_ty: Ty<'tcx>) {
914 debug_assert_matches!(param_ty.kind(), ty::Param(_));
915
916 let tcx = self.tcx;
917 let bounds = self.param_env.caller_bounds().iter().filter_map(|predicate| {
918 let bound_predicate = predicate.kind();
919 match bound_predicate.skip_binder() {
920 ty::ClauseKind::Trait(trait_predicate) => DeepRejectCtxt::relate_rigid_rigid(tcx)
921 .types_may_unify(param_ty, trait_predicate.trait_ref.self_ty())
922 .then(|| bound_predicate.rebind(trait_predicate.trait_ref)),
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(
999 self.span,
1000 BoundRegionConversionTime::FnCall,
1001 fty,
1002 );
1003 self.can_eq(self.param_env, fty.output(), expected)
1004 }),
1005 _ => false,
1006 }
1007 }
1008
1009 #[instrument(level = "debug", skip(self))]
1010 fn assemble_extension_candidates_for_trait(
1011 &mut self,
1012 import_ids: &SmallVec<[LocalDefId; 1]>,
1013 trait_def_id: DefId,
1014 ) {
1015 let trait_args = self.fresh_args_for_item(self.span, trait_def_id);
1016 let trait_ref = ty::TraitRef::new_from_args(self.tcx, trait_def_id, trait_args);
1017
1018 if self.tcx.is_trait_alias(trait_def_id) {
1019 for (bound_trait_pred, _) in
1021 traits::expand_trait_aliases(self.tcx, [(trait_ref.upcast(self.tcx), self.span)]).0
1022 {
1023 assert_eq!(bound_trait_pred.polarity(), ty::PredicatePolarity::Positive);
1024 let bound_trait_ref = bound_trait_pred.map_bound(|pred| pred.trait_ref);
1025 for item in self.impl_or_trait_item(bound_trait_ref.def_id()) {
1026 if !self.has_applicable_self(&item) {
1027 self.record_static_candidate(CandidateSource::Trait(
1028 bound_trait_ref.def_id(),
1029 ));
1030 } else {
1031 self.push_candidate(
1032 Candidate {
1033 item,
1034 import_ids: import_ids.clone(),
1035 kind: TraitCandidate(bound_trait_ref),
1036 },
1037 false,
1038 );
1039 }
1040 }
1041 }
1042 } else {
1043 debug_assert!(self.tcx.is_trait(trait_def_id));
1044 if self.tcx.trait_is_auto(trait_def_id) {
1045 return;
1046 }
1047 for item in self.impl_or_trait_item(trait_def_id) {
1048 if !self.has_applicable_self(&item) {
1050 debug!("method has inapplicable self");
1051 self.record_static_candidate(CandidateSource::Trait(trait_def_id));
1052 continue;
1053 }
1054 self.push_candidate(
1055 Candidate {
1056 item,
1057 import_ids: import_ids.clone(),
1058 kind: TraitCandidate(ty::Binder::dummy(trait_ref)),
1059 },
1060 false,
1061 );
1062 }
1063 }
1064 }
1065
1066 fn candidate_method_names(
1067 &self,
1068 candidate_filter: impl Fn(&ty::AssocItem) -> bool,
1069 ) -> Vec<Ident> {
1070 let mut set = FxHashSet::default();
1071 let mut names: Vec<_> = self
1072 .inherent_candidates
1073 .iter()
1074 .chain(&self.extension_candidates)
1075 .filter(|candidate| candidate_filter(&candidate.item))
1076 .filter(|candidate| {
1077 if let Some(return_ty) = self.return_type {
1078 self.matches_return_type(candidate.item, return_ty)
1079 } else {
1080 true
1081 }
1082 })
1083 .filter(|candidate| {
1085 !matches!(
1088 self.tcx.eval_stability(candidate.item.def_id, None, DUMMY_SP, None),
1089 stability::EvalResult::Deny { .. }
1090 )
1091 })
1092 .map(|candidate| candidate.item.ident(self.tcx))
1093 .filter(|&name| set.insert(name))
1094 .collect();
1095
1096 names.sort_by(|a, b| a.as_str().cmp(b.as_str()));
1098 names
1099 }
1100
1101 #[instrument(level = "debug", skip(self))]
1105 fn pick(mut self) -> PickResult<'tcx> {
1106 assert!(self.method_name.is_some());
1107
1108 let mut unsatisfied_predicates = Vec::new();
1109
1110 if let Some(r) = self.pick_core(&mut unsatisfied_predicates) {
1111 return r;
1112 }
1113
1114 if self.is_suggestion.0 {
1117 return Err(MethodError::NoMatch(NoMatchData {
1118 static_candidates: vec![],
1119 unsatisfied_predicates: vec![],
1120 out_of_scope_traits: vec![],
1121 similar_candidate: None,
1122 mode: self.mode,
1123 }));
1124 }
1125
1126 debug!("pick: actual search failed, assemble diagnostics");
1127
1128 let static_candidates = std::mem::take(self.static_candidates.get_mut());
1129 let private_candidate = self.private_candidate.take();
1130
1131 self.reset();
1133
1134 let span = self.span;
1135 let tcx = self.tcx;
1136
1137 self.assemble_extension_candidates_for_all_traits();
1138
1139 let out_of_scope_traits = match self.pick_core(&mut Vec::new()) {
1140 Some(Ok(p)) => vec![p.item.container_id(self.tcx)],
1141 Some(Err(MethodError::Ambiguity(v))) => v
1142 .into_iter()
1143 .map(|source| match source {
1144 CandidateSource::Trait(id) => id,
1145 CandidateSource::Impl(impl_id) => match tcx.trait_id_of_impl(impl_id) {
1146 Some(id) => id,
1147 None => span_bug!(span, "found inherent method when looking at traits"),
1148 },
1149 })
1150 .collect(),
1151 Some(Err(MethodError::NoMatch(NoMatchData {
1152 out_of_scope_traits: others, ..
1153 }))) => {
1154 assert!(others.is_empty());
1155 vec![]
1156 }
1157 _ => vec![],
1158 };
1159
1160 if let Some((kind, def_id)) = private_candidate {
1161 return Err(MethodError::PrivateMatch(kind, def_id, out_of_scope_traits));
1162 }
1163 let similar_candidate = self.probe_for_similar_candidate()?;
1164
1165 Err(MethodError::NoMatch(NoMatchData {
1166 static_candidates,
1167 unsatisfied_predicates,
1168 out_of_scope_traits,
1169 similar_candidate,
1170 mode: self.mode,
1171 }))
1172 }
1173
1174 fn pick_core(
1175 &self,
1176 unsatisfied_predicates: &mut Vec<(
1177 ty::Predicate<'tcx>,
1178 Option<ty::Predicate<'tcx>>,
1179 Option<ObligationCause<'tcx>>,
1180 )>,
1181 ) -> Option<PickResult<'tcx>> {
1182 self.pick_all_method(&mut PickDiagHints {
1184 unstable_candidates: Some(Vec::new()),
1187 unsatisfied_predicates,
1190 })
1191 .or_else(|| {
1192 self.pick_all_method(&mut PickDiagHints {
1193 unstable_candidates: None,
1198 unsatisfied_predicates: &mut Vec::new(),
1201 })
1202 })
1203 }
1204
1205 fn pick_all_method<'b>(
1206 &self,
1207 pick_diag_hints: &mut PickDiagHints<'b, 'tcx>,
1208 ) -> Option<PickResult<'tcx>> {
1209 let track_unstable_candidates = pick_diag_hints.unstable_candidates.is_some();
1210 self.steps
1211 .iter()
1212 .filter(|step| step.reachable_via_deref)
1216 .filter(|step| {
1217 debug!("pick_all_method: step={:?}", step);
1218 !step.self_ty.value.references_error() && !step.from_unsafe_deref
1221 })
1222 .find_map(|step| {
1223 let InferOk { value: self_ty, obligations: _ } = self
1224 .fcx
1225 .probe_instantiate_query_response(
1226 self.span,
1227 self.orig_steps_var_values,
1228 &step.self_ty,
1229 )
1230 .unwrap_or_else(|_| {
1231 span_bug!(self.span, "{:?} was applicable but now isn't?", step.self_ty)
1232 });
1233
1234 let by_value_pick = self.pick_by_value_method(step, self_ty, pick_diag_hints);
1235
1236 if let Some(by_value_pick) = by_value_pick {
1238 if let Ok(by_value_pick) = by_value_pick.as_ref() {
1239 if by_value_pick.kind == PickKind::InherentImplPick {
1240 for mutbl in [hir::Mutability::Not, hir::Mutability::Mut] {
1241 if let Err(e) = self.check_for_shadowed_autorefd_method(
1242 by_value_pick,
1243 step,
1244 self_ty,
1245 mutbl,
1246 track_unstable_candidates,
1247 ) {
1248 return Some(Err(e));
1249 }
1250 }
1251 }
1252 }
1253 return Some(by_value_pick);
1254 }
1255
1256 let autoref_pick = self.pick_autorefd_method(
1257 step,
1258 self_ty,
1259 hir::Mutability::Not,
1260 pick_diag_hints,
1261 None,
1262 );
1263 if let Some(autoref_pick) = autoref_pick {
1265 if let Ok(autoref_pick) = autoref_pick.as_ref() {
1266 if autoref_pick.kind == PickKind::InherentImplPick {
1268 if let Err(e) = self.check_for_shadowed_autorefd_method(
1269 autoref_pick,
1270 step,
1271 self_ty,
1272 hir::Mutability::Mut,
1273 track_unstable_candidates,
1274 ) {
1275 return Some(Err(e));
1276 }
1277 }
1278 }
1279 return Some(autoref_pick);
1280 }
1281
1282 self.pick_autorefd_method(
1306 step,
1307 self_ty,
1308 hir::Mutability::Mut,
1309 pick_diag_hints,
1310 None,
1311 )
1312 .or_else(|| self.pick_const_ptr_method(step, self_ty, pick_diag_hints))
1313 .or_else(|| self.pick_reborrow_pin_method(step, self_ty, pick_diag_hints))
1314 })
1315 }
1316
1317 fn check_for_shadowed_autorefd_method(
1333 &self,
1334 possible_shadower: &Pick<'tcx>,
1335 step: &CandidateStep<'tcx>,
1336 self_ty: Ty<'tcx>,
1337 mutbl: hir::Mutability,
1338 track_unstable_candidates: bool,
1339 ) -> Result<(), MethodError<'tcx>> {
1340 if !self.tcx.features().arbitrary_self_types()
1344 && !self.tcx.features().arbitrary_self_types_pointers()
1345 {
1346 return Ok(());
1347 }
1348
1349 let mut pick_diag_hints = PickDiagHints {
1354 unstable_candidates: if track_unstable_candidates { Some(Vec::new()) } else { None },
1355 unsatisfied_predicates: &mut Vec::new(),
1356 };
1357 let pick_constraints = PickConstraintsForShadowed {
1359 autoderefs: possible_shadower.autoderefs,
1361 receiver_steps: possible_shadower.receiver_steps,
1365 def_id: possible_shadower.item.def_id,
1368 };
1369 let potentially_shadowed_pick = self.pick_autorefd_method(
1399 step,
1400 self_ty,
1401 mutbl,
1402 &mut pick_diag_hints,
1403 Some(&pick_constraints),
1404 );
1405 if let Some(Ok(possible_shadowed)) = potentially_shadowed_pick.as_ref() {
1408 let sources = [possible_shadower, possible_shadowed]
1409 .into_iter()
1410 .map(|p| self.candidate_source_from_pick(p))
1411 .collect();
1412 return Err(MethodError::Ambiguity(sources));
1413 }
1414 Ok(())
1415 }
1416
1417 fn pick_by_value_method(
1424 &self,
1425 step: &CandidateStep<'tcx>,
1426 self_ty: Ty<'tcx>,
1427 pick_diag_hints: &mut PickDiagHints<'_, 'tcx>,
1428 ) -> Option<PickResult<'tcx>> {
1429 if step.unsize {
1430 return None;
1431 }
1432
1433 self.pick_method(self_ty, pick_diag_hints, None).map(|r| {
1434 r.map(|mut pick| {
1435 pick.autoderefs = step.autoderefs;
1436
1437 match *step.self_ty.value.value.kind() {
1438 ty::Ref(_, _, mutbl) => {
1440 pick.autoderefs += 1;
1441 pick.autoref_or_ptr_adjustment = Some(AutorefOrPtrAdjustment::Autoref {
1442 mutbl,
1443 unsize: pick.autoref_or_ptr_adjustment.is_some_and(|a| a.get_unsize()),
1444 })
1445 }
1446
1447 ty::Adt(def, args)
1448 if self.tcx.features().pin_ergonomics()
1449 && self.tcx.is_lang_item(def.did(), hir::LangItem::Pin) =>
1450 {
1451 if let ty::Ref(_, _, mutbl) = args[0].expect_ty().kind() {
1453 pick.autoref_or_ptr_adjustment =
1454 Some(AutorefOrPtrAdjustment::ReborrowPin(*mutbl));
1455 }
1456 }
1457
1458 _ => (),
1459 }
1460
1461 pick
1462 })
1463 })
1464 }
1465
1466 fn pick_autorefd_method(
1467 &self,
1468 step: &CandidateStep<'tcx>,
1469 self_ty: Ty<'tcx>,
1470 mutbl: hir::Mutability,
1471 pick_diag_hints: &mut PickDiagHints<'_, 'tcx>,
1472 pick_constraints: Option<&PickConstraintsForShadowed>,
1473 ) -> Option<PickResult<'tcx>> {
1474 let tcx = self.tcx;
1475
1476 if let Some(pick_constraints) = pick_constraints {
1477 if !pick_constraints.may_shadow_based_on_autoderefs(step.autoderefs) {
1478 return None;
1479 }
1480 }
1481
1482 let region = tcx.lifetimes.re_erased;
1484
1485 let autoref_ty = Ty::new_ref(tcx, region, self_ty, mutbl);
1486 self.pick_method(autoref_ty, pick_diag_hints, pick_constraints).map(|r| {
1487 r.map(|mut pick| {
1488 pick.autoderefs = step.autoderefs;
1489 pick.autoref_or_ptr_adjustment =
1490 Some(AutorefOrPtrAdjustment::Autoref { mutbl, unsize: step.unsize });
1491 pick
1492 })
1493 })
1494 }
1495
1496 #[instrument(level = "debug", skip(self, step, pick_diag_hints))]
1498 fn pick_reborrow_pin_method(
1499 &self,
1500 step: &CandidateStep<'tcx>,
1501 self_ty: Ty<'tcx>,
1502 pick_diag_hints: &mut PickDiagHints<'_, 'tcx>,
1503 ) -> Option<PickResult<'tcx>> {
1504 if !self.tcx.features().pin_ergonomics() {
1505 return None;
1506 }
1507
1508 let inner_ty = match self_ty.kind() {
1510 ty::Adt(def, args) if self.tcx.is_lang_item(def.did(), hir::LangItem::Pin) => {
1511 match args[0].expect_ty().kind() {
1512 ty::Ref(_, ty, hir::Mutability::Mut) => *ty,
1513 _ => {
1514 return None;
1515 }
1516 }
1517 }
1518 _ => return None,
1519 };
1520
1521 let region = self.tcx.lifetimes.re_erased;
1522 let autopin_ty = Ty::new_pinned_ref(self.tcx, region, inner_ty, hir::Mutability::Not);
1523 self.pick_method(autopin_ty, pick_diag_hints, None).map(|r| {
1524 r.map(|mut pick| {
1525 pick.autoderefs = step.autoderefs;
1526 pick.autoref_or_ptr_adjustment =
1527 Some(AutorefOrPtrAdjustment::ReborrowPin(hir::Mutability::Not));
1528 pick
1529 })
1530 })
1531 }
1532
1533 fn pick_const_ptr_method(
1537 &self,
1538 step: &CandidateStep<'tcx>,
1539 self_ty: Ty<'tcx>,
1540 pick_diag_hints: &mut PickDiagHints<'_, 'tcx>,
1541 ) -> Option<PickResult<'tcx>> {
1542 if step.unsize {
1544 return None;
1545 }
1546
1547 let &ty::RawPtr(ty, hir::Mutability::Mut) = self_ty.kind() else {
1548 return None;
1549 };
1550
1551 let const_ptr_ty = Ty::new_imm_ptr(self.tcx, ty);
1552 self.pick_method(const_ptr_ty, pick_diag_hints, None).map(|r| {
1553 r.map(|mut pick| {
1554 pick.autoderefs = step.autoderefs;
1555 pick.autoref_or_ptr_adjustment = Some(AutorefOrPtrAdjustment::ToConstPtr);
1556 pick
1557 })
1558 })
1559 }
1560
1561 fn pick_method(
1562 &self,
1563 self_ty: Ty<'tcx>,
1564 pick_diag_hints: &mut PickDiagHints<'_, 'tcx>,
1565 pick_constraints: Option<&PickConstraintsForShadowed>,
1566 ) -> Option<PickResult<'tcx>> {
1567 debug!("pick_method(self_ty={})", self.ty_to_string(self_ty));
1568
1569 for (kind, candidates) in
1570 [("inherent", &self.inherent_candidates), ("extension", &self.extension_candidates)]
1571 {
1572 debug!("searching {} candidates", kind);
1573 let res =
1574 self.consider_candidates(self_ty, candidates, pick_diag_hints, pick_constraints);
1575 if let Some(pick) = res {
1576 return Some(pick);
1577 }
1578 }
1579
1580 if self.private_candidate.get().is_none() {
1581 if let Some(Ok(pick)) = self.consider_candidates(
1582 self_ty,
1583 &self.private_candidates,
1584 &mut PickDiagHints {
1585 unstable_candidates: None,
1586 unsatisfied_predicates: &mut vec![],
1587 },
1588 None,
1589 ) {
1590 self.private_candidate.set(Some((pick.item.as_def_kind(), pick.item.def_id)));
1591 }
1592 }
1593 None
1594 }
1595
1596 fn consider_candidates(
1597 &self,
1598 self_ty: Ty<'tcx>,
1599 candidates: &[Candidate<'tcx>],
1600 pick_diag_hints: &mut PickDiagHints<'_, 'tcx>,
1601 pick_constraints: Option<&PickConstraintsForShadowed>,
1602 ) -> Option<PickResult<'tcx>> {
1603 let mut applicable_candidates: Vec<_> = candidates
1604 .iter()
1605 .filter(|candidate| {
1606 pick_constraints
1607 .map(|pick_constraints| pick_constraints.candidate_may_shadow(&candidate))
1608 .unwrap_or(true)
1609 })
1610 .map(|probe| {
1611 (
1612 probe,
1613 self.consider_probe(
1614 self_ty,
1615 probe,
1616 &mut pick_diag_hints.unsatisfied_predicates,
1617 ),
1618 )
1619 })
1620 .filter(|&(_, status)| status != ProbeResult::NoMatch)
1621 .collect();
1622
1623 debug!("applicable_candidates: {:?}", applicable_candidates);
1624
1625 if applicable_candidates.len() > 1 {
1626 if let Some(pick) =
1627 self.collapse_candidates_to_trait_pick(self_ty, &applicable_candidates)
1628 {
1629 return Some(Ok(pick));
1630 }
1631 }
1632
1633 if let Some(uc) = &mut pick_diag_hints.unstable_candidates {
1634 applicable_candidates.retain(|&(candidate, _)| {
1635 if let stability::EvalResult::Deny { feature, .. } =
1636 self.tcx.eval_stability(candidate.item.def_id, None, self.span, None)
1637 {
1638 uc.push((candidate.clone(), feature));
1639 return false;
1640 }
1641 true
1642 });
1643 }
1644
1645 if applicable_candidates.len() > 1 {
1646 if self.tcx.features().supertrait_item_shadowing() {
1650 if let Some(pick) =
1651 self.collapse_candidates_to_subtrait_pick(self_ty, &applicable_candidates)
1652 {
1653 return Some(Ok(pick));
1654 }
1655 }
1656
1657 let sources = candidates.iter().map(|p| self.candidate_source(p, self_ty)).collect();
1658 return Some(Err(MethodError::Ambiguity(sources)));
1659 }
1660
1661 applicable_candidates.pop().map(|(probe, status)| match status {
1662 ProbeResult::Match => Ok(probe.to_unadjusted_pick(
1663 self_ty,
1664 pick_diag_hints.unstable_candidates.clone().unwrap_or_default(),
1665 )),
1666 ProbeResult::NoMatch | ProbeResult::BadReturnType => Err(MethodError::BadReturnType),
1667 })
1668 }
1669}
1670
1671impl<'tcx> Pick<'tcx> {
1672 pub(crate) fn differs_from(&self, other: &Self) -> bool {
1677 let Self {
1678 item: AssocItem { def_id, kind: _, container: _, trait_item_def_id: _ },
1679 kind: _,
1680 import_ids: _,
1681 autoderefs: _,
1682 autoref_or_ptr_adjustment: _,
1683 self_ty,
1684 unstable_candidates: _,
1685 receiver_steps: _,
1686 shadowed_candidates: _,
1687 } = *self;
1688 self_ty != other.self_ty || def_id != other.item.def_id
1689 }
1690
1691 pub(crate) fn maybe_emit_unstable_name_collision_hint(
1693 &self,
1694 tcx: TyCtxt<'tcx>,
1695 span: Span,
1696 scope_expr_id: HirId,
1697 ) {
1698 if self.unstable_candidates.is_empty() {
1699 return;
1700 }
1701 let def_kind = self.item.as_def_kind();
1702 tcx.node_span_lint(lint::builtin::UNSTABLE_NAME_COLLISIONS, scope_expr_id, span, |lint| {
1703 lint.primary_message(format!(
1704 "{} {} with this name may be added to the standard library in the future",
1705 tcx.def_kind_descr_article(def_kind, self.item.def_id),
1706 tcx.def_kind_descr(def_kind, self.item.def_id),
1707 ));
1708
1709 match (self.item.kind, self.item.container) {
1710 (ty::AssocKind::Fn { .. }, _) => {
1711 lint.help(format!(
1716 "call with fully qualified syntax `{}(...)` to keep using the current \
1717 method",
1718 tcx.def_path_str(self.item.def_id),
1719 ));
1720 }
1721 (ty::AssocKind::Const { name }, ty::AssocItemContainer::Trait) => {
1722 let def_id = self.item.container_id(tcx);
1723 lint.span_suggestion(
1724 span,
1725 "use the fully qualified path to the associated const",
1726 format!("<{} as {}>::{}", self.self_ty, tcx.def_path_str(def_id), name),
1727 Applicability::MachineApplicable,
1728 );
1729 }
1730 _ => {}
1731 }
1732 tcx.disabled_nightly_features(
1733 lint,
1734 self.unstable_candidates.iter().map(|(candidate, feature)| {
1735 (format!(" `{}`", tcx.def_path_str(candidate.item.def_id)), *feature)
1736 }),
1737 );
1738 });
1739 }
1740}
1741
1742impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
1743 fn select_trait_candidate(
1744 &self,
1745 trait_ref: ty::TraitRef<'tcx>,
1746 ) -> traits::SelectionResult<'tcx, traits::Selection<'tcx>> {
1747 let obligation =
1748 traits::Obligation::new(self.tcx, self.misc(self.span), self.param_env, trait_ref);
1749 traits::SelectionContext::new(self).select(&obligation)
1750 }
1751
1752 fn candidate_source(&self, candidate: &Candidate<'tcx>, self_ty: Ty<'tcx>) -> CandidateSource {
1755 match candidate.kind {
1756 InherentImplCandidate { .. } => {
1757 CandidateSource::Impl(candidate.item.container_id(self.tcx))
1758 }
1759 ObjectCandidate(_) | WhereClauseCandidate(_) => {
1760 CandidateSource::Trait(candidate.item.container_id(self.tcx))
1761 }
1762 TraitCandidate(trait_ref) => self.probe(|_| {
1763 let trait_ref = self.instantiate_binder_with_fresh_vars(
1764 self.span,
1765 BoundRegionConversionTime::FnCall,
1766 trait_ref,
1767 );
1768 let (xform_self_ty, _) =
1769 self.xform_self_ty(candidate.item, trait_ref.self_ty(), trait_ref.args);
1770 let _ = self.at(&ObligationCause::dummy(), self.param_env).sup(
1773 DefineOpaqueTypes::Yes,
1774 xform_self_ty,
1775 self_ty,
1776 );
1777 match self.select_trait_candidate(trait_ref) {
1778 Ok(Some(traits::ImplSource::UserDefined(ref impl_data))) => {
1779 CandidateSource::Impl(impl_data.impl_def_id)
1782 }
1783 _ => CandidateSource::Trait(candidate.item.container_id(self.tcx)),
1784 }
1785 }),
1786 }
1787 }
1788
1789 fn candidate_source_from_pick(&self, pick: &Pick<'tcx>) -> CandidateSource {
1790 match pick.kind {
1791 InherentImplPick => CandidateSource::Impl(pick.item.container_id(self.tcx)),
1792 ObjectPick | WhereClausePick(_) | TraitPick => {
1793 CandidateSource::Trait(pick.item.container_id(self.tcx))
1794 }
1795 }
1796 }
1797
1798 #[instrument(level = "trace", skip(self, possibly_unsatisfied_predicates), ret)]
1799 fn consider_probe(
1800 &self,
1801 self_ty: Ty<'tcx>,
1802 probe: &Candidate<'tcx>,
1803 possibly_unsatisfied_predicates: &mut Vec<(
1804 ty::Predicate<'tcx>,
1805 Option<ty::Predicate<'tcx>>,
1806 Option<ObligationCause<'tcx>>,
1807 )>,
1808 ) -> ProbeResult {
1809 debug!("consider_probe: self_ty={:?} probe={:?}", self_ty, probe);
1810
1811 self.probe(|snapshot| {
1812 let outer_universe = self.universe();
1813
1814 let mut result = ProbeResult::Match;
1815 let cause = &self.misc(self.span);
1816 let ocx = ObligationCtxt::new_with_diagnostics(self);
1817
1818 let mut trait_predicate = None;
1819 let (mut xform_self_ty, mut xform_ret_ty);
1820
1821 match probe.kind {
1822 InherentImplCandidate { impl_def_id, .. } => {
1823 let impl_args = self.fresh_args_for_item(self.span, impl_def_id);
1824 let impl_ty = self.tcx.type_of(impl_def_id).instantiate(self.tcx, impl_args);
1825 (xform_self_ty, xform_ret_ty) =
1826 self.xform_self_ty(probe.item, impl_ty, impl_args);
1827 xform_self_ty = ocx.normalize(cause, self.param_env, xform_self_ty);
1828 match ocx.relate(cause, self.param_env, self.variance(), self_ty, xform_self_ty)
1829 {
1830 Ok(()) => {}
1831 Err(err) => {
1832 debug!("--> cannot relate self-types {:?}", err);
1833 return ProbeResult::NoMatch;
1834 }
1835 }
1836 xform_ret_ty = ocx.normalize(cause, self.param_env, xform_ret_ty);
1838 let impl_def_id = probe.item.container_id(self.tcx);
1840 let impl_bounds =
1841 self.tcx.predicates_of(impl_def_id).instantiate(self.tcx, impl_args);
1842 let impl_bounds = ocx.normalize(cause, self.param_env, impl_bounds);
1843 ocx.register_obligations(traits::predicates_for_generics(
1845 |idx, span| {
1846 let code = ObligationCauseCode::WhereClauseInExpr(
1847 impl_def_id,
1848 span,
1849 self.scope_expr_id,
1850 idx,
1851 );
1852 self.cause(self.span, code)
1853 },
1854 self.param_env,
1855 impl_bounds,
1856 ));
1857 }
1858 TraitCandidate(poly_trait_ref) => {
1859 if let Some(method_name) = self.method_name {
1862 if self_ty.is_array() && !method_name.span.at_least_rust_2021() {
1863 let trait_def = self.tcx.trait_def(poly_trait_ref.def_id());
1864 if trait_def.skip_array_during_method_dispatch {
1865 return ProbeResult::NoMatch;
1866 }
1867 }
1868
1869 if self_ty.boxed_ty().is_some_and(Ty::is_slice)
1872 && !method_name.span.at_least_rust_2024()
1873 {
1874 let trait_def = self.tcx.trait_def(poly_trait_ref.def_id());
1875 if trait_def.skip_boxed_slice_during_method_dispatch {
1876 return ProbeResult::NoMatch;
1877 }
1878 }
1879 }
1880
1881 let trait_ref = self.instantiate_binder_with_fresh_vars(
1882 self.span,
1883 BoundRegionConversionTime::FnCall,
1884 poly_trait_ref,
1885 );
1886 let trait_ref = ocx.normalize(cause, self.param_env, trait_ref);
1887 (xform_self_ty, xform_ret_ty) =
1888 self.xform_self_ty(probe.item, trait_ref.self_ty(), trait_ref.args);
1889 xform_self_ty = ocx.normalize(cause, self.param_env, xform_self_ty);
1890 match self_ty.kind() {
1891 ty::Alias(ty::Opaque, alias_ty)
1895 if !self.next_trait_solver()
1896 && self.infcx.can_define_opaque_ty(alias_ty.def_id)
1897 && !xform_self_ty.is_ty_var() =>
1898 {
1899 return ProbeResult::NoMatch;
1900 }
1901 _ => match ocx.relate(
1902 cause,
1903 self.param_env,
1904 self.variance(),
1905 self_ty,
1906 xform_self_ty,
1907 ) {
1908 Ok(()) => {}
1909 Err(err) => {
1910 debug!("--> cannot relate self-types {:?}", err);
1911 return ProbeResult::NoMatch;
1912 }
1913 },
1914 }
1915 let obligation = traits::Obligation::new(
1916 self.tcx,
1917 cause.clone(),
1918 self.param_env,
1919 ty::Binder::dummy(trait_ref),
1920 );
1921
1922 if self.infcx.next_trait_solver() || self.infcx.predicate_may_hold(&obligation)
1924 {
1925 ocx.register_obligation(obligation);
1926 } else {
1927 result = ProbeResult::NoMatch;
1928 if let Ok(Some(candidate)) = self.select_trait_candidate(trait_ref) {
1929 for nested_obligation in candidate.nested_obligations() {
1930 if !self.infcx.predicate_may_hold(&nested_obligation) {
1931 possibly_unsatisfied_predicates.push((
1932 self.resolve_vars_if_possible(nested_obligation.predicate),
1933 Some(self.resolve_vars_if_possible(obligation.predicate)),
1934 Some(nested_obligation.cause),
1935 ));
1936 }
1937 }
1938 }
1939 }
1940
1941 trait_predicate = Some(trait_ref.upcast(self.tcx));
1942 }
1943 ObjectCandidate(poly_trait_ref) | WhereClauseCandidate(poly_trait_ref) => {
1944 let trait_ref = self.instantiate_binder_with_fresh_vars(
1945 self.span,
1946 BoundRegionConversionTime::FnCall,
1947 poly_trait_ref,
1948 );
1949 (xform_self_ty, xform_ret_ty) =
1950 self.xform_self_ty(probe.item, trait_ref.self_ty(), trait_ref.args);
1951 xform_self_ty = ocx.normalize(cause, self.param_env, xform_self_ty);
1952 match ocx.relate(cause, self.param_env, self.variance(), self_ty, xform_self_ty)
1953 {
1954 Ok(()) => {}
1955 Err(err) => {
1956 debug!("--> cannot relate self-types {:?}", err);
1957 return ProbeResult::NoMatch;
1958 }
1959 }
1960 }
1961 }
1962
1963 if let Some(xform_ret_ty) = xform_ret_ty
1975 && self.infcx.next_trait_solver()
1976 {
1977 ocx.register_obligation(traits::Obligation::new(
1978 self.tcx,
1979 cause.clone(),
1980 self.param_env,
1981 ty::ClauseKind::WellFormed(xform_ret_ty.into()),
1982 ));
1983 }
1984
1985 for error in ocx.select_where_possible() {
1987 result = ProbeResult::NoMatch;
1988 let nested_predicate = self.resolve_vars_if_possible(error.obligation.predicate);
1989 if let Some(trait_predicate) = trait_predicate
1990 && nested_predicate == self.resolve_vars_if_possible(trait_predicate)
1991 {
1992 } else {
1996 possibly_unsatisfied_predicates.push((
1997 nested_predicate,
1998 Some(self.resolve_vars_if_possible(error.root_obligation.predicate))
1999 .filter(|root_predicate| *root_predicate != nested_predicate),
2000 Some(error.obligation.cause),
2001 ));
2002 }
2003 }
2004
2005 if let ProbeResult::Match = result
2006 && let Some(return_ty) = self.return_type
2007 && let Some(mut xform_ret_ty) = xform_ret_ty
2008 {
2009 if !matches!(probe.kind, InherentImplCandidate { .. }) {
2014 xform_ret_ty = ocx.normalize(&cause, self.param_env, xform_ret_ty);
2015 }
2016
2017 debug!("comparing return_ty {:?} with xform ret ty {:?}", return_ty, xform_ret_ty);
2018 match ocx.relate(cause, self.param_env, self.variance(), xform_ret_ty, return_ty) {
2019 Ok(()) => {}
2020 Err(_) => {
2021 result = ProbeResult::BadReturnType;
2022 }
2023 }
2024
2025 for error in ocx.select_where_possible() {
2027 result = ProbeResult::NoMatch;
2028 possibly_unsatisfied_predicates.push((
2029 error.obligation.predicate,
2030 Some(error.root_obligation.predicate)
2031 .filter(|predicate| *predicate != error.obligation.predicate),
2032 Some(error.root_obligation.cause),
2033 ));
2034 }
2035 }
2036
2037 if let Err(_) = self.leak_check(outer_universe, Some(snapshot)) {
2043 result = ProbeResult::NoMatch;
2044 }
2045
2046 result
2047 })
2048 }
2049
2050 fn collapse_candidates_to_trait_pick(
2068 &self,
2069 self_ty: Ty<'tcx>,
2070 probes: &[(&Candidate<'tcx>, ProbeResult)],
2071 ) -> Option<Pick<'tcx>> {
2072 let container = probes[0].0.item.trait_container(self.tcx)?;
2074 for (p, _) in &probes[1..] {
2075 let p_container = p.item.trait_container(self.tcx)?;
2076 if p_container != container {
2077 return None;
2078 }
2079 }
2080
2081 Some(Pick {
2084 item: probes[0].0.item,
2085 kind: TraitPick,
2086 import_ids: probes[0].0.import_ids.clone(),
2087 autoderefs: 0,
2088 autoref_or_ptr_adjustment: None,
2089 self_ty,
2090 unstable_candidates: vec![],
2091 receiver_steps: None,
2092 shadowed_candidates: vec![],
2093 })
2094 }
2095
2096 fn collapse_candidates_to_subtrait_pick(
2102 &self,
2103 self_ty: Ty<'tcx>,
2104 probes: &[(&Candidate<'tcx>, ProbeResult)],
2105 ) -> Option<Pick<'tcx>> {
2106 let mut child_candidate = probes[0].0;
2107 let mut child_trait = child_candidate.item.trait_container(self.tcx)?;
2108 let mut supertraits: SsoHashSet<_> = supertrait_def_ids(self.tcx, child_trait).collect();
2109
2110 let mut remaining_candidates: Vec<_> = probes[1..].iter().map(|&(p, _)| p).collect();
2111 while !remaining_candidates.is_empty() {
2112 let mut made_progress = false;
2113 let mut next_round = vec![];
2114
2115 for remaining_candidate in remaining_candidates {
2116 let remaining_trait = remaining_candidate.item.trait_container(self.tcx)?;
2117 if supertraits.contains(&remaining_trait) {
2118 made_progress = true;
2119 continue;
2120 }
2121
2122 let remaining_trait_supertraits: SsoHashSet<_> =
2128 supertrait_def_ids(self.tcx, remaining_trait).collect();
2129 if remaining_trait_supertraits.contains(&child_trait) {
2130 child_candidate = remaining_candidate;
2131 child_trait = remaining_trait;
2132 supertraits = remaining_trait_supertraits;
2133 made_progress = true;
2134 continue;
2135 }
2136
2137 next_round.push(remaining_candidate);
2143 }
2144
2145 if made_progress {
2146 remaining_candidates = next_round;
2148 } else {
2149 return None;
2152 }
2153 }
2154
2155 Some(Pick {
2156 item: child_candidate.item,
2157 kind: TraitPick,
2158 import_ids: child_candidate.import_ids.clone(),
2159 autoderefs: 0,
2160 autoref_or_ptr_adjustment: None,
2161 self_ty,
2162 unstable_candidates: vec![],
2163 shadowed_candidates: probes
2164 .iter()
2165 .map(|(c, _)| c.item)
2166 .filter(|item| item.def_id != child_candidate.item.def_id)
2167 .collect(),
2168 receiver_steps: None,
2169 })
2170 }
2171
2172 #[instrument(level = "debug", skip(self))]
2176 pub(crate) fn probe_for_similar_candidate(
2177 &mut self,
2178 ) -> Result<Option<ty::AssocItem>, MethodError<'tcx>> {
2179 debug!("probing for method names similar to {:?}", self.method_name);
2180
2181 self.probe(|_| {
2182 let mut pcx = ProbeContext::new(
2183 self.fcx,
2184 self.span,
2185 self.mode,
2186 self.method_name,
2187 self.return_type,
2188 self.orig_steps_var_values,
2189 self.steps,
2190 self.scope_expr_id,
2191 IsSuggestion(true),
2192 );
2193 pcx.allow_similar_names = true;
2194 pcx.assemble_inherent_candidates();
2195 pcx.assemble_extension_candidates_for_all_traits();
2196
2197 let method_names = pcx.candidate_method_names(|_| true);
2198 pcx.allow_similar_names = false;
2199 let applicable_close_candidates: Vec<ty::AssocItem> = method_names
2200 .iter()
2201 .filter_map(|&method_name| {
2202 pcx.reset();
2203 pcx.method_name = Some(method_name);
2204 pcx.assemble_inherent_candidates();
2205 pcx.assemble_extension_candidates_for_all_traits();
2206 pcx.pick_core(&mut Vec::new()).and_then(|pick| pick.ok()).map(|pick| pick.item)
2207 })
2208 .collect();
2209
2210 if applicable_close_candidates.is_empty() {
2211 Ok(None)
2212 } else {
2213 let best_name = {
2214 let names = applicable_close_candidates
2215 .iter()
2216 .map(|cand| cand.name())
2217 .collect::<Vec<Symbol>>();
2218 find_best_match_for_name_with_substrings(
2219 &names,
2220 self.method_name.unwrap().name,
2221 None,
2222 )
2223 }
2224 .or_else(|| {
2225 applicable_close_candidates
2226 .iter()
2227 .find(|cand| self.matches_by_doc_alias(cand.def_id))
2228 .map(|cand| cand.name())
2229 });
2230 Ok(best_name.and_then(|best_name| {
2231 applicable_close_candidates
2232 .into_iter()
2233 .find(|method| method.name() == best_name)
2234 }))
2235 }
2236 })
2237 }
2238
2239 fn has_applicable_self(&self, item: &ty::AssocItem) -> bool {
2242 match self.mode {
2248 Mode::MethodCall => item.is_method(),
2249 Mode::Path => match item.kind {
2250 ty::AssocKind::Type { .. } => false,
2251 ty::AssocKind::Fn { .. } | ty::AssocKind::Const { .. } => true,
2252 },
2253 }
2254 }
2261
2262 fn record_static_candidate(&self, source: CandidateSource) {
2263 self.static_candidates.borrow_mut().push(source);
2264 }
2265
2266 #[instrument(level = "debug", skip(self))]
2267 fn xform_self_ty(
2268 &self,
2269 item: ty::AssocItem,
2270 impl_ty: Ty<'tcx>,
2271 args: GenericArgsRef<'tcx>,
2272 ) -> (Ty<'tcx>, Option<Ty<'tcx>>) {
2273 if item.is_fn() && self.mode == Mode::MethodCall {
2274 let sig = self.xform_method_sig(item.def_id, args);
2275 (sig.inputs()[0], Some(sig.output()))
2276 } else {
2277 (impl_ty, None)
2278 }
2279 }
2280
2281 #[instrument(level = "debug", skip(self))]
2282 fn xform_method_sig(&self, method: DefId, args: GenericArgsRef<'tcx>) -> ty::FnSig<'tcx> {
2283 let fn_sig = self.tcx.fn_sig(method);
2284 debug!(?fn_sig);
2285
2286 assert!(!args.has_escaping_bound_vars());
2287
2288 let generics = self.tcx.generics_of(method);
2294 assert_eq!(args.len(), generics.parent_count);
2295
2296 let xform_fn_sig = if generics.is_own_empty() {
2297 fn_sig.instantiate(self.tcx, args)
2298 } else {
2299 let args = GenericArgs::for_item(self.tcx, method, |param, _| {
2300 let i = param.index as usize;
2301 if i < args.len() {
2302 args[i]
2303 } else {
2304 match param.kind {
2305 GenericParamDefKind::Lifetime => {
2306 self.tcx.lifetimes.re_erased.into()
2308 }
2309 GenericParamDefKind::Type { .. } | GenericParamDefKind::Const { .. } => {
2310 self.var_for_def(self.span, param)
2311 }
2312 }
2313 }
2314 });
2315 fn_sig.instantiate(self.tcx, args)
2316 };
2317
2318 self.tcx.instantiate_bound_regions_with_erased(xform_fn_sig)
2319 }
2320
2321 fn is_relevant_kind_for_mode(&self, kind: ty::AssocKind) -> bool {
2323 match (self.mode, kind) {
2324 (Mode::MethodCall, ty::AssocKind::Fn { .. }) => true,
2325 (Mode::Path, ty::AssocKind::Const { .. } | ty::AssocKind::Fn { .. }) => true,
2326 _ => false,
2327 }
2328 }
2329
2330 fn matches_by_doc_alias(&self, def_id: DefId) -> bool {
2333 let Some(method) = self.method_name else {
2334 return false;
2335 };
2336 let Some(local_def_id) = def_id.as_local() else {
2337 return false;
2338 };
2339 let hir_id = self.fcx.tcx.local_def_id_to_hir_id(local_def_id);
2340 let attrs = self.fcx.tcx.hir_attrs(hir_id);
2341
2342 if is_doc_alias_attrs_contain_symbol(attrs.into_iter(), method.name) {
2343 return true;
2344 }
2345
2346 for attr in attrs {
2347 if attr.has_name(sym::rustc_confusables) {
2348 let Some(confusables) = attr.meta_item_list() else {
2349 continue;
2350 };
2351 for n in confusables {
2353 if let Some(lit) = n.lit()
2354 && method.name == lit.symbol
2355 {
2356 return true;
2357 }
2358 }
2359 }
2360 }
2361 false
2362 }
2363
2364 fn impl_or_trait_item(&self, def_id: DefId) -> SmallVec<[ty::AssocItem; 1]> {
2369 if let Some(name) = self.method_name {
2370 if self.allow_similar_names {
2371 let max_dist = max(name.as_str().len(), 3) / 3;
2372 self.tcx
2373 .associated_items(def_id)
2374 .in_definition_order()
2375 .filter(|x| {
2376 if !self.is_relevant_kind_for_mode(x.kind) {
2377 return false;
2378 }
2379 if self.matches_by_doc_alias(x.def_id) {
2380 return true;
2381 }
2382 match edit_distance_with_substrings(
2383 name.as_str(),
2384 x.name().as_str(),
2385 max_dist,
2386 ) {
2387 Some(d) => d > 0,
2388 None => false,
2389 }
2390 })
2391 .copied()
2392 .collect()
2393 } else {
2394 self.fcx
2395 .associated_value(def_id, name)
2396 .filter(|x| self.is_relevant_kind_for_mode(x.kind))
2397 .map_or_else(SmallVec::new, |x| SmallVec::from_buf([x]))
2398 }
2399 } else {
2400 self.tcx
2401 .associated_items(def_id)
2402 .in_definition_order()
2403 .filter(|x| self.is_relevant_kind_for_mode(x.kind))
2404 .copied()
2405 .collect()
2406 }
2407 }
2408}
2409
2410impl<'tcx> Candidate<'tcx> {
2411 fn to_unadjusted_pick(
2412 &self,
2413 self_ty: Ty<'tcx>,
2414 unstable_candidates: Vec<(Candidate<'tcx>, Symbol)>,
2415 ) -> Pick<'tcx> {
2416 Pick {
2417 item: self.item,
2418 kind: match self.kind {
2419 InherentImplCandidate { .. } => InherentImplPick,
2420 ObjectCandidate(_) => ObjectPick,
2421 TraitCandidate(_) => TraitPick,
2422 WhereClauseCandidate(trait_ref) => {
2423 assert!(
2429 !trait_ref.skip_binder().args.has_infer()
2430 && !trait_ref.skip_binder().args.has_placeholders()
2431 );
2432
2433 WhereClausePick(trait_ref)
2434 }
2435 },
2436 import_ids: self.import_ids.clone(),
2437 autoderefs: 0,
2438 autoref_or_ptr_adjustment: None,
2439 self_ty,
2440 unstable_candidates,
2441 receiver_steps: match self.kind {
2442 InherentImplCandidate { receiver_steps, .. } => Some(receiver_steps),
2443 _ => None,
2444 },
2445 shadowed_candidates: vec![],
2446 }
2447 }
2448}