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