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