1use std::assert_matches::debug_assert_matches;
2use std::cell::{Cell, RefCell};
3use std::cmp::max;
4use std::ops::Deref;
5
6use rustc_attr_parsing::is_doc_alias_attrs_contain_symbol;
7use rustc_data_structures::fx::FxHashSet;
8use rustc_data_structures::sso::SsoHashSet;
9use rustc_errors::Applicability;
10use rustc_hir as hir;
11use rustc_hir::HirId;
12use rustc_hir::def::DefKind;
13use rustc_hir_analysis::autoderef::{self, Autoderef};
14use rustc_infer::infer::canonical::{Canonical, OriginalQueryValues, QueryResponse};
15use rustc_infer::infer::{BoundRegionConversionTime, DefineOpaqueTypes, InferOk, TyCtxtInferExt};
16use rustc_infer::traits::ObligationCauseCode;
17use rustc_middle::middle::stability;
18use rustc_middle::ty::elaborate::supertrait_def_ids;
19use rustc_middle::ty::fast_reject::{DeepRejectCtxt, TreatParams, simplify_type};
20use rustc_middle::ty::{
21 self, AssocContainer, AssocItem, GenericArgs, GenericArgsRef, GenericParamDefKind, ParamEnvAnd,
22 Ty, TyCtxt, TypeVisitableExt, Upcast,
23};
24use rustc_middle::{bug, span_bug};
25use rustc_session::lint;
26use rustc_span::def_id::{DefId, LocalDefId};
27use rustc_span::edit_distance::{
28 edit_distance_with_substrings, find_best_match_for_name_with_substrings,
29};
30use rustc_span::{DUMMY_SP, Ident, Span, Symbol, sym};
31use rustc_trait_selection::error_reporting::infer::need_type_info::TypeAnnotationNeeded;
32use rustc_trait_selection::infer::InferCtxtExt as _;
33use rustc_trait_selection::traits::query::CanonicalTyGoal;
34use rustc_trait_selection::traits::query::evaluate_obligation::InferCtxtExt;
35use rustc_trait_selection::traits::query::method_autoderef::{
36 CandidateStep, MethodAutoderefBadTy, MethodAutoderefStepsResult,
37};
38use rustc_trait_selection::traits::{self, ObligationCause, ObligationCtxt};
39use smallvec::{SmallVec, smallvec};
40use tracing::{debug, instrument};
41
42use self::CandidateKind::*;
43pub(crate) use self::PickKind::*;
44use super::{CandidateSource, MethodError, NoMatchData, suggest};
45use crate::FnCtxt;
46
47#[derive(Clone, Copy, Debug)]
50pub(crate) struct IsSuggestion(pub bool);
51
52pub(crate) struct ProbeContext<'a, 'tcx> {
53 fcx: &'a FnCtxt<'a, 'tcx>,
54 span: Span,
55 mode: Mode,
56 method_name: Option<Ident>,
57 return_type: Option<Ty<'tcx>>,
58
59 orig_steps_var_values: &'a OriginalQueryValues<'tcx>,
62 steps: &'tcx [CandidateStep<'tcx>],
63
64 inherent_candidates: Vec<Candidate<'tcx>>,
65 extension_candidates: Vec<Candidate<'tcx>>,
66 impl_dups: FxHashSet<DefId>,
67
68 allow_similar_names: bool,
71
72 private_candidates: Vec<Candidate<'tcx>>,
75
76 private_candidate: Cell<Option<(DefKind, DefId)>>,
78
79 static_candidates: RefCell<Vec<CandidateSource>>,
82
83 scope_expr_id: HirId,
84
85 is_suggestion: IsSuggestion,
89}
90
91impl<'a, 'tcx> Deref for ProbeContext<'a, 'tcx> {
92 type Target = FnCtxt<'a, 'tcx>;
93 fn deref(&self) -> &Self::Target {
94 self.fcx
95 }
96}
97
98#[derive(Debug, Clone)]
99pub(crate) struct Candidate<'tcx> {
100 pub(crate) item: ty::AssocItem,
101 pub(crate) kind: CandidateKind<'tcx>,
102 pub(crate) import_ids: SmallVec<[LocalDefId; 1]>,
103}
104
105#[derive(Debug, Clone)]
106pub(crate) enum CandidateKind<'tcx> {
107 InherentImplCandidate { impl_def_id: DefId, receiver_steps: usize },
108 ObjectCandidate(ty::PolyTraitRef<'tcx>),
109 TraitCandidate(ty::PolyTraitRef<'tcx>),
110 WhereClauseCandidate(ty::PolyTraitRef<'tcx>),
111}
112
113#[derive(Debug, PartialEq, Eq, Copy, Clone)]
114enum ProbeResult {
115 NoMatch,
116 BadReturnType,
117 Match,
118}
119
120#[derive(Debug, PartialEq, Copy, Clone)]
133pub(crate) enum AutorefOrPtrAdjustment {
134 Autoref {
137 mutbl: hir::Mutability,
138
139 unsize: bool,
142 },
143 ToConstPtr,
145
146 ReborrowPin(hir::Mutability),
148}
149
150impl AutorefOrPtrAdjustment {
151 fn get_unsize(&self) -> bool {
152 match self {
153 AutorefOrPtrAdjustment::Autoref { mutbl: _, unsize } => *unsize,
154 AutorefOrPtrAdjustment::ToConstPtr => false,
155 AutorefOrPtrAdjustment::ReborrowPin(_) => false,
156 }
157 }
158}
159
160#[derive(Debug)]
162struct PickDiagHints<'a, 'tcx> {
163 unstable_candidates: Option<Vec<(Candidate<'tcx>, Symbol)>>,
165
166 unsatisfied_predicates: &'a mut Vec<(
169 ty::Predicate<'tcx>,
170 Option<ty::Predicate<'tcx>>,
171 Option<ObligationCause<'tcx>>,
172 )>,
173}
174
175#[derive(Debug)]
179struct PickConstraintsForShadowed {
180 autoderefs: usize,
181 receiver_steps: Option<usize>,
182 def_id: DefId,
183}
184
185impl PickConstraintsForShadowed {
186 fn may_shadow_based_on_autoderefs(&self, autoderefs: usize) -> bool {
187 autoderefs == self.autoderefs
188 }
189
190 fn candidate_may_shadow(&self, candidate: &Candidate<'_>) -> bool {
191 candidate.item.def_id != self.def_id
193 && match candidate.kind {
197 CandidateKind::InherentImplCandidate { receiver_steps, .. } => match self.receiver_steps {
198 Some(shadowed_receiver_steps) => receiver_steps > shadowed_receiver_steps,
199 _ => false
200 },
201 _ => false
202 }
203 }
204}
205
206#[derive(Debug, Clone)]
207pub(crate) struct Pick<'tcx> {
208 pub item: ty::AssocItem,
209 pub kind: PickKind<'tcx>,
210 pub import_ids: SmallVec<[LocalDefId; 1]>,
211
212 pub autoderefs: usize,
217
218 pub autoref_or_ptr_adjustment: Option<AutorefOrPtrAdjustment>,
221 pub self_ty: Ty<'tcx>,
222
223 unstable_candidates: Vec<(Candidate<'tcx>, Symbol)>,
225
226 pub receiver_steps: Option<usize>,
230
231 pub shadowed_candidates: Vec<ty::AssocItem>,
233}
234
235#[derive(Clone, Debug, PartialEq, Eq)]
236pub(crate) enum PickKind<'tcx> {
237 InherentImplPick,
238 ObjectPick,
239 TraitPick,
240 WhereClausePick(
241 ty::PolyTraitRef<'tcx>,
243 ),
244}
245
246pub(crate) type PickResult<'tcx> = Result<Pick<'tcx>, MethodError<'tcx>>;
247
248#[derive(PartialEq, Eq, Copy, Clone, Debug)]
249pub(crate) enum Mode {
250 MethodCall,
254 Path,
258}
259
260#[derive(PartialEq, Eq, Copy, Clone, Debug)]
261pub(crate) enum ProbeScope {
262 Single(DefId),
264
265 TraitsInScope,
267
268 AllTraits,
270}
271
272impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
273 #[instrument(level = "debug", skip(self, candidate_filter))]
280 pub(crate) fn probe_for_return_type_for_diagnostic(
281 &self,
282 span: Span,
283 mode: Mode,
284 return_type: Ty<'tcx>,
285 self_ty: Ty<'tcx>,
286 scope_expr_id: HirId,
287 candidate_filter: impl Fn(&ty::AssocItem) -> bool,
288 ) -> Vec<ty::AssocItem> {
289 let method_names = self
290 .probe_op(
291 span,
292 mode,
293 None,
294 Some(return_type),
295 IsSuggestion(true),
296 self_ty,
297 scope_expr_id,
298 ProbeScope::AllTraits,
299 |probe_cx| Ok(probe_cx.candidate_method_names(candidate_filter)),
300 )
301 .unwrap_or_default();
302 method_names
303 .iter()
304 .flat_map(|&method_name| {
305 self.probe_op(
306 span,
307 mode,
308 Some(method_name),
309 Some(return_type),
310 IsSuggestion(true),
311 self_ty,
312 scope_expr_id,
313 ProbeScope::AllTraits,
314 |probe_cx| probe_cx.pick(),
315 )
316 .ok()
317 .map(|pick| pick.item)
318 })
319 .collect()
320 }
321
322 #[instrument(level = "debug", skip(self))]
323 pub(crate) fn probe_for_name(
324 &self,
325 mode: Mode,
326 item_name: Ident,
327 return_type: Option<Ty<'tcx>>,
328 is_suggestion: IsSuggestion,
329 self_ty: Ty<'tcx>,
330 scope_expr_id: HirId,
331 scope: ProbeScope,
332 ) -> PickResult<'tcx> {
333 self.probe_op(
334 item_name.span,
335 mode,
336 Some(item_name),
337 return_type,
338 is_suggestion,
339 self_ty,
340 scope_expr_id,
341 scope,
342 |probe_cx| probe_cx.pick(),
343 )
344 }
345
346 #[instrument(level = "debug", skip(self))]
347 pub(crate) fn probe_for_name_many(
348 &self,
349 mode: Mode,
350 item_name: Ident,
351 return_type: Option<Ty<'tcx>>,
352 is_suggestion: IsSuggestion,
353 self_ty: Ty<'tcx>,
354 scope_expr_id: HirId,
355 scope: ProbeScope,
356 ) -> Result<Vec<Candidate<'tcx>>, MethodError<'tcx>> {
357 self.probe_op(
358 item_name.span,
359 mode,
360 Some(item_name),
361 return_type,
362 is_suggestion,
363 self_ty,
364 scope_expr_id,
365 scope,
366 |probe_cx| {
367 Ok(probe_cx
368 .inherent_candidates
369 .into_iter()
370 .chain(probe_cx.extension_candidates)
371 .collect())
372 },
373 )
374 }
375
376 pub(crate) fn probe_op<OP, R>(
377 &'a self,
378 span: Span,
379 mode: Mode,
380 method_name: Option<Ident>,
381 return_type: Option<Ty<'tcx>>,
382 is_suggestion: IsSuggestion,
383 self_ty: Ty<'tcx>,
384 scope_expr_id: HirId,
385 scope: ProbeScope,
386 op: OP,
387 ) -> Result<R, MethodError<'tcx>>
388 where
389 OP: FnOnce(ProbeContext<'_, 'tcx>) -> Result<R, MethodError<'tcx>>,
390 {
391 let mut orig_values = OriginalQueryValues::default();
392 let query_input = self.canonicalize_query(
393 ParamEnvAnd { param_env: self.param_env, value: self_ty },
394 &mut orig_values,
395 );
396
397 let steps = match mode {
398 Mode::MethodCall => self.tcx.method_autoderef_steps(query_input),
399 Mode::Path => self.probe(|_| {
400 let infcx = &self.infcx;
406 let (ParamEnvAnd { param_env: _, value: self_ty }, var_values) =
407 infcx.instantiate_canonical(span, &query_input.canonical);
408 debug!(?self_ty, ?query_input, "probe_op: Mode::Path");
409 MethodAutoderefStepsResult {
410 steps: infcx.tcx.arena.alloc_from_iter([CandidateStep {
411 self_ty: self
412 .make_query_response_ignoring_pending_obligations(var_values, self_ty),
413 autoderefs: 0,
414 from_unsafe_deref: false,
415 unsize: false,
416 reachable_via_deref: true,
417 }]),
418 opt_bad_ty: None,
419 reached_recursion_limit: false,
420 }
421 }),
422 };
423
424 if steps.reached_recursion_limit && !is_suggestion.0 {
428 self.probe(|_| {
429 let ty = &steps
430 .steps
431 .last()
432 .unwrap_or_else(|| span_bug!(span, "reached the recursion limit in 0 steps?"))
433 .self_ty;
434 let ty = self
435 .probe_instantiate_query_response(span, &orig_values, ty)
436 .unwrap_or_else(|_| span_bug!(span, "instantiating {:?} failed?", ty));
437 autoderef::report_autoderef_recursion_limit_error(self.tcx, span, ty.value);
438 });
439 }
440
441 if let Some(bad_ty) = &steps.opt_bad_ty {
444 if is_suggestion.0 {
445 return Err(MethodError::NoMatch(NoMatchData {
448 static_candidates: Vec::new(),
449 unsatisfied_predicates: Vec::new(),
450 out_of_scope_traits: Vec::new(),
451 similar_candidate: None,
452 mode,
453 }));
454 } else if bad_ty.reached_raw_pointer
455 && !self.tcx.features().arbitrary_self_types_pointers()
456 && !self.tcx.sess.at_least_rust_2018()
457 {
458 self.tcx.node_span_lint(
462 lint::builtin::TYVAR_BEHIND_RAW_POINTER,
463 scope_expr_id,
464 span,
465 |lint| {
466 lint.primary_message("type annotations needed");
467 },
468 );
469 } else {
470 let ty = &bad_ty.ty;
474 let ty = self
475 .probe_instantiate_query_response(span, &orig_values, ty)
476 .unwrap_or_else(|_| span_bug!(span, "instantiating {:?} failed?", ty));
477 let ty = self.resolve_vars_if_possible(ty.value);
478 let guar = match *ty.kind() {
479 ty::Infer(ty::TyVar(_)) => {
480 let raw_ptr_call = bad_ty.reached_raw_pointer
481 && !self.tcx.features().arbitrary_self_types();
482 let mut err = self.err_ctxt().emit_inference_failure_err(
483 self.body_id,
484 span,
485 ty.into(),
486 TypeAnnotationNeeded::E0282,
487 !raw_ptr_call,
488 );
489 if raw_ptr_call {
490 err.span_label(span, "cannot call a method on a raw pointer with an unknown pointee type");
491 }
492 err.emit()
493 }
494 ty::Error(guar) => guar,
495 _ => bug!("unexpected bad final type in method autoderef"),
496 };
497 self.demand_eqtype(span, ty, Ty::new_error(self.tcx, guar));
498 return Err(MethodError::ErrorReported(guar));
499 }
500 }
501
502 debug!("ProbeContext: steps for self_ty={:?} are {:?}", self_ty, steps);
503
504 self.probe(|_| {
507 let mut probe_cx = ProbeContext::new(
508 self,
509 span,
510 mode,
511 method_name,
512 return_type,
513 &orig_values,
514 steps.steps,
515 scope_expr_id,
516 is_suggestion,
517 );
518
519 match scope {
520 ProbeScope::TraitsInScope => {
521 probe_cx.assemble_inherent_candidates();
522 probe_cx.assemble_extension_candidates_for_traits_in_scope();
523 }
524 ProbeScope::AllTraits => {
525 probe_cx.assemble_inherent_candidates();
526 probe_cx.assemble_extension_candidates_for_all_traits();
527 }
528 ProbeScope::Single(def_id) => {
529 let item = self.tcx.associated_item(def_id);
530 assert_eq!(item.container, AssocContainer::Trait);
532
533 let trait_def_id = self.tcx.parent(def_id);
534 let trait_span = self.tcx.def_span(trait_def_id);
535
536 let trait_args = self.fresh_args_for_item(trait_span, trait_def_id);
537 let trait_ref = ty::TraitRef::new_from_args(self.tcx, trait_def_id, trait_args);
538
539 probe_cx.push_candidate(
540 Candidate {
541 item,
542 kind: CandidateKind::TraitCandidate(ty::Binder::dummy(trait_ref)),
543 import_ids: smallvec![],
544 },
545 false,
546 );
547 }
548 };
549 op(probe_cx)
550 })
551 }
552}
553
554pub(crate) fn method_autoderef_steps<'tcx>(
555 tcx: TyCtxt<'tcx>,
556 goal: CanonicalTyGoal<'tcx>,
557) -> MethodAutoderefStepsResult<'tcx> {
558 debug!("method_autoderef_steps({:?})", goal);
559
560 let (ref infcx, goal, inference_vars) = tcx.infer_ctxt().build_with_canonical(DUMMY_SP, &goal);
561 let ParamEnvAnd { param_env, value: self_ty } = goal;
562
563 let mut autoderef_via_deref =
573 Autoderef::new(infcx, param_env, hir::def_id::CRATE_DEF_ID, DUMMY_SP, self_ty)
574 .include_raw_pointers()
575 .silence_errors();
576
577 let mut reached_raw_pointer = false;
578 let arbitrary_self_types_enabled =
579 tcx.features().arbitrary_self_types() || tcx.features().arbitrary_self_types_pointers();
580 let (mut steps, reached_recursion_limit): (Vec<_>, bool) = if arbitrary_self_types_enabled {
581 let reachable_via_deref =
582 autoderef_via_deref.by_ref().map(|_| true).chain(std::iter::repeat(false));
583
584 let mut autoderef_via_receiver =
585 Autoderef::new(infcx, param_env, hir::def_id::CRATE_DEF_ID, DUMMY_SP, self_ty)
586 .include_raw_pointers()
587 .use_receiver_trait()
588 .silence_errors();
589 let steps = autoderef_via_receiver
590 .by_ref()
591 .zip(reachable_via_deref)
592 .map(|((ty, d), reachable_via_deref)| {
593 let step = CandidateStep {
594 self_ty: infcx
595 .make_query_response_ignoring_pending_obligations(inference_vars, ty),
596 autoderefs: d,
597 from_unsafe_deref: reached_raw_pointer,
598 unsize: false,
599 reachable_via_deref,
600 };
601 if ty.is_raw_ptr() {
602 reached_raw_pointer = true;
604 }
605 step
606 })
607 .collect();
608 (steps, autoderef_via_receiver.reached_recursion_limit())
609 } else {
610 let steps = autoderef_via_deref
611 .by_ref()
612 .map(|(ty, d)| {
613 let step = CandidateStep {
614 self_ty: infcx
615 .make_query_response_ignoring_pending_obligations(inference_vars, ty),
616 autoderefs: d,
617 from_unsafe_deref: reached_raw_pointer,
618 unsize: false,
619 reachable_via_deref: true,
620 };
621 if ty.is_raw_ptr() {
622 reached_raw_pointer = true;
624 }
625 step
626 })
627 .collect();
628 (steps, autoderef_via_deref.reached_recursion_limit())
629 };
630 let final_ty = autoderef_via_deref.final_ty();
631 let opt_bad_ty = match final_ty.kind() {
632 ty::Infer(ty::TyVar(_)) | ty::Error(_) => Some(MethodAutoderefBadTy {
633 reached_raw_pointer,
634 ty: infcx.make_query_response_ignoring_pending_obligations(inference_vars, final_ty),
635 }),
636 ty::Array(elem_ty, _) => {
637 let autoderefs = steps.iter().filter(|s| s.reachable_via_deref).count() - 1;
638 steps.push(CandidateStep {
639 self_ty: infcx.make_query_response_ignoring_pending_obligations(
640 inference_vars,
641 Ty::new_slice(infcx.tcx, *elem_ty),
642 ),
643 autoderefs,
644 from_unsafe_deref: reached_raw_pointer,
647 unsize: true,
648 reachable_via_deref: true, });
651
652 None
653 }
654 _ => None,
655 };
656
657 debug!("method_autoderef_steps: steps={:?} opt_bad_ty={:?}", steps, opt_bad_ty);
658
659 MethodAutoderefStepsResult {
660 steps: tcx.arena.alloc_from_iter(steps),
661 opt_bad_ty: opt_bad_ty.map(|ty| &*tcx.arena.alloc(ty)),
662 reached_recursion_limit,
663 }
664}
665
666impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
667 fn new(
668 fcx: &'a FnCtxt<'a, 'tcx>,
669 span: Span,
670 mode: Mode,
671 method_name: Option<Ident>,
672 return_type: Option<Ty<'tcx>>,
673 orig_steps_var_values: &'a OriginalQueryValues<'tcx>,
674 steps: &'tcx [CandidateStep<'tcx>],
675 scope_expr_id: HirId,
676 is_suggestion: IsSuggestion,
677 ) -> ProbeContext<'a, 'tcx> {
678 ProbeContext {
679 fcx,
680 span,
681 mode,
682 method_name,
683 return_type,
684 inherent_candidates: Vec::new(),
685 extension_candidates: Vec::new(),
686 impl_dups: FxHashSet::default(),
687 orig_steps_var_values,
688 steps,
689 allow_similar_names: false,
690 private_candidates: Vec::new(),
691 private_candidate: Cell::new(None),
692 static_candidates: RefCell::new(Vec::new()),
693 scope_expr_id,
694 is_suggestion,
695 }
696 }
697
698 fn reset(&mut self) {
699 self.inherent_candidates.clear();
700 self.extension_candidates.clear();
701 self.impl_dups.clear();
702 self.private_candidates.clear();
703 self.private_candidate.set(None);
704 self.static_candidates.borrow_mut().clear();
705 }
706
707 fn variance(&self) -> ty::Variance {
711 match self.mode {
712 Mode::MethodCall => ty::Covariant,
713 Mode::Path => ty::Invariant,
714 }
715 }
716
717 fn push_candidate(&mut self, candidate: Candidate<'tcx>, is_inherent: bool) {
721 let is_accessible = if let Some(name) = self.method_name {
722 let item = candidate.item;
723 let hir_id = self.tcx.local_def_id_to_hir_id(self.body_id);
724 let def_scope =
725 self.tcx.adjust_ident_and_get_scope(name, item.container_id(self.tcx), hir_id).1;
726 item.visibility(self.tcx).is_accessible_from(def_scope, self.tcx)
727 } else {
728 true
729 };
730 if is_accessible {
731 if is_inherent {
732 self.inherent_candidates.push(candidate);
733 } else {
734 self.extension_candidates.push(candidate);
735 }
736 } else {
737 self.private_candidates.push(candidate);
738 }
739 }
740
741 fn assemble_inherent_candidates(&mut self) {
742 for step in self.steps.iter() {
743 self.assemble_probe(&step.self_ty, step.autoderefs);
744 }
745 }
746
747 #[instrument(level = "debug", skip(self))]
748 fn assemble_probe(
749 &mut self,
750 self_ty: &Canonical<'tcx, QueryResponse<'tcx, Ty<'tcx>>>,
751 receiver_steps: usize,
752 ) {
753 let raw_self_ty = self_ty.value.value;
754 match *raw_self_ty.kind() {
755 ty::Dynamic(data, ..) if let Some(p) = data.principal() => {
756 let (QueryResponse { value: generalized_self_ty, .. }, _ignored_var_values) =
774 self.fcx.instantiate_canonical(self.span, self_ty);
775
776 self.assemble_inherent_candidates_from_object(generalized_self_ty);
777 self.assemble_inherent_impl_candidates_for_type(p.def_id(), receiver_steps);
778 self.assemble_inherent_candidates_for_incoherent_ty(raw_self_ty, receiver_steps);
779 }
780 ty::Adt(def, _) => {
781 let def_id = def.did();
782 self.assemble_inherent_impl_candidates_for_type(def_id, receiver_steps);
783 self.assemble_inherent_candidates_for_incoherent_ty(raw_self_ty, receiver_steps);
784 }
785 ty::Foreign(did) => {
786 self.assemble_inherent_impl_candidates_for_type(did, receiver_steps);
787 self.assemble_inherent_candidates_for_incoherent_ty(raw_self_ty, receiver_steps);
788 }
789 ty::Param(_) => {
790 self.assemble_inherent_candidates_from_param(raw_self_ty);
791 }
792 ty::Bool
793 | ty::Char
794 | ty::Int(_)
795 | ty::Uint(_)
796 | ty::Float(_)
797 | ty::Str
798 | ty::Array(..)
799 | ty::Slice(_)
800 | ty::RawPtr(_, _)
801 | ty::Ref(..)
802 | ty::Never
803 | ty::Tuple(..) => {
804 self.assemble_inherent_candidates_for_incoherent_ty(raw_self_ty, receiver_steps)
805 }
806 _ => {}
807 }
808 }
809
810 fn assemble_inherent_candidates_for_incoherent_ty(
811 &mut self,
812 self_ty: Ty<'tcx>,
813 receiver_steps: usize,
814 ) {
815 let Some(simp) = simplify_type(self.tcx, self_ty, TreatParams::InstantiateWithInfer) else {
816 bug!("unexpected incoherent type: {:?}", self_ty)
817 };
818 for &impl_def_id in self.tcx.incoherent_impls(simp).into_iter() {
819 self.assemble_inherent_impl_probe(impl_def_id, receiver_steps);
820 }
821 }
822
823 fn assemble_inherent_impl_candidates_for_type(&mut self, def_id: DefId, receiver_steps: usize) {
824 let impl_def_ids = self.tcx.at(self.span).inherent_impls(def_id).into_iter();
825 for &impl_def_id in impl_def_ids {
826 self.assemble_inherent_impl_probe(impl_def_id, receiver_steps);
827 }
828 }
829
830 #[instrument(level = "debug", skip(self))]
831 fn assemble_inherent_impl_probe(&mut self, impl_def_id: DefId, receiver_steps: usize) {
832 if !self.impl_dups.insert(impl_def_id) {
833 return; }
835
836 for item in self.impl_or_trait_item(impl_def_id) {
837 if !self.has_applicable_self(&item) {
838 self.record_static_candidate(CandidateSource::Impl(impl_def_id));
840 continue;
841 }
842 self.push_candidate(
843 Candidate {
844 item,
845 kind: InherentImplCandidate { impl_def_id, receiver_steps },
846 import_ids: smallvec![],
847 },
848 true,
849 );
850 }
851 }
852
853 #[instrument(level = "debug", skip(self))]
854 fn assemble_inherent_candidates_from_object(&mut self, self_ty: Ty<'tcx>) {
855 let principal = match self_ty.kind() {
856 ty::Dynamic(data, ..) => Some(data),
857 _ => None,
858 }
859 .and_then(|data| data.principal())
860 .unwrap_or_else(|| {
861 span_bug!(
862 self.span,
863 "non-object {:?} in assemble_inherent_candidates_from_object",
864 self_ty
865 )
866 });
867
868 let trait_ref = principal.with_self_ty(self.tcx, self_ty);
875 self.assemble_candidates_for_bounds(
876 traits::supertraits(self.tcx, trait_ref),
877 |this, new_trait_ref, item| {
878 this.push_candidate(
879 Candidate {
880 item,
881 kind: ObjectCandidate(new_trait_ref),
882 import_ids: smallvec![],
883 },
884 true,
885 );
886 },
887 );
888 }
889
890 #[instrument(level = "debug", skip(self))]
891 fn assemble_inherent_candidates_from_param(&mut self, param_ty: Ty<'tcx>) {
892 debug_assert_matches!(param_ty.kind(), ty::Param(_));
893
894 let tcx = self.tcx;
895
896 let bounds = self.param_env.caller_bounds().iter().filter_map(|predicate| {
900 let bound_predicate = predicate.kind();
901 match bound_predicate.skip_binder() {
902 ty::ClauseKind::Trait(trait_predicate) => DeepRejectCtxt::relate_rigid_rigid(tcx)
903 .types_may_unify(param_ty, trait_predicate.trait_ref.self_ty())
904 .then(|| bound_predicate.rebind(trait_predicate.trait_ref)),
905 ty::ClauseKind::RegionOutlives(_)
906 | ty::ClauseKind::TypeOutlives(_)
907 | ty::ClauseKind::Projection(_)
908 | ty::ClauseKind::ConstArgHasType(_, _)
909 | ty::ClauseKind::WellFormed(_)
910 | ty::ClauseKind::ConstEvaluatable(_)
911 | ty::ClauseKind::UnstableFeature(_)
912 | ty::ClauseKind::HostEffect(..) => None,
913 }
914 });
915
916 self.assemble_candidates_for_bounds(bounds, |this, poly_trait_ref, item| {
917 this.push_candidate(
918 Candidate {
919 item,
920 kind: WhereClauseCandidate(poly_trait_ref),
921 import_ids: smallvec![],
922 },
923 true,
924 );
925 });
926 }
927
928 fn assemble_candidates_for_bounds<F>(
931 &mut self,
932 bounds: impl Iterator<Item = ty::PolyTraitRef<'tcx>>,
933 mut mk_cand: F,
934 ) where
935 F: for<'b> FnMut(&mut ProbeContext<'b, 'tcx>, ty::PolyTraitRef<'tcx>, ty::AssocItem),
936 {
937 for bound_trait_ref in bounds {
938 debug!("elaborate_bounds(bound_trait_ref={:?})", bound_trait_ref);
939 for item in self.impl_or_trait_item(bound_trait_ref.def_id()) {
940 if !self.has_applicable_self(&item) {
941 self.record_static_candidate(CandidateSource::Trait(bound_trait_ref.def_id()));
942 } else {
943 mk_cand(self, bound_trait_ref, item);
944 }
945 }
946 }
947 }
948
949 #[instrument(level = "debug", skip(self))]
950 fn assemble_extension_candidates_for_traits_in_scope(&mut self) {
951 let mut duplicates = FxHashSet::default();
952 let opt_applicable_traits = self.tcx.in_scope_traits(self.scope_expr_id);
953 if let Some(applicable_traits) = opt_applicable_traits {
954 for trait_candidate in applicable_traits.iter() {
955 let trait_did = trait_candidate.def_id;
956 if duplicates.insert(trait_did) {
957 self.assemble_extension_candidates_for_trait(
958 &trait_candidate.import_ids,
959 trait_did,
960 );
961 }
962 }
963 }
964 }
965
966 #[instrument(level = "debug", skip(self))]
967 fn assemble_extension_candidates_for_all_traits(&mut self) {
968 let mut duplicates = FxHashSet::default();
969 for trait_info in suggest::all_traits(self.tcx) {
970 if duplicates.insert(trait_info.def_id) {
971 self.assemble_extension_candidates_for_trait(&smallvec![], trait_info.def_id);
972 }
973 }
974 }
975
976 fn matches_return_type(&self, method: ty::AssocItem, expected: Ty<'tcx>) -> bool {
977 match method.kind {
978 ty::AssocKind::Fn { .. } => self.probe(|_| {
979 let args = self.fresh_args_for_item(self.span, method.def_id);
980 let fty = self.tcx.fn_sig(method.def_id).instantiate(self.tcx, args);
981 let fty = self.instantiate_binder_with_fresh_vars(
982 self.span,
983 BoundRegionConversionTime::FnCall,
984 fty,
985 );
986 self.can_eq(self.param_env, fty.output(), expected)
987 }),
988 _ => false,
989 }
990 }
991
992 #[instrument(level = "debug", skip(self))]
993 fn assemble_extension_candidates_for_trait(
994 &mut self,
995 import_ids: &SmallVec<[LocalDefId; 1]>,
996 trait_def_id: DefId,
997 ) {
998 let trait_args = self.fresh_args_for_item(self.span, trait_def_id);
999 let trait_ref = ty::TraitRef::new_from_args(self.tcx, trait_def_id, trait_args);
1000
1001 if self.tcx.is_trait_alias(trait_def_id) {
1002 for (bound_trait_pred, _) in
1004 traits::expand_trait_aliases(self.tcx, [(trait_ref.upcast(self.tcx), self.span)]).0
1005 {
1006 assert_eq!(bound_trait_pred.polarity(), ty::PredicatePolarity::Positive);
1007 let bound_trait_ref = bound_trait_pred.map_bound(|pred| pred.trait_ref);
1008 for item in self.impl_or_trait_item(bound_trait_ref.def_id()) {
1009 if !self.has_applicable_self(&item) {
1010 self.record_static_candidate(CandidateSource::Trait(
1011 bound_trait_ref.def_id(),
1012 ));
1013 } else {
1014 self.push_candidate(
1015 Candidate {
1016 item,
1017 import_ids: import_ids.clone(),
1018 kind: TraitCandidate(bound_trait_ref),
1019 },
1020 false,
1021 );
1022 }
1023 }
1024 }
1025 } else {
1026 debug_assert!(self.tcx.is_trait(trait_def_id));
1027 if self.tcx.trait_is_auto(trait_def_id) {
1028 return;
1029 }
1030 for item in self.impl_or_trait_item(trait_def_id) {
1031 if !self.has_applicable_self(&item) {
1033 debug!("method has inapplicable self");
1034 self.record_static_candidate(CandidateSource::Trait(trait_def_id));
1035 continue;
1036 }
1037 self.push_candidate(
1038 Candidate {
1039 item,
1040 import_ids: import_ids.clone(),
1041 kind: TraitCandidate(ty::Binder::dummy(trait_ref)),
1042 },
1043 false,
1044 );
1045 }
1046 }
1047 }
1048
1049 fn candidate_method_names(
1050 &self,
1051 candidate_filter: impl Fn(&ty::AssocItem) -> bool,
1052 ) -> Vec<Ident> {
1053 let mut set = FxHashSet::default();
1054 let mut names: Vec<_> = self
1055 .inherent_candidates
1056 .iter()
1057 .chain(&self.extension_candidates)
1058 .filter(|candidate| candidate_filter(&candidate.item))
1059 .filter(|candidate| {
1060 if let Some(return_ty) = self.return_type {
1061 self.matches_return_type(candidate.item, return_ty)
1062 } else {
1063 true
1064 }
1065 })
1066 .filter(|candidate| {
1068 !matches!(
1071 self.tcx.eval_stability(candidate.item.def_id, None, DUMMY_SP, None),
1072 stability::EvalResult::Deny { .. }
1073 )
1074 })
1075 .map(|candidate| candidate.item.ident(self.tcx))
1076 .filter(|&name| set.insert(name))
1077 .collect();
1078
1079 names.sort_by(|a, b| a.as_str().cmp(b.as_str()));
1081 names
1082 }
1083
1084 #[instrument(level = "debug", skip(self))]
1088 fn pick(mut self) -> PickResult<'tcx> {
1089 assert!(self.method_name.is_some());
1090
1091 let mut unsatisfied_predicates = Vec::new();
1092
1093 if let Some(r) = self.pick_core(&mut unsatisfied_predicates) {
1094 return r;
1095 }
1096
1097 if self.is_suggestion.0 {
1100 return Err(MethodError::NoMatch(NoMatchData {
1101 static_candidates: vec![],
1102 unsatisfied_predicates: vec![],
1103 out_of_scope_traits: vec![],
1104 similar_candidate: None,
1105 mode: self.mode,
1106 }));
1107 }
1108
1109 debug!("pick: actual search failed, assemble diagnostics");
1110
1111 let static_candidates = std::mem::take(self.static_candidates.get_mut());
1112 let private_candidate = self.private_candidate.take();
1113
1114 self.reset();
1116
1117 let span = self.span;
1118 let tcx = self.tcx;
1119
1120 self.assemble_extension_candidates_for_all_traits();
1121
1122 let out_of_scope_traits = match self.pick_core(&mut Vec::new()) {
1123 Some(Ok(p)) => vec![p.item.container_id(self.tcx)],
1124 Some(Err(MethodError::Ambiguity(v))) => v
1125 .into_iter()
1126 .map(|source| match source {
1127 CandidateSource::Trait(id) => id,
1128 CandidateSource::Impl(impl_id) => match tcx.trait_id_of_impl(impl_id) {
1129 Some(id) => id,
1130 None => span_bug!(span, "found inherent method when looking at traits"),
1131 },
1132 })
1133 .collect(),
1134 Some(Err(MethodError::NoMatch(NoMatchData {
1135 out_of_scope_traits: others, ..
1136 }))) => {
1137 assert!(others.is_empty());
1138 vec![]
1139 }
1140 _ => vec![],
1141 };
1142
1143 if let Some((kind, def_id)) = private_candidate {
1144 return Err(MethodError::PrivateMatch(kind, def_id, out_of_scope_traits));
1145 }
1146 let similar_candidate = self.probe_for_similar_candidate()?;
1147
1148 Err(MethodError::NoMatch(NoMatchData {
1149 static_candidates,
1150 unsatisfied_predicates,
1151 out_of_scope_traits,
1152 similar_candidate,
1153 mode: self.mode,
1154 }))
1155 }
1156
1157 fn pick_core(
1158 &self,
1159 unsatisfied_predicates: &mut Vec<(
1160 ty::Predicate<'tcx>,
1161 Option<ty::Predicate<'tcx>>,
1162 Option<ObligationCause<'tcx>>,
1163 )>,
1164 ) -> Option<PickResult<'tcx>> {
1165 self.pick_all_method(&mut PickDiagHints {
1167 unstable_candidates: Some(Vec::new()),
1170 unsatisfied_predicates,
1173 })
1174 .or_else(|| {
1175 self.pick_all_method(&mut PickDiagHints {
1176 unstable_candidates: None,
1181 unsatisfied_predicates: &mut Vec::new(),
1184 })
1185 })
1186 }
1187
1188 fn pick_all_method<'b>(
1189 &self,
1190 pick_diag_hints: &mut PickDiagHints<'b, 'tcx>,
1191 ) -> Option<PickResult<'tcx>> {
1192 let track_unstable_candidates = pick_diag_hints.unstable_candidates.is_some();
1193 self.steps
1194 .iter()
1195 .filter(|step| step.reachable_via_deref)
1199 .filter(|step| {
1200 debug!("pick_all_method: step={:?}", step);
1201 !step.self_ty.value.references_error() && !step.from_unsafe_deref
1204 })
1205 .find_map(|step| {
1206 let InferOk { value: self_ty, obligations: _ } = self
1207 .fcx
1208 .probe_instantiate_query_response(
1209 self.span,
1210 self.orig_steps_var_values,
1211 &step.self_ty,
1212 )
1213 .unwrap_or_else(|_| {
1214 span_bug!(self.span, "{:?} was applicable but now isn't?", step.self_ty)
1215 });
1216
1217 let by_value_pick = self.pick_by_value_method(step, self_ty, pick_diag_hints);
1218
1219 if let Some(by_value_pick) = by_value_pick {
1221 if let Ok(by_value_pick) = by_value_pick.as_ref() {
1222 if by_value_pick.kind == PickKind::InherentImplPick {
1223 for mutbl in [hir::Mutability::Not, hir::Mutability::Mut] {
1224 if let Err(e) = self.check_for_shadowed_autorefd_method(
1225 by_value_pick,
1226 step,
1227 self_ty,
1228 mutbl,
1229 track_unstable_candidates,
1230 ) {
1231 return Some(Err(e));
1232 }
1233 }
1234 }
1235 }
1236 return Some(by_value_pick);
1237 }
1238
1239 let autoref_pick = self.pick_autorefd_method(
1240 step,
1241 self_ty,
1242 hir::Mutability::Not,
1243 pick_diag_hints,
1244 None,
1245 );
1246 if let Some(autoref_pick) = autoref_pick {
1248 if let Ok(autoref_pick) = autoref_pick.as_ref() {
1249 if autoref_pick.kind == PickKind::InherentImplPick {
1251 if let Err(e) = self.check_for_shadowed_autorefd_method(
1252 autoref_pick,
1253 step,
1254 self_ty,
1255 hir::Mutability::Mut,
1256 track_unstable_candidates,
1257 ) {
1258 return Some(Err(e));
1259 }
1260 }
1261 }
1262 return Some(autoref_pick);
1263 }
1264
1265 self.pick_autorefd_method(
1289 step,
1290 self_ty,
1291 hir::Mutability::Mut,
1292 pick_diag_hints,
1293 None,
1294 )
1295 .or_else(|| self.pick_const_ptr_method(step, self_ty, pick_diag_hints))
1296 .or_else(|| self.pick_reborrow_pin_method(step, self_ty, pick_diag_hints))
1297 })
1298 }
1299
1300 fn check_for_shadowed_autorefd_method(
1316 &self,
1317 possible_shadower: &Pick<'tcx>,
1318 step: &CandidateStep<'tcx>,
1319 self_ty: Ty<'tcx>,
1320 mutbl: hir::Mutability,
1321 track_unstable_candidates: bool,
1322 ) -> Result<(), MethodError<'tcx>> {
1323 if !self.tcx.features().arbitrary_self_types()
1327 && !self.tcx.features().arbitrary_self_types_pointers()
1328 {
1329 return Ok(());
1330 }
1331
1332 let mut pick_diag_hints = PickDiagHints {
1337 unstable_candidates: if track_unstable_candidates { Some(Vec::new()) } else { None },
1338 unsatisfied_predicates: &mut Vec::new(),
1339 };
1340 let pick_constraints = PickConstraintsForShadowed {
1342 autoderefs: possible_shadower.autoderefs,
1344 receiver_steps: possible_shadower.receiver_steps,
1348 def_id: possible_shadower.item.def_id,
1351 };
1352 let potentially_shadowed_pick = self.pick_autorefd_method(
1382 step,
1383 self_ty,
1384 mutbl,
1385 &mut pick_diag_hints,
1386 Some(&pick_constraints),
1387 );
1388 if let Some(Ok(possible_shadowed)) = potentially_shadowed_pick.as_ref() {
1391 let sources = [possible_shadower, possible_shadowed]
1392 .into_iter()
1393 .map(|p| self.candidate_source_from_pick(p))
1394 .collect();
1395 return Err(MethodError::Ambiguity(sources));
1396 }
1397 Ok(())
1398 }
1399
1400 fn pick_by_value_method(
1407 &self,
1408 step: &CandidateStep<'tcx>,
1409 self_ty: Ty<'tcx>,
1410 pick_diag_hints: &mut PickDiagHints<'_, 'tcx>,
1411 ) -> Option<PickResult<'tcx>> {
1412 if step.unsize {
1413 return None;
1414 }
1415
1416 self.pick_method(self_ty, pick_diag_hints, None).map(|r| {
1417 r.map(|mut pick| {
1418 pick.autoderefs = step.autoderefs;
1419
1420 match *step.self_ty.value.value.kind() {
1421 ty::Ref(_, _, mutbl) => {
1423 pick.autoderefs += 1;
1424 pick.autoref_or_ptr_adjustment = Some(AutorefOrPtrAdjustment::Autoref {
1425 mutbl,
1426 unsize: pick.autoref_or_ptr_adjustment.is_some_and(|a| a.get_unsize()),
1427 })
1428 }
1429
1430 ty::Adt(def, args)
1431 if self.tcx.features().pin_ergonomics()
1432 && self.tcx.is_lang_item(def.did(), hir::LangItem::Pin) =>
1433 {
1434 if let ty::Ref(_, _, mutbl) = args[0].expect_ty().kind() {
1436 pick.autoref_or_ptr_adjustment =
1437 Some(AutorefOrPtrAdjustment::ReborrowPin(*mutbl));
1438 }
1439 }
1440
1441 _ => (),
1442 }
1443
1444 pick
1445 })
1446 })
1447 }
1448
1449 fn pick_autorefd_method(
1450 &self,
1451 step: &CandidateStep<'tcx>,
1452 self_ty: Ty<'tcx>,
1453 mutbl: hir::Mutability,
1454 pick_diag_hints: &mut PickDiagHints<'_, 'tcx>,
1455 pick_constraints: Option<&PickConstraintsForShadowed>,
1456 ) -> Option<PickResult<'tcx>> {
1457 let tcx = self.tcx;
1458
1459 if let Some(pick_constraints) = pick_constraints {
1460 if !pick_constraints.may_shadow_based_on_autoderefs(step.autoderefs) {
1461 return None;
1462 }
1463 }
1464
1465 let region = tcx.lifetimes.re_erased;
1467
1468 let autoref_ty = Ty::new_ref(tcx, region, self_ty, mutbl);
1469 self.pick_method(autoref_ty, pick_diag_hints, pick_constraints).map(|r| {
1470 r.map(|mut pick| {
1471 pick.autoderefs = step.autoderefs;
1472 pick.autoref_or_ptr_adjustment =
1473 Some(AutorefOrPtrAdjustment::Autoref { mutbl, unsize: step.unsize });
1474 pick
1475 })
1476 })
1477 }
1478
1479 #[instrument(level = "debug", skip(self, step, pick_diag_hints))]
1481 fn pick_reborrow_pin_method(
1482 &self,
1483 step: &CandidateStep<'tcx>,
1484 self_ty: Ty<'tcx>,
1485 pick_diag_hints: &mut PickDiagHints<'_, 'tcx>,
1486 ) -> Option<PickResult<'tcx>> {
1487 if !self.tcx.features().pin_ergonomics() {
1488 return None;
1489 }
1490
1491 let inner_ty = match self_ty.kind() {
1493 ty::Adt(def, args) if self.tcx.is_lang_item(def.did(), hir::LangItem::Pin) => {
1494 match args[0].expect_ty().kind() {
1495 ty::Ref(_, ty, hir::Mutability::Mut) => *ty,
1496 _ => {
1497 return None;
1498 }
1499 }
1500 }
1501 _ => return None,
1502 };
1503
1504 let region = self.tcx.lifetimes.re_erased;
1505 let autopin_ty = Ty::new_pinned_ref(self.tcx, region, inner_ty, hir::Mutability::Not);
1506 self.pick_method(autopin_ty, pick_diag_hints, None).map(|r| {
1507 r.map(|mut pick| {
1508 pick.autoderefs = step.autoderefs;
1509 pick.autoref_or_ptr_adjustment =
1510 Some(AutorefOrPtrAdjustment::ReborrowPin(hir::Mutability::Not));
1511 pick
1512 })
1513 })
1514 }
1515
1516 fn pick_const_ptr_method(
1520 &self,
1521 step: &CandidateStep<'tcx>,
1522 self_ty: Ty<'tcx>,
1523 pick_diag_hints: &mut PickDiagHints<'_, 'tcx>,
1524 ) -> Option<PickResult<'tcx>> {
1525 if step.unsize {
1527 return None;
1528 }
1529
1530 let &ty::RawPtr(ty, hir::Mutability::Mut) = self_ty.kind() else {
1531 return None;
1532 };
1533
1534 let const_ptr_ty = Ty::new_imm_ptr(self.tcx, ty);
1535 self.pick_method(const_ptr_ty, pick_diag_hints, None).map(|r| {
1536 r.map(|mut pick| {
1537 pick.autoderefs = step.autoderefs;
1538 pick.autoref_or_ptr_adjustment = Some(AutorefOrPtrAdjustment::ToConstPtr);
1539 pick
1540 })
1541 })
1542 }
1543
1544 fn pick_method(
1545 &self,
1546 self_ty: Ty<'tcx>,
1547 pick_diag_hints: &mut PickDiagHints<'_, 'tcx>,
1548 pick_constraints: Option<&PickConstraintsForShadowed>,
1549 ) -> Option<PickResult<'tcx>> {
1550 debug!("pick_method(self_ty={})", self.ty_to_string(self_ty));
1551
1552 for (kind, candidates) in
1553 [("inherent", &self.inherent_candidates), ("extension", &self.extension_candidates)]
1554 {
1555 debug!("searching {} candidates", kind);
1556 let res =
1557 self.consider_candidates(self_ty, candidates, pick_diag_hints, pick_constraints);
1558 if let Some(pick) = res {
1559 return Some(pick);
1560 }
1561 }
1562
1563 if self.private_candidate.get().is_none() {
1564 if let Some(Ok(pick)) = self.consider_candidates(
1565 self_ty,
1566 &self.private_candidates,
1567 &mut PickDiagHints {
1568 unstable_candidates: None,
1569 unsatisfied_predicates: &mut vec![],
1570 },
1571 None,
1572 ) {
1573 self.private_candidate.set(Some((pick.item.as_def_kind(), pick.item.def_id)));
1574 }
1575 }
1576 None
1577 }
1578
1579 fn consider_candidates(
1580 &self,
1581 self_ty: Ty<'tcx>,
1582 candidates: &[Candidate<'tcx>],
1583 pick_diag_hints: &mut PickDiagHints<'_, 'tcx>,
1584 pick_constraints: Option<&PickConstraintsForShadowed>,
1585 ) -> Option<PickResult<'tcx>> {
1586 let mut applicable_candidates: Vec<_> = candidates
1587 .iter()
1588 .filter(|candidate| {
1589 pick_constraints
1590 .map(|pick_constraints| pick_constraints.candidate_may_shadow(&candidate))
1591 .unwrap_or(true)
1592 })
1593 .map(|probe| {
1594 (
1595 probe,
1596 self.consider_probe(
1597 self_ty,
1598 probe,
1599 &mut pick_diag_hints.unsatisfied_predicates,
1600 ),
1601 )
1602 })
1603 .filter(|&(_, status)| status != ProbeResult::NoMatch)
1604 .collect();
1605
1606 debug!("applicable_candidates: {:?}", applicable_candidates);
1607
1608 if applicable_candidates.len() > 1 {
1609 if let Some(pick) =
1610 self.collapse_candidates_to_trait_pick(self_ty, &applicable_candidates)
1611 {
1612 return Some(Ok(pick));
1613 }
1614 }
1615
1616 if let Some(uc) = &mut pick_diag_hints.unstable_candidates {
1617 applicable_candidates.retain(|&(candidate, _)| {
1618 if let stability::EvalResult::Deny { feature, .. } =
1619 self.tcx.eval_stability(candidate.item.def_id, None, self.span, None)
1620 {
1621 uc.push((candidate.clone(), feature));
1622 return false;
1623 }
1624 true
1625 });
1626 }
1627
1628 if applicable_candidates.len() > 1 {
1629 if self.tcx.features().supertrait_item_shadowing() {
1633 if let Some(pick) =
1634 self.collapse_candidates_to_subtrait_pick(self_ty, &applicable_candidates)
1635 {
1636 return Some(Ok(pick));
1637 }
1638 }
1639
1640 let sources =
1641 applicable_candidates.iter().map(|p| self.candidate_source(p.0, self_ty)).collect();
1642 return Some(Err(MethodError::Ambiguity(sources)));
1643 }
1644
1645 applicable_candidates.pop().map(|(probe, status)| match status {
1646 ProbeResult::Match => Ok(probe.to_unadjusted_pick(
1647 self_ty,
1648 pick_diag_hints.unstable_candidates.clone().unwrap_or_default(),
1649 )),
1650 ProbeResult::NoMatch | ProbeResult::BadReturnType => Err(MethodError::BadReturnType),
1651 })
1652 }
1653}
1654
1655impl<'tcx> Pick<'tcx> {
1656 pub(crate) fn differs_from(&self, other: &Self) -> bool {
1661 let Self {
1662 item: AssocItem { def_id, kind: _, container: _ },
1663 kind: _,
1664 import_ids: _,
1665 autoderefs: _,
1666 autoref_or_ptr_adjustment: _,
1667 self_ty,
1668 unstable_candidates: _,
1669 receiver_steps: _,
1670 shadowed_candidates: _,
1671 } = *self;
1672 self_ty != other.self_ty || def_id != other.item.def_id
1673 }
1674
1675 pub(crate) fn maybe_emit_unstable_name_collision_hint(
1677 &self,
1678 tcx: TyCtxt<'tcx>,
1679 span: Span,
1680 scope_expr_id: HirId,
1681 ) {
1682 if self.unstable_candidates.is_empty() {
1683 return;
1684 }
1685 let def_kind = self.item.as_def_kind();
1686 tcx.node_span_lint(lint::builtin::UNSTABLE_NAME_COLLISIONS, scope_expr_id, span, |lint| {
1687 lint.primary_message(format!(
1688 "{} {} with this name may be added to the standard library in the future",
1689 tcx.def_kind_descr_article(def_kind, self.item.def_id),
1690 tcx.def_kind_descr(def_kind, self.item.def_id),
1691 ));
1692
1693 match (self.item.kind, self.item.container) {
1694 (ty::AssocKind::Fn { .. }, _) => {
1695 lint.help(format!(
1700 "call with fully qualified syntax `{}(...)` to keep using the current \
1701 method",
1702 tcx.def_path_str(self.item.def_id),
1703 ));
1704 }
1705 (ty::AssocKind::Const { name }, ty::AssocContainer::Trait) => {
1706 let def_id = self.item.container_id(tcx);
1707 lint.span_suggestion(
1708 span,
1709 "use the fully qualified path to the associated const",
1710 format!("<{} as {}>::{}", self.self_ty, tcx.def_path_str(def_id), name),
1711 Applicability::MachineApplicable,
1712 );
1713 }
1714 _ => {}
1715 }
1716 tcx.disabled_nightly_features(
1717 lint,
1718 self.unstable_candidates.iter().map(|(candidate, feature)| {
1719 (format!(" `{}`", tcx.def_path_str(candidate.item.def_id)), *feature)
1720 }),
1721 );
1722 });
1723 }
1724}
1725
1726impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
1727 fn select_trait_candidate(
1728 &self,
1729 trait_ref: ty::TraitRef<'tcx>,
1730 ) -> traits::SelectionResult<'tcx, traits::Selection<'tcx>> {
1731 let obligation =
1732 traits::Obligation::new(self.tcx, self.misc(self.span), self.param_env, trait_ref);
1733 traits::SelectionContext::new(self).select(&obligation)
1734 }
1735
1736 fn candidate_source(&self, candidate: &Candidate<'tcx>, self_ty: Ty<'tcx>) -> CandidateSource {
1739 match candidate.kind {
1740 InherentImplCandidate { .. } => {
1741 CandidateSource::Impl(candidate.item.container_id(self.tcx))
1742 }
1743 ObjectCandidate(_) | WhereClauseCandidate(_) => {
1744 CandidateSource::Trait(candidate.item.container_id(self.tcx))
1745 }
1746 TraitCandidate(trait_ref) => self.probe(|_| {
1747 let trait_ref = self.instantiate_binder_with_fresh_vars(
1748 self.span,
1749 BoundRegionConversionTime::FnCall,
1750 trait_ref,
1751 );
1752 let (xform_self_ty, _) =
1753 self.xform_self_ty(candidate.item, trait_ref.self_ty(), trait_ref.args);
1754 let _ = self.at(&ObligationCause::dummy(), self.param_env).sup(
1757 DefineOpaqueTypes::Yes,
1758 xform_self_ty,
1759 self_ty,
1760 );
1761 match self.select_trait_candidate(trait_ref) {
1762 Ok(Some(traits::ImplSource::UserDefined(ref impl_data))) => {
1763 CandidateSource::Impl(impl_data.impl_def_id)
1766 }
1767 _ => CandidateSource::Trait(candidate.item.container_id(self.tcx)),
1768 }
1769 }),
1770 }
1771 }
1772
1773 fn candidate_source_from_pick(&self, pick: &Pick<'tcx>) -> CandidateSource {
1774 match pick.kind {
1775 InherentImplPick => CandidateSource::Impl(pick.item.container_id(self.tcx)),
1776 ObjectPick | WhereClausePick(_) | TraitPick => {
1777 CandidateSource::Trait(pick.item.container_id(self.tcx))
1778 }
1779 }
1780 }
1781
1782 #[instrument(level = "trace", skip(self, possibly_unsatisfied_predicates), ret)]
1783 fn consider_probe(
1784 &self,
1785 self_ty: Ty<'tcx>,
1786 probe: &Candidate<'tcx>,
1787 possibly_unsatisfied_predicates: &mut Vec<(
1788 ty::Predicate<'tcx>,
1789 Option<ty::Predicate<'tcx>>,
1790 Option<ObligationCause<'tcx>>,
1791 )>,
1792 ) -> ProbeResult {
1793 debug!("consider_probe: self_ty={:?} probe={:?}", self_ty, probe);
1794
1795 self.probe(|snapshot| {
1796 let outer_universe = self.universe();
1797
1798 let mut result = ProbeResult::Match;
1799 let cause = &self.misc(self.span);
1800 let ocx = ObligationCtxt::new_with_diagnostics(self);
1801
1802 let mut trait_predicate = None;
1803 let (mut xform_self_ty, mut xform_ret_ty);
1804
1805 match probe.kind {
1806 InherentImplCandidate { impl_def_id, .. } => {
1807 let impl_args = self.fresh_args_for_item(self.span, impl_def_id);
1808 let impl_ty = self.tcx.type_of(impl_def_id).instantiate(self.tcx, impl_args);
1809 (xform_self_ty, xform_ret_ty) =
1810 self.xform_self_ty(probe.item, impl_ty, impl_args);
1811 xform_self_ty = ocx.normalize(cause, self.param_env, xform_self_ty);
1812 match ocx.relate(cause, self.param_env, self.variance(), self_ty, xform_self_ty)
1813 {
1814 Ok(()) => {}
1815 Err(err) => {
1816 debug!("--> cannot relate self-types {:?}", err);
1817 return ProbeResult::NoMatch;
1818 }
1819 }
1820 xform_ret_ty = ocx.normalize(cause, self.param_env, xform_ret_ty);
1822 let impl_def_id = probe.item.container_id(self.tcx);
1824 let impl_bounds =
1825 self.tcx.predicates_of(impl_def_id).instantiate(self.tcx, impl_args);
1826 let impl_bounds = ocx.normalize(cause, self.param_env, impl_bounds);
1827 ocx.register_obligations(traits::predicates_for_generics(
1829 |idx, span| {
1830 let code = ObligationCauseCode::WhereClauseInExpr(
1831 impl_def_id,
1832 span,
1833 self.scope_expr_id,
1834 idx,
1835 );
1836 self.cause(self.span, code)
1837 },
1838 self.param_env,
1839 impl_bounds,
1840 ));
1841 }
1842 TraitCandidate(poly_trait_ref) => {
1843 if let Some(method_name) = self.method_name {
1846 if self_ty.is_array() && !method_name.span.at_least_rust_2021() {
1847 let trait_def = self.tcx.trait_def(poly_trait_ref.def_id());
1848 if trait_def.skip_array_during_method_dispatch {
1849 return ProbeResult::NoMatch;
1850 }
1851 }
1852
1853 if self_ty.boxed_ty().is_some_and(Ty::is_slice)
1856 && !method_name.span.at_least_rust_2024()
1857 {
1858 let trait_def = self.tcx.trait_def(poly_trait_ref.def_id());
1859 if trait_def.skip_boxed_slice_during_method_dispatch {
1860 return ProbeResult::NoMatch;
1861 }
1862 }
1863 }
1864
1865 let trait_ref = self.instantiate_binder_with_fresh_vars(
1866 self.span,
1867 BoundRegionConversionTime::FnCall,
1868 poly_trait_ref,
1869 );
1870 let trait_ref = ocx.normalize(cause, self.param_env, trait_ref);
1871 (xform_self_ty, xform_ret_ty) =
1872 self.xform_self_ty(probe.item, trait_ref.self_ty(), trait_ref.args);
1873 xform_self_ty = ocx.normalize(cause, self.param_env, xform_self_ty);
1874 match self_ty.kind() {
1875 ty::Alias(ty::Opaque, alias_ty)
1879 if !self.next_trait_solver()
1880 && self.infcx.can_define_opaque_ty(alias_ty.def_id)
1881 && !xform_self_ty.is_ty_var() =>
1882 {
1883 return ProbeResult::NoMatch;
1884 }
1885 _ => match ocx.relate(
1886 cause,
1887 self.param_env,
1888 self.variance(),
1889 self_ty,
1890 xform_self_ty,
1891 ) {
1892 Ok(()) => {}
1893 Err(err) => {
1894 debug!("--> cannot relate self-types {:?}", err);
1895 return ProbeResult::NoMatch;
1896 }
1897 },
1898 }
1899 let obligation = traits::Obligation::new(
1900 self.tcx,
1901 cause.clone(),
1902 self.param_env,
1903 ty::Binder::dummy(trait_ref),
1904 );
1905
1906 if self.infcx.next_trait_solver() || self.infcx.predicate_may_hold(&obligation)
1908 {
1909 ocx.register_obligation(obligation);
1910 } else {
1911 result = ProbeResult::NoMatch;
1912 if let Ok(Some(candidate)) = self.select_trait_candidate(trait_ref) {
1913 for nested_obligation in candidate.nested_obligations() {
1914 if !self.infcx.predicate_may_hold(&nested_obligation) {
1915 possibly_unsatisfied_predicates.push((
1916 self.resolve_vars_if_possible(nested_obligation.predicate),
1917 Some(self.resolve_vars_if_possible(obligation.predicate)),
1918 Some(nested_obligation.cause),
1919 ));
1920 }
1921 }
1922 }
1923 }
1924
1925 trait_predicate = Some(trait_ref.upcast(self.tcx));
1926 }
1927 ObjectCandidate(poly_trait_ref) | WhereClauseCandidate(poly_trait_ref) => {
1928 let trait_ref = self.instantiate_binder_with_fresh_vars(
1929 self.span,
1930 BoundRegionConversionTime::FnCall,
1931 poly_trait_ref,
1932 );
1933 (xform_self_ty, xform_ret_ty) =
1934 self.xform_self_ty(probe.item, trait_ref.self_ty(), trait_ref.args);
1935
1936 if matches!(probe.kind, WhereClauseCandidate(_)) {
1937 match ocx.structurally_normalize_ty(
1941 cause,
1942 self.param_env,
1943 trait_ref.self_ty(),
1944 ) {
1945 Ok(ty) => {
1946 if !matches!(ty.kind(), ty::Param(_)) {
1947 debug!("--> not a param ty: {xform_self_ty:?}");
1948 return ProbeResult::NoMatch;
1949 }
1950 }
1951 Err(errors) => {
1952 debug!("--> cannot relate self-types {:?}", errors);
1953 return ProbeResult::NoMatch;
1954 }
1955 }
1956 }
1957
1958 xform_self_ty = ocx.normalize(cause, self.param_env, xform_self_ty);
1959 match ocx.relate(cause, self.param_env, self.variance(), self_ty, xform_self_ty)
1960 {
1961 Ok(()) => {}
1962 Err(err) => {
1963 debug!("--> cannot relate self-types {:?}", err);
1964 return ProbeResult::NoMatch;
1965 }
1966 }
1967 }
1968 }
1969
1970 if let Some(xform_ret_ty) = xform_ret_ty
1982 && self.infcx.next_trait_solver()
1983 {
1984 ocx.register_obligation(traits::Obligation::new(
1985 self.tcx,
1986 cause.clone(),
1987 self.param_env,
1988 ty::ClauseKind::WellFormed(xform_ret_ty.into()),
1989 ));
1990 }
1991
1992 for error in ocx.select_where_possible() {
1994 result = ProbeResult::NoMatch;
1995 let nested_predicate = self.resolve_vars_if_possible(error.obligation.predicate);
1996 if let Some(trait_predicate) = trait_predicate
1997 && nested_predicate == self.resolve_vars_if_possible(trait_predicate)
1998 {
1999 } else {
2003 possibly_unsatisfied_predicates.push((
2004 nested_predicate,
2005 Some(self.resolve_vars_if_possible(error.root_obligation.predicate))
2006 .filter(|root_predicate| *root_predicate != nested_predicate),
2007 Some(error.obligation.cause),
2008 ));
2009 }
2010 }
2011
2012 if let ProbeResult::Match = result
2013 && let Some(return_ty) = self.return_type
2014 && let Some(mut xform_ret_ty) = xform_ret_ty
2015 {
2016 if !matches!(probe.kind, InherentImplCandidate { .. }) {
2021 xform_ret_ty = ocx.normalize(&cause, self.param_env, xform_ret_ty);
2022 }
2023
2024 debug!("comparing return_ty {:?} with xform ret ty {:?}", return_ty, xform_ret_ty);
2025 match ocx.relate(cause, self.param_env, self.variance(), xform_ret_ty, return_ty) {
2026 Ok(()) => {}
2027 Err(_) => {
2028 result = ProbeResult::BadReturnType;
2029 }
2030 }
2031
2032 for error in ocx.select_where_possible() {
2034 result = ProbeResult::NoMatch;
2035 possibly_unsatisfied_predicates.push((
2036 error.obligation.predicate,
2037 Some(error.root_obligation.predicate)
2038 .filter(|predicate| *predicate != error.obligation.predicate),
2039 Some(error.root_obligation.cause),
2040 ));
2041 }
2042 }
2043
2044 if let Err(_) = self.leak_check(outer_universe, Some(snapshot)) {
2050 result = ProbeResult::NoMatch;
2051 }
2052
2053 result
2054 })
2055 }
2056
2057 fn collapse_candidates_to_trait_pick(
2075 &self,
2076 self_ty: Ty<'tcx>,
2077 probes: &[(&Candidate<'tcx>, ProbeResult)],
2078 ) -> Option<Pick<'tcx>> {
2079 let container = probes[0].0.item.trait_container(self.tcx)?;
2081 for (p, _) in &probes[1..] {
2082 let p_container = p.item.trait_container(self.tcx)?;
2083 if p_container != container {
2084 return None;
2085 }
2086 }
2087
2088 Some(Pick {
2091 item: probes[0].0.item,
2092 kind: TraitPick,
2093 import_ids: probes[0].0.import_ids.clone(),
2094 autoderefs: 0,
2095 autoref_or_ptr_adjustment: None,
2096 self_ty,
2097 unstable_candidates: vec![],
2098 receiver_steps: None,
2099 shadowed_candidates: vec![],
2100 })
2101 }
2102
2103 fn collapse_candidates_to_subtrait_pick(
2109 &self,
2110 self_ty: Ty<'tcx>,
2111 probes: &[(&Candidate<'tcx>, ProbeResult)],
2112 ) -> Option<Pick<'tcx>> {
2113 let mut child_candidate = probes[0].0;
2114 let mut child_trait = child_candidate.item.trait_container(self.tcx)?;
2115 let mut supertraits: SsoHashSet<_> = supertrait_def_ids(self.tcx, child_trait).collect();
2116
2117 let mut remaining_candidates: Vec<_> = probes[1..].iter().map(|&(p, _)| p).collect();
2118 while !remaining_candidates.is_empty() {
2119 let mut made_progress = false;
2120 let mut next_round = vec![];
2121
2122 for remaining_candidate in remaining_candidates {
2123 let remaining_trait = remaining_candidate.item.trait_container(self.tcx)?;
2124 if supertraits.contains(&remaining_trait) {
2125 made_progress = true;
2126 continue;
2127 }
2128
2129 let remaining_trait_supertraits: SsoHashSet<_> =
2135 supertrait_def_ids(self.tcx, remaining_trait).collect();
2136 if remaining_trait_supertraits.contains(&child_trait) {
2137 child_candidate = remaining_candidate;
2138 child_trait = remaining_trait;
2139 supertraits = remaining_trait_supertraits;
2140 made_progress = true;
2141 continue;
2142 }
2143
2144 next_round.push(remaining_candidate);
2150 }
2151
2152 if made_progress {
2153 remaining_candidates = next_round;
2155 } else {
2156 return None;
2159 }
2160 }
2161
2162 Some(Pick {
2163 item: child_candidate.item,
2164 kind: TraitPick,
2165 import_ids: child_candidate.import_ids.clone(),
2166 autoderefs: 0,
2167 autoref_or_ptr_adjustment: None,
2168 self_ty,
2169 unstable_candidates: vec![],
2170 shadowed_candidates: probes
2171 .iter()
2172 .map(|(c, _)| c.item)
2173 .filter(|item| item.def_id != child_candidate.item.def_id)
2174 .collect(),
2175 receiver_steps: None,
2176 })
2177 }
2178
2179 #[instrument(level = "debug", skip(self))]
2183 pub(crate) fn probe_for_similar_candidate(
2184 &mut self,
2185 ) -> Result<Option<ty::AssocItem>, MethodError<'tcx>> {
2186 debug!("probing for method names similar to {:?}", self.method_name);
2187
2188 self.probe(|_| {
2189 let mut pcx = ProbeContext::new(
2190 self.fcx,
2191 self.span,
2192 self.mode,
2193 self.method_name,
2194 self.return_type,
2195 self.orig_steps_var_values,
2196 self.steps,
2197 self.scope_expr_id,
2198 IsSuggestion(true),
2199 );
2200 pcx.allow_similar_names = true;
2201 pcx.assemble_inherent_candidates();
2202 pcx.assemble_extension_candidates_for_all_traits();
2203
2204 let method_names = pcx.candidate_method_names(|_| true);
2205 pcx.allow_similar_names = false;
2206 let applicable_close_candidates: Vec<ty::AssocItem> = method_names
2207 .iter()
2208 .filter_map(|&method_name| {
2209 pcx.reset();
2210 pcx.method_name = Some(method_name);
2211 pcx.assemble_inherent_candidates();
2212 pcx.assemble_extension_candidates_for_all_traits();
2213 pcx.pick_core(&mut Vec::new()).and_then(|pick| pick.ok()).map(|pick| pick.item)
2214 })
2215 .collect();
2216
2217 if applicable_close_candidates.is_empty() {
2218 Ok(None)
2219 } else {
2220 let best_name = {
2221 let names = applicable_close_candidates
2222 .iter()
2223 .map(|cand| cand.name())
2224 .collect::<Vec<Symbol>>();
2225 find_best_match_for_name_with_substrings(
2226 &names,
2227 self.method_name.unwrap().name,
2228 None,
2229 )
2230 }
2231 .or_else(|| {
2232 applicable_close_candidates
2233 .iter()
2234 .find(|cand| self.matches_by_doc_alias(cand.def_id))
2235 .map(|cand| cand.name())
2236 });
2237 Ok(best_name.and_then(|best_name| {
2238 applicable_close_candidates
2239 .into_iter()
2240 .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.is_method(),
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.is_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
2349 if is_doc_alias_attrs_contain_symbol(attrs.into_iter(), method.name) {
2350 return true;
2351 }
2352
2353 for attr in attrs {
2354 if attr.has_name(sym::rustc_confusables) {
2355 let Some(confusables) = attr.meta_item_list() else {
2356 continue;
2357 };
2358 for n in confusables {
2360 if let Some(lit) = n.lit()
2361 && method.name == lit.symbol
2362 {
2363 return true;
2364 }
2365 }
2366 }
2367 }
2368 false
2369 }
2370
2371 fn impl_or_trait_item(&self, def_id: DefId) -> SmallVec<[ty::AssocItem; 1]> {
2376 if let Some(name) = self.method_name {
2377 if self.allow_similar_names {
2378 let max_dist = max(name.as_str().len(), 3) / 3;
2379 self.tcx
2380 .associated_items(def_id)
2381 .in_definition_order()
2382 .filter(|x| {
2383 if !self.is_relevant_kind_for_mode(x.kind) {
2384 return false;
2385 }
2386 if let Some(d) = edit_distance_with_substrings(
2387 name.as_str(),
2388 x.name().as_str(),
2389 max_dist,
2390 ) {
2391 return d > 0;
2392 }
2393 self.matches_by_doc_alias(x.def_id)
2394 })
2395 .copied()
2396 .collect()
2397 } else {
2398 self.fcx
2399 .associated_value(def_id, name)
2400 .filter(|x| self.is_relevant_kind_for_mode(x.kind))
2401 .map_or_else(SmallVec::new, |x| SmallVec::from_buf([x]))
2402 }
2403 } else {
2404 self.tcx
2405 .associated_items(def_id)
2406 .in_definition_order()
2407 .filter(|x| self.is_relevant_kind_for_mode(x.kind))
2408 .copied()
2409 .collect()
2410 }
2411 }
2412}
2413
2414impl<'tcx> Candidate<'tcx> {
2415 fn to_unadjusted_pick(
2416 &self,
2417 self_ty: Ty<'tcx>,
2418 unstable_candidates: Vec<(Candidate<'tcx>, Symbol)>,
2419 ) -> Pick<'tcx> {
2420 Pick {
2421 item: self.item,
2422 kind: match self.kind {
2423 InherentImplCandidate { .. } => InherentImplPick,
2424 ObjectCandidate(_) => ObjectPick,
2425 TraitCandidate(_) => TraitPick,
2426 WhereClauseCandidate(trait_ref) => {
2427 assert!(
2433 !trait_ref.skip_binder().args.has_infer()
2434 && !trait_ref.skip_binder().args.has_placeholders()
2435 );
2436
2437 WhereClausePick(trait_ref)
2438 }
2439 },
2440 import_ids: self.import_ids.clone(),
2441 autoderefs: 0,
2442 autoref_or_ptr_adjustment: None,
2443 self_ty,
2444 unstable_candidates,
2445 receiver_steps: match self.kind {
2446 InherentImplCandidate { receiver_steps, .. } => Some(receiver_steps),
2447 _ => None,
2448 },
2449 shadowed_candidates: vec![],
2450 }
2451 }
2452}