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