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::def::DefKind;
11use rustc_hir::{self as hir, ExprKind, HirId, Node};
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, PredicateObligation, query};
16use rustc_middle::middle::stability;
17use rustc_middle::ty::elaborate::supertrait_def_ids;
18use rustc_middle::ty::fast_reject::{DeepRejectCtxt, TreatParams, simplify_type};
19use rustc_middle::ty::{
20 self, AssocContainer, AssocItem, GenericArgs, GenericArgsRef, GenericParamDefKind, ParamEnvAnd,
21 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::solve::Goal;
33use rustc_trait_selection::traits::query::CanonicalMethodAutoderefStepsGoal;
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 UnsatisfiedPredicates<'tcx>,
169}
170
171pub(crate) type UnsatisfiedPredicates<'tcx> =
172 Vec<(ty::Predicate<'tcx>, Option<ty::Predicate<'tcx>>, Option<ObligationCause<'tcx>>)>;
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 predefined_opaques_in_body = if self.next_trait_solver() {
392 self.tcx.mk_predefined_opaques_in_body_from_iter(
393 self.inner.borrow_mut().opaque_types().iter_opaque_types().map(|(k, v)| (k, v.ty)),
394 )
395 } else {
396 ty::List::empty()
397 };
398 let value = query::MethodAutoderefSteps { predefined_opaques_in_body, self_ty };
399 let query_input = self
400 .canonicalize_query(ParamEnvAnd { param_env: self.param_env, value }, &mut orig_values);
401
402 let steps = match mode {
403 Mode::MethodCall => self.tcx.method_autoderef_steps(query_input),
404 Mode::Path => self.probe(|_| {
405 let infcx = &self.infcx;
411 let (ParamEnvAnd { param_env: _, value }, var_values) =
412 infcx.instantiate_canonical(span, &query_input.canonical);
413 let query::MethodAutoderefSteps { predefined_opaques_in_body: _, self_ty } = value;
414 debug!(?self_ty, ?query_input, "probe_op: Mode::Path");
415 MethodAutoderefStepsResult {
416 steps: infcx.tcx.arena.alloc_from_iter([CandidateStep {
417 self_ty: self
418 .make_query_response_ignoring_pending_obligations(var_values, self_ty),
419 self_ty_is_opaque: false,
420 autoderefs: 0,
421 from_unsafe_deref: false,
422 unsize: false,
423 reachable_via_deref: true,
424 }]),
425 opt_bad_ty: None,
426 reached_recursion_limit: false,
427 }
428 }),
429 };
430
431 if steps.reached_recursion_limit && !is_suggestion.0 {
435 self.probe(|_| {
436 let ty = &steps
437 .steps
438 .last()
439 .unwrap_or_else(|| span_bug!(span, "reached the recursion limit in 0 steps?"))
440 .self_ty;
441 let ty = self
442 .probe_instantiate_query_response(span, &orig_values, ty)
443 .unwrap_or_else(|_| span_bug!(span, "instantiating {:?} failed?", ty));
444 autoderef::report_autoderef_recursion_limit_error(self.tcx, span, ty.value);
445 });
446 }
447
448 if let Some(bad_ty) = &steps.opt_bad_ty {
451 if is_suggestion.0 {
452 return Err(MethodError::NoMatch(NoMatchData {
455 static_candidates: Vec::new(),
456 unsatisfied_predicates: Vec::new(),
457 out_of_scope_traits: Vec::new(),
458 similar_candidate: None,
459 mode,
460 }));
461 } else if bad_ty.reached_raw_pointer
462 && !self.tcx.features().arbitrary_self_types_pointers()
463 && !self.tcx.sess.at_least_rust_2018()
464 {
465 self.tcx.node_span_lint(
469 lint::builtin::TYVAR_BEHIND_RAW_POINTER,
470 scope_expr_id,
471 span,
472 |lint| {
473 lint.primary_message("type annotations needed");
474 },
475 );
476 } else {
477 let ty = &bad_ty.ty;
481 let ty = self
482 .probe_instantiate_query_response(span, &orig_values, ty)
483 .unwrap_or_else(|_| span_bug!(span, "instantiating {:?} failed?", ty));
484 let ty = self.resolve_vars_if_possible(ty.value);
485 let guar = match *ty.kind() {
486 ty::Infer(ty::TyVar(_)) => {
487 let err_span = match (mode, self.tcx.hir_node(scope_expr_id)) {
490 (
491 Mode::MethodCall,
492 Node::Expr(hir::Expr {
493 kind: ExprKind::MethodCall(_, recv, ..),
494 ..
495 }),
496 ) => recv.span,
497 _ => span,
498 };
499
500 let raw_ptr_call = bad_ty.reached_raw_pointer
501 && !self.tcx.features().arbitrary_self_types();
502
503 let mut err = self.err_ctxt().emit_inference_failure_err(
504 self.body_id,
505 err_span,
506 ty.into(),
507 TypeAnnotationNeeded::E0282,
508 !raw_ptr_call,
509 );
510 if raw_ptr_call {
511 err.span_label(span, "cannot call a method on a raw pointer with an unknown pointee type");
512 }
513 err.emit()
514 }
515 ty::Error(guar) => guar,
516 _ => bug!("unexpected bad final type in method autoderef"),
517 };
518 self.demand_eqtype(span, ty, Ty::new_error(self.tcx, guar));
519 return Err(MethodError::ErrorReported(guar));
520 }
521 }
522
523 debug!("ProbeContext: steps for self_ty={:?} are {:?}", self_ty, steps);
524
525 self.probe(|_| {
528 let mut probe_cx = ProbeContext::new(
529 self,
530 span,
531 mode,
532 method_name,
533 return_type,
534 &orig_values,
535 steps.steps,
536 scope_expr_id,
537 is_suggestion,
538 );
539
540 match scope {
541 ProbeScope::TraitsInScope => {
542 probe_cx.assemble_inherent_candidates();
543 probe_cx.assemble_extension_candidates_for_traits_in_scope();
544 }
545 ProbeScope::AllTraits => {
546 probe_cx.assemble_inherent_candidates();
547 probe_cx.assemble_extension_candidates_for_all_traits();
548 }
549 ProbeScope::Single(def_id) => {
550 let item = self.tcx.associated_item(def_id);
551 assert_eq!(item.container, AssocContainer::Trait);
553
554 let trait_def_id = self.tcx.parent(def_id);
555 let trait_span = self.tcx.def_span(trait_def_id);
556
557 let trait_args = self.fresh_args_for_item(trait_span, trait_def_id);
558 let trait_ref = ty::TraitRef::new_from_args(self.tcx, trait_def_id, trait_args);
559
560 probe_cx.push_candidate(
561 Candidate {
562 item,
563 kind: CandidateKind::TraitCandidate(ty::Binder::dummy(trait_ref)),
564 import_ids: smallvec![],
565 },
566 false,
567 );
568 }
569 };
570 op(probe_cx)
571 })
572 }
573}
574
575pub(crate) fn method_autoderef_steps<'tcx>(
576 tcx: TyCtxt<'tcx>,
577 goal: CanonicalMethodAutoderefStepsGoal<'tcx>,
578) -> MethodAutoderefStepsResult<'tcx> {
579 debug!("method_autoderef_steps({:?})", goal);
580
581 let (ref infcx, goal, inference_vars) = tcx.infer_ctxt().build_with_canonical(DUMMY_SP, &goal);
582 let ParamEnvAnd {
583 param_env,
584 value: query::MethodAutoderefSteps { predefined_opaques_in_body, self_ty },
585 } = goal;
586 for (key, ty) in predefined_opaques_in_body {
587 let prev = infcx
588 .register_hidden_type_in_storage(key, ty::ProvisionalHiddenType { span: DUMMY_SP, ty });
589 if let Some(prev) = prev {
601 debug!(?key, ?ty, ?prev, "ignore duplicate in `opaque_types_storage`");
602 }
603 }
604
605 let self_ty_is_opaque = |ty: Ty<'_>| {
609 if let &ty::Infer(ty::TyVar(vid)) = ty.kind() {
610 infcx.has_opaques_with_sub_unified_hidden_type(vid)
611 } else {
612 false
613 }
614 };
615
616 let mut autoderef_via_deref =
626 Autoderef::new(infcx, param_env, hir::def_id::CRATE_DEF_ID, DUMMY_SP, self_ty)
627 .include_raw_pointers()
628 .silence_errors();
629
630 let mut reached_raw_pointer = false;
631 let arbitrary_self_types_enabled =
632 tcx.features().arbitrary_self_types() || tcx.features().arbitrary_self_types_pointers();
633 let (mut steps, reached_recursion_limit): (Vec<_>, bool) = if arbitrary_self_types_enabled {
634 let reachable_via_deref =
635 autoderef_via_deref.by_ref().map(|_| true).chain(std::iter::repeat(false));
636
637 let mut autoderef_via_receiver =
638 Autoderef::new(infcx, param_env, hir::def_id::CRATE_DEF_ID, DUMMY_SP, self_ty)
639 .include_raw_pointers()
640 .use_receiver_trait()
641 .silence_errors();
642 let steps = autoderef_via_receiver
643 .by_ref()
644 .zip(reachable_via_deref)
645 .map(|((ty, d), reachable_via_deref)| {
646 let step = CandidateStep {
647 self_ty: infcx
648 .make_query_response_ignoring_pending_obligations(inference_vars, ty),
649 self_ty_is_opaque: self_ty_is_opaque(ty),
650 autoderefs: d,
651 from_unsafe_deref: reached_raw_pointer,
652 unsize: false,
653 reachable_via_deref,
654 };
655 if ty.is_raw_ptr() {
656 reached_raw_pointer = true;
658 }
659 step
660 })
661 .collect();
662 (steps, autoderef_via_receiver.reached_recursion_limit())
663 } else {
664 let steps = autoderef_via_deref
665 .by_ref()
666 .map(|(ty, d)| {
667 let step = CandidateStep {
668 self_ty: infcx
669 .make_query_response_ignoring_pending_obligations(inference_vars, ty),
670 self_ty_is_opaque: self_ty_is_opaque(ty),
671 autoderefs: d,
672 from_unsafe_deref: reached_raw_pointer,
673 unsize: false,
674 reachable_via_deref: true,
675 };
676 if ty.is_raw_ptr() {
677 reached_raw_pointer = true;
679 }
680 step
681 })
682 .collect();
683 (steps, autoderef_via_deref.reached_recursion_limit())
684 };
685 let final_ty = autoderef_via_deref.final_ty();
686 let opt_bad_ty = match final_ty.kind() {
687 ty::Infer(ty::TyVar(_)) if !self_ty_is_opaque(final_ty) => Some(MethodAutoderefBadTy {
688 reached_raw_pointer,
689 ty: infcx.make_query_response_ignoring_pending_obligations(inference_vars, final_ty),
690 }),
691 ty::Error(_) => Some(MethodAutoderefBadTy {
692 reached_raw_pointer,
693 ty: infcx.make_query_response_ignoring_pending_obligations(inference_vars, final_ty),
694 }),
695 ty::Array(elem_ty, _) => {
696 let autoderefs = steps.iter().filter(|s| s.reachable_via_deref).count() - 1;
697 steps.push(CandidateStep {
698 self_ty: infcx.make_query_response_ignoring_pending_obligations(
699 inference_vars,
700 Ty::new_slice(infcx.tcx, *elem_ty),
701 ),
702 self_ty_is_opaque: false,
703 autoderefs,
704 from_unsafe_deref: reached_raw_pointer,
707 unsize: true,
708 reachable_via_deref: true, });
711
712 None
713 }
714 _ => None,
715 };
716
717 debug!("method_autoderef_steps: steps={:?} opt_bad_ty={:?}", steps, opt_bad_ty);
718 let _ = infcx.take_opaque_types();
720 MethodAutoderefStepsResult {
721 steps: tcx.arena.alloc_from_iter(steps),
722 opt_bad_ty: opt_bad_ty.map(|ty| &*tcx.arena.alloc(ty)),
723 reached_recursion_limit,
724 }
725}
726
727impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
728 fn new(
729 fcx: &'a FnCtxt<'a, 'tcx>,
730 span: Span,
731 mode: Mode,
732 method_name: Option<Ident>,
733 return_type: Option<Ty<'tcx>>,
734 orig_steps_var_values: &'a OriginalQueryValues<'tcx>,
735 steps: &'tcx [CandidateStep<'tcx>],
736 scope_expr_id: HirId,
737 is_suggestion: IsSuggestion,
738 ) -> ProbeContext<'a, 'tcx> {
739 ProbeContext {
740 fcx,
741 span,
742 mode,
743 method_name,
744 return_type,
745 inherent_candidates: Vec::new(),
746 extension_candidates: Vec::new(),
747 impl_dups: FxHashSet::default(),
748 orig_steps_var_values,
749 steps,
750 allow_similar_names: false,
751 private_candidates: Vec::new(),
752 private_candidate: Cell::new(None),
753 static_candidates: RefCell::new(Vec::new()),
754 scope_expr_id,
755 is_suggestion,
756 }
757 }
758
759 fn reset(&mut self) {
760 self.inherent_candidates.clear();
761 self.extension_candidates.clear();
762 self.impl_dups.clear();
763 self.private_candidates.clear();
764 self.private_candidate.set(None);
765 self.static_candidates.borrow_mut().clear();
766 }
767
768 fn variance(&self) -> ty::Variance {
772 match self.mode {
773 Mode::MethodCall => ty::Covariant,
774 Mode::Path => ty::Invariant,
775 }
776 }
777
778 fn push_candidate(&mut self, candidate: Candidate<'tcx>, is_inherent: bool) {
782 let is_accessible = if let Some(name) = self.method_name {
783 let item = candidate.item;
784 let hir_id = self.tcx.local_def_id_to_hir_id(self.body_id);
785 let def_scope =
786 self.tcx.adjust_ident_and_get_scope(name, item.container_id(self.tcx), hir_id).1;
787 item.visibility(self.tcx).is_accessible_from(def_scope, self.tcx)
788 } else {
789 true
790 };
791 if is_accessible {
792 if is_inherent {
793 self.inherent_candidates.push(candidate);
794 } else {
795 self.extension_candidates.push(candidate);
796 }
797 } else {
798 self.private_candidates.push(candidate);
799 }
800 }
801
802 fn assemble_inherent_candidates(&mut self) {
803 for step in self.steps.iter() {
804 self.assemble_probe(&step.self_ty, step.autoderefs);
805 }
806 }
807
808 #[instrument(level = "debug", skip(self))]
809 fn assemble_probe(
810 &mut self,
811 self_ty: &Canonical<'tcx, QueryResponse<'tcx, Ty<'tcx>>>,
812 receiver_steps: usize,
813 ) {
814 let raw_self_ty = self_ty.value.value;
815 match *raw_self_ty.kind() {
816 ty::Dynamic(data, ..) if let Some(p) = data.principal() => {
817 let (QueryResponse { value: generalized_self_ty, .. }, _ignored_var_values) =
835 self.fcx.instantiate_canonical(self.span, self_ty);
836
837 self.assemble_inherent_candidates_from_object(generalized_self_ty);
838 self.assemble_inherent_impl_candidates_for_type(p.def_id(), receiver_steps);
839 self.assemble_inherent_candidates_for_incoherent_ty(raw_self_ty, receiver_steps);
840 }
841 ty::Adt(def, _) => {
842 let def_id = def.did();
843 self.assemble_inherent_impl_candidates_for_type(def_id, receiver_steps);
844 self.assemble_inherent_candidates_for_incoherent_ty(raw_self_ty, receiver_steps);
845 }
846 ty::Foreign(did) => {
847 self.assemble_inherent_impl_candidates_for_type(did, receiver_steps);
848 self.assemble_inherent_candidates_for_incoherent_ty(raw_self_ty, receiver_steps);
849 }
850 ty::Param(_) => {
851 self.assemble_inherent_candidates_from_param(raw_self_ty);
852 }
853 ty::Bool
854 | ty::Char
855 | ty::Int(_)
856 | ty::Uint(_)
857 | ty::Float(_)
858 | ty::Str
859 | ty::Array(..)
860 | ty::Slice(_)
861 | ty::RawPtr(_, _)
862 | ty::Ref(..)
863 | ty::Never
864 | ty::Tuple(..) => {
865 self.assemble_inherent_candidates_for_incoherent_ty(raw_self_ty, receiver_steps)
866 }
867 _ => {}
868 }
869 }
870
871 fn assemble_inherent_candidates_for_incoherent_ty(
872 &mut self,
873 self_ty: Ty<'tcx>,
874 receiver_steps: usize,
875 ) {
876 let Some(simp) = simplify_type(self.tcx, self_ty, TreatParams::InstantiateWithInfer) else {
877 bug!("unexpected incoherent type: {:?}", self_ty)
878 };
879 for &impl_def_id in self.tcx.incoherent_impls(simp).into_iter() {
880 self.assemble_inherent_impl_probe(impl_def_id, receiver_steps);
881 }
882 }
883
884 fn assemble_inherent_impl_candidates_for_type(&mut self, def_id: DefId, receiver_steps: usize) {
885 let impl_def_ids = self.tcx.at(self.span).inherent_impls(def_id).into_iter();
886 for &impl_def_id in impl_def_ids {
887 self.assemble_inherent_impl_probe(impl_def_id, receiver_steps);
888 }
889 }
890
891 #[instrument(level = "debug", skip(self))]
892 fn assemble_inherent_impl_probe(&mut self, impl_def_id: DefId, receiver_steps: usize) {
893 if !self.impl_dups.insert(impl_def_id) {
894 return; }
896
897 for item in self.impl_or_trait_item(impl_def_id) {
898 if !self.has_applicable_self(&item) {
899 self.record_static_candidate(CandidateSource::Impl(impl_def_id));
901 continue;
902 }
903 self.push_candidate(
904 Candidate {
905 item,
906 kind: InherentImplCandidate { impl_def_id, receiver_steps },
907 import_ids: smallvec![],
908 },
909 true,
910 );
911 }
912 }
913
914 #[instrument(level = "debug", skip(self))]
915 fn assemble_inherent_candidates_from_object(&mut self, self_ty: Ty<'tcx>) {
916 let principal = match self_ty.kind() {
917 ty::Dynamic(data, ..) => Some(data),
918 _ => None,
919 }
920 .and_then(|data| data.principal())
921 .unwrap_or_else(|| {
922 span_bug!(
923 self.span,
924 "non-object {:?} in assemble_inherent_candidates_from_object",
925 self_ty
926 )
927 });
928
929 let trait_ref = principal.with_self_ty(self.tcx, self_ty);
936 self.assemble_candidates_for_bounds(
937 traits::supertraits(self.tcx, trait_ref),
938 |this, new_trait_ref, item| {
939 this.push_candidate(
940 Candidate {
941 item,
942 kind: ObjectCandidate(new_trait_ref),
943 import_ids: smallvec![],
944 },
945 true,
946 );
947 },
948 );
949 }
950
951 #[instrument(level = "debug", skip(self))]
952 fn assemble_inherent_candidates_from_param(&mut self, param_ty: Ty<'tcx>) {
953 debug_assert_matches!(param_ty.kind(), ty::Param(_));
954
955 let tcx = self.tcx;
956
957 let bounds = self.param_env.caller_bounds().iter().filter_map(|predicate| {
961 let bound_predicate = predicate.kind();
962 match bound_predicate.skip_binder() {
963 ty::ClauseKind::Trait(trait_predicate) => DeepRejectCtxt::relate_rigid_rigid(tcx)
964 .types_may_unify(param_ty, trait_predicate.trait_ref.self_ty())
965 .then(|| bound_predicate.rebind(trait_predicate.trait_ref)),
966 ty::ClauseKind::RegionOutlives(_)
967 | ty::ClauseKind::TypeOutlives(_)
968 | ty::ClauseKind::Projection(_)
969 | ty::ClauseKind::ConstArgHasType(_, _)
970 | ty::ClauseKind::WellFormed(_)
971 | ty::ClauseKind::ConstEvaluatable(_)
972 | ty::ClauseKind::UnstableFeature(_)
973 | ty::ClauseKind::HostEffect(..) => None,
974 }
975 });
976
977 self.assemble_candidates_for_bounds(bounds, |this, poly_trait_ref, item| {
978 this.push_candidate(
979 Candidate {
980 item,
981 kind: WhereClauseCandidate(poly_trait_ref),
982 import_ids: smallvec![],
983 },
984 true,
985 );
986 });
987 }
988
989 fn assemble_candidates_for_bounds<F>(
992 &mut self,
993 bounds: impl Iterator<Item = ty::PolyTraitRef<'tcx>>,
994 mut mk_cand: F,
995 ) where
996 F: for<'b> FnMut(&mut ProbeContext<'b, 'tcx>, ty::PolyTraitRef<'tcx>, ty::AssocItem),
997 {
998 for bound_trait_ref in bounds {
999 debug!("elaborate_bounds(bound_trait_ref={:?})", bound_trait_ref);
1000 for item in self.impl_or_trait_item(bound_trait_ref.def_id()) {
1001 if !self.has_applicable_self(&item) {
1002 self.record_static_candidate(CandidateSource::Trait(bound_trait_ref.def_id()));
1003 } else {
1004 mk_cand(self, bound_trait_ref, item);
1005 }
1006 }
1007 }
1008 }
1009
1010 #[instrument(level = "debug", skip(self))]
1011 fn assemble_extension_candidates_for_traits_in_scope(&mut self) {
1012 let mut duplicates = FxHashSet::default();
1013 let opt_applicable_traits = self.tcx.in_scope_traits(self.scope_expr_id);
1014 if let Some(applicable_traits) = opt_applicable_traits {
1015 for trait_candidate in applicable_traits.iter() {
1016 let trait_did = trait_candidate.def_id;
1017 if duplicates.insert(trait_did) {
1018 self.assemble_extension_candidates_for_trait(
1019 &trait_candidate.import_ids,
1020 trait_did,
1021 );
1022 }
1023 }
1024 }
1025 }
1026
1027 #[instrument(level = "debug", skip(self))]
1028 fn assemble_extension_candidates_for_all_traits(&mut self) {
1029 let mut duplicates = FxHashSet::default();
1030 for trait_info in suggest::all_traits(self.tcx) {
1031 if duplicates.insert(trait_info.def_id) {
1032 self.assemble_extension_candidates_for_trait(&smallvec![], trait_info.def_id);
1033 }
1034 }
1035 }
1036
1037 fn matches_return_type(&self, method: ty::AssocItem, expected: Ty<'tcx>) -> bool {
1038 match method.kind {
1039 ty::AssocKind::Fn { .. } => self.probe(|_| {
1040 let args = self.fresh_args_for_item(self.span, method.def_id);
1041 let fty = self.tcx.fn_sig(method.def_id).instantiate(self.tcx, args);
1042 let fty = self.instantiate_binder_with_fresh_vars(
1043 self.span,
1044 BoundRegionConversionTime::FnCall,
1045 fty,
1046 );
1047 self.can_eq(self.param_env, fty.output(), expected)
1048 }),
1049 _ => false,
1050 }
1051 }
1052
1053 #[instrument(level = "debug", skip(self))]
1054 fn assemble_extension_candidates_for_trait(
1055 &mut self,
1056 import_ids: &SmallVec<[LocalDefId; 1]>,
1057 trait_def_id: DefId,
1058 ) {
1059 let trait_args = self.fresh_args_for_item(self.span, trait_def_id);
1060 let trait_ref = ty::TraitRef::new_from_args(self.tcx, trait_def_id, trait_args);
1061
1062 if self.tcx.is_trait_alias(trait_def_id) {
1063 for (bound_trait_pred, _) in
1065 traits::expand_trait_aliases(self.tcx, [(trait_ref.upcast(self.tcx), self.span)]).0
1066 {
1067 assert_eq!(bound_trait_pred.polarity(), ty::PredicatePolarity::Positive);
1068 let bound_trait_ref = bound_trait_pred.map_bound(|pred| pred.trait_ref);
1069 for item in self.impl_or_trait_item(bound_trait_ref.def_id()) {
1070 if !self.has_applicable_self(&item) {
1071 self.record_static_candidate(CandidateSource::Trait(
1072 bound_trait_ref.def_id(),
1073 ));
1074 } else {
1075 self.push_candidate(
1076 Candidate {
1077 item,
1078 import_ids: import_ids.clone(),
1079 kind: TraitCandidate(bound_trait_ref),
1080 },
1081 false,
1082 );
1083 }
1084 }
1085 }
1086 } else {
1087 debug_assert!(self.tcx.is_trait(trait_def_id));
1088 if self.tcx.trait_is_auto(trait_def_id) {
1089 return;
1090 }
1091 for item in self.impl_or_trait_item(trait_def_id) {
1092 if !self.has_applicable_self(&item) {
1094 debug!("method has inapplicable self");
1095 self.record_static_candidate(CandidateSource::Trait(trait_def_id));
1096 continue;
1097 }
1098 self.push_candidate(
1099 Candidate {
1100 item,
1101 import_ids: import_ids.clone(),
1102 kind: TraitCandidate(ty::Binder::dummy(trait_ref)),
1103 },
1104 false,
1105 );
1106 }
1107 }
1108 }
1109
1110 fn candidate_method_names(
1111 &self,
1112 candidate_filter: impl Fn(&ty::AssocItem) -> bool,
1113 ) -> Vec<Ident> {
1114 let mut set = FxHashSet::default();
1115 let mut names: Vec<_> = self
1116 .inherent_candidates
1117 .iter()
1118 .chain(&self.extension_candidates)
1119 .filter(|candidate| candidate_filter(&candidate.item))
1120 .filter(|candidate| {
1121 if let Some(return_ty) = self.return_type {
1122 self.matches_return_type(candidate.item, return_ty)
1123 } else {
1124 true
1125 }
1126 })
1127 .filter(|candidate| {
1129 !matches!(
1132 self.tcx.eval_stability(candidate.item.def_id, None, DUMMY_SP, None),
1133 stability::EvalResult::Deny { .. }
1134 )
1135 })
1136 .map(|candidate| candidate.item.ident(self.tcx))
1137 .filter(|&name| set.insert(name))
1138 .collect();
1139
1140 names.sort_by(|a, b| a.as_str().cmp(b.as_str()));
1142 names
1143 }
1144
1145 #[instrument(level = "debug", skip(self))]
1149 fn pick(mut self) -> PickResult<'tcx> {
1150 assert!(self.method_name.is_some());
1151
1152 let mut unsatisfied_predicates = Vec::new();
1153
1154 if let Some(r) = self.pick_core(&mut unsatisfied_predicates) {
1155 return r;
1156 }
1157
1158 if self.is_suggestion.0 {
1161 return Err(MethodError::NoMatch(NoMatchData {
1162 static_candidates: vec![],
1163 unsatisfied_predicates: vec![],
1164 out_of_scope_traits: vec![],
1165 similar_candidate: None,
1166 mode: self.mode,
1167 }));
1168 }
1169
1170 debug!("pick: actual search failed, assemble diagnostics");
1171
1172 let static_candidates = std::mem::take(self.static_candidates.get_mut());
1173 let private_candidate = self.private_candidate.take();
1174
1175 self.reset();
1177
1178 self.assemble_extension_candidates_for_all_traits();
1179
1180 let out_of_scope_traits = match self.pick_core(&mut Vec::new()) {
1181 Some(Ok(p)) => vec![p.item.container_id(self.tcx)],
1182 Some(Err(MethodError::Ambiguity(v))) => v
1183 .into_iter()
1184 .map(|source| match source {
1185 CandidateSource::Trait(id) => id,
1186 CandidateSource::Impl(impl_id) => self.tcx.impl_trait_id(impl_id),
1187 })
1188 .collect(),
1189 Some(Err(MethodError::NoMatch(NoMatchData {
1190 out_of_scope_traits: others, ..
1191 }))) => {
1192 assert!(others.is_empty());
1193 vec![]
1194 }
1195 _ => vec![],
1196 };
1197
1198 if let Some((kind, def_id)) = private_candidate {
1199 return Err(MethodError::PrivateMatch(kind, def_id, out_of_scope_traits));
1200 }
1201 let similar_candidate = self.probe_for_similar_candidate()?;
1202
1203 Err(MethodError::NoMatch(NoMatchData {
1204 static_candidates,
1205 unsatisfied_predicates,
1206 out_of_scope_traits,
1207 similar_candidate,
1208 mode: self.mode,
1209 }))
1210 }
1211
1212 fn pick_core(
1213 &self,
1214 unsatisfied_predicates: &mut UnsatisfiedPredicates<'tcx>,
1215 ) -> Option<PickResult<'tcx>> {
1216 self.pick_all_method(&mut PickDiagHints {
1218 unstable_candidates: Some(Vec::new()),
1221 unsatisfied_predicates,
1224 })
1225 .or_else(|| {
1226 self.pick_all_method(&mut PickDiagHints {
1227 unstable_candidates: None,
1232 unsatisfied_predicates: &mut Vec::new(),
1235 })
1236 })
1237 }
1238
1239 fn pick_all_method<'b>(
1240 &self,
1241 pick_diag_hints: &mut PickDiagHints<'b, 'tcx>,
1242 ) -> Option<PickResult<'tcx>> {
1243 let track_unstable_candidates = pick_diag_hints.unstable_candidates.is_some();
1244 self.steps
1245 .iter()
1246 .filter(|step| step.reachable_via_deref)
1250 .filter(|step| {
1251 debug!("pick_all_method: step={:?}", step);
1252 !step.self_ty.value.references_error() && !step.from_unsafe_deref
1255 })
1256 .find_map(|step| {
1257 let InferOk { value: self_ty, obligations: instantiate_self_ty_obligations } = self
1258 .fcx
1259 .probe_instantiate_query_response(
1260 self.span,
1261 self.orig_steps_var_values,
1262 &step.self_ty,
1263 )
1264 .unwrap_or_else(|_| {
1265 span_bug!(self.span, "{:?} was applicable but now isn't?", step.self_ty)
1266 });
1267
1268 let by_value_pick = self.pick_by_value_method(
1269 step,
1270 self_ty,
1271 &instantiate_self_ty_obligations,
1272 pick_diag_hints,
1273 );
1274
1275 if let Some(by_value_pick) = by_value_pick {
1277 if let Ok(by_value_pick) = by_value_pick.as_ref() {
1278 if by_value_pick.kind == PickKind::InherentImplPick {
1279 for mutbl in [hir::Mutability::Not, hir::Mutability::Mut] {
1280 if let Err(e) = self.check_for_shadowed_autorefd_method(
1281 by_value_pick,
1282 step,
1283 self_ty,
1284 &instantiate_self_ty_obligations,
1285 mutbl,
1286 track_unstable_candidates,
1287 ) {
1288 return Some(Err(e));
1289 }
1290 }
1291 }
1292 }
1293 return Some(by_value_pick);
1294 }
1295
1296 let autoref_pick = self.pick_autorefd_method(
1297 step,
1298 self_ty,
1299 &instantiate_self_ty_obligations,
1300 hir::Mutability::Not,
1301 pick_diag_hints,
1302 None,
1303 );
1304 if let Some(autoref_pick) = autoref_pick {
1306 if let Ok(autoref_pick) = autoref_pick.as_ref() {
1307 if autoref_pick.kind == PickKind::InherentImplPick {
1309 if let Err(e) = self.check_for_shadowed_autorefd_method(
1310 autoref_pick,
1311 step,
1312 self_ty,
1313 &instantiate_self_ty_obligations,
1314 hir::Mutability::Mut,
1315 track_unstable_candidates,
1316 ) {
1317 return Some(Err(e));
1318 }
1319 }
1320 }
1321 return Some(autoref_pick);
1322 }
1323
1324 self.pick_autorefd_method(
1348 step,
1349 self_ty,
1350 &instantiate_self_ty_obligations,
1351 hir::Mutability::Mut,
1352 pick_diag_hints,
1353 None,
1354 )
1355 .or_else(|| {
1356 self.pick_const_ptr_method(
1357 step,
1358 self_ty,
1359 &instantiate_self_ty_obligations,
1360 pick_diag_hints,
1361 )
1362 })
1363 .or_else(|| {
1364 self.pick_reborrow_pin_method(
1365 step,
1366 self_ty,
1367 &instantiate_self_ty_obligations,
1368 pick_diag_hints,
1369 )
1370 })
1371 })
1372 }
1373
1374 fn check_for_shadowed_autorefd_method(
1390 &self,
1391 possible_shadower: &Pick<'tcx>,
1392 step: &CandidateStep<'tcx>,
1393 self_ty: Ty<'tcx>,
1394 instantiate_self_ty_obligations: &[PredicateObligation<'tcx>],
1395 mutbl: hir::Mutability,
1396 track_unstable_candidates: bool,
1397 ) -> Result<(), MethodError<'tcx>> {
1398 if !self.tcx.features().arbitrary_self_types()
1402 && !self.tcx.features().arbitrary_self_types_pointers()
1403 {
1404 return Ok(());
1405 }
1406
1407 let mut pick_diag_hints = PickDiagHints {
1412 unstable_candidates: if track_unstable_candidates { Some(Vec::new()) } else { None },
1413 unsatisfied_predicates: &mut Vec::new(),
1414 };
1415 let pick_constraints = PickConstraintsForShadowed {
1417 autoderefs: possible_shadower.autoderefs,
1419 receiver_steps: possible_shadower.receiver_steps,
1423 def_id: possible_shadower.item.def_id,
1426 };
1427 let potentially_shadowed_pick = self.pick_autorefd_method(
1457 step,
1458 self_ty,
1459 instantiate_self_ty_obligations,
1460 mutbl,
1461 &mut pick_diag_hints,
1462 Some(&pick_constraints),
1463 );
1464 if let Some(Ok(possible_shadowed)) = potentially_shadowed_pick.as_ref() {
1467 let sources = [possible_shadower, possible_shadowed]
1468 .into_iter()
1469 .map(|p| self.candidate_source_from_pick(p))
1470 .collect();
1471 return Err(MethodError::Ambiguity(sources));
1472 }
1473 Ok(())
1474 }
1475
1476 fn pick_by_value_method(
1483 &self,
1484 step: &CandidateStep<'tcx>,
1485 self_ty: Ty<'tcx>,
1486 instantiate_self_ty_obligations: &[PredicateObligation<'tcx>],
1487 pick_diag_hints: &mut PickDiagHints<'_, 'tcx>,
1488 ) -> Option<PickResult<'tcx>> {
1489 if step.unsize {
1490 return None;
1491 }
1492
1493 self.pick_method(self_ty, instantiate_self_ty_obligations, pick_diag_hints, None).map(|r| {
1494 r.map(|mut pick| {
1495 pick.autoderefs = step.autoderefs;
1496
1497 match *step.self_ty.value.value.kind() {
1498 ty::Ref(_, _, mutbl) => {
1500 pick.autoderefs += 1;
1501 pick.autoref_or_ptr_adjustment = Some(AutorefOrPtrAdjustment::Autoref {
1502 mutbl,
1503 unsize: pick.autoref_or_ptr_adjustment.is_some_and(|a| a.get_unsize()),
1504 })
1505 }
1506
1507 ty::Adt(def, args)
1508 if self.tcx.features().pin_ergonomics()
1509 && self.tcx.is_lang_item(def.did(), hir::LangItem::Pin) =>
1510 {
1511 if let ty::Ref(_, _, mutbl) = args[0].expect_ty().kind() {
1513 pick.autoref_or_ptr_adjustment =
1514 Some(AutorefOrPtrAdjustment::ReborrowPin(*mutbl));
1515 }
1516 }
1517
1518 _ => (),
1519 }
1520
1521 pick
1522 })
1523 })
1524 }
1525
1526 fn pick_autorefd_method(
1527 &self,
1528 step: &CandidateStep<'tcx>,
1529 self_ty: Ty<'tcx>,
1530 instantiate_self_ty_obligations: &[PredicateObligation<'tcx>],
1531 mutbl: hir::Mutability,
1532 pick_diag_hints: &mut PickDiagHints<'_, 'tcx>,
1533 pick_constraints: Option<&PickConstraintsForShadowed>,
1534 ) -> Option<PickResult<'tcx>> {
1535 let tcx = self.tcx;
1536
1537 if let Some(pick_constraints) = pick_constraints {
1538 if !pick_constraints.may_shadow_based_on_autoderefs(step.autoderefs) {
1539 return None;
1540 }
1541 }
1542
1543 let region = tcx.lifetimes.re_erased;
1545
1546 let autoref_ty = Ty::new_ref(tcx, region, self_ty, mutbl);
1547 self.pick_method(
1548 autoref_ty,
1549 instantiate_self_ty_obligations,
1550 pick_diag_hints,
1551 pick_constraints,
1552 )
1553 .map(|r| {
1554 r.map(|mut pick| {
1555 pick.autoderefs = step.autoderefs;
1556 pick.autoref_or_ptr_adjustment =
1557 Some(AutorefOrPtrAdjustment::Autoref { mutbl, unsize: step.unsize });
1558 pick
1559 })
1560 })
1561 }
1562
1563 #[instrument(level = "debug", skip(self, step, pick_diag_hints))]
1565 fn pick_reborrow_pin_method(
1566 &self,
1567 step: &CandidateStep<'tcx>,
1568 self_ty: Ty<'tcx>,
1569 instantiate_self_ty_obligations: &[PredicateObligation<'tcx>],
1570 pick_diag_hints: &mut PickDiagHints<'_, 'tcx>,
1571 ) -> Option<PickResult<'tcx>> {
1572 if !self.tcx.features().pin_ergonomics() {
1573 return None;
1574 }
1575
1576 let inner_ty = match self_ty.kind() {
1578 ty::Adt(def, args) if self.tcx.is_lang_item(def.did(), hir::LangItem::Pin) => {
1579 match args[0].expect_ty().kind() {
1580 ty::Ref(_, ty, hir::Mutability::Mut) => *ty,
1581 _ => {
1582 return None;
1583 }
1584 }
1585 }
1586 _ => return None,
1587 };
1588
1589 let region = self.tcx.lifetimes.re_erased;
1590 let autopin_ty = Ty::new_pinned_ref(self.tcx, region, inner_ty, hir::Mutability::Not);
1591 self.pick_method(autopin_ty, instantiate_self_ty_obligations, pick_diag_hints, None).map(
1592 |r| {
1593 r.map(|mut pick| {
1594 pick.autoderefs = step.autoderefs;
1595 pick.autoref_or_ptr_adjustment =
1596 Some(AutorefOrPtrAdjustment::ReborrowPin(hir::Mutability::Not));
1597 pick
1598 })
1599 },
1600 )
1601 }
1602
1603 fn pick_const_ptr_method(
1607 &self,
1608 step: &CandidateStep<'tcx>,
1609 self_ty: Ty<'tcx>,
1610 instantiate_self_ty_obligations: &[PredicateObligation<'tcx>],
1611 pick_diag_hints: &mut PickDiagHints<'_, 'tcx>,
1612 ) -> Option<PickResult<'tcx>> {
1613 if step.unsize {
1615 return None;
1616 }
1617
1618 let &ty::RawPtr(ty, hir::Mutability::Mut) = self_ty.kind() else {
1619 return None;
1620 };
1621
1622 let const_ptr_ty = Ty::new_imm_ptr(self.tcx, ty);
1623 self.pick_method(const_ptr_ty, instantiate_self_ty_obligations, pick_diag_hints, None).map(
1624 |r| {
1625 r.map(|mut pick| {
1626 pick.autoderefs = step.autoderefs;
1627 pick.autoref_or_ptr_adjustment = Some(AutorefOrPtrAdjustment::ToConstPtr);
1628 pick
1629 })
1630 },
1631 )
1632 }
1633
1634 fn pick_method(
1635 &self,
1636 self_ty: Ty<'tcx>,
1637 instantiate_self_ty_obligations: &[PredicateObligation<'tcx>],
1638 pick_diag_hints: &mut PickDiagHints<'_, 'tcx>,
1639 pick_constraints: Option<&PickConstraintsForShadowed>,
1640 ) -> Option<PickResult<'tcx>> {
1641 debug!("pick_method(self_ty={})", self.ty_to_string(self_ty));
1642
1643 for (kind, candidates) in
1644 [("inherent", &self.inherent_candidates), ("extension", &self.extension_candidates)]
1645 {
1646 debug!("searching {} candidates", kind);
1647 let res = self.consider_candidates(
1648 self_ty,
1649 instantiate_self_ty_obligations,
1650 candidates,
1651 pick_diag_hints,
1652 pick_constraints,
1653 );
1654 if let Some(pick) = res {
1655 return Some(pick);
1656 }
1657 }
1658
1659 if self.private_candidate.get().is_none() {
1660 if let Some(Ok(pick)) = self.consider_candidates(
1661 self_ty,
1662 instantiate_self_ty_obligations,
1663 &self.private_candidates,
1664 &mut PickDiagHints {
1665 unstable_candidates: None,
1666 unsatisfied_predicates: &mut vec![],
1667 },
1668 None,
1669 ) {
1670 self.private_candidate.set(Some((pick.item.as_def_kind(), pick.item.def_id)));
1671 }
1672 }
1673 None
1674 }
1675
1676 fn consider_candidates(
1677 &self,
1678 self_ty: Ty<'tcx>,
1679 instantiate_self_ty_obligations: &[PredicateObligation<'tcx>],
1680 candidates: &[Candidate<'tcx>],
1681 pick_diag_hints: &mut PickDiagHints<'_, 'tcx>,
1682 pick_constraints: Option<&PickConstraintsForShadowed>,
1683 ) -> Option<PickResult<'tcx>> {
1684 let mut applicable_candidates: Vec<_> = candidates
1685 .iter()
1686 .filter(|candidate| {
1687 pick_constraints
1688 .map(|pick_constraints| pick_constraints.candidate_may_shadow(&candidate))
1689 .unwrap_or(true)
1690 })
1691 .map(|probe| {
1692 (
1693 probe,
1694 self.consider_probe(
1695 self_ty,
1696 instantiate_self_ty_obligations,
1697 probe,
1698 &mut pick_diag_hints.unsatisfied_predicates,
1699 ),
1700 )
1701 })
1702 .filter(|&(_, status)| status != ProbeResult::NoMatch)
1703 .collect();
1704
1705 debug!("applicable_candidates: {:?}", applicable_candidates);
1706
1707 if applicable_candidates.len() > 1 {
1708 if let Some(pick) =
1709 self.collapse_candidates_to_trait_pick(self_ty, &applicable_candidates)
1710 {
1711 return Some(Ok(pick));
1712 }
1713 }
1714
1715 if let Some(uc) = &mut pick_diag_hints.unstable_candidates {
1716 applicable_candidates.retain(|&(candidate, _)| {
1717 if let stability::EvalResult::Deny { feature, .. } =
1718 self.tcx.eval_stability(candidate.item.def_id, None, self.span, None)
1719 {
1720 uc.push((candidate.clone(), feature));
1721 return false;
1722 }
1723 true
1724 });
1725 }
1726
1727 if applicable_candidates.len() > 1 {
1728 if self.tcx.features().supertrait_item_shadowing() {
1732 if let Some(pick) =
1733 self.collapse_candidates_to_subtrait_pick(self_ty, &applicable_candidates)
1734 {
1735 return Some(Ok(pick));
1736 }
1737 }
1738
1739 let sources =
1740 applicable_candidates.iter().map(|p| self.candidate_source(p.0, self_ty)).collect();
1741 return Some(Err(MethodError::Ambiguity(sources)));
1742 }
1743
1744 applicable_candidates.pop().map(|(probe, status)| match status {
1745 ProbeResult::Match => Ok(probe.to_unadjusted_pick(
1746 self_ty,
1747 pick_diag_hints.unstable_candidates.clone().unwrap_or_default(),
1748 )),
1749 ProbeResult::NoMatch | ProbeResult::BadReturnType => Err(MethodError::BadReturnType),
1750 })
1751 }
1752}
1753
1754impl<'tcx> Pick<'tcx> {
1755 pub(crate) fn differs_from(&self, other: &Self) -> bool {
1760 let Self {
1761 item: AssocItem { def_id, kind: _, container: _ },
1762 kind: _,
1763 import_ids: _,
1764 autoderefs: _,
1765 autoref_or_ptr_adjustment: _,
1766 self_ty,
1767 unstable_candidates: _,
1768 receiver_steps: _,
1769 shadowed_candidates: _,
1770 } = *self;
1771 self_ty != other.self_ty || def_id != other.item.def_id
1772 }
1773
1774 pub(crate) fn maybe_emit_unstable_name_collision_hint(
1776 &self,
1777 tcx: TyCtxt<'tcx>,
1778 span: Span,
1779 scope_expr_id: HirId,
1780 ) {
1781 if self.unstable_candidates.is_empty() {
1782 return;
1783 }
1784 let def_kind = self.item.as_def_kind();
1785 tcx.node_span_lint(lint::builtin::UNSTABLE_NAME_COLLISIONS, scope_expr_id, span, |lint| {
1786 lint.primary_message(format!(
1787 "{} {} with this name may be added to the standard library in the future",
1788 tcx.def_kind_descr_article(def_kind, self.item.def_id),
1789 tcx.def_kind_descr(def_kind, self.item.def_id),
1790 ));
1791
1792 match (self.item.kind, self.item.container) {
1793 (ty::AssocKind::Fn { .. }, _) => {
1794 lint.help(format!(
1799 "call with fully qualified syntax `{}(...)` to keep using the current \
1800 method",
1801 tcx.def_path_str(self.item.def_id),
1802 ));
1803 }
1804 (ty::AssocKind::Const { name }, ty::AssocContainer::Trait) => {
1805 let def_id = self.item.container_id(tcx);
1806 lint.span_suggestion(
1807 span,
1808 "use the fully qualified path to the associated const",
1809 format!("<{} as {}>::{}", self.self_ty, tcx.def_path_str(def_id), name),
1810 Applicability::MachineApplicable,
1811 );
1812 }
1813 _ => {}
1814 }
1815 tcx.disabled_nightly_features(
1816 lint,
1817 self.unstable_candidates.iter().map(|(candidate, feature)| {
1818 (format!(" `{}`", tcx.def_path_str(candidate.item.def_id)), *feature)
1819 }),
1820 );
1821 });
1822 }
1823}
1824
1825impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
1826 fn select_trait_candidate(
1827 &self,
1828 trait_ref: ty::TraitRef<'tcx>,
1829 ) -> traits::SelectionResult<'tcx, traits::Selection<'tcx>> {
1830 let obligation =
1831 traits::Obligation::new(self.tcx, self.misc(self.span), self.param_env, trait_ref);
1832 traits::SelectionContext::new(self).select(&obligation)
1833 }
1834
1835 fn candidate_source(&self, candidate: &Candidate<'tcx>, self_ty: Ty<'tcx>) -> CandidateSource {
1838 match candidate.kind {
1839 InherentImplCandidate { .. } => {
1840 CandidateSource::Impl(candidate.item.container_id(self.tcx))
1841 }
1842 ObjectCandidate(_) | WhereClauseCandidate(_) => {
1843 CandidateSource::Trait(candidate.item.container_id(self.tcx))
1844 }
1845 TraitCandidate(trait_ref) => self.probe(|_| {
1846 let trait_ref = self.instantiate_binder_with_fresh_vars(
1847 self.span,
1848 BoundRegionConversionTime::FnCall,
1849 trait_ref,
1850 );
1851 let (xform_self_ty, _) =
1852 self.xform_self_ty(candidate.item, trait_ref.self_ty(), trait_ref.args);
1853 let _ = self.at(&ObligationCause::dummy(), self.param_env).sup(
1856 DefineOpaqueTypes::Yes,
1857 xform_self_ty,
1858 self_ty,
1859 );
1860 match self.select_trait_candidate(trait_ref) {
1861 Ok(Some(traits::ImplSource::UserDefined(ref impl_data))) => {
1862 CandidateSource::Impl(impl_data.impl_def_id)
1865 }
1866 _ => CandidateSource::Trait(candidate.item.container_id(self.tcx)),
1867 }
1868 }),
1869 }
1870 }
1871
1872 fn candidate_source_from_pick(&self, pick: &Pick<'tcx>) -> CandidateSource {
1873 match pick.kind {
1874 InherentImplPick => CandidateSource::Impl(pick.item.container_id(self.tcx)),
1875 ObjectPick | WhereClausePick(_) | TraitPick => {
1876 CandidateSource::Trait(pick.item.container_id(self.tcx))
1877 }
1878 }
1879 }
1880
1881 #[instrument(level = "debug", skip(self, possibly_unsatisfied_predicates), ret)]
1882 fn consider_probe(
1883 &self,
1884 self_ty: Ty<'tcx>,
1885 instantiate_self_ty_obligations: &[PredicateObligation<'tcx>],
1886 probe: &Candidate<'tcx>,
1887 possibly_unsatisfied_predicates: &mut UnsatisfiedPredicates<'tcx>,
1888 ) -> ProbeResult {
1889 self.probe(|snapshot| {
1890 let outer_universe = self.universe();
1891
1892 let mut result = ProbeResult::Match;
1893 let cause = &self.misc(self.span);
1894 let ocx = ObligationCtxt::new_with_diagnostics(self);
1895
1896 if self.next_trait_solver() {
1904 ocx.register_obligations(instantiate_self_ty_obligations.iter().cloned());
1905 let errors = ocx.try_evaluate_obligations();
1906 if !errors.is_empty() {
1907 unreachable!("unexpected autoderef error {errors:?}");
1908 }
1909 }
1910
1911 let mut trait_predicate = None;
1912 let (mut xform_self_ty, mut xform_ret_ty);
1913
1914 match probe.kind {
1915 InherentImplCandidate { impl_def_id, .. } => {
1916 let impl_args = self.fresh_args_for_item(self.span, impl_def_id);
1917 let impl_ty = self.tcx.type_of(impl_def_id).instantiate(self.tcx, impl_args);
1918 (xform_self_ty, xform_ret_ty) =
1919 self.xform_self_ty(probe.item, impl_ty, impl_args);
1920 xform_self_ty = ocx.normalize(cause, self.param_env, xform_self_ty);
1921 match ocx.relate(cause, self.param_env, self.variance(), self_ty, xform_self_ty)
1922 {
1923 Ok(()) => {}
1924 Err(err) => {
1925 debug!("--> cannot relate self-types {:?}", err);
1926 return ProbeResult::NoMatch;
1927 }
1928 }
1929 xform_ret_ty = ocx.normalize(cause, self.param_env, xform_ret_ty);
1931 let impl_def_id = probe.item.container_id(self.tcx);
1933 let impl_bounds =
1934 self.tcx.predicates_of(impl_def_id).instantiate(self.tcx, impl_args);
1935 let impl_bounds = ocx.normalize(cause, self.param_env, impl_bounds);
1936 ocx.register_obligations(traits::predicates_for_generics(
1938 |idx, span| {
1939 let code = ObligationCauseCode::WhereClauseInExpr(
1940 impl_def_id,
1941 span,
1942 self.scope_expr_id,
1943 idx,
1944 );
1945 self.cause(self.span, code)
1946 },
1947 self.param_env,
1948 impl_bounds,
1949 ));
1950 }
1951 TraitCandidate(poly_trait_ref) => {
1952 if let Some(method_name) = self.method_name {
1955 if self_ty.is_array() && !method_name.span.at_least_rust_2021() {
1956 let trait_def = self.tcx.trait_def(poly_trait_ref.def_id());
1957 if trait_def.skip_array_during_method_dispatch {
1958 return ProbeResult::NoMatch;
1959 }
1960 }
1961
1962 if self_ty.boxed_ty().is_some_and(Ty::is_slice)
1965 && !method_name.span.at_least_rust_2024()
1966 {
1967 let trait_def = self.tcx.trait_def(poly_trait_ref.def_id());
1968 if trait_def.skip_boxed_slice_during_method_dispatch {
1969 return ProbeResult::NoMatch;
1970 }
1971 }
1972 }
1973
1974 let trait_ref = self.instantiate_binder_with_fresh_vars(
1975 self.span,
1976 BoundRegionConversionTime::FnCall,
1977 poly_trait_ref,
1978 );
1979 let trait_ref = ocx.normalize(cause, self.param_env, trait_ref);
1980 (xform_self_ty, xform_ret_ty) =
1981 self.xform_self_ty(probe.item, trait_ref.self_ty(), trait_ref.args);
1982 xform_self_ty = ocx.normalize(cause, self.param_env, xform_self_ty);
1983 match self_ty.kind() {
1984 ty::Alias(ty::Opaque, alias_ty)
1988 if !self.next_trait_solver()
1989 && self.infcx.can_define_opaque_ty(alias_ty.def_id)
1990 && !xform_self_ty.is_ty_var() =>
1991 {
1992 return ProbeResult::NoMatch;
1993 }
1994 _ => match ocx.relate(
1995 cause,
1996 self.param_env,
1997 self.variance(),
1998 self_ty,
1999 xform_self_ty,
2000 ) {
2001 Ok(()) => {}
2002 Err(err) => {
2003 debug!("--> cannot relate self-types {:?}", err);
2004 return ProbeResult::NoMatch;
2005 }
2006 },
2007 }
2008 let obligation = traits::Obligation::new(
2009 self.tcx,
2010 cause.clone(),
2011 self.param_env,
2012 ty::Binder::dummy(trait_ref),
2013 );
2014
2015 if self.infcx.next_trait_solver() || self.infcx.predicate_may_hold(&obligation)
2017 {
2018 ocx.register_obligation(obligation);
2019 } else {
2020 result = ProbeResult::NoMatch;
2021 if let Ok(Some(candidate)) = self.select_trait_candidate(trait_ref) {
2022 for nested_obligation in candidate.nested_obligations() {
2023 if !self.infcx.predicate_may_hold(&nested_obligation) {
2024 possibly_unsatisfied_predicates.push((
2025 self.resolve_vars_if_possible(nested_obligation.predicate),
2026 Some(self.resolve_vars_if_possible(obligation.predicate)),
2027 Some(nested_obligation.cause),
2028 ));
2029 }
2030 }
2031 }
2032 }
2033
2034 trait_predicate = Some(trait_ref.upcast(self.tcx));
2035 }
2036 ObjectCandidate(poly_trait_ref) | WhereClauseCandidate(poly_trait_ref) => {
2037 let trait_ref = self.instantiate_binder_with_fresh_vars(
2038 self.span,
2039 BoundRegionConversionTime::FnCall,
2040 poly_trait_ref,
2041 );
2042 (xform_self_ty, xform_ret_ty) =
2043 self.xform_self_ty(probe.item, trait_ref.self_ty(), trait_ref.args);
2044
2045 if matches!(probe.kind, WhereClauseCandidate(_)) {
2046 match ocx.structurally_normalize_ty(
2050 cause,
2051 self.param_env,
2052 trait_ref.self_ty(),
2053 ) {
2054 Ok(ty) => {
2055 if !matches!(ty.kind(), ty::Param(_)) {
2056 debug!("--> not a param ty: {xform_self_ty:?}");
2057 return ProbeResult::NoMatch;
2058 }
2059 }
2060 Err(errors) => {
2061 debug!("--> cannot relate self-types {:?}", errors);
2062 return ProbeResult::NoMatch;
2063 }
2064 }
2065 }
2066
2067 xform_self_ty = ocx.normalize(cause, self.param_env, xform_self_ty);
2068 match ocx.relate(cause, self.param_env, self.variance(), self_ty, xform_self_ty)
2069 {
2070 Ok(()) => {}
2071 Err(err) => {
2072 debug!("--> cannot relate self-types {:?}", err);
2073 return ProbeResult::NoMatch;
2074 }
2075 }
2076 }
2077 }
2078
2079 if let Some(xform_ret_ty) = xform_ret_ty
2091 && self.infcx.next_trait_solver()
2092 {
2093 ocx.register_obligation(traits::Obligation::new(
2094 self.tcx,
2095 cause.clone(),
2096 self.param_env,
2097 ty::ClauseKind::WellFormed(xform_ret_ty.into()),
2098 ));
2099 }
2100
2101 for error in ocx.try_evaluate_obligations() {
2103 result = ProbeResult::NoMatch;
2104 let nested_predicate = self.resolve_vars_if_possible(error.obligation.predicate);
2105 if let Some(trait_predicate) = trait_predicate
2106 && nested_predicate == self.resolve_vars_if_possible(trait_predicate)
2107 {
2108 } else {
2112 possibly_unsatisfied_predicates.push((
2113 nested_predicate,
2114 Some(self.resolve_vars_if_possible(error.root_obligation.predicate))
2115 .filter(|root_predicate| *root_predicate != nested_predicate),
2116 Some(error.obligation.cause),
2117 ));
2118 }
2119 }
2120
2121 if let ProbeResult::Match = result
2122 && let Some(return_ty) = self.return_type
2123 && let Some(mut xform_ret_ty) = xform_ret_ty
2124 {
2125 if !matches!(probe.kind, InherentImplCandidate { .. }) {
2130 xform_ret_ty = ocx.normalize(&cause, self.param_env, xform_ret_ty);
2131 }
2132
2133 debug!("comparing return_ty {:?} with xform ret ty {:?}", return_ty, xform_ret_ty);
2134 match ocx.relate(cause, self.param_env, self.variance(), xform_ret_ty, return_ty) {
2135 Ok(()) => {}
2136 Err(_) => {
2137 result = ProbeResult::BadReturnType;
2138 }
2139 }
2140
2141 for error in ocx.try_evaluate_obligations() {
2143 result = ProbeResult::NoMatch;
2144 possibly_unsatisfied_predicates.push((
2145 error.obligation.predicate,
2146 Some(error.root_obligation.predicate)
2147 .filter(|predicate| *predicate != error.obligation.predicate),
2148 Some(error.root_obligation.cause),
2149 ));
2150 }
2151 }
2152
2153 if self.infcx.next_trait_solver() {
2154 if self.should_reject_candidate_due_to_opaque_treated_as_rigid(trait_predicate) {
2155 result = ProbeResult::NoMatch;
2156 }
2157 }
2158
2159 if let Err(_) = self.leak_check(outer_universe, Some(snapshot)) {
2165 result = ProbeResult::NoMatch;
2166 }
2167
2168 result
2169 })
2170 }
2171
2172 #[instrument(level = "debug", skip(self), ret)]
2187 fn should_reject_candidate_due_to_opaque_treated_as_rigid(
2188 &self,
2189 trait_predicate: Option<ty::Predicate<'tcx>>,
2190 ) -> bool {
2191 if let Some(predicate) = trait_predicate {
2203 let goal = Goal { param_env: self.param_env, predicate };
2204 if !self.infcx.goal_may_hold_opaque_types_jank(goal) {
2205 return true;
2206 }
2207 }
2208
2209 for step in self.steps {
2212 if step.self_ty_is_opaque {
2213 debug!(?step.autoderefs, ?step.self_ty, "self_type_is_opaque");
2214 let constrained_opaque = self.probe(|_| {
2215 let Ok(ok) = self.fcx.probe_instantiate_query_response(
2220 self.span,
2221 self.orig_steps_var_values,
2222 &step.self_ty,
2223 ) else {
2224 debug!("failed to instantiate self_ty");
2225 return false;
2226 };
2227 let ocx = ObligationCtxt::new(self);
2228 let self_ty = ocx.register_infer_ok_obligations(ok);
2229 if !ocx.try_evaluate_obligations().is_empty() {
2230 debug!("failed to prove instantiate self_ty obligations");
2231 return false;
2232 }
2233
2234 !self.resolve_vars_if_possible(self_ty).is_ty_var()
2235 });
2236 if constrained_opaque {
2237 debug!("opaque type has been constrained");
2238 return true;
2239 }
2240 }
2241 }
2242
2243 false
2244 }
2245
2246 fn collapse_candidates_to_trait_pick(
2264 &self,
2265 self_ty: Ty<'tcx>,
2266 probes: &[(&Candidate<'tcx>, ProbeResult)],
2267 ) -> Option<Pick<'tcx>> {
2268 let container = probes[0].0.item.trait_container(self.tcx)?;
2270 for (p, _) in &probes[1..] {
2271 let p_container = p.item.trait_container(self.tcx)?;
2272 if p_container != container {
2273 return None;
2274 }
2275 }
2276
2277 Some(Pick {
2280 item: probes[0].0.item,
2281 kind: TraitPick,
2282 import_ids: probes[0].0.import_ids.clone(),
2283 autoderefs: 0,
2284 autoref_or_ptr_adjustment: None,
2285 self_ty,
2286 unstable_candidates: vec![],
2287 receiver_steps: None,
2288 shadowed_candidates: vec![],
2289 })
2290 }
2291
2292 fn collapse_candidates_to_subtrait_pick(
2298 &self,
2299 self_ty: Ty<'tcx>,
2300 probes: &[(&Candidate<'tcx>, ProbeResult)],
2301 ) -> Option<Pick<'tcx>> {
2302 let mut child_candidate = probes[0].0;
2303 let mut child_trait = child_candidate.item.trait_container(self.tcx)?;
2304 let mut supertraits: SsoHashSet<_> = supertrait_def_ids(self.tcx, child_trait).collect();
2305
2306 let mut remaining_candidates: Vec<_> = probes[1..].iter().map(|&(p, _)| p).collect();
2307 while !remaining_candidates.is_empty() {
2308 let mut made_progress = false;
2309 let mut next_round = vec![];
2310
2311 for remaining_candidate in remaining_candidates {
2312 let remaining_trait = remaining_candidate.item.trait_container(self.tcx)?;
2313 if supertraits.contains(&remaining_trait) {
2314 made_progress = true;
2315 continue;
2316 }
2317
2318 let remaining_trait_supertraits: SsoHashSet<_> =
2324 supertrait_def_ids(self.tcx, remaining_trait).collect();
2325 if remaining_trait_supertraits.contains(&child_trait) {
2326 child_candidate = remaining_candidate;
2327 child_trait = remaining_trait;
2328 supertraits = remaining_trait_supertraits;
2329 made_progress = true;
2330 continue;
2331 }
2332
2333 next_round.push(remaining_candidate);
2339 }
2340
2341 if made_progress {
2342 remaining_candidates = next_round;
2344 } else {
2345 return None;
2348 }
2349 }
2350
2351 Some(Pick {
2352 item: child_candidate.item,
2353 kind: TraitPick,
2354 import_ids: child_candidate.import_ids.clone(),
2355 autoderefs: 0,
2356 autoref_or_ptr_adjustment: None,
2357 self_ty,
2358 unstable_candidates: vec![],
2359 shadowed_candidates: probes
2360 .iter()
2361 .map(|(c, _)| c.item)
2362 .filter(|item| item.def_id != child_candidate.item.def_id)
2363 .collect(),
2364 receiver_steps: None,
2365 })
2366 }
2367
2368 #[instrument(level = "debug", skip(self))]
2372 pub(crate) fn probe_for_similar_candidate(
2373 &mut self,
2374 ) -> Result<Option<ty::AssocItem>, MethodError<'tcx>> {
2375 debug!("probing for method names similar to {:?}", self.method_name);
2376
2377 self.probe(|_| {
2378 let mut pcx = ProbeContext::new(
2379 self.fcx,
2380 self.span,
2381 self.mode,
2382 self.method_name,
2383 self.return_type,
2384 self.orig_steps_var_values,
2385 self.steps,
2386 self.scope_expr_id,
2387 IsSuggestion(true),
2388 );
2389 pcx.allow_similar_names = true;
2390 pcx.assemble_inherent_candidates();
2391 pcx.assemble_extension_candidates_for_all_traits();
2392
2393 let method_names = pcx.candidate_method_names(|_| true);
2394 pcx.allow_similar_names = false;
2395 let applicable_close_candidates: Vec<ty::AssocItem> = method_names
2396 .iter()
2397 .filter_map(|&method_name| {
2398 pcx.reset();
2399 pcx.method_name = Some(method_name);
2400 pcx.assemble_inherent_candidates();
2401 pcx.assemble_extension_candidates_for_all_traits();
2402 pcx.pick_core(&mut Vec::new()).and_then(|pick| pick.ok()).map(|pick| pick.item)
2403 })
2404 .collect();
2405
2406 if applicable_close_candidates.is_empty() {
2407 Ok(None)
2408 } else {
2409 let best_name = {
2410 let names = applicable_close_candidates
2411 .iter()
2412 .map(|cand| cand.name())
2413 .collect::<Vec<Symbol>>();
2414 find_best_match_for_name_with_substrings(
2415 &names,
2416 self.method_name.unwrap().name,
2417 None,
2418 )
2419 }
2420 .or_else(|| {
2421 applicable_close_candidates
2422 .iter()
2423 .find(|cand| self.matches_by_doc_alias(cand.def_id))
2424 .map(|cand| cand.name())
2425 });
2426 Ok(best_name.and_then(|best_name| {
2427 applicable_close_candidates
2428 .into_iter()
2429 .find(|method| method.name() == best_name)
2430 }))
2431 }
2432 })
2433 }
2434
2435 fn has_applicable_self(&self, item: &ty::AssocItem) -> bool {
2438 match self.mode {
2444 Mode::MethodCall => item.is_method(),
2445 Mode::Path => match item.kind {
2446 ty::AssocKind::Type { .. } => false,
2447 ty::AssocKind::Fn { .. } | ty::AssocKind::Const { .. } => true,
2448 },
2449 }
2450 }
2457
2458 fn record_static_candidate(&self, source: CandidateSource) {
2459 self.static_candidates.borrow_mut().push(source);
2460 }
2461
2462 #[instrument(level = "debug", skip(self))]
2463 fn xform_self_ty(
2464 &self,
2465 item: ty::AssocItem,
2466 impl_ty: Ty<'tcx>,
2467 args: GenericArgsRef<'tcx>,
2468 ) -> (Ty<'tcx>, Option<Ty<'tcx>>) {
2469 if item.is_fn() && self.mode == Mode::MethodCall {
2470 let sig = self.xform_method_sig(item.def_id, args);
2471 (sig.inputs()[0], Some(sig.output()))
2472 } else {
2473 (impl_ty, None)
2474 }
2475 }
2476
2477 #[instrument(level = "debug", skip(self))]
2478 fn xform_method_sig(&self, method: DefId, args: GenericArgsRef<'tcx>) -> ty::FnSig<'tcx> {
2479 let fn_sig = self.tcx.fn_sig(method);
2480 debug!(?fn_sig);
2481
2482 assert!(!args.has_escaping_bound_vars());
2483
2484 let generics = self.tcx.generics_of(method);
2490 assert_eq!(args.len(), generics.parent_count);
2491
2492 let xform_fn_sig = if generics.is_own_empty() {
2493 fn_sig.instantiate(self.tcx, args)
2494 } else {
2495 let args = GenericArgs::for_item(self.tcx, method, |param, _| {
2496 let i = param.index as usize;
2497 if i < args.len() {
2498 args[i]
2499 } else {
2500 match param.kind {
2501 GenericParamDefKind::Lifetime => {
2502 self.tcx.lifetimes.re_erased.into()
2504 }
2505 GenericParamDefKind::Type { .. } | GenericParamDefKind::Const { .. } => {
2506 self.var_for_def(self.span, param)
2507 }
2508 }
2509 }
2510 });
2511 fn_sig.instantiate(self.tcx, args)
2512 };
2513
2514 self.tcx.instantiate_bound_regions_with_erased(xform_fn_sig)
2515 }
2516
2517 fn is_relevant_kind_for_mode(&self, kind: ty::AssocKind) -> bool {
2519 match (self.mode, kind) {
2520 (Mode::MethodCall, ty::AssocKind::Fn { .. }) => true,
2521 (Mode::Path, ty::AssocKind::Const { .. } | ty::AssocKind::Fn { .. }) => true,
2522 _ => false,
2523 }
2524 }
2525
2526 fn matches_by_doc_alias(&self, def_id: DefId) -> bool {
2529 let Some(method) = self.method_name else {
2530 return false;
2531 };
2532 let Some(local_def_id) = def_id.as_local() else {
2533 return false;
2534 };
2535 let hir_id = self.fcx.tcx.local_def_id_to_hir_id(local_def_id);
2536 let attrs = self.fcx.tcx.hir_attrs(hir_id);
2537
2538 if is_doc_alias_attrs_contain_symbol(attrs.into_iter(), method.name) {
2539 return true;
2540 }
2541
2542 for attr in attrs {
2543 if attr.has_name(sym::rustc_confusables) {
2544 let Some(confusables) = attr.meta_item_list() else {
2545 continue;
2546 };
2547 for n in confusables {
2549 if let Some(lit) = n.lit()
2550 && method.name == lit.symbol
2551 {
2552 return true;
2553 }
2554 }
2555 }
2556 }
2557 false
2558 }
2559
2560 fn impl_or_trait_item(&self, def_id: DefId) -> SmallVec<[ty::AssocItem; 1]> {
2565 if let Some(name) = self.method_name {
2566 if self.allow_similar_names {
2567 let max_dist = max(name.as_str().len(), 3) / 3;
2568 self.tcx
2569 .associated_items(def_id)
2570 .in_definition_order()
2571 .filter(|x| {
2572 if !self.is_relevant_kind_for_mode(x.kind) {
2573 return false;
2574 }
2575 if let Some(d) = edit_distance_with_substrings(
2576 name.as_str(),
2577 x.name().as_str(),
2578 max_dist,
2579 ) {
2580 return d > 0;
2581 }
2582 self.matches_by_doc_alias(x.def_id)
2583 })
2584 .copied()
2585 .collect()
2586 } else {
2587 self.fcx
2588 .associated_value(def_id, name)
2589 .filter(|x| self.is_relevant_kind_for_mode(x.kind))
2590 .map_or_else(SmallVec::new, |x| SmallVec::from_buf([x]))
2591 }
2592 } else {
2593 self.tcx
2594 .associated_items(def_id)
2595 .in_definition_order()
2596 .filter(|x| self.is_relevant_kind_for_mode(x.kind))
2597 .copied()
2598 .collect()
2599 }
2600 }
2601}
2602
2603impl<'tcx> Candidate<'tcx> {
2604 fn to_unadjusted_pick(
2605 &self,
2606 self_ty: Ty<'tcx>,
2607 unstable_candidates: Vec<(Candidate<'tcx>, Symbol)>,
2608 ) -> Pick<'tcx> {
2609 Pick {
2610 item: self.item,
2611 kind: match self.kind {
2612 InherentImplCandidate { .. } => InherentImplPick,
2613 ObjectCandidate(_) => ObjectPick,
2614 TraitCandidate(_) => TraitPick,
2615 WhereClauseCandidate(trait_ref) => {
2616 assert!(
2622 !trait_ref.skip_binder().args.has_infer()
2623 && !trait_ref.skip_binder().args.has_placeholders()
2624 );
2625
2626 WhereClausePick(trait_ref)
2627 }
2628 },
2629 import_ids: self.import_ids.clone(),
2630 autoderefs: 0,
2631 autoref_or_ptr_adjustment: None,
2632 self_ty,
2633 unstable_candidates,
2634 receiver_steps: match self.kind {
2635 InherentImplCandidate { receiver_steps, .. } => Some(receiver_steps),
2636 _ => None,
2637 },
2638 shadowed_candidates: vec![],
2639 }
2640 }
2641}