Skip to main content

rustc_hir_typeck/method/
suggest.rs

1//! Give useful errors and suggestions to users when an item can't be
2//! found or is otherwise invalid.
3
4// ignore-tidy-filelength
5
6use core::ops::ControlFlow;
7use std::borrow::Cow;
8use std::path::PathBuf;
9
10use hir::Expr;
11use rustc_ast::ast::Mutability;
12use rustc_data_structures::fx::{FxIndexMap, FxIndexSet};
13use rustc_data_structures::sorted_map::SortedMap;
14use rustc_data_structures::unord::UnordSet;
15use rustc_errors::codes::*;
16use rustc_errors::{
17    Applicability, Diag, MultiSpan, StashKey, StringPart, listify, pluralize, struct_span_code_err,
18};
19use rustc_hir::attrs::diagnostic::CustomDiagnostic;
20use rustc_hir::def::{CtorKind, DefKind, Res};
21use rustc_hir::def_id::DefId;
22use rustc_hir::intravisit::{self, Visitor};
23use rustc_hir::lang_items::LangItem;
24use rustc_hir::{
25    self as hir, ExprKind, HirId, Node, PathSegment, QPath, find_attr, is_range_literal,
26};
27use rustc_infer::infer::{BoundRegionConversionTime, RegionVariableOrigin};
28use rustc_middle::bug;
29use rustc_middle::ty::fast_reject::{DeepRejectCtxt, TreatParams, simplify_type};
30use rustc_middle::ty::print::{
31    PrintTraitRefExt as _, with_crate_prefix, with_forced_trimmed_paths,
32    with_no_visible_paths_if_doc_hidden,
33};
34use rustc_middle::ty::{self, GenericArgKind, IsSuggestable, Ty, TyCtxt, TypeVisitableExt};
35use rustc_span::def_id::DefIdSet;
36use rustc_span::{
37    DUMMY_SP, ErrorGuaranteed, ExpnKind, FileName, Ident, MacroKind, Span, Symbol, edit_distance,
38    kw, sym,
39};
40use rustc_trait_selection::error_reporting::traits::DefIdOrName;
41use rustc_trait_selection::infer::InferCtxtExt;
42use rustc_trait_selection::traits::query::evaluate_obligation::InferCtxtExt as _;
43use rustc_trait_selection::traits::{
44    FulfillmentError, Obligation, ObligationCauseCode, supertraits,
45};
46use tracing::{debug, info, instrument};
47
48use super::probe::{AutorefOrPtrAdjustment, IsSuggestion, Mode, ProbeScope};
49use super::{CandidateSource, MethodError, NoMatchData};
50use crate::errors::{self, CandidateTraitNote, NoAssociatedItem};
51use crate::method::probe::UnsatisfiedPredicates;
52use crate::{Expectation, FnCtxt};
53
54/// Tracks trait bounds and detects duplicates between ref and non-ref versions of self types.
55/// This is used to condense error messages when the same trait bound appears for both
56/// `T` and `&T` (or `&mut T`).
57struct TraitBoundDuplicateTracker {
58    trait_def_ids: FxIndexSet<DefId>,
59    seen_ref: FxIndexSet<DefId>,
60    seen_non_ref: FxIndexSet<DefId>,
61    has_ref_dupes: bool,
62}
63
64impl TraitBoundDuplicateTracker {
65    fn new() -> Self {
66        Self {
67            trait_def_ids: FxIndexSet::default(),
68            seen_ref: FxIndexSet::default(),
69            seen_non_ref: FxIndexSet::default(),
70            has_ref_dupes: false,
71        }
72    }
73
74    /// Track a trait bound. `is_ref` indicates whether the self type is a reference.
75    fn track(&mut self, def_id: DefId, is_ref: bool) {
76        self.trait_def_ids.insert(def_id);
77        if is_ref {
78            if self.seen_non_ref.contains(&def_id) {
79                self.has_ref_dupes = true;
80            }
81            self.seen_ref.insert(def_id);
82        } else {
83            if self.seen_ref.contains(&def_id) {
84                self.has_ref_dupes = true;
85            }
86            self.seen_non_ref.insert(def_id);
87        }
88    }
89
90    fn has_ref_dupes(&self) -> bool {
91        self.has_ref_dupes
92    }
93
94    fn into_trait_def_ids(self) -> FxIndexSet<DefId> {
95        self.trait_def_ids
96    }
97}
98
99impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
100    fn is_slice_ty(&self, ty: Ty<'tcx>, span: Span) -> bool {
101        self.autoderef(span, ty)
102            .silence_errors()
103            .any(|(ty, _)| #[allow(non_exhaustive_omitted_patterns)] match ty.kind() {
    ty::Slice(..) | ty::Array(..) => true,
    _ => false,
}matches!(ty.kind(), ty::Slice(..) | ty::Array(..)))
104    }
105
106    fn impl_into_iterator_should_be_iterator(
107        &self,
108        ty: Ty<'tcx>,
109        span: Span,
110        unsatisfied_predicates: &UnsatisfiedPredicates<'tcx>,
111    ) -> bool {
112        fn predicate_bounds_generic_param<'tcx>(
113            predicate: ty::Predicate<'_>,
114            generics: &'tcx ty::Generics,
115            generic_param: &ty::GenericParamDef,
116            tcx: TyCtxt<'tcx>,
117        ) -> bool {
118            if let ty::PredicateKind::Clause(ty::ClauseKind::Trait(trait_pred)) =
119                predicate.kind().as_ref().skip_binder()
120            {
121                let ty::TraitPredicate { trait_ref: ty::TraitRef { args, .. }, .. } = trait_pred;
122                if args.is_empty() {
123                    return false;
124                }
125                let Some(arg_ty) = args[0].as_type() else {
126                    return false;
127                };
128                let ty::Param(param) = *arg_ty.kind() else {
129                    return false;
130                };
131                // Is `generic_param` the same as the arg for this trait predicate?
132                generic_param.index == generics.type_param(param, tcx).index
133            } else {
134                false
135            }
136        }
137
138        let is_iterator_predicate = |predicate: ty::Predicate<'tcx>| -> bool {
139            if let ty::PredicateKind::Clause(ty::ClauseKind::Trait(trait_pred)) =
140                predicate.kind().as_ref().skip_binder()
141            {
142                self.tcx.is_diagnostic_item(sym::Iterator, trait_pred.trait_ref.def_id)
143                    // ignore unsatisfied predicates generated from trying to auto-ref ty (#127511)
144                    && trait_pred.trait_ref.self_ty() == ty
145            } else {
146                false
147            }
148        };
149
150        // Does the `ty` implement `IntoIterator`?
151        let Some(into_iterator_trait) = self.tcx.get_diagnostic_item(sym::IntoIterator) else {
152            return false;
153        };
154        let trait_ref = ty::TraitRef::new(self.tcx, into_iterator_trait, [ty]);
155        let obligation = Obligation::new(self.tcx, self.misc(span), self.param_env, trait_ref);
156        if !self.predicate_must_hold_modulo_regions(&obligation) {
157            return false;
158        }
159
160        match *ty.peel_refs().kind() {
161            ty::Param(param) => {
162                let generics = self.tcx.generics_of(self.body_id);
163                let generic_param = generics.type_param(param, self.tcx);
164                for unsatisfied in unsatisfied_predicates.iter() {
165                    // The parameter implements `IntoIterator`
166                    // but it has called a method that requires it to implement `Iterator`
167                    if predicate_bounds_generic_param(
168                        unsatisfied.0,
169                        generics,
170                        generic_param,
171                        self.tcx,
172                    ) && is_iterator_predicate(unsatisfied.0)
173                    {
174                        return true;
175                    }
176                }
177            }
178            ty::Slice(..)
179            | ty::Adt(..)
180            | ty::Alias(ty::AliasTy { kind: ty::Opaque { .. }, .. }) => {
181                for unsatisfied in unsatisfied_predicates.iter() {
182                    if is_iterator_predicate(unsatisfied.0) {
183                        return true;
184                    }
185                }
186            }
187            _ => return false,
188        }
189        false
190    }
191
192    #[allow(clippy :: suspicious_else_formatting)]
{
    let __tracing_attr_span;
    let __tracing_attr_guard;
    if ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() ||
            { false } {
        __tracing_attr_span =
            {
                use ::tracing::__macro_support::Callsite as _;
                static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                    {
                        static META: ::tracing::Metadata<'static> =
                            {
                                ::tracing_core::metadata::Metadata::new("report_method_error",
                                    "rustc_hir_typeck::method::suggest",
                                    ::tracing::Level::DEBUG,
                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_typeck/src/method/suggest.rs"),
                                    ::tracing_core::__macro_support::Option::Some(192u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::method::suggest"),
                                    ::tracing_core::field::FieldSet::new(&["call_id", "rcvr_ty",
                                                    "error", "expected", "trait_missing_method"],
                                        ::tracing_core::callsite::Identifier(&__CALLSITE)),
                                    ::tracing::metadata::Kind::SPAN)
                            };
                        ::tracing::callsite::DefaultCallsite::new(&META)
                    };
                let mut interest = ::tracing::subscriber::Interest::never();
                if ::tracing::Level::DEBUG <=
                                    ::tracing::level_filters::STATIC_MAX_LEVEL &&
                                ::tracing::Level::DEBUG <=
                                    ::tracing::level_filters::LevelFilter::current() &&
                            { interest = __CALLSITE.interest(); !interest.is_never() }
                        &&
                        ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                            interest) {
                    let meta = __CALLSITE.metadata();
                    ::tracing::Span::new(meta,
                        &{
                                #[allow(unused_imports)]
                                use ::tracing::field::{debug, display, Value};
                                let mut iter = meta.fields().iter();
                                meta.fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                    ::tracing::__macro_support::Option::Some(&::tracing::field::debug(&call_id)
                                                            as &dyn Value)),
                                                (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                    ::tracing::__macro_support::Option::Some(&::tracing::field::debug(&rcvr_ty)
                                                            as &dyn Value)),
                                                (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                    ::tracing::__macro_support::Option::Some(&::tracing::field::debug(&error)
                                                            as &dyn Value)),
                                                (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                    ::tracing::__macro_support::Option::Some(&::tracing::field::debug(&expected)
                                                            as &dyn Value)),
                                                (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                    ::tracing::__macro_support::Option::Some(&trait_missing_method
                                                            as &dyn Value))])
                            })
                } else {
                    let span =
                        ::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
                    {};
                    span
                }
            };
        __tracing_attr_guard = __tracing_attr_span.enter();
    }

    #[warn(clippy :: suspicious_else_formatting)]
    {

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