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