rustc_hir_typeck/method/
suggest.rs

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