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