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