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        self.suggest_bounds_for_range_to_method(&mut err, source, item_ident);
1083        err.emit()
1084    }
1085
1086    fn set_not_found_span_label(
1087        &self,
1088        err: &mut Diag<'_>,
1089        rcvr_ty: Ty<'tcx>,
1090        item_ident: Ident,
1091        item_kind: &str,
1092        mode: Mode,
1093        source: SelfSource<'tcx>,
1094        span: Span,
1095        unsatisfied_predicates: &UnsatisfiedPredicates<'tcx>,
1096        find_candidate_for_method: &mut bool,
1097    ) {
1098        let ty_str = self.tcx.short_string(rcvr_ty, err.long_ty_path());
1099        if unsatisfied_predicates.is_empty() {
1100            err.span_label(span, format!("{item_kind} not found in `{ty_str}`"));
1101            let is_string_or_ref_str = match rcvr_ty.kind() {
1102                ty::Ref(_, ty, _) => {
1103                    ty.is_str()
1104                        || matches!(
1105                            ty.kind(),
1106                            ty::Adt(adt, _) if self.tcx.is_lang_item(adt.did(), LangItem::String)
1107                        )
1108                }
1109                ty::Adt(adt, _) => self.tcx.is_lang_item(adt.did(), LangItem::String),
1110                _ => false,
1111            };
1112            if is_string_or_ref_str && item_ident.name == sym::iter {
1113                err.span_suggestion_verbose(
1114                    item_ident.span,
1115                    "because of the in-memory representation of `&str`, to obtain \
1116                     an `Iterator` over each of its codepoint use method `chars`",
1117                    "chars",
1118                    Applicability::MachineApplicable,
1119                );
1120            }
1121            if let ty::Adt(adt, _) = rcvr_ty.kind() {
1122                let mut inherent_impls_candidate = self
1123                    .tcx
1124                    .inherent_impls(adt.did())
1125                    .into_iter()
1126                    .copied()
1127                    .filter(|def_id| {
1128                        if let Some(assoc) = self.associated_value(*def_id, item_ident) {
1129                            // Check for both mode is the same so we avoid suggesting
1130                            // incorrect associated item.
1131                            match (mode, assoc.is_method(), source) {
1132                                (Mode::MethodCall, true, SelfSource::MethodCall(_)) => {
1133                                    // We check that the suggest type is actually
1134                                    // different from the received one
1135                                    // So we avoid suggestion method with Box<Self>
1136                                    // for instance
1137                                    self.tcx.at(span).type_of(*def_id).instantiate_identity()
1138                                        != rcvr_ty
1139                                }
1140                                (Mode::Path, false, _) => true,
1141                                _ => false,
1142                            }
1143                        } else {
1144                            false
1145                        }
1146                    })
1147                    .collect::<Vec<_>>();
1148                if !inherent_impls_candidate.is_empty() {
1149                    inherent_impls_candidate.sort_by_key(|id| self.tcx.def_path_str(id));
1150                    inherent_impls_candidate.dedup();
1151
1152                    // number of types to show at most
1153                    let limit = if inherent_impls_candidate.len() == 5 { 5 } else { 4 };
1154                    let type_candidates = inherent_impls_candidate
1155                        .iter()
1156                        .take(limit)
1157                        .map(|impl_item| {
1158                            format!(
1159                                "- `{}`",
1160                                self.tcx.at(span).type_of(*impl_item).instantiate_identity()
1161                            )
1162                        })
1163                        .collect::<Vec<_>>()
1164                        .join("\n");
1165                    let additional_types = if inherent_impls_candidate.len() > limit {
1166                        format!("\nand {} more types", inherent_impls_candidate.len() - limit)
1167                    } else {
1168                        "".to_string()
1169                    };
1170                    err.note(format!(
1171                        "the {item_kind} was found for\n{type_candidates}{additional_types}"
1172                    ));
1173                    *find_candidate_for_method = mode == Mode::MethodCall;
1174                }
1175            }
1176        } else {
1177            let ty_str = if ty_str.len() > 50 { String::new() } else { format!("on `{ty_str}` ") };
1178            err.span_label(
1179                span,
1180                format!("{item_kind} cannot be called {ty_str}due to unsatisfied trait bounds"),
1181            );
1182        }
1183    }
1184
1185    /// Suggest similar enum variant when method call fails
1186    fn suggest_enum_variant_for_method_call(
1187        &self,
1188        err: &mut Diag<'_>,
1189        rcvr_ty: Ty<'tcx>,
1190        item_ident: Ident,
1191        span: Span,
1192        source: SelfSource<'tcx>,
1193        unsatisfied_predicates: &UnsatisfiedPredicates<'tcx>,
1194    ) {
1195        // Don't emit a suggestion if we found an actual method that had unsatisfied trait bounds
1196        if !unsatisfied_predicates.is_empty() || !rcvr_ty.is_enum() {
1197            return;
1198        }
1199
1200        let tcx = self.tcx;
1201        let adt_def = rcvr_ty.ty_adt_def().expect("enum is not an ADT");
1202        if let Some(var_name) = edit_distance::find_best_match_for_name(
1203            &adt_def.variants().iter().map(|s| s.name).collect::<Vec<_>>(),
1204            item_ident.name,
1205            None,
1206        ) && let Some(variant) = adt_def.variants().iter().find(|s| s.name == var_name)
1207        {
1208            let mut suggestion = vec![(span, var_name.to_string())];
1209            if let SelfSource::QPath(ty) = source
1210                && let hir::Node::Expr(ref path_expr) = tcx.parent_hir_node(ty.hir_id)
1211                && let hir::ExprKind::Path(_) = path_expr.kind
1212                && let hir::Node::Stmt(&hir::Stmt { kind: hir::StmtKind::Semi(parent), .. })
1213                | hir::Node::Expr(parent) = tcx.parent_hir_node(path_expr.hir_id)
1214            {
1215                let replacement_span =
1216                    if let hir::ExprKind::Call(..) | hir::ExprKind::Struct(..) = parent.kind {
1217                        // We want to replace the parts that need to go, like `()` and `{}`.
1218                        span.with_hi(parent.span.hi())
1219                    } else {
1220                        span
1221                    };
1222                match (variant.ctor, parent.kind) {
1223                    (None, hir::ExprKind::Struct(..)) => {
1224                        // We want a struct and we have a struct. We won't suggest changing
1225                        // the fields (at least for now).
1226                        suggestion = vec![(span, var_name.to_string())];
1227                    }
1228                    (None, _) => {
1229                        // struct
1230                        suggestion = vec![(
1231                            replacement_span,
1232                            if variant.fields.is_empty() {
1233                                format!("{var_name} {{}}")
1234                            } else {
1235                                format!(
1236                                    "{var_name} {{ {} }}",
1237                                    variant
1238                                        .fields
1239                                        .iter()
1240                                        .map(|f| format!("{}: /* value */", f.name))
1241                                        .collect::<Vec<_>>()
1242                                        .join(", ")
1243                                )
1244                            },
1245                        )];
1246                    }
1247                    (Some((hir::def::CtorKind::Const, _)), _) => {
1248                        // unit, remove the `()`.
1249                        suggestion = vec![(replacement_span, var_name.to_string())];
1250                    }
1251                    (Some((hir::def::CtorKind::Fn, def_id)), hir::ExprKind::Call(rcvr, args)) => {
1252                        let fn_sig = tcx.fn_sig(def_id).instantiate_identity();
1253                        let inputs = fn_sig.inputs().skip_binder();
1254                        // FIXME: reuse the logic for "change args" suggestion to account for types
1255                        // involved and detect things like substitution.
1256                        match (inputs, args) {
1257                            (inputs, []) => {
1258                                // Add arguments.
1259                                suggestion.push((
1260                                    rcvr.span.shrink_to_hi().with_hi(parent.span.hi()),
1261                                    format!(
1262                                        "({})",
1263                                        inputs
1264                                            .iter()
1265                                            .map(|i| format!("/* {i} */"))
1266                                            .collect::<Vec<String>>()
1267                                            .join(", ")
1268                                    ),
1269                                ));
1270                            }
1271                            (_, [arg]) if inputs.len() != args.len() => {
1272                                // Replace arguments.
1273                                suggestion.push((
1274                                    arg.span,
1275                                    inputs
1276                                        .iter()
1277                                        .map(|i| format!("/* {i} */"))
1278                                        .collect::<Vec<String>>()
1279                                        .join(", "),
1280                                ));
1281                            }
1282                            (_, [arg_start, .., arg_end]) if inputs.len() != args.len() => {
1283                                // Replace arguments.
1284                                suggestion.push((
1285                                    arg_start.span.to(arg_end.span),
1286                                    inputs
1287                                        .iter()
1288                                        .map(|i| format!("/* {i} */"))
1289                                        .collect::<Vec<String>>()
1290                                        .join(", "),
1291                                ));
1292                            }
1293                            // Argument count is the same, keep as is.
1294                            _ => {}
1295                        }
1296                    }
1297                    (Some((hir::def::CtorKind::Fn, def_id)), _) => {
1298                        let fn_sig = tcx.fn_sig(def_id).instantiate_identity();
1299                        let inputs = fn_sig.inputs().skip_binder();
1300                        suggestion = vec![(
1301                            replacement_span,
1302                            format!(
1303                                "{var_name}({})",
1304                                inputs
1305                                    .iter()
1306                                    .map(|i| format!("/* {i} */"))
1307                                    .collect::<Vec<String>>()
1308                                    .join(", ")
1309                            ),
1310                        )];
1311                    }
1312                }
1313            }
1314            err.multipart_suggestion_verbose(
1315                "there is a variant with a similar name",
1316                suggestion,
1317                Applicability::HasPlaceholders,
1318            );
1319        }
1320    }
1321
1322    fn handle_unsatisfied_predicates(
1323        &self,
1324        err: &mut Diag<'_>,
1325        rcvr_ty: Ty<'tcx>,
1326        item_ident: Ident,
1327        item_kind: &str,
1328        span: Span,
1329        unsatisfied_predicates: &UnsatisfiedPredicates<'tcx>,
1330        restrict_type_params: &mut bool,
1331        suggested_derive: &mut bool,
1332        unsatisfied_bounds: &mut bool,
1333        custom_span_label: &mut bool,
1334        bound_spans: &mut SortedMap<Span, Vec<String>>,
1335    ) {
1336        let tcx = self.tcx;
1337        let mut type_params = FxIndexMap::default();
1338
1339        // Pick out the list of unimplemented traits on the receiver.
1340        // This is used for custom error messages with the `#[rustc_on_unimplemented]` attribute.
1341        let mut unimplemented_traits = FxIndexMap::default();
1342
1343        let mut unimplemented_traits_only = true;
1344        for (predicate, _parent_pred, cause) in unsatisfied_predicates {
1345            if let (ty::PredicateKind::Clause(ty::ClauseKind::Trait(p)), Some(cause)) =
1346                (predicate.kind().skip_binder(), cause.as_ref())
1347            {
1348                if p.trait_ref.self_ty() != rcvr_ty {
1349                    // This is necessary, not just to keep the errors clean, but also
1350                    // because our derived obligations can wind up with a trait ref that
1351                    // requires a different param_env to be correctly compared.
1352                    continue;
1353                }
1354                unimplemented_traits.entry(p.trait_ref.def_id).or_insert((
1355                    predicate.kind().rebind(p),
1356                    Obligation {
1357                        cause: cause.clone(),
1358                        param_env: self.param_env,
1359                        predicate: *predicate,
1360                        recursion_depth: 0,
1361                    },
1362                ));
1363            }
1364        }
1365
1366        // Make sure that, if any traits other than the found ones were involved,
1367        // we don't report an unimplemented trait.
1368        // We don't want to say that `iter::Cloned` is not an iterator, just
1369        // because of some non-Clone item being iterated over.
1370        for (predicate, _parent_pred, _cause) in unsatisfied_predicates {
1371            match predicate.kind().skip_binder() {
1372                ty::PredicateKind::Clause(ty::ClauseKind::Trait(p))
1373                    if unimplemented_traits.contains_key(&p.trait_ref.def_id) => {}
1374                _ => {
1375                    unimplemented_traits_only = false;
1376                    break;
1377                }
1378            }
1379        }
1380
1381        let mut collect_type_param_suggestions =
1382            |self_ty: Ty<'tcx>, parent_pred: ty::Predicate<'tcx>, obligation: &str| {
1383                // We don't care about regions here, so it's fine to skip the binder here.
1384                if let (ty::Param(_), ty::PredicateKind::Clause(ty::ClauseKind::Trait(p))) =
1385                    (self_ty.kind(), parent_pred.kind().skip_binder())
1386                {
1387                    let node = match p.trait_ref.self_ty().kind() {
1388                        ty::Param(_) => {
1389                            // Account for `fn` items like in `issue-35677.rs` to
1390                            // suggest restricting its type params.
1391                            Some(self.tcx.hir_node_by_def_id(self.body_id))
1392                        }
1393                        ty::Adt(def, _) => {
1394                            def.did().as_local().map(|def_id| self.tcx.hir_node_by_def_id(def_id))
1395                        }
1396                        _ => None,
1397                    };
1398                    if let Some(hir::Node::Item(hir::Item { kind, .. })) = node
1399                        && let Some(g) = kind.generics()
1400                    {
1401                        let key = (
1402                            g.tail_span_for_predicate_suggestion(),
1403                            g.add_where_or_trailing_comma(),
1404                        );
1405                        type_params
1406                            .entry(key)
1407                            .or_insert_with(UnordSet::default)
1408                            .insert(obligation.to_owned());
1409                        return true;
1410                    }
1411                }
1412                false
1413            };
1414        let mut bound_span_label = |self_ty: Ty<'_>, obligation: &str, quiet: &str| {
1415            let msg = format!("`{}`", if obligation.len() > 50 { quiet } else { obligation });
1416            match self_ty.kind() {
1417                // Point at the type that couldn't satisfy the bound.
1418                ty::Adt(def, _) => {
1419                    bound_spans.get_mut_or_insert_default(tcx.def_span(def.did())).push(msg)
1420                }
1421                // Point at the trait object that couldn't satisfy the bound.
1422                ty::Dynamic(preds, _) => {
1423                    for pred in preds.iter() {
1424                        match pred.skip_binder() {
1425                            ty::ExistentialPredicate::Trait(tr) => {
1426                                bound_spans
1427                                    .get_mut_or_insert_default(tcx.def_span(tr.def_id))
1428                                    .push(msg.clone());
1429                            }
1430                            ty::ExistentialPredicate::Projection(_)
1431                            | ty::ExistentialPredicate::AutoTrait(_) => {}
1432                        }
1433                    }
1434                }
1435                // Point at the closure that couldn't satisfy the bound.
1436                ty::Closure(def_id, _) => {
1437                    bound_spans
1438                        .get_mut_or_insert_default(tcx.def_span(*def_id))
1439                        .push(format!("`{quiet}`"));
1440                }
1441                _ => {}
1442            }
1443        };
1444
1445        let mut format_pred = |pred: ty::Predicate<'tcx>| {
1446            let bound_predicate = pred.kind();
1447            match bound_predicate.skip_binder() {
1448                ty::PredicateKind::Clause(ty::ClauseKind::Projection(pred)) => {
1449                    let pred = bound_predicate.rebind(pred);
1450                    // `<Foo as Iterator>::Item = String`.
1451                    let projection_term = pred.skip_binder().projection_term;
1452                    let quiet_projection_term = projection_term
1453                        .with_replaced_self_ty(tcx, Ty::new_var(tcx, ty::TyVid::ZERO));
1454
1455                    let term = pred.skip_binder().term;
1456
1457                    let obligation = format!("{projection_term} = {term}");
1458                    let quiet =
1459                        with_forced_trimmed_paths!(format!("{} = {}", quiet_projection_term, term));
1460
1461                    bound_span_label(projection_term.self_ty(), &obligation, &quiet);
1462                    Some((obligation, projection_term.self_ty()))
1463                }
1464                ty::PredicateKind::Clause(ty::ClauseKind::Trait(poly_trait_ref)) => {
1465                    let p = poly_trait_ref.trait_ref;
1466                    let self_ty = p.self_ty();
1467                    let path = p.print_only_trait_path();
1468                    let obligation = format!("{self_ty}: {path}");
1469                    let quiet = with_forced_trimmed_paths!(format!("_: {}", path));
1470                    bound_span_label(self_ty, &obligation, &quiet);
1471                    Some((obligation, self_ty))
1472                }
1473                _ => None,
1474            }
1475        };
1476
1477        // Find all the requirements that come from a local `impl` block.
1478        let mut skip_list: UnordSet<_> = Default::default();
1479        let mut spanned_predicates = FxIndexMap::default();
1480        for (p, parent_p, cause) in unsatisfied_predicates {
1481            // Extract the predicate span and parent def id of the cause,
1482            // if we have one.
1483            let (item_def_id, cause_span) = match cause.as_ref().map(|cause| cause.code()) {
1484                Some(ObligationCauseCode::ImplDerived(data)) => {
1485                    (data.impl_or_alias_def_id, data.span)
1486                }
1487                Some(
1488                    ObligationCauseCode::WhereClauseInExpr(def_id, span, _, _)
1489                    | ObligationCauseCode::WhereClause(def_id, span),
1490                ) if !span.is_dummy() => (*def_id, *span),
1491                _ => continue,
1492            };
1493
1494            // Don't point out the span of `WellFormed` predicates.
1495            if !matches!(
1496                p.kind().skip_binder(),
1497                ty::PredicateKind::Clause(
1498                    ty::ClauseKind::Projection(..) | ty::ClauseKind::Trait(..)
1499                )
1500            ) {
1501                continue;
1502            }
1503
1504            match self.tcx.hir_get_if_local(item_def_id) {
1505                // Unmet obligation comes from a `derive` macro, point at it once to
1506                // avoid multiple span labels pointing at the same place.
1507                Some(Node::Item(hir::Item {
1508                    kind: hir::ItemKind::Impl(hir::Impl { of_trait, self_ty, .. }),
1509                    ..
1510                })) if matches!(
1511                    self_ty.span.ctxt().outer_expn_data().kind,
1512                    ExpnKind::Macro(MacroKind::Derive, _)
1513                ) || matches!(
1514                    of_trait.map(|t| t.trait_ref.path.span.ctxt().outer_expn_data().kind),
1515                    Some(ExpnKind::Macro(MacroKind::Derive, _))
1516                ) =>
1517                {
1518                    let span = self_ty.span.ctxt().outer_expn_data().call_site;
1519                    let entry = spanned_predicates.entry(span);
1520                    let entry = entry.or_insert_with(|| {
1521                        (FxIndexSet::default(), FxIndexSet::default(), Vec::new())
1522                    });
1523                    entry.0.insert(span);
1524                    entry.1.insert((
1525                        span,
1526                        "unsatisfied trait bound introduced in this `derive` macro",
1527                    ));
1528                    entry.2.push(p);
1529                    skip_list.insert(p);
1530                }
1531
1532                // Unmet obligation coming from an `impl`.
1533                Some(Node::Item(hir::Item {
1534                    kind: hir::ItemKind::Impl(hir::Impl { of_trait, self_ty, generics, .. }),
1535                    span: item_span,
1536                    ..
1537                })) => {
1538                    let sized_pred =
1539                        unsatisfied_predicates.iter().any(|(pred, _, _)| {
1540                            match pred.kind().skip_binder() {
1541                                ty::PredicateKind::Clause(ty::ClauseKind::Trait(pred)) => {
1542                                    self.tcx.is_lang_item(pred.def_id(), LangItem::Sized)
1543                                        && pred.polarity == ty::PredicatePolarity::Positive
1544                                }
1545                                _ => false,
1546                            }
1547                        });
1548                    for param in generics.params {
1549                        if param.span == cause_span && sized_pred {
1550                            let (sp, sugg) = match param.colon_span {
1551                                Some(sp) => (sp.shrink_to_hi(), " ?Sized +"),
1552                                None => (param.span.shrink_to_hi(), ": ?Sized"),
1553                            };
1554                            err.span_suggestion_verbose(
1555                                sp,
1556                                "consider relaxing the type parameter's implicit `Sized` bound",
1557                                sugg,
1558                                Applicability::MachineApplicable,
1559                            );
1560                        }
1561                    }
1562                    if let Some(pred) = parent_p {
1563                        // Done to add the "doesn't satisfy" `span_label`.
1564                        let _ = format_pred(*pred);
1565                    }
1566                    skip_list.insert(p);
1567                    let entry = spanned_predicates.entry(self_ty.span);
1568                    let entry = entry.or_insert_with(|| {
1569                        (FxIndexSet::default(), FxIndexSet::default(), Vec::new())
1570                    });
1571                    entry.2.push(p);
1572                    if cause_span != *item_span {
1573                        entry.0.insert(cause_span);
1574                        entry.1.insert((cause_span, "unsatisfied trait bound introduced here"));
1575                    } else {
1576                        if let Some(of_trait) = of_trait {
1577                            entry.0.insert(of_trait.trait_ref.path.span);
1578                        }
1579                        entry.0.insert(self_ty.span);
1580                    };
1581                    if let Some(of_trait) = of_trait {
1582                        entry.1.insert((of_trait.trait_ref.path.span, ""));
1583                    }
1584                    entry.1.insert((self_ty.span, ""));
1585                }
1586                Some(Node::Item(hir::Item {
1587                    kind: hir::ItemKind::Trait(_, rustc_ast::ast::IsAuto::Yes, ..),
1588                    span: item_span,
1589                    ..
1590                })) => {
1591                    self.dcx().span_delayed_bug(
1592                        *item_span,
1593                        "auto trait is invoked with no method error, but no error reported?",
1594                    );
1595                }
1596                Some(
1597                    Node::Item(hir::Item {
1598                        kind:
1599                            hir::ItemKind::Trait(_, _, _, ident, ..)
1600                            | hir::ItemKind::TraitAlias(_, ident, ..),
1601                        ..
1602                    })
1603                    // We may also encounter unsatisfied GAT or method bounds
1604                    | Node::TraitItem(hir::TraitItem { ident, .. })
1605                    | Node::ImplItem(hir::ImplItem { ident, .. })
1606                ) => {
1607                    skip_list.insert(p);
1608                    let entry = spanned_predicates.entry(ident.span);
1609                    let entry = entry.or_insert_with(|| {
1610                        (FxIndexSet::default(), FxIndexSet::default(), Vec::new())
1611                    });
1612                    entry.0.insert(cause_span);
1613                    entry.1.insert((ident.span, ""));
1614                    entry.1.insert((cause_span, "unsatisfied trait bound introduced here"));
1615                    entry.2.push(p);
1616                }
1617                _ => {
1618                    // It's possible to use well-formedness clauses to get obligations
1619                    // which point arbitrary items like ADTs, so there's no use in ICEing
1620                    // here if we find that the obligation originates from some other
1621                    // node that we don't handle.
1622                }
1623            }
1624        }
1625        let mut spanned_predicates: Vec<_> = spanned_predicates.into_iter().collect();
1626        spanned_predicates.sort_by_key(|(span, _)| *span);
1627        for (_, (primary_spans, span_labels, predicates)) in spanned_predicates {
1628            let mut preds: Vec<_> = predicates
1629                .iter()
1630                .filter_map(|pred| format_pred(**pred))
1631                .map(|(p, _)| format!("`{p}`"))
1632                .collect();
1633            preds.sort();
1634            preds.dedup();
1635            let msg = if let [pred] = &preds[..] {
1636                format!("trait bound {pred} was not satisfied")
1637            } else {
1638                format!("the following trait bounds were not satisfied:\n{}", preds.join("\n"),)
1639            };
1640            let mut span: MultiSpan = primary_spans.into_iter().collect::<Vec<_>>().into();
1641            for (sp, label) in span_labels {
1642                span.push_span_label(sp, label);
1643            }
1644            err.span_note(span, msg);
1645            *unsatisfied_bounds = true;
1646        }
1647
1648        let mut suggested_bounds = UnordSet::default();
1649        // The requirements that didn't have an `impl` span to show.
1650        let mut bound_list = unsatisfied_predicates
1651            .iter()
1652            .filter_map(|(pred, parent_pred, _cause)| {
1653                let mut suggested = false;
1654                format_pred(*pred).map(|(p, self_ty)| {
1655                    if let Some(parent) = parent_pred
1656                        && suggested_bounds.contains(parent)
1657                    {
1658                        // We don't suggest `PartialEq` when we already suggest `Eq`.
1659                    } else if !suggested_bounds.contains(pred)
1660                        && collect_type_param_suggestions(self_ty, *pred, &p)
1661                    {
1662                        suggested = true;
1663                        suggested_bounds.insert(pred);
1664                    }
1665                    (
1666                        match parent_pred {
1667                            None => format!("`{p}`"),
1668                            Some(parent_pred) => match format_pred(*parent_pred) {
1669                                None => format!("`{p}`"),
1670                                Some((parent_p, _)) => {
1671                                    if !suggested
1672                                        && !suggested_bounds.contains(pred)
1673                                        && !suggested_bounds.contains(parent_pred)
1674                                        && collect_type_param_suggestions(self_ty, *parent_pred, &p)
1675                                    {
1676                                        suggested_bounds.insert(pred);
1677                                    }
1678                                    format!("`{p}`\nwhich is required by `{parent_p}`")
1679                                }
1680                            },
1681                        },
1682                        *pred,
1683                    )
1684                })
1685            })
1686            .filter(|(_, pred)| !skip_list.contains(&pred))
1687            .map(|(t, _)| t)
1688            .enumerate()
1689            .collect::<Vec<(usize, String)>>();
1690
1691        if !matches!(rcvr_ty.peel_refs().kind(), ty::Param(_)) {
1692            for ((span, add_where_or_comma), obligations) in type_params.into_iter() {
1693                *restrict_type_params = true;
1694                // #74886: Sort here so that the output is always the same.
1695                let obligations = obligations.into_sorted_stable_ord();
1696                err.span_suggestion_verbose(
1697                    span,
1698                    format!(
1699                        "consider restricting the type parameter{s} to satisfy the trait \
1700                         bound{s}",
1701                        s = pluralize!(obligations.len())
1702                    ),
1703                    format!("{} {}", add_where_or_comma, obligations.join(", ")),
1704                    Applicability::MaybeIncorrect,
1705                );
1706            }
1707        }
1708
1709        bound_list.sort_by(|(_, a), (_, b)| a.cmp(b)); // Sort alphabetically.
1710        bound_list.dedup_by(|(_, a), (_, b)| a == b); // #35677
1711        bound_list.sort_by_key(|(pos, _)| *pos); // Keep the original predicate order.
1712
1713        if !bound_list.is_empty() || !skip_list.is_empty() {
1714            let bound_list =
1715                bound_list.into_iter().map(|(_, path)| path).collect::<Vec<_>>().join("\n");
1716            let actual_prefix = rcvr_ty.prefix_string(self.tcx);
1717            info!("unimplemented_traits.len() == {}", unimplemented_traits.len());
1718            let (primary_message, label, notes) = if unimplemented_traits.len() == 1
1719                && unimplemented_traits_only
1720            {
1721                unimplemented_traits
1722                    .into_iter()
1723                    .next()
1724                    .map(|(_, (trait_ref, obligation))| {
1725                        if trait_ref.self_ty().references_error() || rcvr_ty.references_error() {
1726                            // Avoid crashing.
1727                            return (None, None, Vec::new());
1728                        }
1729                        let OnUnimplementedNote { message, label, notes, .. } = self
1730                            .err_ctxt()
1731                            .on_unimplemented_note(trait_ref, &obligation, err.long_ty_path());
1732                        (message, label, notes)
1733                    })
1734                    .unwrap()
1735            } else {
1736                (None, None, Vec::new())
1737            };
1738            let primary_message = primary_message.unwrap_or_else(|| {
1739                let ty_str = self.tcx.short_string(rcvr_ty, err.long_ty_path());
1740                format!(
1741                    "the {item_kind} `{item_ident}` exists for {actual_prefix} `{ty_str}`, \
1742                     but its trait bounds were not satisfied"
1743                )
1744            });
1745            err.primary_message(primary_message);
1746            if let Some(label) = label {
1747                *custom_span_label = true;
1748                err.span_label(span, label);
1749            }
1750            if !bound_list.is_empty() {
1751                err.note(format!("the following trait bounds were not satisfied:\n{bound_list}"));
1752            }
1753            for note in notes {
1754                err.note(note);
1755            }
1756
1757            if let ty::Adt(adt_def, _) = rcvr_ty.kind() {
1758                unsatisfied_predicates.iter().find(|(pred, _parent, _cause)| {
1759                    if let ty::PredicateKind::Clause(ty::ClauseKind::Trait(pred)) =
1760                        pred.kind().skip_binder()
1761                    {
1762                        self.suggest_hashmap_on_unsatisfied_hashset_buildhasher(
1763                            err, &pred, *adt_def,
1764                        )
1765                    } else {
1766                        false
1767                    }
1768                });
1769            }
1770
1771            *suggested_derive = self.suggest_derive(err, unsatisfied_predicates);
1772            *unsatisfied_bounds = true;
1773        }
1774    }
1775
1776    /// If an appropriate error source is not found, check method chain for possible candidates
1777    fn lookup_segments_chain_for_no_match_method(
1778        &self,
1779        err: &mut Diag<'_>,
1780        item_name: Ident,
1781        item_kind: &str,
1782        source: SelfSource<'tcx>,
1783        no_match_data: &NoMatchData<'tcx>,
1784    ) {
1785        if no_match_data.unsatisfied_predicates.is_empty()
1786            && let Mode::MethodCall = no_match_data.mode
1787            && let SelfSource::MethodCall(mut source_expr) = source
1788        {
1789            let mut stack_methods = vec![];
1790            while let hir::ExprKind::MethodCall(_path_segment, rcvr_expr, _args, method_span) =
1791                source_expr.kind
1792            {
1793                // Pop the matching receiver, to align on it's notional span
1794                if let Some(prev_match) = stack_methods.pop() {
1795                    err.span_label(
1796                        method_span,
1797                        format!("{item_kind} `{item_name}` is available on `{prev_match}`"),
1798                    );
1799                }
1800                let rcvr_ty = self.resolve_vars_if_possible(
1801                    self.typeck_results
1802                        .borrow()
1803                        .expr_ty_adjusted_opt(rcvr_expr)
1804                        .unwrap_or(Ty::new_misc_error(self.tcx)),
1805                );
1806
1807                let Ok(candidates) = self.probe_for_name_many(
1808                    Mode::MethodCall,
1809                    item_name,
1810                    None,
1811                    IsSuggestion(true),
1812                    rcvr_ty,
1813                    source_expr.hir_id,
1814                    ProbeScope::TraitsInScope,
1815                ) else {
1816                    return;
1817                };
1818
1819                // FIXME: `probe_for_name_many` searches for methods in inherent implementations,
1820                // so it may return a candidate that doesn't belong to this `revr_ty`. We need to
1821                // check whether the instantiated type matches the received one.
1822                for _matched_method in candidates {
1823                    // found a match, push to stack
1824                    stack_methods.push(rcvr_ty);
1825                }
1826                source_expr = rcvr_expr;
1827            }
1828            // If there is a match at the start of the chain, add a label for it too!
1829            if let Some(prev_match) = stack_methods.pop() {
1830                err.span_label(
1831                    source_expr.span,
1832                    format!("{item_kind} `{item_name}` is available on `{prev_match}`"),
1833                );
1834            }
1835        }
1836    }
1837
1838    fn find_likely_intended_associated_item(
1839        &self,
1840        err: &mut Diag<'_>,
1841        similar_candidate: ty::AssocItem,
1842        span: Span,
1843        args: Option<&'tcx [hir::Expr<'tcx>]>,
1844        mode: Mode,
1845    ) {
1846        let tcx = self.tcx;
1847        let def_kind = similar_candidate.as_def_kind();
1848        let an = self.tcx.def_kind_descr_article(def_kind, similar_candidate.def_id);
1849        let similar_candidate_name = similar_candidate.name();
1850        let msg = format!(
1851            "there is {an} {} `{}` with a similar name",
1852            self.tcx.def_kind_descr(def_kind, similar_candidate.def_id),
1853            similar_candidate_name,
1854        );
1855        // Methods are defined within the context of a struct and their first parameter
1856        // is always `self`, which represents the instance of the struct the method is
1857        // being called on Associated functions don’t take self as a parameter and they are
1858        // not methods because they don’t have an instance of the struct to work with.
1859        if def_kind == DefKind::AssocFn {
1860            let ty_args = self.infcx.fresh_args_for_item(span, similar_candidate.def_id);
1861            let fn_sig = tcx.fn_sig(similar_candidate.def_id).instantiate(tcx, ty_args);
1862            let fn_sig = self.instantiate_binder_with_fresh_vars(
1863                span,
1864                BoundRegionConversionTime::FnCall,
1865                fn_sig,
1866            );
1867            if similar_candidate.is_method() {
1868                if let Some(args) = args
1869                    && fn_sig.inputs()[1..].len() == args.len()
1870                {
1871                    // We found a method with the same number of arguments as the method
1872                    // call expression the user wrote.
1873                    err.span_suggestion_verbose(
1874                        span,
1875                        msg,
1876                        similar_candidate_name,
1877                        Applicability::MaybeIncorrect,
1878                    );
1879                } else {
1880                    // We found a method but either the expression is not a method call or
1881                    // the argument count didn't match.
1882                    err.span_help(
1883                        tcx.def_span(similar_candidate.def_id),
1884                        format!(
1885                            "{msg}{}",
1886                            if let None = args { "" } else { ", but with different arguments" },
1887                        ),
1888                    );
1889                }
1890            } else if let Some(args) = args
1891                && fn_sig.inputs().len() == args.len()
1892            {
1893                // We have fn call expression and the argument count match the associated
1894                // function we found.
1895                err.span_suggestion_verbose(
1896                    span,
1897                    msg,
1898                    similar_candidate_name,
1899                    Applicability::MaybeIncorrect,
1900                );
1901            } else {
1902                err.span_help(tcx.def_span(similar_candidate.def_id), msg);
1903            }
1904        } else if let Mode::Path = mode
1905            && args.unwrap_or(&[]).is_empty()
1906        {
1907            // We have an associated item syntax and we found something that isn't an fn.
1908            err.span_suggestion_verbose(
1909                span,
1910                msg,
1911                similar_candidate_name,
1912                Applicability::MaybeIncorrect,
1913            );
1914        } else {
1915            // The expression is a function or method call, but the item we found is an
1916            // associated const or type.
1917            err.span_help(tcx.def_span(similar_candidate.def_id), msg);
1918        }
1919    }
1920
1921    pub(crate) fn confusable_method_name(
1922        &self,
1923        err: &mut Diag<'_>,
1924        rcvr_ty: Ty<'tcx>,
1925        item_name: Ident,
1926        call_args: Option<Vec<Ty<'tcx>>>,
1927    ) -> Option<Symbol> {
1928        if let ty::Adt(adt, adt_args) = rcvr_ty.kind() {
1929            for inherent_impl_did in self.tcx.inherent_impls(adt.did()).into_iter() {
1930                for inherent_method in
1931                    self.tcx.associated_items(inherent_impl_did).in_definition_order()
1932                {
1933                    if let Some(candidates) = find_attr!(self.tcx.get_all_attrs(inherent_method.def_id), AttributeKind::Confusables{symbols, ..} => symbols)
1934                        && candidates.contains(&item_name.name)
1935                        && inherent_method.is_fn()
1936                    {
1937                        let args =
1938                            ty::GenericArgs::identity_for_item(self.tcx, inherent_method.def_id)
1939                                .rebase_onto(
1940                                    self.tcx,
1941                                    inherent_method.container_id(self.tcx),
1942                                    adt_args,
1943                                );
1944                        let fn_sig =
1945                            self.tcx.fn_sig(inherent_method.def_id).instantiate(self.tcx, args);
1946                        let fn_sig = self.instantiate_binder_with_fresh_vars(
1947                            item_name.span,
1948                            BoundRegionConversionTime::FnCall,
1949                            fn_sig,
1950                        );
1951                        let name = inherent_method.name();
1952                        if let Some(ref args) = call_args
1953                            && fn_sig.inputs()[1..]
1954                                .iter()
1955                                .eq_by(args, |expected, found| self.may_coerce(*expected, *found))
1956                        {
1957                            err.span_suggestion_verbose(
1958                                item_name.span,
1959                                format!("you might have meant to use `{}`", name),
1960                                name,
1961                                Applicability::MaybeIncorrect,
1962                            );
1963                            return Some(name);
1964                        } else if let None = call_args {
1965                            err.span_note(
1966                                self.tcx.def_span(inherent_method.def_id),
1967                                format!("you might have meant to use method `{}`", name),
1968                            );
1969                            return Some(name);
1970                        }
1971                    }
1972                }
1973            }
1974        }
1975        None
1976    }
1977    fn note_candidates_on_method_error(
1978        &self,
1979        rcvr_ty: Ty<'tcx>,
1980        item_name: Ident,
1981        self_source: SelfSource<'tcx>,
1982        args: Option<&'tcx [hir::Expr<'tcx>]>,
1983        span: Span,
1984        err: &mut Diag<'_>,
1985        sources: &mut Vec<CandidateSource>,
1986        sugg_span: Option<Span>,
1987    ) {
1988        sources.sort_by_key(|source| match source {
1989            CandidateSource::Trait(id) => (0, self.tcx.def_path_str(id)),
1990            CandidateSource::Impl(id) => (1, self.tcx.def_path_str(id)),
1991        });
1992        sources.dedup();
1993        // Dynamic limit to avoid hiding just one candidate, which is silly.
1994        let limit = if sources.len() == 5 { 5 } else { 4 };
1995
1996        let mut suggs = vec![];
1997        for (idx, source) in sources.iter().take(limit).enumerate() {
1998            match *source {
1999                CandidateSource::Impl(impl_did) => {
2000                    // Provide the best span we can. Use the item, if local to crate, else
2001                    // the impl, if local to crate (item may be defaulted), else nothing.
2002                    let Some(item) = self.associated_value(impl_did, item_name).or_else(|| {
2003                        let impl_trait_id = self.tcx.impl_opt_trait_id(impl_did)?;
2004                        self.associated_value(impl_trait_id, item_name)
2005                    }) else {
2006                        continue;
2007                    };
2008
2009                    let note_span = if item.def_id.is_local() {
2010                        Some(self.tcx.def_span(item.def_id))
2011                    } else if impl_did.is_local() {
2012                        Some(self.tcx.def_span(impl_did))
2013                    } else {
2014                        None
2015                    };
2016
2017                    let impl_ty = self.tcx.at(span).type_of(impl_did).instantiate_identity();
2018
2019                    let insertion = match self.tcx.impl_opt_trait_ref(impl_did) {
2020                        None => String::new(),
2021                        Some(trait_ref) => {
2022                            format!(
2023                                " of the trait `{}`",
2024                                self.tcx.def_path_str(trait_ref.skip_binder().def_id)
2025                            )
2026                        }
2027                    };
2028
2029                    let (note_str, idx) = if sources.len() > 1 {
2030                        (
2031                            format!(
2032                                "candidate #{} is defined in an impl{} for the type `{}`",
2033                                idx + 1,
2034                                insertion,
2035                                impl_ty,
2036                            ),
2037                            Some(idx + 1),
2038                        )
2039                    } else {
2040                        (
2041                            format!(
2042                                "the candidate is defined in an impl{insertion} for the type `{impl_ty}`",
2043                            ),
2044                            None,
2045                        )
2046                    };
2047                    if let Some(note_span) = note_span {
2048                        // We have a span pointing to the method. Show note with snippet.
2049                        err.span_note(note_span, note_str);
2050                    } else {
2051                        err.note(note_str);
2052                    }
2053                    if let Some(sugg_span) = sugg_span
2054                        && let Some(trait_ref) = self.tcx.impl_opt_trait_ref(impl_did)
2055                        && let Some(sugg) = print_disambiguation_help(
2056                            self.tcx,
2057                            err,
2058                            self_source,
2059                            args,
2060                            trait_ref
2061                                .instantiate(
2062                                    self.tcx,
2063                                    self.fresh_args_for_item(sugg_span, impl_did),
2064                                )
2065                                .with_replaced_self_ty(self.tcx, rcvr_ty),
2066                            idx,
2067                            sugg_span,
2068                            item,
2069                        )
2070                    {
2071                        suggs.push(sugg);
2072                    }
2073                }
2074                CandidateSource::Trait(trait_did) => {
2075                    let Some(item) = self.associated_value(trait_did, item_name) else { continue };
2076                    let item_span = self.tcx.def_span(item.def_id);
2077                    let idx = if sources.len() > 1 {
2078                        let msg = format!(
2079                            "candidate #{} is defined in the trait `{}`",
2080                            idx + 1,
2081                            self.tcx.def_path_str(trait_did)
2082                        );
2083                        err.span_note(item_span, msg);
2084                        Some(idx + 1)
2085                    } else {
2086                        let msg = format!(
2087                            "the candidate is defined in the trait `{}`",
2088                            self.tcx.def_path_str(trait_did)
2089                        );
2090                        err.span_note(item_span, msg);
2091                        None
2092                    };
2093                    if let Some(sugg_span) = sugg_span
2094                        && let Some(sugg) = print_disambiguation_help(
2095                            self.tcx,
2096                            err,
2097                            self_source,
2098                            args,
2099                            ty::TraitRef::new_from_args(
2100                                self.tcx,
2101                                trait_did,
2102                                self.fresh_args_for_item(sugg_span, trait_did),
2103                            )
2104                            .with_replaced_self_ty(self.tcx, rcvr_ty),
2105                            idx,
2106                            sugg_span,
2107                            item,
2108                        )
2109                    {
2110                        suggs.push(sugg);
2111                    }
2112                }
2113            }
2114        }
2115        if !suggs.is_empty()
2116            && let Some(span) = sugg_span
2117        {
2118            suggs.sort();
2119            err.span_suggestions(
2120                span.with_hi(item_name.span.lo()),
2121                "use fully-qualified syntax to disambiguate",
2122                suggs,
2123                Applicability::MachineApplicable,
2124            );
2125        }
2126        if sources.len() > limit {
2127            err.note(format!("and {} others", sources.len() - limit));
2128        }
2129    }
2130
2131    /// Look at all the associated functions without receivers in the type's inherent impls
2132    /// to look for builders that return `Self`, `Option<Self>` or `Result<Self, _>`.
2133    fn find_builder_fn(&self, err: &mut Diag<'_>, rcvr_ty: Ty<'tcx>, expr_id: hir::HirId) {
2134        let ty::Adt(adt_def, _) = rcvr_ty.kind() else {
2135            return;
2136        };
2137        let mut items = self
2138            .tcx
2139            .inherent_impls(adt_def.did())
2140            .iter()
2141            .flat_map(|i| self.tcx.associated_items(i).in_definition_order())
2142            // Only assoc fn with no receivers and only if
2143            // they are resolvable
2144            .filter(|item| {
2145                matches!(item.kind, ty::AssocKind::Fn { has_self: false, .. })
2146                    && self
2147                        .probe_for_name(
2148                            Mode::Path,
2149                            item.ident(self.tcx),
2150                            None,
2151                            IsSuggestion(true),
2152                            rcvr_ty,
2153                            expr_id,
2154                            ProbeScope::TraitsInScope,
2155                        )
2156                        .is_ok()
2157            })
2158            .filter_map(|item| {
2159                // Only assoc fns that return `Self`, `Option<Self>` or `Result<Self, _>`.
2160                let ret_ty = self
2161                    .tcx
2162                    .fn_sig(item.def_id)
2163                    .instantiate(self.tcx, self.fresh_args_for_item(DUMMY_SP, item.def_id))
2164                    .output();
2165                let ret_ty = self.tcx.instantiate_bound_regions_with_erased(ret_ty);
2166                let ty::Adt(def, args) = ret_ty.kind() else {
2167                    return None;
2168                };
2169                // Check for `-> Self`
2170                if self.can_eq(self.param_env, ret_ty, rcvr_ty) {
2171                    return Some((item.def_id, ret_ty));
2172                }
2173                // Check for `-> Option<Self>` or `-> Result<Self, _>`
2174                if ![self.tcx.lang_items().option_type(), self.tcx.get_diagnostic_item(sym::Result)]
2175                    .contains(&Some(def.did()))
2176                {
2177                    return None;
2178                }
2179                let arg = args.get(0)?.expect_ty();
2180                if self.can_eq(self.param_env, rcvr_ty, arg) {
2181                    Some((item.def_id, ret_ty))
2182                } else {
2183                    None
2184                }
2185            })
2186            .collect::<Vec<_>>();
2187        let post = if items.len() > 5 {
2188            let items_len = items.len();
2189            items.truncate(4);
2190            format!("\nand {} others", items_len - 4)
2191        } else {
2192            String::new()
2193        };
2194        match &items[..] {
2195            [] => {}
2196            [(def_id, ret_ty)] => {
2197                err.span_note(
2198                    self.tcx.def_span(def_id),
2199                    format!(
2200                        "if you're trying to build a new `{rcvr_ty}`, consider using `{}` which \
2201                         returns `{ret_ty}`",
2202                        self.tcx.def_path_str(def_id),
2203                    ),
2204                );
2205            }
2206            _ => {
2207                let span: MultiSpan = items
2208                    .iter()
2209                    .map(|(def_id, _)| self.tcx.def_span(def_id))
2210                    .collect::<Vec<Span>>()
2211                    .into();
2212                err.span_note(
2213                    span,
2214                    format!(
2215                        "if you're trying to build a new `{rcvr_ty}` consider using one of the \
2216                         following associated functions:\n{}{post}",
2217                        items
2218                            .iter()
2219                            .map(|(def_id, _ret_ty)| self.tcx.def_path_str(def_id))
2220                            .collect::<Vec<String>>()
2221                            .join("\n")
2222                    ),
2223                );
2224            }
2225        }
2226    }
2227
2228    /// Suggest calling `Ty::method` if `.method()` isn't found because the method
2229    /// doesn't take a `self` receiver.
2230    fn suggest_associated_call_syntax(
2231        &self,
2232        err: &mut Diag<'_>,
2233        static_candidates: &[CandidateSource],
2234        rcvr_ty: Ty<'tcx>,
2235        source: SelfSource<'tcx>,
2236        item_name: Ident,
2237        args: Option<&'tcx [hir::Expr<'tcx>]>,
2238        sugg_span: Span,
2239    ) {
2240        let mut has_unsuggestable_args = false;
2241        let ty_str = if let Some(CandidateSource::Impl(impl_did)) = static_candidates.get(0) {
2242            // When the "method" is resolved through dereferencing, we really want the
2243            // original type that has the associated function for accurate suggestions.
2244            // (#61411)
2245            let impl_ty = self.tcx.type_of(*impl_did).instantiate_identity();
2246            let target_ty = self
2247                .autoderef(sugg_span, rcvr_ty)
2248                .silence_errors()
2249                .find(|(rcvr_ty, _)| {
2250                    DeepRejectCtxt::relate_rigid_infer(self.tcx).types_may_unify(*rcvr_ty, impl_ty)
2251                })
2252                .map_or(impl_ty, |(ty, _)| ty)
2253                .peel_refs();
2254            if let ty::Adt(def, args) = target_ty.kind() {
2255                // If there are any inferred arguments, (`{integer}`), we should replace
2256                // them with underscores to allow the compiler to infer them
2257                let infer_args = self.tcx.mk_args_from_iter(args.into_iter().map(|arg| {
2258                    if !arg.is_suggestable(self.tcx, true) {
2259                        has_unsuggestable_args = true;
2260                        match arg.kind() {
2261                            GenericArgKind::Lifetime(_) => {
2262                                self.next_region_var(RegionVariableOrigin::Misc(DUMMY_SP)).into()
2263                            }
2264                            GenericArgKind::Type(_) => self.next_ty_var(DUMMY_SP).into(),
2265                            GenericArgKind::Const(_) => self.next_const_var(DUMMY_SP).into(),
2266                        }
2267                    } else {
2268                        arg
2269                    }
2270                }));
2271
2272                self.tcx.value_path_str_with_args(def.did(), infer_args)
2273            } else {
2274                self.ty_to_value_string(target_ty)
2275            }
2276        } else {
2277            self.ty_to_value_string(rcvr_ty.peel_refs())
2278        };
2279        if let SelfSource::MethodCall(_) = source {
2280            let first_arg = static_candidates.get(0).and_then(|candidate_source| {
2281                let (assoc_did, self_ty) = match candidate_source {
2282                    CandidateSource::Impl(impl_did) => {
2283                        (*impl_did, self.tcx.type_of(*impl_did).instantiate_identity())
2284                    }
2285                    CandidateSource::Trait(trait_did) => (*trait_did, rcvr_ty),
2286                };
2287
2288                let assoc = self.associated_value(assoc_did, item_name)?;
2289                if !assoc.is_fn() {
2290                    return None;
2291                }
2292
2293                // for CandidateSource::Impl, `Self` will be instantiated to a concrete type
2294                // but for CandidateSource::Trait, `Self` is still `Self`
2295                let sig = self.tcx.fn_sig(assoc.def_id).instantiate_identity();
2296                sig.inputs().skip_binder().get(0).and_then(|first| {
2297                    // if the type of first arg is the same as the current impl type, we should take the first arg into assoc function
2298                    let first_ty = first.peel_refs();
2299                    if first_ty == self_ty || first_ty == self.tcx.types.self_param {
2300                        Some(first.ref_mutability().map_or("", |mutbl| mutbl.ref_prefix_str()))
2301                    } else {
2302                        None
2303                    }
2304                })
2305            });
2306
2307            let mut applicability = Applicability::MachineApplicable;
2308            let args = if let SelfSource::MethodCall(receiver) = source
2309                && let Some(args) = args
2310            {
2311                // The first arg is the same kind as the receiver
2312                let explicit_args = if first_arg.is_some() {
2313                    std::iter::once(receiver).chain(args.iter()).collect::<Vec<_>>()
2314                } else {
2315                    // There is no `Self` kind to infer the arguments from
2316                    if has_unsuggestable_args {
2317                        applicability = Applicability::HasPlaceholders;
2318                    }
2319                    args.iter().collect()
2320                };
2321                format!(
2322                    "({}{})",
2323                    first_arg.unwrap_or(""),
2324                    explicit_args
2325                        .iter()
2326                        .map(|arg| self
2327                            .tcx
2328                            .sess
2329                            .source_map()
2330                            .span_to_snippet(arg.span)
2331                            .unwrap_or_else(|_| {
2332                                applicability = Applicability::HasPlaceholders;
2333                                "_".to_owned()
2334                            }))
2335                        .collect::<Vec<_>>()
2336                        .join(", "),
2337                )
2338            } else {
2339                applicability = Applicability::HasPlaceholders;
2340                "(...)".to_owned()
2341            };
2342            err.span_suggestion(
2343                sugg_span,
2344                "use associated function syntax instead",
2345                format!("{ty_str}::{item_name}{args}"),
2346                applicability,
2347            );
2348        } else {
2349            err.help(format!("try with `{ty_str}::{item_name}`",));
2350        }
2351    }
2352
2353    /// Suggest calling a field with a type that implements the `Fn*` traits instead of a method with
2354    /// the same name as the field i.e. `(a.my_fn_ptr)(10)` instead of `a.my_fn_ptr(10)`.
2355    fn suggest_calling_field_as_fn(
2356        &self,
2357        span: Span,
2358        rcvr_ty: Ty<'tcx>,
2359        expr: &hir::Expr<'_>,
2360        item_name: Ident,
2361        err: &mut Diag<'_>,
2362    ) -> bool {
2363        let tcx = self.tcx;
2364        let field_receiver =
2365            self.autoderef(span, rcvr_ty).silence_errors().find_map(|(ty, _)| match ty.kind() {
2366                ty::Adt(def, args) if !def.is_enum() => {
2367                    let variant = &def.non_enum_variant();
2368                    tcx.find_field_index(item_name, variant).map(|index| {
2369                        let field = &variant.fields[index];
2370                        let field_ty = field.ty(tcx, args);
2371                        (field, field_ty)
2372                    })
2373                }
2374                _ => None,
2375            });
2376        if let Some((field, field_ty)) = field_receiver {
2377            let scope = tcx.parent_module_from_def_id(self.body_id);
2378            let is_accessible = field.vis.is_accessible_from(scope, tcx);
2379
2380            if is_accessible {
2381                if let Some((what, _, _)) = self.extract_callable_info(field_ty) {
2382                    let what = match what {
2383                        DefIdOrName::DefId(def_id) => self.tcx.def_descr(def_id),
2384                        DefIdOrName::Name(what) => what,
2385                    };
2386                    let expr_span = expr.span.to(item_name.span);
2387                    err.multipart_suggestion(
2388                        format!(
2389                            "to call the {what} stored in `{item_name}`, \
2390                            surround the field access with parentheses",
2391                        ),
2392                        vec![
2393                            (expr_span.shrink_to_lo(), '('.to_string()),
2394                            (expr_span.shrink_to_hi(), ')'.to_string()),
2395                        ],
2396                        Applicability::MachineApplicable,
2397                    );
2398                } else {
2399                    let call_expr = tcx.hir_expect_expr(tcx.parent_hir_id(expr.hir_id));
2400
2401                    if let Some(span) = call_expr.span.trim_start(item_name.span) {
2402                        err.span_suggestion(
2403                            span,
2404                            "remove the arguments",
2405                            "",
2406                            Applicability::MaybeIncorrect,
2407                        );
2408                    }
2409                }
2410            }
2411
2412            let field_kind = if is_accessible { "field" } else { "private field" };
2413            err.span_label(item_name.span, format!("{field_kind}, not a method"));
2414            return true;
2415        }
2416        false
2417    }
2418
2419    /// Suggest possible range with adding parentheses, for example:
2420    /// when encountering `0..1.map(|i| i + 1)` suggest `(0..1).map(|i| i + 1)`.
2421    fn report_failed_method_call_on_range_end(
2422        &self,
2423        tcx: TyCtxt<'tcx>,
2424        actual: Ty<'tcx>,
2425        source: SelfSource<'tcx>,
2426        span: Span,
2427        item_name: Ident,
2428    ) -> Result<(), ErrorGuaranteed> {
2429        if let SelfSource::MethodCall(expr) = source {
2430            for (_, parent) in tcx.hir_parent_iter(expr.hir_id).take(5) {
2431                if let Node::Expr(parent_expr) = parent {
2432                    if !is_range_literal(parent_expr) {
2433                        continue;
2434                    }
2435                    let lang_item = match parent_expr.kind {
2436                        ExprKind::Struct(qpath, _, _) => match tcx.qpath_lang_item(*qpath) {
2437                            Some(
2438                                lang_item @ (LangItem::Range
2439                                | LangItem::RangeCopy
2440                                | LangItem::RangeInclusiveCopy
2441                                | LangItem::RangeTo
2442                                | LangItem::RangeToInclusive),
2443                            ) => Some(lang_item),
2444                            _ => None,
2445                        },
2446                        ExprKind::Call(func, _) => match func.kind {
2447                            // `..=` desugars into `::std::ops::RangeInclusive::new(...)`.
2448                            ExprKind::Path(qpath)
2449                                if tcx.qpath_is_lang_item(qpath, LangItem::RangeInclusiveNew) =>
2450                            {
2451                                Some(LangItem::RangeInclusiveStruct)
2452                            }
2453                            _ => None,
2454                        },
2455                        _ => None,
2456                    };
2457
2458                    if lang_item.is_none() {
2459                        continue;
2460                    }
2461
2462                    let span_included = match parent_expr.kind {
2463                        hir::ExprKind::Struct(_, eps, _) => {
2464                            eps.last().is_some_and(|ep| ep.span.contains(span))
2465                        }
2466                        // `..=` desugars into `::std::ops::RangeInclusive::new(...)`.
2467                        hir::ExprKind::Call(func, ..) => func.span.contains(span),
2468                        _ => false,
2469                    };
2470
2471                    if !span_included {
2472                        continue;
2473                    }
2474
2475                    let Some(range_def_id) =
2476                        lang_item.and_then(|lang_item| self.tcx.lang_items().get(lang_item))
2477                    else {
2478                        continue;
2479                    };
2480                    let range_ty =
2481                        self.tcx.type_of(range_def_id).instantiate(self.tcx, &[actual.into()]);
2482
2483                    let pick = self.lookup_probe_for_diagnostic(
2484                        item_name,
2485                        range_ty,
2486                        expr,
2487                        ProbeScope::AllTraits,
2488                        None,
2489                    );
2490                    if pick.is_ok() {
2491                        let range_span = parent_expr.span.with_hi(expr.span.hi());
2492                        return Err(self.dcx().emit_err(errors::MissingParenthesesInRange {
2493                            span,
2494                            ty: actual,
2495                            method_name: item_name.as_str().to_string(),
2496                            add_missing_parentheses: Some(errors::AddMissingParenthesesInRange {
2497                                func_name: item_name.name.as_str().to_string(),
2498                                left: range_span.shrink_to_lo(),
2499                                right: range_span.shrink_to_hi(),
2500                            }),
2501                        }));
2502                    }
2503                }
2504            }
2505        }
2506        Ok(())
2507    }
2508
2509    fn report_failed_method_call_on_numerical_infer_var(
2510        &self,
2511        tcx: TyCtxt<'tcx>,
2512        actual: Ty<'tcx>,
2513        source: SelfSource<'_>,
2514        span: Span,
2515        item_kind: &str,
2516        item_name: Ident,
2517        long_ty_path: &mut Option<PathBuf>,
2518    ) -> Result<(), ErrorGuaranteed> {
2519        let found_candidate = all_traits(self.tcx)
2520            .into_iter()
2521            .any(|info| self.associated_value(info.def_id, item_name).is_some());
2522        let found_assoc = |ty: Ty<'tcx>| {
2523            simplify_type(tcx, ty, TreatParams::InstantiateWithInfer)
2524                .and_then(|simp| {
2525                    tcx.incoherent_impls(simp)
2526                        .iter()
2527                        .find_map(|&id| self.associated_value(id, item_name))
2528                })
2529                .is_some()
2530        };
2531        let found_candidate = found_candidate
2532            || found_assoc(tcx.types.i8)
2533            || found_assoc(tcx.types.i16)
2534            || found_assoc(tcx.types.i32)
2535            || found_assoc(tcx.types.i64)
2536            || found_assoc(tcx.types.i128)
2537            || found_assoc(tcx.types.u8)
2538            || found_assoc(tcx.types.u16)
2539            || found_assoc(tcx.types.u32)
2540            || found_assoc(tcx.types.u64)
2541            || found_assoc(tcx.types.u128)
2542            || found_assoc(tcx.types.f32)
2543            || found_assoc(tcx.types.f64);
2544        if found_candidate
2545            && actual.is_numeric()
2546            && !actual.has_concrete_skeleton()
2547            && let SelfSource::MethodCall(expr) = source
2548        {
2549            let ty_str = self.tcx.short_string(actual, long_ty_path);
2550            let mut err = struct_span_code_err!(
2551                self.dcx(),
2552                span,
2553                E0689,
2554                "can't call {item_kind} `{item_name}` on ambiguous numeric type `{ty_str}`"
2555            );
2556            *err.long_ty_path() = long_ty_path.take();
2557            let concrete_type = if actual.is_integral() { "i32" } else { "f32" };
2558            match expr.kind {
2559                ExprKind::Lit(lit) => {
2560                    // numeric literal
2561                    let snippet = tcx
2562                        .sess
2563                        .source_map()
2564                        .span_to_snippet(lit.span)
2565                        .unwrap_or_else(|_| "<numeric literal>".to_owned());
2566
2567                    // If this is a floating point literal that ends with '.',
2568                    // get rid of it to stop this from becoming a member access.
2569                    let snippet = snippet.trim_suffix('.');
2570                    err.span_suggestion(
2571                        lit.span,
2572                        format!(
2573                            "you must specify a concrete type for this numeric value, \
2574                                         like `{concrete_type}`"
2575                        ),
2576                        format!("{snippet}_{concrete_type}"),
2577                        Applicability::MaybeIncorrect,
2578                    );
2579                }
2580                ExprKind::Path(QPath::Resolved(_, path)) => {
2581                    // local binding
2582                    if let hir::def::Res::Local(hir_id) = path.res {
2583                        let span = tcx.hir_span(hir_id);
2584                        let filename = tcx.sess.source_map().span_to_filename(span);
2585
2586                        let parent_node = self.tcx.parent_hir_node(hir_id);
2587                        let msg = format!(
2588                            "you must specify a type for this binding, like `{concrete_type}`",
2589                        );
2590
2591                        // FIXME: Maybe FileName::Anon should also be handled,
2592                        // otherwise there would be no suggestion if the source is STDIN for example.
2593                        match (filename, parent_node) {
2594                            (
2595                                FileName::Real(_),
2596                                Node::LetStmt(hir::LetStmt {
2597                                    source: hir::LocalSource::Normal,
2598                                    ty,
2599                                    ..
2600                                }),
2601                            ) => {
2602                                let type_span = ty
2603                                    .map(|ty| ty.span.with_lo(span.hi()))
2604                                    .unwrap_or(span.shrink_to_hi());
2605                                err.span_suggestion(
2606                                    // account for `let x: _ = 42;`
2607                                    //                   ^^^
2608                                    type_span,
2609                                    msg,
2610                                    format!(": {concrete_type}"),
2611                                    Applicability::MaybeIncorrect,
2612                                );
2613                            }
2614                            // For closure parameters with reference patterns (e.g., |&v|), suggest the type annotation
2615                            // on the pattern itself, e.g., |&v: &i32|
2616                            (FileName::Real(_), Node::Pat(pat))
2617                                if let Node::Pat(binding_pat) = self.tcx.hir_node(hir_id)
2618                                    && let hir::PatKind::Binding(..) = binding_pat.kind
2619                                    && let Node::Pat(parent_pat) = parent_node
2620                                    && matches!(parent_pat.kind, hir::PatKind::Ref(..)) =>
2621                            {
2622                                err.span_label(span, "you must specify a type for this binding");
2623
2624                                let mut ref_muts = Vec::new();
2625                                let mut current_node = parent_node;
2626
2627                                while let Node::Pat(parent_pat) = current_node {
2628                                    if let hir::PatKind::Ref(_, _, mutability) = parent_pat.kind {
2629                                        ref_muts.push(mutability);
2630                                        current_node = self.tcx.parent_hir_node(parent_pat.hir_id);
2631                                    } else {
2632                                        break;
2633                                    }
2634                                }
2635
2636                                let mut type_annotation = String::new();
2637                                for mutability in ref_muts.iter().rev() {
2638                                    match mutability {
2639                                        hir::Mutability::Mut => type_annotation.push_str("&mut "),
2640                                        hir::Mutability::Not => type_annotation.push('&'),
2641                                    }
2642                                }
2643                                type_annotation.push_str(&concrete_type);
2644
2645                                err.span_suggestion_verbose(
2646                                    pat.span.shrink_to_hi(),
2647                                    "specify the type in the closure argument list",
2648                                    format!(": {type_annotation}"),
2649                                    Applicability::MaybeIncorrect,
2650                                );
2651                            }
2652                            _ => {
2653                                err.span_label(span, msg);
2654                            }
2655                        }
2656                    }
2657                }
2658                _ => {}
2659            }
2660            return Err(err.emit());
2661        }
2662        Ok(())
2663    }
2664
2665    /// For code `rect::area(...)`,
2666    /// if `rect` is a local variable and `area` is a valid assoc method for it,
2667    /// we try to suggest `rect.area()`
2668    pub(crate) fn suggest_assoc_method_call(&self, segs: &[PathSegment<'_>]) {
2669        debug!("suggest_assoc_method_call segs: {:?}", segs);
2670        let [seg1, seg2] = segs else {
2671            return;
2672        };
2673        self.dcx().try_steal_modify_and_emit_err(
2674            seg1.ident.span,
2675            StashKey::CallAssocMethod,
2676            |err| {
2677                let body = self.tcx.hir_body_owned_by(self.body_id);
2678                struct LetVisitor {
2679                    ident_name: Symbol,
2680                }
2681
2682                // FIXME: This really should be taking scoping, etc into account.
2683                impl<'v> Visitor<'v> for LetVisitor {
2684                    type Result = ControlFlow<Option<&'v hir::Expr<'v>>>;
2685                    fn visit_stmt(&mut self, ex: &'v hir::Stmt<'v>) -> Self::Result {
2686                        if let hir::StmtKind::Let(&hir::LetStmt { pat, init, .. }) = ex.kind
2687                            && let hir::PatKind::Binding(_, _, ident, ..) = pat.kind
2688                            && ident.name == self.ident_name
2689                        {
2690                            ControlFlow::Break(init)
2691                        } else {
2692                            hir::intravisit::walk_stmt(self, ex)
2693                        }
2694                    }
2695                }
2696
2697                if let Node::Expr(call_expr) = self.tcx.parent_hir_node(seg1.hir_id)
2698                    && let ControlFlow::Break(Some(expr)) =
2699                        (LetVisitor { ident_name: seg1.ident.name }).visit_body(body)
2700                    && let Some(self_ty) = self.node_ty_opt(expr.hir_id)
2701                {
2702                    let probe = self.lookup_probe_for_diagnostic(
2703                        seg2.ident,
2704                        self_ty,
2705                        call_expr,
2706                        ProbeScope::TraitsInScope,
2707                        None,
2708                    );
2709                    if probe.is_ok() {
2710                        let sm = self.infcx.tcx.sess.source_map();
2711                        err.span_suggestion_verbose(
2712                            sm.span_extend_while(seg1.ident.span.shrink_to_hi(), |c| c == ':')
2713                                .unwrap(),
2714                            "you may have meant to call an instance method",
2715                            ".",
2716                            Applicability::MaybeIncorrect,
2717                        );
2718                    }
2719                }
2720            },
2721        );
2722    }
2723
2724    /// Suggest calling a method on a field i.e. `a.field.bar()` instead of `a.bar()`
2725    fn suggest_calling_method_on_field(
2726        &self,
2727        err: &mut Diag<'_>,
2728        source: SelfSource<'tcx>,
2729        span: Span,
2730        actual: Ty<'tcx>,
2731        item_name: Ident,
2732        return_type: Option<Ty<'tcx>>,
2733    ) {
2734        if let SelfSource::MethodCall(expr) = source {
2735            let mod_id = self.tcx.parent_module(expr.hir_id).to_def_id();
2736            for fields in self.get_field_candidates_considering_privacy_for_diag(
2737                span,
2738                actual,
2739                mod_id,
2740                expr.hir_id,
2741            ) {
2742                let call_expr = self.tcx.hir_expect_expr(self.tcx.parent_hir_id(expr.hir_id));
2743
2744                let lang_items = self.tcx.lang_items();
2745                let never_mention_traits = [
2746                    lang_items.clone_trait(),
2747                    lang_items.deref_trait(),
2748                    lang_items.deref_mut_trait(),
2749                    self.tcx.get_diagnostic_item(sym::AsRef),
2750                    self.tcx.get_diagnostic_item(sym::AsMut),
2751                    self.tcx.get_diagnostic_item(sym::Borrow),
2752                    self.tcx.get_diagnostic_item(sym::BorrowMut),
2753                ];
2754                let mut candidate_fields: Vec<_> = fields
2755                    .into_iter()
2756                    .filter_map(|candidate_field| {
2757                        self.check_for_nested_field_satisfying_condition_for_diag(
2758                            span,
2759                            &|_, field_ty| {
2760                                self.lookup_probe_for_diagnostic(
2761                                    item_name,
2762                                    field_ty,
2763                                    call_expr,
2764                                    ProbeScope::TraitsInScope,
2765                                    return_type,
2766                                )
2767                                .is_ok_and(|pick| {
2768                                    !never_mention_traits
2769                                        .iter()
2770                                        .flatten()
2771                                        .any(|def_id| self.tcx.parent(pick.item.def_id) == *def_id)
2772                                })
2773                            },
2774                            candidate_field,
2775                            vec![],
2776                            mod_id,
2777                            expr.hir_id,
2778                        )
2779                    })
2780                    .map(|field_path| {
2781                        field_path
2782                            .iter()
2783                            .map(|id| id.to_string())
2784                            .collect::<Vec<String>>()
2785                            .join(".")
2786                    })
2787                    .collect();
2788                candidate_fields.sort();
2789
2790                let len = candidate_fields.len();
2791                if len > 0 {
2792                    err.span_suggestions(
2793                        item_name.span.shrink_to_lo(),
2794                        format!(
2795                            "{} of the expressions' fields {} a method of the same name",
2796                            if len > 1 { "some" } else { "one" },
2797                            if len > 1 { "have" } else { "has" },
2798                        ),
2799                        candidate_fields.iter().map(|path| format!("{path}.")),
2800                        Applicability::MaybeIncorrect,
2801                    );
2802                }
2803            }
2804        }
2805    }
2806
2807    fn suggest_unwrapping_inner_self(
2808        &self,
2809        err: &mut Diag<'_>,
2810        source: SelfSource<'tcx>,
2811        actual: Ty<'tcx>,
2812        item_name: Ident,
2813    ) {
2814        let tcx = self.tcx;
2815        let SelfSource::MethodCall(expr) = source else {
2816            return;
2817        };
2818        let call_expr = tcx.hir_expect_expr(tcx.parent_hir_id(expr.hir_id));
2819
2820        let ty::Adt(kind, args) = actual.kind() else {
2821            return;
2822        };
2823        match kind.adt_kind() {
2824            ty::AdtKind::Enum => {
2825                let matching_variants: Vec<_> = kind
2826                    .variants()
2827                    .iter()
2828                    .flat_map(|variant| {
2829                        let [field] = &variant.fields.raw[..] else {
2830                            return None;
2831                        };
2832                        let field_ty = field.ty(tcx, args);
2833
2834                        // Skip `_`, since that'll just lead to ambiguity.
2835                        if self.resolve_vars_if_possible(field_ty).is_ty_var() {
2836                            return None;
2837                        }
2838
2839                        self.lookup_probe_for_diagnostic(
2840                            item_name,
2841                            field_ty,
2842                            call_expr,
2843                            ProbeScope::TraitsInScope,
2844                            None,
2845                        )
2846                        .ok()
2847                        .map(|pick| (variant, field, pick))
2848                    })
2849                    .collect();
2850
2851                let ret_ty_matches = |diagnostic_item| {
2852                    if let Some(ret_ty) = self
2853                        .ret_coercion
2854                        .as_ref()
2855                        .map(|c| self.resolve_vars_if_possible(c.borrow().expected_ty()))
2856                        && let ty::Adt(kind, _) = ret_ty.kind()
2857                        && tcx.get_diagnostic_item(diagnostic_item) == Some(kind.did())
2858                    {
2859                        true
2860                    } else {
2861                        false
2862                    }
2863                };
2864
2865                match &matching_variants[..] {
2866                    [(_, field, pick)] => {
2867                        let self_ty = field.ty(tcx, args);
2868                        err.span_note(
2869                            tcx.def_span(pick.item.def_id),
2870                            format!("the method `{item_name}` exists on the type `{self_ty}`"),
2871                        );
2872                        let (article, kind, variant, question) =
2873                            if tcx.is_diagnostic_item(sym::Result, kind.did()) {
2874                                ("a", "Result", "Err", ret_ty_matches(sym::Result))
2875                            } else if tcx.is_diagnostic_item(sym::Option, kind.did()) {
2876                                ("an", "Option", "None", ret_ty_matches(sym::Option))
2877                            } else {
2878                                return;
2879                            };
2880                        if question {
2881                            err.span_suggestion_verbose(
2882                                expr.span.shrink_to_hi(),
2883                                format!(
2884                                    "use the `?` operator to extract the `{self_ty}` value, propagating \
2885                                    {article} `{kind}::{variant}` value to the caller"
2886                                ),
2887                                "?",
2888                                Applicability::MachineApplicable,
2889                            );
2890                        } else {
2891                            err.span_suggestion_verbose(
2892                                expr.span.shrink_to_hi(),
2893                                format!(
2894                                    "consider using `{kind}::expect` to unwrap the `{self_ty}` value, \
2895                                    panicking if the value is {article} `{kind}::{variant}`"
2896                                ),
2897                                ".expect(\"REASON\")",
2898                                Applicability::HasPlaceholders,
2899                            );
2900                        }
2901                    }
2902                    // FIXME(compiler-errors): Support suggestions for other matching enum variants
2903                    _ => {}
2904                }
2905            }
2906            // Target wrapper types - types that wrap or pretend to wrap another type,
2907            // perhaps this inner type is meant to be called?
2908            ty::AdtKind::Struct | ty::AdtKind::Union => {
2909                let [first] = ***args else {
2910                    return;
2911                };
2912                let ty::GenericArgKind::Type(ty) = first.kind() else {
2913                    return;
2914                };
2915                let Ok(pick) = self.lookup_probe_for_diagnostic(
2916                    item_name,
2917                    ty,
2918                    call_expr,
2919                    ProbeScope::TraitsInScope,
2920                    None,
2921                ) else {
2922                    return;
2923                };
2924
2925                let name = self.ty_to_value_string(actual);
2926                let inner_id = kind.did();
2927                let mutable = if let Some(AutorefOrPtrAdjustment::Autoref { mutbl, .. }) =
2928                    pick.autoref_or_ptr_adjustment
2929                {
2930                    Some(mutbl)
2931                } else {
2932                    None
2933                };
2934
2935                if tcx.is_diagnostic_item(sym::LocalKey, inner_id) {
2936                    err.help("use `with` or `try_with` to access thread local storage");
2937                } else if tcx.is_lang_item(kind.did(), LangItem::MaybeUninit) {
2938                    err.help(format!(
2939                        "if this `{name}` has been initialized, \
2940                        use one of the `assume_init` methods to access the inner value"
2941                    ));
2942                } else if tcx.is_diagnostic_item(sym::RefCell, inner_id) {
2943                    let (suggestion, borrow_kind, panic_if) = match mutable {
2944                        Some(Mutability::Not) => (".borrow()", "borrow", "a mutable borrow exists"),
2945                        Some(Mutability::Mut) => {
2946                            (".borrow_mut()", "mutably borrow", "any borrows exist")
2947                        }
2948                        None => return,
2949                    };
2950                    err.span_suggestion_verbose(
2951                        expr.span.shrink_to_hi(),
2952                        format!(
2953                            "use `{suggestion}` to {borrow_kind} the `{ty}`, \
2954                            panicking if {panic_if}"
2955                        ),
2956                        suggestion,
2957                        Applicability::MaybeIncorrect,
2958                    );
2959                } else if tcx.is_diagnostic_item(sym::Mutex, inner_id) {
2960                    err.span_suggestion_verbose(
2961                        expr.span.shrink_to_hi(),
2962                        format!(
2963                            "use `.lock().unwrap()` to borrow the `{ty}`, \
2964                            blocking the current thread until it can be acquired"
2965                        ),
2966                        ".lock().unwrap()",
2967                        Applicability::MaybeIncorrect,
2968                    );
2969                } else if tcx.is_diagnostic_item(sym::RwLock, inner_id) {
2970                    let (suggestion, borrow_kind) = match mutable {
2971                        Some(Mutability::Not) => (".read().unwrap()", "borrow"),
2972                        Some(Mutability::Mut) => (".write().unwrap()", "mutably borrow"),
2973                        None => return,
2974                    };
2975                    err.span_suggestion_verbose(
2976                        expr.span.shrink_to_hi(),
2977                        format!(
2978                            "use `{suggestion}` to {borrow_kind} the `{ty}`, \
2979                            blocking the current thread until it can be acquired"
2980                        ),
2981                        suggestion,
2982                        Applicability::MaybeIncorrect,
2983                    );
2984                } else {
2985                    return;
2986                };
2987
2988                err.span_note(
2989                    tcx.def_span(pick.item.def_id),
2990                    format!("the method `{item_name}` exists on the type `{ty}`"),
2991                );
2992            }
2993        }
2994    }
2995
2996    pub(crate) fn note_unmet_impls_on_type(
2997        &self,
2998        err: &mut Diag<'_>,
2999        errors: &[FulfillmentError<'tcx>],
3000        suggest_derive: bool,
3001    ) {
3002        let preds: Vec<_> = errors
3003            .iter()
3004            .filter_map(|e| match e.obligation.predicate.kind().skip_binder() {
3005                ty::PredicateKind::Clause(ty::ClauseKind::Trait(pred)) => {
3006                    match pred.self_ty().kind() {
3007                        ty::Adt(_, _) => Some((e.root_obligation.predicate, pred)),
3008                        _ => None,
3009                    }
3010                }
3011                _ => None,
3012            })
3013            .collect();
3014
3015        // Note for local items and foreign items respectively.
3016        let (mut local_preds, mut foreign_preds): (Vec<_>, Vec<_>) =
3017            preds.iter().partition(|&(_, pred)| {
3018                if let ty::Adt(def, _) = pred.self_ty().kind() {
3019                    def.did().is_local()
3020                } else {
3021                    false
3022                }
3023            });
3024
3025        local_preds.sort_by_key(|(_, pred)| pred.trait_ref.to_string());
3026        let local_def_ids = local_preds
3027            .iter()
3028            .filter_map(|(_, pred)| match pred.self_ty().kind() {
3029                ty::Adt(def, _) => Some(def.did()),
3030                _ => None,
3031            })
3032            .collect::<FxIndexSet<_>>();
3033        let mut local_spans: MultiSpan = local_def_ids
3034            .iter()
3035            .filter_map(|def_id| {
3036                let span = self.tcx.def_span(*def_id);
3037                if span.is_dummy() { None } else { Some(span) }
3038            })
3039            .collect::<Vec<_>>()
3040            .into();
3041        for (_, pred) in &local_preds {
3042            if let ty::Adt(def, _) = pred.self_ty().kind() {
3043                local_spans.push_span_label(
3044                    self.tcx.def_span(def.did()),
3045                    format!("must implement `{}`", pred.trait_ref.print_trait_sugared()),
3046                );
3047            }
3048        }
3049        if local_spans.primary_span().is_some() {
3050            let msg = if let [(_, local_pred)] = local_preds.as_slice() {
3051                format!(
3052                    "an implementation of `{}` might be missing for `{}`",
3053                    local_pred.trait_ref.print_trait_sugared(),
3054                    local_pred.self_ty()
3055                )
3056            } else {
3057                format!(
3058                    "the following type{} would have to `impl` {} required trait{} for this \
3059                     operation to be valid",
3060                    pluralize!(local_def_ids.len()),
3061                    if local_def_ids.len() == 1 { "its" } else { "their" },
3062                    pluralize!(local_preds.len()),
3063                )
3064            };
3065            err.span_note(local_spans, msg);
3066        }
3067
3068        foreign_preds
3069            .sort_by_key(|(_, pred): &(_, ty::TraitPredicate<'_>)| pred.trait_ref.to_string());
3070
3071        for (_, pred) in &foreign_preds {
3072            let ty = pred.self_ty();
3073            let ty::Adt(def, _) = ty.kind() else { continue };
3074            let span = self.tcx.def_span(def.did());
3075            if span.is_dummy() {
3076                continue;
3077            }
3078            let mut mspan: MultiSpan = span.into();
3079            mspan.push_span_label(span, format!("`{ty}` is defined in another crate"));
3080            err.span_note(
3081                mspan,
3082                format!("`{ty}` does not implement `{}`", pred.trait_ref.print_trait_sugared()),
3083            );
3084
3085            foreign_preds.iter().find(|&(root_pred, pred)| {
3086                if let ty::PredicateKind::Clause(ty::ClauseKind::Trait(root_pred)) =
3087                    root_pred.kind().skip_binder()
3088                    && let Some(root_adt) = root_pred.self_ty().ty_adt_def()
3089                {
3090                    self.suggest_hashmap_on_unsatisfied_hashset_buildhasher(err, pred, root_adt)
3091                } else {
3092                    false
3093                }
3094            });
3095        }
3096
3097        let preds: Vec<_> = errors
3098            .iter()
3099            .map(|e| (e.obligation.predicate, None, Some(e.obligation.cause.clone())))
3100            .collect();
3101        if suggest_derive {
3102            self.suggest_derive(err, &preds);
3103        } else {
3104            // The predicate comes from a binop where the lhs and rhs have different types.
3105            let _ = self.note_predicate_source_and_get_derives(err, &preds);
3106        }
3107    }
3108
3109    fn note_predicate_source_and_get_derives(
3110        &self,
3111        err: &mut Diag<'_>,
3112        unsatisfied_predicates: &UnsatisfiedPredicates<'tcx>,
3113    ) -> Vec<(String, Span, Symbol)> {
3114        let mut derives = Vec::new();
3115        let mut traits = Vec::new();
3116        for (pred, _, _) in unsatisfied_predicates {
3117            let Some(ty::PredicateKind::Clause(ty::ClauseKind::Trait(trait_pred))) =
3118                pred.kind().no_bound_vars()
3119            else {
3120                continue;
3121            };
3122            let adt = match trait_pred.self_ty().ty_adt_def() {
3123                Some(adt) if adt.did().is_local() => adt,
3124                _ => continue,
3125            };
3126            if let Some(diagnostic_name) = self.tcx.get_diagnostic_name(trait_pred.def_id()) {
3127                let can_derive = match diagnostic_name {
3128                    sym::Default => !adt.is_enum(),
3129                    sym::Eq
3130                    | sym::PartialEq
3131                    | sym::Ord
3132                    | sym::PartialOrd
3133                    | sym::Clone
3134                    | sym::Copy
3135                    | sym::Hash
3136                    | sym::Debug => true,
3137                    _ => false,
3138                };
3139                if can_derive {
3140                    let self_name = trait_pred.self_ty().to_string();
3141                    let self_span = self.tcx.def_span(adt.did());
3142                    for super_trait in
3143                        supertraits(self.tcx, ty::Binder::dummy(trait_pred.trait_ref))
3144                    {
3145                        if let Some(parent_diagnostic_name) =
3146                            self.tcx.get_diagnostic_name(super_trait.def_id())
3147                        {
3148                            derives.push((self_name.clone(), self_span, parent_diagnostic_name));
3149                        }
3150                    }
3151                    derives.push((self_name, self_span, diagnostic_name));
3152                } else {
3153                    traits.push(trait_pred.def_id());
3154                }
3155            } else {
3156                traits.push(trait_pred.def_id());
3157            }
3158        }
3159        traits.sort_by_key(|id| self.tcx.def_path_str(id));
3160        traits.dedup();
3161
3162        let len = traits.len();
3163        if len > 0 {
3164            let span =
3165                MultiSpan::from_spans(traits.iter().map(|&did| self.tcx.def_span(did)).collect());
3166            let mut names = format!("`{}`", self.tcx.def_path_str(traits[0]));
3167            for (i, &did) in traits.iter().enumerate().skip(1) {
3168                if len > 2 {
3169                    names.push_str(", ");
3170                }
3171                if i == len - 1 {
3172                    names.push_str(" and ");
3173                }
3174                names.push('`');
3175                names.push_str(&self.tcx.def_path_str(did));
3176                names.push('`');
3177            }
3178            err.span_note(
3179                span,
3180                format!("the trait{} {} must be implemented", pluralize!(len), names),
3181            );
3182        }
3183
3184        derives
3185    }
3186
3187    pub(crate) fn suggest_derive(
3188        &self,
3189        err: &mut Diag<'_>,
3190        unsatisfied_predicates: &UnsatisfiedPredicates<'tcx>,
3191    ) -> bool {
3192        let mut derives = self.note_predicate_source_and_get_derives(err, unsatisfied_predicates);
3193        derives.sort();
3194        derives.dedup();
3195
3196        let mut derives_grouped = Vec::<(String, Span, String)>::new();
3197        for (self_name, self_span, trait_name) in derives.into_iter() {
3198            if let Some((last_self_name, _, last_trait_names)) = derives_grouped.last_mut() {
3199                if last_self_name == &self_name {
3200                    last_trait_names.push_str(format!(", {trait_name}").as_str());
3201                    continue;
3202                }
3203            }
3204            derives_grouped.push((self_name, self_span, trait_name.to_string()));
3205        }
3206
3207        for (self_name, self_span, traits) in &derives_grouped {
3208            err.span_suggestion_verbose(
3209                self_span.shrink_to_lo(),
3210                format!("consider annotating `{self_name}` with `#[derive({traits})]`"),
3211                format!("#[derive({traits})]\n"),
3212                Applicability::MaybeIncorrect,
3213            );
3214        }
3215        !derives_grouped.is_empty()
3216    }
3217
3218    fn note_derefed_ty_has_method(
3219        &self,
3220        err: &mut Diag<'_>,
3221        self_source: SelfSource<'tcx>,
3222        rcvr_ty: Ty<'tcx>,
3223        item_name: Ident,
3224        expected: Expectation<'tcx>,
3225    ) {
3226        let SelfSource::QPath(ty) = self_source else {
3227            return;
3228        };
3229        for (deref_ty, _) in self.autoderef(DUMMY_SP, rcvr_ty).silence_errors().skip(1) {
3230            if let Ok(pick) = self.probe_for_name(
3231                Mode::Path,
3232                item_name,
3233                expected.only_has_type(self),
3234                IsSuggestion(true),
3235                deref_ty,
3236                ty.hir_id,
3237                ProbeScope::TraitsInScope,
3238            ) {
3239                if deref_ty.is_suggestable(self.tcx, true)
3240                    // If this method receives `&self`, then the provided
3241                    // argument _should_ coerce, so it's valid to suggest
3242                    // just changing the path.
3243                    && pick.item.is_method()
3244                    && let Some(self_ty) =
3245                        self.tcx.fn_sig(pick.item.def_id).instantiate_identity().inputs().skip_binder().get(0)
3246                    && self_ty.is_ref()
3247                {
3248                    let suggested_path = match deref_ty.kind() {
3249                        ty::Bool
3250                        | ty::Char
3251                        | ty::Int(_)
3252                        | ty::Uint(_)
3253                        | ty::Float(_)
3254                        | ty::Adt(_, _)
3255                        | ty::Str
3256                        | ty::Alias(ty::Projection | ty::Inherent, _)
3257                        | ty::Param(_) => format!("{deref_ty}"),
3258                        // we need to test something like  <&[_]>::len or <(&[u32])>::len
3259                        // and Vec::function();
3260                        // <&[_]>::len or <&[u32]>::len doesn't need an extra "<>" between
3261                        // but for Adt type like Vec::function()
3262                        // we would suggest <[_]>::function();
3263                        _ if self
3264                            .tcx
3265                            .sess
3266                            .source_map()
3267                            .span_wrapped_by_angle_or_parentheses(ty.span) =>
3268                        {
3269                            format!("{deref_ty}")
3270                        }
3271                        _ => format!("<{deref_ty}>"),
3272                    };
3273                    err.span_suggestion_verbose(
3274                        ty.span,
3275                        format!("the function `{item_name}` is implemented on `{deref_ty}`"),
3276                        suggested_path,
3277                        Applicability::MaybeIncorrect,
3278                    );
3279                } else {
3280                    err.span_note(
3281                        ty.span,
3282                        format!("the function `{item_name}` is implemented on `{deref_ty}`"),
3283                    );
3284                }
3285                return;
3286            }
3287        }
3288    }
3289
3290    fn suggest_bounds_for_range_to_method(
3291        &self,
3292        err: &mut Diag<'_>,
3293        source: SelfSource<'tcx>,
3294        item_ident: Ident,
3295    ) {
3296        let SelfSource::MethodCall(rcvr_expr) = source else { return };
3297        let hir::ExprKind::Struct(qpath, fields, _) = rcvr_expr.kind else { return };
3298        let Some(lang_item) = self.tcx.qpath_lang_item(*qpath) else {
3299            return;
3300        };
3301        let is_inclusive = match lang_item {
3302            hir::LangItem::RangeTo => false,
3303            hir::LangItem::RangeToInclusive | hir::LangItem::RangeInclusiveCopy => true,
3304            _ => return,
3305        };
3306
3307        let Some(iterator_trait) = self.tcx.get_diagnostic_item(sym::Iterator) else { return };
3308        let Some(_) = self
3309            .tcx
3310            .associated_items(iterator_trait)
3311            .filter_by_name_unhygienic(item_ident.name)
3312            .next()
3313        else {
3314            return;
3315        };
3316
3317        let source_map = self.tcx.sess.source_map();
3318        let range_type = if is_inclusive { "RangeInclusive" } else { "Range" };
3319        let Some(end_field) = fields.iter().find(|f| f.ident.name == rustc_span::sym::end) else {
3320            return;
3321        };
3322
3323        let element_ty = self.typeck_results.borrow().expr_ty_opt(end_field.expr);
3324        let is_integral = element_ty.is_some_and(|ty| ty.is_integral());
3325        let end_is_negative = is_integral
3326            && matches!(end_field.expr.kind, hir::ExprKind::Unary(rustc_ast::UnOp::Neg, _));
3327
3328        let Ok(snippet) = source_map.span_to_snippet(rcvr_expr.span) else { return };
3329
3330        let offset = snippet
3331            .chars()
3332            .take_while(|&c| c == '(' || c.is_whitespace())
3333            .map(|c| c.len_utf8())
3334            .sum::<usize>();
3335
3336        let insert_span = rcvr_expr
3337            .span
3338            .with_lo(rcvr_expr.span.lo() + rustc_span::BytePos(offset as u32))
3339            .shrink_to_lo();
3340
3341        let (value, appl) = if is_integral && !end_is_negative {
3342            ("0", Applicability::MachineApplicable)
3343        } else {
3344            ("/* start */", Applicability::HasPlaceholders)
3345        };
3346
3347        err.span_suggestion_verbose(
3348            insert_span,
3349            format!("consider using a bounded `{range_type}` by adding a concrete starting value"),
3350            value,
3351            appl,
3352        );
3353    }
3354
3355    /// Print out the type for use in value namespace.
3356    fn ty_to_value_string(&self, ty: Ty<'tcx>) -> String {
3357        match ty.kind() {
3358            ty::Adt(def, args) => self.tcx.def_path_str_with_args(def.did(), args),
3359            _ => self.ty_to_string(ty),
3360        }
3361    }
3362
3363    fn suggest_await_before_method(
3364        &self,
3365        err: &mut Diag<'_>,
3366        item_name: Ident,
3367        ty: Ty<'tcx>,
3368        call: &hir::Expr<'_>,
3369        span: Span,
3370        return_type: Option<Ty<'tcx>>,
3371    ) {
3372        let output_ty = match self.err_ctxt().get_impl_future_output_ty(ty) {
3373            Some(output_ty) => self.resolve_vars_if_possible(output_ty),
3374            _ => return,
3375        };
3376        let method_exists =
3377            self.method_exists_for_diagnostic(item_name, output_ty, call.hir_id, return_type);
3378        debug!("suggest_await_before_method: is_method_exist={}", method_exists);
3379        if method_exists {
3380            err.span_suggestion_verbose(
3381                span.shrink_to_lo(),
3382                "consider `await`ing on the `Future` and calling the method on its `Output`",
3383                "await.",
3384                Applicability::MaybeIncorrect,
3385            );
3386        }
3387    }
3388
3389    fn set_label_for_method_error(
3390        &self,
3391        err: &mut Diag<'_>,
3392        source: SelfSource<'tcx>,
3393        rcvr_ty: Ty<'tcx>,
3394        item_ident: Ident,
3395        expr_id: hir::HirId,
3396        span: Span,
3397        sugg_span: Span,
3398        within_macro_span: Option<Span>,
3399        args: Option<&'tcx [hir::Expr<'tcx>]>,
3400    ) {
3401        let tcx = self.tcx;
3402        if tcx.sess.source_map().is_multiline(sugg_span) {
3403            err.span_label(sugg_span.with_hi(span.lo()), "");
3404        }
3405        if let Some(within_macro_span) = within_macro_span {
3406            err.span_label(within_macro_span, "due to this macro variable");
3407        }
3408
3409        if matches!(source, SelfSource::QPath(_)) && args.is_some() {
3410            self.find_builder_fn(err, rcvr_ty, expr_id);
3411        }
3412
3413        if tcx.ty_is_opaque_future(rcvr_ty) && item_ident.name == sym::poll {
3414            let ty_str = self.tcx.short_string(rcvr_ty, err.long_ty_path());
3415            err.help(format!(
3416                "method `poll` found on `Pin<&mut {ty_str}>`, \
3417                see documentation for `std::pin::Pin`"
3418            ));
3419            err.help("self type must be pinned to call `Future::poll`, \
3420                see https://rust-lang.github.io/async-book/04_pinning/01_chapter.html#pinning-in-practice"
3421            );
3422        }
3423
3424        if let Some(span) =
3425            tcx.resolutions(()).confused_type_with_std_module.get(&span.with_parent(None))
3426        {
3427            err.span_suggestion(
3428                span.shrink_to_lo(),
3429                "you are looking for the module in `std`, not the primitive type",
3430                "std::",
3431                Applicability::MachineApplicable,
3432            );
3433        }
3434    }
3435
3436    fn suggest_on_pointer_type(
3437        &self,
3438        err: &mut Diag<'_>,
3439        source: SelfSource<'tcx>,
3440        rcvr_ty: Ty<'tcx>,
3441        item_ident: Ident,
3442    ) {
3443        let tcx = self.tcx;
3444        // on pointers, check if the method would exist on a reference
3445        if let SelfSource::MethodCall(rcvr_expr) = source
3446            && let ty::RawPtr(ty, ptr_mutbl) = *rcvr_ty.kind()
3447            && let Ok(pick) = self.lookup_probe_for_diagnostic(
3448                item_ident,
3449                Ty::new_ref(tcx, ty::Region::new_error_misc(tcx), ty, ptr_mutbl),
3450                self.tcx.hir_expect_expr(self.tcx.parent_hir_id(rcvr_expr.hir_id)),
3451                ProbeScope::TraitsInScope,
3452                None,
3453            )
3454            && let ty::Ref(_, _, sugg_mutbl) = *pick.self_ty.kind()
3455            && (sugg_mutbl.is_not() || ptr_mutbl.is_mut())
3456        {
3457            let (method, method_anchor) = match sugg_mutbl {
3458                Mutability::Not => {
3459                    let method_anchor = match ptr_mutbl {
3460                        Mutability::Not => "as_ref",
3461                        Mutability::Mut => "as_ref-1",
3462                    };
3463                    ("as_ref", method_anchor)
3464                }
3465                Mutability::Mut => ("as_mut", "as_mut"),
3466            };
3467            err.span_note(
3468                tcx.def_span(pick.item.def_id),
3469                format!("the method `{item_ident}` exists on the type `{ty}`", ty = pick.self_ty),
3470            );
3471            let mut_str = ptr_mutbl.ptr_str();
3472            err.note(format!(
3473                "you might want to use the unsafe method `<*{mut_str} T>::{method}` to get \
3474                an optional reference to the value behind the pointer"
3475            ));
3476            err.note(format!(
3477                "read the documentation for `<*{mut_str} T>::{method}` and ensure you satisfy its \
3478                safety preconditions before calling it to avoid undefined behavior: \
3479                https://doc.rust-lang.org/std/primitive.pointer.html#method.{method_anchor}"
3480            ));
3481        }
3482    }
3483
3484    fn suggest_use_candidates<F>(&self, candidates: Vec<DefId>, handle_candidates: F)
3485    where
3486        F: FnOnce(Vec<String>, Vec<String>, Span),
3487    {
3488        let parent_map = self.tcx.visible_parent_map(());
3489
3490        let scope = self.tcx.parent_module_from_def_id(self.body_id);
3491        let (accessible_candidates, inaccessible_candidates): (Vec<_>, Vec<_>) =
3492            candidates.into_iter().partition(|id| {
3493                let vis = self.tcx.visibility(*id);
3494                vis.is_accessible_from(scope, self.tcx)
3495            });
3496
3497        let sugg = |candidates: Vec<_>, visible| {
3498            // Separate out candidates that must be imported with a glob, because they are named `_`
3499            // and cannot be referred with their identifier.
3500            let (candidates, globs): (Vec<_>, Vec<_>) =
3501                candidates.into_iter().partition(|trait_did| {
3502                    if let Some(parent_did) = parent_map.get(trait_did) {
3503                        // If the item is re-exported as `_`, we should suggest a glob-import instead.
3504                        if *parent_did != self.tcx.parent(*trait_did)
3505                            && self
3506                                .tcx
3507                                .module_children(*parent_did)
3508                                .iter()
3509                                .filter(|child| child.res.opt_def_id() == Some(*trait_did))
3510                                .all(|child| child.ident.name == kw::Underscore)
3511                        {
3512                            return false;
3513                        }
3514                    }
3515
3516                    true
3517                });
3518
3519            let prefix = if visible { "use " } else { "" };
3520            let postfix = if visible { ";" } else { "" };
3521            let path_strings = candidates.iter().map(|trait_did| {
3522                format!(
3523                    "{prefix}{}{postfix}\n",
3524                    with_no_visible_paths_if_doc_hidden!(with_crate_prefix!(
3525                        self.tcx.def_path_str(*trait_did)
3526                    )),
3527                )
3528            });
3529
3530            let glob_path_strings = globs.iter().map(|trait_did| {
3531                let parent_did = parent_map.get(trait_did).unwrap();
3532                format!(
3533                    "{prefix}{}::*{postfix} // trait {}\n",
3534                    with_no_visible_paths_if_doc_hidden!(with_crate_prefix!(
3535                        self.tcx.def_path_str(*parent_did)
3536                    )),
3537                    self.tcx.item_name(*trait_did),
3538                )
3539            });
3540            let mut sugg: Vec<_> = path_strings.chain(glob_path_strings).collect();
3541            sugg.sort();
3542            sugg
3543        };
3544
3545        let accessible_sugg = sugg(accessible_candidates, true);
3546        let inaccessible_sugg = sugg(inaccessible_candidates, false);
3547
3548        let (module, _, _) = self.tcx.hir_get_module(scope);
3549        let span = module.spans.inject_use_span;
3550        handle_candidates(accessible_sugg, inaccessible_sugg, span);
3551    }
3552
3553    fn suggest_valid_traits(
3554        &self,
3555        err: &mut Diag<'_>,
3556        item_name: Ident,
3557        mut valid_out_of_scope_traits: Vec<DefId>,
3558        explain: bool,
3559    ) -> bool {
3560        valid_out_of_scope_traits.retain(|id| self.tcx.is_user_visible_dep(id.krate));
3561        if !valid_out_of_scope_traits.is_empty() {
3562            let mut candidates = valid_out_of_scope_traits;
3563            candidates.sort_by_key(|id| self.tcx.def_path_str(id));
3564            candidates.dedup();
3565
3566            // `TryFrom` and `FromIterator` have no methods
3567            let edition_fix = candidates
3568                .iter()
3569                .find(|did| self.tcx.is_diagnostic_item(sym::TryInto, **did))
3570                .copied();
3571
3572            if explain {
3573                err.help("items from traits can only be used if the trait is in scope");
3574            }
3575
3576            let msg = format!(
3577                "{this_trait_is} implemented but not in scope",
3578                this_trait_is = if candidates.len() == 1 {
3579                    format!(
3580                        "trait `{}` which provides `{item_name}` is",
3581                        self.tcx.item_name(candidates[0]),
3582                    )
3583                } else {
3584                    format!("the following traits which provide `{item_name}` are")
3585                }
3586            );
3587
3588            self.suggest_use_candidates(candidates, |accessible_sugg, inaccessible_sugg, span| {
3589                let suggest_for_access = |err: &mut Diag<'_>, mut msg: String, suggs: Vec<_>| {
3590                    msg += &format!(
3591                        "; perhaps you want to import {one_of}",
3592                        one_of = if suggs.len() == 1 { "it" } else { "one of them" },
3593                    );
3594                    err.span_suggestions(span, msg, suggs, Applicability::MaybeIncorrect);
3595                };
3596                let suggest_for_privacy = |err: &mut Diag<'_>, suggs: Vec<String>| {
3597                    let msg = format!(
3598                        "{this_trait_is} implemented but not reachable",
3599                        this_trait_is = if let [sugg] = suggs.as_slice() {
3600                            format!("trait `{}` which provides `{item_name}` is", sugg.trim())
3601                        } else {
3602                            format!("the following traits which provide `{item_name}` are")
3603                        }
3604                    );
3605                    if suggs.len() == 1 {
3606                        err.help(msg);
3607                    } else {
3608                        err.span_suggestions(span, msg, suggs, Applicability::MaybeIncorrect);
3609                    }
3610                };
3611                if accessible_sugg.is_empty() {
3612                    // `inaccessible_sugg` must not be empty
3613                    suggest_for_privacy(err, inaccessible_sugg);
3614                } else if inaccessible_sugg.is_empty() {
3615                    suggest_for_access(err, msg, accessible_sugg);
3616                } else {
3617                    suggest_for_access(err, msg, accessible_sugg);
3618                    suggest_for_privacy(err, inaccessible_sugg);
3619                }
3620            });
3621
3622            if let Some(did) = edition_fix {
3623                err.note(format!(
3624                    "'{}' is included in the prelude starting in Edition 2021",
3625                    with_crate_prefix!(self.tcx.def_path_str(did))
3626                ));
3627            }
3628
3629            true
3630        } else {
3631            false
3632        }
3633    }
3634
3635    fn suggest_traits_to_import(
3636        &self,
3637        err: &mut Diag<'_>,
3638        span: Span,
3639        rcvr_ty: Ty<'tcx>,
3640        item_name: Ident,
3641        inputs_len: Option<usize>,
3642        source: SelfSource<'tcx>,
3643        valid_out_of_scope_traits: Vec<DefId>,
3644        static_candidates: &[CandidateSource],
3645        unsatisfied_bounds: bool,
3646        return_type: Option<Ty<'tcx>>,
3647        trait_missing_method: bool,
3648    ) {
3649        let mut alt_rcvr_sugg = false;
3650        let mut trait_in_other_version_found = false;
3651        if let (SelfSource::MethodCall(rcvr), false) = (source, unsatisfied_bounds) {
3652            debug!(
3653                "suggest_traits_to_import: span={:?}, item_name={:?}, rcvr_ty={:?}, rcvr={:?}",
3654                span, item_name, rcvr_ty, rcvr
3655            );
3656            let skippable = [
3657                self.tcx.lang_items().clone_trait(),
3658                self.tcx.lang_items().deref_trait(),
3659                self.tcx.lang_items().deref_mut_trait(),
3660                self.tcx.lang_items().drop_trait(),
3661                self.tcx.get_diagnostic_item(sym::AsRef),
3662            ];
3663            // Try alternative arbitrary self types that could fulfill this call.
3664            // FIXME: probe for all types that *could* be arbitrary self-types, not
3665            // just this list.
3666            for (rcvr_ty, post, pin_call) in &[
3667                (rcvr_ty, "", None),
3668                (
3669                    Ty::new_mut_ref(self.tcx, self.tcx.lifetimes.re_erased, rcvr_ty),
3670                    "&mut ",
3671                    Some("as_mut"),
3672                ),
3673                (
3674                    Ty::new_imm_ref(self.tcx, self.tcx.lifetimes.re_erased, rcvr_ty),
3675                    "&",
3676                    Some("as_ref"),
3677                ),
3678            ] {
3679                match self.lookup_probe_for_diagnostic(
3680                    item_name,
3681                    *rcvr_ty,
3682                    rcvr,
3683                    ProbeScope::AllTraits,
3684                    return_type,
3685                ) {
3686                    Ok(pick) => {
3687                        // If the method is defined for the receiver we have, it likely wasn't `use`d.
3688                        // We point at the method, but we just skip the rest of the check for arbitrary
3689                        // self types and rely on the suggestion to `use` the trait from
3690                        // `suggest_valid_traits`.
3691                        let did = Some(pick.item.container_id(self.tcx));
3692                        if skippable.contains(&did) {
3693                            continue;
3694                        }
3695                        trait_in_other_version_found = self
3696                            .detect_and_explain_multiple_crate_versions_of_trait_item(
3697                                err,
3698                                pick.item.def_id,
3699                                rcvr.hir_id,
3700                                Some(*rcvr_ty),
3701                            );
3702                        if pick.autoderefs == 0 && !trait_in_other_version_found {
3703                            err.span_label(
3704                                pick.item.ident(self.tcx).span,
3705                                format!("the method is available for `{rcvr_ty}` here"),
3706                            );
3707                        }
3708                        break;
3709                    }
3710                    Err(MethodError::Ambiguity(_)) => {
3711                        // If the method is defined (but ambiguous) for the receiver we have, it is also
3712                        // likely we haven't `use`d it. It may be possible that if we `Box`/`Pin`/etc.
3713                        // the receiver, then it might disambiguate this method, but I think these
3714                        // suggestions are generally misleading (see #94218).
3715                        break;
3716                    }
3717                    Err(_) => (),
3718                }
3719
3720                let Some(unpin_trait) = self.tcx.lang_items().unpin_trait() else {
3721                    return;
3722                };
3723                let pred = ty::TraitRef::new(self.tcx, unpin_trait, [*rcvr_ty]);
3724                let unpin = self.predicate_must_hold_considering_regions(&Obligation::new(
3725                    self.tcx,
3726                    self.misc(rcvr.span),
3727                    self.param_env,
3728                    pred,
3729                ));
3730                for (rcvr_ty, pre) in &[
3731                    (Ty::new_lang_item(self.tcx, *rcvr_ty, LangItem::OwnedBox), "Box::new"),
3732                    (Ty::new_lang_item(self.tcx, *rcvr_ty, LangItem::Pin), "Pin::new"),
3733                    (Ty::new_diagnostic_item(self.tcx, *rcvr_ty, sym::Arc), "Arc::new"),
3734                    (Ty::new_diagnostic_item(self.tcx, *rcvr_ty, sym::Rc), "Rc::new"),
3735                ] {
3736                    if let Some(new_rcvr_t) = *rcvr_ty
3737                        && let Ok(pick) = self.lookup_probe_for_diagnostic(
3738                            item_name,
3739                            new_rcvr_t,
3740                            rcvr,
3741                            ProbeScope::AllTraits,
3742                            return_type,
3743                        )
3744                    {
3745                        debug!("try_alt_rcvr: pick candidate {:?}", pick);
3746                        let did = pick.item.trait_container(self.tcx);
3747                        // We don't want to suggest a container type when the missing
3748                        // method is `.clone()` or `.deref()` otherwise we'd suggest
3749                        // `Arc::new(foo).clone()`, which is far from what the user wants.
3750                        // Explicitly ignore the `Pin::as_ref()` method as `Pin` does not
3751                        // implement the `AsRef` trait.
3752                        let skip = skippable.contains(&did)
3753                            || (("Pin::new" == *pre)
3754                                && ((sym::as_ref == item_name.name) || !unpin))
3755                            || inputs_len.is_some_and(|inputs_len| {
3756                                pick.item.is_fn()
3757                                    && self
3758                                        .tcx
3759                                        .fn_sig(pick.item.def_id)
3760                                        .skip_binder()
3761                                        .skip_binder()
3762                                        .inputs()
3763                                        .len()
3764                                        != inputs_len
3765                            });
3766                        // Make sure the method is defined for the *actual* receiver: we don't
3767                        // want to treat `Box<Self>` as a receiver if it only works because of
3768                        // an autoderef to `&self`
3769                        if pick.autoderefs == 0 && !skip {
3770                            err.span_label(
3771                                pick.item.ident(self.tcx).span,
3772                                format!("the method is available for `{new_rcvr_t}` here"),
3773                            );
3774                            err.multipart_suggestion(
3775                                "consider wrapping the receiver expression with the \
3776                                 appropriate type",
3777                                vec![
3778                                    (rcvr.span.shrink_to_lo(), format!("{pre}({post}")),
3779                                    (rcvr.span.shrink_to_hi(), ")".to_string()),
3780                                ],
3781                                Applicability::MaybeIncorrect,
3782                            );
3783                            // We don't care about the other suggestions.
3784                            alt_rcvr_sugg = true;
3785                        }
3786                    }
3787                }
3788                // We special case the situation where `Pin::new` wouldn't work, and instead
3789                // suggest using the `pin!()` macro instead.
3790                if let Some(new_rcvr_t) = Ty::new_lang_item(self.tcx, *rcvr_ty, LangItem::Pin)
3791                    // We didn't find an alternative receiver for the method.
3792                    && !alt_rcvr_sugg
3793                    // `T: !Unpin`
3794                    && !unpin
3795                    // Either `Pin::as_ref` or `Pin::as_mut`.
3796                    && let Some(pin_call) = pin_call
3797                    // Search for `item_name` as a method accessible on `Pin<T>`.
3798                    && let Ok(pick) = self.lookup_probe_for_diagnostic(
3799                        item_name,
3800                        new_rcvr_t,
3801                        rcvr,
3802                        ProbeScope::AllTraits,
3803                        return_type,
3804                    )
3805                    // We skip some common traits that we don't want to consider because autoderefs
3806                    // would take care of them.
3807                    && !skippable.contains(&Some(pick.item.container_id(self.tcx)))
3808                    // Do not suggest pinning when the method is directly on `Pin`.
3809                    && pick.item.impl_container(self.tcx).is_none_or(|did| {
3810                        match self.tcx.type_of(did).skip_binder().kind() {
3811                            ty::Adt(def, _) => Some(def.did()) != self.tcx.lang_items().pin_type(),
3812                            _ => true,
3813                        }
3814                    })
3815                    // We don't want to go through derefs.
3816                    && pick.autoderefs == 0
3817                    // Check that the method of the same name that was found on the new `Pin<T>`
3818                    // receiver has the same number of arguments that appear in the user's code.
3819                    && 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)
3820                {
3821                    let indent = self
3822                        .tcx
3823                        .sess
3824                        .source_map()
3825                        .indentation_before(rcvr.span)
3826                        .unwrap_or_else(|| " ".to_string());
3827                    let mut expr = rcvr;
3828                    while let Node::Expr(call_expr) = self.tcx.parent_hir_node(expr.hir_id)
3829                        && let hir::ExprKind::MethodCall(hir::PathSegment { .. }, ..) =
3830                            call_expr.kind
3831                    {
3832                        expr = call_expr;
3833                    }
3834                    match self.tcx.parent_hir_node(expr.hir_id) {
3835                        Node::LetStmt(stmt)
3836                            if let Some(init) = stmt.init
3837                                && let Ok(code) =
3838                                    self.tcx.sess.source_map().span_to_snippet(rcvr.span) =>
3839                        {
3840                            // We need to take care to account for the existing binding when we
3841                            // suggest the code.
3842                            err.multipart_suggestion(
3843                                "consider pinning the expression",
3844                                vec![
3845                                    (
3846                                        stmt.span.shrink_to_lo(),
3847                                        format!(
3848                                            "let mut pinned = std::pin::pin!({code});\n{indent}"
3849                                        ),
3850                                    ),
3851                                    (
3852                                        init.span.until(rcvr.span.shrink_to_hi()),
3853                                        format!("pinned.{pin_call}()"),
3854                                    ),
3855                                ],
3856                                Applicability::MaybeIncorrect,
3857                            );
3858                        }
3859                        Node::Block(_) | Node::Stmt(_) => {
3860                            // There's no binding, so we can provide a slightly nicer looking
3861                            // suggestion.
3862                            err.multipart_suggestion(
3863                                "consider pinning the expression",
3864                                vec![
3865                                    (
3866                                        rcvr.span.shrink_to_lo(),
3867                                        "let mut pinned = std::pin::pin!(".to_string(),
3868                                    ),
3869                                    (
3870                                        rcvr.span.shrink_to_hi(),
3871                                        format!(");\n{indent}pinned.{pin_call}()"),
3872                                    ),
3873                                ],
3874                                Applicability::MaybeIncorrect,
3875                            );
3876                        }
3877                        _ => {
3878                            // We don't quite know what the users' code looks like, so we don't
3879                            // provide a pinning suggestion.
3880                            err.span_help(
3881                                rcvr.span,
3882                                "consider pinning the expression with `std::pin::pin!()` and \
3883                                 assigning that to a new binding",
3884                            );
3885                        }
3886                    }
3887                    // We don't care about the other suggestions.
3888                    alt_rcvr_sugg = true;
3889                }
3890            }
3891        }
3892
3893        if let SelfSource::QPath(ty) = source
3894            && !valid_out_of_scope_traits.is_empty()
3895            && let hir::TyKind::Path(path) = ty.kind
3896            && let hir::QPath::Resolved(..) = path
3897            && let Some(assoc) = self
3898                .tcx
3899                .associated_items(valid_out_of_scope_traits[0])
3900                .filter_by_name_unhygienic(item_name.name)
3901                .next()
3902        {
3903            // See if the `Type::function(val)` where `function` wasn't found corresponds to a
3904            // `Trait` that is imported directly, but `Type` came from a different version of the
3905            // same crate.
3906
3907            let rcvr_ty = self.node_ty_opt(ty.hir_id);
3908            trait_in_other_version_found = self
3909                .detect_and_explain_multiple_crate_versions_of_trait_item(
3910                    err,
3911                    assoc.def_id,
3912                    ty.hir_id,
3913                    rcvr_ty,
3914                );
3915        }
3916        if !trait_in_other_version_found
3917            && self.suggest_valid_traits(err, item_name, valid_out_of_scope_traits, true)
3918        {
3919            return;
3920        }
3921
3922        let type_is_local = self.type_derefs_to_local(span, rcvr_ty, source);
3923
3924        let mut arbitrary_rcvr = vec![];
3925        // There are no traits implemented, so lets suggest some traits to
3926        // implement, by finding ones that have the item name, and are
3927        // legal to implement.
3928        let mut candidates = all_traits(self.tcx)
3929            .into_iter()
3930            // Don't issue suggestions for unstable traits since they're
3931            // unlikely to be implementable anyway
3932            .filter(|info| match self.tcx.lookup_stability(info.def_id) {
3933                Some(attr) => attr.level.is_stable(),
3934                None => true,
3935            })
3936            .filter(|info| {
3937                // Static candidates are already implemented, and known not to work
3938                // Do not suggest them again
3939                static_candidates.iter().all(|sc| match *sc {
3940                    CandidateSource::Trait(def_id) => def_id != info.def_id,
3941                    CandidateSource::Impl(def_id) => {
3942                        self.tcx.impl_opt_trait_id(def_id) != Some(info.def_id)
3943                    }
3944                })
3945            })
3946            .filter(|info| {
3947                // We approximate the coherence rules to only suggest
3948                // traits that are legal to implement by requiring that
3949                // either the type or trait is local. Multi-dispatch means
3950                // this isn't perfect (that is, there are cases when
3951                // implementing a trait would be legal but is rejected
3952                // here).
3953                (type_is_local || info.def_id.is_local())
3954                    && !self.tcx.trait_is_auto(info.def_id)
3955                    && self
3956                        .associated_value(info.def_id, item_name)
3957                        .filter(|item| {
3958                            if item.is_fn() {
3959                                let id = item
3960                                    .def_id
3961                                    .as_local()
3962                                    .map(|def_id| self.tcx.hir_node_by_def_id(def_id));
3963                                if let Some(hir::Node::TraitItem(hir::TraitItem {
3964                                    kind: hir::TraitItemKind::Fn(fn_sig, method),
3965                                    ..
3966                                })) = id
3967                                {
3968                                    let self_first_arg = match method {
3969                                        hir::TraitFn::Required([ident, ..]) => {
3970                                            matches!(ident, Some(Ident { name: kw::SelfLower, .. }))
3971                                        }
3972                                        hir::TraitFn::Provided(body_id) => {
3973                                            self.tcx.hir_body(*body_id).params.first().is_some_and(
3974                                                |param| {
3975                                                    matches!(
3976                                                        param.pat.kind,
3977                                                        hir::PatKind::Binding(_, _, ident, _)
3978                                                            if ident.name == kw::SelfLower
3979                                                    )
3980                                                },
3981                                            )
3982                                        }
3983                                        _ => false,
3984                                    };
3985
3986                                    if !fn_sig.decl.implicit_self.has_implicit_self()
3987                                        && self_first_arg
3988                                    {
3989                                        if let Some(ty) = fn_sig.decl.inputs.get(0) {
3990                                            arbitrary_rcvr.push(ty.span);
3991                                        }
3992                                        return false;
3993                                    }
3994                                }
3995                            }
3996                            // We only want to suggest public or local traits (#45781).
3997                            item.visibility(self.tcx).is_public() || info.def_id.is_local()
3998                        })
3999                        .is_some()
4000            })
4001            .collect::<Vec<_>>();
4002        for span in &arbitrary_rcvr {
4003            err.span_label(
4004                *span,
4005                "the method might not be found because of this arbitrary self type",
4006            );
4007        }
4008        if alt_rcvr_sugg {
4009            return;
4010        }
4011
4012        if !candidates.is_empty() {
4013            // Sort local crate results before others
4014            candidates
4015                .sort_by_key(|&info| (!info.def_id.is_local(), self.tcx.def_path_str(info.def_id)));
4016            candidates.dedup();
4017
4018            let param_type = match *rcvr_ty.kind() {
4019                ty::Param(param) => Some(param),
4020                ty::Ref(_, ty, _) => match *ty.kind() {
4021                    ty::Param(param) => Some(param),
4022                    _ => None,
4023                },
4024                _ => None,
4025            };
4026            if !trait_missing_method {
4027                err.help(if param_type.is_some() {
4028                    "items from traits can only be used if the type parameter is bounded by the trait"
4029                } else {
4030                    "items from traits can only be used if the trait is implemented and in scope"
4031                });
4032            }
4033
4034            let candidates_len = candidates.len();
4035            let message = |action| {
4036                format!(
4037                    "the following {traits_define} an item `{name}`, perhaps you need to {action} \
4038                     {one_of_them}:",
4039                    traits_define =
4040                        if candidates_len == 1 { "trait defines" } else { "traits define" },
4041                    action = action,
4042                    one_of_them = if candidates_len == 1 { "it" } else { "one of them" },
4043                    name = item_name,
4044                )
4045            };
4046            // Obtain the span for `param` and use it for a structured suggestion.
4047            if let Some(param) = param_type {
4048                let generics = self.tcx.generics_of(self.body_id.to_def_id());
4049                let type_param = generics.type_param(param, self.tcx);
4050                let tcx = self.tcx;
4051                if let Some(def_id) = type_param.def_id.as_local() {
4052                    let id = tcx.local_def_id_to_hir_id(def_id);
4053                    // Get the `hir::Param` to verify whether it already has any bounds.
4054                    // We do this to avoid suggesting code that ends up as `T: FooBar`,
4055                    // instead we suggest `T: Foo + Bar` in that case.
4056                    match tcx.hir_node(id) {
4057                        Node::GenericParam(param) => {
4058                            enum Introducer {
4059                                Plus,
4060                                Colon,
4061                                Nothing,
4062                            }
4063                            let hir_generics = tcx.hir_get_generics(id.owner.def_id).unwrap();
4064                            let trait_def_ids: DefIdSet = hir_generics
4065                                .bounds_for_param(def_id)
4066                                .flat_map(|bp| bp.bounds.iter())
4067                                .filter_map(|bound| bound.trait_ref()?.trait_def_id())
4068                                .collect();
4069                            if candidates.iter().any(|t| trait_def_ids.contains(&t.def_id)) {
4070                                return;
4071                            }
4072                            let msg = message(format!(
4073                                "restrict type parameter `{}` with",
4074                                param.name.ident(),
4075                            ));
4076                            let bounds_span = hir_generics.bounds_span_for_suggestions(def_id);
4077                            let mut applicability = Applicability::MaybeIncorrect;
4078                            // Format the path of each suggested candidate, providing placeholders
4079                            // for any generic arguments without defaults.
4080                            let candidate_strs: Vec<_> = candidates
4081                                .iter()
4082                                .map(|cand| {
4083                                    let cand_path = tcx.def_path_str(cand.def_id);
4084                                    let cand_params = &tcx.generics_of(cand.def_id).own_params;
4085                                    let cand_args: String = cand_params
4086                                        .iter()
4087                                        .skip(1)
4088                                        .filter_map(|param| match param.kind {
4089                                            ty::GenericParamDefKind::Type {
4090                                                has_default: true,
4091                                                ..
4092                                            }
4093                                            | ty::GenericParamDefKind::Const {
4094                                                has_default: true,
4095                                                ..
4096                                            } => None,
4097                                            _ => Some(param.name.as_str()),
4098                                        })
4099                                        .intersperse(", ")
4100                                        .collect();
4101                                    if cand_args.is_empty() {
4102                                        cand_path
4103                                    } else {
4104                                        applicability = Applicability::HasPlaceholders;
4105                                        format!("{cand_path}</* {cand_args} */>")
4106                                    }
4107                                })
4108                                .collect();
4109
4110                            if rcvr_ty.is_ref()
4111                                && param.is_impl_trait()
4112                                && let Some((bounds_span, _)) = bounds_span
4113                            {
4114                                err.multipart_suggestions(
4115                                    msg,
4116                                    candidate_strs.iter().map(|cand| {
4117                                        vec![
4118                                            (param.span.shrink_to_lo(), "(".to_string()),
4119                                            (bounds_span, format!(" + {cand})")),
4120                                        ]
4121                                    }),
4122                                    applicability,
4123                                );
4124                                return;
4125                            }
4126
4127                            let (sp, introducer, open_paren_sp) =
4128                                if let Some((span, open_paren_sp)) = bounds_span {
4129                                    (span, Introducer::Plus, open_paren_sp)
4130                                } else if let Some(colon_span) = param.colon_span {
4131                                    (colon_span.shrink_to_hi(), Introducer::Nothing, None)
4132                                } else if param.is_impl_trait() {
4133                                    (param.span.shrink_to_hi(), Introducer::Plus, None)
4134                                } else {
4135                                    (param.span.shrink_to_hi(), Introducer::Colon, None)
4136                                };
4137
4138                            let all_suggs = candidate_strs.iter().map(|cand| {
4139                                let suggestion = format!(
4140                                    "{} {cand}",
4141                                    match introducer {
4142                                        Introducer::Plus => " +",
4143                                        Introducer::Colon => ":",
4144                                        Introducer::Nothing => "",
4145                                    },
4146                                );
4147
4148                                let mut suggs = vec![];
4149
4150                                if let Some(open_paren_sp) = open_paren_sp {
4151                                    suggs.push((open_paren_sp, "(".to_string()));
4152                                    suggs.push((sp, format!("){suggestion}")));
4153                                } else {
4154                                    suggs.push((sp, suggestion));
4155                                }
4156
4157                                suggs
4158                            });
4159
4160                            err.multipart_suggestions(msg, all_suggs, applicability);
4161
4162                            return;
4163                        }
4164                        Node::Item(hir::Item {
4165                            kind: hir::ItemKind::Trait(_, _, _, ident, _, bounds, _),
4166                            ..
4167                        }) => {
4168                            let (sp, sep, article) = if bounds.is_empty() {
4169                                (ident.span.shrink_to_hi(), ":", "a")
4170                            } else {
4171                                (bounds.last().unwrap().span().shrink_to_hi(), " +", "another")
4172                            };
4173                            err.span_suggestions(
4174                                sp,
4175                                message(format!("add {article} supertrait for")),
4176                                candidates
4177                                    .iter()
4178                                    .map(|t| format!("{} {}", sep, tcx.def_path_str(t.def_id),)),
4179                                Applicability::MaybeIncorrect,
4180                            );
4181                            return;
4182                        }
4183                        _ => {}
4184                    }
4185                }
4186            }
4187
4188            let (potential_candidates, explicitly_negative) = if param_type.is_some() {
4189                // FIXME: Even though negative bounds are not implemented, we could maybe handle
4190                // cases where a positive bound implies a negative impl.
4191                (candidates, Vec::new())
4192            } else if let Some(simp_rcvr_ty) =
4193                simplify_type(self.tcx, rcvr_ty, TreatParams::AsRigid)
4194            {
4195                let mut potential_candidates = Vec::new();
4196                let mut explicitly_negative = Vec::new();
4197                for candidate in candidates {
4198                    // Check if there's a negative impl of `candidate` for `rcvr_ty`
4199                    if self
4200                        .tcx
4201                        .all_impls(candidate.def_id)
4202                        .map(|imp_did| self.tcx.impl_trait_header(imp_did))
4203                        .filter(|header| header.polarity != ty::ImplPolarity::Positive)
4204                        .any(|header| {
4205                            let imp = header.trait_ref.instantiate_identity();
4206                            let imp_simp =
4207                                simplify_type(self.tcx, imp.self_ty(), TreatParams::AsRigid);
4208                            imp_simp.is_some_and(|s| s == simp_rcvr_ty)
4209                        })
4210                    {
4211                        explicitly_negative.push(candidate);
4212                    } else {
4213                        potential_candidates.push(candidate);
4214                    }
4215                }
4216                (potential_candidates, explicitly_negative)
4217            } else {
4218                // We don't know enough about `recv_ty` to make proper suggestions.
4219                (candidates, Vec::new())
4220            };
4221
4222            let impls_trait = |def_id: DefId| {
4223                let args = ty::GenericArgs::for_item(self.tcx, def_id, |param, _| {
4224                    if param.index == 0 {
4225                        rcvr_ty.into()
4226                    } else {
4227                        self.infcx.var_for_def(span, param)
4228                    }
4229                });
4230                self.infcx
4231                    .type_implements_trait(def_id, args, self.param_env)
4232                    .must_apply_modulo_regions()
4233                    && param_type.is_none()
4234            };
4235            match &potential_candidates[..] {
4236                [] => {}
4237                [trait_info] if trait_info.def_id.is_local() => {
4238                    if impls_trait(trait_info.def_id) {
4239                        self.suggest_valid_traits(err, item_name, vec![trait_info.def_id], false);
4240                    } else {
4241                        err.subdiagnostic(CandidateTraitNote {
4242                            span: self.tcx.def_span(trait_info.def_id),
4243                            trait_name: self.tcx.def_path_str(trait_info.def_id),
4244                            item_name,
4245                            action_or_ty: if trait_missing_method {
4246                                "NONE".to_string()
4247                            } else {
4248                                param_type.map_or_else(
4249                                    || "implement".to_string(), // FIXME: it might only need to be imported into scope, not implemented.
4250                                    |p| p.to_string(),
4251                                )
4252                            },
4253                        });
4254                    }
4255                }
4256                trait_infos => {
4257                    let mut msg = message(param_type.map_or_else(
4258                        || "implement".to_string(), // FIXME: it might only need to be imported into scope, not implemented.
4259                        |param| format!("restrict type parameter `{param}` with"),
4260                    ));
4261                    for (i, trait_info) in trait_infos.iter().enumerate() {
4262                        if impls_trait(trait_info.def_id) {
4263                            self.suggest_valid_traits(
4264                                err,
4265                                item_name,
4266                                vec![trait_info.def_id],
4267                                false,
4268                            );
4269                        }
4270                        msg.push_str(&format!(
4271                            "\ncandidate #{}: `{}`",
4272                            i + 1,
4273                            self.tcx.def_path_str(trait_info.def_id),
4274                        ));
4275                    }
4276                    err.note(msg);
4277                }
4278            }
4279            match &explicitly_negative[..] {
4280                [] => {}
4281                [trait_info] => {
4282                    let msg = format!(
4283                        "the trait `{}` defines an item `{}`, but is explicitly unimplemented",
4284                        self.tcx.def_path_str(trait_info.def_id),
4285                        item_name
4286                    );
4287                    err.note(msg);
4288                }
4289                trait_infos => {
4290                    let mut msg = format!(
4291                        "the following traits define an item `{item_name}`, but are explicitly unimplemented:"
4292                    );
4293                    for trait_info in trait_infos {
4294                        msg.push_str(&format!("\n{}", self.tcx.def_path_str(trait_info.def_id)));
4295                    }
4296                    err.note(msg);
4297                }
4298            }
4299        }
4300    }
4301
4302    fn detect_and_explain_multiple_crate_versions_of_trait_item(
4303        &self,
4304        err: &mut Diag<'_>,
4305        item_def_id: DefId,
4306        hir_id: hir::HirId,
4307        rcvr_ty: Option<Ty<'tcx>>,
4308    ) -> bool {
4309        let hir_id = self.tcx.parent_hir_id(hir_id);
4310        let Some(traits) = self.tcx.in_scope_traits(hir_id) else { return false };
4311        if traits.is_empty() {
4312            return false;
4313        }
4314        let trait_def_id = self.tcx.parent(item_def_id);
4315        if !self.tcx.is_trait(trait_def_id) {
4316            return false;
4317        }
4318        let hir::Node::Expr(rcvr) = self.tcx.hir_node(hir_id) else {
4319            return false;
4320        };
4321        let trait_ref = ty::TraitRef::new(self.tcx, trait_def_id, rcvr_ty.into_iter());
4322        let trait_pred = ty::Binder::dummy(ty::TraitPredicate {
4323            trait_ref,
4324            polarity: ty::PredicatePolarity::Positive,
4325        });
4326        let obligation = Obligation::new(self.tcx, self.misc(rcvr.span), self.param_env, trait_ref);
4327        self.err_ctxt().note_different_trait_with_same_name(err, &obligation, trait_pred)
4328    }
4329
4330    /// issue #102320, for `unwrap_or` with closure as argument, suggest `unwrap_or_else`
4331    /// FIXME: currently not working for suggesting `map_or_else`, see #102408
4332    pub(crate) fn suggest_else_fn_with_closure(
4333        &self,
4334        err: &mut Diag<'_>,
4335        expr: &hir::Expr<'_>,
4336        found: Ty<'tcx>,
4337        expected: Ty<'tcx>,
4338    ) -> bool {
4339        let Some((_def_id_or_name, output, _inputs)) = self.extract_callable_info(found) else {
4340            return false;
4341        };
4342
4343        if !self.may_coerce(output, expected) {
4344            return false;
4345        }
4346
4347        if let Node::Expr(call_expr) = self.tcx.parent_hir_node(expr.hir_id)
4348            && let hir::ExprKind::MethodCall(
4349                hir::PathSegment { ident: method_name, .. },
4350                self_expr,
4351                args,
4352                ..,
4353            ) = call_expr.kind
4354            && let Some(self_ty) = self.typeck_results.borrow().expr_ty_opt(self_expr)
4355        {
4356            let new_name = Ident {
4357                name: Symbol::intern(&format!("{}_else", method_name.as_str())),
4358                span: method_name.span,
4359            };
4360            let probe = self.lookup_probe_for_diagnostic(
4361                new_name,
4362                self_ty,
4363                self_expr,
4364                ProbeScope::TraitsInScope,
4365                Some(expected),
4366            );
4367
4368            // check the method arguments number
4369            if let Ok(pick) = probe
4370                && let fn_sig = self.tcx.fn_sig(pick.item.def_id)
4371                && let fn_args = fn_sig.skip_binder().skip_binder().inputs()
4372                && fn_args.len() == args.len() + 1
4373            {
4374                err.span_suggestion_verbose(
4375                    method_name.span.shrink_to_hi(),
4376                    format!("try calling `{}` instead", new_name.name.as_str()),
4377                    "_else",
4378                    Applicability::MaybeIncorrect,
4379                );
4380                return true;
4381            }
4382        }
4383        false
4384    }
4385
4386    /// Checks whether there is a local type somewhere in the chain of
4387    /// autoderefs of `rcvr_ty`.
4388    fn type_derefs_to_local(
4389        &self,
4390        span: Span,
4391        rcvr_ty: Ty<'tcx>,
4392        source: SelfSource<'tcx>,
4393    ) -> bool {
4394        fn is_local(ty: Ty<'_>) -> bool {
4395            match ty.kind() {
4396                ty::Adt(def, _) => def.did().is_local(),
4397                ty::Foreign(did) => did.is_local(),
4398                ty::Dynamic(tr, ..) => tr.principal().is_some_and(|d| d.def_id().is_local()),
4399                ty::Param(_) => true,
4400
4401                // Everything else (primitive types, etc.) is effectively
4402                // non-local (there are "edge" cases, e.g., `(LocalType,)`, but
4403                // the noise from these sort of types is usually just really
4404                // annoying, rather than any sort of help).
4405                _ => false,
4406            }
4407        }
4408
4409        // This occurs for UFCS desugaring of `T::method`, where there is no
4410        // receiver expression for the method call, and thus no autoderef.
4411        if let SelfSource::QPath(_) = source {
4412            return is_local(rcvr_ty);
4413        }
4414
4415        self.autoderef(span, rcvr_ty).silence_errors().any(|(ty, _)| is_local(ty))
4416    }
4417
4418    fn suggest_hashmap_on_unsatisfied_hashset_buildhasher(
4419        &self,
4420        err: &mut Diag<'_>,
4421        pred: &ty::TraitPredicate<'_>,
4422        adt: ty::AdtDef<'_>,
4423    ) -> bool {
4424        if self.tcx.is_diagnostic_item(sym::HashSet, adt.did())
4425            && self.tcx.is_diagnostic_item(sym::BuildHasher, pred.def_id())
4426        {
4427            err.help("you might have intended to use a HashMap instead");
4428            true
4429        } else {
4430            false
4431        }
4432    }
4433}
4434
4435#[derive(Copy, Clone, Debug)]
4436enum SelfSource<'a> {
4437    QPath(&'a hir::Ty<'a>),
4438    MethodCall(&'a hir::Expr<'a> /* rcvr */),
4439}
4440
4441#[derive(Copy, Clone, PartialEq, Eq)]
4442pub(crate) struct TraitInfo {
4443    pub def_id: DefId,
4444}
4445
4446/// Retrieves all traits in this crate and any dependent crates,
4447/// and wraps them into `TraitInfo` for custom sorting.
4448pub(crate) fn all_traits(tcx: TyCtxt<'_>) -> Vec<TraitInfo> {
4449    tcx.all_traits_including_private().map(|def_id| TraitInfo { def_id }).collect()
4450}
4451
4452fn print_disambiguation_help<'tcx>(
4453    tcx: TyCtxt<'tcx>,
4454    err: &mut Diag<'_>,
4455    source: SelfSource<'tcx>,
4456    args: Option<&'tcx [hir::Expr<'tcx>]>,
4457    trait_ref: ty::TraitRef<'tcx>,
4458    candidate_idx: Option<usize>,
4459    span: Span,
4460    item: ty::AssocItem,
4461) -> Option<String> {
4462    let trait_impl_type = trait_ref.self_ty().peel_refs();
4463    let trait_ref = if item.is_method() {
4464        trait_ref.print_only_trait_name().to_string()
4465    } else {
4466        format!("<{} as {}>", trait_ref.args[0], trait_ref.print_only_trait_name())
4467    };
4468    Some(
4469        if item.is_fn()
4470            && let SelfSource::MethodCall(receiver) = source
4471            && let Some(args) = args
4472        {
4473            let def_kind_descr = tcx.def_kind_descr(item.as_def_kind(), item.def_id);
4474            let item_name = item.ident(tcx);
4475            let first_input =
4476                tcx.fn_sig(item.def_id).instantiate_identity().skip_binder().inputs().get(0);
4477            let (first_arg_type, rcvr_ref) = (
4478                first_input.map(|first| first.peel_refs()),
4479                first_input
4480                    .and_then(|ty| ty.ref_mutability())
4481                    .map_or("", |mutbl| mutbl.ref_prefix_str()),
4482            );
4483
4484            // 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.
4485            let args = if let Some(first_arg_type) = first_arg_type
4486                && (first_arg_type == tcx.types.self_param
4487                    || first_arg_type == trait_impl_type
4488                    || item.is_method())
4489            {
4490                Some(receiver)
4491            } else {
4492                None
4493            }
4494            .into_iter()
4495            .chain(args)
4496            .map(|arg| {
4497                tcx.sess.source_map().span_to_snippet(arg.span).unwrap_or_else(|_| "_".to_owned())
4498            })
4499            .collect::<Vec<_>>()
4500            .join(", ");
4501
4502            let args = format!("({}{})", rcvr_ref, args);
4503            err.span_suggestion_verbose(
4504                span,
4505                format!(
4506                    "disambiguate the {def_kind_descr} for {}",
4507                    if let Some(candidate) = candidate_idx {
4508                        format!("candidate #{candidate}")
4509                    } else {
4510                        "the candidate".to_string()
4511                    },
4512                ),
4513                format!("{trait_ref}::{item_name}{args}"),
4514                Applicability::HasPlaceholders,
4515            );
4516            return None;
4517        } else {
4518            format!("{trait_ref}::")
4519        },
4520    )
4521}