rustc_hir_typeck/method/
suggest.rs

1//! Give useful errors and suggestions to users when an item can't be
2//! found or is otherwise invalid.
3
4// ignore-tidy-filelength
5
6use core::ops::ControlFlow;
7use std::borrow::Cow;
8use std::path::PathBuf;
9
10use hir::Expr;
11use rustc_ast::ast::Mutability;
12use rustc_attr_data_structures::{AttributeKind, find_attr};
13use rustc_data_structures::fx::{FxIndexMap, FxIndexSet};
14use rustc_data_structures::sorted_map::SortedMap;
15use rustc_data_structures::unord::UnordSet;
16use rustc_errors::codes::*;
17use rustc_errors::{
18    Applicability, Diag, DiagStyledString, MultiSpan, StashKey, pluralize, struct_span_code_err,
19};
20use rustc_hir::def::{CtorKind, DefKind, Res};
21use rustc_hir::def_id::DefId;
22use rustc_hir::intravisit::{self, Visitor};
23use rustc_hir::lang_items::LangItem;
24use rustc_hir::{self as hir, ExprKind, HirId, Node, PathSegment, QPath};
25use rustc_infer::infer::{self, RegionVariableOrigin};
26use rustc_middle::bug;
27use rustc_middle::ty::fast_reject::{DeepRejectCtxt, TreatParams, simplify_type};
28use rustc_middle::ty::print::{
29    PrintTraitRefExt as _, with_crate_prefix, with_forced_trimmed_paths,
30    with_no_visible_paths_if_doc_hidden,
31};
32use rustc_middle::ty::{self, GenericArgKind, IsSuggestable, Ty, TyCtxt, TypeVisitableExt};
33use rustc_span::def_id::DefIdSet;
34use rustc_span::{
35    DUMMY_SP, ErrorGuaranteed, ExpnKind, FileName, Ident, MacroKind, Span, Symbol, edit_distance,
36    kw, sym,
37};
38use rustc_trait_selection::error_reporting::traits::DefIdOrName;
39use rustc_trait_selection::error_reporting::traits::on_unimplemented::OnUnimplementedNote;
40use rustc_trait_selection::infer::InferCtxtExt;
41use rustc_trait_selection::traits::query::evaluate_obligation::InferCtxtExt as _;
42use rustc_trait_selection::traits::{
43    FulfillmentError, Obligation, ObligationCause, ObligationCauseCode, supertraits,
44};
45use tracing::{debug, info, instrument};
46
47use super::probe::{AutorefOrPtrAdjustment, IsSuggestion, Mode, ProbeScope};
48use super::{CandidateSource, MethodError, NoMatchData};
49use crate::errors::{self, CandidateTraitNote, NoAssociatedItem};
50use crate::{Expectation, FnCtxt};
51
52impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
53    fn is_slice_ty(&self, ty: Ty<'tcx>, span: Span) -> bool {
54        self.autoderef(span, ty)
55            .silence_errors()
56            .any(|(ty, _)| matches!(ty.kind(), ty::Slice(..) | ty::Array(..)))
57    }
58
59    fn impl_into_iterator_should_be_iterator(
60        &self,
61        ty: Ty<'tcx>,
62        span: Span,
63        unsatisfied_predicates: &Vec<(
64            ty::Predicate<'tcx>,
65            Option<ty::Predicate<'tcx>>,
66            Option<ObligationCause<'tcx>>,
67        )>,
68    ) -> bool {
69        fn predicate_bounds_generic_param<'tcx>(
70            predicate: ty::Predicate<'_>,
71            generics: &'tcx ty::Generics,
72            generic_param: &ty::GenericParamDef,
73            tcx: TyCtxt<'tcx>,
74        ) -> bool {
75            if let ty::PredicateKind::Clause(ty::ClauseKind::Trait(trait_pred)) =
76                predicate.kind().as_ref().skip_binder()
77            {
78                let ty::TraitPredicate { trait_ref: ty::TraitRef { args, .. }, .. } = trait_pred;
79                if args.is_empty() {
80                    return false;
81                }
82                let Some(arg_ty) = args[0].as_type() else {
83                    return false;
84                };
85                let ty::Param(param) = *arg_ty.kind() else {
86                    return false;
87                };
88                // Is `generic_param` the same as the arg for this trait predicate?
89                generic_param.index == generics.type_param(param, tcx).index
90            } else {
91                false
92            }
93        }
94
95        let is_iterator_predicate = |predicate: ty::Predicate<'tcx>| -> bool {
96            if let ty::PredicateKind::Clause(ty::ClauseKind::Trait(trait_pred)) =
97                predicate.kind().as_ref().skip_binder()
98            {
99                self.tcx.is_diagnostic_item(sym::Iterator, trait_pred.trait_ref.def_id)
100                    // ignore unsatisfied predicates generated from trying to auto-ref ty (#127511)
101                    && trait_pred.trait_ref.self_ty() == ty
102            } else {
103                false
104            }
105        };
106
107        // Does the `ty` implement `IntoIterator`?
108        let Some(into_iterator_trait) = self.tcx.get_diagnostic_item(sym::IntoIterator) else {
109            return false;
110        };
111        let trait_ref = ty::TraitRef::new(self.tcx, into_iterator_trait, [ty]);
112        let obligation = Obligation::new(self.tcx, self.misc(span), self.param_env, trait_ref);
113        if !self.predicate_must_hold_modulo_regions(&obligation) {
114            return false;
115        }
116
117        match *ty.peel_refs().kind() {
118            ty::Param(param) => {
119                let generics = self.tcx.generics_of(self.body_id);
120                let generic_param = generics.type_param(param, self.tcx);
121                for unsatisfied in unsatisfied_predicates.iter() {
122                    // The parameter implements `IntoIterator`
123                    // but it has called a method that requires it to implement `Iterator`
124                    if predicate_bounds_generic_param(
125                        unsatisfied.0,
126                        generics,
127                        generic_param,
128                        self.tcx,
129                    ) && is_iterator_predicate(unsatisfied.0)
130                    {
131                        return true;
132                    }
133                }
134            }
135            ty::Slice(..) | ty::Adt(..) | ty::Alias(ty::Opaque, _) => {
136                for unsatisfied in unsatisfied_predicates.iter() {
137                    if is_iterator_predicate(unsatisfied.0) {
138                        return true;
139                    }
140                }
141            }
142            _ => return false,
143        }
144        false
145    }
146
147    #[instrument(level = "debug", skip(self))]
148    pub(crate) fn report_method_error(
149        &self,
150        call_id: HirId,
151        rcvr_ty: Ty<'tcx>,
152        error: MethodError<'tcx>,
153        expected: Expectation<'tcx>,
154        trait_missing_method: bool,
155    ) -> ErrorGuaranteed {
156        // NOTE: Reporting a method error should also suppress any unused trait errors,
157        // since the method error is very possibly the reason why the trait wasn't used.
158        for &import_id in
159            self.tcx.in_scope_traits(call_id).into_iter().flatten().flat_map(|c| &c.import_ids)
160        {
161            self.typeck_results.borrow_mut().used_trait_imports.insert(import_id);
162        }
163
164        let (span, expr_span, source, item_name, args) = match self.tcx.hir_node(call_id) {
165            hir::Node::Expr(&hir::Expr {
166                kind: hir::ExprKind::MethodCall(segment, rcvr, args, _),
167                span,
168                ..
169            }) => {
170                (segment.ident.span, span, SelfSource::MethodCall(rcvr), segment.ident, Some(args))
171            }
172            hir::Node::Expr(&hir::Expr {
173                kind: hir::ExprKind::Path(QPath::TypeRelative(rcvr, segment)),
174                span,
175                ..
176            })
177            | hir::Node::PatExpr(&hir::PatExpr {
178                kind: hir::PatExprKind::Path(QPath::TypeRelative(rcvr, segment)),
179                span,
180                ..
181            })
182            | hir::Node::Pat(&hir::Pat {
183                kind:
184                    hir::PatKind::Struct(QPath::TypeRelative(rcvr, segment), ..)
185                    | hir::PatKind::TupleStruct(QPath::TypeRelative(rcvr, segment), ..),
186                span,
187                ..
188            }) => {
189                let args = match self.tcx.parent_hir_node(call_id) {
190                    hir::Node::Expr(&hir::Expr {
191                        kind: hir::ExprKind::Call(callee, args), ..
192                    }) if callee.hir_id == call_id => Some(args),
193                    _ => None,
194                };
195                (segment.ident.span, span, SelfSource::QPath(rcvr), segment.ident, args)
196            }
197            node => unreachable!("{node:?}"),
198        };
199
200        // Try to get the span of the identifier within the expression's syntax context
201        // (if that's different).
202        let within_macro_span = span.within_macro(expr_span, self.tcx.sess.source_map());
203
204        // Avoid suggestions when we don't know what's going on.
205        if let Err(guar) = rcvr_ty.error_reported() {
206            return guar;
207        }
208
209        match error {
210            MethodError::NoMatch(mut no_match_data) => self.report_no_match_method_error(
211                span,
212                rcvr_ty,
213                item_name,
214                call_id,
215                source,
216                args,
217                expr_span,
218                &mut no_match_data,
219                expected,
220                trait_missing_method,
221                within_macro_span,
222            ),
223
224            MethodError::Ambiguity(mut sources) => {
225                let mut err = struct_span_code_err!(
226                    self.dcx(),
227                    item_name.span,
228                    E0034,
229                    "multiple applicable items in scope"
230                );
231                err.span_label(item_name.span, format!("multiple `{item_name}` found"));
232                if let Some(within_macro_span) = within_macro_span {
233                    err.span_label(within_macro_span, "due to this macro variable");
234                }
235
236                self.note_candidates_on_method_error(
237                    rcvr_ty,
238                    item_name,
239                    source,
240                    args,
241                    span,
242                    &mut err,
243                    &mut sources,
244                    Some(expr_span),
245                );
246                err.emit()
247            }
248
249            MethodError::PrivateMatch(kind, def_id, out_of_scope_traits) => {
250                let kind = self.tcx.def_kind_descr(kind, def_id);
251                let mut err = struct_span_code_err!(
252                    self.dcx(),
253                    item_name.span,
254                    E0624,
255                    "{} `{}` is private",
256                    kind,
257                    item_name
258                );
259                err.span_label(item_name.span, format!("private {kind}"));
260                let sp =
261                    self.tcx.hir_span_if_local(def_id).unwrap_or_else(|| self.tcx.def_span(def_id));
262                err.span_label(sp, format!("private {kind} defined here"));
263                if let Some(within_macro_span) = within_macro_span {
264                    err.span_label(within_macro_span, "due to this macro variable");
265                }
266                self.suggest_valid_traits(&mut err, item_name, out_of_scope_traits, true);
267                err.emit()
268            }
269
270            MethodError::IllegalSizedBound { candidates, needs_mut, bound_span, self_expr } => {
271                let msg = if needs_mut {
272                    with_forced_trimmed_paths!(format!(
273                        "the `{item_name}` method cannot be invoked on `{rcvr_ty}`"
274                    ))
275                } else {
276                    format!("the `{item_name}` method cannot be invoked on a trait object")
277                };
278                let mut err = self.dcx().struct_span_err(span, msg);
279                if !needs_mut {
280                    err.span_label(bound_span, "this has a `Sized` requirement");
281                }
282                if let Some(within_macro_span) = within_macro_span {
283                    err.span_label(within_macro_span, "due to this macro variable");
284                }
285                if !candidates.is_empty() {
286                    let help = format!(
287                        "{an}other candidate{s} {were} found in the following trait{s}",
288                        an = if candidates.len() == 1 { "an" } else { "" },
289                        s = pluralize!(candidates.len()),
290                        were = pluralize!("was", candidates.len()),
291                    );
292                    self.suggest_use_candidates(
293                        candidates,
294                        |accessible_sugg, inaccessible_sugg, span| {
295                            let suggest_for_access =
296                                |err: &mut Diag<'_>, mut msg: String, sugg: Vec<_>| {
297                                    msg += &format!(
298                                        ", perhaps add a `use` for {one_of_them}:",
299                                        one_of_them =
300                                            if sugg.len() == 1 { "it" } else { "one_of_them" },
301                                    );
302                                    err.span_suggestions(
303                                        span,
304                                        msg,
305                                        sugg,
306                                        Applicability::MaybeIncorrect,
307                                    );
308                                };
309                            let suggest_for_privacy =
310                                |err: &mut Diag<'_>, mut msg: String, suggs: Vec<String>| {
311                                    if let [sugg] = suggs.as_slice() {
312                                        err.help(format!("\
313                                            trait `{}` provides `{item_name}` is implemented but not reachable",
314                                            sugg.trim(),
315                                        ));
316                                    } else {
317                                        msg += &format!(" but {} not reachable", pluralize!("is", suggs.len()));
318                                        err.span_suggestions(
319                                            span,
320                                            msg,
321                                            suggs,
322                                            Applicability::MaybeIncorrect,
323                                        );
324                                    }
325                                };
326                            if accessible_sugg.is_empty() {
327                                // `inaccessible_sugg` must not be empty
328                                suggest_for_privacy(&mut err, help, inaccessible_sugg);
329                            } else if inaccessible_sugg.is_empty() {
330                                suggest_for_access(&mut err, help, accessible_sugg);
331                            } else {
332                                suggest_for_access(&mut err, help.clone(), accessible_sugg);
333                                suggest_for_privacy(&mut err, help, inaccessible_sugg);
334                            }
335                        },
336                    );
337                }
338                if let ty::Ref(region, t_type, mutability) = rcvr_ty.kind() {
339                    if needs_mut {
340                        let trait_type =
341                            Ty::new_ref(self.tcx, *region, *t_type, mutability.invert());
342                        let msg = format!("you need `{trait_type}` instead of `{rcvr_ty}`");
343                        let mut kind = &self_expr.kind;
344                        while let hir::ExprKind::AddrOf(_, _, expr)
345                        | hir::ExprKind::Unary(hir::UnOp::Deref, expr) = kind
346                        {
347                            kind = &expr.kind;
348                        }
349                        if let hir::ExprKind::Path(hir::QPath::Resolved(None, path)) = kind
350                            && let hir::def::Res::Local(hir_id) = path.res
351                            && let hir::Node::Pat(b) = self.tcx.hir_node(hir_id)
352                            && let hir::Node::Param(p) = self.tcx.parent_hir_node(b.hir_id)
353                            && let Some(decl) = self.tcx.parent_hir_node(p.hir_id).fn_decl()
354                            && let Some(ty) = decl.inputs.iter().find(|ty| ty.span == p.ty_span)
355                            && let hir::TyKind::Ref(_, mut_ty) = &ty.kind
356                            && let hir::Mutability::Not = mut_ty.mutbl
357                        {
358                            err.span_suggestion_verbose(
359                                mut_ty.ty.span.shrink_to_lo(),
360                                msg,
361                                "mut ",
362                                Applicability::MachineApplicable,
363                            );
364                        } else {
365                            err.help(msg);
366                        }
367                    }
368                }
369                err.emit()
370            }
371
372            MethodError::ErrorReported(guar) => guar,
373
374            MethodError::BadReturnType => bug!("no return type expectations but got BadReturnType"),
375        }
376    }
377
378    fn suggest_missing_writer(&self, rcvr_ty: Ty<'tcx>, rcvr_expr: &hir::Expr<'tcx>) -> Diag<'_> {
379        let mut file = None;
380        let mut err = struct_span_code_err!(
381            self.dcx(),
382            rcvr_expr.span,
383            E0599,
384            "cannot write into `{}`",
385            self.tcx.short_string(rcvr_ty, &mut file),
386        );
387        *err.long_ty_path() = file;
388        err.span_note(
389            rcvr_expr.span,
390            "must implement `io::Write`, `fmt::Write`, or have a `write_fmt` method",
391        );
392        if let ExprKind::Lit(_) = rcvr_expr.kind {
393            err.span_help(
394                rcvr_expr.span.shrink_to_lo(),
395                "a writer is needed before this format string",
396            );
397        };
398        err
399    }
400
401    fn suggest_use_shadowed_binding_with_method(
402        &self,
403        self_source: SelfSource<'tcx>,
404        method_name: Ident,
405        ty_str_reported: &str,
406        err: &mut Diag<'_>,
407    ) {
408        #[derive(Debug)]
409        struct LetStmt {
410            ty_hir_id_opt: Option<hir::HirId>,
411            binding_id: hir::HirId,
412            span: Span,
413            init_hir_id: hir::HirId,
414        }
415
416        // Used for finding suggest binding.
417        // ```rust
418        // earlier binding for suggesting:
419        // let y = vec![1, 2];
420        // now binding:
421        // if let Some(y) = x {
422        //     y.push(y);
423        // }
424        // ```
425        struct LetVisitor<'a, 'tcx> {
426            // Error binding which don't have `method_name`.
427            binding_name: Symbol,
428            binding_id: hir::HirId,
429            // Used for check if the suggest binding has `method_name`.
430            fcx: &'a FnCtxt<'a, 'tcx>,
431            call_expr: &'tcx Expr<'tcx>,
432            method_name: Ident,
433            // Suggest the binding which is shallowed.
434            sugg_let: Option<LetStmt>,
435        }
436
437        impl<'a, 'tcx> LetVisitor<'a, 'tcx> {
438            // Check scope of binding.
439            fn is_sub_scope(&self, sub_id: hir::ItemLocalId, super_id: hir::ItemLocalId) -> bool {
440                let scope_tree = self.fcx.tcx.region_scope_tree(self.fcx.body_id);
441                if let Some(sub_var_scope) = scope_tree.var_scope(sub_id)
442                    && let Some(super_var_scope) = scope_tree.var_scope(super_id)
443                    && scope_tree.is_subscope_of(sub_var_scope, super_var_scope)
444                {
445                    return true;
446                }
447                false
448            }
449
450            // Check if an earlier shadowed binding make `the receiver` of a MethodCall has the method.
451            // If it does, record the earlier binding for subsequent notes.
452            fn check_and_add_sugg_binding(&mut self, binding: LetStmt) -> bool {
453                if !self.is_sub_scope(self.binding_id.local_id, binding.binding_id.local_id) {
454                    return false;
455                }
456
457                // Get the earlier shadowed binding'ty and use it to check the method.
458                if let Some(ty_hir_id) = binding.ty_hir_id_opt
459                    && let Some(tyck_ty) = self.fcx.node_ty_opt(ty_hir_id)
460                {
461                    if self
462                        .fcx
463                        .lookup_probe_for_diagnostic(
464                            self.method_name,
465                            tyck_ty,
466                            self.call_expr,
467                            ProbeScope::TraitsInScope,
468                            None,
469                        )
470                        .is_ok()
471                    {
472                        self.sugg_let = Some(binding);
473                        return true;
474                    } else {
475                        return false;
476                    }
477                }
478
479                // If the shadowed binding has an itializer expression,
480                // use the initializer expression'ty to try to find the method again.
481                // For example like:  `let mut x = Vec::new();`,
482                // `Vec::new()` is the itializer expression.
483                if let Some(self_ty) = self.fcx.node_ty_opt(binding.init_hir_id)
484                    && self
485                        .fcx
486                        .lookup_probe_for_diagnostic(
487                            self.method_name,
488                            self_ty,
489                            self.call_expr,
490                            ProbeScope::TraitsInScope,
491                            None,
492                        )
493                        .is_ok()
494                {
495                    self.sugg_let = Some(binding);
496                    return true;
497                }
498                return false;
499            }
500        }
501
502        impl<'v> Visitor<'v> for LetVisitor<'_, '_> {
503            type Result = ControlFlow<()>;
504            fn visit_stmt(&mut self, ex: &'v hir::Stmt<'v>) -> Self::Result {
505                if let hir::StmtKind::Let(&hir::LetStmt { pat, ty, init, .. }) = ex.kind
506                    && let hir::PatKind::Binding(_, binding_id, binding_name, ..) = pat.kind
507                    && let Some(init) = init
508                    && binding_name.name == self.binding_name
509                    && binding_id != self.binding_id
510                {
511                    if self.check_and_add_sugg_binding(LetStmt {
512                        ty_hir_id_opt: ty.map(|ty| ty.hir_id),
513                        binding_id,
514                        span: pat.span,
515                        init_hir_id: init.hir_id,
516                    }) {
517                        return ControlFlow::Break(());
518                    }
519                    ControlFlow::Continue(())
520                } else {
521                    hir::intravisit::walk_stmt(self, ex)
522                }
523            }
524
525            // Used for find the error binding.
526            // When the visitor reaches this point, all the shadowed bindings
527            // have been found, so the visitor ends.
528            fn visit_pat(&mut self, p: &'v hir::Pat<'v>) -> Self::Result {
529                match p.kind {
530                    hir::PatKind::Binding(_, binding_id, binding_name, _) => {
531                        if binding_name.name == self.binding_name && binding_id == self.binding_id {
532                            return ControlFlow::Break(());
533                        }
534                    }
535                    _ => {
536                        let _ = intravisit::walk_pat(self, p);
537                    }
538                }
539                ControlFlow::Continue(())
540            }
541        }
542
543        if let SelfSource::MethodCall(rcvr) = self_source
544            && let hir::ExprKind::Path(QPath::Resolved(_, path)) = rcvr.kind
545            && let hir::def::Res::Local(recv_id) = path.res
546            && let Some(segment) = path.segments.first()
547        {
548            let body = self.tcx.hir_body_owned_by(self.body_id);
549
550            if let Node::Expr(call_expr) = self.tcx.parent_hir_node(rcvr.hir_id) {
551                let mut let_visitor = LetVisitor {
552                    fcx: self,
553                    call_expr,
554                    binding_name: segment.ident.name,
555                    binding_id: recv_id,
556                    method_name,
557                    sugg_let: None,
558                };
559                let _ = let_visitor.visit_body(&body);
560                if let Some(sugg_let) = let_visitor.sugg_let
561                    && let Some(self_ty) = self.node_ty_opt(sugg_let.init_hir_id)
562                {
563                    let _sm = self.infcx.tcx.sess.source_map();
564                    let rcvr_name = segment.ident.name;
565                    let mut span = MultiSpan::from_span(sugg_let.span);
566                    span.push_span_label(sugg_let.span,
567                            format!("`{rcvr_name}` of type `{self_ty}` that has method `{method_name}` defined earlier here"));
568                    span.push_span_label(
569                        self.tcx.hir_span(recv_id),
570                        format!(
571                            "earlier `{rcvr_name}` shadowed here with type `{ty_str_reported}`"
572                        ),
573                    );
574                    err.span_note(
575                        span,
576                        format!(
577                            "there's an earlier shadowed binding `{rcvr_name}` of type `{self_ty}` \
578                                    that has method `{method_name}` available"
579                        ),
580                    );
581                }
582            }
583        }
584    }
585
586    fn report_no_match_method_error(
587        &self,
588        mut span: Span,
589        rcvr_ty: Ty<'tcx>,
590        item_ident: Ident,
591        expr_id: hir::HirId,
592        source: SelfSource<'tcx>,
593        args: Option<&'tcx [hir::Expr<'tcx>]>,
594        sugg_span: Span,
595        no_match_data: &mut NoMatchData<'tcx>,
596        expected: Expectation<'tcx>,
597        trait_missing_method: bool,
598        within_macro_span: Option<Span>,
599    ) -> ErrorGuaranteed {
600        let mode = no_match_data.mode;
601        let tcx = self.tcx;
602        let rcvr_ty = self.resolve_vars_if_possible(rcvr_ty);
603        let mut ty_file = None;
604        let (ty_str, short_ty_str) =
605            if trait_missing_method && let ty::Dynamic(predicates, _, _) = rcvr_ty.kind() {
606                (predicates.to_string(), with_forced_trimmed_paths!(predicates.to_string()))
607            } else {
608                (
609                    tcx.short_string(rcvr_ty, &mut ty_file),
610                    with_forced_trimmed_paths!(rcvr_ty.to_string()),
611                )
612            };
613        let is_method = mode == Mode::MethodCall;
614        let unsatisfied_predicates = &no_match_data.unsatisfied_predicates;
615        let similar_candidate = no_match_data.similar_candidate;
616        let item_kind = if is_method {
617            "method"
618        } else if rcvr_ty.is_enum() {
619            "variant or associated item"
620        } else {
621            match (item_ident.as_str().chars().next(), rcvr_ty.is_fresh_ty()) {
622                (Some(name), false) if name.is_lowercase() => "function or associated item",
623                (Some(_), false) => "associated item",
624                (Some(_), true) | (None, false) => "variant or associated item",
625                (None, true) => "variant",
626            }
627        };
628
629        // We could pass the file for long types into these two, but it isn't strictly necessary
630        // given how targeted they are.
631        if let Err(guar) = self.report_failed_method_call_on_range_end(
632            tcx,
633            rcvr_ty,
634            source,
635            span,
636            item_ident,
637            &short_ty_str,
638            &mut ty_file,
639        ) {
640            return guar;
641        }
642        if let Err(guar) = self.report_failed_method_call_on_numerical_infer_var(
643            tcx,
644            rcvr_ty,
645            source,
646            span,
647            item_kind,
648            item_ident,
649            &short_ty_str,
650            &mut ty_file,
651        ) {
652            return guar;
653        }
654        span = item_ident.span;
655
656        // Don't show generic arguments when the method can't be found in any implementation (#81576).
657        let mut ty_str_reported = ty_str.clone();
658        if let ty::Adt(_, generics) = rcvr_ty.kind() {
659            if generics.len() > 0 {
660                let mut autoderef = self.autoderef(span, rcvr_ty).silence_errors();
661                let candidate_found = autoderef.any(|(ty, _)| {
662                    if let ty::Adt(adt_def, _) = ty.kind() {
663                        self.tcx
664                            .inherent_impls(adt_def.did())
665                            .into_iter()
666                            .any(|def_id| self.associated_value(*def_id, item_ident).is_some())
667                    } else {
668                        false
669                    }
670                });
671                let has_deref = autoderef.step_count() > 0;
672                if !candidate_found && !has_deref && unsatisfied_predicates.is_empty() {
673                    if let Some((path_string, _)) = ty_str.split_once('<') {
674                        ty_str_reported = path_string.to_string();
675                    }
676                }
677            }
678        }
679
680        let is_write = sugg_span.ctxt().outer_expn_data().macro_def_id.is_some_and(|def_id| {
681            tcx.is_diagnostic_item(sym::write_macro, def_id)
682                || tcx.is_diagnostic_item(sym::writeln_macro, def_id)
683        }) && item_ident.name == sym::write_fmt;
684        let mut err = if is_write && let SelfSource::MethodCall(rcvr_expr) = source {
685            self.suggest_missing_writer(rcvr_ty, rcvr_expr)
686        } else {
687            let mut err = self.dcx().create_err(NoAssociatedItem {
688                span,
689                item_kind,
690                item_ident,
691                ty_prefix: if trait_missing_method {
692                    // FIXME(mu001999) E0599 maybe not suitable here because it is for types
693                    Cow::from("trait")
694                } else {
695                    rcvr_ty.prefix_string(self.tcx)
696                },
697                ty_str: ty_str_reported.clone(),
698                trait_missing_method,
699            });
700
701            if is_method {
702                self.suggest_use_shadowed_binding_with_method(
703                    source,
704                    item_ident,
705                    &ty_str_reported,
706                    &mut err,
707                );
708            }
709
710            // Check if we wrote `Self::Assoc(1)` as if it were a tuple ctor.
711            if let SelfSource::QPath(ty) = source
712                && let hir::TyKind::Path(hir::QPath::Resolved(_, path)) = ty.kind
713                && let Res::SelfTyAlias { alias_to: impl_def_id, .. } = path.res
714                && let DefKind::Impl { .. } = self.tcx.def_kind(impl_def_id)
715                && let Some(candidate) = tcx.associated_items(impl_def_id).find_by_ident_and_kind(
716                    self.tcx,
717                    item_ident,
718                    ty::AssocTag::Type,
719                    impl_def_id,
720                )
721                && let Some(adt_def) = tcx.type_of(candidate.def_id).skip_binder().ty_adt_def()
722                && adt_def.is_struct()
723                && adt_def.non_enum_variant().ctor_kind() == Some(CtorKind::Fn)
724            {
725                let def_path = tcx.def_path_str(adt_def.did());
726                err.span_suggestion(
727                    sugg_span,
728                    format!("to construct a value of type `{}`, use the explicit path", def_path),
729                    def_path,
730                    Applicability::MachineApplicable,
731                );
732            }
733
734            err
735        };
736        if tcx.sess.source_map().is_multiline(sugg_span) {
737            err.span_label(sugg_span.with_hi(span.lo()), "");
738        }
739        if let Some(within_macro_span) = within_macro_span {
740            err.span_label(within_macro_span, "due to this macro variable");
741        }
742
743        if rcvr_ty.references_error() {
744            err.downgrade_to_delayed_bug();
745        }
746
747        if matches!(source, SelfSource::QPath(_)) && args.is_some() {
748            self.find_builder_fn(&mut err, rcvr_ty, expr_id);
749        }
750
751        if tcx.ty_is_opaque_future(rcvr_ty) && item_ident.name == sym::poll {
752            err.help(format!(
753                "method `poll` found on `Pin<&mut {ty_str}>`, \
754                see documentation for `std::pin::Pin`"
755            ));
756            err.help("self type must be pinned to call `Future::poll`, \
757                see https://rust-lang.github.io/async-book/04_pinning/01_chapter.html#pinning-in-practice"
758            );
759        }
760
761        if let Mode::MethodCall = mode
762            && let SelfSource::MethodCall(cal) = source
763        {
764            self.suggest_await_before_method(
765                &mut err,
766                item_ident,
767                rcvr_ty,
768                cal,
769                span,
770                expected.only_has_type(self),
771            );
772        }
773        if let Some(span) =
774            tcx.resolutions(()).confused_type_with_std_module.get(&span.with_parent(None))
775        {
776            err.span_suggestion(
777                span.shrink_to_lo(),
778                "you are looking for the module in `std`, not the primitive type",
779                "std::",
780                Applicability::MachineApplicable,
781            );
782        }
783
784        // on pointers, check if the method would exist on a reference
785        if let SelfSource::MethodCall(rcvr_expr) = source
786            && let ty::RawPtr(ty, ptr_mutbl) = *rcvr_ty.kind()
787            && let Ok(pick) = self.lookup_probe_for_diagnostic(
788                item_ident,
789                Ty::new_ref(tcx, ty::Region::new_error_misc(tcx), ty, ptr_mutbl),
790                self.tcx.hir_expect_expr(self.tcx.parent_hir_id(rcvr_expr.hir_id)),
791                ProbeScope::TraitsInScope,
792                None,
793            )
794            && let ty::Ref(_, _, sugg_mutbl) = *pick.self_ty.kind()
795            && (sugg_mutbl.is_not() || ptr_mutbl.is_mut())
796        {
797            let (method, method_anchor) = match sugg_mutbl {
798                Mutability::Not => {
799                    let method_anchor = match ptr_mutbl {
800                        Mutability::Not => "as_ref",
801                        Mutability::Mut => "as_ref-1",
802                    };
803                    ("as_ref", method_anchor)
804                }
805                Mutability::Mut => ("as_mut", "as_mut"),
806            };
807            err.span_note(
808                tcx.def_span(pick.item.def_id),
809                format!("the method `{item_ident}` exists on the type `{ty}`", ty = pick.self_ty),
810            );
811            let mut_str = ptr_mutbl.ptr_str();
812            err.note(format!(
813                "you might want to use the unsafe method `<*{mut_str} T>::{method}` to get \
814                an optional reference to the value behind the pointer"
815            ));
816            err.note(format!(
817                "read the documentation for `<*{mut_str} T>::{method}` and ensure you satisfy its \
818                safety preconditions before calling it to avoid undefined behavior: \
819                https://doc.rust-lang.org/std/primitive.pointer.html#method.{method_anchor}"
820            ));
821        }
822
823        let mut ty_span = match rcvr_ty.kind() {
824            ty::Param(param_type) => {
825                Some(param_type.span_from_generics(self.tcx, self.body_id.to_def_id()))
826            }
827            ty::Adt(def, _) if def.did().is_local() => Some(tcx.def_span(def.did())),
828            _ => None,
829        };
830
831        if let SelfSource::MethodCall(rcvr_expr) = source {
832            self.suggest_fn_call(&mut err, rcvr_expr, rcvr_ty, |output_ty| {
833                let call_expr = self.tcx.hir_expect_expr(self.tcx.parent_hir_id(rcvr_expr.hir_id));
834                let probe = self.lookup_probe_for_diagnostic(
835                    item_ident,
836                    output_ty,
837                    call_expr,
838                    ProbeScope::AllTraits,
839                    expected.only_has_type(self),
840                );
841                probe.is_ok()
842            });
843            self.note_internal_mutation_in_method(
844                &mut err,
845                rcvr_expr,
846                expected.to_option(self),
847                rcvr_ty,
848            );
849        }
850
851        let mut custom_span_label = false;
852
853        let static_candidates = &mut no_match_data.static_candidates;
854
855        // `static_candidates` may have same candidates appended by
856        // inherent and extension, which may result in incorrect
857        // diagnostic.
858        static_candidates.dedup();
859
860        if !static_candidates.is_empty() {
861            err.note(
862                "found the following associated functions; to be used as methods, \
863                 functions must have a `self` parameter",
864            );
865            err.span_label(span, "this is an associated function, not a method");
866            custom_span_label = true;
867        }
868        if static_candidates.len() == 1 {
869            self.suggest_associated_call_syntax(
870                &mut err,
871                static_candidates,
872                rcvr_ty,
873                source,
874                item_ident,
875                args,
876                sugg_span,
877            );
878            self.note_candidates_on_method_error(
879                rcvr_ty,
880                item_ident,
881                source,
882                args,
883                span,
884                &mut err,
885                static_candidates,
886                None,
887            );
888        } else if static_candidates.len() > 1 {
889            self.note_candidates_on_method_error(
890                rcvr_ty,
891                item_ident,
892                source,
893                args,
894                span,
895                &mut err,
896                static_candidates,
897                Some(sugg_span),
898            );
899        }
900
901        let mut bound_spans: SortedMap<Span, Vec<String>> = Default::default();
902        let mut restrict_type_params = false;
903        let mut suggested_derive = false;
904        let mut unsatisfied_bounds = false;
905        if item_ident.name == sym::count && self.is_slice_ty(rcvr_ty, span) {
906            let msg = "consider using `len` instead";
907            if let SelfSource::MethodCall(_expr) = source {
908                err.span_suggestion_short(span, msg, "len", Applicability::MachineApplicable);
909            } else {
910                err.span_label(span, msg);
911            }
912            if let Some(iterator_trait) = self.tcx.get_diagnostic_item(sym::Iterator) {
913                let iterator_trait = self.tcx.def_path_str(iterator_trait);
914                err.note(format!(
915                    "`count` is defined on `{iterator_trait}`, which `{rcvr_ty}` does not implement"
916                ));
917            }
918        } else if self.impl_into_iterator_should_be_iterator(rcvr_ty, span, unsatisfied_predicates)
919        {
920            err.span_label(span, format!("`{rcvr_ty}` is not an iterator"));
921            if !span.in_external_macro(self.tcx.sess.source_map()) {
922                err.multipart_suggestion_verbose(
923                    "call `.into_iter()` first",
924                    vec![(span.shrink_to_lo(), format!("into_iter()."))],
925                    Applicability::MaybeIncorrect,
926                );
927            }
928            return err.emit();
929        } else if !unsatisfied_predicates.is_empty() && matches!(rcvr_ty.kind(), ty::Param(_)) {
930            // We special case the situation where we are looking for `_` in
931            // `<TypeParam as _>::method` because otherwise the machinery will look for blanket
932            // implementations that have unsatisfied trait bounds to suggest, leading us to claim
933            // things like "we're looking for a trait with method `cmp`, both `Iterator` and `Ord`
934            // have one, in order to implement `Ord` you need to restrict `TypeParam: FnPtr` so
935            // that `impl<T: FnPtr> Ord for T` can apply", which is not what we want. We have a type
936            // parameter, we want to directly say "`Ord::cmp` and `Iterator::cmp` exist, restrict
937            // `TypeParam: Ord` or `TypeParam: Iterator`"". That is done further down when calling
938            // `self.suggest_traits_to_import`, so we ignore the `unsatisfied_predicates`
939            // suggestions.
940        } else if !unsatisfied_predicates.is_empty() {
941            let mut type_params = FxIndexMap::default();
942
943            // Pick out the list of unimplemented traits on the receiver.
944            // This is used for custom error messages with the `#[rustc_on_unimplemented]` attribute.
945            let mut unimplemented_traits = FxIndexMap::default();
946            let mut unimplemented_traits_only = true;
947            for (predicate, _parent_pred, cause) in unsatisfied_predicates {
948                if let (ty::PredicateKind::Clause(ty::ClauseKind::Trait(p)), Some(cause)) =
949                    (predicate.kind().skip_binder(), cause.as_ref())
950                {
951                    if p.trait_ref.self_ty() != rcvr_ty {
952                        // This is necessary, not just to keep the errors clean, but also
953                        // because our derived obligations can wind up with a trait ref that
954                        // requires a different param_env to be correctly compared.
955                        continue;
956                    }
957                    unimplemented_traits.entry(p.trait_ref.def_id).or_insert((
958                        predicate.kind().rebind(p),
959                        Obligation {
960                            cause: cause.clone(),
961                            param_env: self.param_env,
962                            predicate: *predicate,
963                            recursion_depth: 0,
964                        },
965                    ));
966                }
967            }
968
969            // Make sure that, if any traits other than the found ones were involved,
970            // we don't report an unimplemented trait.
971            // We don't want to say that `iter::Cloned` is not an iterator, just
972            // because of some non-Clone item being iterated over.
973            for (predicate, _parent_pred, _cause) in unsatisfied_predicates {
974                match predicate.kind().skip_binder() {
975                    ty::PredicateKind::Clause(ty::ClauseKind::Trait(p))
976                        if unimplemented_traits.contains_key(&p.trait_ref.def_id) => {}
977                    _ => {
978                        unimplemented_traits_only = false;
979                        break;
980                    }
981                }
982            }
983
984            let mut collect_type_param_suggestions =
985                |self_ty: Ty<'tcx>, parent_pred: ty::Predicate<'tcx>, obligation: &str| {
986                    // We don't care about regions here, so it's fine to skip the binder here.
987                    if let (ty::Param(_), ty::PredicateKind::Clause(ty::ClauseKind::Trait(p))) =
988                        (self_ty.kind(), parent_pred.kind().skip_binder())
989                    {
990                        let node = match p.trait_ref.self_ty().kind() {
991                            ty::Param(_) => {
992                                // Account for `fn` items like in `issue-35677.rs` to
993                                // suggest restricting its type params.
994                                Some(self.tcx.hir_node_by_def_id(self.body_id))
995                            }
996                            ty::Adt(def, _) => def
997                                .did()
998                                .as_local()
999                                .map(|def_id| self.tcx.hir_node_by_def_id(def_id)),
1000                            _ => None,
1001                        };
1002                        if let Some(hir::Node::Item(hir::Item { kind, .. })) = node
1003                            && let Some(g) = kind.generics()
1004                        {
1005                            let key = (
1006                                g.tail_span_for_predicate_suggestion(),
1007                                g.add_where_or_trailing_comma(),
1008                            );
1009                            type_params
1010                                .entry(key)
1011                                .or_insert_with(UnordSet::default)
1012                                .insert(obligation.to_owned());
1013                            return true;
1014                        }
1015                    }
1016                    false
1017                };
1018            let mut bound_span_label = |self_ty: Ty<'_>, obligation: &str, quiet: &str| {
1019                let msg = format!("`{}`", if obligation.len() > 50 { quiet } else { obligation });
1020                match self_ty.kind() {
1021                    // Point at the type that couldn't satisfy the bound.
1022                    ty::Adt(def, _) => {
1023                        bound_spans.get_mut_or_insert_default(tcx.def_span(def.did())).push(msg)
1024                    }
1025                    // Point at the trait object that couldn't satisfy the bound.
1026                    ty::Dynamic(preds, _, _) => {
1027                        for pred in preds.iter() {
1028                            match pred.skip_binder() {
1029                                ty::ExistentialPredicate::Trait(tr) => {
1030                                    bound_spans
1031                                        .get_mut_or_insert_default(tcx.def_span(tr.def_id))
1032                                        .push(msg.clone());
1033                                }
1034                                ty::ExistentialPredicate::Projection(_)
1035                                | ty::ExistentialPredicate::AutoTrait(_) => {}
1036                            }
1037                        }
1038                    }
1039                    // Point at the closure that couldn't satisfy the bound.
1040                    ty::Closure(def_id, _) => {
1041                        bound_spans
1042                            .get_mut_or_insert_default(tcx.def_span(*def_id))
1043                            .push(format!("`{quiet}`"));
1044                    }
1045                    _ => {}
1046                }
1047            };
1048            let mut format_pred = |pred: ty::Predicate<'tcx>| {
1049                let bound_predicate = pred.kind();
1050                match bound_predicate.skip_binder() {
1051                    ty::PredicateKind::Clause(ty::ClauseKind::Projection(pred)) => {
1052                        let pred = bound_predicate.rebind(pred);
1053                        // `<Foo as Iterator>::Item = String`.
1054                        let projection_term = pred.skip_binder().projection_term;
1055                        let quiet_projection_term =
1056                            projection_term.with_self_ty(tcx, Ty::new_var(tcx, ty::TyVid::ZERO));
1057
1058                        let term = pred.skip_binder().term;
1059
1060                        let obligation = format!("{projection_term} = {term}");
1061                        let quiet = with_forced_trimmed_paths!(format!(
1062                            "{} = {}",
1063                            quiet_projection_term, term
1064                        ));
1065
1066                        bound_span_label(projection_term.self_ty(), &obligation, &quiet);
1067                        Some((obligation, projection_term.self_ty()))
1068                    }
1069                    ty::PredicateKind::Clause(ty::ClauseKind::Trait(poly_trait_ref)) => {
1070                        let p = poly_trait_ref.trait_ref;
1071                        let self_ty = p.self_ty();
1072                        let path = p.print_only_trait_path();
1073                        let obligation = format!("{self_ty}: {path}");
1074                        let quiet = with_forced_trimmed_paths!(format!("_: {}", path));
1075                        bound_span_label(self_ty, &obligation, &quiet);
1076                        Some((obligation, self_ty))
1077                    }
1078                    _ => None,
1079                }
1080            };
1081
1082            // Find all the requirements that come from a local `impl` block.
1083            let mut skip_list: UnordSet<_> = Default::default();
1084            let mut spanned_predicates = FxIndexMap::default();
1085            for (p, parent_p, cause) in unsatisfied_predicates {
1086                // Extract the predicate span and parent def id of the cause,
1087                // if we have one.
1088                let (item_def_id, cause_span) = match cause.as_ref().map(|cause| cause.code()) {
1089                    Some(ObligationCauseCode::ImplDerived(data)) => {
1090                        (data.impl_or_alias_def_id, data.span)
1091                    }
1092                    Some(
1093                        ObligationCauseCode::WhereClauseInExpr(def_id, span, _, _)
1094                        | ObligationCauseCode::WhereClause(def_id, span),
1095                    ) if !span.is_dummy() => (*def_id, *span),
1096                    _ => continue,
1097                };
1098
1099                // Don't point out the span of `WellFormed` predicates.
1100                if !matches!(
1101                    p.kind().skip_binder(),
1102                    ty::PredicateKind::Clause(
1103                        ty::ClauseKind::Projection(..) | ty::ClauseKind::Trait(..)
1104                    )
1105                ) {
1106                    continue;
1107                }
1108
1109                match self.tcx.hir_get_if_local(item_def_id) {
1110                    // Unmet obligation comes from a `derive` macro, point at it once to
1111                    // avoid multiple span labels pointing at the same place.
1112                    Some(Node::Item(hir::Item {
1113                        kind: hir::ItemKind::Impl(hir::Impl { of_trait, self_ty, .. }),
1114                        ..
1115                    })) if matches!(
1116                        self_ty.span.ctxt().outer_expn_data().kind,
1117                        ExpnKind::Macro(MacroKind::Derive, _)
1118                    ) || matches!(
1119                        of_trait.as_ref().map(|t| t.path.span.ctxt().outer_expn_data().kind),
1120                        Some(ExpnKind::Macro(MacroKind::Derive, _))
1121                    ) =>
1122                    {
1123                        let span = self_ty.span.ctxt().outer_expn_data().call_site;
1124                        let entry = spanned_predicates.entry(span);
1125                        let entry = entry.or_insert_with(|| {
1126                            (FxIndexSet::default(), FxIndexSet::default(), Vec::new())
1127                        });
1128                        entry.0.insert(span);
1129                        entry.1.insert((
1130                            span,
1131                            "unsatisfied trait bound introduced in this `derive` macro",
1132                        ));
1133                        entry.2.push(p);
1134                        skip_list.insert(p);
1135                    }
1136
1137                    // Unmet obligation coming from an `impl`.
1138                    Some(Node::Item(hir::Item {
1139                        kind: hir::ItemKind::Impl(hir::Impl { of_trait, self_ty, generics, .. }),
1140                        span: item_span,
1141                        ..
1142                    })) => {
1143                        let sized_pred =
1144                            unsatisfied_predicates.iter().any(|(pred, _, _)| {
1145                                match pred.kind().skip_binder() {
1146                                    ty::PredicateKind::Clause(ty::ClauseKind::Trait(pred)) => {
1147                                        self.tcx.is_lang_item(pred.def_id(), LangItem::Sized)
1148                                            && pred.polarity == ty::PredicatePolarity::Positive
1149                                    }
1150                                    _ => false,
1151                                }
1152                            });
1153                        for param in generics.params {
1154                            if param.span == cause_span && sized_pred {
1155                                let (sp, sugg) = match param.colon_span {
1156                                    Some(sp) => (sp.shrink_to_hi(), " ?Sized +"),
1157                                    None => (param.span.shrink_to_hi(), ": ?Sized"),
1158                                };
1159                                err.span_suggestion_verbose(
1160                                    sp,
1161                                    "consider relaxing the type parameter's implicit `Sized` bound",
1162                                    sugg,
1163                                    Applicability::MachineApplicable,
1164                                );
1165                            }
1166                        }
1167                        if let Some(pred) = parent_p {
1168                            // Done to add the "doesn't satisfy" `span_label`.
1169                            let _ = format_pred(*pred);
1170                        }
1171                        skip_list.insert(p);
1172                        let entry = spanned_predicates.entry(self_ty.span);
1173                        let entry = entry.or_insert_with(|| {
1174                            (FxIndexSet::default(), FxIndexSet::default(), Vec::new())
1175                        });
1176                        entry.2.push(p);
1177                        if cause_span != *item_span {
1178                            entry.0.insert(cause_span);
1179                            entry.1.insert((cause_span, "unsatisfied trait bound introduced here"));
1180                        } else {
1181                            if let Some(trait_ref) = of_trait {
1182                                entry.0.insert(trait_ref.path.span);
1183                            }
1184                            entry.0.insert(self_ty.span);
1185                        };
1186                        if let Some(trait_ref) = of_trait {
1187                            entry.1.insert((trait_ref.path.span, ""));
1188                        }
1189                        entry.1.insert((self_ty.span, ""));
1190                    }
1191                    Some(Node::Item(hir::Item {
1192                        kind: hir::ItemKind::Trait(rustc_ast::ast::IsAuto::Yes, ..),
1193                        span: item_span,
1194                        ..
1195                    })) => {
1196                        self.dcx().span_delayed_bug(
1197                            *item_span,
1198                            "auto trait is invoked with no method error, but no error reported?",
1199                        );
1200                    }
1201                    Some(
1202                        Node::Item(hir::Item {
1203                            kind:
1204                                hir::ItemKind::Trait(_, _, ident, ..)
1205                                | hir::ItemKind::TraitAlias(ident, ..),
1206                            ..
1207                        })
1208                        // We may also encounter unsatisfied GAT or method bounds
1209                        | Node::TraitItem(hir::TraitItem { ident, .. })
1210                        | Node::ImplItem(hir::ImplItem { ident, .. })
1211                    ) => {
1212                        skip_list.insert(p);
1213                        let entry = spanned_predicates.entry(ident.span);
1214                        let entry = entry.or_insert_with(|| {
1215                            (FxIndexSet::default(), FxIndexSet::default(), Vec::new())
1216                        });
1217                        entry.0.insert(cause_span);
1218                        entry.1.insert((ident.span, ""));
1219                        entry.1.insert((cause_span, "unsatisfied trait bound introduced here"));
1220                        entry.2.push(p);
1221                    }
1222                    _ => {
1223                        // It's possible to use well-formedness clauses to get obligations
1224                        // which point arbitrary items like ADTs, so there's no use in ICEing
1225                        // here if we find that the obligation originates from some other
1226                        // node that we don't handle.
1227                    }
1228                }
1229            }
1230            let mut spanned_predicates: Vec<_> = spanned_predicates.into_iter().collect();
1231            spanned_predicates.sort_by_key(|(span, _)| *span);
1232            for (_, (primary_spans, span_labels, predicates)) in spanned_predicates {
1233                let mut preds: Vec<_> = predicates
1234                    .iter()
1235                    .filter_map(|pred| format_pred(**pred))
1236                    .map(|(p, _)| format!("`{p}`"))
1237                    .collect();
1238                preds.sort();
1239                preds.dedup();
1240                let msg = if let [pred] = &preds[..] {
1241                    format!("trait bound {pred} was not satisfied")
1242                } else {
1243                    format!("the following trait bounds were not satisfied:\n{}", preds.join("\n"),)
1244                };
1245                let mut span: MultiSpan = primary_spans.into_iter().collect::<Vec<_>>().into();
1246                for (sp, label) in span_labels {
1247                    span.push_span_label(sp, label);
1248                }
1249                err.span_note(span, msg);
1250                unsatisfied_bounds = true;
1251            }
1252
1253            let mut suggested_bounds = UnordSet::default();
1254            // The requirements that didn't have an `impl` span to show.
1255            let mut bound_list = unsatisfied_predicates
1256                .iter()
1257                .filter_map(|(pred, parent_pred, _cause)| {
1258                    let mut suggested = false;
1259                    format_pred(*pred).map(|(p, self_ty)| {
1260                        if let Some(parent) = parent_pred
1261                            && suggested_bounds.contains(parent)
1262                        {
1263                            // We don't suggest `PartialEq` when we already suggest `Eq`.
1264                        } else if !suggested_bounds.contains(pred)
1265                            && collect_type_param_suggestions(self_ty, *pred, &p)
1266                        {
1267                            suggested = true;
1268                            suggested_bounds.insert(pred);
1269                        }
1270                        (
1271                            match parent_pred {
1272                                None => format!("`{p}`"),
1273                                Some(parent_pred) => match format_pred(*parent_pred) {
1274                                    None => format!("`{p}`"),
1275                                    Some((parent_p, _)) => {
1276                                        if !suggested
1277                                            && !suggested_bounds.contains(pred)
1278                                            && !suggested_bounds.contains(parent_pred)
1279                                            && collect_type_param_suggestions(
1280                                                self_ty,
1281                                                *parent_pred,
1282                                                &p,
1283                                            )
1284                                        {
1285                                            suggested_bounds.insert(pred);
1286                                        }
1287                                        format!("`{p}`\nwhich is required by `{parent_p}`")
1288                                    }
1289                                },
1290                            },
1291                            *pred,
1292                        )
1293                    })
1294                })
1295                .filter(|(_, pred)| !skip_list.contains(&pred))
1296                .map(|(t, _)| t)
1297                .enumerate()
1298                .collect::<Vec<(usize, String)>>();
1299
1300            if !matches!(rcvr_ty.peel_refs().kind(), ty::Param(_)) {
1301                for ((span, add_where_or_comma), obligations) in type_params.into_iter() {
1302                    restrict_type_params = true;
1303                    // #74886: Sort here so that the output is always the same.
1304                    let obligations = obligations.into_sorted_stable_ord();
1305                    err.span_suggestion_verbose(
1306                        span,
1307                        format!(
1308                            "consider restricting the type parameter{s} to satisfy the trait \
1309                             bound{s}",
1310                            s = pluralize!(obligations.len())
1311                        ),
1312                        format!("{} {}", add_where_or_comma, obligations.join(", ")),
1313                        Applicability::MaybeIncorrect,
1314                    );
1315                }
1316            }
1317
1318            bound_list.sort_by(|(_, a), (_, b)| a.cmp(b)); // Sort alphabetically.
1319            bound_list.dedup_by(|(_, a), (_, b)| a == b); // #35677
1320            bound_list.sort_by_key(|(pos, _)| *pos); // Keep the original predicate order.
1321
1322            if !bound_list.is_empty() || !skip_list.is_empty() {
1323                let bound_list =
1324                    bound_list.into_iter().map(|(_, path)| path).collect::<Vec<_>>().join("\n");
1325                let actual_prefix = rcvr_ty.prefix_string(self.tcx);
1326                info!("unimplemented_traits.len() == {}", unimplemented_traits.len());
1327                let (primary_message, label, notes) = if unimplemented_traits.len() == 1
1328                    && unimplemented_traits_only
1329                {
1330                    unimplemented_traits
1331                        .into_iter()
1332                        .next()
1333                        .map(|(_, (trait_ref, obligation))| {
1334                            if trait_ref.self_ty().references_error() || rcvr_ty.references_error()
1335                            {
1336                                // Avoid crashing.
1337                                return (None, None, Vec::new());
1338                            }
1339                            let OnUnimplementedNote { message, label, notes, .. } = self
1340                                .err_ctxt()
1341                                .on_unimplemented_note(trait_ref, &obligation, &mut ty_file);
1342                            (message, label, notes)
1343                        })
1344                        .unwrap()
1345                } else {
1346                    (None, None, Vec::new())
1347                };
1348                let primary_message = primary_message.unwrap_or_else(|| {
1349                    format!(
1350                        "the {item_kind} `{item_ident}` exists for {actual_prefix} `{ty_str}`, \
1351                         but its trait bounds were not satisfied"
1352                    )
1353                });
1354                err.primary_message(primary_message);
1355                if let Some(label) = label {
1356                    custom_span_label = true;
1357                    err.span_label(span, label);
1358                }
1359                if !bound_list.is_empty() {
1360                    err.note(format!(
1361                        "the following trait bounds were not satisfied:\n{bound_list}"
1362                    ));
1363                }
1364                for note in notes {
1365                    err.note(note);
1366                }
1367
1368                suggested_derive = self.suggest_derive(&mut err, unsatisfied_predicates);
1369
1370                unsatisfied_bounds = true;
1371            }
1372        } else if let ty::Adt(def, targs) = rcvr_ty.kind()
1373            && let SelfSource::MethodCall(rcvr_expr) = source
1374        {
1375            // This is useful for methods on arbitrary self types that might have a simple
1376            // mutability difference, like calling a method on `Pin<&mut Self>` that is on
1377            // `Pin<&Self>`.
1378            if targs.len() == 1 {
1379                let mut item_segment = hir::PathSegment::invalid();
1380                item_segment.ident = item_ident;
1381                for t in [Ty::new_mut_ref, Ty::new_imm_ref, |_, _, t| t] {
1382                    let new_args =
1383                        tcx.mk_args_from_iter(targs.iter().map(|arg| match arg.as_type() {
1384                            Some(ty) => ty::GenericArg::from(t(
1385                                tcx,
1386                                tcx.lifetimes.re_erased,
1387                                ty.peel_refs(),
1388                            )),
1389                            _ => arg,
1390                        }));
1391                    let rcvr_ty = Ty::new_adt(tcx, *def, new_args);
1392                    if let Ok(method) = self.lookup_method_for_diagnostic(
1393                        rcvr_ty,
1394                        &item_segment,
1395                        span,
1396                        tcx.parent_hir_node(rcvr_expr.hir_id).expect_expr(),
1397                        rcvr_expr,
1398                    ) {
1399                        err.span_note(
1400                            tcx.def_span(method.def_id),
1401                            format!("{item_kind} is available for `{rcvr_ty}`"),
1402                        );
1403                    }
1404                }
1405            }
1406        }
1407
1408        let mut find_candidate_for_method = false;
1409
1410        let mut label_span_not_found = |err: &mut Diag<'_>| {
1411            if unsatisfied_predicates.is_empty() {
1412                err.span_label(span, format!("{item_kind} not found in `{ty_str}`"));
1413                let is_string_or_ref_str = match rcvr_ty.kind() {
1414                    ty::Ref(_, ty, _) => {
1415                        ty.is_str()
1416                            || matches!(
1417                                ty.kind(),
1418                                ty::Adt(adt, _) if self.tcx.is_lang_item(adt.did(), LangItem::String)
1419                            )
1420                    }
1421                    ty::Adt(adt, _) => self.tcx.is_lang_item(adt.did(), LangItem::String),
1422                    _ => false,
1423                };
1424                if is_string_or_ref_str && item_ident.name == sym::iter {
1425                    err.span_suggestion_verbose(
1426                        item_ident.span,
1427                        "because of the in-memory representation of `&str`, to obtain \
1428                         an `Iterator` over each of its codepoint use method `chars`",
1429                        "chars",
1430                        Applicability::MachineApplicable,
1431                    );
1432                }
1433                if let ty::Adt(adt, _) = rcvr_ty.kind() {
1434                    let mut inherent_impls_candidate = self
1435                        .tcx
1436                        .inherent_impls(adt.did())
1437                        .into_iter()
1438                        .copied()
1439                        .filter(|def_id| {
1440                            if let Some(assoc) = self.associated_value(*def_id, item_ident) {
1441                                // Check for both mode is the same so we avoid suggesting
1442                                // incorrect associated item.
1443                                match (mode, assoc.is_method(), source) {
1444                                    (Mode::MethodCall, true, SelfSource::MethodCall(_)) => {
1445                                        // We check that the suggest type is actually
1446                                        // different from the received one
1447                                        // So we avoid suggestion method with Box<Self>
1448                                        // for instance
1449                                        self.tcx.at(span).type_of(*def_id).instantiate_identity()
1450                                            != rcvr_ty
1451                                    }
1452                                    (Mode::Path, false, _) => true,
1453                                    _ => false,
1454                                }
1455                            } else {
1456                                false
1457                            }
1458                        })
1459                        .collect::<Vec<_>>();
1460                    if !inherent_impls_candidate.is_empty() {
1461                        inherent_impls_candidate.sort_by_key(|id| self.tcx.def_path_str(id));
1462                        inherent_impls_candidate.dedup();
1463
1464                        // number of types to show at most
1465                        let limit = if inherent_impls_candidate.len() == 5 { 5 } else { 4 };
1466                        let type_candidates = inherent_impls_candidate
1467                            .iter()
1468                            .take(limit)
1469                            .map(|impl_item| {
1470                                format!(
1471                                    "- `{}`",
1472                                    self.tcx.at(span).type_of(*impl_item).instantiate_identity()
1473                                )
1474                            })
1475                            .collect::<Vec<_>>()
1476                            .join("\n");
1477                        let additional_types = if inherent_impls_candidate.len() > limit {
1478                            format!("\nand {} more types", inherent_impls_candidate.len() - limit)
1479                        } else {
1480                            "".to_string()
1481                        };
1482                        err.note(format!(
1483                            "the {item_kind} was found for\n{type_candidates}{additional_types}"
1484                        ));
1485                        find_candidate_for_method = mode == Mode::MethodCall;
1486                    }
1487                }
1488            } else {
1489                let ty_str =
1490                    if ty_str.len() > 50 { String::new() } else { format!("on `{ty_str}` ") };
1491                err.span_label(
1492                    span,
1493                    format!("{item_kind} cannot be called {ty_str}due to unsatisfied trait bounds"),
1494                );
1495            }
1496        };
1497
1498        // If the method name is the name of a field with a function or closure type,
1499        // give a helping note that it has to be called as `(x.f)(...)`.
1500        if let SelfSource::MethodCall(expr) = source {
1501            if !self.suggest_calling_field_as_fn(span, rcvr_ty, expr, item_ident, &mut err)
1502                && similar_candidate.is_none()
1503                && !custom_span_label
1504            {
1505                label_span_not_found(&mut err);
1506            }
1507        } else if !custom_span_label {
1508            label_span_not_found(&mut err);
1509        }
1510
1511        let confusable_suggested = self.confusable_method_name(
1512            &mut err,
1513            rcvr_ty,
1514            item_ident,
1515            args.map(|args| {
1516                args.iter()
1517                    .map(|expr| {
1518                        self.node_ty_opt(expr.hir_id).unwrap_or_else(|| self.next_ty_var(expr.span))
1519                    })
1520                    .collect()
1521            }),
1522        );
1523
1524        // Don't suggest (for example) `expr.field.clone()` if `expr.clone()`
1525        // can't be called due to `typeof(expr): Clone` not holding.
1526        if unsatisfied_predicates.is_empty() {
1527            self.suggest_calling_method_on_field(
1528                &mut err,
1529                source,
1530                span,
1531                rcvr_ty,
1532                item_ident,
1533                expected.only_has_type(self),
1534            );
1535        }
1536
1537        self.suggest_unwrapping_inner_self(&mut err, source, rcvr_ty, item_ident);
1538
1539        for (span, mut bounds) in bound_spans {
1540            if !tcx.sess.source_map().is_span_accessible(span) {
1541                continue;
1542            }
1543            bounds.sort();
1544            bounds.dedup();
1545            let pre = if Some(span) == ty_span {
1546                ty_span.take();
1547                format!(
1548                    "{item_kind} `{item_ident}` not found for this {} because it ",
1549                    rcvr_ty.prefix_string(self.tcx)
1550                )
1551            } else {
1552                String::new()
1553            };
1554            let msg = match &bounds[..] {
1555                [bound] => format!("{pre}doesn't satisfy {bound}"),
1556                bounds if bounds.len() > 4 => format!("doesn't satisfy {} bounds", bounds.len()),
1557                [bounds @ .., last] => {
1558                    format!("{pre}doesn't satisfy {} or {last}", bounds.join(", "))
1559                }
1560                [] => unreachable!(),
1561            };
1562            err.span_label(span, msg);
1563        }
1564        if let Some(span) = ty_span {
1565            err.span_label(
1566                span,
1567                format!(
1568                    "{item_kind} `{item_ident}` not found for this {}",
1569                    rcvr_ty.prefix_string(self.tcx)
1570                ),
1571            );
1572        }
1573
1574        if rcvr_ty.is_numeric() && rcvr_ty.is_fresh()
1575            || restrict_type_params
1576            || suggested_derive
1577            || self.lookup_alternative_tuple_impls(&mut err, &unsatisfied_predicates)
1578        {
1579        } else {
1580            self.suggest_traits_to_import(
1581                &mut err,
1582                span,
1583                rcvr_ty,
1584                item_ident,
1585                args.map(|args| args.len() + 1),
1586                source,
1587                no_match_data.out_of_scope_traits.clone(),
1588                static_candidates,
1589                unsatisfied_bounds,
1590                expected.only_has_type(self),
1591                trait_missing_method,
1592            );
1593        }
1594
1595        // Don't emit a suggestion if we found an actual method
1596        // that had unsatisfied trait bounds
1597        if unsatisfied_predicates.is_empty() && rcvr_ty.is_enum() {
1598            let adt_def = rcvr_ty.ty_adt_def().expect("enum is not an ADT");
1599            if let Some(var_name) = edit_distance::find_best_match_for_name(
1600                &adt_def.variants().iter().map(|s| s.name).collect::<Vec<_>>(),
1601                item_ident.name,
1602                None,
1603            ) && let Some(variant) = adt_def.variants().iter().find(|s| s.name == var_name)
1604            {
1605                let mut suggestion = vec![(span, var_name.to_string())];
1606                if let SelfSource::QPath(ty) = source
1607                    && let hir::Node::Expr(ref path_expr) = self.tcx.parent_hir_node(ty.hir_id)
1608                    && let hir::ExprKind::Path(_) = path_expr.kind
1609                    && let hir::Node::Stmt(&hir::Stmt { kind: hir::StmtKind::Semi(parent), .. })
1610                    | hir::Node::Expr(parent) = self.tcx.parent_hir_node(path_expr.hir_id)
1611                {
1612                    let replacement_span =
1613                        if let hir::ExprKind::Call(..) | hir::ExprKind::Struct(..) = parent.kind {
1614                            // We want to replace the parts that need to go, like `()` and `{}`.
1615                            span.with_hi(parent.span.hi())
1616                        } else {
1617                            span
1618                        };
1619                    match (variant.ctor, parent.kind) {
1620                        (None, hir::ExprKind::Struct(..)) => {
1621                            // We want a struct and we have a struct. We won't suggest changing
1622                            // the fields (at least for now).
1623                            suggestion = vec![(span, var_name.to_string())];
1624                        }
1625                        (None, _) => {
1626                            // struct
1627                            suggestion = vec![(
1628                                replacement_span,
1629                                if variant.fields.is_empty() {
1630                                    format!("{var_name} {{}}")
1631                                } else {
1632                                    format!(
1633                                        "{var_name} {{ {} }}",
1634                                        variant
1635                                            .fields
1636                                            .iter()
1637                                            .map(|f| format!("{}: /* value */", f.name))
1638                                            .collect::<Vec<_>>()
1639                                            .join(", ")
1640                                    )
1641                                },
1642                            )];
1643                        }
1644                        (Some((hir::def::CtorKind::Const, _)), _) => {
1645                            // unit, remove the `()`.
1646                            suggestion = vec![(replacement_span, var_name.to_string())];
1647                        }
1648                        (
1649                            Some((hir::def::CtorKind::Fn, def_id)),
1650                            hir::ExprKind::Call(rcvr, args),
1651                        ) => {
1652                            let fn_sig = self.tcx.fn_sig(def_id).instantiate_identity();
1653                            let inputs = fn_sig.inputs().skip_binder();
1654                            // FIXME: reuse the logic for "change args" suggestion to account for types
1655                            // involved and detect things like substitution.
1656                            match (inputs, args) {
1657                                (inputs, []) => {
1658                                    // Add arguments.
1659                                    suggestion.push((
1660                                        rcvr.span.shrink_to_hi().with_hi(parent.span.hi()),
1661                                        format!(
1662                                            "({})",
1663                                            inputs
1664                                                .iter()
1665                                                .map(|i| format!("/* {i} */"))
1666                                                .collect::<Vec<String>>()
1667                                                .join(", ")
1668                                        ),
1669                                    ));
1670                                }
1671                                (_, [arg]) if inputs.len() != args.len() => {
1672                                    // Replace arguments.
1673                                    suggestion.push((
1674                                        arg.span,
1675                                        inputs
1676                                            .iter()
1677                                            .map(|i| format!("/* {i} */"))
1678                                            .collect::<Vec<String>>()
1679                                            .join(", "),
1680                                    ));
1681                                }
1682                                (_, [arg_start, .., arg_end]) if inputs.len() != args.len() => {
1683                                    // Replace arguments.
1684                                    suggestion.push((
1685                                        arg_start.span.to(arg_end.span),
1686                                        inputs
1687                                            .iter()
1688                                            .map(|i| format!("/* {i} */"))
1689                                            .collect::<Vec<String>>()
1690                                            .join(", "),
1691                                    ));
1692                                }
1693                                // Argument count is the same, keep as is.
1694                                _ => {}
1695                            }
1696                        }
1697                        (Some((hir::def::CtorKind::Fn, def_id)), _) => {
1698                            let fn_sig = self.tcx.fn_sig(def_id).instantiate_identity();
1699                            let inputs = fn_sig.inputs().skip_binder();
1700                            suggestion = vec![(
1701                                replacement_span,
1702                                format!(
1703                                    "{var_name}({})",
1704                                    inputs
1705                                        .iter()
1706                                        .map(|i| format!("/* {i} */"))
1707                                        .collect::<Vec<String>>()
1708                                        .join(", ")
1709                                ),
1710                            )];
1711                        }
1712                    }
1713                }
1714                err.multipart_suggestion_verbose(
1715                    "there is a variant with a similar name",
1716                    suggestion,
1717                    Applicability::HasPlaceholders,
1718                );
1719            }
1720        }
1721
1722        if let Some(similar_candidate) = similar_candidate {
1723            // Don't emit a suggestion if we found an actual method
1724            // that had unsatisfied trait bounds
1725            if unsatisfied_predicates.is_empty()
1726                // ...or if we already suggested that name because of `rustc_confusable` annotation.
1727                && Some(similar_candidate.name()) != confusable_suggested
1728            {
1729                self.find_likely_intended_associated_item(
1730                    &mut err,
1731                    similar_candidate,
1732                    span,
1733                    args,
1734                    mode,
1735                );
1736            }
1737        }
1738
1739        if !find_candidate_for_method {
1740            self.lookup_segments_chain_for_no_match_method(
1741                &mut err,
1742                item_ident,
1743                item_kind,
1744                source,
1745                no_match_data,
1746            );
1747        }
1748
1749        self.note_derefed_ty_has_method(&mut err, source, rcvr_ty, item_ident, expected);
1750        err.emit()
1751    }
1752
1753    /// If the predicate failure is caused by an unmet bound on a tuple, recheck if the bound would
1754    /// succeed if all the types on the tuple had no borrows. This is a common problem for libraries
1755    /// like Bevy and ORMs, which rely heavily on traits being implemented on tuples.
1756    fn lookup_alternative_tuple_impls(
1757        &self,
1758        err: &mut Diag<'_>,
1759        unsatisfied_predicates: &[(
1760            ty::Predicate<'tcx>,
1761            Option<ty::Predicate<'tcx>>,
1762            Option<ObligationCause<'tcx>>,
1763        )],
1764    ) -> bool {
1765        let mut found_tuple = false;
1766        for (pred, root, _ob) in unsatisfied_predicates {
1767            let mut preds = vec![pred];
1768            if let Some(root) = root {
1769                // We will look at both the current predicate and the root predicate that caused it
1770                // to be needed. If calling something like `<(A, &B)>::default()`, then `pred` is
1771                // `&B: Default` and `root` is `(A, &B): Default`, which is the one we are checking
1772                // for further down, so we check both.
1773                preds.push(root);
1774            }
1775            for pred in preds {
1776                if let Some(clause) = pred.as_clause()
1777                    && let Some(clause) = clause.as_trait_clause()
1778                    && let ty = clause.self_ty().skip_binder()
1779                    && let ty::Tuple(types) = ty.kind()
1780                {
1781                    let path = clause.skip_binder().trait_ref.print_only_trait_path();
1782                    let def_id = clause.def_id();
1783                    let ty = Ty::new_tup(
1784                        self.tcx,
1785                        self.tcx.mk_type_list_from_iter(types.iter().map(|ty| ty.peel_refs())),
1786                    );
1787                    let args = ty::GenericArgs::for_item(self.tcx, def_id, |param, _| {
1788                        if param.index == 0 {
1789                            ty.into()
1790                        } else {
1791                            self.infcx.var_for_def(DUMMY_SP, param)
1792                        }
1793                    });
1794                    if self
1795                        .infcx
1796                        .type_implements_trait(def_id, args, self.param_env)
1797                        .must_apply_modulo_regions()
1798                    {
1799                        // "`Trait` is implemented for `(A, B)` but not for `(A, &B)`"
1800                        let mut msg = DiagStyledString::normal(format!("`{path}` "));
1801                        msg.push_highlighted("is");
1802                        msg.push_normal(" implemented for `(");
1803                        let len = types.len();
1804                        for (i, t) in types.iter().enumerate() {
1805                            msg.push(
1806                                format!("{}", with_forced_trimmed_paths!(t.peel_refs())),
1807                                t.peel_refs() != t,
1808                            );
1809                            if i < len - 1 {
1810                                msg.push_normal(", ");
1811                            }
1812                        }
1813                        msg.push_normal(")` but ");
1814                        msg.push_highlighted("not");
1815                        msg.push_normal(" for `(");
1816                        for (i, t) in types.iter().enumerate() {
1817                            msg.push(
1818                                format!("{}", with_forced_trimmed_paths!(t)),
1819                                t.peel_refs() != t,
1820                            );
1821                            if i < len - 1 {
1822                                msg.push_normal(", ");
1823                            }
1824                        }
1825                        msg.push_normal(")`");
1826
1827                        // Find the span corresponding to the impl that was found to point at it.
1828                        if let Some(impl_span) = self
1829                            .tcx
1830                            .all_impls(def_id)
1831                            .filter(|&impl_def_id| {
1832                                let header = self.tcx.impl_trait_header(impl_def_id).unwrap();
1833                                let trait_ref = header.trait_ref.instantiate(
1834                                    self.tcx,
1835                                    self.infcx.fresh_args_for_item(DUMMY_SP, impl_def_id),
1836                                );
1837
1838                                let value = ty::fold_regions(self.tcx, ty, |_, _| {
1839                                    self.tcx.lifetimes.re_erased
1840                                });
1841                                // FIXME: Don't bother dealing with non-lifetime binders here...
1842                                if value.has_escaping_bound_vars() {
1843                                    return false;
1844                                }
1845                                self.infcx.can_eq(ty::ParamEnv::empty(), trait_ref.self_ty(), value)
1846                                    && header.polarity == ty::ImplPolarity::Positive
1847                            })
1848                            .map(|impl_def_id| self.tcx.def_span(impl_def_id))
1849                            .next()
1850                        {
1851                            err.highlighted_span_note(impl_span, msg.0);
1852                        } else {
1853                            err.highlighted_note(msg.0);
1854                        }
1855                        found_tuple = true;
1856                    }
1857                    // If `pred` was already on the tuple, we don't need to look at the root
1858                    // obligation too.
1859                    break;
1860                }
1861            }
1862        }
1863        found_tuple
1864    }
1865
1866    /// If an appropriate error source is not found, check method chain for possible candidates
1867    fn lookup_segments_chain_for_no_match_method(
1868        &self,
1869        err: &mut Diag<'_>,
1870        item_name: Ident,
1871        item_kind: &str,
1872        source: SelfSource<'tcx>,
1873        no_match_data: &NoMatchData<'tcx>,
1874    ) {
1875        if no_match_data.unsatisfied_predicates.is_empty()
1876            && let Mode::MethodCall = no_match_data.mode
1877            && let SelfSource::MethodCall(mut source_expr) = source
1878        {
1879            let mut stack_methods = vec![];
1880            while let hir::ExprKind::MethodCall(_path_segment, rcvr_expr, _args, method_span) =
1881                source_expr.kind
1882            {
1883                // Pop the matching receiver, to align on it's notional span
1884                if let Some(prev_match) = stack_methods.pop() {
1885                    err.span_label(
1886                        method_span,
1887                        format!("{item_kind} `{item_name}` is available on `{prev_match}`"),
1888                    );
1889                }
1890                let rcvr_ty = self.resolve_vars_if_possible(
1891                    self.typeck_results
1892                        .borrow()
1893                        .expr_ty_adjusted_opt(rcvr_expr)
1894                        .unwrap_or(Ty::new_misc_error(self.tcx)),
1895                );
1896
1897                let Ok(candidates) = self.probe_for_name_many(
1898                    Mode::MethodCall,
1899                    item_name,
1900                    None,
1901                    IsSuggestion(true),
1902                    rcvr_ty,
1903                    source_expr.hir_id,
1904                    ProbeScope::TraitsInScope,
1905                ) else {
1906                    return;
1907                };
1908
1909                // FIXME: `probe_for_name_many` searches for methods in inherent implementations,
1910                // so it may return a candidate that doesn't belong to this `revr_ty`. We need to
1911                // check whether the instantiated type matches the received one.
1912                for _matched_method in candidates {
1913                    // found a match, push to stack
1914                    stack_methods.push(rcvr_ty);
1915                }
1916                source_expr = rcvr_expr;
1917            }
1918            // If there is a match at the start of the chain, add a label for it too!
1919            if let Some(prev_match) = stack_methods.pop() {
1920                err.span_label(
1921                    source_expr.span,
1922                    format!("{item_kind} `{item_name}` is available on `{prev_match}`"),
1923                );
1924            }
1925        }
1926    }
1927
1928    fn find_likely_intended_associated_item(
1929        &self,
1930        err: &mut Diag<'_>,
1931        similar_candidate: ty::AssocItem,
1932        span: Span,
1933        args: Option<&'tcx [hir::Expr<'tcx>]>,
1934        mode: Mode,
1935    ) {
1936        let tcx = self.tcx;
1937        let def_kind = similar_candidate.as_def_kind();
1938        let an = self.tcx.def_kind_descr_article(def_kind, similar_candidate.def_id);
1939        let similar_candidate_name = similar_candidate.name();
1940        let msg = format!(
1941            "there is {an} {} `{}` with a similar name",
1942            self.tcx.def_kind_descr(def_kind, similar_candidate.def_id),
1943            similar_candidate_name,
1944        );
1945        // Methods are defined within the context of a struct and their first parameter
1946        // is always `self`, which represents the instance of the struct the method is
1947        // being called on Associated functions don’t take self as a parameter and they are
1948        // not methods because they don’t have an instance of the struct to work with.
1949        if def_kind == DefKind::AssocFn {
1950            let ty_args = self.infcx.fresh_args_for_item(span, similar_candidate.def_id);
1951            let fn_sig = tcx.fn_sig(similar_candidate.def_id).instantiate(tcx, ty_args);
1952            let fn_sig = self.instantiate_binder_with_fresh_vars(span, infer::FnCall, fn_sig);
1953            if similar_candidate.is_method() {
1954                if let Some(args) = args
1955                    && fn_sig.inputs()[1..].len() == args.len()
1956                {
1957                    // We found a method with the same number of arguments as the method
1958                    // call expression the user wrote.
1959                    err.span_suggestion_verbose(
1960                        span,
1961                        msg,
1962                        similar_candidate_name,
1963                        Applicability::MaybeIncorrect,
1964                    );
1965                } else {
1966                    // We found a method but either the expression is not a method call or
1967                    // the argument count didn't match.
1968                    err.span_help(
1969                        tcx.def_span(similar_candidate.def_id),
1970                        format!(
1971                            "{msg}{}",
1972                            if let None = args { "" } else { ", but with different arguments" },
1973                        ),
1974                    );
1975                }
1976            } else if let Some(args) = args
1977                && fn_sig.inputs().len() == args.len()
1978            {
1979                // We have fn call expression and the argument count match the associated
1980                // function we found.
1981                err.span_suggestion_verbose(
1982                    span,
1983                    msg,
1984                    similar_candidate_name,
1985                    Applicability::MaybeIncorrect,
1986                );
1987            } else {
1988                err.span_help(tcx.def_span(similar_candidate.def_id), msg);
1989            }
1990        } else if let Mode::Path = mode
1991            && args.unwrap_or(&[]).is_empty()
1992        {
1993            // We have an associated item syntax and we found something that isn't an fn.
1994            err.span_suggestion_verbose(
1995                span,
1996                msg,
1997                similar_candidate_name,
1998                Applicability::MaybeIncorrect,
1999            );
2000        } else {
2001            // The expression is a function or method call, but the item we found is an
2002            // associated const or type.
2003            err.span_help(tcx.def_span(similar_candidate.def_id), msg);
2004        }
2005    }
2006
2007    pub(crate) fn confusable_method_name(
2008        &self,
2009        err: &mut Diag<'_>,
2010        rcvr_ty: Ty<'tcx>,
2011        item_name: Ident,
2012        call_args: Option<Vec<Ty<'tcx>>>,
2013    ) -> Option<Symbol> {
2014        if let ty::Adt(adt, adt_args) = rcvr_ty.kind() {
2015            for inherent_impl_did in self.tcx.inherent_impls(adt.did()).into_iter() {
2016                for inherent_method in
2017                    self.tcx.associated_items(inherent_impl_did).in_definition_order()
2018                {
2019                    if let Some(candidates) = find_attr!(self.tcx.get_all_attrs(inherent_method.def_id), AttributeKind::Confusables{symbols, ..} => symbols)
2020                        && candidates.contains(&item_name.name)
2021                        && inherent_method.is_fn()
2022                    {
2023                        let args =
2024                            ty::GenericArgs::identity_for_item(self.tcx, inherent_method.def_id)
2025                                .rebase_onto(
2026                                    self.tcx,
2027                                    inherent_method.container_id(self.tcx),
2028                                    adt_args,
2029                                );
2030                        let fn_sig =
2031                            self.tcx.fn_sig(inherent_method.def_id).instantiate(self.tcx, args);
2032                        let fn_sig = self.instantiate_binder_with_fresh_vars(
2033                            item_name.span,
2034                            infer::FnCall,
2035                            fn_sig,
2036                        );
2037                        let name = inherent_method.name();
2038                        if let Some(ref args) = call_args
2039                            && fn_sig.inputs()[1..]
2040                                .iter()
2041                                .zip(args.into_iter())
2042                                .all(|(expected, found)| self.may_coerce(*expected, *found))
2043                            && fn_sig.inputs()[1..].len() == args.len()
2044                        {
2045                            err.span_suggestion_verbose(
2046                                item_name.span,
2047                                format!("you might have meant to use `{}`", name),
2048                                name,
2049                                Applicability::MaybeIncorrect,
2050                            );
2051                            return Some(name);
2052                        } else if let None = call_args {
2053                            err.span_note(
2054                                self.tcx.def_span(inherent_method.def_id),
2055                                format!("you might have meant to use method `{}`", name),
2056                            );
2057                            return Some(name);
2058                        }
2059                    }
2060                }
2061            }
2062        }
2063        None
2064    }
2065    fn note_candidates_on_method_error(
2066        &self,
2067        rcvr_ty: Ty<'tcx>,
2068        item_name: Ident,
2069        self_source: SelfSource<'tcx>,
2070        args: Option<&'tcx [hir::Expr<'tcx>]>,
2071        span: Span,
2072        err: &mut Diag<'_>,
2073        sources: &mut Vec<CandidateSource>,
2074        sugg_span: Option<Span>,
2075    ) {
2076        sources.sort_by_key(|source| match source {
2077            CandidateSource::Trait(id) => (0, self.tcx.def_path_str(id)),
2078            CandidateSource::Impl(id) => (1, self.tcx.def_path_str(id)),
2079        });
2080        sources.dedup();
2081        // Dynamic limit to avoid hiding just one candidate, which is silly.
2082        let limit = if sources.len() == 5 { 5 } else { 4 };
2083
2084        let mut suggs = vec![];
2085        for (idx, source) in sources.iter().take(limit).enumerate() {
2086            match *source {
2087                CandidateSource::Impl(impl_did) => {
2088                    // Provide the best span we can. Use the item, if local to crate, else
2089                    // the impl, if local to crate (item may be defaulted), else nothing.
2090                    let Some(item) = self.associated_value(impl_did, item_name).or_else(|| {
2091                        let impl_trait_ref = self.tcx.impl_trait_ref(impl_did)?;
2092                        self.associated_value(impl_trait_ref.skip_binder().def_id, item_name)
2093                    }) else {
2094                        continue;
2095                    };
2096
2097                    let note_span = if item.def_id.is_local() {
2098                        Some(self.tcx.def_span(item.def_id))
2099                    } else if impl_did.is_local() {
2100                        Some(self.tcx.def_span(impl_did))
2101                    } else {
2102                        None
2103                    };
2104
2105                    let impl_ty = self.tcx.at(span).type_of(impl_did).instantiate_identity();
2106
2107                    let insertion = match self.tcx.impl_trait_ref(impl_did) {
2108                        None => String::new(),
2109                        Some(trait_ref) => {
2110                            format!(
2111                                " of the trait `{}`",
2112                                self.tcx.def_path_str(trait_ref.skip_binder().def_id)
2113                            )
2114                        }
2115                    };
2116
2117                    let (note_str, idx) = if sources.len() > 1 {
2118                        (
2119                            format!(
2120                                "candidate #{} is defined in an impl{} for the type `{}`",
2121                                idx + 1,
2122                                insertion,
2123                                impl_ty,
2124                            ),
2125                            Some(idx + 1),
2126                        )
2127                    } else {
2128                        (
2129                            format!(
2130                                "the candidate is defined in an impl{insertion} for the type `{impl_ty}`",
2131                            ),
2132                            None,
2133                        )
2134                    };
2135                    if let Some(note_span) = note_span {
2136                        // We have a span pointing to the method. Show note with snippet.
2137                        err.span_note(note_span, note_str);
2138                    } else {
2139                        err.note(note_str);
2140                    }
2141                    if let Some(sugg_span) = sugg_span
2142                        && let Some(trait_ref) = self.tcx.impl_trait_ref(impl_did)
2143                        && let Some(sugg) = print_disambiguation_help(
2144                            self.tcx,
2145                            err,
2146                            self_source,
2147                            args,
2148                            trait_ref
2149                                .instantiate(
2150                                    self.tcx,
2151                                    self.fresh_args_for_item(sugg_span, impl_did),
2152                                )
2153                                .with_self_ty(self.tcx, rcvr_ty),
2154                            idx,
2155                            sugg_span,
2156                            item,
2157                        )
2158                    {
2159                        suggs.push(sugg);
2160                    }
2161                }
2162                CandidateSource::Trait(trait_did) => {
2163                    let Some(item) = self.associated_value(trait_did, item_name) else { continue };
2164                    let item_span = self.tcx.def_span(item.def_id);
2165                    let idx = if sources.len() > 1 {
2166                        let msg = format!(
2167                            "candidate #{} is defined in the trait `{}`",
2168                            idx + 1,
2169                            self.tcx.def_path_str(trait_did)
2170                        );
2171                        err.span_note(item_span, msg);
2172                        Some(idx + 1)
2173                    } else {
2174                        let msg = format!(
2175                            "the candidate is defined in the trait `{}`",
2176                            self.tcx.def_path_str(trait_did)
2177                        );
2178                        err.span_note(item_span, msg);
2179                        None
2180                    };
2181                    if let Some(sugg_span) = sugg_span
2182                        && let Some(sugg) = print_disambiguation_help(
2183                            self.tcx,
2184                            err,
2185                            self_source,
2186                            args,
2187                            ty::TraitRef::new_from_args(
2188                                self.tcx,
2189                                trait_did,
2190                                self.fresh_args_for_item(sugg_span, trait_did),
2191                            )
2192                            .with_self_ty(self.tcx, rcvr_ty),
2193                            idx,
2194                            sugg_span,
2195                            item,
2196                        )
2197                    {
2198                        suggs.push(sugg);
2199                    }
2200                }
2201            }
2202        }
2203        if !suggs.is_empty()
2204            && let Some(span) = sugg_span
2205        {
2206            suggs.sort();
2207            err.span_suggestions(
2208                span.with_hi(item_name.span.lo()),
2209                "use fully-qualified syntax to disambiguate",
2210                suggs,
2211                Applicability::MachineApplicable,
2212            );
2213        }
2214        if sources.len() > limit {
2215            err.note(format!("and {} others", sources.len() - limit));
2216        }
2217    }
2218
2219    /// Look at all the associated functions without receivers in the type's inherent impls
2220    /// to look for builders that return `Self`, `Option<Self>` or `Result<Self, _>`.
2221    fn find_builder_fn(&self, err: &mut Diag<'_>, rcvr_ty: Ty<'tcx>, expr_id: hir::HirId) {
2222        let ty::Adt(adt_def, _) = rcvr_ty.kind() else {
2223            return;
2224        };
2225        let mut items = self
2226            .tcx
2227            .inherent_impls(adt_def.did())
2228            .iter()
2229            .flat_map(|i| self.tcx.associated_items(i).in_definition_order())
2230            // Only assoc fn with no receivers and only if
2231            // they are resolvable
2232            .filter(|item| {
2233                matches!(item.kind, ty::AssocKind::Fn { has_self: false, .. })
2234                    && self
2235                        .probe_for_name(
2236                            Mode::Path,
2237                            item.ident(self.tcx),
2238                            None,
2239                            IsSuggestion(true),
2240                            rcvr_ty,
2241                            expr_id,
2242                            ProbeScope::TraitsInScope,
2243                        )
2244                        .is_ok()
2245            })
2246            .filter_map(|item| {
2247                // Only assoc fns that return `Self`, `Option<Self>` or `Result<Self, _>`.
2248                let ret_ty = self
2249                    .tcx
2250                    .fn_sig(item.def_id)
2251                    .instantiate(self.tcx, self.fresh_args_for_item(DUMMY_SP, item.def_id))
2252                    .output();
2253                let ret_ty = self.tcx.instantiate_bound_regions_with_erased(ret_ty);
2254                let ty::Adt(def, args) = ret_ty.kind() else {
2255                    return None;
2256                };
2257                // Check for `-> Self`
2258                if self.can_eq(self.param_env, ret_ty, rcvr_ty) {
2259                    return Some((item.def_id, ret_ty));
2260                }
2261                // Check for `-> Option<Self>` or `-> Result<Self, _>`
2262                if ![self.tcx.lang_items().option_type(), self.tcx.get_diagnostic_item(sym::Result)]
2263                    .contains(&Some(def.did()))
2264                {
2265                    return None;
2266                }
2267                let arg = args.get(0)?.expect_ty();
2268                if self.can_eq(self.param_env, rcvr_ty, arg) {
2269                    Some((item.def_id, ret_ty))
2270                } else {
2271                    None
2272                }
2273            })
2274            .collect::<Vec<_>>();
2275        let post = if items.len() > 5 {
2276            let items_len = items.len();
2277            items.truncate(4);
2278            format!("\nand {} others", items_len - 4)
2279        } else {
2280            String::new()
2281        };
2282        match &items[..] {
2283            [] => {}
2284            [(def_id, ret_ty)] => {
2285                err.span_note(
2286                    self.tcx.def_span(def_id),
2287                    format!(
2288                        "if you're trying to build a new `{rcvr_ty}`, consider using `{}` which \
2289                         returns `{ret_ty}`",
2290                        self.tcx.def_path_str(def_id),
2291                    ),
2292                );
2293            }
2294            _ => {
2295                let span: MultiSpan = items
2296                    .iter()
2297                    .map(|(def_id, _)| self.tcx.def_span(def_id))
2298                    .collect::<Vec<Span>>()
2299                    .into();
2300                err.span_note(
2301                    span,
2302                    format!(
2303                        "if you're trying to build a new `{rcvr_ty}` consider using one of the \
2304                         following associated functions:\n{}{post}",
2305                        items
2306                            .iter()
2307                            .map(|(def_id, _ret_ty)| self.tcx.def_path_str(def_id))
2308                            .collect::<Vec<String>>()
2309                            .join("\n")
2310                    ),
2311                );
2312            }
2313        }
2314    }
2315
2316    /// Suggest calling `Ty::method` if `.method()` isn't found because the method
2317    /// doesn't take a `self` receiver.
2318    fn suggest_associated_call_syntax(
2319        &self,
2320        err: &mut Diag<'_>,
2321        static_candidates: &Vec<CandidateSource>,
2322        rcvr_ty: Ty<'tcx>,
2323        source: SelfSource<'tcx>,
2324        item_name: Ident,
2325        args: Option<&'tcx [hir::Expr<'tcx>]>,
2326        sugg_span: Span,
2327    ) {
2328        let mut has_unsuggestable_args = false;
2329        let ty_str = if let Some(CandidateSource::Impl(impl_did)) = static_candidates.get(0) {
2330            // When the "method" is resolved through dereferencing, we really want the
2331            // original type that has the associated function for accurate suggestions.
2332            // (#61411)
2333            let impl_ty = self.tcx.type_of(*impl_did).instantiate_identity();
2334            let target_ty = self
2335                .autoderef(sugg_span, rcvr_ty)
2336                .silence_errors()
2337                .find(|(rcvr_ty, _)| {
2338                    DeepRejectCtxt::relate_rigid_infer(self.tcx).types_may_unify(*rcvr_ty, impl_ty)
2339                })
2340                .map_or(impl_ty, |(ty, _)| ty)
2341                .peel_refs();
2342            if let ty::Adt(def, args) = target_ty.kind() {
2343                // If there are any inferred arguments, (`{integer}`), we should replace
2344                // them with underscores to allow the compiler to infer them
2345                let infer_args = self.tcx.mk_args_from_iter(args.into_iter().map(|arg| {
2346                    if !arg.is_suggestable(self.tcx, true) {
2347                        has_unsuggestable_args = true;
2348                        match arg.kind() {
2349                            GenericArgKind::Lifetime(_) => self
2350                                .next_region_var(RegionVariableOrigin::MiscVariable(DUMMY_SP))
2351                                .into(),
2352                            GenericArgKind::Type(_) => self.next_ty_var(DUMMY_SP).into(),
2353                            GenericArgKind::Const(_) => self.next_const_var(DUMMY_SP).into(),
2354                        }
2355                    } else {
2356                        arg
2357                    }
2358                }));
2359
2360                self.tcx.value_path_str_with_args(def.did(), infer_args)
2361            } else {
2362                self.ty_to_value_string(target_ty)
2363            }
2364        } else {
2365            self.ty_to_value_string(rcvr_ty.peel_refs())
2366        };
2367        if let SelfSource::MethodCall(_) = source {
2368            let first_arg = static_candidates.get(0).and_then(|candidate_source| {
2369                let (assoc_did, self_ty) = match candidate_source {
2370                    CandidateSource::Impl(impl_did) => {
2371                        (*impl_did, self.tcx.type_of(*impl_did).instantiate_identity())
2372                    }
2373                    CandidateSource::Trait(trait_did) => (*trait_did, rcvr_ty),
2374                };
2375
2376                let assoc = self.associated_value(assoc_did, item_name)?;
2377                if !assoc.is_fn() {
2378                    return None;
2379                }
2380
2381                // for CandidateSource::Impl, `Self` will be instantiated to a concrete type
2382                // but for CandidateSource::Trait, `Self` is still `Self`
2383                let sig = self.tcx.fn_sig(assoc.def_id).instantiate_identity();
2384                sig.inputs().skip_binder().get(0).and_then(|first| {
2385                    // if the type of first arg is the same as the current impl type, we should take the first arg into assoc function
2386                    let first_ty = first.peel_refs();
2387                    if first_ty == self_ty || first_ty == self.tcx.types.self_param {
2388                        Some(first.ref_mutability().map_or("", |mutbl| mutbl.ref_prefix_str()))
2389                    } else {
2390                        None
2391                    }
2392                })
2393            });
2394
2395            let mut applicability = Applicability::MachineApplicable;
2396            let args = if let SelfSource::MethodCall(receiver) = source
2397                && let Some(args) = args
2398            {
2399                // The first arg is the same kind as the receiver
2400                let explicit_args = if first_arg.is_some() {
2401                    std::iter::once(receiver).chain(args.iter()).collect::<Vec<_>>()
2402                } else {
2403                    // There is no `Self` kind to infer the arguments from
2404                    if has_unsuggestable_args {
2405                        applicability = Applicability::HasPlaceholders;
2406                    }
2407                    args.iter().collect()
2408                };
2409                format!(
2410                    "({}{})",
2411                    first_arg.unwrap_or(""),
2412                    explicit_args
2413                        .iter()
2414                        .map(|arg| self
2415                            .tcx
2416                            .sess
2417                            .source_map()
2418                            .span_to_snippet(arg.span)
2419                            .unwrap_or_else(|_| {
2420                                applicability = Applicability::HasPlaceholders;
2421                                "_".to_owned()
2422                            }))
2423                        .collect::<Vec<_>>()
2424                        .join(", "),
2425                )
2426            } else {
2427                applicability = Applicability::HasPlaceholders;
2428                "(...)".to_owned()
2429            };
2430            err.span_suggestion(
2431                sugg_span,
2432                "use associated function syntax instead",
2433                format!("{ty_str}::{item_name}{args}"),
2434                applicability,
2435            );
2436        } else {
2437            err.help(format!("try with `{ty_str}::{item_name}`",));
2438        }
2439    }
2440
2441    /// Suggest calling a field with a type that implements the `Fn*` traits instead of a method with
2442    /// the same name as the field i.e. `(a.my_fn_ptr)(10)` instead of `a.my_fn_ptr(10)`.
2443    fn suggest_calling_field_as_fn(
2444        &self,
2445        span: Span,
2446        rcvr_ty: Ty<'tcx>,
2447        expr: &hir::Expr<'_>,
2448        item_name: Ident,
2449        err: &mut Diag<'_>,
2450    ) -> bool {
2451        let tcx = self.tcx;
2452        let field_receiver =
2453            self.autoderef(span, rcvr_ty).silence_errors().find_map(|(ty, _)| match ty.kind() {
2454                ty::Adt(def, args) if !def.is_enum() => {
2455                    let variant = &def.non_enum_variant();
2456                    tcx.find_field_index(item_name, variant).map(|index| {
2457                        let field = &variant.fields[index];
2458                        let field_ty = field.ty(tcx, args);
2459                        (field, field_ty)
2460                    })
2461                }
2462                _ => None,
2463            });
2464        if let Some((field, field_ty)) = field_receiver {
2465            let scope = tcx.parent_module_from_def_id(self.body_id);
2466            let is_accessible = field.vis.is_accessible_from(scope, tcx);
2467
2468            if is_accessible {
2469                if let Some((what, _, _)) = self.extract_callable_info(field_ty) {
2470                    let what = match what {
2471                        DefIdOrName::DefId(def_id) => self.tcx.def_descr(def_id),
2472                        DefIdOrName::Name(what) => what,
2473                    };
2474                    let expr_span = expr.span.to(item_name.span);
2475                    err.multipart_suggestion(
2476                        format!(
2477                            "to call the {what} stored in `{item_name}`, \
2478                            surround the field access with parentheses",
2479                        ),
2480                        vec![
2481                            (expr_span.shrink_to_lo(), '('.to_string()),
2482                            (expr_span.shrink_to_hi(), ')'.to_string()),
2483                        ],
2484                        Applicability::MachineApplicable,
2485                    );
2486                } else {
2487                    let call_expr = tcx.hir_expect_expr(tcx.parent_hir_id(expr.hir_id));
2488
2489                    if let Some(span) = call_expr.span.trim_start(item_name.span) {
2490                        err.span_suggestion(
2491                            span,
2492                            "remove the arguments",
2493                            "",
2494                            Applicability::MaybeIncorrect,
2495                        );
2496                    }
2497                }
2498            }
2499
2500            let field_kind = if is_accessible { "field" } else { "private field" };
2501            err.span_label(item_name.span, format!("{field_kind}, not a method"));
2502            return true;
2503        }
2504        false
2505    }
2506
2507    /// Suggest possible range with adding parentheses, for example:
2508    /// when encountering `0..1.map(|i| i + 1)` suggest `(0..1).map(|i| i + 1)`.
2509    fn report_failed_method_call_on_range_end(
2510        &self,
2511        tcx: TyCtxt<'tcx>,
2512        actual: Ty<'tcx>,
2513        source: SelfSource<'tcx>,
2514        span: Span,
2515        item_name: Ident,
2516        ty_str: &str,
2517        long_ty_path: &mut Option<PathBuf>,
2518    ) -> Result<(), ErrorGuaranteed> {
2519        if let SelfSource::MethodCall(expr) = source {
2520            for (_, parent) in tcx.hir_parent_iter(expr.hir_id).take(5) {
2521                if let Node::Expr(parent_expr) = parent {
2522                    let lang_item = match parent_expr.kind {
2523                        ExprKind::Struct(qpath, _, _) => match *qpath {
2524                            QPath::LangItem(LangItem::Range, ..) => Some(LangItem::Range),
2525                            QPath::LangItem(LangItem::RangeCopy, ..) => Some(LangItem::RangeCopy),
2526                            QPath::LangItem(LangItem::RangeInclusiveCopy, ..) => {
2527                                Some(LangItem::RangeInclusiveCopy)
2528                            }
2529                            QPath::LangItem(LangItem::RangeTo, ..) => Some(LangItem::RangeTo),
2530                            QPath::LangItem(LangItem::RangeToInclusive, ..) => {
2531                                Some(LangItem::RangeToInclusive)
2532                            }
2533                            _ => None,
2534                        },
2535                        ExprKind::Call(func, _) => match func.kind {
2536                            // `..=` desugars into `::std::ops::RangeInclusive::new(...)`.
2537                            ExprKind::Path(QPath::LangItem(LangItem::RangeInclusiveNew, ..)) => {
2538                                Some(LangItem::RangeInclusiveStruct)
2539                            }
2540                            _ => None,
2541                        },
2542                        _ => None,
2543                    };
2544
2545                    if lang_item.is_none() {
2546                        continue;
2547                    }
2548
2549                    let span_included = match parent_expr.kind {
2550                        hir::ExprKind::Struct(_, eps, _) => {
2551                            eps.len() > 0 && eps.last().is_some_and(|ep| ep.span.contains(span))
2552                        }
2553                        // `..=` desugars into `::std::ops::RangeInclusive::new(...)`.
2554                        hir::ExprKind::Call(func, ..) => func.span.contains(span),
2555                        _ => false,
2556                    };
2557
2558                    if !span_included {
2559                        continue;
2560                    }
2561
2562                    let Some(range_def_id) =
2563                        lang_item.and_then(|lang_item| self.tcx.lang_items().get(lang_item))
2564                    else {
2565                        continue;
2566                    };
2567                    let range_ty =
2568                        self.tcx.type_of(range_def_id).instantiate(self.tcx, &[actual.into()]);
2569
2570                    let pick = self.lookup_probe_for_diagnostic(
2571                        item_name,
2572                        range_ty,
2573                        expr,
2574                        ProbeScope::AllTraits,
2575                        None,
2576                    );
2577                    if pick.is_ok() {
2578                        let range_span = parent_expr.span.with_hi(expr.span.hi());
2579                        let mut err = self.dcx().create_err(errors::MissingParenthesesInRange {
2580                            span,
2581                            ty_str: ty_str.to_string(),
2582                            method_name: item_name.as_str().to_string(),
2583                            add_missing_parentheses: Some(errors::AddMissingParenthesesInRange {
2584                                func_name: item_name.name.as_str().to_string(),
2585                                left: range_span.shrink_to_lo(),
2586                                right: range_span.shrink_to_hi(),
2587                            }),
2588                        });
2589                        *err.long_ty_path() = long_ty_path.take();
2590                        return Err(err.emit());
2591                    }
2592                }
2593            }
2594        }
2595        Ok(())
2596    }
2597
2598    fn report_failed_method_call_on_numerical_infer_var(
2599        &self,
2600        tcx: TyCtxt<'tcx>,
2601        actual: Ty<'tcx>,
2602        source: SelfSource<'_>,
2603        span: Span,
2604        item_kind: &str,
2605        item_name: Ident,
2606        ty_str: &str,
2607        long_ty_path: &mut Option<PathBuf>,
2608    ) -> Result<(), ErrorGuaranteed> {
2609        let found_candidate = all_traits(self.tcx)
2610            .into_iter()
2611            .any(|info| self.associated_value(info.def_id, item_name).is_some());
2612        let found_assoc = |ty: Ty<'tcx>| {
2613            simplify_type(tcx, ty, TreatParams::InstantiateWithInfer)
2614                .and_then(|simp| {
2615                    tcx.incoherent_impls(simp)
2616                        .into_iter()
2617                        .find_map(|&id| self.associated_value(id, item_name))
2618                })
2619                .is_some()
2620        };
2621        let found_candidate = found_candidate
2622            || found_assoc(tcx.types.i8)
2623            || found_assoc(tcx.types.i16)
2624            || found_assoc(tcx.types.i32)
2625            || found_assoc(tcx.types.i64)
2626            || found_assoc(tcx.types.i128)
2627            || found_assoc(tcx.types.u8)
2628            || found_assoc(tcx.types.u16)
2629            || found_assoc(tcx.types.u32)
2630            || found_assoc(tcx.types.u64)
2631            || found_assoc(tcx.types.u128)
2632            || found_assoc(tcx.types.f32)
2633            || found_assoc(tcx.types.f64);
2634        if found_candidate
2635            && actual.is_numeric()
2636            && !actual.has_concrete_skeleton()
2637            && let SelfSource::MethodCall(expr) = source
2638        {
2639            let mut err = struct_span_code_err!(
2640                self.dcx(),
2641                span,
2642                E0689,
2643                "can't call {} `{}` on ambiguous numeric type `{}`",
2644                item_kind,
2645                item_name,
2646                ty_str
2647            );
2648            *err.long_ty_path() = long_ty_path.take();
2649            let concrete_type = if actual.is_integral() { "i32" } else { "f32" };
2650            match expr.kind {
2651                ExprKind::Lit(lit) => {
2652                    // numeric literal
2653                    let snippet = tcx
2654                        .sess
2655                        .source_map()
2656                        .span_to_snippet(lit.span)
2657                        .unwrap_or_else(|_| "<numeric literal>".to_owned());
2658
2659                    // If this is a floating point literal that ends with '.',
2660                    // get rid of it to stop this from becoming a member access.
2661                    let snippet = snippet.strip_suffix('.').unwrap_or(&snippet);
2662                    err.span_suggestion(
2663                        lit.span,
2664                        format!(
2665                            "you must specify a concrete type for this numeric value, \
2666                                         like `{concrete_type}`"
2667                        ),
2668                        format!("{snippet}_{concrete_type}"),
2669                        Applicability::MaybeIncorrect,
2670                    );
2671                }
2672                ExprKind::Path(QPath::Resolved(_, path)) => {
2673                    // local binding
2674                    if let hir::def::Res::Local(hir_id) = path.res {
2675                        let span = tcx.hir_span(hir_id);
2676                        let filename = tcx.sess.source_map().span_to_filename(span);
2677
2678                        let parent_node = self.tcx.parent_hir_node(hir_id);
2679                        let msg = format!(
2680                            "you must specify a type for this binding, like `{concrete_type}`",
2681                        );
2682
2683                        match (filename, parent_node) {
2684                            (
2685                                FileName::Real(_),
2686                                Node::LetStmt(hir::LetStmt {
2687                                    source: hir::LocalSource::Normal,
2688                                    ty,
2689                                    ..
2690                                }),
2691                            ) => {
2692                                let type_span = ty
2693                                    .map(|ty| ty.span.with_lo(span.hi()))
2694                                    .unwrap_or(span.shrink_to_hi());
2695                                err.span_suggestion(
2696                                    // account for `let x: _ = 42;`
2697                                    //                   ^^^
2698                                    type_span,
2699                                    msg,
2700                                    format!(": {concrete_type}"),
2701                                    Applicability::MaybeIncorrect,
2702                                );
2703                            }
2704                            _ => {
2705                                err.span_label(span, msg);
2706                            }
2707                        }
2708                    }
2709                }
2710                _ => {}
2711            }
2712            return Err(err.emit());
2713        }
2714        Ok(())
2715    }
2716
2717    /// For code `rect::area(...)`,
2718    /// if `rect` is a local variable and `area` is a valid assoc method for it,
2719    /// we try to suggest `rect.area()`
2720    pub(crate) fn suggest_assoc_method_call(&self, segs: &[PathSegment<'_>]) {
2721        debug!("suggest_assoc_method_call segs: {:?}", segs);
2722        let [seg1, seg2] = segs else {
2723            return;
2724        };
2725        self.dcx().try_steal_modify_and_emit_err(
2726            seg1.ident.span,
2727            StashKey::CallAssocMethod,
2728            |err| {
2729                let body = self.tcx.hir_body_owned_by(self.body_id);
2730                struct LetVisitor {
2731                    ident_name: Symbol,
2732                }
2733
2734                // FIXME: This really should be taking scoping, etc into account.
2735                impl<'v> Visitor<'v> for LetVisitor {
2736                    type Result = ControlFlow<Option<&'v hir::Expr<'v>>>;
2737                    fn visit_stmt(&mut self, ex: &'v hir::Stmt<'v>) -> Self::Result {
2738                        if let hir::StmtKind::Let(&hir::LetStmt { pat, init, .. }) = ex.kind
2739                            && let hir::PatKind::Binding(_, _, ident, ..) = pat.kind
2740                            && ident.name == self.ident_name
2741                        {
2742                            ControlFlow::Break(init)
2743                        } else {
2744                            hir::intravisit::walk_stmt(self, ex)
2745                        }
2746                    }
2747                }
2748
2749                if let Node::Expr(call_expr) = self.tcx.parent_hir_node(seg1.hir_id)
2750                    && let ControlFlow::Break(Some(expr)) =
2751                        (LetVisitor { ident_name: seg1.ident.name }).visit_body(&body)
2752                    && let Some(self_ty) = self.node_ty_opt(expr.hir_id)
2753                {
2754                    let probe = self.lookup_probe_for_diagnostic(
2755                        seg2.ident,
2756                        self_ty,
2757                        call_expr,
2758                        ProbeScope::TraitsInScope,
2759                        None,
2760                    );
2761                    if probe.is_ok() {
2762                        let sm = self.infcx.tcx.sess.source_map();
2763                        err.span_suggestion_verbose(
2764                            sm.span_extend_while(seg1.ident.span.shrink_to_hi(), |c| c == ':')
2765                                .unwrap(),
2766                            "you may have meant to call an instance method",
2767                            ".",
2768                            Applicability::MaybeIncorrect,
2769                        );
2770                    }
2771                }
2772            },
2773        );
2774    }
2775
2776    /// Suggest calling a method on a field i.e. `a.field.bar()` instead of `a.bar()`
2777    fn suggest_calling_method_on_field(
2778        &self,
2779        err: &mut Diag<'_>,
2780        source: SelfSource<'tcx>,
2781        span: Span,
2782        actual: Ty<'tcx>,
2783        item_name: Ident,
2784        return_type: Option<Ty<'tcx>>,
2785    ) {
2786        if let SelfSource::MethodCall(expr) = source {
2787            let mod_id = self.tcx.parent_module(expr.hir_id).to_def_id();
2788            for (fields, args) in self.get_field_candidates_considering_privacy_for_diag(
2789                span,
2790                actual,
2791                mod_id,
2792                expr.hir_id,
2793            ) {
2794                let call_expr = self.tcx.hir_expect_expr(self.tcx.parent_hir_id(expr.hir_id));
2795
2796                let lang_items = self.tcx.lang_items();
2797                let never_mention_traits = [
2798                    lang_items.clone_trait(),
2799                    lang_items.deref_trait(),
2800                    lang_items.deref_mut_trait(),
2801                    self.tcx.get_diagnostic_item(sym::AsRef),
2802                    self.tcx.get_diagnostic_item(sym::AsMut),
2803                    self.tcx.get_diagnostic_item(sym::Borrow),
2804                    self.tcx.get_diagnostic_item(sym::BorrowMut),
2805                ];
2806                let mut candidate_fields: Vec<_> = fields
2807                    .into_iter()
2808                    .filter_map(|candidate_field| {
2809                        self.check_for_nested_field_satisfying_condition_for_diag(
2810                            span,
2811                            &|_, field_ty| {
2812                                self.lookup_probe_for_diagnostic(
2813                                    item_name,
2814                                    field_ty,
2815                                    call_expr,
2816                                    ProbeScope::TraitsInScope,
2817                                    return_type,
2818                                )
2819                                .is_ok_and(|pick| {
2820                                    !never_mention_traits
2821                                        .iter()
2822                                        .flatten()
2823                                        .any(|def_id| self.tcx.parent(pick.item.def_id) == *def_id)
2824                                })
2825                            },
2826                            candidate_field,
2827                            args,
2828                            vec![],
2829                            mod_id,
2830                            expr.hir_id,
2831                        )
2832                    })
2833                    .map(|field_path| {
2834                        field_path
2835                            .iter()
2836                            .map(|id| id.to_string())
2837                            .collect::<Vec<String>>()
2838                            .join(".")
2839                    })
2840                    .collect();
2841                candidate_fields.sort();
2842
2843                let len = candidate_fields.len();
2844                if len > 0 {
2845                    err.span_suggestions(
2846                        item_name.span.shrink_to_lo(),
2847                        format!(
2848                            "{} of the expressions' fields {} a method of the same name",
2849                            if len > 1 { "some" } else { "one" },
2850                            if len > 1 { "have" } else { "has" },
2851                        ),
2852                        candidate_fields.iter().map(|path| format!("{path}.")),
2853                        Applicability::MaybeIncorrect,
2854                    );
2855                }
2856            }
2857        }
2858    }
2859
2860    fn suggest_unwrapping_inner_self(
2861        &self,
2862        err: &mut Diag<'_>,
2863        source: SelfSource<'tcx>,
2864        actual: Ty<'tcx>,
2865        item_name: Ident,
2866    ) {
2867        let tcx = self.tcx;
2868        let SelfSource::MethodCall(expr) = source else {
2869            return;
2870        };
2871        let call_expr = tcx.hir_expect_expr(tcx.parent_hir_id(expr.hir_id));
2872
2873        let ty::Adt(kind, args) = actual.kind() else {
2874            return;
2875        };
2876        match kind.adt_kind() {
2877            ty::AdtKind::Enum => {
2878                let matching_variants: Vec<_> = kind
2879                    .variants()
2880                    .iter()
2881                    .flat_map(|variant| {
2882                        let [field] = &variant.fields.raw[..] else {
2883                            return None;
2884                        };
2885                        let field_ty = field.ty(tcx, args);
2886
2887                        // Skip `_`, since that'll just lead to ambiguity.
2888                        if self.resolve_vars_if_possible(field_ty).is_ty_var() {
2889                            return None;
2890                        }
2891
2892                        self.lookup_probe_for_diagnostic(
2893                            item_name,
2894                            field_ty,
2895                            call_expr,
2896                            ProbeScope::TraitsInScope,
2897                            None,
2898                        )
2899                        .ok()
2900                        .map(|pick| (variant, field, pick))
2901                    })
2902                    .collect();
2903
2904                let ret_ty_matches = |diagnostic_item| {
2905                    if let Some(ret_ty) = self
2906                        .ret_coercion
2907                        .as_ref()
2908                        .map(|c| self.resolve_vars_if_possible(c.borrow().expected_ty()))
2909                        && let ty::Adt(kind, _) = ret_ty.kind()
2910                        && tcx.get_diagnostic_item(diagnostic_item) == Some(kind.did())
2911                    {
2912                        true
2913                    } else {
2914                        false
2915                    }
2916                };
2917
2918                match &matching_variants[..] {
2919                    [(_, field, pick)] => {
2920                        let self_ty = field.ty(tcx, args);
2921                        err.span_note(
2922                            tcx.def_span(pick.item.def_id),
2923                            format!("the method `{item_name}` exists on the type `{self_ty}`"),
2924                        );
2925                        let (article, kind, variant, question) =
2926                            if tcx.is_diagnostic_item(sym::Result, kind.did()) {
2927                                ("a", "Result", "Err", ret_ty_matches(sym::Result))
2928                            } else if tcx.is_diagnostic_item(sym::Option, kind.did()) {
2929                                ("an", "Option", "None", ret_ty_matches(sym::Option))
2930                            } else {
2931                                return;
2932                            };
2933                        if question {
2934                            err.span_suggestion_verbose(
2935                                expr.span.shrink_to_hi(),
2936                                format!(
2937                                    "use the `?` operator to extract the `{self_ty}` value, propagating \
2938                                    {article} `{kind}::{variant}` value to the caller"
2939                                ),
2940                                "?",
2941                                Applicability::MachineApplicable,
2942                            );
2943                        } else {
2944                            err.span_suggestion_verbose(
2945                                expr.span.shrink_to_hi(),
2946                                format!(
2947                                    "consider using `{kind}::expect` to unwrap the `{self_ty}` value, \
2948                                    panicking if the value is {article} `{kind}::{variant}`"
2949                                ),
2950                                ".expect(\"REASON\")",
2951                                Applicability::HasPlaceholders,
2952                            );
2953                        }
2954                    }
2955                    // FIXME(compiler-errors): Support suggestions for other matching enum variants
2956                    _ => {}
2957                }
2958            }
2959            // Target wrapper types - types that wrap or pretend to wrap another type,
2960            // perhaps this inner type is meant to be called?
2961            ty::AdtKind::Struct | ty::AdtKind::Union => {
2962                let [first] = ***args else {
2963                    return;
2964                };
2965                let ty::GenericArgKind::Type(ty) = first.kind() else {
2966                    return;
2967                };
2968                let Ok(pick) = self.lookup_probe_for_diagnostic(
2969                    item_name,
2970                    ty,
2971                    call_expr,
2972                    ProbeScope::TraitsInScope,
2973                    None,
2974                ) else {
2975                    return;
2976                };
2977
2978                let name = self.ty_to_value_string(actual);
2979                let inner_id = kind.did();
2980                let mutable = if let Some(AutorefOrPtrAdjustment::Autoref { mutbl, .. }) =
2981                    pick.autoref_or_ptr_adjustment
2982                {
2983                    Some(mutbl)
2984                } else {
2985                    None
2986                };
2987
2988                if tcx.is_diagnostic_item(sym::LocalKey, inner_id) {
2989                    err.help("use `with` or `try_with` to access thread local storage");
2990                } else if tcx.is_lang_item(kind.did(), LangItem::MaybeUninit) {
2991                    err.help(format!(
2992                        "if this `{name}` has been initialized, \
2993                        use one of the `assume_init` methods to access the inner value"
2994                    ));
2995                } else if tcx.is_diagnostic_item(sym::RefCell, inner_id) {
2996                    let (suggestion, borrow_kind, panic_if) = match mutable {
2997                        Some(Mutability::Not) => (".borrow()", "borrow", "a mutable borrow exists"),
2998                        Some(Mutability::Mut) => {
2999                            (".borrow_mut()", "mutably borrow", "any borrows exist")
3000                        }
3001                        None => return,
3002                    };
3003                    err.span_suggestion_verbose(
3004                        expr.span.shrink_to_hi(),
3005                        format!(
3006                            "use `{suggestion}` to {borrow_kind} the `{ty}`, \
3007                            panicking if {panic_if}"
3008                        ),
3009                        suggestion,
3010                        Applicability::MaybeIncorrect,
3011                    );
3012                } else if tcx.is_diagnostic_item(sym::Mutex, inner_id) {
3013                    err.span_suggestion_verbose(
3014                        expr.span.shrink_to_hi(),
3015                        format!(
3016                            "use `.lock().unwrap()` to borrow the `{ty}`, \
3017                            blocking the current thread until it can be acquired"
3018                        ),
3019                        ".lock().unwrap()",
3020                        Applicability::MaybeIncorrect,
3021                    );
3022                } else if tcx.is_diagnostic_item(sym::RwLock, inner_id) {
3023                    let (suggestion, borrow_kind) = match mutable {
3024                        Some(Mutability::Not) => (".read().unwrap()", "borrow"),
3025                        Some(Mutability::Mut) => (".write().unwrap()", "mutably borrow"),
3026                        None => return,
3027                    };
3028                    err.span_suggestion_verbose(
3029                        expr.span.shrink_to_hi(),
3030                        format!(
3031                            "use `{suggestion}` to {borrow_kind} the `{ty}`, \
3032                            blocking the current thread until it can be acquired"
3033                        ),
3034                        suggestion,
3035                        Applicability::MaybeIncorrect,
3036                    );
3037                } else {
3038                    return;
3039                };
3040
3041                err.span_note(
3042                    tcx.def_span(pick.item.def_id),
3043                    format!("the method `{item_name}` exists on the type `{ty}`"),
3044                );
3045            }
3046        }
3047    }
3048
3049    pub(crate) fn note_unmet_impls_on_type(
3050        &self,
3051        err: &mut Diag<'_>,
3052        errors: Vec<FulfillmentError<'tcx>>,
3053        suggest_derive: bool,
3054    ) {
3055        let preds: Vec<_> = errors
3056            .iter()
3057            .filter_map(|e| match e.obligation.predicate.kind().skip_binder() {
3058                ty::PredicateKind::Clause(ty::ClauseKind::Trait(pred)) => {
3059                    match pred.self_ty().kind() {
3060                        ty::Adt(_, _) => Some(pred),
3061                        _ => None,
3062                    }
3063                }
3064                _ => None,
3065            })
3066            .collect();
3067
3068        // Note for local items and foreign items respectively.
3069        let (mut local_preds, mut foreign_preds): (Vec<_>, Vec<_>) =
3070            preds.iter().partition(|&pred| {
3071                if let ty::Adt(def, _) = pred.self_ty().kind() {
3072                    def.did().is_local()
3073                } else {
3074                    false
3075                }
3076            });
3077
3078        local_preds.sort_by_key(|pred: &&ty::TraitPredicate<'_>| pred.trait_ref.to_string());
3079        let local_def_ids = local_preds
3080            .iter()
3081            .filter_map(|pred| match pred.self_ty().kind() {
3082                ty::Adt(def, _) => Some(def.did()),
3083                _ => None,
3084            })
3085            .collect::<FxIndexSet<_>>();
3086        let mut local_spans: MultiSpan = local_def_ids
3087            .iter()
3088            .filter_map(|def_id| {
3089                let span = self.tcx.def_span(*def_id);
3090                if span.is_dummy() { None } else { Some(span) }
3091            })
3092            .collect::<Vec<_>>()
3093            .into();
3094        for pred in &local_preds {
3095            match pred.self_ty().kind() {
3096                ty::Adt(def, _) => {
3097                    local_spans.push_span_label(
3098                        self.tcx.def_span(def.did()),
3099                        format!("must implement `{}`", pred.trait_ref.print_trait_sugared()),
3100                    );
3101                }
3102                _ => {}
3103            }
3104        }
3105        if local_spans.primary_span().is_some() {
3106            let msg = if let [local_pred] = local_preds.as_slice() {
3107                format!(
3108                    "an implementation of `{}` might be missing for `{}`",
3109                    local_pred.trait_ref.print_trait_sugared(),
3110                    local_pred.self_ty()
3111                )
3112            } else {
3113                format!(
3114                    "the following type{} would have to `impl` {} required trait{} for this \
3115                     operation to be valid",
3116                    pluralize!(local_def_ids.len()),
3117                    if local_def_ids.len() == 1 { "its" } else { "their" },
3118                    pluralize!(local_preds.len()),
3119                )
3120            };
3121            err.span_note(local_spans, msg);
3122        }
3123
3124        foreign_preds.sort_by_key(|pred: &&ty::TraitPredicate<'_>| pred.trait_ref.to_string());
3125        let foreign_def_ids = foreign_preds
3126            .iter()
3127            .filter_map(|pred| match pred.self_ty().kind() {
3128                ty::Adt(def, _) => Some(def.did()),
3129                _ => None,
3130            })
3131            .collect::<FxIndexSet<_>>();
3132        let mut foreign_spans: MultiSpan = foreign_def_ids
3133            .iter()
3134            .filter_map(|def_id| {
3135                let span = self.tcx.def_span(*def_id);
3136                if span.is_dummy() { None } else { Some(span) }
3137            })
3138            .collect::<Vec<_>>()
3139            .into();
3140        for pred in &foreign_preds {
3141            match pred.self_ty().kind() {
3142                ty::Adt(def, _) => {
3143                    foreign_spans.push_span_label(
3144                        self.tcx.def_span(def.did()),
3145                        format!("not implement `{}`", pred.trait_ref.print_trait_sugared()),
3146                    );
3147                }
3148                _ => {}
3149            }
3150        }
3151        if foreign_spans.primary_span().is_some() {
3152            let msg = if let [foreign_pred] = foreign_preds.as_slice() {
3153                format!(
3154                    "the foreign item type `{}` doesn't implement `{}`",
3155                    foreign_pred.self_ty(),
3156                    foreign_pred.trait_ref.print_trait_sugared()
3157                )
3158            } else {
3159                format!(
3160                    "the foreign item type{} {} implement required trait{} for this \
3161                     operation to be valid",
3162                    pluralize!(foreign_def_ids.len()),
3163                    if foreign_def_ids.len() > 1 { "don't" } else { "doesn't" },
3164                    pluralize!(foreign_preds.len()),
3165                )
3166            };
3167            err.span_note(foreign_spans, msg);
3168        }
3169
3170        let preds: Vec<_> = errors
3171            .iter()
3172            .map(|e| (e.obligation.predicate, None, Some(e.obligation.cause.clone())))
3173            .collect();
3174        if suggest_derive {
3175            self.suggest_derive(err, &preds);
3176        } else {
3177            // The predicate comes from a binop where the lhs and rhs have different types.
3178            let _ = self.note_predicate_source_and_get_derives(err, &preds);
3179        }
3180    }
3181
3182    fn note_predicate_source_and_get_derives(
3183        &self,
3184        err: &mut Diag<'_>,
3185        unsatisfied_predicates: &[(
3186            ty::Predicate<'tcx>,
3187            Option<ty::Predicate<'tcx>>,
3188            Option<ObligationCause<'tcx>>,
3189        )],
3190    ) -> Vec<(String, Span, Symbol)> {
3191        let mut derives = Vec::<(String, Span, Symbol)>::new();
3192        let mut traits = Vec::new();
3193        for (pred, _, _) in unsatisfied_predicates {
3194            let Some(ty::PredicateKind::Clause(ty::ClauseKind::Trait(trait_pred))) =
3195                pred.kind().no_bound_vars()
3196            else {
3197                continue;
3198            };
3199            let adt = match trait_pred.self_ty().ty_adt_def() {
3200                Some(adt) if adt.did().is_local() => adt,
3201                _ => continue,
3202            };
3203            if let Some(diagnostic_name) = self.tcx.get_diagnostic_name(trait_pred.def_id()) {
3204                let can_derive = match diagnostic_name {
3205                    sym::Default => !adt.is_enum(),
3206                    sym::Eq
3207                    | sym::PartialEq
3208                    | sym::Ord
3209                    | sym::PartialOrd
3210                    | sym::Clone
3211                    | sym::Copy
3212                    | sym::Hash
3213                    | sym::Debug => true,
3214                    _ => false,
3215                };
3216                if can_derive {
3217                    let self_name = trait_pred.self_ty().to_string();
3218                    let self_span = self.tcx.def_span(adt.did());
3219                    for super_trait in
3220                        supertraits(self.tcx, ty::Binder::dummy(trait_pred.trait_ref))
3221                    {
3222                        if let Some(parent_diagnostic_name) =
3223                            self.tcx.get_diagnostic_name(super_trait.def_id())
3224                        {
3225                            derives.push((self_name.clone(), self_span, parent_diagnostic_name));
3226                        }
3227                    }
3228                    derives.push((self_name, self_span, diagnostic_name));
3229                } else {
3230                    traits.push(trait_pred.def_id());
3231                }
3232            } else {
3233                traits.push(trait_pred.def_id());
3234            }
3235        }
3236        traits.sort_by_key(|id| self.tcx.def_path_str(id));
3237        traits.dedup();
3238
3239        let len = traits.len();
3240        if len > 0 {
3241            let span =
3242                MultiSpan::from_spans(traits.iter().map(|&did| self.tcx.def_span(did)).collect());
3243            let mut names = format!("`{}`", self.tcx.def_path_str(traits[0]));
3244            for (i, &did) in traits.iter().enumerate().skip(1) {
3245                if len > 2 {
3246                    names.push_str(", ");
3247                }
3248                if i == len - 1 {
3249                    names.push_str(" and ");
3250                }
3251                names.push('`');
3252                names.push_str(&self.tcx.def_path_str(did));
3253                names.push('`');
3254            }
3255            err.span_note(
3256                span,
3257                format!("the trait{} {} must be implemented", pluralize!(len), names),
3258            );
3259        }
3260
3261        derives
3262    }
3263
3264    pub(crate) fn suggest_derive(
3265        &self,
3266        err: &mut Diag<'_>,
3267        unsatisfied_predicates: &[(
3268            ty::Predicate<'tcx>,
3269            Option<ty::Predicate<'tcx>>,
3270            Option<ObligationCause<'tcx>>,
3271        )],
3272    ) -> bool {
3273        let mut derives = self.note_predicate_source_and_get_derives(err, unsatisfied_predicates);
3274        derives.sort();
3275        derives.dedup();
3276
3277        let mut derives_grouped = Vec::<(String, Span, String)>::new();
3278        for (self_name, self_span, trait_name) in derives.into_iter() {
3279            if let Some((last_self_name, _, last_trait_names)) = derives_grouped.last_mut() {
3280                if last_self_name == &self_name {
3281                    last_trait_names.push_str(format!(", {trait_name}").as_str());
3282                    continue;
3283                }
3284            }
3285            derives_grouped.push((self_name, self_span, trait_name.to_string()));
3286        }
3287
3288        for (self_name, self_span, traits) in &derives_grouped {
3289            err.span_suggestion_verbose(
3290                self_span.shrink_to_lo(),
3291                format!("consider annotating `{self_name}` with `#[derive({traits})]`"),
3292                format!("#[derive({traits})]\n"),
3293                Applicability::MaybeIncorrect,
3294            );
3295        }
3296        !derives_grouped.is_empty()
3297    }
3298
3299    fn note_derefed_ty_has_method(
3300        &self,
3301        err: &mut Diag<'_>,
3302        self_source: SelfSource<'tcx>,
3303        rcvr_ty: Ty<'tcx>,
3304        item_name: Ident,
3305        expected: Expectation<'tcx>,
3306    ) {
3307        let SelfSource::QPath(ty) = self_source else {
3308            return;
3309        };
3310        for (deref_ty, _) in self.autoderef(DUMMY_SP, rcvr_ty).silence_errors().skip(1) {
3311            if let Ok(pick) = self.probe_for_name(
3312                Mode::Path,
3313                item_name,
3314                expected.only_has_type(self),
3315                IsSuggestion(true),
3316                deref_ty,
3317                ty.hir_id,
3318                ProbeScope::TraitsInScope,
3319            ) {
3320                if deref_ty.is_suggestable(self.tcx, true)
3321                    // If this method receives `&self`, then the provided
3322                    // argument _should_ coerce, so it's valid to suggest
3323                    // just changing the path.
3324                    && pick.item.is_method()
3325                    && let Some(self_ty) =
3326                        self.tcx.fn_sig(pick.item.def_id).instantiate_identity().inputs().skip_binder().get(0)
3327                    && self_ty.is_ref()
3328                {
3329                    let suggested_path = match deref_ty.kind() {
3330                        ty::Bool
3331                        | ty::Char
3332                        | ty::Int(_)
3333                        | ty::Uint(_)
3334                        | ty::Float(_)
3335                        | ty::Adt(_, _)
3336                        | ty::Str
3337                        | ty::Alias(ty::Projection | ty::Inherent, _)
3338                        | ty::Param(_) => format!("{deref_ty}"),
3339                        // we need to test something like  <&[_]>::len or <(&[u32])>::len
3340                        // and Vec::function();
3341                        // <&[_]>::len or <&[u32]>::len doesn't need an extra "<>" between
3342                        // but for Adt type like Vec::function()
3343                        // we would suggest <[_]>::function();
3344                        _ if self
3345                            .tcx
3346                            .sess
3347                            .source_map()
3348                            .span_wrapped_by_angle_or_parentheses(ty.span) =>
3349                        {
3350                            format!("{deref_ty}")
3351                        }
3352                        _ => format!("<{deref_ty}>"),
3353                    };
3354                    err.span_suggestion_verbose(
3355                        ty.span,
3356                        format!("the function `{item_name}` is implemented on `{deref_ty}`"),
3357                        suggested_path,
3358                        Applicability::MaybeIncorrect,
3359                    );
3360                } else {
3361                    err.span_note(
3362                        ty.span,
3363                        format!("the function `{item_name}` is implemented on `{deref_ty}`"),
3364                    );
3365                }
3366                return;
3367            }
3368        }
3369    }
3370
3371    /// Print out the type for use in value namespace.
3372    fn ty_to_value_string(&self, ty: Ty<'tcx>) -> String {
3373        match ty.kind() {
3374            ty::Adt(def, args) => self.tcx.def_path_str_with_args(def.did(), args),
3375            _ => self.ty_to_string(ty),
3376        }
3377    }
3378
3379    fn suggest_await_before_method(
3380        &self,
3381        err: &mut Diag<'_>,
3382        item_name: Ident,
3383        ty: Ty<'tcx>,
3384        call: &hir::Expr<'_>,
3385        span: Span,
3386        return_type: Option<Ty<'tcx>>,
3387    ) {
3388        let output_ty = match self.err_ctxt().get_impl_future_output_ty(ty) {
3389            Some(output_ty) => self.resolve_vars_if_possible(output_ty),
3390            _ => return,
3391        };
3392        let method_exists =
3393            self.method_exists_for_diagnostic(item_name, output_ty, call.hir_id, return_type);
3394        debug!("suggest_await_before_method: is_method_exist={}", method_exists);
3395        if method_exists {
3396            err.span_suggestion_verbose(
3397                span.shrink_to_lo(),
3398                "consider `await`ing on the `Future` and calling the method on its `Output`",
3399                "await.",
3400                Applicability::MaybeIncorrect,
3401            );
3402        }
3403    }
3404
3405    fn suggest_use_candidates<F>(&self, candidates: Vec<DefId>, handle_candidates: F)
3406    where
3407        F: FnOnce(Vec<String>, Vec<String>, Span),
3408    {
3409        let parent_map = self.tcx.visible_parent_map(());
3410
3411        let scope = self.tcx.parent_module_from_def_id(self.body_id);
3412        let (accessible_candidates, inaccessible_candidates): (Vec<_>, Vec<_>) =
3413            candidates.into_iter().partition(|id| {
3414                let vis = self.tcx.visibility(*id);
3415                vis.is_accessible_from(scope, self.tcx)
3416            });
3417
3418        let sugg = |candidates: Vec<_>, visible| {
3419            // Separate out candidates that must be imported with a glob, because they are named `_`
3420            // and cannot be referred with their identifier.
3421            let (candidates, globs): (Vec<_>, Vec<_>) =
3422                candidates.into_iter().partition(|trait_did| {
3423                    if let Some(parent_did) = parent_map.get(trait_did) {
3424                        // If the item is re-exported as `_`, we should suggest a glob-import instead.
3425                        if *parent_did != self.tcx.parent(*trait_did)
3426                            && self
3427                                .tcx
3428                                .module_children(*parent_did)
3429                                .iter()
3430                                .filter(|child| child.res.opt_def_id() == Some(*trait_did))
3431                                .all(|child| child.ident.name == kw::Underscore)
3432                        {
3433                            return false;
3434                        }
3435                    }
3436
3437                    true
3438                });
3439
3440            let prefix = if visible { "use " } else { "" };
3441            let postfix = if visible { ";" } else { "" };
3442            let path_strings = candidates.iter().map(|trait_did| {
3443                format!(
3444                    "{prefix}{}{postfix}\n",
3445                    with_no_visible_paths_if_doc_hidden!(with_crate_prefix!(
3446                        self.tcx.def_path_str(*trait_did)
3447                    )),
3448                )
3449            });
3450
3451            let glob_path_strings = globs.iter().map(|trait_did| {
3452                let parent_did = parent_map.get(trait_did).unwrap();
3453                format!(
3454                    "{prefix}{}::*{postfix} // trait {}\n",
3455                    with_no_visible_paths_if_doc_hidden!(with_crate_prefix!(
3456                        self.tcx.def_path_str(*parent_did)
3457                    )),
3458                    self.tcx.item_name(*trait_did),
3459                )
3460            });
3461            let mut sugg: Vec<_> = path_strings.chain(glob_path_strings).collect();
3462            sugg.sort();
3463            sugg
3464        };
3465
3466        let accessible_sugg = sugg(accessible_candidates, true);
3467        let inaccessible_sugg = sugg(inaccessible_candidates, false);
3468
3469        let (module, _, _) = self.tcx.hir_get_module(scope);
3470        let span = module.spans.inject_use_span;
3471        handle_candidates(accessible_sugg, inaccessible_sugg, span);
3472    }
3473
3474    fn suggest_valid_traits(
3475        &self,
3476        err: &mut Diag<'_>,
3477        item_name: Ident,
3478        valid_out_of_scope_traits: Vec<DefId>,
3479        explain: bool,
3480    ) -> bool {
3481        if !valid_out_of_scope_traits.is_empty() {
3482            let mut candidates = valid_out_of_scope_traits;
3483            candidates.sort_by_key(|id| self.tcx.def_path_str(id));
3484            candidates.dedup();
3485
3486            // `TryFrom` and `FromIterator` have no methods
3487            let edition_fix = candidates
3488                .iter()
3489                .find(|did| self.tcx.is_diagnostic_item(sym::TryInto, **did))
3490                .copied();
3491
3492            if explain {
3493                err.help("items from traits can only be used if the trait is in scope");
3494            }
3495
3496            let msg = format!(
3497                "{this_trait_is} implemented but not in scope",
3498                this_trait_is = if candidates.len() == 1 {
3499                    format!(
3500                        "trait `{}` which provides `{item_name}` is",
3501                        self.tcx.item_name(candidates[0]),
3502                    )
3503                } else {
3504                    format!("the following traits which provide `{item_name}` are")
3505                }
3506            );
3507
3508            self.suggest_use_candidates(candidates, |accessible_sugg, inaccessible_sugg, span| {
3509                let suggest_for_access = |err: &mut Diag<'_>, mut msg: String, suggs: Vec<_>| {
3510                    msg += &format!(
3511                        "; perhaps you want to import {one_of}",
3512                        one_of = if suggs.len() == 1 { "it" } else { "one of them" },
3513                    );
3514                    err.span_suggestions(span, msg, suggs, Applicability::MaybeIncorrect);
3515                };
3516                let suggest_for_privacy = |err: &mut Diag<'_>, suggs: Vec<String>| {
3517                    let msg = format!(
3518                        "{this_trait_is} implemented but not reachable",
3519                        this_trait_is = if let [sugg] = suggs.as_slice() {
3520                            format!("trait `{}` which provides `{item_name}` is", sugg.trim())
3521                        } else {
3522                            format!("the following traits which provide `{item_name}` are")
3523                        }
3524                    );
3525                    if suggs.len() == 1 {
3526                        err.help(msg);
3527                    } else {
3528                        err.span_suggestions(span, msg, suggs, Applicability::MaybeIncorrect);
3529                    }
3530                };
3531                if accessible_sugg.is_empty() {
3532                    // `inaccessible_sugg` must not be empty
3533                    suggest_for_privacy(err, inaccessible_sugg);
3534                } else if inaccessible_sugg.is_empty() {
3535                    suggest_for_access(err, msg, accessible_sugg);
3536                } else {
3537                    suggest_for_access(err, msg, accessible_sugg);
3538                    suggest_for_privacy(err, inaccessible_sugg);
3539                }
3540            });
3541
3542            if let Some(did) = edition_fix {
3543                err.note(format!(
3544                    "'{}' is included in the prelude starting in Edition 2021",
3545                    with_crate_prefix!(self.tcx.def_path_str(did))
3546                ));
3547            }
3548
3549            true
3550        } else {
3551            false
3552        }
3553    }
3554
3555    fn suggest_traits_to_import(
3556        &self,
3557        err: &mut Diag<'_>,
3558        span: Span,
3559        rcvr_ty: Ty<'tcx>,
3560        item_name: Ident,
3561        inputs_len: Option<usize>,
3562        source: SelfSource<'tcx>,
3563        valid_out_of_scope_traits: Vec<DefId>,
3564        static_candidates: &[CandidateSource],
3565        unsatisfied_bounds: bool,
3566        return_type: Option<Ty<'tcx>>,
3567        trait_missing_method: bool,
3568    ) {
3569        let mut alt_rcvr_sugg = false;
3570        let mut trait_in_other_version_found = false;
3571        if let (SelfSource::MethodCall(rcvr), false) = (source, unsatisfied_bounds) {
3572            debug!(
3573                "suggest_traits_to_import: span={:?}, item_name={:?}, rcvr_ty={:?}, rcvr={:?}",
3574                span, item_name, rcvr_ty, rcvr
3575            );
3576            let skippable = [
3577                self.tcx.lang_items().clone_trait(),
3578                self.tcx.lang_items().deref_trait(),
3579                self.tcx.lang_items().deref_mut_trait(),
3580                self.tcx.lang_items().drop_trait(),
3581                self.tcx.get_diagnostic_item(sym::AsRef),
3582            ];
3583            // Try alternative arbitrary self types that could fulfill this call.
3584            // FIXME: probe for all types that *could* be arbitrary self-types, not
3585            // just this list.
3586            for (rcvr_ty, post, pin_call) in &[
3587                (rcvr_ty, "", None),
3588                (
3589                    Ty::new_mut_ref(self.tcx, self.tcx.lifetimes.re_erased, rcvr_ty),
3590                    "&mut ",
3591                    Some("as_mut"),
3592                ),
3593                (
3594                    Ty::new_imm_ref(self.tcx, self.tcx.lifetimes.re_erased, rcvr_ty),
3595                    "&",
3596                    Some("as_ref"),
3597                ),
3598            ] {
3599                match self.lookup_probe_for_diagnostic(
3600                    item_name,
3601                    *rcvr_ty,
3602                    rcvr,
3603                    ProbeScope::AllTraits,
3604                    return_type,
3605                ) {
3606                    Ok(pick) => {
3607                        // If the method is defined for the receiver we have, it likely wasn't `use`d.
3608                        // We point at the method, but we just skip the rest of the check for arbitrary
3609                        // self types and rely on the suggestion to `use` the trait from
3610                        // `suggest_valid_traits`.
3611                        let did = Some(pick.item.container_id(self.tcx));
3612                        if skippable.contains(&did) {
3613                            continue;
3614                        }
3615                        trait_in_other_version_found = self
3616                            .detect_and_explain_multiple_crate_versions_of_trait_item(
3617                                err,
3618                                pick.item.def_id,
3619                                rcvr.hir_id,
3620                                Some(*rcvr_ty),
3621                            );
3622                        if pick.autoderefs == 0 && !trait_in_other_version_found {
3623                            err.span_label(
3624                                pick.item.ident(self.tcx).span,
3625                                format!("the method is available for `{rcvr_ty}` here"),
3626                            );
3627                        }
3628                        break;
3629                    }
3630                    Err(MethodError::Ambiguity(_)) => {
3631                        // If the method is defined (but ambiguous) for the receiver we have, it is also
3632                        // likely we haven't `use`d it. It may be possible that if we `Box`/`Pin`/etc.
3633                        // the receiver, then it might disambiguate this method, but I think these
3634                        // suggestions are generally misleading (see #94218).
3635                        break;
3636                    }
3637                    Err(_) => (),
3638                }
3639
3640                let Some(unpin_trait) = self.tcx.lang_items().unpin_trait() else {
3641                    return;
3642                };
3643                let pred = ty::TraitRef::new(self.tcx, unpin_trait, [*rcvr_ty]);
3644                let unpin = self.predicate_must_hold_considering_regions(&Obligation::new(
3645                    self.tcx,
3646                    self.misc(rcvr.span),
3647                    self.param_env,
3648                    pred,
3649                ));
3650                for (rcvr_ty, pre) in &[
3651                    (Ty::new_lang_item(self.tcx, *rcvr_ty, LangItem::OwnedBox), "Box::new"),
3652                    (Ty::new_lang_item(self.tcx, *rcvr_ty, LangItem::Pin), "Pin::new"),
3653                    (Ty::new_diagnostic_item(self.tcx, *rcvr_ty, sym::Arc), "Arc::new"),
3654                    (Ty::new_diagnostic_item(self.tcx, *rcvr_ty, sym::Rc), "Rc::new"),
3655                ] {
3656                    if let Some(new_rcvr_t) = *rcvr_ty
3657                        && let Ok(pick) = self.lookup_probe_for_diagnostic(
3658                            item_name,
3659                            new_rcvr_t,
3660                            rcvr,
3661                            ProbeScope::AllTraits,
3662                            return_type,
3663                        )
3664                    {
3665                        debug!("try_alt_rcvr: pick candidate {:?}", pick);
3666                        let did = Some(pick.item.container_id(self.tcx));
3667                        // We don't want to suggest a container type when the missing
3668                        // method is `.clone()` or `.deref()` otherwise we'd suggest
3669                        // `Arc::new(foo).clone()`, which is far from what the user wants.
3670                        // Explicitly ignore the `Pin::as_ref()` method as `Pin` does not
3671                        // implement the `AsRef` trait.
3672                        let skip = skippable.contains(&did)
3673                            || (("Pin::new" == *pre)
3674                                && ((sym::as_ref == item_name.name) || !unpin))
3675                            || inputs_len.is_some_and(|inputs_len| {
3676                                pick.item.is_fn()
3677                                    && self
3678                                        .tcx
3679                                        .fn_sig(pick.item.def_id)
3680                                        .skip_binder()
3681                                        .skip_binder()
3682                                        .inputs()
3683                                        .len()
3684                                        != inputs_len
3685                            });
3686                        // Make sure the method is defined for the *actual* receiver: we don't
3687                        // want to treat `Box<Self>` as a receiver if it only works because of
3688                        // an autoderef to `&self`
3689                        if pick.autoderefs == 0 && !skip {
3690                            err.span_label(
3691                                pick.item.ident(self.tcx).span,
3692                                format!("the method is available for `{new_rcvr_t}` here"),
3693                            );
3694                            err.multipart_suggestion(
3695                                "consider wrapping the receiver expression with the \
3696                                 appropriate type",
3697                                vec![
3698                                    (rcvr.span.shrink_to_lo(), format!("{pre}({post}")),
3699                                    (rcvr.span.shrink_to_hi(), ")".to_string()),
3700                                ],
3701                                Applicability::MaybeIncorrect,
3702                            );
3703                            // We don't care about the other suggestions.
3704                            alt_rcvr_sugg = true;
3705                        }
3706                    }
3707                }
3708                // We special case the situation where `Pin::new` wouldn't work, and instead
3709                // suggest using the `pin!()` macro instead.
3710                if let Some(new_rcvr_t) = Ty::new_lang_item(self.tcx, *rcvr_ty, LangItem::Pin)
3711                    // We didn't find an alternative receiver for the method.
3712                    && !alt_rcvr_sugg
3713                    // `T: !Unpin`
3714                    && !unpin
3715                    // The method isn't `as_ref`, as it would provide a wrong suggestion for `Pin`.
3716                    && sym::as_ref != item_name.name
3717                    // Either `Pin::as_ref` or `Pin::as_mut`.
3718                    && let Some(pin_call) = pin_call
3719                    // Search for `item_name` as a method accessible on `Pin<T>`.
3720                    && let Ok(pick) = self.lookup_probe_for_diagnostic(
3721                        item_name,
3722                        new_rcvr_t,
3723                        rcvr,
3724                        ProbeScope::AllTraits,
3725                        return_type,
3726                    )
3727                    // We skip some common traits that we don't want to consider because autoderefs
3728                    // would take care of them.
3729                    && !skippable.contains(&Some(pick.item.container_id(self.tcx)))
3730                    // We don't want to go through derefs.
3731                    && pick.autoderefs == 0
3732                    // Check that the method of the same name that was found on the new `Pin<T>`
3733                    // receiver has the same number of arguments that appear in the user's code.
3734                    && inputs_len.is_some_and(|inputs_len| pick.item.is_fn() && self.tcx.fn_sig(pick.item.def_id).skip_binder().skip_binder().inputs().len() == inputs_len)
3735                {
3736                    let indent = self
3737                        .tcx
3738                        .sess
3739                        .source_map()
3740                        .indentation_before(rcvr.span)
3741                        .unwrap_or_else(|| " ".to_string());
3742                    let mut expr = rcvr;
3743                    while let Node::Expr(call_expr) = self.tcx.parent_hir_node(expr.hir_id)
3744                        && let hir::ExprKind::MethodCall(hir::PathSegment { .. }, ..) =
3745                            call_expr.kind
3746                    {
3747                        expr = call_expr;
3748                    }
3749                    match self.tcx.parent_hir_node(expr.hir_id) {
3750                        Node::LetStmt(stmt)
3751                            if let Some(init) = stmt.init
3752                                && let Ok(code) =
3753                                    self.tcx.sess.source_map().span_to_snippet(rcvr.span) =>
3754                        {
3755                            // We need to take care to account for the existing binding when we
3756                            // suggest the code.
3757                            err.multipart_suggestion(
3758                                "consider pinning the expression",
3759                                vec![
3760                                    (
3761                                        stmt.span.shrink_to_lo(),
3762                                        format!(
3763                                            "let mut pinned = std::pin::pin!({code});\n{indent}"
3764                                        ),
3765                                    ),
3766                                    (
3767                                        init.span.until(rcvr.span.shrink_to_hi()),
3768                                        format!("pinned.{pin_call}()"),
3769                                    ),
3770                                ],
3771                                Applicability::MaybeIncorrect,
3772                            );
3773                        }
3774                        Node::Block(_) | Node::Stmt(_) => {
3775                            // There's no binding, so we can provide a slightly nicer looking
3776                            // suggestion.
3777                            err.multipart_suggestion(
3778                                "consider pinning the expression",
3779                                vec![
3780                                    (
3781                                        rcvr.span.shrink_to_lo(),
3782                                        format!("let mut pinned = std::pin::pin!("),
3783                                    ),
3784                                    (
3785                                        rcvr.span.shrink_to_hi(),
3786                                        format!(");\n{indent}pinned.{pin_call}()"),
3787                                    ),
3788                                ],
3789                                Applicability::MaybeIncorrect,
3790                            );
3791                        }
3792                        _ => {
3793                            // We don't quite know what the users' code looks like, so we don't
3794                            // provide a pinning suggestion.
3795                            err.span_help(
3796                                rcvr.span,
3797                                "consider pinning the expression with `std::pin::pin!()` and \
3798                                 assigning that to a new binding",
3799                            );
3800                        }
3801                    }
3802                    // We don't care about the other suggestions.
3803                    alt_rcvr_sugg = true;
3804                }
3805            }
3806        }
3807
3808        if let SelfSource::QPath(ty) = source
3809            && !valid_out_of_scope_traits.is_empty()
3810            && let hir::TyKind::Path(path) = ty.kind
3811            && let hir::QPath::Resolved(..) = path
3812            && let Some(assoc) = self
3813                .tcx
3814                .associated_items(valid_out_of_scope_traits[0])
3815                .filter_by_name_unhygienic(item_name.name)
3816                .next()
3817        {
3818            // See if the `Type::function(val)` where `function` wasn't found corresponds to a
3819            // `Trait` that is imported directly, but `Type` came from a different version of the
3820            // same crate.
3821
3822            let rcvr_ty = self.node_ty_opt(ty.hir_id);
3823            trait_in_other_version_found = self
3824                .detect_and_explain_multiple_crate_versions_of_trait_item(
3825                    err,
3826                    assoc.def_id,
3827                    ty.hir_id,
3828                    rcvr_ty,
3829                );
3830        }
3831        if !trait_in_other_version_found
3832            && self.suggest_valid_traits(err, item_name, valid_out_of_scope_traits, true)
3833        {
3834            return;
3835        }
3836
3837        let type_is_local = self.type_derefs_to_local(span, rcvr_ty, source);
3838
3839        let mut arbitrary_rcvr = vec![];
3840        // There are no traits implemented, so lets suggest some traits to
3841        // implement, by finding ones that have the item name, and are
3842        // legal to implement.
3843        let mut candidates = all_traits(self.tcx)
3844            .into_iter()
3845            // Don't issue suggestions for unstable traits since they're
3846            // unlikely to be implementable anyway
3847            .filter(|info| match self.tcx.lookup_stability(info.def_id) {
3848                Some(attr) => attr.level.is_stable(),
3849                None => true,
3850            })
3851            .filter(|info| {
3852                // Static candidates are already implemented, and known not to work
3853                // Do not suggest them again
3854                static_candidates.iter().all(|sc| match *sc {
3855                    CandidateSource::Trait(def_id) => def_id != info.def_id,
3856                    CandidateSource::Impl(def_id) => {
3857                        self.tcx.trait_id_of_impl(def_id) != Some(info.def_id)
3858                    }
3859                })
3860            })
3861            .filter(|info| {
3862                // We approximate the coherence rules to only suggest
3863                // traits that are legal to implement by requiring that
3864                // either the type or trait is local. Multi-dispatch means
3865                // this isn't perfect (that is, there are cases when
3866                // implementing a trait would be legal but is rejected
3867                // here).
3868                (type_is_local || info.def_id.is_local())
3869                    && !self.tcx.trait_is_auto(info.def_id)
3870                    && self
3871                        .associated_value(info.def_id, item_name)
3872                        .filter(|item| {
3873                            if item.is_fn() {
3874                                let id = item
3875                                    .def_id
3876                                    .as_local()
3877                                    .map(|def_id| self.tcx.hir_node_by_def_id(def_id));
3878                                if let Some(hir::Node::TraitItem(hir::TraitItem {
3879                                    kind: hir::TraitItemKind::Fn(fn_sig, method),
3880                                    ..
3881                                })) = id
3882                                {
3883                                    let self_first_arg = match method {
3884                                        hir::TraitFn::Required([ident, ..]) => {
3885                                            matches!(ident, Some(Ident { name: kw::SelfLower, .. }))
3886                                        }
3887                                        hir::TraitFn::Provided(body_id) => {
3888                                            self.tcx.hir_body(*body_id).params.first().is_some_and(
3889                                                |param| {
3890                                                    matches!(
3891                                                        param.pat.kind,
3892                                                        hir::PatKind::Binding(_, _, ident, _)
3893                                                            if ident.name == kw::SelfLower
3894                                                    )
3895                                                },
3896                                            )
3897                                        }
3898                                        _ => false,
3899                                    };
3900
3901                                    if !fn_sig.decl.implicit_self.has_implicit_self()
3902                                        && self_first_arg
3903                                    {
3904                                        if let Some(ty) = fn_sig.decl.inputs.get(0) {
3905                                            arbitrary_rcvr.push(ty.span);
3906                                        }
3907                                        return false;
3908                                    }
3909                                }
3910                            }
3911                            // We only want to suggest public or local traits (#45781).
3912                            item.visibility(self.tcx).is_public() || info.def_id.is_local()
3913                        })
3914                        .is_some()
3915            })
3916            .collect::<Vec<_>>();
3917        for span in &arbitrary_rcvr {
3918            err.span_label(
3919                *span,
3920                "the method might not be found because of this arbitrary self type",
3921            );
3922        }
3923        if alt_rcvr_sugg {
3924            return;
3925        }
3926
3927        if !candidates.is_empty() {
3928            // Sort local crate results before others
3929            candidates
3930                .sort_by_key(|&info| (!info.def_id.is_local(), self.tcx.def_path_str(info.def_id)));
3931            candidates.dedup();
3932
3933            let param_type = match *rcvr_ty.kind() {
3934                ty::Param(param) => Some(param),
3935                ty::Ref(_, ty, _) => match *ty.kind() {
3936                    ty::Param(param) => Some(param),
3937                    _ => None,
3938                },
3939                _ => None,
3940            };
3941            if !trait_missing_method {
3942                err.help(if param_type.is_some() {
3943                    "items from traits can only be used if the type parameter is bounded by the trait"
3944                } else {
3945                    "items from traits can only be used if the trait is implemented and in scope"
3946                });
3947            }
3948
3949            let candidates_len = candidates.len();
3950            let message = |action| {
3951                format!(
3952                    "the following {traits_define} an item `{name}`, perhaps you need to {action} \
3953                     {one_of_them}:",
3954                    traits_define =
3955                        if candidates_len == 1 { "trait defines" } else { "traits define" },
3956                    action = action,
3957                    one_of_them = if candidates_len == 1 { "it" } else { "one of them" },
3958                    name = item_name,
3959                )
3960            };
3961            // Obtain the span for `param` and use it for a structured suggestion.
3962            if let Some(param) = param_type {
3963                let generics = self.tcx.generics_of(self.body_id.to_def_id());
3964                let type_param = generics.type_param(param, self.tcx);
3965                let tcx = self.tcx;
3966                if let Some(def_id) = type_param.def_id.as_local() {
3967                    let id = tcx.local_def_id_to_hir_id(def_id);
3968                    // Get the `hir::Param` to verify whether it already has any bounds.
3969                    // We do this to avoid suggesting code that ends up as `T: FooBar`,
3970                    // instead we suggest `T: Foo + Bar` in that case.
3971                    match tcx.hir_node(id) {
3972                        Node::GenericParam(param) => {
3973                            enum Introducer {
3974                                Plus,
3975                                Colon,
3976                                Nothing,
3977                            }
3978                            let hir_generics = tcx.hir_get_generics(id.owner.def_id).unwrap();
3979                            let trait_def_ids: DefIdSet = hir_generics
3980                                .bounds_for_param(def_id)
3981                                .flat_map(|bp| bp.bounds.iter())
3982                                .filter_map(|bound| bound.trait_ref()?.trait_def_id())
3983                                .collect();
3984                            if candidates.iter().any(|t| trait_def_ids.contains(&t.def_id)) {
3985                                return;
3986                            }
3987                            let msg = message(format!(
3988                                "restrict type parameter `{}` with",
3989                                param.name.ident(),
3990                            ));
3991                            let bounds_span = hir_generics.bounds_span_for_suggestions(def_id);
3992                            let mut applicability = Applicability::MaybeIncorrect;
3993                            // Format the path of each suggested candidate, providing placeholders
3994                            // for any generic arguments without defaults.
3995                            let candidate_strs: Vec<_> = candidates
3996                                .iter()
3997                                .map(|cand| {
3998                                    let cand_path = tcx.def_path_str(cand.def_id);
3999                                    let cand_params = &tcx.generics_of(cand.def_id).own_params;
4000                                    let cand_args: String = cand_params
4001                                        .iter()
4002                                        .skip(1)
4003                                        .filter_map(|param| match param.kind {
4004                                            ty::GenericParamDefKind::Type {
4005                                                has_default: true,
4006                                                ..
4007                                            }
4008                                            | ty::GenericParamDefKind::Const {
4009                                                has_default: true,
4010                                                ..
4011                                            } => None,
4012                                            _ => Some(param.name.as_str()),
4013                                        })
4014                                        .intersperse(", ")
4015                                        .collect();
4016                                    if cand_args.is_empty() {
4017                                        cand_path
4018                                    } else {
4019                                        applicability = Applicability::HasPlaceholders;
4020                                        format!("{cand_path}</* {cand_args} */>")
4021                                    }
4022                                })
4023                                .collect();
4024
4025                            if rcvr_ty.is_ref()
4026                                && param.is_impl_trait()
4027                                && let Some((bounds_span, _)) = bounds_span
4028                            {
4029                                err.multipart_suggestions(
4030                                    msg,
4031                                    candidate_strs.iter().map(|cand| {
4032                                        vec![
4033                                            (param.span.shrink_to_lo(), "(".to_string()),
4034                                            (bounds_span, format!(" + {cand})")),
4035                                        ]
4036                                    }),
4037                                    applicability,
4038                                );
4039                                return;
4040                            }
4041
4042                            let (sp, introducer, open_paren_sp) =
4043                                if let Some((span, open_paren_sp)) = bounds_span {
4044                                    (span, Introducer::Plus, open_paren_sp)
4045                                } else if let Some(colon_span) = param.colon_span {
4046                                    (colon_span.shrink_to_hi(), Introducer::Nothing, None)
4047                                } else if param.is_impl_trait() {
4048                                    (param.span.shrink_to_hi(), Introducer::Plus, None)
4049                                } else {
4050                                    (param.span.shrink_to_hi(), Introducer::Colon, None)
4051                                };
4052
4053                            let all_suggs = candidate_strs.iter().map(|cand| {
4054                                let suggestion = format!(
4055                                    "{} {cand}",
4056                                    match introducer {
4057                                        Introducer::Plus => " +",
4058                                        Introducer::Colon => ":",
4059                                        Introducer::Nothing => "",
4060                                    },
4061                                );
4062
4063                                let mut suggs = vec![];
4064
4065                                if let Some(open_paren_sp) = open_paren_sp {
4066                                    suggs.push((open_paren_sp, "(".to_string()));
4067                                    suggs.push((sp, format!("){suggestion}")));
4068                                } else {
4069                                    suggs.push((sp, suggestion));
4070                                }
4071
4072                                suggs
4073                            });
4074
4075                            err.multipart_suggestions(msg, all_suggs, applicability);
4076
4077                            return;
4078                        }
4079                        Node::Item(hir::Item {
4080                            kind: hir::ItemKind::Trait(_, _, ident, _, bounds, _),
4081                            ..
4082                        }) => {
4083                            let (sp, sep, article) = if bounds.is_empty() {
4084                                (ident.span.shrink_to_hi(), ":", "a")
4085                            } else {
4086                                (bounds.last().unwrap().span().shrink_to_hi(), " +", "another")
4087                            };
4088                            err.span_suggestions(
4089                                sp,
4090                                message(format!("add {article} supertrait for")),
4091                                candidates
4092                                    .iter()
4093                                    .map(|t| format!("{} {}", sep, tcx.def_path_str(t.def_id),)),
4094                                Applicability::MaybeIncorrect,
4095                            );
4096                            return;
4097                        }
4098                        _ => {}
4099                    }
4100                }
4101            }
4102
4103            let (potential_candidates, explicitly_negative) = if param_type.is_some() {
4104                // FIXME: Even though negative bounds are not implemented, we could maybe handle
4105                // cases where a positive bound implies a negative impl.
4106                (candidates, Vec::new())
4107            } else if let Some(simp_rcvr_ty) =
4108                simplify_type(self.tcx, rcvr_ty, TreatParams::AsRigid)
4109            {
4110                let mut potential_candidates = Vec::new();
4111                let mut explicitly_negative = Vec::new();
4112                for candidate in candidates {
4113                    // Check if there's a negative impl of `candidate` for `rcvr_ty`
4114                    if self
4115                        .tcx
4116                        .all_impls(candidate.def_id)
4117                        .map(|imp_did| {
4118                            self.tcx.impl_trait_header(imp_did).expect(
4119                                "inherent impls can't be candidates, only trait impls can be",
4120                            )
4121                        })
4122                        .filter(|header| header.polarity != ty::ImplPolarity::Positive)
4123                        .any(|header| {
4124                            let imp = header.trait_ref.instantiate_identity();
4125                            let imp_simp =
4126                                simplify_type(self.tcx, imp.self_ty(), TreatParams::AsRigid);
4127                            imp_simp.is_some_and(|s| s == simp_rcvr_ty)
4128                        })
4129                    {
4130                        explicitly_negative.push(candidate);
4131                    } else {
4132                        potential_candidates.push(candidate);
4133                    }
4134                }
4135                (potential_candidates, explicitly_negative)
4136            } else {
4137                // We don't know enough about `recv_ty` to make proper suggestions.
4138                (candidates, Vec::new())
4139            };
4140
4141            let impls_trait = |def_id: DefId| {
4142                let args = ty::GenericArgs::for_item(self.tcx, def_id, |param, _| {
4143                    if param.index == 0 {
4144                        rcvr_ty.into()
4145                    } else {
4146                        self.infcx.var_for_def(span, param)
4147                    }
4148                });
4149                self.infcx
4150                    .type_implements_trait(def_id, args, self.param_env)
4151                    .must_apply_modulo_regions()
4152                    && param_type.is_none()
4153            };
4154            match &potential_candidates[..] {
4155                [] => {}
4156                [trait_info] if trait_info.def_id.is_local() => {
4157                    if impls_trait(trait_info.def_id) {
4158                        self.suggest_valid_traits(err, item_name, vec![trait_info.def_id], false);
4159                    } else {
4160                        err.subdiagnostic(CandidateTraitNote {
4161                            span: self.tcx.def_span(trait_info.def_id),
4162                            trait_name: self.tcx.def_path_str(trait_info.def_id),
4163                            item_name,
4164                            action_or_ty: if trait_missing_method {
4165                                "NONE".to_string()
4166                            } else {
4167                                param_type.map_or_else(
4168                                    || "implement".to_string(), // FIXME: it might only need to be imported into scope, not implemented.
4169                                    |p| p.to_string(),
4170                                )
4171                            },
4172                        });
4173                    }
4174                }
4175                trait_infos => {
4176                    let mut msg = message(param_type.map_or_else(
4177                        || "implement".to_string(), // FIXME: it might only need to be imported into scope, not implemented.
4178                        |param| format!("restrict type parameter `{param}` with"),
4179                    ));
4180                    for (i, trait_info) in trait_infos.iter().enumerate() {
4181                        if impls_trait(trait_info.def_id) {
4182                            self.suggest_valid_traits(
4183                                err,
4184                                item_name,
4185                                vec![trait_info.def_id],
4186                                false,
4187                            );
4188                        }
4189                        msg.push_str(&format!(
4190                            "\ncandidate #{}: `{}`",
4191                            i + 1,
4192                            self.tcx.def_path_str(trait_info.def_id),
4193                        ));
4194                    }
4195                    err.note(msg);
4196                }
4197            }
4198            match &explicitly_negative[..] {
4199                [] => {}
4200                [trait_info] => {
4201                    let msg = format!(
4202                        "the trait `{}` defines an item `{}`, but is explicitly unimplemented",
4203                        self.tcx.def_path_str(trait_info.def_id),
4204                        item_name
4205                    );
4206                    err.note(msg);
4207                }
4208                trait_infos => {
4209                    let mut msg = format!(
4210                        "the following traits define an item `{item_name}`, but are explicitly unimplemented:"
4211                    );
4212                    for trait_info in trait_infos {
4213                        msg.push_str(&format!("\n{}", self.tcx.def_path_str(trait_info.def_id)));
4214                    }
4215                    err.note(msg);
4216                }
4217            }
4218        }
4219    }
4220
4221    fn detect_and_explain_multiple_crate_versions_of_trait_item(
4222        &self,
4223        err: &mut Diag<'_>,
4224        item_def_id: DefId,
4225        hir_id: hir::HirId,
4226        rcvr_ty: Option<Ty<'_>>,
4227    ) -> bool {
4228        let hir_id = self.tcx.parent_hir_id(hir_id);
4229        let Some(traits) = self.tcx.in_scope_traits(hir_id) else { return false };
4230        if traits.is_empty() {
4231            return false;
4232        }
4233        let trait_def_id = self.tcx.parent(item_def_id);
4234        if !self.tcx.is_trait(trait_def_id) {
4235            return false;
4236        }
4237        let krate = self.tcx.crate_name(trait_def_id.krate);
4238        let name = self.tcx.item_name(trait_def_id);
4239        let candidates: Vec<_> = traits
4240            .iter()
4241            .filter(|c| {
4242                c.def_id.krate != trait_def_id.krate
4243                    && self.tcx.crate_name(c.def_id.krate) == krate
4244                    && self.tcx.item_name(c.def_id) == name
4245            })
4246            .map(|c| (c.def_id, c.import_ids.get(0).cloned()))
4247            .collect();
4248        if candidates.is_empty() {
4249            return false;
4250        }
4251        let item_span = self.tcx.def_span(item_def_id);
4252        let msg = format!(
4253            "there are multiple different versions of crate `{krate}` in the dependency graph",
4254        );
4255        let trait_span = self.tcx.def_span(trait_def_id);
4256        let mut multi_span: MultiSpan = trait_span.into();
4257        multi_span.push_span_label(trait_span, format!("this is the trait that is needed"));
4258        let descr = self.tcx.associated_item(item_def_id).descr();
4259        let rcvr_ty =
4260            rcvr_ty.map(|t| format!("`{t}`")).unwrap_or_else(|| "the receiver".to_string());
4261        multi_span
4262            .push_span_label(item_span, format!("the {descr} is available for {rcvr_ty} here"));
4263        for (def_id, import_def_id) in candidates {
4264            if let Some(import_def_id) = import_def_id {
4265                multi_span.push_span_label(
4266                    self.tcx.def_span(import_def_id),
4267                    format!(
4268                        "`{name}` imported here doesn't correspond to the right version of crate \
4269                         `{krate}`",
4270                    ),
4271                );
4272            }
4273            multi_span.push_span_label(
4274                self.tcx.def_span(def_id),
4275                format!("this is the trait that was imported"),
4276            );
4277        }
4278        err.span_note(multi_span, msg);
4279        true
4280    }
4281
4282    /// issue #102320, for `unwrap_or` with closure as argument, suggest `unwrap_or_else`
4283    /// FIXME: currently not working for suggesting `map_or_else`, see #102408
4284    pub(crate) fn suggest_else_fn_with_closure(
4285        &self,
4286        err: &mut Diag<'_>,
4287        expr: &hir::Expr<'_>,
4288        found: Ty<'tcx>,
4289        expected: Ty<'tcx>,
4290    ) -> bool {
4291        let Some((_def_id_or_name, output, _inputs)) = self.extract_callable_info(found) else {
4292            return false;
4293        };
4294
4295        if !self.may_coerce(output, expected) {
4296            return false;
4297        }
4298
4299        if let Node::Expr(call_expr) = self.tcx.parent_hir_node(expr.hir_id)
4300            && let hir::ExprKind::MethodCall(
4301                hir::PathSegment { ident: method_name, .. },
4302                self_expr,
4303                args,
4304                ..,
4305            ) = call_expr.kind
4306            && let Some(self_ty) = self.typeck_results.borrow().expr_ty_opt(self_expr)
4307        {
4308            let new_name = Ident {
4309                name: Symbol::intern(&format!("{}_else", method_name.as_str())),
4310                span: method_name.span,
4311            };
4312            let probe = self.lookup_probe_for_diagnostic(
4313                new_name,
4314                self_ty,
4315                self_expr,
4316                ProbeScope::TraitsInScope,
4317                Some(expected),
4318            );
4319
4320            // check the method arguments number
4321            if let Ok(pick) = probe
4322                && let fn_sig = self.tcx.fn_sig(pick.item.def_id)
4323                && let fn_args = fn_sig.skip_binder().skip_binder().inputs()
4324                && fn_args.len() == args.len() + 1
4325            {
4326                err.span_suggestion_verbose(
4327                    method_name.span.shrink_to_hi(),
4328                    format!("try calling `{}` instead", new_name.name.as_str()),
4329                    "_else",
4330                    Applicability::MaybeIncorrect,
4331                );
4332                return true;
4333            }
4334        }
4335        false
4336    }
4337
4338    /// Checks whether there is a local type somewhere in the chain of
4339    /// autoderefs of `rcvr_ty`.
4340    fn type_derefs_to_local(
4341        &self,
4342        span: Span,
4343        rcvr_ty: Ty<'tcx>,
4344        source: SelfSource<'tcx>,
4345    ) -> bool {
4346        fn is_local(ty: Ty<'_>) -> bool {
4347            match ty.kind() {
4348                ty::Adt(def, _) => def.did().is_local(),
4349                ty::Foreign(did) => did.is_local(),
4350                ty::Dynamic(tr, ..) => tr.principal().is_some_and(|d| d.def_id().is_local()),
4351                ty::Param(_) => true,
4352
4353                // Everything else (primitive types, etc.) is effectively
4354                // non-local (there are "edge" cases, e.g., `(LocalType,)`, but
4355                // the noise from these sort of types is usually just really
4356                // annoying, rather than any sort of help).
4357                _ => false,
4358            }
4359        }
4360
4361        // This occurs for UFCS desugaring of `T::method`, where there is no
4362        // receiver expression for the method call, and thus no autoderef.
4363        if let SelfSource::QPath(_) = source {
4364            return is_local(rcvr_ty);
4365        }
4366
4367        self.autoderef(span, rcvr_ty).silence_errors().any(|(ty, _)| is_local(ty))
4368    }
4369}
4370
4371#[derive(Copy, Clone, Debug)]
4372enum SelfSource<'a> {
4373    QPath(&'a hir::Ty<'a>),
4374    MethodCall(&'a hir::Expr<'a> /* rcvr */),
4375}
4376
4377#[derive(Copy, Clone, PartialEq, Eq)]
4378pub(crate) struct TraitInfo {
4379    pub def_id: DefId,
4380}
4381
4382/// Retrieves all traits in this crate and any dependent crates,
4383/// and wraps them into `TraitInfo` for custom sorting.
4384pub(crate) fn all_traits(tcx: TyCtxt<'_>) -> Vec<TraitInfo> {
4385    tcx.all_traits().map(|def_id| TraitInfo { def_id }).collect()
4386}
4387
4388fn print_disambiguation_help<'tcx>(
4389    tcx: TyCtxt<'tcx>,
4390    err: &mut Diag<'_>,
4391    source: SelfSource<'tcx>,
4392    args: Option<&'tcx [hir::Expr<'tcx>]>,
4393    trait_ref: ty::TraitRef<'tcx>,
4394    candidate_idx: Option<usize>,
4395    span: Span,
4396    item: ty::AssocItem,
4397) -> Option<String> {
4398    let trait_impl_type = trait_ref.self_ty().peel_refs();
4399    let trait_ref = if item.is_method() {
4400        trait_ref.print_only_trait_name().to_string()
4401    } else {
4402        format!("<{} as {}>", trait_ref.args[0], trait_ref.print_only_trait_name())
4403    };
4404    Some(
4405        if item.is_fn()
4406            && let SelfSource::MethodCall(receiver) = source
4407            && let Some(args) = args
4408        {
4409            let def_kind_descr = tcx.def_kind_descr(item.as_def_kind(), item.def_id);
4410            let item_name = item.ident(tcx);
4411            let first_input =
4412                tcx.fn_sig(item.def_id).instantiate_identity().skip_binder().inputs().get(0);
4413            let (first_arg_type, rcvr_ref) = (
4414                first_input.map(|first| first.peel_refs()),
4415                first_input
4416                    .and_then(|ty| ty.ref_mutability())
4417                    .map_or("", |mutbl| mutbl.ref_prefix_str()),
4418            );
4419
4420            // If the type of first arg of this assoc function is `Self` or current trait impl type or `arbitrary_self_types`, we need to take the receiver as args. Otherwise, we don't.
4421            let args = if let Some(first_arg_type) = first_arg_type
4422                && (first_arg_type == tcx.types.self_param
4423                    || first_arg_type == trait_impl_type
4424                    || item.is_method())
4425            {
4426                Some(receiver)
4427            } else {
4428                None
4429            }
4430            .into_iter()
4431            .chain(args)
4432            .map(|arg| {
4433                tcx.sess.source_map().span_to_snippet(arg.span).unwrap_or_else(|_| "_".to_owned())
4434            })
4435            .collect::<Vec<_>>()
4436            .join(", ");
4437
4438            let args = format!("({}{})", rcvr_ref, args);
4439            err.span_suggestion_verbose(
4440                span,
4441                format!(
4442                    "disambiguate the {def_kind_descr} for {}",
4443                    if let Some(candidate) = candidate_idx {
4444                        format!("candidate #{candidate}")
4445                    } else {
4446                        "the candidate".to_string()
4447                    },
4448                ),
4449                format!("{trait_ref}::{item_name}{args}"),
4450                Applicability::HasPlaceholders,
4451            );
4452            return None;
4453        } else {
4454            format!("{trait_ref}::")
4455        },
4456    )
4457}