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