]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_hir_typeck/src/method/suggest.rs
Rollup merge of #106533 - TaKO8Ki:use-smaller-span-for-missing-lifetime/generic-args...
[rust.git] / compiler / rustc_hir_typeck / src / 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 use crate::errors;
5 use crate::FnCtxt;
6 use rustc_ast::ast::Mutability;
7 use rustc_data_structures::fx::{FxHashMap, FxHashSet};
8 use rustc_errors::StashKey;
9 use rustc_errors::{
10     pluralize, struct_span_err, Applicability, Diagnostic, DiagnosticBuilder, ErrorGuaranteed,
11     MultiSpan,
12 };
13 use rustc_hir as hir;
14 use rustc_hir::def::DefKind;
15 use rustc_hir::def_id::DefId;
16 use rustc_hir::lang_items::LangItem;
17 use rustc_hir::PatKind::Binding;
18 use rustc_hir::PathSegment;
19 use rustc_hir::{ExprKind, Node, QPath};
20 use rustc_infer::infer::{
21     type_variable::{TypeVariableOrigin, TypeVariableOriginKind},
22     RegionVariableOrigin,
23 };
24 use rustc_middle::infer::unify_key::{ConstVariableOrigin, ConstVariableOriginKind};
25 use rustc_middle::traits::util::supertraits;
26 use rustc_middle::ty::fast_reject::DeepRejectCtxt;
27 use rustc_middle::ty::fast_reject::{simplify_type, TreatParams};
28 use rustc_middle::ty::print::{with_crate_prefix, with_forced_trimmed_paths};
29 use rustc_middle::ty::{self, DefIdTree, GenericArgKind, Ty, TyCtxt, TypeVisitable};
30 use rustc_middle::ty::{IsSuggestable, ToPolyTraitRef};
31 use rustc_span::symbol::{kw, sym, Ident};
32 use rustc_span::Symbol;
33 use rustc_span::{lev_distance, source_map, ExpnKind, FileName, MacroKind, Span};
34 use rustc_trait_selection::traits::error_reporting::on_unimplemented::OnUnimplementedNote;
35 use rustc_trait_selection::traits::error_reporting::on_unimplemented::TypeErrCtxtExt as _;
36 use rustc_trait_selection::traits::query::evaluate_obligation::InferCtxtExt as _;
37 use rustc_trait_selection::traits::{
38     FulfillmentError, Obligation, ObligationCause, ObligationCauseCode,
39 };
40
41 use super::probe::{AutorefOrPtrAdjustment, IsSuggestion, Mode, ProbeScope};
42 use super::{CandidateSource, MethodError, NoMatchData};
43 use rustc_hir::intravisit::Visitor;
44 use std::cmp::Ordering;
45 use std::iter;
46
47 impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
48     fn is_fn_ty(&self, ty: Ty<'tcx>, span: Span) -> bool {
49         let tcx = self.tcx;
50         match ty.kind() {
51             // Not all of these (e.g., unsafe fns) implement `FnOnce`,
52             // so we look for these beforehand.
53             ty::Closure(..) | ty::FnDef(..) | ty::FnPtr(_) => true,
54             // If it's not a simple function, look for things which implement `FnOnce`.
55             _ => {
56                 let Some(fn_once) = tcx.lang_items().fn_once_trait() else {
57                     return false;
58                 };
59
60                 // This conditional prevents us from asking to call errors and unresolved types.
61                 // It might seem that we can use `predicate_must_hold_modulo_regions`,
62                 // but since a Dummy binder is used to fill in the FnOnce trait's arguments,
63                 // type resolution always gives a "maybe" here.
64                 if self.autoderef(span, ty).any(|(ty, _)| {
65                     info!("check deref {:?} error", ty);
66                     matches!(ty.kind(), ty::Error(_) | ty::Infer(_))
67                 }) {
68                     return false;
69                 }
70
71                 self.autoderef(span, ty).any(|(ty, _)| {
72                     info!("check deref {:?} impl FnOnce", ty);
73                     self.probe(|_| {
74                         let trait_ref = tcx.mk_trait_ref(
75                             fn_once,
76                             [
77                                 ty,
78                                 self.next_ty_var(TypeVariableOrigin {
79                                     kind: TypeVariableOriginKind::MiscVariable,
80                                     span,
81                                 }),
82                             ],
83                         );
84                         let poly_trait_ref = ty::Binder::dummy(trait_ref);
85                         let obligation = Obligation::misc(
86                             tcx,
87                             span,
88                             self.body_id,
89                             self.param_env,
90                             poly_trait_ref.without_const(),
91                         );
92                         self.predicate_may_hold(&obligation)
93                     })
94                 })
95             }
96         }
97     }
98
99     fn is_slice_ty(&self, ty: Ty<'tcx>, span: Span) -> bool {
100         self.autoderef(span, ty).any(|(ty, _)| matches!(ty.kind(), ty::Slice(..) | ty::Array(..)))
101     }
102
103     pub fn report_method_error(
104         &self,
105         span: Span,
106         rcvr_ty: Ty<'tcx>,
107         item_name: Ident,
108         source: SelfSource<'tcx>,
109         error: MethodError<'tcx>,
110         args: Option<(&'tcx hir::Expr<'tcx>, &'tcx [hir::Expr<'tcx>])>,
111     ) -> Option<DiagnosticBuilder<'_, ErrorGuaranteed>> {
112         // Avoid suggestions when we don't know what's going on.
113         if rcvr_ty.references_error() {
114             return None;
115         }
116
117         let sugg_span = if let SelfSource::MethodCall(expr) = source {
118             // Given `foo.bar(baz)`, `expr` is `bar`, but we want to point to the whole thing.
119             self.tcx.hir().expect_expr(self.tcx.hir().parent_id(expr.hir_id)).span
120         } else {
121             span
122         };
123
124         match error {
125             MethodError::NoMatch(mut no_match_data) => {
126                 return self.report_no_match_method_error(
127                     span,
128                     rcvr_ty,
129                     item_name,
130                     source,
131                     args,
132                     sugg_span,
133                     &mut no_match_data,
134                 );
135             }
136
137             MethodError::Ambiguity(mut sources) => {
138                 let mut err = struct_span_err!(
139                     self.sess(),
140                     item_name.span,
141                     E0034,
142                     "multiple applicable items in scope"
143                 );
144                 err.span_label(item_name.span, format!("multiple `{}` found", item_name));
145
146                 self.note_candidates_on_method_error(
147                     rcvr_ty,
148                     item_name,
149                     args,
150                     span,
151                     &mut err,
152                     &mut sources,
153                     Some(sugg_span),
154                 );
155                 err.emit();
156             }
157
158             MethodError::PrivateMatch(kind, def_id, out_of_scope_traits) => {
159                 let kind = kind.descr(def_id);
160                 let mut err = struct_span_err!(
161                     self.tcx.sess,
162                     item_name.span,
163                     E0624,
164                     "{} `{}` is private",
165                     kind,
166                     item_name
167                 );
168                 err.span_label(item_name.span, &format!("private {}", kind));
169                 let sp = self
170                     .tcx
171                     .hir()
172                     .span_if_local(def_id)
173                     .unwrap_or_else(|| self.tcx.def_span(def_id));
174                 err.span_label(sp, &format!("private {} defined here", kind));
175                 self.suggest_valid_traits(&mut err, out_of_scope_traits);
176                 err.emit();
177             }
178
179             MethodError::IllegalSizedBound(candidates, needs_mut, bound_span) => {
180                 let msg = format!("the `{}` method cannot be invoked on a trait object", item_name);
181                 let mut err = self.sess().struct_span_err(span, &msg);
182                 err.span_label(bound_span, "this has a `Sized` requirement");
183                 if !candidates.is_empty() {
184                     let help = format!(
185                         "{an}other candidate{s} {were} found in the following trait{s}, perhaps \
186                          add a `use` for {one_of_them}:",
187                         an = if candidates.len() == 1 { "an" } else { "" },
188                         s = pluralize!(candidates.len()),
189                         were = pluralize!("was", candidates.len()),
190                         one_of_them = if candidates.len() == 1 { "it" } else { "one_of_them" },
191                     );
192                     self.suggest_use_candidates(&mut err, help, candidates);
193                 }
194                 if let ty::Ref(region, t_type, mutability) = rcvr_ty.kind() {
195                     if needs_mut {
196                         let trait_type = self.tcx.mk_ref(
197                             *region,
198                             ty::TypeAndMut { ty: *t_type, mutbl: mutability.invert() },
199                         );
200                         err.note(&format!("you need `{}` instead of `{}`", trait_type, rcvr_ty));
201                     }
202                 }
203                 err.emit();
204             }
205
206             MethodError::BadReturnType => bug!("no return type expectations but got BadReturnType"),
207         }
208         None
209     }
210
211     pub fn report_no_match_method_error(
212         &self,
213         mut span: Span,
214         rcvr_ty: Ty<'tcx>,
215         item_name: Ident,
216         source: SelfSource<'tcx>,
217         args: Option<(&'tcx hir::Expr<'tcx>, &'tcx [hir::Expr<'tcx>])>,
218         sugg_span: Span,
219         no_match_data: &mut NoMatchData<'tcx>,
220     ) -> Option<DiagnosticBuilder<'_, ErrorGuaranteed>> {
221         let mode = no_match_data.mode;
222         let tcx = self.tcx;
223         let rcvr_ty = self.resolve_vars_if_possible(rcvr_ty);
224         let ty_str = with_forced_trimmed_paths!(self.ty_to_string(rcvr_ty));
225         let is_method = mode == Mode::MethodCall;
226         let unsatisfied_predicates = &no_match_data.unsatisfied_predicates;
227         let lev_candidate = no_match_data.lev_candidate;
228         let item_kind = if is_method {
229             "method"
230         } else if rcvr_ty.is_enum() {
231             "variant or associated item"
232         } else {
233             match (item_name.as_str().chars().next(), rcvr_ty.is_fresh_ty()) {
234                 (Some(name), false) if name.is_lowercase() => "function or associated item",
235                 (Some(_), false) => "associated item",
236                 (Some(_), true) | (None, false) => "variant or associated item",
237                 (None, true) => "variant",
238             }
239         };
240
241         if self.suggest_wrapping_range_with_parens(tcx, rcvr_ty, source, span, item_name, &ty_str)
242             || self.suggest_constraining_numerical_ty(
243                 tcx, rcvr_ty, source, span, item_kind, item_name, &ty_str,
244             )
245         {
246             return None;
247         }
248         span = item_name.span;
249
250         // Don't show generic arguments when the method can't be found in any implementation (#81576).
251         let mut ty_str_reported = ty_str.clone();
252         if let ty::Adt(_, generics) = rcvr_ty.kind() {
253             if generics.len() > 0 {
254                 let mut autoderef = self.autoderef(span, rcvr_ty);
255                 let candidate_found = autoderef.any(|(ty, _)| {
256                     if let ty::Adt(adt_def, _) = ty.kind() {
257                         self.tcx
258                             .inherent_impls(adt_def.did())
259                             .iter()
260                             .any(|def_id| self.associated_value(*def_id, item_name).is_some())
261                     } else {
262                         false
263                     }
264                 });
265                 let has_deref = autoderef.step_count() > 0;
266                 if !candidate_found && !has_deref && unsatisfied_predicates.is_empty() {
267                     if let Some((path_string, _)) = ty_str.split_once('<') {
268                         ty_str_reported = path_string.to_string();
269                     }
270                 }
271             }
272         }
273
274         let mut err = struct_span_err!(
275             tcx.sess,
276             span,
277             E0599,
278             "no {} named `{}` found for {} `{}` in the current scope",
279             item_kind,
280             item_name,
281             rcvr_ty.prefix_string(self.tcx),
282             ty_str_reported,
283         );
284         if rcvr_ty.references_error() {
285             err.downgrade_to_delayed_bug();
286         }
287
288         if let Mode::MethodCall = mode && let SelfSource::MethodCall(cal) = source {
289             self.suggest_await_before_method(
290                 &mut err, item_name, rcvr_ty, cal, span,
291             );
292         }
293         if let Some(span) =
294             tcx.resolutions(()).confused_type_with_std_module.get(&span.with_parent(None))
295         {
296             err.span_suggestion(
297                 span.shrink_to_lo(),
298                 "you are looking for the module in `std`, not the primitive type",
299                 "std::",
300                 Applicability::MachineApplicable,
301             );
302         }
303         if let ty::RawPtr(_) = &rcvr_ty.kind() {
304             err.note(
305                 "try using `<*const T>::as_ref()` to get a reference to the \
306                  type behind the pointer: https://doc.rust-lang.org/std/\
307                  primitive.pointer.html#method.as_ref",
308             );
309             err.note(
310                 "using `<*const T>::as_ref()` on a pointer which is unaligned or points \
311                  to invalid or uninitialized memory is undefined behavior",
312             );
313         }
314
315         let ty_span = match rcvr_ty.kind() {
316             ty::Param(param_type) => {
317                 Some(param_type.span_from_generics(self.tcx, self.body_id.owner.to_def_id()))
318             }
319             ty::Adt(def, _) if def.did().is_local() => Some(tcx.def_span(def.did())),
320             _ => None,
321         };
322         if let Some(span) = ty_span {
323             err.span_label(
324                 span,
325                 format!(
326                     "{item_kind} `{item_name}` not found for this {}",
327                     rcvr_ty.prefix_string(self.tcx)
328                 ),
329             );
330         }
331
332         if let SelfSource::MethodCall(rcvr_expr) = source {
333             self.suggest_fn_call(&mut err, rcvr_expr, rcvr_ty, |output_ty| {
334                 let call_expr =
335                     self.tcx.hir().expect_expr(self.tcx.hir().parent_id(rcvr_expr.hir_id));
336                 let probe =
337                     self.lookup_probe(item_name, output_ty, call_expr, ProbeScope::AllTraits);
338                 probe.is_ok()
339             });
340         }
341
342         let mut custom_span_label = false;
343
344         let static_candidates = &mut no_match_data.static_candidates;
345         if !static_candidates.is_empty() {
346             err.note(
347                 "found the following associated functions; to be used as methods, \
348                  functions must have a `self` parameter",
349             );
350             err.span_label(span, "this is an associated function, not a method");
351             custom_span_label = true;
352         }
353         if static_candidates.len() == 1 {
354             self.suggest_associated_call_syntax(
355                 &mut err,
356                 &static_candidates,
357                 rcvr_ty,
358                 source,
359                 item_name,
360                 args,
361                 sugg_span,
362             );
363
364             self.note_candidates_on_method_error(
365                 rcvr_ty,
366                 item_name,
367                 args,
368                 span,
369                 &mut err,
370                 static_candidates,
371                 None,
372             );
373         } else if static_candidates.len() > 1 {
374             self.note_candidates_on_method_error(
375                 rcvr_ty,
376                 item_name,
377                 args,
378                 span,
379                 &mut err,
380                 static_candidates,
381                 Some(sugg_span),
382             );
383         }
384
385         let mut bound_spans = vec![];
386         let mut restrict_type_params = false;
387         let mut unsatisfied_bounds = false;
388         if item_name.name == sym::count && self.is_slice_ty(rcvr_ty, span) {
389             let msg = "consider using `len` instead";
390             if let SelfSource::MethodCall(_expr) = source {
391                 err.span_suggestion_short(span, msg, "len", Applicability::MachineApplicable);
392             } else {
393                 err.span_label(span, msg);
394             }
395             if let Some(iterator_trait) = self.tcx.get_diagnostic_item(sym::Iterator) {
396                 let iterator_trait = self.tcx.def_path_str(iterator_trait);
397                 err.note(&format!(
398                     "`count` is defined on `{iterator_trait}`, which `{rcvr_ty}` does not implement"
399                 ));
400             }
401         } else if !unsatisfied_predicates.is_empty() {
402             let mut type_params = FxHashMap::default();
403
404             // Pick out the list of unimplemented traits on the receiver.
405             // This is used for custom error messages with the `#[rustc_on_unimplemented]` attribute.
406             let mut unimplemented_traits = FxHashMap::default();
407             let mut unimplemented_traits_only = true;
408             for (predicate, _parent_pred, cause) in unsatisfied_predicates {
409                 if let (ty::PredicateKind::Clause(ty::Clause::Trait(p)), Some(cause)) =
410                     (predicate.kind().skip_binder(), cause.as_ref())
411                 {
412                     if p.trait_ref.self_ty() != rcvr_ty {
413                         // This is necessary, not just to keep the errors clean, but also
414                         // because our derived obligations can wind up with a trait ref that
415                         // requires a different param_env to be correctly compared.
416                         continue;
417                     }
418                     unimplemented_traits.entry(p.trait_ref.def_id).or_insert((
419                         predicate.kind().rebind(p.trait_ref),
420                         Obligation {
421                             cause: cause.clone(),
422                             param_env: self.param_env,
423                             predicate: *predicate,
424                             recursion_depth: 0,
425                         },
426                     ));
427                 }
428             }
429
430             // Make sure that, if any traits other than the found ones were involved,
431             // we don't don't report an unimplemented trait.
432             // We don't want to say that `iter::Cloned` is not an iterator, just
433             // because of some non-Clone item being iterated over.
434             for (predicate, _parent_pred, _cause) in unsatisfied_predicates {
435                 match predicate.kind().skip_binder() {
436                     ty::PredicateKind::Clause(ty::Clause::Trait(p))
437                         if unimplemented_traits.contains_key(&p.trait_ref.def_id) => {}
438                     _ => {
439                         unimplemented_traits_only = false;
440                         break;
441                     }
442                 }
443             }
444
445             let mut collect_type_param_suggestions =
446                 |self_ty: Ty<'tcx>, parent_pred: ty::Predicate<'tcx>, obligation: &str| {
447                     // We don't care about regions here, so it's fine to skip the binder here.
448                     if let (ty::Param(_), ty::PredicateKind::Clause(ty::Clause::Trait(p))) =
449                         (self_ty.kind(), parent_pred.kind().skip_binder())
450                     {
451                         let hir = self.tcx.hir();
452                         let node = match p.trait_ref.self_ty().kind() {
453                             ty::Param(_) => {
454                                 // Account for `fn` items like in `issue-35677.rs` to
455                                 // suggest restricting its type params.
456                                 let parent_body =
457                                     hir.body_owner(hir::BodyId { hir_id: self.body_id });
458                                 Some(hir.get(parent_body))
459                             }
460                             ty::Adt(def, _) => {
461                                 def.did().as_local().map(|def_id| hir.get_by_def_id(def_id))
462                             }
463                             _ => None,
464                         };
465                         if let Some(hir::Node::Item(hir::Item { kind, .. })) = node {
466                             if let Some(g) = kind.generics() {
467                                 let key = (
468                                     g.tail_span_for_predicate_suggestion(),
469                                     g.add_where_or_trailing_comma(),
470                                 );
471                                 type_params
472                                     .entry(key)
473                                     .or_insert_with(FxHashSet::default)
474                                     .insert(obligation.to_owned());
475                             }
476                         }
477                     }
478                 };
479             let mut bound_span_label = |self_ty: Ty<'_>, obligation: &str, quiet: &str| {
480                 let msg = format!(
481                     "doesn't satisfy `{}`",
482                     if obligation.len() > 50 { quiet } else { obligation }
483                 );
484                 match &self_ty.kind() {
485                     // Point at the type that couldn't satisfy the bound.
486                     ty::Adt(def, _) => bound_spans.push((self.tcx.def_span(def.did()), msg)),
487                     // Point at the trait object that couldn't satisfy the bound.
488                     ty::Dynamic(preds, _, _) => {
489                         for pred in preds.iter() {
490                             match pred.skip_binder() {
491                                 ty::ExistentialPredicate::Trait(tr) => {
492                                     bound_spans.push((self.tcx.def_span(tr.def_id), msg.clone()))
493                                 }
494                                 ty::ExistentialPredicate::Projection(_)
495                                 | ty::ExistentialPredicate::AutoTrait(_) => {}
496                             }
497                         }
498                     }
499                     // Point at the closure that couldn't satisfy the bound.
500                     ty::Closure(def_id, _) => bound_spans
501                         .push((tcx.def_span(*def_id), format!("doesn't satisfy `{}`", quiet))),
502                     _ => {}
503                 }
504             };
505             let mut format_pred = |pred: ty::Predicate<'tcx>| {
506                 let bound_predicate = pred.kind();
507                 match bound_predicate.skip_binder() {
508                     ty::PredicateKind::Clause(ty::Clause::Projection(pred)) => {
509                         let pred = bound_predicate.rebind(pred);
510                         // `<Foo as Iterator>::Item = String`.
511                         let projection_ty = pred.skip_binder().projection_ty;
512
513                         let substs_with_infer_self = tcx.mk_substs(
514                             iter::once(tcx.mk_ty_var(ty::TyVid::from_u32(0)).into())
515                                 .chain(projection_ty.substs.iter().skip(1)),
516                         );
517
518                         let quiet_projection_ty =
519                             tcx.mk_alias_ty(projection_ty.def_id, substs_with_infer_self);
520
521                         let term = pred.skip_binder().term;
522
523                         let obligation = format!("{} = {}", projection_ty, term);
524                         let quiet = with_forced_trimmed_paths!(format!(
525                             "{} = {}",
526                             quiet_projection_ty, term
527                         ));
528
529                         bound_span_label(projection_ty.self_ty(), &obligation, &quiet);
530                         Some((obligation, projection_ty.self_ty()))
531                     }
532                     ty::PredicateKind::Clause(ty::Clause::Trait(poly_trait_ref)) => {
533                         let p = poly_trait_ref.trait_ref;
534                         let self_ty = p.self_ty();
535                         let path = p.print_only_trait_path();
536                         let obligation = format!("{}: {}", self_ty, path);
537                         let quiet = with_forced_trimmed_paths!(format!("_: {}", path));
538                         bound_span_label(self_ty, &obligation, &quiet);
539                         Some((obligation, self_ty))
540                     }
541                     _ => None,
542                 }
543             };
544
545             // Find all the requirements that come from a local `impl` block.
546             let mut skip_list: FxHashSet<_> = Default::default();
547             let mut spanned_predicates: FxHashMap<MultiSpan, _> = Default::default();
548             for (data, p, parent_p, impl_def_id, cause) in unsatisfied_predicates
549                 .iter()
550                 .filter_map(|(p, parent, c)| c.as_ref().map(|c| (p, parent, c)))
551                 .filter_map(|(p, parent, c)| match c.code() {
552                     ObligationCauseCode::ImplDerivedObligation(data) => {
553                         Some((&data.derived, p, parent, data.impl_def_id, data))
554                     }
555                     _ => None,
556                 })
557             {
558                 let parent_trait_ref = data.parent_trait_pred;
559                 let path = parent_trait_ref.print_modifiers_and_trait_path();
560                 let tr_self_ty = parent_trait_ref.skip_binder().self_ty();
561                 let unsatisfied_msg = "unsatisfied trait bound introduced here";
562                 let derive_msg = "unsatisfied trait bound introduced in this `derive` macro";
563                 match self.tcx.hir().get_if_local(impl_def_id) {
564                     // Unmet obligation comes from a `derive` macro, point at it once to
565                     // avoid multiple span labels pointing at the same place.
566                     Some(Node::Item(hir::Item {
567                         kind: hir::ItemKind::Impl(hir::Impl { of_trait, self_ty, .. }),
568                         ..
569                     })) if matches!(
570                         self_ty.span.ctxt().outer_expn_data().kind,
571                         ExpnKind::Macro(MacroKind::Derive, _)
572                     ) || matches!(
573                         of_trait.as_ref().map(|t| t.path.span.ctxt().outer_expn_data().kind),
574                         Some(ExpnKind::Macro(MacroKind::Derive, _))
575                     ) =>
576                     {
577                         let span = self_ty.span.ctxt().outer_expn_data().call_site;
578                         let mut spans: MultiSpan = span.into();
579                         spans.push_span_label(span, derive_msg);
580                         let entry = spanned_predicates.entry(spans);
581                         entry.or_insert_with(|| (path, tr_self_ty, Vec::new())).2.push(p);
582                     }
583
584                     // Unmet obligation coming from an `impl`.
585                     Some(Node::Item(hir::Item {
586                         kind: hir::ItemKind::Impl(hir::Impl { of_trait, self_ty, generics, .. }),
587                         span: item_span,
588                         ..
589                     })) => {
590                         let sized_pred =
591                             unsatisfied_predicates.iter().any(|(pred, _, _)| {
592                                 match pred.kind().skip_binder() {
593                                     ty::PredicateKind::Clause(ty::Clause::Trait(pred)) => {
594                                         Some(pred.def_id()) == self.tcx.lang_items().sized_trait()
595                                             && pred.polarity == ty::ImplPolarity::Positive
596                                     }
597                                     _ => false,
598                                 }
599                             });
600                         for param in generics.params {
601                             if param.span == cause.span && sized_pred {
602                                 let (sp, sugg) = match param.colon_span {
603                                     Some(sp) => (sp.shrink_to_hi(), " ?Sized +"),
604                                     None => (param.span.shrink_to_hi(), ": ?Sized"),
605                                 };
606                                 err.span_suggestion_verbose(
607                                     sp,
608                                     "consider relaxing the type parameter's implicit \
609                                      `Sized` bound",
610                                     sugg,
611                                     Applicability::MachineApplicable,
612                                 );
613                             }
614                         }
615                         if let Some(pred) = parent_p {
616                             // Done to add the "doesn't satisfy" `span_label`.
617                             let _ = format_pred(*pred);
618                         }
619                         skip_list.insert(p);
620                         let mut spans = if cause.span != *item_span {
621                             let mut spans: MultiSpan = cause.span.into();
622                             spans.push_span_label(cause.span, unsatisfied_msg);
623                             spans
624                         } else {
625                             let mut spans = Vec::with_capacity(2);
626                             if let Some(trait_ref) = of_trait {
627                                 spans.push(trait_ref.path.span);
628                             }
629                             spans.push(self_ty.span);
630                             spans.into()
631                         };
632                         if let Some(trait_ref) = of_trait {
633                             spans.push_span_label(trait_ref.path.span, "");
634                         }
635                         spans.push_span_label(self_ty.span, "");
636
637                         let entry = spanned_predicates.entry(spans);
638                         entry.or_insert_with(|| (path, tr_self_ty, Vec::new())).2.push(p);
639                     }
640                     Some(Node::Item(hir::Item {
641                         kind: hir::ItemKind::Trait(rustc_ast::ast::IsAuto::Yes, ..),
642                         span: item_span,
643                         ..
644                     })) => {
645                         tcx.sess.delay_span_bug(
646                             *item_span,
647                             "auto trait is invoked with no method error, but no error reported?",
648                         );
649                     }
650                     Some(_) => unreachable!(),
651                     None => (),
652                 }
653             }
654             let mut spanned_predicates: Vec<_> = spanned_predicates.into_iter().collect();
655             spanned_predicates.sort_by_key(|(span, (_, _, _))| span.primary_span());
656             for (span, (_path, _self_ty, preds)) in spanned_predicates {
657                 let mut preds: Vec<_> = preds
658                     .into_iter()
659                     .filter_map(|pred| format_pred(*pred))
660                     .map(|(p, _)| format!("`{}`", p))
661                     .collect();
662                 preds.sort();
663                 preds.dedup();
664                 let msg = if let [pred] = &preds[..] {
665                     format!("trait bound {} was not satisfied", pred)
666                 } else {
667                     format!("the following trait bounds were not satisfied:\n{}", preds.join("\n"),)
668                 };
669                 err.span_note(span, &msg);
670                 unsatisfied_bounds = true;
671             }
672
673             // The requirements that didn't have an `impl` span to show.
674             let mut bound_list = unsatisfied_predicates
675                 .iter()
676                 .filter_map(|(pred, parent_pred, _cause)| {
677                     format_pred(*pred).map(|(p, self_ty)| {
678                         collect_type_param_suggestions(self_ty, *pred, &p);
679                         (
680                             match parent_pred {
681                                 None => format!("`{}`", &p),
682                                 Some(parent_pred) => match format_pred(*parent_pred) {
683                                     None => format!("`{}`", &p),
684                                     Some((parent_p, _)) => {
685                                         collect_type_param_suggestions(self_ty, *parent_pred, &p);
686                                         format!("`{}`\nwhich is required by `{}`", p, parent_p)
687                                     }
688                                 },
689                             },
690                             *pred,
691                         )
692                     })
693                 })
694                 .filter(|(_, pred)| !skip_list.contains(&pred))
695                 .map(|(t, _)| t)
696                 .enumerate()
697                 .collect::<Vec<(usize, String)>>();
698
699             for ((span, add_where_or_comma), obligations) in type_params.into_iter() {
700                 restrict_type_params = true;
701                 // #74886: Sort here so that the output is always the same.
702                 let mut obligations = obligations.into_iter().collect::<Vec<_>>();
703                 obligations.sort();
704                 err.span_suggestion_verbose(
705                     span,
706                     &format!(
707                         "consider restricting the type parameter{s} to satisfy the \
708                          trait bound{s}",
709                         s = pluralize!(obligations.len())
710                     ),
711                     format!("{} {}", add_where_or_comma, obligations.join(", ")),
712                     Applicability::MaybeIncorrect,
713                 );
714             }
715
716             bound_list.sort_by(|(_, a), (_, b)| a.cmp(b)); // Sort alphabetically.
717             bound_list.dedup_by(|(_, a), (_, b)| a == b); // #35677
718             bound_list.sort_by_key(|(pos, _)| *pos); // Keep the original predicate order.
719
720             if !bound_list.is_empty() || !skip_list.is_empty() {
721                 let bound_list =
722                     bound_list.into_iter().map(|(_, path)| path).collect::<Vec<_>>().join("\n");
723                 let actual_prefix = rcvr_ty.prefix_string(self.tcx);
724                 info!("unimplemented_traits.len() == {}", unimplemented_traits.len());
725                 let (primary_message, label) = if unimplemented_traits.len() == 1
726                     && unimplemented_traits_only
727                 {
728                     unimplemented_traits
729                         .into_iter()
730                         .next()
731                         .map(|(_, (trait_ref, obligation))| {
732                             if trait_ref.self_ty().references_error() || rcvr_ty.references_error()
733                             {
734                                 // Avoid crashing.
735                                 return (None, None);
736                             }
737                             let OnUnimplementedNote { message, label, .. } =
738                                 self.err_ctxt().on_unimplemented_note(trait_ref, &obligation);
739                             (message, label)
740                         })
741                         .unwrap()
742                 } else {
743                     (None, None)
744                 };
745                 let primary_message = primary_message.unwrap_or_else(|| {
746                     format!(
747                         "the {item_kind} `{item_name}` exists for {actual_prefix} `{ty_str}`, \
748                     but its trait bounds were not satisfied"
749                     )
750                 });
751                 err.set_primary_message(&primary_message);
752                 if let Some(label) = label {
753                     custom_span_label = true;
754                     err.span_label(span, label);
755                 }
756                 if !bound_list.is_empty() {
757                     err.note(&format!(
758                         "the following trait bounds were not satisfied:\n{bound_list}"
759                     ));
760                 }
761                 self.suggest_derive(&mut err, &unsatisfied_predicates);
762
763                 unsatisfied_bounds = true;
764             }
765         }
766
767         let label_span_not_found = |err: &mut Diagnostic| {
768             if unsatisfied_predicates.is_empty() {
769                 err.span_label(span, format!("{item_kind} not found in `{ty_str}`"));
770                 let is_string_or_ref_str = match rcvr_ty.kind() {
771                     ty::Ref(_, ty, _) => {
772                         ty.is_str()
773                             || matches!(
774                                 ty.kind(),
775                                 ty::Adt(adt, _) if Some(adt.did()) == self.tcx.lang_items().string()
776                             )
777                     }
778                     ty::Adt(adt, _) => Some(adt.did()) == self.tcx.lang_items().string(),
779                     _ => false,
780                 };
781                 if is_string_or_ref_str && item_name.name == sym::iter {
782                     err.span_suggestion_verbose(
783                         item_name.span,
784                         "because of the in-memory representation of `&str`, to obtain \
785                          an `Iterator` over each of its codepoint use method `chars`",
786                         "chars",
787                         Applicability::MachineApplicable,
788                     );
789                 }
790                 if let ty::Adt(adt, _) = rcvr_ty.kind() {
791                     let mut inherent_impls_candidate = self
792                         .tcx
793                         .inherent_impls(adt.did())
794                         .iter()
795                         .copied()
796                         .filter(|def_id| {
797                             if let Some(assoc) = self.associated_value(*def_id, item_name) {
798                                 // Check for both mode is the same so we avoid suggesting
799                                 // incorrect associated item.
800                                 match (mode, assoc.fn_has_self_parameter, source) {
801                                     (Mode::MethodCall, true, SelfSource::MethodCall(_)) => {
802                                         // We check that the suggest type is actually
803                                         // different from the received one
804                                         // So we avoid suggestion method with Box<Self>
805                                         // for instance
806                                         self.tcx.at(span).type_of(*def_id) != rcvr_ty
807                                             && self.tcx.at(span).type_of(*def_id) != rcvr_ty
808                                     }
809                                     (Mode::Path, false, _) => true,
810                                     _ => false,
811                                 }
812                             } else {
813                                 false
814                             }
815                         })
816                         .collect::<Vec<_>>();
817                     if !inherent_impls_candidate.is_empty() {
818                         inherent_impls_candidate.sort();
819                         inherent_impls_candidate.dedup();
820
821                         // number of type to shows at most.
822                         let limit = if inherent_impls_candidate.len() == 5 { 5 } else { 4 };
823                         let type_candidates = inherent_impls_candidate
824                             .iter()
825                             .take(limit)
826                             .map(|impl_item| {
827                                 format!("- `{}`", self.tcx.at(span).type_of(*impl_item))
828                             })
829                             .collect::<Vec<_>>()
830                             .join("\n");
831                         let additional_types = if inherent_impls_candidate.len() > limit {
832                             format!("\nand {} more types", inherent_impls_candidate.len() - limit)
833                         } else {
834                             "".to_string()
835                         };
836                         err.note(&format!(
837                             "the {item_kind} was found for\n{}{}",
838                             type_candidates, additional_types
839                         ));
840                     }
841                 }
842             } else {
843                 let ty_str =
844                     if ty_str.len() > 50 { String::new() } else { format!("on `{ty_str}` ") };
845                 err.span_label(
846                     span,
847                     format!("{item_kind} cannot be called {ty_str}due to unsatisfied trait bounds"),
848                 );
849             }
850         };
851
852         // If the method name is the name of a field with a function or closure type,
853         // give a helping note that it has to be called as `(x.f)(...)`.
854         if let SelfSource::MethodCall(expr) = source {
855             if !self.suggest_calling_field_as_fn(span, rcvr_ty, expr, item_name, &mut err)
856                 && lev_candidate.is_none()
857                 && !custom_span_label
858             {
859                 label_span_not_found(&mut err);
860             }
861         } else if !custom_span_label {
862             label_span_not_found(&mut err);
863         }
864
865         // Don't suggest (for example) `expr.field.clone()` if `expr.clone()`
866         // can't be called due to `typeof(expr): Clone` not holding.
867         if unsatisfied_predicates.is_empty() {
868             self.suggest_calling_method_on_field(&mut err, source, span, rcvr_ty, item_name);
869         }
870
871         self.check_for_inner_self(&mut err, source, rcvr_ty, item_name);
872
873         bound_spans.sort();
874         bound_spans.dedup();
875         for (span, msg) in bound_spans.into_iter() {
876             err.span_label(span, &msg);
877         }
878
879         if rcvr_ty.is_numeric() && rcvr_ty.is_fresh() || restrict_type_params {
880         } else {
881             self.suggest_traits_to_import(
882                 &mut err,
883                 span,
884                 rcvr_ty,
885                 item_name,
886                 args.map(|(_, args)| args.len() + 1),
887                 source,
888                 no_match_data.out_of_scope_traits.clone(),
889                 &unsatisfied_predicates,
890                 &static_candidates,
891                 unsatisfied_bounds,
892             );
893         }
894
895         // Don't emit a suggestion if we found an actual method
896         // that had unsatisfied trait bounds
897         if unsatisfied_predicates.is_empty() && rcvr_ty.is_enum() {
898             let adt_def = rcvr_ty.ty_adt_def().expect("enum is not an ADT");
899             if let Some(suggestion) = lev_distance::find_best_match_for_name(
900                 &adt_def.variants().iter().map(|s| s.name).collect::<Vec<_>>(),
901                 item_name.name,
902                 None,
903             ) {
904                 err.span_suggestion(
905                     span,
906                     "there is a variant with a similar name",
907                     suggestion,
908                     Applicability::MaybeIncorrect,
909                 );
910             }
911         }
912
913         if item_name.name == sym::as_str && rcvr_ty.peel_refs().is_str() {
914             let msg = "remove this method call";
915             let mut fallback_span = true;
916             if let SelfSource::MethodCall(expr) = source {
917                 let call_expr = self.tcx.hir().expect_expr(self.tcx.hir().parent_id(expr.hir_id));
918                 if let Some(span) = call_expr.span.trim_start(expr.span) {
919                     err.span_suggestion(span, msg, "", Applicability::MachineApplicable);
920                     fallback_span = false;
921                 }
922             }
923             if fallback_span {
924                 err.span_label(span, msg);
925             }
926         } else if let Some(lev_candidate) = lev_candidate {
927             // Don't emit a suggestion if we found an actual method
928             // that had unsatisfied trait bounds
929             if unsatisfied_predicates.is_empty() {
930                 let def_kind = lev_candidate.kind.as_def_kind();
931                 // Methods are defined within the context of a struct and their first parameter is always self,
932                 // which represents the instance of the struct the method is being called on
933                 // Associated functions don’t take self as a parameter and
934                 // they are not methods because they don’t have an instance of the struct to work with.
935                 if def_kind == DefKind::AssocFn && lev_candidate.fn_has_self_parameter {
936                     err.span_suggestion(
937                         span,
938                         "there is a method with a similar name",
939                         lev_candidate.name,
940                         Applicability::MaybeIncorrect,
941                     );
942                 } else {
943                     err.span_suggestion(
944                         span,
945                         &format!(
946                             "there is {} {} with a similar name",
947                             def_kind.article(),
948                             def_kind.descr(lev_candidate.def_id),
949                         ),
950                         lev_candidate.name,
951                         Applicability::MaybeIncorrect,
952                     );
953                 }
954             }
955         }
956
957         self.check_for_deref_method(&mut err, source, rcvr_ty, item_name);
958         return Some(err);
959     }
960
961     fn note_candidates_on_method_error(
962         &self,
963         rcvr_ty: Ty<'tcx>,
964         item_name: Ident,
965         args: Option<(&'tcx hir::Expr<'tcx>, &'tcx [hir::Expr<'tcx>])>,
966         span: Span,
967         err: &mut Diagnostic,
968         sources: &mut Vec<CandidateSource>,
969         sugg_span: Option<Span>,
970     ) {
971         sources.sort();
972         sources.dedup();
973         // Dynamic limit to avoid hiding just one candidate, which is silly.
974         let limit = if sources.len() == 5 { 5 } else { 4 };
975
976         for (idx, source) in sources.iter().take(limit).enumerate() {
977             match *source {
978                 CandidateSource::Impl(impl_did) => {
979                     // Provide the best span we can. Use the item, if local to crate, else
980                     // the impl, if local to crate (item may be defaulted), else nothing.
981                     let Some(item) = self.associated_value(impl_did, item_name).or_else(|| {
982                         let impl_trait_ref = self.tcx.impl_trait_ref(impl_did)?;
983                         self.associated_value(impl_trait_ref.def_id, item_name)
984                     }) else {
985                         continue;
986                     };
987
988                     let note_span = if item.def_id.is_local() {
989                         Some(self.tcx.def_span(item.def_id))
990                     } else if impl_did.is_local() {
991                         Some(self.tcx.def_span(impl_did))
992                     } else {
993                         None
994                     };
995
996                     let impl_ty = self.tcx.at(span).type_of(impl_did);
997
998                     let insertion = match self.tcx.impl_trait_ref(impl_did) {
999                         None => String::new(),
1000                         Some(trait_ref) => {
1001                             format!(" of the trait `{}`", self.tcx.def_path_str(trait_ref.def_id))
1002                         }
1003                     };
1004
1005                     let (note_str, idx) = if sources.len() > 1 {
1006                         (
1007                             format!(
1008                                 "candidate #{} is defined in an impl{} for the type `{}`",
1009                                 idx + 1,
1010                                 insertion,
1011                                 impl_ty,
1012                             ),
1013                             Some(idx + 1),
1014                         )
1015                     } else {
1016                         (
1017                             format!(
1018                                 "the candidate is defined in an impl{} for the type `{}`",
1019                                 insertion, impl_ty,
1020                             ),
1021                             None,
1022                         )
1023                     };
1024                     if let Some(note_span) = note_span {
1025                         // We have a span pointing to the method. Show note with snippet.
1026                         err.span_note(note_span, &note_str);
1027                     } else {
1028                         err.note(&note_str);
1029                     }
1030                     if let Some(sugg_span) = sugg_span
1031                         && let Some(trait_ref) = self.tcx.impl_trait_ref(impl_did) {
1032                         let path = self.tcx.def_path_str(trait_ref.def_id);
1033
1034                         let ty = match item.kind {
1035                             ty::AssocKind::Const | ty::AssocKind::Type => rcvr_ty,
1036                             ty::AssocKind::Fn => self
1037                                 .tcx
1038                                 .fn_sig(item.def_id)
1039                                 .inputs()
1040                                 .skip_binder()
1041                                 .get(0)
1042                                 .filter(|ty| ty.is_region_ptr() && !rcvr_ty.is_region_ptr())
1043                                 .copied()
1044                                 .unwrap_or(rcvr_ty),
1045                         };
1046                         print_disambiguation_help(
1047                             item_name,
1048                             args,
1049                             err,
1050                             path,
1051                             ty,
1052                             item.kind,
1053                             item.def_id,
1054                             sugg_span,
1055                             idx,
1056                             self.tcx.sess.source_map(),
1057                             item.fn_has_self_parameter,
1058                         );
1059                     }
1060                 }
1061                 CandidateSource::Trait(trait_did) => {
1062                     let Some(item) = self.associated_value(trait_did, item_name) else { continue };
1063                     let item_span = self.tcx.def_span(item.def_id);
1064                     let idx = if sources.len() > 1 {
1065                         let msg = &format!(
1066                             "candidate #{} is defined in the trait `{}`",
1067                             idx + 1,
1068                             self.tcx.def_path_str(trait_did)
1069                         );
1070                         err.span_note(item_span, msg);
1071                         Some(idx + 1)
1072                     } else {
1073                         let msg = &format!(
1074                             "the candidate is defined in the trait `{}`",
1075                             self.tcx.def_path_str(trait_did)
1076                         );
1077                         err.span_note(item_span, msg);
1078                         None
1079                     };
1080                     if let Some(sugg_span) = sugg_span {
1081                         let path = self.tcx.def_path_str(trait_did);
1082                         print_disambiguation_help(
1083                             item_name,
1084                             args,
1085                             err,
1086                             path,
1087                             rcvr_ty,
1088                             item.kind,
1089                             item.def_id,
1090                             sugg_span,
1091                             idx,
1092                             self.tcx.sess.source_map(),
1093                             item.fn_has_self_parameter,
1094                         );
1095                     }
1096                 }
1097             }
1098         }
1099         if sources.len() > limit {
1100             err.note(&format!("and {} others", sources.len() - limit));
1101         }
1102     }
1103
1104     /// Suggest calling `Ty::method` if `.method()` isn't found because the method
1105     /// doesn't take a `self` receiver.
1106     fn suggest_associated_call_syntax(
1107         &self,
1108         err: &mut Diagnostic,
1109         static_candidates: &Vec<CandidateSource>,
1110         rcvr_ty: Ty<'tcx>,
1111         source: SelfSource<'tcx>,
1112         item_name: Ident,
1113         args: Option<(&hir::Expr<'tcx>, &[hir::Expr<'tcx>])>,
1114         sugg_span: Span,
1115     ) {
1116         let mut has_unsuggestable_args = false;
1117         let ty_str = if let Some(CandidateSource::Impl(impl_did)) = static_candidates.get(0) {
1118             // When the "method" is resolved through dereferencing, we really want the
1119             // original type that has the associated function for accurate suggestions.
1120             // (#61411)
1121             let impl_ty = self.tcx.type_of(*impl_did);
1122             let target_ty = self
1123                 .autoderef(sugg_span, rcvr_ty)
1124                 .find(|(rcvr_ty, _)| {
1125                     DeepRejectCtxt { treat_obligation_params: TreatParams::AsInfer }
1126                         .types_may_unify(*rcvr_ty, impl_ty)
1127                 })
1128                 .map_or(impl_ty, |(ty, _)| ty)
1129                 .peel_refs();
1130             if let ty::Adt(def, substs) = target_ty.kind() {
1131                 // If there are any inferred arguments, (`{integer}`), we should replace
1132                 // them with underscores to allow the compiler to infer them
1133                 let infer_substs = self.tcx.mk_substs(substs.into_iter().map(|arg| {
1134                     if !arg.is_suggestable(self.tcx, true) {
1135                         has_unsuggestable_args = true;
1136                         match arg.unpack() {
1137                             GenericArgKind::Lifetime(_) => self
1138                                 .next_region_var(RegionVariableOrigin::MiscVariable(
1139                                     rustc_span::DUMMY_SP,
1140                                 ))
1141                                 .into(),
1142                             GenericArgKind::Type(_) => self
1143                                 .next_ty_var(TypeVariableOrigin {
1144                                     span: rustc_span::DUMMY_SP,
1145                                     kind: TypeVariableOriginKind::MiscVariable,
1146                                 })
1147                                 .into(),
1148                             GenericArgKind::Const(arg) => self
1149                                 .next_const_var(
1150                                     arg.ty(),
1151                                     ConstVariableOrigin {
1152                                         span: rustc_span::DUMMY_SP,
1153                                         kind: ConstVariableOriginKind::MiscVariable,
1154                                     },
1155                                 )
1156                                 .into(),
1157                         }
1158                     } else {
1159                         arg
1160                     }
1161                 }));
1162
1163                 self.tcx.value_path_str_with_substs(def.did(), infer_substs)
1164             } else {
1165                 self.ty_to_value_string(target_ty)
1166             }
1167         } else {
1168             self.ty_to_value_string(rcvr_ty.peel_refs())
1169         };
1170         if let SelfSource::MethodCall(_) = source {
1171             let first_arg = if let Some(CandidateSource::Impl(impl_did)) = static_candidates.get(0)
1172                 && let Some(assoc) = self.associated_value(*impl_did, item_name)
1173                 && assoc.kind == ty::AssocKind::Fn
1174             {
1175                 let sig = self.tcx.fn_sig(assoc.def_id);
1176                 sig.inputs().skip_binder().get(0).and_then(|first| if first.peel_refs() == rcvr_ty.peel_refs() {
1177                     None
1178                 } else {
1179                     Some(first.ref_mutability().map_or("", |mutbl| mutbl.ref_prefix_str()))
1180                 })
1181             } else {
1182                 None
1183             };
1184             let mut applicability = Applicability::MachineApplicable;
1185             let args = if let Some((receiver, args)) = args {
1186                 // The first arg is the same kind as the receiver
1187                 let explicit_args = if first_arg.is_some() {
1188                     std::iter::once(receiver).chain(args.iter()).collect::<Vec<_>>()
1189                 } else {
1190                     // There is no `Self` kind to infer the arguments from
1191                     if has_unsuggestable_args {
1192                         applicability = Applicability::HasPlaceholders;
1193                     }
1194                     args.iter().collect()
1195                 };
1196                 format!(
1197                     "({}{})",
1198                     first_arg.unwrap_or(""),
1199                     explicit_args
1200                         .iter()
1201                         .map(|arg| self
1202                             .tcx
1203                             .sess
1204                             .source_map()
1205                             .span_to_snippet(arg.span)
1206                             .unwrap_or_else(|_| {
1207                                 applicability = Applicability::HasPlaceholders;
1208                                 "_".to_owned()
1209                             }))
1210                         .collect::<Vec<_>>()
1211                         .join(", "),
1212                 )
1213             } else {
1214                 applicability = Applicability::HasPlaceholders;
1215                 "(...)".to_owned()
1216             };
1217             err.span_suggestion(
1218                 sugg_span,
1219                 "use associated function syntax instead",
1220                 format!("{}::{}{}", ty_str, item_name, args),
1221                 applicability,
1222             );
1223         } else {
1224             err.help(&format!("try with `{}::{}`", ty_str, item_name,));
1225         }
1226     }
1227
1228     /// Suggest calling a field with a type that implements the `Fn*` traits instead of a method with
1229     /// the same name as the field i.e. `(a.my_fn_ptr)(10)` instead of `a.my_fn_ptr(10)`.
1230     fn suggest_calling_field_as_fn(
1231         &self,
1232         span: Span,
1233         rcvr_ty: Ty<'tcx>,
1234         expr: &hir::Expr<'_>,
1235         item_name: Ident,
1236         err: &mut Diagnostic,
1237     ) -> bool {
1238         let tcx = self.tcx;
1239         let field_receiver = self.autoderef(span, rcvr_ty).find_map(|(ty, _)| match ty.kind() {
1240             ty::Adt(def, substs) if !def.is_enum() => {
1241                 let variant = &def.non_enum_variant();
1242                 tcx.find_field_index(item_name, variant).map(|index| {
1243                     let field = &variant.fields[index];
1244                     let field_ty = field.ty(tcx, substs);
1245                     (field, field_ty)
1246                 })
1247             }
1248             _ => None,
1249         });
1250         if let Some((field, field_ty)) = field_receiver {
1251             let scope = tcx.parent_module(self.body_id);
1252             let is_accessible = field.vis.is_accessible_from(scope, tcx);
1253
1254             if is_accessible {
1255                 if self.is_fn_ty(field_ty, span) {
1256                     let expr_span = expr.span.to(item_name.span);
1257                     err.multipart_suggestion(
1258                         &format!(
1259                             "to call the function stored in `{}`, \
1260                                          surround the field access with parentheses",
1261                             item_name,
1262                         ),
1263                         vec![
1264                             (expr_span.shrink_to_lo(), '('.to_string()),
1265                             (expr_span.shrink_to_hi(), ')'.to_string()),
1266                         ],
1267                         Applicability::MachineApplicable,
1268                     );
1269                 } else {
1270                     let call_expr = tcx.hir().expect_expr(tcx.hir().parent_id(expr.hir_id));
1271
1272                     if let Some(span) = call_expr.span.trim_start(item_name.span) {
1273                         err.span_suggestion(
1274                             span,
1275                             "remove the arguments",
1276                             "",
1277                             Applicability::MaybeIncorrect,
1278                         );
1279                     }
1280                 }
1281             }
1282
1283             let field_kind = if is_accessible { "field" } else { "private field" };
1284             err.span_label(item_name.span, format!("{}, not a method", field_kind));
1285             return true;
1286         }
1287         false
1288     }
1289
1290     /// Suggest possible range with adding parentheses, for example:
1291     /// when encountering `0..1.map(|i| i + 1)` suggest `(0..1).map(|i| i + 1)`.
1292     fn suggest_wrapping_range_with_parens(
1293         &self,
1294         tcx: TyCtxt<'tcx>,
1295         actual: Ty<'tcx>,
1296         source: SelfSource<'tcx>,
1297         span: Span,
1298         item_name: Ident,
1299         ty_str: &str,
1300     ) -> bool {
1301         if let SelfSource::MethodCall(expr) = source {
1302             for (_, parent) in tcx.hir().parent_iter(expr.hir_id).take(5) {
1303                 if let Node::Expr(parent_expr) = parent {
1304                     let lang_item = match parent_expr.kind {
1305                         ExprKind::Struct(ref qpath, _, _) => match **qpath {
1306                             QPath::LangItem(LangItem::Range, ..) => Some(LangItem::Range),
1307                             QPath::LangItem(LangItem::RangeTo, ..) => Some(LangItem::RangeTo),
1308                             QPath::LangItem(LangItem::RangeToInclusive, ..) => {
1309                                 Some(LangItem::RangeToInclusive)
1310                             }
1311                             _ => None,
1312                         },
1313                         ExprKind::Call(ref func, _) => match func.kind {
1314                             // `..=` desugars into `::std::ops::RangeInclusive::new(...)`.
1315                             ExprKind::Path(QPath::LangItem(LangItem::RangeInclusiveNew, ..)) => {
1316                                 Some(LangItem::RangeInclusiveStruct)
1317                             }
1318                             _ => None,
1319                         },
1320                         _ => None,
1321                     };
1322
1323                     if lang_item.is_none() {
1324                         continue;
1325                     }
1326
1327                     let span_included = match parent_expr.kind {
1328                         hir::ExprKind::Struct(_, eps, _) => {
1329                             eps.len() > 0 && eps.last().map_or(false, |ep| ep.span.contains(span))
1330                         }
1331                         // `..=` desugars into `::std::ops::RangeInclusive::new(...)`.
1332                         hir::ExprKind::Call(ref func, ..) => func.span.contains(span),
1333                         _ => false,
1334                     };
1335
1336                     if !span_included {
1337                         continue;
1338                     }
1339
1340                     let range_def_id = self.tcx.require_lang_item(lang_item.unwrap(), None);
1341                     let range_ty =
1342                         self.tcx.bound_type_of(range_def_id).subst(self.tcx, &[actual.into()]);
1343
1344                     let pick = self.probe_for_name(
1345                         Mode::MethodCall,
1346                         item_name,
1347                         IsSuggestion(true),
1348                         range_ty,
1349                         expr.hir_id,
1350                         ProbeScope::AllTraits,
1351                     );
1352                     if pick.is_ok() {
1353                         let range_span = parent_expr.span.with_hi(expr.span.hi());
1354                         tcx.sess.emit_err(errors::MissingParentheseInRange {
1355                             span,
1356                             ty_str: ty_str.to_string(),
1357                             method_name: item_name.as_str().to_string(),
1358                             add_missing_parentheses: Some(errors::AddMissingParenthesesInRange {
1359                                 func_name: item_name.name.as_str().to_string(),
1360                                 left: range_span.shrink_to_lo(),
1361                                 right: range_span.shrink_to_hi(),
1362                             }),
1363                         });
1364                         return true;
1365                     }
1366                 }
1367             }
1368         }
1369         false
1370     }
1371
1372     fn suggest_constraining_numerical_ty(
1373         &self,
1374         tcx: TyCtxt<'tcx>,
1375         actual: Ty<'tcx>,
1376         source: SelfSource<'_>,
1377         span: Span,
1378         item_kind: &str,
1379         item_name: Ident,
1380         ty_str: &str,
1381     ) -> bool {
1382         let found_candidate = all_traits(self.tcx)
1383             .into_iter()
1384             .any(|info| self.associated_value(info.def_id, item_name).is_some());
1385         let found_assoc = |ty: Ty<'tcx>| {
1386             simplify_type(tcx, ty, TreatParams::AsInfer)
1387                 .and_then(|simp| {
1388                     tcx.incoherent_impls(simp)
1389                         .iter()
1390                         .find_map(|&id| self.associated_value(id, item_name))
1391                 })
1392                 .is_some()
1393         };
1394         let found_candidate = found_candidate
1395             || found_assoc(tcx.types.i8)
1396             || found_assoc(tcx.types.i16)
1397             || found_assoc(tcx.types.i32)
1398             || found_assoc(tcx.types.i64)
1399             || found_assoc(tcx.types.i128)
1400             || found_assoc(tcx.types.u8)
1401             || found_assoc(tcx.types.u16)
1402             || found_assoc(tcx.types.u32)
1403             || found_assoc(tcx.types.u64)
1404             || found_assoc(tcx.types.u128)
1405             || found_assoc(tcx.types.f32)
1406             || found_assoc(tcx.types.f32);
1407         if found_candidate
1408             && actual.is_numeric()
1409             && !actual.has_concrete_skeleton()
1410             && let SelfSource::MethodCall(expr) = source
1411         {
1412             let mut err = struct_span_err!(
1413                 tcx.sess,
1414                 span,
1415                 E0689,
1416                 "can't call {} `{}` on ambiguous numeric type `{}`",
1417                 item_kind,
1418                 item_name,
1419                 ty_str
1420             );
1421             let concrete_type = if actual.is_integral() { "i32" } else { "f32" };
1422             match expr.kind {
1423                 ExprKind::Lit(ref lit) => {
1424                     // numeric literal
1425                     let snippet = tcx
1426                         .sess
1427                         .source_map()
1428                         .span_to_snippet(lit.span)
1429                         .unwrap_or_else(|_| "<numeric literal>".to_owned());
1430
1431                     // If this is a floating point literal that ends with '.',
1432                     // get rid of it to stop this from becoming a member access.
1433                     let snippet = snippet.strip_suffix('.').unwrap_or(&snippet);
1434                     err.span_suggestion(
1435                         lit.span,
1436                         &format!(
1437                             "you must specify a concrete type for this numeric value, \
1438                                          like `{}`",
1439                             concrete_type
1440                         ),
1441                         format!("{snippet}_{concrete_type}"),
1442                         Applicability::MaybeIncorrect,
1443                     );
1444                 }
1445                 ExprKind::Path(QPath::Resolved(_, path)) => {
1446                     // local binding
1447                     if let hir::def::Res::Local(hir_id) = path.res {
1448                         let span = tcx.hir().span(hir_id);
1449                         let filename = tcx.sess.source_map().span_to_filename(span);
1450
1451                         let parent_node =
1452                             self.tcx.hir().get_parent(hir_id);
1453                         let msg = format!(
1454                             "you must specify a type for this binding, like `{}`",
1455                             concrete_type,
1456                         );
1457
1458                         match (filename, parent_node) {
1459                             (
1460                                 FileName::Real(_),
1461                                 Node::Local(hir::Local {
1462                                     source: hir::LocalSource::Normal,
1463                                     ty,
1464                                     ..
1465                                 }),
1466                             ) => {
1467                                 let type_span = ty.map(|ty| ty.span.with_lo(span.hi())).unwrap_or(span.shrink_to_hi());
1468                                 err.span_suggestion(
1469                                     // account for `let x: _ = 42;`
1470                                     //                   ^^^
1471                                     type_span,
1472                                     &msg,
1473                                     format!(": {concrete_type}"),
1474                                     Applicability::MaybeIncorrect,
1475                                 );
1476                             }
1477                             _ => {
1478                                 err.span_label(span, msg);
1479                             }
1480                         }
1481                     }
1482                 }
1483                 _ => {}
1484             }
1485             err.emit();
1486             return true;
1487         }
1488         false
1489     }
1490
1491     /// For code `rect::area(...)`,
1492     /// if `rect` is a local variable and `area` is a valid assoc method for it,
1493     /// we try to suggest `rect.area()`
1494     pub(crate) fn suggest_assoc_method_call(&self, segs: &[PathSegment<'_>]) {
1495         debug!("suggest_assoc_method_call segs: {:?}", segs);
1496         let [seg1, seg2] = segs else { return; };
1497         let Some(mut diag) =
1498                 self.tcx.sess.diagnostic().steal_diagnostic(seg1.ident.span, StashKey::CallAssocMethod)
1499                 else { return };
1500
1501         let map = self.infcx.tcx.hir();
1502         let body = map.body(rustc_hir::BodyId { hir_id: self.body_id });
1503         struct LetVisitor<'a> {
1504             result: Option<&'a hir::Expr<'a>>,
1505             ident_name: Symbol,
1506         }
1507
1508         // FIXME: This really should be taking scoping, etc into account.
1509         impl<'v> Visitor<'v> for LetVisitor<'v> {
1510             fn visit_stmt(&mut self, ex: &'v hir::Stmt<'v>) {
1511                 if let hir::StmtKind::Local(hir::Local { pat, init, .. }) = &ex.kind
1512                     && let Binding(_, _, ident, ..) = pat.kind
1513                     && ident.name == self.ident_name
1514                 {
1515                     self.result = *init;
1516                 } else {
1517                     hir::intravisit::walk_stmt(self, ex);
1518                 }
1519             }
1520         }
1521
1522         let mut visitor = LetVisitor { result: None, ident_name: seg1.ident.name };
1523         visitor.visit_body(&body);
1524
1525         let parent = self.tcx.hir().parent_id(seg1.hir_id);
1526         if let Some(Node::Expr(call_expr)) = self.tcx.hir().find(parent)
1527             && let Some(expr) = visitor.result
1528             && let Some(self_ty) = self.node_ty_opt(expr.hir_id)
1529         {
1530             let probe = self.lookup_probe(
1531                 seg2.ident,
1532                 self_ty,
1533                 call_expr,
1534                 ProbeScope::TraitsInScope,
1535             );
1536             if probe.is_ok() {
1537                 let sm = self.infcx.tcx.sess.source_map();
1538                 diag.span_suggestion_verbose(
1539                     sm.span_extend_while(seg1.ident.span.shrink_to_hi(), |c| c == ':').unwrap(),
1540                     "you may have meant to call an instance method",
1541                     ".".to_string(),
1542                     Applicability::MaybeIncorrect,
1543                 );
1544             }
1545         }
1546         diag.emit();
1547     }
1548
1549     /// Suggest calling a method on a field i.e. `a.field.bar()` instead of `a.bar()`
1550     fn suggest_calling_method_on_field(
1551         &self,
1552         err: &mut Diagnostic,
1553         source: SelfSource<'tcx>,
1554         span: Span,
1555         actual: Ty<'tcx>,
1556         item_name: Ident,
1557     ) {
1558         if let SelfSource::MethodCall(expr) = source
1559         && let mod_id = self.tcx.parent_module(expr.hir_id).to_def_id()
1560         && let Some((fields, substs)) =
1561             self.get_field_candidates_considering_privacy(span, actual, mod_id)
1562         {
1563             let call_expr = self.tcx.hir().expect_expr(self.tcx.hir().parent_id(expr.hir_id));
1564
1565             let lang_items = self.tcx.lang_items();
1566             let never_mention_traits = [
1567                 lang_items.clone_trait(),
1568                 lang_items.deref_trait(),
1569                 lang_items.deref_mut_trait(),
1570                 self.tcx.get_diagnostic_item(sym::AsRef),
1571                 self.tcx.get_diagnostic_item(sym::AsMut),
1572                 self.tcx.get_diagnostic_item(sym::Borrow),
1573                 self.tcx.get_diagnostic_item(sym::BorrowMut),
1574             ];
1575             let candidate_fields: Vec<_> = fields
1576                 .filter_map(|candidate_field| {
1577                     self.check_for_nested_field_satisfying(
1578                         span,
1579                         &|_, field_ty| {
1580                             self.lookup_probe(
1581                                 item_name,
1582                                 field_ty,
1583                                 call_expr,
1584                                 ProbeScope::TraitsInScope,
1585                             )
1586                             .map_or(false, |pick| {
1587                                 !never_mention_traits
1588                                     .iter()
1589                                     .flatten()
1590                                     .any(|def_id| self.tcx.parent(pick.item.def_id) == *def_id)
1591                             })
1592                         },
1593                         candidate_field,
1594                         substs,
1595                         vec![],
1596                         mod_id,
1597                     )
1598                 })
1599                 .map(|field_path| {
1600                     field_path
1601                         .iter()
1602                         .map(|id| id.name.to_ident_string())
1603                         .collect::<Vec<String>>()
1604                         .join(".")
1605                 })
1606                 .collect();
1607
1608             let len = candidate_fields.len();
1609             if len > 0 {
1610                 err.span_suggestions(
1611                     item_name.span.shrink_to_lo(),
1612                     format!(
1613                         "{} of the expressions' fields {} a method of the same name",
1614                         if len > 1 { "some" } else { "one" },
1615                         if len > 1 { "have" } else { "has" },
1616                     ),
1617                     candidate_fields.iter().map(|path| format!("{path}.")),
1618                     Applicability::MaybeIncorrect,
1619                 );
1620             }
1621         }
1622     }
1623
1624     fn check_for_inner_self(
1625         &self,
1626         err: &mut Diagnostic,
1627         source: SelfSource<'tcx>,
1628         actual: Ty<'tcx>,
1629         item_name: Ident,
1630     ) {
1631         let tcx = self.tcx;
1632         let SelfSource::MethodCall(expr) = source else { return; };
1633         let call_expr = tcx.hir().expect_expr(tcx.hir().parent_id(expr.hir_id));
1634
1635         let ty::Adt(kind, substs) = actual.kind() else { return; };
1636         match kind.adt_kind() {
1637             ty::AdtKind::Enum => {
1638                 let matching_variants: Vec<_> = kind
1639                     .variants()
1640                     .iter()
1641                     .flat_map(|variant| {
1642                         let [field] = &variant.fields[..] else { return None; };
1643                         let field_ty = field.ty(tcx, substs);
1644
1645                         // Skip `_`, since that'll just lead to ambiguity.
1646                         if self.resolve_vars_if_possible(field_ty).is_ty_var() {
1647                             return None;
1648                         }
1649
1650                         self.lookup_probe(item_name, field_ty, call_expr, ProbeScope::TraitsInScope)
1651                             .ok()
1652                             .map(|pick| (variant, field, pick))
1653                     })
1654                     .collect();
1655
1656                 let ret_ty_matches = |diagnostic_item| {
1657                     if let Some(ret_ty) = self
1658                         .ret_coercion
1659                         .as_ref()
1660                         .map(|c| self.resolve_vars_if_possible(c.borrow().expected_ty()))
1661                         && let ty::Adt(kind, _) = ret_ty.kind()
1662                         && tcx.get_diagnostic_item(diagnostic_item) == Some(kind.did())
1663                     {
1664                         true
1665                     } else {
1666                         false
1667                     }
1668                 };
1669
1670                 match &matching_variants[..] {
1671                     [(_, field, pick)] => {
1672                         let self_ty = field.ty(tcx, substs);
1673                         err.span_note(
1674                             tcx.def_span(pick.item.def_id),
1675                             &format!("the method `{item_name}` exists on the type `{self_ty}`"),
1676                         );
1677                         let (article, kind, variant, question) =
1678                             if tcx.is_diagnostic_item(sym::Result, kind.did()) {
1679                                 ("a", "Result", "Err", ret_ty_matches(sym::Result))
1680                             } else if tcx.is_diagnostic_item(sym::Option, kind.did()) {
1681                                 ("an", "Option", "None", ret_ty_matches(sym::Option))
1682                             } else {
1683                                 return;
1684                             };
1685                         if question {
1686                             err.span_suggestion_verbose(
1687                                 expr.span.shrink_to_hi(),
1688                                 format!(
1689                                     "use the `?` operator to extract the `{self_ty}` value, propagating \
1690                                     {article} `{kind}::{variant}` value to the caller"
1691                                 ),
1692                                 "?",
1693                                 Applicability::MachineApplicable,
1694                             );
1695                         } else {
1696                             err.span_suggestion_verbose(
1697                                 expr.span.shrink_to_hi(),
1698                                 format!(
1699                                     "consider using `{kind}::expect` to unwrap the `{self_ty}` value, \
1700                                     panicking if the value is {article} `{kind}::{variant}`"
1701                                 ),
1702                                 ".expect(\"REASON\")",
1703                                 Applicability::HasPlaceholders,
1704                             );
1705                         }
1706                     }
1707                     // FIXME(compiler-errors): Support suggestions for other matching enum variants
1708                     _ => {}
1709                 }
1710             }
1711             // Target wrapper types - types that wrap or pretend to wrap another type,
1712             // perhaps this inner type is meant to be called?
1713             ty::AdtKind::Struct | ty::AdtKind::Union => {
1714                 let [first] = ***substs else { return; };
1715                 let ty::GenericArgKind::Type(ty) = first.unpack() else { return; };
1716                 let Ok(pick) = self.lookup_probe(
1717                     item_name,
1718                     ty,
1719                     call_expr,
1720                     ProbeScope::TraitsInScope,
1721                 )  else { return; };
1722
1723                 let name = self.ty_to_value_string(actual);
1724                 let inner_id = kind.did();
1725                 let mutable = if let Some(AutorefOrPtrAdjustment::Autoref { mutbl, .. }) =
1726                     pick.autoref_or_ptr_adjustment
1727                 {
1728                     Some(mutbl)
1729                 } else {
1730                     None
1731                 };
1732
1733                 if tcx.is_diagnostic_item(sym::LocalKey, inner_id) {
1734                     err.help("use `with` or `try_with` to access thread local storage");
1735                 } else if Some(kind.did()) == tcx.lang_items().maybe_uninit() {
1736                     err.help(format!(
1737                         "if this `{name}` has been initialized, \
1738                         use one of the `assume_init` methods to access the inner value"
1739                     ));
1740                 } else if tcx.is_diagnostic_item(sym::RefCell, inner_id) {
1741                     let (suggestion, borrow_kind, panic_if) = match mutable {
1742                         Some(Mutability::Not) => (".borrow()", "borrow", "a mutable borrow exists"),
1743                         Some(Mutability::Mut) => {
1744                             (".borrow_mut()", "mutably borrow", "any borrows exist")
1745                         }
1746                         None => return,
1747                     };
1748                     err.span_suggestion_verbose(
1749                         expr.span.shrink_to_hi(),
1750                         format!(
1751                             "use `{suggestion}` to {borrow_kind} the `{ty}`, \
1752                             panicking if {panic_if}"
1753                         ),
1754                         suggestion,
1755                         Applicability::MaybeIncorrect,
1756                     );
1757                 } else if tcx.is_diagnostic_item(sym::Mutex, inner_id) {
1758                     err.span_suggestion_verbose(
1759                         expr.span.shrink_to_hi(),
1760                         format!(
1761                             "use `.lock().unwrap()` to borrow the `{ty}`, \
1762                             blocking the current thread until it can be acquired"
1763                         ),
1764                         ".lock().unwrap()",
1765                         Applicability::MaybeIncorrect,
1766                     );
1767                 } else if tcx.is_diagnostic_item(sym::RwLock, inner_id) {
1768                     let (suggestion, borrow_kind) = match mutable {
1769                         Some(Mutability::Not) => (".read().unwrap()", "borrow"),
1770                         Some(Mutability::Mut) => (".write().unwrap()", "mutably borrow"),
1771                         None => return,
1772                     };
1773                     err.span_suggestion_verbose(
1774                         expr.span.shrink_to_hi(),
1775                         format!(
1776                             "use `{suggestion}` to {borrow_kind} the `{ty}`, \
1777                             blocking the current thread until it can be acquired"
1778                         ),
1779                         suggestion,
1780                         Applicability::MaybeIncorrect,
1781                     );
1782                 } else {
1783                     return;
1784                 };
1785
1786                 err.span_note(
1787                     tcx.def_span(pick.item.def_id),
1788                     &format!("the method `{item_name}` exists on the type `{ty}`"),
1789                 );
1790             }
1791         }
1792     }
1793
1794     pub(crate) fn note_unmet_impls_on_type(
1795         &self,
1796         err: &mut Diagnostic,
1797         errors: Vec<FulfillmentError<'tcx>>,
1798     ) {
1799         let all_local_types_needing_impls =
1800             errors.iter().all(|e| match e.obligation.predicate.kind().skip_binder() {
1801                 ty::PredicateKind::Clause(ty::Clause::Trait(pred)) => match pred.self_ty().kind() {
1802                     ty::Adt(def, _) => def.did().is_local(),
1803                     _ => false,
1804                 },
1805                 _ => false,
1806             });
1807         let mut preds: Vec<_> = errors
1808             .iter()
1809             .filter_map(|e| match e.obligation.predicate.kind().skip_binder() {
1810                 ty::PredicateKind::Clause(ty::Clause::Trait(pred)) => Some(pred),
1811                 _ => None,
1812             })
1813             .collect();
1814         preds.sort_by_key(|pred| (pred.def_id(), pred.self_ty()));
1815         let def_ids = preds
1816             .iter()
1817             .filter_map(|pred| match pred.self_ty().kind() {
1818                 ty::Adt(def, _) => Some(def.did()),
1819                 _ => None,
1820             })
1821             .collect::<FxHashSet<_>>();
1822         let mut spans: MultiSpan = def_ids
1823             .iter()
1824             .filter_map(|def_id| {
1825                 let span = self.tcx.def_span(*def_id);
1826                 if span.is_dummy() { None } else { Some(span) }
1827             })
1828             .collect::<Vec<_>>()
1829             .into();
1830
1831         for pred in &preds {
1832             match pred.self_ty().kind() {
1833                 ty::Adt(def, _) if def.did().is_local() => {
1834                     spans.push_span_label(
1835                         self.tcx.def_span(def.did()),
1836                         format!("must implement `{}`", pred.trait_ref.print_only_trait_path()),
1837                     );
1838                 }
1839                 _ => {}
1840             }
1841         }
1842
1843         if all_local_types_needing_impls && spans.primary_span().is_some() {
1844             let msg = if preds.len() == 1 {
1845                 format!(
1846                     "an implementation of `{}` might be missing for `{}`",
1847                     preds[0].trait_ref.print_only_trait_path(),
1848                     preds[0].self_ty()
1849                 )
1850             } else {
1851                 format!(
1852                     "the following type{} would have to `impl` {} required trait{} for this \
1853                      operation to be valid",
1854                     pluralize!(def_ids.len()),
1855                     if def_ids.len() == 1 { "its" } else { "their" },
1856                     pluralize!(preds.len()),
1857                 )
1858             };
1859             err.span_note(spans, &msg);
1860         }
1861
1862         let preds: Vec<_> = errors
1863             .iter()
1864             .map(|e| (e.obligation.predicate, None, Some(e.obligation.cause.clone())))
1865             .collect();
1866         self.suggest_derive(err, &preds);
1867     }
1868
1869     pub fn suggest_derive(
1870         &self,
1871         err: &mut Diagnostic,
1872         unsatisfied_predicates: &[(
1873             ty::Predicate<'tcx>,
1874             Option<ty::Predicate<'tcx>>,
1875             Option<ObligationCause<'tcx>>,
1876         )],
1877     ) {
1878         let mut derives = Vec::<(String, Span, Symbol)>::new();
1879         let mut traits = Vec::new();
1880         for (pred, _, _) in unsatisfied_predicates {
1881             let ty::PredicateKind::Clause(ty::Clause::Trait(trait_pred)) = pred.kind().skip_binder() else { continue };
1882             let adt = match trait_pred.self_ty().ty_adt_def() {
1883                 Some(adt) if adt.did().is_local() => adt,
1884                 _ => continue,
1885             };
1886             if let Some(diagnostic_name) = self.tcx.get_diagnostic_name(trait_pred.def_id()) {
1887                 let can_derive = match diagnostic_name {
1888                     sym::Default => !adt.is_enum(),
1889                     sym::Eq
1890                     | sym::PartialEq
1891                     | sym::Ord
1892                     | sym::PartialOrd
1893                     | sym::Clone
1894                     | sym::Copy
1895                     | sym::Hash
1896                     | sym::Debug => true,
1897                     _ => false,
1898                 };
1899                 if can_derive {
1900                     let self_name = trait_pred.self_ty().to_string();
1901                     let self_span = self.tcx.def_span(adt.did());
1902                     if let Some(poly_trait_ref) = pred.to_opt_poly_trait_pred() {
1903                         for super_trait in supertraits(self.tcx, poly_trait_ref.to_poly_trait_ref())
1904                         {
1905                             if let Some(parent_diagnostic_name) =
1906                                 self.tcx.get_diagnostic_name(super_trait.def_id())
1907                             {
1908                                 derives.push((
1909                                     self_name.clone(),
1910                                     self_span,
1911                                     parent_diagnostic_name,
1912                                 ));
1913                             }
1914                         }
1915                     }
1916                     derives.push((self_name, self_span, diagnostic_name));
1917                 } else {
1918                     traits.push(trait_pred.def_id());
1919                 }
1920             } else {
1921                 traits.push(trait_pred.def_id());
1922             }
1923         }
1924         traits.sort();
1925         traits.dedup();
1926
1927         derives.sort();
1928         derives.dedup();
1929
1930         let mut derives_grouped = Vec::<(String, Span, String)>::new();
1931         for (self_name, self_span, trait_name) in derives.into_iter() {
1932             if let Some((last_self_name, _, ref mut last_trait_names)) = derives_grouped.last_mut()
1933             {
1934                 if last_self_name == &self_name {
1935                     last_trait_names.push_str(format!(", {}", trait_name).as_str());
1936                     continue;
1937                 }
1938             }
1939             derives_grouped.push((self_name, self_span, trait_name.to_string()));
1940         }
1941
1942         let len = traits.len();
1943         if len > 0 {
1944             let span =
1945                 MultiSpan::from_spans(traits.iter().map(|&did| self.tcx.def_span(did)).collect());
1946             let mut names = format!("`{}`", self.tcx.def_path_str(traits[0]));
1947             for (i, &did) in traits.iter().enumerate().skip(1) {
1948                 if len > 2 {
1949                     names.push_str(", ");
1950                 }
1951                 if i == len - 1 {
1952                     names.push_str(" and ");
1953                 }
1954                 names.push('`');
1955                 names.push_str(&self.tcx.def_path_str(did));
1956                 names.push('`');
1957             }
1958             err.span_note(
1959                 span,
1960                 &format!("the trait{} {} must be implemented", pluralize!(len), names),
1961             );
1962         }
1963
1964         for (self_name, self_span, traits) in &derives_grouped {
1965             err.span_suggestion_verbose(
1966                 self_span.shrink_to_lo(),
1967                 &format!("consider annotating `{}` with `#[derive({})]`", self_name, traits),
1968                 format!("#[derive({})]\n", traits),
1969                 Applicability::MaybeIncorrect,
1970             );
1971         }
1972     }
1973
1974     fn check_for_deref_method(
1975         &self,
1976         err: &mut Diagnostic,
1977         self_source: SelfSource<'tcx>,
1978         rcvr_ty: Ty<'tcx>,
1979         item_name: Ident,
1980     ) {
1981         let SelfSource::QPath(ty) = self_source else { return; };
1982         for (deref_ty, _) in self.autoderef(rustc_span::DUMMY_SP, rcvr_ty).skip(1) {
1983             if let Ok(pick) = self.probe_for_name(
1984                 Mode::Path,
1985                 item_name,
1986                 IsSuggestion(true),
1987                 deref_ty,
1988                 ty.hir_id,
1989                 ProbeScope::TraitsInScope,
1990             ) {
1991                 if deref_ty.is_suggestable(self.tcx, true)
1992                     // If this method receives `&self`, then the provided
1993                     // argument _should_ coerce, so it's valid to suggest
1994                     // just changing the path.
1995                     && pick.item.fn_has_self_parameter
1996                     && let Some(self_ty) =
1997                         self.tcx.fn_sig(pick.item.def_id).inputs().skip_binder().get(0)
1998                     && self_ty.is_ref()
1999                 {
2000                     let suggested_path = match deref_ty.kind() {
2001                         ty::Bool
2002                         | ty::Char
2003                         | ty::Int(_)
2004                         | ty::Uint(_)
2005                         | ty::Float(_)
2006                         | ty::Adt(_, _)
2007                         | ty::Str
2008                         | ty::Alias(ty::Projection, _)
2009                         | ty::Param(_) => format!("{deref_ty}"),
2010                         // we need to test something like  <&[_]>::len or <(&[u32])>::len
2011                         // and Vec::function();
2012                         // <&[_]>::len or <&[u32]>::len doesn't need an extra "<>" between
2013                         // but for Adt type like Vec::function()
2014                         // we would suggest <[_]>::function();
2015                         _ if self.tcx.sess.source_map().span_wrapped_by_angle_or_parentheses(ty.span)  => format!("{deref_ty}"),
2016                         _ => format!("<{deref_ty}>"),
2017                     };
2018                     err.span_suggestion_verbose(
2019                         ty.span,
2020                         format!("the function `{item_name}` is implemented on `{deref_ty}`"),
2021                         suggested_path,
2022                         Applicability::MaybeIncorrect,
2023                     );
2024                 } else {
2025                     err.span_note(
2026                         ty.span,
2027                         format!("the function `{item_name}` is implemented on `{deref_ty}`"),
2028                     );
2029                 }
2030                 return;
2031             }
2032         }
2033     }
2034
2035     /// Print out the type for use in value namespace.
2036     fn ty_to_value_string(&self, ty: Ty<'tcx>) -> String {
2037         match ty.kind() {
2038             ty::Adt(def, substs) => self.tcx.def_path_str_with_substs(def.did(), substs),
2039             _ => self.ty_to_string(ty),
2040         }
2041     }
2042
2043     fn suggest_await_before_method(
2044         &self,
2045         err: &mut Diagnostic,
2046         item_name: Ident,
2047         ty: Ty<'tcx>,
2048         call: &hir::Expr<'_>,
2049         span: Span,
2050     ) {
2051         let output_ty = match self.get_impl_future_output_ty(ty) {
2052             Some(output_ty) => self.resolve_vars_if_possible(output_ty),
2053             _ => return,
2054         };
2055         let method_exists = self.method_exists(item_name, output_ty, call.hir_id, true);
2056         debug!("suggest_await_before_method: is_method_exist={}", method_exists);
2057         if method_exists {
2058             err.span_suggestion_verbose(
2059                 span.shrink_to_lo(),
2060                 "consider `await`ing on the `Future` and calling the method on its `Output`",
2061                 "await.",
2062                 Applicability::MaybeIncorrect,
2063             );
2064         }
2065     }
2066
2067     fn suggest_use_candidates(&self, err: &mut Diagnostic, msg: String, candidates: Vec<DefId>) {
2068         let parent_map = self.tcx.visible_parent_map(());
2069
2070         // Separate out candidates that must be imported with a glob, because they are named `_`
2071         // and cannot be referred with their identifier.
2072         let (candidates, globs): (Vec<_>, Vec<_>) = candidates.into_iter().partition(|trait_did| {
2073             if let Some(parent_did) = parent_map.get(trait_did) {
2074                 // If the item is re-exported as `_`, we should suggest a glob-import instead.
2075                 if *parent_did != self.tcx.parent(*trait_did)
2076                     && self
2077                         .tcx
2078                         .module_children(*parent_did)
2079                         .iter()
2080                         .filter(|child| child.res.opt_def_id() == Some(*trait_did))
2081                         .all(|child| child.ident.name == kw::Underscore)
2082                 {
2083                     return false;
2084                 }
2085             }
2086
2087             true
2088         });
2089
2090         let module_did = self.tcx.parent_module(self.body_id);
2091         let (module, _, _) = self.tcx.hir().get_module(module_did);
2092         let span = module.spans.inject_use_span;
2093
2094         let path_strings = candidates.iter().map(|trait_did| {
2095             format!("use {};\n", with_crate_prefix!(self.tcx.def_path_str(*trait_did)),)
2096         });
2097
2098         let glob_path_strings = globs.iter().map(|trait_did| {
2099             let parent_did = parent_map.get(trait_did).unwrap();
2100             format!(
2101                 "use {}::*; // trait {}\n",
2102                 with_crate_prefix!(self.tcx.def_path_str(*parent_did)),
2103                 self.tcx.item_name(*trait_did),
2104             )
2105         });
2106
2107         err.span_suggestions(
2108             span,
2109             &msg,
2110             path_strings.chain(glob_path_strings),
2111             Applicability::MaybeIncorrect,
2112         );
2113     }
2114
2115     fn suggest_valid_traits(
2116         &self,
2117         err: &mut Diagnostic,
2118         valid_out_of_scope_traits: Vec<DefId>,
2119     ) -> bool {
2120         if !valid_out_of_scope_traits.is_empty() {
2121             let mut candidates = valid_out_of_scope_traits;
2122             candidates.sort();
2123             candidates.dedup();
2124
2125             // `TryFrom` and `FromIterator` have no methods
2126             let edition_fix = candidates
2127                 .iter()
2128                 .find(|did| self.tcx.is_diagnostic_item(sym::TryInto, **did))
2129                 .copied();
2130
2131             err.help("items from traits can only be used if the trait is in scope");
2132             let msg = format!(
2133                 "the following {traits_are} implemented but not in scope; \
2134                  perhaps add a `use` for {one_of_them}:",
2135                 traits_are = if candidates.len() == 1 { "trait is" } else { "traits are" },
2136                 one_of_them = if candidates.len() == 1 { "it" } else { "one of them" },
2137             );
2138
2139             self.suggest_use_candidates(err, msg, candidates);
2140             if let Some(did) = edition_fix {
2141                 err.note(&format!(
2142                     "'{}' is included in the prelude starting in Edition 2021",
2143                     with_crate_prefix!(self.tcx.def_path_str(did))
2144                 ));
2145             }
2146
2147             true
2148         } else {
2149             false
2150         }
2151     }
2152
2153     fn suggest_traits_to_import(
2154         &self,
2155         err: &mut Diagnostic,
2156         span: Span,
2157         rcvr_ty: Ty<'tcx>,
2158         item_name: Ident,
2159         inputs_len: Option<usize>,
2160         source: SelfSource<'tcx>,
2161         valid_out_of_scope_traits: Vec<DefId>,
2162         unsatisfied_predicates: &[(
2163             ty::Predicate<'tcx>,
2164             Option<ty::Predicate<'tcx>>,
2165             Option<ObligationCause<'tcx>>,
2166         )],
2167         static_candidates: &[CandidateSource],
2168         unsatisfied_bounds: bool,
2169     ) {
2170         let mut alt_rcvr_sugg = false;
2171         if let (SelfSource::MethodCall(rcvr), false) = (source, unsatisfied_bounds) {
2172             debug!(
2173                 "suggest_traits_to_import: span={:?}, item_name={:?}, rcvr_ty={:?}, rcvr={:?}",
2174                 span, item_name, rcvr_ty, rcvr
2175             );
2176             let skippable = [
2177                 self.tcx.lang_items().clone_trait(),
2178                 self.tcx.lang_items().deref_trait(),
2179                 self.tcx.lang_items().deref_mut_trait(),
2180                 self.tcx.lang_items().drop_trait(),
2181                 self.tcx.get_diagnostic_item(sym::AsRef),
2182             ];
2183             // Try alternative arbitrary self types that could fulfill this call.
2184             // FIXME: probe for all types that *could* be arbitrary self-types, not
2185             // just this list.
2186             for (rcvr_ty, post) in &[
2187                 (rcvr_ty, ""),
2188                 (self.tcx.mk_mut_ref(self.tcx.lifetimes.re_erased, rcvr_ty), "&mut "),
2189                 (self.tcx.mk_imm_ref(self.tcx.lifetimes.re_erased, rcvr_ty), "&"),
2190             ] {
2191                 match self.lookup_probe(item_name, *rcvr_ty, rcvr, ProbeScope::AllTraits) {
2192                     Ok(pick) => {
2193                         // If the method is defined for the receiver we have, it likely wasn't `use`d.
2194                         // We point at the method, but we just skip the rest of the check for arbitrary
2195                         // self types and rely on the suggestion to `use` the trait from
2196                         // `suggest_valid_traits`.
2197                         let did = Some(pick.item.container_id(self.tcx));
2198                         let skip = skippable.contains(&did);
2199                         if pick.autoderefs == 0 && !skip {
2200                             err.span_label(
2201                                 pick.item.ident(self.tcx).span,
2202                                 &format!("the method is available for `{}` here", rcvr_ty),
2203                             );
2204                         }
2205                         break;
2206                     }
2207                     Err(MethodError::Ambiguity(_)) => {
2208                         // If the method is defined (but ambiguous) for the receiver we have, it is also
2209                         // likely we haven't `use`d it. It may be possible that if we `Box`/`Pin`/etc.
2210                         // the receiver, then it might disambiguate this method, but I think these
2211                         // suggestions are generally misleading (see #94218).
2212                         break;
2213                     }
2214                     Err(_) => (),
2215                 }
2216
2217                 for (rcvr_ty, pre) in &[
2218                     (self.tcx.mk_lang_item(*rcvr_ty, LangItem::OwnedBox), "Box::new"),
2219                     (self.tcx.mk_lang_item(*rcvr_ty, LangItem::Pin), "Pin::new"),
2220                     (self.tcx.mk_diagnostic_item(*rcvr_ty, sym::Arc), "Arc::new"),
2221                     (self.tcx.mk_diagnostic_item(*rcvr_ty, sym::Rc), "Rc::new"),
2222                 ] {
2223                     if let Some(new_rcvr_t) = *rcvr_ty
2224                         && let Ok(pick) = self.lookup_probe(
2225                             item_name,
2226                             new_rcvr_t,
2227                             rcvr,
2228                             ProbeScope::AllTraits,
2229                         )
2230                     {
2231                         debug!("try_alt_rcvr: pick candidate {:?}", pick);
2232                         let did = Some(pick.item.container_id(self.tcx));
2233                         // We don't want to suggest a container type when the missing
2234                         // method is `.clone()` or `.deref()` otherwise we'd suggest
2235                         // `Arc::new(foo).clone()`, which is far from what the user wants.
2236                         // Explicitly ignore the `Pin::as_ref()` method as `Pin` does not
2237                         // implement the `AsRef` trait.
2238                         let skip = skippable.contains(&did)
2239                             || (("Pin::new" == *pre) && (sym::as_ref == item_name.name))
2240                             || inputs_len.map_or(false, |inputs_len| pick.item.kind == ty::AssocKind::Fn && self.tcx.fn_sig(pick.item.def_id).skip_binder().inputs().len() != inputs_len);
2241                         // Make sure the method is defined for the *actual* receiver: we don't
2242                         // want to treat `Box<Self>` as a receiver if it only works because of
2243                         // an autoderef to `&self`
2244                         if pick.autoderefs == 0 && !skip {
2245                             err.span_label(
2246                                 pick.item.ident(self.tcx).span,
2247                                 &format!("the method is available for `{}` here", new_rcvr_t),
2248                             );
2249                             err.multipart_suggestion(
2250                                 "consider wrapping the receiver expression with the \
2251                                     appropriate type",
2252                                 vec![
2253                                     (rcvr.span.shrink_to_lo(), format!("{}({}", pre, post)),
2254                                     (rcvr.span.shrink_to_hi(), ")".to_string()),
2255                                 ],
2256                                 Applicability::MaybeIncorrect,
2257                             );
2258                             // We don't care about the other suggestions.
2259                             alt_rcvr_sugg = true;
2260                         }
2261                     }
2262                 }
2263             }
2264         }
2265         if self.suggest_valid_traits(err, valid_out_of_scope_traits) {
2266             return;
2267         }
2268
2269         let type_is_local = self.type_derefs_to_local(span, rcvr_ty, source);
2270
2271         let mut arbitrary_rcvr = vec![];
2272         // There are no traits implemented, so lets suggest some traits to
2273         // implement, by finding ones that have the item name, and are
2274         // legal to implement.
2275         let mut candidates = all_traits(self.tcx)
2276             .into_iter()
2277             // Don't issue suggestions for unstable traits since they're
2278             // unlikely to be implementable anyway
2279             .filter(|info| match self.tcx.lookup_stability(info.def_id) {
2280                 Some(attr) => attr.level.is_stable(),
2281                 None => true,
2282             })
2283             .filter(|info| {
2284                 // Static candidates are already implemented, and known not to work
2285                 // Do not suggest them again
2286                 static_candidates.iter().all(|sc| match *sc {
2287                     CandidateSource::Trait(def_id) => def_id != info.def_id,
2288                     CandidateSource::Impl(def_id) => {
2289                         self.tcx.trait_id_of_impl(def_id) != Some(info.def_id)
2290                     }
2291                 })
2292             })
2293             .filter(|info| {
2294                 // We approximate the coherence rules to only suggest
2295                 // traits that are legal to implement by requiring that
2296                 // either the type or trait is local. Multi-dispatch means
2297                 // this isn't perfect (that is, there are cases when
2298                 // implementing a trait would be legal but is rejected
2299                 // here).
2300                 unsatisfied_predicates.iter().all(|(p, _, _)| {
2301                     match p.kind().skip_binder() {
2302                         // Hide traits if they are present in predicates as they can be fixed without
2303                         // having to implement them.
2304                         ty::PredicateKind::Clause(ty::Clause::Trait(t)) => {
2305                             t.def_id() == info.def_id
2306                         }
2307                         ty::PredicateKind::Clause(ty::Clause::Projection(p)) => {
2308                             p.projection_ty.def_id == info.def_id
2309                         }
2310                         _ => false,
2311                     }
2312                 }) && (type_is_local || info.def_id.is_local())
2313                     && !self.tcx.trait_is_auto(info.def_id)
2314                     && self
2315                         .associated_value(info.def_id, item_name)
2316                         .filter(|item| {
2317                             if let ty::AssocKind::Fn = item.kind {
2318                                 let id = item
2319                                     .def_id
2320                                     .as_local()
2321                                     .map(|def_id| self.tcx.hir().local_def_id_to_hir_id(def_id));
2322                                 if let Some(hir::Node::TraitItem(hir::TraitItem {
2323                                     kind: hir::TraitItemKind::Fn(fn_sig, method),
2324                                     ..
2325                                 })) = id.map(|id| self.tcx.hir().get(id))
2326                                 {
2327                                     let self_first_arg = match method {
2328                                         hir::TraitFn::Required([ident, ..]) => {
2329                                             ident.name == kw::SelfLower
2330                                         }
2331                                         hir::TraitFn::Provided(body_id) => {
2332                                             self.tcx.hir().body(*body_id).params.first().map_or(
2333                                                 false,
2334                                                 |param| {
2335                                                     matches!(
2336                                                         param.pat.kind,
2337                                                         hir::PatKind::Binding(_, _, ident, _)
2338                                                             if ident.name == kw::SelfLower
2339                                                     )
2340                                                 },
2341                                             )
2342                                         }
2343                                         _ => false,
2344                                     };
2345
2346                                     if !fn_sig.decl.implicit_self.has_implicit_self()
2347                                         && self_first_arg
2348                                     {
2349                                         if let Some(ty) = fn_sig.decl.inputs.get(0) {
2350                                             arbitrary_rcvr.push(ty.span);
2351                                         }
2352                                         return false;
2353                                     }
2354                                 }
2355                             }
2356                             // We only want to suggest public or local traits (#45781).
2357                             item.visibility(self.tcx).is_public() || info.def_id.is_local()
2358                         })
2359                         .is_some()
2360             })
2361             .collect::<Vec<_>>();
2362         for span in &arbitrary_rcvr {
2363             err.span_label(
2364                 *span,
2365                 "the method might not be found because of this arbitrary self type",
2366             );
2367         }
2368         if alt_rcvr_sugg {
2369             return;
2370         }
2371
2372         if !candidates.is_empty() {
2373             // Sort from most relevant to least relevant.
2374             candidates.sort_by(|a, b| a.cmp(b).reverse());
2375             candidates.dedup();
2376
2377             let param_type = match rcvr_ty.kind() {
2378                 ty::Param(param) => Some(param),
2379                 ty::Ref(_, ty, _) => match ty.kind() {
2380                     ty::Param(param) => Some(param),
2381                     _ => None,
2382                 },
2383                 _ => None,
2384             };
2385             err.help(if param_type.is_some() {
2386                 "items from traits can only be used if the type parameter is bounded by the trait"
2387             } else {
2388                 "items from traits can only be used if the trait is implemented and in scope"
2389             });
2390             let candidates_len = candidates.len();
2391             let message = |action| {
2392                 format!(
2393                     "the following {traits_define} an item `{name}`, perhaps you need to {action} \
2394                      {one_of_them}:",
2395                     traits_define =
2396                         if candidates_len == 1 { "trait defines" } else { "traits define" },
2397                     action = action,
2398                     one_of_them = if candidates_len == 1 { "it" } else { "one of them" },
2399                     name = item_name,
2400                 )
2401             };
2402             // Obtain the span for `param` and use it for a structured suggestion.
2403             if let Some(param) = param_type {
2404                 let generics = self.tcx.generics_of(self.body_id.owner.to_def_id());
2405                 let type_param = generics.type_param(param, self.tcx);
2406                 let hir = self.tcx.hir();
2407                 if let Some(def_id) = type_param.def_id.as_local() {
2408                     let id = hir.local_def_id_to_hir_id(def_id);
2409                     // Get the `hir::Param` to verify whether it already has any bounds.
2410                     // We do this to avoid suggesting code that ends up as `T: FooBar`,
2411                     // instead we suggest `T: Foo + Bar` in that case.
2412                     match hir.get(id) {
2413                         Node::GenericParam(param) => {
2414                             enum Introducer {
2415                                 Plus,
2416                                 Colon,
2417                                 Nothing,
2418                             }
2419                             let ast_generics = hir.get_generics(id.owner.def_id).unwrap();
2420                             let (sp, mut introducer) = if let Some(span) =
2421                                 ast_generics.bounds_span_for_suggestions(def_id)
2422                             {
2423                                 (span, Introducer::Plus)
2424                             } else if let Some(colon_span) = param.colon_span {
2425                                 (colon_span.shrink_to_hi(), Introducer::Nothing)
2426                             } else {
2427                                 (param.span.shrink_to_hi(), Introducer::Colon)
2428                             };
2429                             if matches!(
2430                                 param.kind,
2431                                 hir::GenericParamKind::Type { synthetic: true, .. },
2432                             ) {
2433                                 introducer = Introducer::Plus
2434                             }
2435                             let trait_def_ids: FxHashSet<DefId> = ast_generics
2436                                 .bounds_for_param(def_id)
2437                                 .flat_map(|bp| bp.bounds.iter())
2438                                 .filter_map(|bound| bound.trait_ref()?.trait_def_id())
2439                                 .collect();
2440                             if !candidates.iter().any(|t| trait_def_ids.contains(&t.def_id)) {
2441                                 err.span_suggestions(
2442                                     sp,
2443                                     &message(format!(
2444                                         "restrict type parameter `{}` with",
2445                                         param.name.ident(),
2446                                     )),
2447                                     candidates.iter().map(|t| {
2448                                         format!(
2449                                             "{} {}",
2450                                             match introducer {
2451                                                 Introducer::Plus => " +",
2452                                                 Introducer::Colon => ":",
2453                                                 Introducer::Nothing => "",
2454                                             },
2455                                             self.tcx.def_path_str(t.def_id),
2456                                         )
2457                                     }),
2458                                     Applicability::MaybeIncorrect,
2459                                 );
2460                             }
2461                             return;
2462                         }
2463                         Node::Item(hir::Item {
2464                             kind: hir::ItemKind::Trait(.., bounds, _),
2465                             ident,
2466                             ..
2467                         }) => {
2468                             let (sp, sep, article) = if bounds.is_empty() {
2469                                 (ident.span.shrink_to_hi(), ":", "a")
2470                             } else {
2471                                 (bounds.last().unwrap().span().shrink_to_hi(), " +", "another")
2472                             };
2473                             err.span_suggestions(
2474                                 sp,
2475                                 &message(format!("add {} supertrait for", article)),
2476                                 candidates.iter().map(|t| {
2477                                     format!("{} {}", sep, self.tcx.def_path_str(t.def_id),)
2478                                 }),
2479                                 Applicability::MaybeIncorrect,
2480                             );
2481                             return;
2482                         }
2483                         _ => {}
2484                     }
2485                 }
2486             }
2487
2488             let (potential_candidates, explicitly_negative) = if param_type.is_some() {
2489                 // FIXME: Even though negative bounds are not implemented, we could maybe handle
2490                 // cases where a positive bound implies a negative impl.
2491                 (candidates, Vec::new())
2492             } else if let Some(simp_rcvr_ty) =
2493                 simplify_type(self.tcx, rcvr_ty, TreatParams::AsPlaceholder)
2494             {
2495                 let mut potential_candidates = Vec::new();
2496                 let mut explicitly_negative = Vec::new();
2497                 for candidate in candidates {
2498                     // Check if there's a negative impl of `candidate` for `rcvr_ty`
2499                     if self
2500                         .tcx
2501                         .all_impls(candidate.def_id)
2502                         .filter(|imp_did| {
2503                             self.tcx.impl_polarity(*imp_did) == ty::ImplPolarity::Negative
2504                         })
2505                         .any(|imp_did| {
2506                             let imp = self.tcx.impl_trait_ref(imp_did).unwrap();
2507                             let imp_simp =
2508                                 simplify_type(self.tcx, imp.self_ty(), TreatParams::AsPlaceholder);
2509                             imp_simp.map_or(false, |s| s == simp_rcvr_ty)
2510                         })
2511                     {
2512                         explicitly_negative.push(candidate);
2513                     } else {
2514                         potential_candidates.push(candidate);
2515                     }
2516                 }
2517                 (potential_candidates, explicitly_negative)
2518             } else {
2519                 // We don't know enough about `recv_ty` to make proper suggestions.
2520                 (candidates, Vec::new())
2521             };
2522
2523             let action = if let Some(param) = param_type {
2524                 format!("restrict type parameter `{}` with", param)
2525             } else {
2526                 // FIXME: it might only need to be imported into scope, not implemented.
2527                 "implement".to_string()
2528             };
2529             match &potential_candidates[..] {
2530                 [] => {}
2531                 [trait_info] if trait_info.def_id.is_local() => {
2532                     err.span_note(
2533                         self.tcx.def_span(trait_info.def_id),
2534                         &format!(
2535                             "`{}` defines an item `{}`, perhaps you need to {} it",
2536                             self.tcx.def_path_str(trait_info.def_id),
2537                             item_name,
2538                             action
2539                         ),
2540                     );
2541                 }
2542                 trait_infos => {
2543                     let mut msg = message(action);
2544                     for (i, trait_info) in trait_infos.iter().enumerate() {
2545                         msg.push_str(&format!(
2546                             "\ncandidate #{}: `{}`",
2547                             i + 1,
2548                             self.tcx.def_path_str(trait_info.def_id),
2549                         ));
2550                     }
2551                     err.note(&msg);
2552                 }
2553             }
2554             match &explicitly_negative[..] {
2555                 [] => {}
2556                 [trait_info] => {
2557                     let msg = format!(
2558                         "the trait `{}` defines an item `{}`, but is explicitly unimplemented",
2559                         self.tcx.def_path_str(trait_info.def_id),
2560                         item_name
2561                     );
2562                     err.note(&msg);
2563                 }
2564                 trait_infos => {
2565                     let mut msg = format!(
2566                         "the following traits define an item `{}`, but are explicitly unimplemented:",
2567                         item_name
2568                     );
2569                     for trait_info in trait_infos {
2570                         msg.push_str(&format!("\n{}", self.tcx.def_path_str(trait_info.def_id)));
2571                     }
2572                     err.note(&msg);
2573                 }
2574             }
2575         }
2576     }
2577
2578     /// issue #102320, for `unwrap_or` with closure as argument, suggest `unwrap_or_else`
2579     /// FIXME: currently not working for suggesting `map_or_else`, see #102408
2580     pub(crate) fn suggest_else_fn_with_closure(
2581         &self,
2582         err: &mut Diagnostic,
2583         expr: &hir::Expr<'_>,
2584         found: Ty<'tcx>,
2585         expected: Ty<'tcx>,
2586     ) -> bool {
2587         let Some((_def_id_or_name, output, _inputs)) = self.extract_callable_info(expr, found)
2588         else { return false; };
2589
2590         if !self.can_coerce(output, expected) {
2591             return false;
2592         }
2593
2594         let parent = self.tcx.hir().parent_id(expr.hir_id);
2595         if  let Some(Node::Expr(call_expr)) = self.tcx.hir().find(parent) &&
2596             let hir::ExprKind::MethodCall(
2597                 hir::PathSegment { ident: method_name, .. },
2598                 self_expr,
2599                 args,
2600                 ..,
2601              ) = call_expr.kind &&
2602             let Some(self_ty) = self.typeck_results.borrow().expr_ty_opt(self_expr) {
2603             let new_name = Ident {
2604                 name: Symbol::intern(&format!("{}_else", method_name.as_str())),
2605                 span: method_name.span,
2606             };
2607             let probe = self.lookup_probe(
2608                 new_name,
2609                 self_ty,
2610                 self_expr,
2611                 ProbeScope::TraitsInScope,
2612             );
2613
2614             // check the method arguments number
2615             if let Ok(pick) = probe &&
2616                 let fn_sig = self.tcx.fn_sig(pick.item.def_id) &&
2617                 let fn_args = fn_sig.skip_binder().inputs() &&
2618                 fn_args.len() == args.len() + 1 {
2619                 err.span_suggestion_verbose(
2620                     method_name.span.shrink_to_hi(),
2621                     &format!("try calling `{}` instead", new_name.name.as_str()),
2622                     "_else",
2623                     Applicability::MaybeIncorrect,
2624                 );
2625                 return true;
2626             }
2627         }
2628         false
2629     }
2630
2631     /// Checks whether there is a local type somewhere in the chain of
2632     /// autoderefs of `rcvr_ty`.
2633     fn type_derefs_to_local(
2634         &self,
2635         span: Span,
2636         rcvr_ty: Ty<'tcx>,
2637         source: SelfSource<'tcx>,
2638     ) -> bool {
2639         fn is_local(ty: Ty<'_>) -> bool {
2640             match ty.kind() {
2641                 ty::Adt(def, _) => def.did().is_local(),
2642                 ty::Foreign(did) => did.is_local(),
2643                 ty::Dynamic(tr, ..) => tr.principal().map_or(false, |d| d.def_id().is_local()),
2644                 ty::Param(_) => true,
2645
2646                 // Everything else (primitive types, etc.) is effectively
2647                 // non-local (there are "edge" cases, e.g., `(LocalType,)`, but
2648                 // the noise from these sort of types is usually just really
2649                 // annoying, rather than any sort of help).
2650                 _ => false,
2651             }
2652         }
2653
2654         // This occurs for UFCS desugaring of `T::method`, where there is no
2655         // receiver expression for the method call, and thus no autoderef.
2656         if let SelfSource::QPath(_) = source {
2657             return is_local(self.resolve_vars_with_obligations(rcvr_ty));
2658         }
2659
2660         self.autoderef(span, rcvr_ty).any(|(ty, _)| is_local(ty))
2661     }
2662 }
2663
2664 #[derive(Copy, Clone, Debug)]
2665 pub enum SelfSource<'a> {
2666     QPath(&'a hir::Ty<'a>),
2667     MethodCall(&'a hir::Expr<'a> /* rcvr */),
2668 }
2669
2670 #[derive(Copy, Clone)]
2671 pub struct TraitInfo {
2672     pub def_id: DefId,
2673 }
2674
2675 impl PartialEq for TraitInfo {
2676     fn eq(&self, other: &TraitInfo) -> bool {
2677         self.cmp(other) == Ordering::Equal
2678     }
2679 }
2680 impl Eq for TraitInfo {}
2681 impl PartialOrd for TraitInfo {
2682     fn partial_cmp(&self, other: &TraitInfo) -> Option<Ordering> {
2683         Some(self.cmp(other))
2684     }
2685 }
2686 impl Ord for TraitInfo {
2687     fn cmp(&self, other: &TraitInfo) -> Ordering {
2688         // Local crates are more important than remote ones (local:
2689         // `cnum == 0`), and otherwise we throw in the defid for totality.
2690
2691         let lhs = (other.def_id.krate, other.def_id);
2692         let rhs = (self.def_id.krate, self.def_id);
2693         lhs.cmp(&rhs)
2694     }
2695 }
2696
2697 /// Retrieves all traits in this crate and any dependent crates,
2698 /// and wraps them into `TraitInfo` for custom sorting.
2699 pub fn all_traits(tcx: TyCtxt<'_>) -> Vec<TraitInfo> {
2700     tcx.all_traits().map(|def_id| TraitInfo { def_id }).collect()
2701 }
2702
2703 fn print_disambiguation_help<'tcx>(
2704     item_name: Ident,
2705     args: Option<(&'tcx hir::Expr<'tcx>, &'tcx [hir::Expr<'tcx>])>,
2706     err: &mut Diagnostic,
2707     trait_name: String,
2708     rcvr_ty: Ty<'_>,
2709     kind: ty::AssocKind,
2710     def_id: DefId,
2711     span: Span,
2712     candidate: Option<usize>,
2713     source_map: &source_map::SourceMap,
2714     fn_has_self_parameter: bool,
2715 ) {
2716     let mut applicability = Applicability::MachineApplicable;
2717     let (span, sugg) = if let (ty::AssocKind::Fn, Some((receiver, args))) = (kind, args) {
2718         let args = format!(
2719             "({}{})",
2720             rcvr_ty.ref_mutability().map_or("", |mutbl| mutbl.ref_prefix_str()),
2721             std::iter::once(receiver)
2722                 .chain(args.iter())
2723                 .map(|arg| source_map.span_to_snippet(arg.span).unwrap_or_else(|_| {
2724                     applicability = Applicability::HasPlaceholders;
2725                     "_".to_owned()
2726                 }))
2727                 .collect::<Vec<_>>()
2728                 .join(", "),
2729         );
2730         let trait_name = if !fn_has_self_parameter {
2731             format!("<{} as {}>", rcvr_ty, trait_name)
2732         } else {
2733             trait_name
2734         };
2735         (span, format!("{}::{}{}", trait_name, item_name, args))
2736     } else {
2737         (span.with_hi(item_name.span.lo()), format!("<{} as {}>::", rcvr_ty, trait_name))
2738     };
2739     err.span_suggestion_verbose(
2740         span,
2741         &format!(
2742             "disambiguate the {} for {}",
2743             kind.as_def_kind().descr(def_id),
2744             if let Some(candidate) = candidate {
2745                 format!("candidate #{}", candidate)
2746             } else {
2747                 "the candidate".to_string()
2748             },
2749         ),
2750         sugg,
2751         applicability,
2752     );
2753 }