1use std::assert_matches::debug_assert_matches;
4use std::borrow::Cow;
5use std::iter;
6use std::path::PathBuf;
7
8use itertools::{EitherOrBoth, Itertools};
9use rustc_abi::ExternAbi;
10use rustc_data_structures::fx::FxHashSet;
11use rustc_data_structures::stack::ensure_sufficient_stack;
12use rustc_errors::codes::*;
13use rustc_errors::{
14 Applicability, Diag, EmissionGuarantee, MultiSpan, Style, SuggestionStyle, pluralize,
15 struct_span_code_err,
16};
17use rustc_hir::def::{CtorOf, DefKind, Res};
18use rustc_hir::def_id::DefId;
19use rustc_hir::intravisit::{Visitor, VisitorExt};
20use rustc_hir::lang_items::LangItem;
21use rustc_hir::{
22 self as hir, AmbigArg, CoroutineDesugaring, CoroutineKind, CoroutineSource, Expr, HirId, Node,
23 expr_needs_parens,
24};
25use rustc_infer::infer::{BoundRegionConversionTime, DefineOpaqueTypes, InferCtxt, InferOk};
26use rustc_middle::middle::privacy::Level;
27use rustc_middle::traits::IsConstable;
28use rustc_middle::ty::error::TypeError;
29use rustc_middle::ty::print::{
30 PrintPolyTraitPredicateExt as _, PrintPolyTraitRefExt, PrintTraitPredicateExt as _,
31 with_forced_trimmed_paths, with_no_trimmed_paths, with_types_for_suggestion,
32};
33use rustc_middle::ty::{
34 self, AdtKind, GenericArgs, InferTy, IsSuggestable, Ty, TyCtxt, TypeFoldable, TypeFolder,
35 TypeSuperFoldable, TypeSuperVisitable, TypeVisitableExt, TypeVisitor, TypeckResults, Upcast,
36 suggest_arbitrary_trait_bound, suggest_constraining_type_param,
37};
38use rustc_middle::{bug, span_bug};
39use rustc_span::def_id::LocalDefId;
40use rustc_span::{
41 BytePos, DUMMY_SP, DesugaringKind, ExpnKind, Ident, MacroKind, Span, Symbol, kw, sym,
42};
43use tracing::{debug, instrument};
44
45use super::{
46 DefIdOrName, FindExprBySpan, ImplCandidate, Obligation, ObligationCause, ObligationCauseCode,
47 PredicateObligation,
48};
49use crate::error_reporting::TypeErrCtxt;
50use crate::errors;
51use crate::infer::InferCtxtExt as _;
52use crate::traits::query::evaluate_obligation::InferCtxtExt as _;
53use crate::traits::{ImplDerivedCause, NormalizeExt, ObligationCtxt};
54
55#[derive(Debug)]
56pub enum CoroutineInteriorOrUpvar {
57 Interior(Span, Option<(Span, Option<Span>)>),
59 Upvar(Span),
61}
62
63#[derive(Debug)]
66struct CoroutineData<'a, 'tcx>(&'a TypeckResults<'tcx>);
67
68impl<'a, 'tcx> CoroutineData<'a, 'tcx> {
69 fn try_get_upvar_span<F>(
73 &self,
74 infer_context: &InferCtxt<'tcx>,
75 coroutine_did: DefId,
76 ty_matches: F,
77 ) -> Option<CoroutineInteriorOrUpvar>
78 where
79 F: Fn(ty::Binder<'tcx, Ty<'tcx>>) -> bool,
80 {
81 infer_context.tcx.upvars_mentioned(coroutine_did).and_then(|upvars| {
82 upvars.iter().find_map(|(upvar_id, upvar)| {
83 let upvar_ty = self.0.node_type(*upvar_id);
84 let upvar_ty = infer_context.resolve_vars_if_possible(upvar_ty);
85 ty_matches(ty::Binder::dummy(upvar_ty))
86 .then(|| CoroutineInteriorOrUpvar::Upvar(upvar.span))
87 })
88 })
89 }
90
91 fn get_from_await_ty<F>(
95 &self,
96 visitor: AwaitsVisitor,
97 tcx: TyCtxt<'tcx>,
98 ty_matches: F,
99 ) -> Option<Span>
100 where
101 F: Fn(ty::Binder<'tcx, Ty<'tcx>>) -> bool,
102 {
103 visitor
104 .awaits
105 .into_iter()
106 .map(|id| tcx.hir_expect_expr(id))
107 .find(|await_expr| ty_matches(ty::Binder::dummy(self.0.expr_ty_adjusted(await_expr))))
108 .map(|expr| expr.span)
109 }
110}
111
112fn predicate_constraint(generics: &hir::Generics<'_>, pred: ty::Predicate<'_>) -> (Span, String) {
113 (
114 generics.tail_span_for_predicate_suggestion(),
115 with_types_for_suggestion!(format!("{} {}", generics.add_where_or_trailing_comma(), pred)),
116 )
117}
118
119pub fn suggest_restriction<'tcx, G: EmissionGuarantee>(
123 tcx: TyCtxt<'tcx>,
124 item_id: LocalDefId,
125 hir_generics: &hir::Generics<'tcx>,
126 msg: &str,
127 err: &mut Diag<'_, G>,
128 fn_sig: Option<&hir::FnSig<'_>>,
129 projection: Option<ty::AliasTy<'_>>,
130 trait_pred: ty::PolyTraitPredicate<'tcx>,
131 super_traits: Option<(&Ident, &hir::GenericBounds<'_>)>,
137) {
138 if hir_generics.where_clause_span.from_expansion()
139 || hir_generics.where_clause_span.desugaring_kind().is_some()
140 || projection.is_some_and(|projection| {
141 (tcx.is_impl_trait_in_trait(projection.def_id)
142 && !tcx.features().return_type_notation())
143 || tcx.lookup_stability(projection.def_id).is_some_and(|stab| stab.is_unstable())
144 })
145 {
146 return;
147 }
148 let generics = tcx.generics_of(item_id);
149 if let Some((param, bound_str, fn_sig)) =
151 fn_sig.zip(projection).and_then(|(sig, p)| match *p.self_ty().kind() {
152 ty::Param(param) => {
154 let param_def = generics.type_param(param, tcx);
155 if param_def.kind.is_synthetic() {
156 let bound_str =
157 param_def.name.as_str().strip_prefix("impl ")?.trim_start().to_string();
158 return Some((param_def, bound_str, sig));
159 }
160 None
161 }
162 _ => None,
163 })
164 {
165 let type_param_name = hir_generics.params.next_type_param_name(Some(&bound_str));
166 let trait_pred = trait_pred.fold_with(&mut ReplaceImplTraitFolder {
167 tcx,
168 param,
169 replace_ty: ty::ParamTy::new(generics.count() as u32, Symbol::intern(&type_param_name))
170 .to_ty(tcx),
171 });
172 if !trait_pred.is_suggestable(tcx, false) {
173 return;
174 }
175 let mut ty_spans = vec![];
183 for input in fn_sig.decl.inputs {
184 ReplaceImplTraitVisitor { ty_spans: &mut ty_spans, param_did: param.def_id }
185 .visit_ty_unambig(input);
186 }
187 let type_param = format!("{type_param_name}: {bound_str}");
189
190 let mut sugg = vec![
191 if let Some(span) = hir_generics.span_for_param_suggestion() {
192 (span, format!(", {type_param}"))
193 } else {
194 (hir_generics.span, format!("<{type_param}>"))
195 },
196 predicate_constraint(hir_generics, trait_pred.upcast(tcx)),
199 ];
200 sugg.extend(ty_spans.into_iter().map(|s| (s, type_param_name.to_string())));
201
202 err.multipart_suggestion(
205 "introduce a type parameter with a trait bound instead of using `impl Trait`",
206 sugg,
207 Applicability::MaybeIncorrect,
208 );
209 } else {
210 if !trait_pred.is_suggestable(tcx, false) {
211 return;
212 }
213 let (sp, suggestion) = match (
215 hir_generics
216 .params
217 .iter()
218 .find(|p| !matches!(p.kind, hir::GenericParamKind::Type { synthetic: true, .. })),
219 super_traits,
220 ) {
221 (_, None) => predicate_constraint(hir_generics, trait_pred.upcast(tcx)),
222 (None, Some((ident, []))) => (
223 ident.span.shrink_to_hi(),
224 format!(": {}", trait_pred.print_modifiers_and_trait_path()),
225 ),
226 (_, Some((_, [.., bounds]))) => (
227 bounds.span().shrink_to_hi(),
228 format!(" + {}", trait_pred.print_modifiers_and_trait_path()),
229 ),
230 (Some(_), Some((_, []))) => (
231 hir_generics.span.shrink_to_hi(),
232 format!(": {}", trait_pred.print_modifiers_and_trait_path()),
233 ),
234 };
235
236 err.span_suggestion_verbose(
237 sp,
238 format!("consider further restricting {msg}"),
239 suggestion,
240 Applicability::MachineApplicable,
241 );
242 }
243}
244
245impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
246 pub fn suggest_restricting_param_bound(
247 &self,
248 err: &mut Diag<'_>,
249 trait_pred: ty::PolyTraitPredicate<'tcx>,
250 associated_ty: Option<(&'static str, Ty<'tcx>)>,
251 mut body_id: LocalDefId,
252 ) {
253 if trait_pred.skip_binder().polarity != ty::PredicatePolarity::Positive {
254 return;
255 }
256
257 let trait_pred = self.resolve_numeric_literals_with_default(trait_pred);
258
259 let self_ty = trait_pred.skip_binder().self_ty();
260 let (param_ty, projection) = match *self_ty.kind() {
261 ty::Param(_) => (true, None),
262 ty::Alias(ty::Projection, projection) => (false, Some(projection)),
263 _ => (false, None),
264 };
265
266 let mut finder = ParamFinder { .. };
267 finder.visit_binder(&trait_pred);
268
269 loop {
272 let node = self.tcx.hir_node_by_def_id(body_id);
273 match node {
274 hir::Node::Item(hir::Item {
275 kind: hir::ItemKind::Trait(_, _, _, ident, generics, bounds, _),
276 ..
277 }) if self_ty == self.tcx.types.self_param => {
278 assert!(param_ty);
279 suggest_restriction(
281 self.tcx,
282 body_id,
283 generics,
284 "`Self`",
285 err,
286 None,
287 projection,
288 trait_pred,
289 Some((&ident, bounds)),
290 );
291 return;
292 }
293
294 hir::Node::TraitItem(hir::TraitItem {
295 generics,
296 kind: hir::TraitItemKind::Fn(..),
297 ..
298 }) if self_ty == self.tcx.types.self_param => {
299 assert!(param_ty);
300 suggest_restriction(
302 self.tcx, body_id, generics, "`Self`", err, None, projection, trait_pred,
303 None,
304 );
305 return;
306 }
307
308 hir::Node::TraitItem(hir::TraitItem {
309 generics,
310 kind: hir::TraitItemKind::Fn(fn_sig, ..),
311 ..
312 })
313 | hir::Node::ImplItem(hir::ImplItem {
314 generics,
315 kind: hir::ImplItemKind::Fn(fn_sig, ..),
316 ..
317 })
318 | hir::Node::Item(hir::Item {
319 kind: hir::ItemKind::Fn { sig: fn_sig, generics, .. },
320 ..
321 }) if projection.is_some() => {
322 suggest_restriction(
324 self.tcx,
325 body_id,
326 generics,
327 "the associated type",
328 err,
329 Some(fn_sig),
330 projection,
331 trait_pred,
332 None,
333 );
334 return;
335 }
336 hir::Node::Item(hir::Item {
337 kind:
338 hir::ItemKind::Trait(_, _, _, _, generics, ..)
339 | hir::ItemKind::Impl(hir::Impl { generics, .. }),
340 ..
341 }) if projection.is_some() => {
342 suggest_restriction(
344 self.tcx,
345 body_id,
346 generics,
347 "the associated type",
348 err,
349 None,
350 projection,
351 trait_pred,
352 None,
353 );
354 return;
355 }
356
357 hir::Node::Item(hir::Item {
358 kind:
359 hir::ItemKind::Struct(_, generics, _)
360 | hir::ItemKind::Enum(_, generics, _)
361 | hir::ItemKind::Union(_, generics, _)
362 | hir::ItemKind::Trait(_, _, _, _, generics, ..)
363 | hir::ItemKind::Impl(hir::Impl { generics, .. })
364 | hir::ItemKind::Fn { generics, .. }
365 | hir::ItemKind::TyAlias(_, generics, _)
366 | hir::ItemKind::Const(_, generics, _, _)
367 | hir::ItemKind::TraitAlias(_, _, generics, _),
368 ..
369 })
370 | hir::Node::TraitItem(hir::TraitItem { generics, .. })
371 | hir::Node::ImplItem(hir::ImplItem { generics, .. })
372 if param_ty =>
373 {
374 if !trait_pred.skip_binder().trait_ref.args[1..]
383 .iter()
384 .all(|g| g.is_suggestable(self.tcx, false))
385 {
386 return;
387 }
388 let param_name = self_ty.to_string();
390 let mut constraint = with_no_trimmed_paths!(
391 trait_pred.print_modifiers_and_trait_path().to_string()
392 );
393
394 if let Some((name, term)) = associated_ty {
395 if let Some(stripped) = constraint.strip_suffix('>') {
398 constraint = format!("{stripped}, {name} = {term}>");
399 } else {
400 constraint.push_str(&format!("<{name} = {term}>"));
401 }
402 }
403
404 if suggest_constraining_type_param(
405 self.tcx,
406 generics,
407 err,
408 ¶m_name,
409 &constraint,
410 Some(trait_pred.def_id()),
411 None,
412 ) {
413 return;
414 }
415 }
416
417 hir::Node::TraitItem(hir::TraitItem {
418 generics,
419 kind: hir::TraitItemKind::Fn(..),
420 ..
421 })
422 | hir::Node::ImplItem(hir::ImplItem {
423 generics,
424 impl_kind: hir::ImplItemImplKind::Inherent { .. },
425 kind: hir::ImplItemKind::Fn(..),
426 ..
427 }) if finder.can_suggest_bound(generics) => {
428 suggest_arbitrary_trait_bound(
430 self.tcx,
431 generics,
432 err,
433 trait_pred,
434 associated_ty,
435 );
436 }
437 hir::Node::Item(hir::Item {
438 kind:
439 hir::ItemKind::Struct(_, generics, _)
440 | hir::ItemKind::Enum(_, generics, _)
441 | hir::ItemKind::Union(_, generics, _)
442 | hir::ItemKind::Trait(_, _, _, _, generics, ..)
443 | hir::ItemKind::Impl(hir::Impl { generics, .. })
444 | hir::ItemKind::Fn { generics, .. }
445 | hir::ItemKind::TyAlias(_, generics, _)
446 | hir::ItemKind::Const(_, generics, _, _)
447 | hir::ItemKind::TraitAlias(_, _, generics, _),
448 ..
449 }) if finder.can_suggest_bound(generics) => {
450 if suggest_arbitrary_trait_bound(
452 self.tcx,
453 generics,
454 err,
455 trait_pred,
456 associated_ty,
457 ) {
458 return;
459 }
460 }
461 hir::Node::Crate(..) => return,
462
463 _ => {}
464 }
465 body_id = self.tcx.local_parent(body_id);
466 }
467 }
468
469 pub(super) fn suggest_dereferences(
472 &self,
473 obligation: &PredicateObligation<'tcx>,
474 err: &mut Diag<'_>,
475 trait_pred: ty::PolyTraitPredicate<'tcx>,
476 ) -> bool {
477 let mut code = obligation.cause.code();
478 if let ObligationCauseCode::FunctionArg { arg_hir_id, call_hir_id, .. } = code
479 && let Some(typeck_results) = &self.typeck_results
480 && let hir::Node::Expr(expr) = self.tcx.hir_node(*arg_hir_id)
481 && let Some(arg_ty) = typeck_results.expr_ty_adjusted_opt(expr)
482 {
483 let mut real_trait_pred = trait_pred;
487 while let Some((parent_code, parent_trait_pred)) = code.parent_with_predicate() {
488 code = parent_code;
489 if let Some(parent_trait_pred) = parent_trait_pred {
490 real_trait_pred = parent_trait_pred;
491 }
492 }
493
494 let real_ty = self.tcx.instantiate_bound_regions_with_erased(real_trait_pred.self_ty());
497 if !self.can_eq(obligation.param_env, real_ty, arg_ty) {
498 return false;
499 }
500
501 let (is_under_ref, base_ty, span) = match expr.kind {
508 hir::ExprKind::AddrOf(hir::BorrowKind::Ref, hir::Mutability::Not, subexpr)
509 if let &ty::Ref(region, base_ty, hir::Mutability::Not) = real_ty.kind() =>
510 {
511 (Some(region), base_ty, subexpr.span)
512 }
513 hir::ExprKind::AddrOf(..) => return false,
515 _ => (None, real_ty, obligation.cause.span),
516 };
517
518 let autoderef = (self.autoderef_steps)(base_ty);
519 let mut is_boxed = base_ty.is_box();
520 if let Some(steps) = autoderef.into_iter().position(|(mut ty, obligations)| {
521 let can_deref = is_under_ref.is_some()
524 || self.type_is_copy_modulo_regions(obligation.param_env, ty)
525 || ty.is_numeric() || is_boxed && self.type_is_sized_modulo_regions(obligation.param_env, ty);
527 is_boxed &= ty.is_box();
528
529 if let Some(region) = is_under_ref {
531 ty = Ty::new_ref(self.tcx, region, ty, hir::Mutability::Not);
532 }
533
534 let real_trait_pred_and_ty =
536 real_trait_pred.map_bound(|inner_trait_pred| (inner_trait_pred, ty));
537 let obligation = self.mk_trait_obligation_with_new_self_ty(
538 obligation.param_env,
539 real_trait_pred_and_ty,
540 );
541
542 can_deref
543 && obligations
544 .iter()
545 .chain([&obligation])
546 .all(|obligation| self.predicate_may_hold(obligation))
547 }) && steps > 0
548 {
549 let derefs = "*".repeat(steps);
550 let msg = "consider dereferencing here";
551 let call_node = self.tcx.hir_node(*call_hir_id);
552 let is_receiver = matches!(
553 call_node,
554 Node::Expr(hir::Expr {
555 kind: hir::ExprKind::MethodCall(_, receiver_expr, ..),
556 ..
557 })
558 if receiver_expr.hir_id == *arg_hir_id
559 );
560 if is_receiver {
561 err.multipart_suggestion_verbose(
562 msg,
563 vec![
564 (span.shrink_to_lo(), format!("({derefs}")),
565 (span.shrink_to_hi(), ")".to_string()),
566 ],
567 Applicability::MachineApplicable,
568 )
569 } else {
570 err.span_suggestion_verbose(
571 span.shrink_to_lo(),
572 msg,
573 derefs,
574 Applicability::MachineApplicable,
575 )
576 };
577 return true;
578 }
579 } else if let (
580 ObligationCauseCode::BinOp { lhs_hir_id, rhs_hir_id, .. },
581 predicate,
582 ) = code.peel_derives_with_predicate()
583 && let Some(typeck_results) = &self.typeck_results
584 && let hir::Node::Expr(lhs) = self.tcx.hir_node(*lhs_hir_id)
585 && let hir::Node::Expr(rhs) = self.tcx.hir_node(*rhs_hir_id)
586 && let Some(rhs_ty) = typeck_results.expr_ty_opt(rhs)
587 && let trait_pred = predicate.unwrap_or(trait_pred)
588 && hir::lang_items::BINARY_OPERATORS
590 .iter()
591 .filter_map(|&op| self.tcx.lang_items().get(op))
592 .any(|op| {
593 op == trait_pred.skip_binder().trait_ref.def_id
594 })
595 {
596 let trait_pred = predicate.unwrap_or(trait_pred);
599 let lhs_ty = self.tcx.instantiate_bound_regions_with_erased(trait_pred.self_ty());
600 let lhs_autoderef = (self.autoderef_steps)(lhs_ty);
601 let rhs_autoderef = (self.autoderef_steps)(rhs_ty);
602 let first_lhs = lhs_autoderef.first().unwrap().clone();
603 let first_rhs = rhs_autoderef.first().unwrap().clone();
604 let mut autoderefs = lhs_autoderef
605 .into_iter()
606 .enumerate()
607 .rev()
608 .zip_longest(rhs_autoderef.into_iter().enumerate().rev())
609 .map(|t| match t {
610 EitherOrBoth::Both(a, b) => (a, b),
611 EitherOrBoth::Left(a) => (a, (0, first_rhs.clone())),
612 EitherOrBoth::Right(b) => ((0, first_lhs.clone()), b),
613 })
614 .rev();
615 if let Some((lsteps, rsteps)) =
616 autoderefs.find_map(|((lsteps, (l_ty, _)), (rsteps, (r_ty, _)))| {
617 let trait_pred_and_ty = trait_pred.map_bound(|inner| {
621 (
622 ty::TraitPredicate {
623 trait_ref: ty::TraitRef::new_from_args(
624 self.tcx,
625 inner.trait_ref.def_id,
626 self.tcx.mk_args(
627 &[&[l_ty.into(), r_ty.into()], &inner.trait_ref.args[2..]]
628 .concat(),
629 ),
630 ),
631 ..inner
632 },
633 l_ty,
634 )
635 });
636 let obligation = self.mk_trait_obligation_with_new_self_ty(
637 obligation.param_env,
638 trait_pred_and_ty,
639 );
640 self.predicate_may_hold(&obligation).then_some(match (lsteps, rsteps) {
641 (_, 0) => (Some(lsteps), None),
642 (0, _) => (None, Some(rsteps)),
643 _ => (Some(lsteps), Some(rsteps)),
644 })
645 })
646 {
647 let make_sugg = |mut expr: &Expr<'_>, mut steps| {
648 let mut prefix_span = expr.span.shrink_to_lo();
649 let mut msg = "consider dereferencing here";
650 if let hir::ExprKind::AddrOf(_, _, inner) = expr.kind {
651 msg = "consider removing the borrow and dereferencing instead";
652 if let hir::ExprKind::AddrOf(..) = inner.kind {
653 msg = "consider removing the borrows and dereferencing instead";
654 }
655 }
656 while let hir::ExprKind::AddrOf(_, _, inner) = expr.kind
657 && steps > 0
658 {
659 prefix_span = prefix_span.with_hi(inner.span.lo());
660 expr = inner;
661 steps -= 1;
662 }
663 if steps == 0 {
665 return (
666 msg.trim_end_matches(" and dereferencing instead"),
667 vec![(prefix_span, String::new())],
668 );
669 }
670 let derefs = "*".repeat(steps);
671 let needs_parens = steps > 0 && expr_needs_parens(expr);
672 let mut suggestion = if needs_parens {
673 vec![
674 (
675 expr.span.with_lo(prefix_span.hi()).shrink_to_lo(),
676 format!("{derefs}("),
677 ),
678 (expr.span.shrink_to_hi(), ")".to_string()),
679 ]
680 } else {
681 vec![(
682 expr.span.with_lo(prefix_span.hi()).shrink_to_lo(),
683 format!("{derefs}"),
684 )]
685 };
686 if !prefix_span.is_empty() {
688 suggestion.push((prefix_span, String::new()));
689 }
690 (msg, suggestion)
691 };
692
693 if let Some(lsteps) = lsteps
694 && let Some(rsteps) = rsteps
695 && lsteps > 0
696 && rsteps > 0
697 {
698 let mut suggestion = make_sugg(lhs, lsteps).1;
699 suggestion.append(&mut make_sugg(rhs, rsteps).1);
700 err.multipart_suggestion_verbose(
701 "consider dereferencing both sides of the expression",
702 suggestion,
703 Applicability::MachineApplicable,
704 );
705 return true;
706 } else if let Some(lsteps) = lsteps
707 && lsteps > 0
708 {
709 let (msg, suggestion) = make_sugg(lhs, lsteps);
710 err.multipart_suggestion_verbose(
711 msg,
712 suggestion,
713 Applicability::MachineApplicable,
714 );
715 return true;
716 } else if let Some(rsteps) = rsteps
717 && rsteps > 0
718 {
719 let (msg, suggestion) = make_sugg(rhs, rsteps);
720 err.multipart_suggestion_verbose(
721 msg,
722 suggestion,
723 Applicability::MachineApplicable,
724 );
725 return true;
726 }
727 }
728 }
729 false
730 }
731
732 fn get_closure_name(
736 &self,
737 def_id: DefId,
738 err: &mut Diag<'_>,
739 msg: Cow<'static, str>,
740 ) -> Option<Symbol> {
741 let get_name = |err: &mut Diag<'_>, kind: &hir::PatKind<'_>| -> Option<Symbol> {
742 match &kind {
745 hir::PatKind::Binding(hir::BindingMode::NONE, _, ident, None) => Some(ident.name),
746 _ => {
747 err.note(msg);
748 None
749 }
750 }
751 };
752
753 let hir_id = self.tcx.local_def_id_to_hir_id(def_id.as_local()?);
754 match self.tcx.parent_hir_node(hir_id) {
755 hir::Node::Stmt(hir::Stmt { kind: hir::StmtKind::Let(local), .. }) => {
756 get_name(err, &local.pat.kind)
757 }
758 hir::Node::LetStmt(local) => get_name(err, &local.pat.kind),
761 _ => None,
762 }
763 }
764
765 pub(super) fn suggest_fn_call(
769 &self,
770 obligation: &PredicateObligation<'tcx>,
771 err: &mut Diag<'_>,
772 trait_pred: ty::PolyTraitPredicate<'tcx>,
773 ) -> bool {
774 if self.typeck_results.is_none() {
777 return false;
778 }
779
780 if let ty::PredicateKind::Clause(ty::ClauseKind::Trait(trait_pred)) =
781 obligation.predicate.kind().skip_binder()
782 && self.tcx.is_lang_item(trait_pred.def_id(), LangItem::Sized)
783 {
784 return false;
786 }
787
788 let self_ty = self.instantiate_binder_with_fresh_vars(
789 DUMMY_SP,
790 BoundRegionConversionTime::FnCall,
791 trait_pred.self_ty(),
792 );
793
794 let Some((def_id_or_name, output, inputs)) =
795 self.extract_callable_info(obligation.cause.body_id, obligation.param_env, self_ty)
796 else {
797 return false;
798 };
799
800 let trait_pred_and_self = trait_pred.map_bound(|trait_pred| (trait_pred, output));
802
803 let new_obligation =
804 self.mk_trait_obligation_with_new_self_ty(obligation.param_env, trait_pred_and_self);
805 if !self.predicate_must_hold_modulo_regions(&new_obligation) {
806 return false;
807 }
808
809 let msg = match def_id_or_name {
811 DefIdOrName::DefId(def_id) => match self.tcx.def_kind(def_id) {
812 DefKind::Ctor(CtorOf::Struct, _) => {
813 Cow::from("use parentheses to construct this tuple struct")
814 }
815 DefKind::Ctor(CtorOf::Variant, _) => {
816 Cow::from("use parentheses to construct this tuple variant")
817 }
818 kind => Cow::from(format!(
819 "use parentheses to call this {}",
820 self.tcx.def_kind_descr(kind, def_id)
821 )),
822 },
823 DefIdOrName::Name(name) => Cow::from(format!("use parentheses to call this {name}")),
824 };
825
826 let args = inputs
827 .into_iter()
828 .map(|ty| {
829 if ty.is_suggestable(self.tcx, false) {
830 format!("/* {ty} */")
831 } else {
832 "/* value */".to_string()
833 }
834 })
835 .collect::<Vec<_>>()
836 .join(", ");
837
838 if matches!(obligation.cause.code(), ObligationCauseCode::FunctionArg { .. })
839 && obligation.cause.span.can_be_used_for_suggestions()
840 {
841 let (span, sugg) = if let Some(snippet) =
842 self.tcx.sess.source_map().span_to_snippet(obligation.cause.span).ok()
843 && snippet.starts_with("|")
844 {
845 (obligation.cause.span, format!("({snippet})({args})"))
846 } else {
847 (obligation.cause.span.shrink_to_hi(), format!("({args})"))
848 };
849
850 err.span_suggestion_verbose(span, msg, sugg, Applicability::HasPlaceholders);
855 } else if let DefIdOrName::DefId(def_id) = def_id_or_name {
856 let name = match self.tcx.hir_get_if_local(def_id) {
857 Some(hir::Node::Expr(hir::Expr {
858 kind: hir::ExprKind::Closure(hir::Closure { fn_decl_span, .. }),
859 ..
860 })) => {
861 err.span_label(*fn_decl_span, "consider calling this closure");
862 let Some(name) = self.get_closure_name(def_id, err, msg.clone()) else {
863 return false;
864 };
865 name.to_string()
866 }
867 Some(hir::Node::Item(hir::Item {
868 kind: hir::ItemKind::Fn { ident, .. }, ..
869 })) => {
870 err.span_label(ident.span, "consider calling this function");
871 ident.to_string()
872 }
873 Some(hir::Node::Ctor(..)) => {
874 let name = self.tcx.def_path_str(def_id);
875 err.span_label(
876 self.tcx.def_span(def_id),
877 format!("consider calling the constructor for `{name}`"),
878 );
879 name
880 }
881 _ => return false,
882 };
883 err.help(format!("{msg}: `{name}({args})`"));
884 }
885 true
886 }
887
888 pub(super) fn check_for_binding_assigned_block_without_tail_expression(
889 &self,
890 obligation: &PredicateObligation<'tcx>,
891 err: &mut Diag<'_>,
892 trait_pred: ty::PolyTraitPredicate<'tcx>,
893 ) {
894 let mut span = obligation.cause.span;
895 while span.from_expansion() {
896 span.remove_mark();
898 }
899 let mut expr_finder = FindExprBySpan::new(span, self.tcx);
900 let Some(body) = self.tcx.hir_maybe_body_owned_by(obligation.cause.body_id) else {
901 return;
902 };
903 expr_finder.visit_expr(body.value);
904 let Some(expr) = expr_finder.result else {
905 return;
906 };
907 let Some(typeck) = &self.typeck_results else {
908 return;
909 };
910 let Some(ty) = typeck.expr_ty_adjusted_opt(expr) else {
911 return;
912 };
913 if !ty.is_unit() {
914 return;
915 };
916 let hir::ExprKind::Path(hir::QPath::Resolved(None, path)) = expr.kind else {
917 return;
918 };
919 let Res::Local(hir_id) = path.res else {
920 return;
921 };
922 let hir::Node::Pat(pat) = self.tcx.hir_node(hir_id) else {
923 return;
924 };
925 let hir::Node::LetStmt(hir::LetStmt { ty: None, init: Some(init), .. }) =
926 self.tcx.parent_hir_node(pat.hir_id)
927 else {
928 return;
929 };
930 let hir::ExprKind::Block(block, None) = init.kind else {
931 return;
932 };
933 if block.expr.is_some() {
934 return;
935 }
936 let [.., stmt] = block.stmts else {
937 err.span_label(block.span, "this empty block is missing a tail expression");
938 return;
939 };
940 if stmt.span.from_expansion() {
943 return;
944 }
945 let hir::StmtKind::Semi(tail_expr) = stmt.kind else {
946 return;
947 };
948 let Some(ty) = typeck.expr_ty_opt(tail_expr) else {
949 err.span_label(block.span, "this block is missing a tail expression");
950 return;
951 };
952 let ty = self.resolve_numeric_literals_with_default(self.resolve_vars_if_possible(ty));
953 let trait_pred_and_self = trait_pred.map_bound(|trait_pred| (trait_pred, ty));
954
955 let new_obligation =
956 self.mk_trait_obligation_with_new_self_ty(obligation.param_env, trait_pred_and_self);
957 if self.predicate_must_hold_modulo_regions(&new_obligation) {
958 err.span_suggestion_short(
959 stmt.span.with_lo(tail_expr.span.hi()),
960 "remove this semicolon",
961 "",
962 Applicability::MachineApplicable,
963 );
964 } else {
965 err.span_label(block.span, "this block is missing a tail expression");
966 }
967 }
968
969 pub(super) fn suggest_add_clone_to_arg(
970 &self,
971 obligation: &PredicateObligation<'tcx>,
972 err: &mut Diag<'_>,
973 trait_pred: ty::PolyTraitPredicate<'tcx>,
974 ) -> bool {
975 let self_ty = self.resolve_vars_if_possible(trait_pred.self_ty());
976 self.enter_forall(self_ty, |ty: Ty<'_>| {
977 let Some(generics) = self.tcx.hir_get_generics(obligation.cause.body_id) else {
978 return false;
979 };
980 let ty::Ref(_, inner_ty, hir::Mutability::Not) = ty.kind() else { return false };
981 let ty::Param(param) = inner_ty.kind() else { return false };
982 let ObligationCauseCode::FunctionArg { arg_hir_id, .. } = obligation.cause.code()
983 else {
984 return false;
985 };
986
987 let clone_trait = self.tcx.require_lang_item(LangItem::Clone, obligation.cause.span);
988 let has_clone = |ty| {
989 self.type_implements_trait(clone_trait, [ty], obligation.param_env)
990 .must_apply_modulo_regions()
991 };
992
993 let existing_clone_call = match self.tcx.hir_node(*arg_hir_id) {
994 Node::Expr(Expr { kind: hir::ExprKind::Path(_), .. }) => None,
996 Node::Expr(Expr {
999 kind:
1000 hir::ExprKind::MethodCall(
1001 hir::PathSegment { ident, .. },
1002 _receiver,
1003 [],
1004 call_span,
1005 ),
1006 hir_id,
1007 ..
1008 }) if ident.name == sym::clone
1009 && !call_span.from_expansion()
1010 && !has_clone(*inner_ty) =>
1011 {
1012 let Some(typeck_results) = self.typeck_results.as_ref() else { return false };
1014 let Some((DefKind::AssocFn, did)) = typeck_results.type_dependent_def(*hir_id)
1015 else {
1016 return false;
1017 };
1018 if self.tcx.trait_of_assoc(did) != Some(clone_trait) {
1019 return false;
1020 }
1021 Some(ident.span)
1022 }
1023 _ => return false,
1024 };
1025
1026 let new_obligation = self.mk_trait_obligation_with_new_self_ty(
1027 obligation.param_env,
1028 trait_pred.map_bound(|trait_pred| (trait_pred, *inner_ty)),
1029 );
1030
1031 if self.predicate_may_hold(&new_obligation) && has_clone(ty) {
1032 if !has_clone(param.to_ty(self.tcx)) {
1033 suggest_constraining_type_param(
1034 self.tcx,
1035 generics,
1036 err,
1037 param.name.as_str(),
1038 "Clone",
1039 Some(clone_trait),
1040 None,
1041 );
1042 }
1043 if let Some(existing_clone_call) = existing_clone_call {
1044 err.span_note(
1045 existing_clone_call,
1046 format!(
1047 "this `clone()` copies the reference, \
1048 which does not do anything, \
1049 because `{inner_ty}` does not implement `Clone`"
1050 ),
1051 );
1052 } else {
1053 err.span_suggestion_verbose(
1054 obligation.cause.span.shrink_to_hi(),
1055 "consider using clone here",
1056 ".clone()".to_string(),
1057 Applicability::MaybeIncorrect,
1058 );
1059 }
1060 return true;
1061 }
1062 false
1063 })
1064 }
1065
1066 pub fn extract_callable_info(
1070 &self,
1071 body_id: LocalDefId,
1072 param_env: ty::ParamEnv<'tcx>,
1073 found: Ty<'tcx>,
1074 ) -> Option<(DefIdOrName, Ty<'tcx>, Vec<Ty<'tcx>>)> {
1075 let Some((def_id_or_name, output, inputs)) =
1077 (self.autoderef_steps)(found).into_iter().find_map(|(found, _)| match *found.kind() {
1078 ty::FnPtr(sig_tys, _) => Some((
1079 DefIdOrName::Name("function pointer"),
1080 sig_tys.output(),
1081 sig_tys.inputs(),
1082 )),
1083 ty::FnDef(def_id, _) => {
1084 let fn_sig = found.fn_sig(self.tcx);
1085 Some((DefIdOrName::DefId(def_id), fn_sig.output(), fn_sig.inputs()))
1086 }
1087 ty::Closure(def_id, args) => {
1088 let fn_sig = args.as_closure().sig();
1089 Some((
1090 DefIdOrName::DefId(def_id),
1091 fn_sig.output(),
1092 fn_sig.inputs().map_bound(|inputs| inputs[0].tuple_fields().as_slice()),
1093 ))
1094 }
1095 ty::CoroutineClosure(def_id, args) => {
1096 let sig_parts = args.as_coroutine_closure().coroutine_closure_sig();
1097 Some((
1098 DefIdOrName::DefId(def_id),
1099 sig_parts.map_bound(|sig| {
1100 sig.to_coroutine(
1101 self.tcx,
1102 args.as_coroutine_closure().parent_args(),
1103 self.next_ty_var(DUMMY_SP),
1106 self.tcx.coroutine_for_closure(def_id),
1107 self.next_ty_var(DUMMY_SP),
1108 )
1109 }),
1110 sig_parts.map_bound(|sig| sig.tupled_inputs_ty.tuple_fields().as_slice()),
1111 ))
1112 }
1113 ty::Alias(ty::Opaque, ty::AliasTy { def_id, args, .. }) => {
1114 self.tcx.item_self_bounds(def_id).instantiate(self.tcx, args).iter().find_map(
1115 |pred| {
1116 if let ty::ClauseKind::Projection(proj) = pred.kind().skip_binder()
1117 && self
1118 .tcx
1119 .is_lang_item(proj.projection_term.def_id, LangItem::FnOnceOutput)
1120 && let ty::Tuple(args) = proj.projection_term.args.type_at(1).kind()
1122 {
1123 Some((
1124 DefIdOrName::DefId(def_id),
1125 pred.kind().rebind(proj.term.expect_type()),
1126 pred.kind().rebind(args.as_slice()),
1127 ))
1128 } else {
1129 None
1130 }
1131 },
1132 )
1133 }
1134 ty::Dynamic(data, _) => data.iter().find_map(|pred| {
1135 if let ty::ExistentialPredicate::Projection(proj) = pred.skip_binder()
1136 && self.tcx.is_lang_item(proj.def_id, LangItem::FnOnceOutput)
1137 && let ty::Tuple(args) = proj.args.type_at(0).kind()
1139 {
1140 Some((
1141 DefIdOrName::Name("trait object"),
1142 pred.rebind(proj.term.expect_type()),
1143 pred.rebind(args.as_slice()),
1144 ))
1145 } else {
1146 None
1147 }
1148 }),
1149 ty::Param(param) => {
1150 let generics = self.tcx.generics_of(body_id);
1151 let name = if generics.count() > param.index as usize
1152 && let def = generics.param_at(param.index as usize, self.tcx)
1153 && matches!(def.kind, ty::GenericParamDefKind::Type { .. })
1154 && def.name == param.name
1155 {
1156 DefIdOrName::DefId(def.def_id)
1157 } else {
1158 DefIdOrName::Name("type parameter")
1159 };
1160 param_env.caller_bounds().iter().find_map(|pred| {
1161 if let ty::ClauseKind::Projection(proj) = pred.kind().skip_binder()
1162 && self
1163 .tcx
1164 .is_lang_item(proj.projection_term.def_id, LangItem::FnOnceOutput)
1165 && proj.projection_term.self_ty() == found
1166 && let ty::Tuple(args) = proj.projection_term.args.type_at(1).kind()
1168 {
1169 Some((
1170 name,
1171 pred.kind().rebind(proj.term.expect_type()),
1172 pred.kind().rebind(args.as_slice()),
1173 ))
1174 } else {
1175 None
1176 }
1177 })
1178 }
1179 _ => None,
1180 })
1181 else {
1182 return None;
1183 };
1184
1185 let output = self.instantiate_binder_with_fresh_vars(
1186 DUMMY_SP,
1187 BoundRegionConversionTime::FnCall,
1188 output,
1189 );
1190 let inputs = inputs
1191 .skip_binder()
1192 .iter()
1193 .map(|ty| {
1194 self.instantiate_binder_with_fresh_vars(
1195 DUMMY_SP,
1196 BoundRegionConversionTime::FnCall,
1197 inputs.rebind(*ty),
1198 )
1199 })
1200 .collect();
1201
1202 let InferOk { value: output, obligations: _ } =
1206 self.at(&ObligationCause::dummy(), param_env).normalize(output);
1207
1208 if output.is_ty_var() { None } else { Some((def_id_or_name, output, inputs)) }
1209 }
1210
1211 pub(super) fn suggest_add_reference_to_arg(
1212 &self,
1213 obligation: &PredicateObligation<'tcx>,
1214 err: &mut Diag<'_>,
1215 poly_trait_pred: ty::PolyTraitPredicate<'tcx>,
1216 has_custom_message: bool,
1217 ) -> bool {
1218 let span = obligation.cause.span;
1219 let param_env = obligation.param_env;
1220
1221 let mk_result = |trait_pred_and_new_ty| {
1222 let obligation =
1223 self.mk_trait_obligation_with_new_self_ty(param_env, trait_pred_and_new_ty);
1224 self.predicate_must_hold_modulo_regions(&obligation)
1225 };
1226
1227 let code = match obligation.cause.code() {
1228 ObligationCauseCode::FunctionArg { parent_code, .. } => parent_code,
1229 c @ ObligationCauseCode::WhereClauseInExpr(_, _, hir_id, _)
1232 if self.tcx.hir_span(*hir_id).lo() == span.lo() =>
1233 {
1234 if let hir::Node::Expr(expr) = self.tcx.parent_hir_node(*hir_id)
1238 && let hir::ExprKind::Call(base, _) = expr.kind
1239 && let hir::ExprKind::Path(hir::QPath::TypeRelative(ty, segment)) = base.kind
1240 && let hir::Node::Expr(outer) = self.tcx.parent_hir_node(expr.hir_id)
1241 && let hir::ExprKind::AddrOf(hir::BorrowKind::Ref, mtbl, _) = outer.kind
1242 && ty.span == span
1243 {
1244 let trait_pred_and_imm_ref = poly_trait_pred.map_bound(|p| {
1250 (p, Ty::new_imm_ref(self.tcx, self.tcx.lifetimes.re_static, p.self_ty()))
1251 });
1252 let trait_pred_and_mut_ref = poly_trait_pred.map_bound(|p| {
1253 (p, Ty::new_mut_ref(self.tcx, self.tcx.lifetimes.re_static, p.self_ty()))
1254 });
1255
1256 let imm_ref_self_ty_satisfies_pred = mk_result(trait_pred_and_imm_ref);
1257 let mut_ref_self_ty_satisfies_pred = mk_result(trait_pred_and_mut_ref);
1258 let sugg_msg = |pre: &str| {
1259 format!(
1260 "you likely meant to call the associated function `{FN}` for type \
1261 `&{pre}{TY}`, but the code as written calls associated function `{FN}` on \
1262 type `{TY}`",
1263 FN = segment.ident,
1264 TY = poly_trait_pred.self_ty(),
1265 )
1266 };
1267 match (imm_ref_self_ty_satisfies_pred, mut_ref_self_ty_satisfies_pred, mtbl) {
1268 (true, _, hir::Mutability::Not) | (_, true, hir::Mutability::Mut) => {
1269 err.multipart_suggestion_verbose(
1270 sugg_msg(mtbl.prefix_str()),
1271 vec![
1272 (outer.span.shrink_to_lo(), "<".to_string()),
1273 (span.shrink_to_hi(), ">".to_string()),
1274 ],
1275 Applicability::MachineApplicable,
1276 );
1277 }
1278 (true, _, hir::Mutability::Mut) => {
1279 err.multipart_suggestion_verbose(
1281 sugg_msg("mut "),
1282 vec![
1283 (outer.span.shrink_to_lo().until(span), "<&".to_string()),
1284 (span.shrink_to_hi(), ">".to_string()),
1285 ],
1286 Applicability::MachineApplicable,
1287 );
1288 }
1289 (_, true, hir::Mutability::Not) => {
1290 err.multipart_suggestion_verbose(
1291 sugg_msg(""),
1292 vec![
1293 (outer.span.shrink_to_lo().until(span), "<&mut ".to_string()),
1294 (span.shrink_to_hi(), ">".to_string()),
1295 ],
1296 Applicability::MachineApplicable,
1297 );
1298 }
1299 _ => {}
1300 }
1301 return false;
1303 }
1304 c
1305 }
1306 c if matches!(
1307 span.ctxt().outer_expn_data().kind,
1308 ExpnKind::Desugaring(DesugaringKind::ForLoop)
1309 ) =>
1310 {
1311 c
1312 }
1313 _ => return false,
1314 };
1315
1316 let mut never_suggest_borrow: Vec<_> =
1320 [LangItem::Copy, LangItem::Clone, LangItem::Unpin, LangItem::Sized]
1321 .iter()
1322 .filter_map(|lang_item| self.tcx.lang_items().get(*lang_item))
1323 .collect();
1324
1325 if let Some(def_id) = self.tcx.get_diagnostic_item(sym::Send) {
1326 never_suggest_borrow.push(def_id);
1327 }
1328
1329 let mut try_borrowing = |old_pred: ty::PolyTraitPredicate<'tcx>,
1331 blacklist: &[DefId]|
1332 -> bool {
1333 if blacklist.contains(&old_pred.def_id()) {
1334 return false;
1335 }
1336 let trait_pred_and_imm_ref = old_pred.map_bound(|trait_pred| {
1338 (
1339 trait_pred,
1340 Ty::new_imm_ref(self.tcx, self.tcx.lifetimes.re_static, trait_pred.self_ty()),
1341 )
1342 });
1343 let trait_pred_and_mut_ref = old_pred.map_bound(|trait_pred| {
1344 (
1345 trait_pred,
1346 Ty::new_mut_ref(self.tcx, self.tcx.lifetimes.re_static, trait_pred.self_ty()),
1347 )
1348 });
1349
1350 let imm_ref_self_ty_satisfies_pred = mk_result(trait_pred_and_imm_ref);
1351 let mut_ref_self_ty_satisfies_pred = mk_result(trait_pred_and_mut_ref);
1352
1353 let (ref_inner_ty_satisfies_pred, ref_inner_ty_is_mut) =
1354 if let ObligationCauseCode::WhereClauseInExpr(..) = obligation.cause.code()
1355 && let ty::Ref(_, ty, mutability) = old_pred.self_ty().skip_binder().kind()
1356 {
1357 (
1358 mk_result(old_pred.map_bound(|trait_pred| (trait_pred, *ty))),
1359 mutability.is_mut(),
1360 )
1361 } else {
1362 (false, false)
1363 };
1364
1365 let is_immut = imm_ref_self_ty_satisfies_pred
1366 || (ref_inner_ty_satisfies_pred && !ref_inner_ty_is_mut);
1367 let is_mut = mut_ref_self_ty_satisfies_pred || ref_inner_ty_is_mut;
1368 if !is_immut && !is_mut {
1369 return false;
1370 }
1371 let Ok(_snippet) = self.tcx.sess.source_map().span_to_snippet(span) else {
1372 return false;
1373 };
1374 if !matches!(
1382 span.ctxt().outer_expn_data().kind,
1383 ExpnKind::Root | ExpnKind::Desugaring(DesugaringKind::ForLoop)
1384 ) {
1385 return false;
1386 }
1387 let mut label = || {
1394 let msg = format!(
1395 "the trait bound `{}` is not satisfied",
1396 self.tcx.short_string(old_pred, err.long_ty_path()),
1397 );
1398 let self_ty_str =
1399 self.tcx.short_string(old_pred.self_ty().skip_binder(), err.long_ty_path());
1400 let trait_path = self
1401 .tcx
1402 .short_string(old_pred.print_modifiers_and_trait_path(), err.long_ty_path());
1403
1404 if has_custom_message {
1405 err.note(msg);
1406 } else {
1407 err.messages = vec![(rustc_errors::DiagMessage::from(msg), Style::NoStyle)];
1408 }
1409 err.span_label(
1410 span,
1411 format!("the trait `{trait_path}` is not implemented for `{self_ty_str}`"),
1412 );
1413 };
1414
1415 let mut sugg_prefixes = vec![];
1416 if is_immut {
1417 sugg_prefixes.push("&");
1418 }
1419 if is_mut {
1420 sugg_prefixes.push("&mut ");
1421 }
1422 let sugg_msg = format!(
1423 "consider{} borrowing here",
1424 if is_mut && !is_immut { " mutably" } else { "" },
1425 );
1426
1427 let Some(body) = self.tcx.hir_maybe_body_owned_by(obligation.cause.body_id) else {
1431 return false;
1432 };
1433 let mut expr_finder = FindExprBySpan::new(span, self.tcx);
1434 expr_finder.visit_expr(body.value);
1435
1436 if let Some(ty) = expr_finder.ty_result {
1437 if let hir::Node::Expr(expr) = self.tcx.parent_hir_node(ty.hir_id)
1438 && let hir::ExprKind::Path(hir::QPath::TypeRelative(_, _)) = expr.kind
1439 && ty.span == span
1440 {
1441 label();
1444 err.multipart_suggestions(
1445 sugg_msg,
1446 sugg_prefixes.into_iter().map(|sugg_prefix| {
1447 vec![
1448 (span.shrink_to_lo(), format!("<{sugg_prefix}")),
1449 (span.shrink_to_hi(), ">".to_string()),
1450 ]
1451 }),
1452 Applicability::MaybeIncorrect,
1453 );
1454 return true;
1455 }
1456 return false;
1457 }
1458 let Some(expr) = expr_finder.result else {
1459 return false;
1460 };
1461 if let hir::ExprKind::AddrOf(_, _, _) = expr.kind {
1462 return false;
1463 }
1464 let needs_parens_post = expr_needs_parens(expr);
1465 let needs_parens_pre = match self.tcx.parent_hir_node(expr.hir_id) {
1466 Node::Expr(e)
1467 if let hir::ExprKind::MethodCall(_, base, _, _) = e.kind
1468 && base.hir_id == expr.hir_id =>
1469 {
1470 true
1471 }
1472 _ => false,
1473 };
1474
1475 label();
1476 let suggestions = sugg_prefixes.into_iter().map(|sugg_prefix| {
1477 match (needs_parens_pre, needs_parens_post) {
1478 (false, false) => vec![(span.shrink_to_lo(), sugg_prefix.to_string())],
1479 (false, true) => vec![
1482 (span.shrink_to_lo(), format!("{sugg_prefix}(")),
1483 (span.shrink_to_hi(), ")".to_string()),
1484 ],
1485 (true, false) => vec![
1488 (span.shrink_to_lo(), format!("({sugg_prefix}")),
1489 (span.shrink_to_hi(), ")".to_string()),
1490 ],
1491 (true, true) => vec![
1492 (span.shrink_to_lo(), format!("({sugg_prefix}(")),
1493 (span.shrink_to_hi(), "))".to_string()),
1494 ],
1495 }
1496 });
1497 err.multipart_suggestions(sugg_msg, suggestions, Applicability::MaybeIncorrect);
1498 return true;
1499 };
1500
1501 if let ObligationCauseCode::ImplDerived(cause) = &*code {
1502 try_borrowing(cause.derived.parent_trait_pred, &[])
1503 } else if let ObligationCauseCode::WhereClause(..)
1504 | ObligationCauseCode::WhereClauseInExpr(..) = code
1505 {
1506 try_borrowing(poly_trait_pred, &never_suggest_borrow)
1507 } else {
1508 false
1509 }
1510 }
1511
1512 pub(super) fn suggest_borrowing_for_object_cast(
1514 &self,
1515 err: &mut Diag<'_>,
1516 obligation: &PredicateObligation<'tcx>,
1517 self_ty: Ty<'tcx>,
1518 target_ty: Ty<'tcx>,
1519 ) {
1520 let ty::Ref(_, object_ty, hir::Mutability::Not) = target_ty.kind() else {
1521 return;
1522 };
1523 let ty::Dynamic(predicates, _) = object_ty.kind() else {
1524 return;
1525 };
1526 let self_ref_ty = Ty::new_imm_ref(self.tcx, self.tcx.lifetimes.re_erased, self_ty);
1527
1528 for predicate in predicates.iter() {
1529 if !self.predicate_must_hold_modulo_regions(
1530 &obligation.with(self.tcx, predicate.with_self_ty(self.tcx, self_ref_ty)),
1531 ) {
1532 return;
1533 }
1534 }
1535
1536 err.span_suggestion_verbose(
1537 obligation.cause.span.shrink_to_lo(),
1538 format!(
1539 "consider borrowing the value, since `&{self_ty}` can be coerced into `{target_ty}`"
1540 ),
1541 "&",
1542 Applicability::MaybeIncorrect,
1543 );
1544 }
1545
1546 pub(super) fn suggest_remove_reference(
1549 &self,
1550 obligation: &PredicateObligation<'tcx>,
1551 err: &mut Diag<'_>,
1552 trait_pred: ty::PolyTraitPredicate<'tcx>,
1553 ) -> bool {
1554 let mut span = obligation.cause.span;
1555 let mut trait_pred = trait_pred;
1556 let mut code = obligation.cause.code();
1557 while let Some((c, Some(parent_trait_pred))) = code.parent_with_predicate() {
1558 code = c;
1561 trait_pred = parent_trait_pred;
1562 }
1563 while span.desugaring_kind().is_some() {
1564 span.remove_mark();
1566 }
1567 let mut expr_finder = super::FindExprBySpan::new(span, self.tcx);
1568 let Some(body) = self.tcx.hir_maybe_body_owned_by(obligation.cause.body_id) else {
1569 return false;
1570 };
1571 expr_finder.visit_expr(body.value);
1572 let mut maybe_suggest = |suggested_ty, count, suggestions| {
1573 let trait_pred_and_suggested_ty =
1575 trait_pred.map_bound(|trait_pred| (trait_pred, suggested_ty));
1576
1577 let new_obligation = self.mk_trait_obligation_with_new_self_ty(
1578 obligation.param_env,
1579 trait_pred_and_suggested_ty,
1580 );
1581
1582 if self.predicate_may_hold(&new_obligation) {
1583 let msg = if count == 1 {
1584 "consider removing the leading `&`-reference".to_string()
1585 } else {
1586 format!("consider removing {count} leading `&`-references")
1587 };
1588
1589 err.multipart_suggestion_verbose(
1590 msg,
1591 suggestions,
1592 Applicability::MachineApplicable,
1593 );
1594 true
1595 } else {
1596 false
1597 }
1598 };
1599
1600 let mut count = 0;
1603 let mut suggestions = vec![];
1604 let mut suggested_ty = trait_pred.self_ty().skip_binder();
1606 if let Some(mut hir_ty) = expr_finder.ty_result {
1607 while let hir::TyKind::Ref(_, mut_ty) = &hir_ty.kind {
1608 count += 1;
1609 let span = hir_ty.span.until(mut_ty.ty.span);
1610 suggestions.push((span, String::new()));
1611
1612 let ty::Ref(_, inner_ty, _) = suggested_ty.kind() else {
1613 break;
1614 };
1615 suggested_ty = *inner_ty;
1616
1617 hir_ty = mut_ty.ty;
1618
1619 if maybe_suggest(suggested_ty, count, suggestions.clone()) {
1620 return true;
1621 }
1622 }
1623 }
1624
1625 let Some(mut expr) = expr_finder.result else {
1627 return false;
1628 };
1629 let mut count = 0;
1630 let mut suggestions = vec![];
1631 let mut suggested_ty = trait_pred.self_ty().skip_binder();
1633 'outer: loop {
1634 while let hir::ExprKind::AddrOf(_, _, borrowed) = expr.kind {
1635 count += 1;
1636 let span =
1637 if let Some(borrowed_span) = borrowed.span.find_ancestor_inside(expr.span) {
1638 expr.span.until(borrowed_span)
1639 } else {
1640 break 'outer;
1641 };
1642
1643 match self.tcx.sess.source_map().span_to_snippet(span) {
1646 Ok(snippet) if snippet.starts_with("&") => {}
1647 _ => break 'outer,
1648 }
1649
1650 suggestions.push((span, String::new()));
1651
1652 let ty::Ref(_, inner_ty, _) = suggested_ty.kind() else {
1653 break 'outer;
1654 };
1655 suggested_ty = *inner_ty;
1656
1657 expr = borrowed;
1658
1659 if maybe_suggest(suggested_ty, count, suggestions.clone()) {
1660 return true;
1661 }
1662 }
1663 if let hir::ExprKind::Path(hir::QPath::Resolved(None, path)) = expr.kind
1664 && let Res::Local(hir_id) = path.res
1665 && let hir::Node::Pat(binding) = self.tcx.hir_node(hir_id)
1666 && let hir::Node::LetStmt(local) = self.tcx.parent_hir_node(binding.hir_id)
1667 && let None = local.ty
1668 && let Some(binding_expr) = local.init
1669 {
1670 expr = binding_expr;
1671 } else {
1672 break 'outer;
1673 }
1674 }
1675 false
1676 }
1677
1678 pub(super) fn suggest_remove_await(
1679 &self,
1680 obligation: &PredicateObligation<'tcx>,
1681 err: &mut Diag<'_>,
1682 ) {
1683 if let ObligationCauseCode::AwaitableExpr(hir_id) = obligation.cause.code().peel_derives()
1684 && let hir::Node::Expr(expr) = self.tcx.hir_node(*hir_id)
1685 {
1686 if let Some((_, hir::Node::Expr(await_expr))) = self.tcx.hir_parent_iter(*hir_id).nth(1)
1693 && let Some(expr_span) = expr.span.find_ancestor_inside_same_ctxt(await_expr.span)
1694 {
1695 let removal_span = self
1696 .tcx
1697 .sess
1698 .source_map()
1699 .span_extend_while_whitespace(expr_span)
1700 .shrink_to_hi()
1701 .to(await_expr.span.shrink_to_hi());
1702 err.span_suggestion_verbose(
1703 removal_span,
1704 "remove the `.await`",
1705 "",
1706 Applicability::MachineApplicable,
1707 );
1708 } else {
1709 err.span_label(obligation.cause.span, "remove the `.await`");
1710 }
1711 if let hir::Expr { span, kind: hir::ExprKind::Call(base, _), .. } = expr {
1713 if let ty::PredicateKind::Clause(ty::ClauseKind::Trait(pred)) =
1714 obligation.predicate.kind().skip_binder()
1715 {
1716 err.span_label(*span, format!("this call returns `{}`", pred.self_ty()));
1717 }
1718 if let Some(typeck_results) = &self.typeck_results
1719 && let ty = typeck_results.expr_ty_adjusted(base)
1720 && let ty::FnDef(def_id, _args) = ty.kind()
1721 && let Some(hir::Node::Item(item)) = self.tcx.hir_get_if_local(*def_id)
1722 {
1723 let (ident, _, _, _) = item.expect_fn();
1724 let msg = format!("alternatively, consider making `fn {ident}` asynchronous");
1725 if item.vis_span.is_empty() {
1726 err.span_suggestion_verbose(
1727 item.span.shrink_to_lo(),
1728 msg,
1729 "async ",
1730 Applicability::MaybeIncorrect,
1731 );
1732 } else {
1733 err.span_suggestion_verbose(
1734 item.vis_span.shrink_to_hi(),
1735 msg,
1736 " async",
1737 Applicability::MaybeIncorrect,
1738 );
1739 }
1740 }
1741 }
1742 }
1743 }
1744
1745 pub(super) fn suggest_change_mut(
1748 &self,
1749 obligation: &PredicateObligation<'tcx>,
1750 err: &mut Diag<'_>,
1751 trait_pred: ty::PolyTraitPredicate<'tcx>,
1752 ) {
1753 let points_at_arg =
1754 matches!(obligation.cause.code(), ObligationCauseCode::FunctionArg { .. },);
1755
1756 let span = obligation.cause.span;
1757 if let Ok(snippet) = self.tcx.sess.source_map().span_to_snippet(span) {
1758 let refs_number =
1759 snippet.chars().filter(|c| !c.is_whitespace()).take_while(|c| *c == '&').count();
1760 if let Some('\'') = snippet.chars().filter(|c| !c.is_whitespace()).nth(refs_number) {
1761 return;
1763 }
1764 let trait_pred = self.resolve_vars_if_possible(trait_pred);
1765 if trait_pred.has_non_region_infer() {
1766 return;
1769 }
1770
1771 if let ty::Ref(region, t_type, mutability) = *trait_pred.skip_binder().self_ty().kind()
1773 {
1774 let suggested_ty = match mutability {
1775 hir::Mutability::Mut => Ty::new_imm_ref(self.tcx, region, t_type),
1776 hir::Mutability::Not => Ty::new_mut_ref(self.tcx, region, t_type),
1777 };
1778
1779 let trait_pred_and_suggested_ty =
1781 trait_pred.map_bound(|trait_pred| (trait_pred, suggested_ty));
1782
1783 let new_obligation = self.mk_trait_obligation_with_new_self_ty(
1784 obligation.param_env,
1785 trait_pred_and_suggested_ty,
1786 );
1787 let suggested_ty_would_satisfy_obligation = self
1788 .evaluate_obligation_no_overflow(&new_obligation)
1789 .must_apply_modulo_regions();
1790 if suggested_ty_would_satisfy_obligation {
1791 let sp = self
1792 .tcx
1793 .sess
1794 .source_map()
1795 .span_take_while(span, |c| c.is_whitespace() || *c == '&');
1796 if points_at_arg && mutability.is_not() && refs_number > 0 {
1797 if snippet
1799 .trim_start_matches(|c: char| c.is_whitespace() || c == '&')
1800 .starts_with("mut")
1801 {
1802 return;
1803 }
1804 err.span_suggestion_verbose(
1805 sp,
1806 "consider changing this borrow's mutability",
1807 "&mut ",
1808 Applicability::MachineApplicable,
1809 );
1810 } else {
1811 err.note(format!(
1812 "`{}` is implemented for `{}`, but not for `{}`",
1813 trait_pred.print_modifiers_and_trait_path(),
1814 suggested_ty,
1815 trait_pred.skip_binder().self_ty(),
1816 ));
1817 }
1818 }
1819 }
1820 }
1821 }
1822
1823 pub(super) fn suggest_semicolon_removal(
1824 &self,
1825 obligation: &PredicateObligation<'tcx>,
1826 err: &mut Diag<'_>,
1827 span: Span,
1828 trait_pred: ty::PolyTraitPredicate<'tcx>,
1829 ) -> bool {
1830 let node = self.tcx.hir_node_by_def_id(obligation.cause.body_id);
1831 if let hir::Node::Item(hir::Item { kind: hir::ItemKind::Fn {sig, body: body_id, .. }, .. }) = node
1832 && let hir::ExprKind::Block(blk, _) = &self.tcx.hir_body(*body_id).value.kind
1833 && sig.decl.output.span().overlaps(span)
1834 && blk.expr.is_none()
1835 && trait_pred.self_ty().skip_binder().is_unit()
1836 && let Some(stmt) = blk.stmts.last()
1837 && let hir::StmtKind::Semi(expr) = stmt.kind
1838 && let Some(typeck_results) = &self.typeck_results
1840 && let Some(ty) = typeck_results.expr_ty_opt(expr)
1841 && self.predicate_may_hold(&self.mk_trait_obligation_with_new_self_ty(
1842 obligation.param_env, trait_pred.map_bound(|trait_pred| (trait_pred, ty))
1843 ))
1844 {
1845 err.span_label(
1846 expr.span,
1847 format!(
1848 "this expression has type `{}`, which implements `{}`",
1849 ty,
1850 trait_pred.print_modifiers_and_trait_path()
1851 ),
1852 );
1853 err.span_suggestion(
1854 self.tcx.sess.source_map().end_point(stmt.span),
1855 "remove this semicolon",
1856 "",
1857 Applicability::MachineApplicable,
1858 );
1859 return true;
1860 }
1861 false
1862 }
1863
1864 pub(super) fn return_type_span(&self, obligation: &PredicateObligation<'tcx>) -> Option<Span> {
1865 let hir::Node::Item(hir::Item { kind: hir::ItemKind::Fn { sig, .. }, .. }) =
1866 self.tcx.hir_node_by_def_id(obligation.cause.body_id)
1867 else {
1868 return None;
1869 };
1870
1871 if let hir::FnRetTy::Return(ret_ty) = sig.decl.output { Some(ret_ty.span) } else { None }
1872 }
1873
1874 pub(super) fn suggest_impl_trait(
1878 &self,
1879 err: &mut Diag<'_>,
1880 obligation: &PredicateObligation<'tcx>,
1881 trait_pred: ty::PolyTraitPredicate<'tcx>,
1882 ) -> bool {
1883 let ObligationCauseCode::SizedReturnType = obligation.cause.code() else {
1884 return false;
1885 };
1886 let ty::Dynamic(_, _) = trait_pred.self_ty().skip_binder().kind() else {
1887 return false;
1888 };
1889 if let Node::Item(hir::Item { kind: hir::ItemKind::Fn { sig: fn_sig, .. }, .. })
1890 | Node::ImplItem(hir::ImplItem { kind: hir::ImplItemKind::Fn(fn_sig, _), .. })
1891 | Node::TraitItem(hir::TraitItem { kind: hir::TraitItemKind::Fn(fn_sig, _), .. }) =
1892 self.tcx.hir_node_by_def_id(obligation.cause.body_id)
1893 && let hir::FnRetTy::Return(ty) = fn_sig.decl.output
1894 && let hir::TyKind::Path(qpath) = ty.kind
1895 && let hir::QPath::Resolved(None, path) = qpath
1896 && let Res::Def(DefKind::TyAlias, def_id) = path.res
1897 {
1898 err.span_note(self.tcx.def_span(def_id), "this type alias is unsized");
1902 err.multipart_suggestion(
1903 format!(
1904 "consider boxing the return type, and wrapping all of the returned values in \
1905 `Box::new`",
1906 ),
1907 vec![
1908 (ty.span.shrink_to_lo(), "Box<".to_string()),
1909 (ty.span.shrink_to_hi(), ">".to_string()),
1910 ],
1911 Applicability::MaybeIncorrect,
1912 );
1913 return false;
1914 }
1915
1916 err.code(E0746);
1917 err.primary_message("return type cannot be a trait object without pointer indirection");
1918 err.children.clear();
1919
1920 let mut span = obligation.cause.span;
1921 if let DefKind::Closure = self.tcx.def_kind(obligation.cause.body_id)
1922 && let parent = self.tcx.parent(obligation.cause.body_id.into())
1923 && let DefKind::Fn | DefKind::AssocFn = self.tcx.def_kind(parent)
1924 && self.tcx.asyncness(parent).is_async()
1925 && let Some(parent) = parent.as_local()
1926 && let Node::Item(hir::Item { kind: hir::ItemKind::Fn { sig: fn_sig, .. }, .. })
1927 | Node::ImplItem(hir::ImplItem { kind: hir::ImplItemKind::Fn(fn_sig, _), .. })
1928 | Node::TraitItem(hir::TraitItem {
1929 kind: hir::TraitItemKind::Fn(fn_sig, _), ..
1930 }) = self.tcx.hir_node_by_def_id(parent)
1931 {
1932 span = fn_sig.decl.output.span();
1937 err.span(span);
1938 }
1939 let body = self.tcx.hir_body_owned_by(obligation.cause.body_id);
1940
1941 let mut visitor = ReturnsVisitor::default();
1942 visitor.visit_body(&body);
1943
1944 let (pre, impl_span) = if let Ok(snip) = self.tcx.sess.source_map().span_to_snippet(span)
1945 && snip.starts_with("dyn ")
1946 {
1947 ("", span.with_hi(span.lo() + BytePos(4)))
1948 } else {
1949 ("dyn ", span.shrink_to_lo())
1950 };
1951
1952 err.span_suggestion_verbose(
1953 impl_span,
1954 "consider returning an `impl Trait` instead of a `dyn Trait`",
1955 "impl ",
1956 Applicability::MaybeIncorrect,
1957 );
1958
1959 let mut sugg = vec![
1960 (span.shrink_to_lo(), format!("Box<{pre}")),
1961 (span.shrink_to_hi(), ">".to_string()),
1962 ];
1963 sugg.extend(visitor.returns.into_iter().flat_map(|expr| {
1964 let span =
1965 expr.span.find_ancestor_in_same_ctxt(obligation.cause.span).unwrap_or(expr.span);
1966 if !span.can_be_used_for_suggestions() {
1967 vec![]
1968 } else if let hir::ExprKind::Call(path, ..) = expr.kind
1969 && let hir::ExprKind::Path(hir::QPath::TypeRelative(ty, method)) = path.kind
1970 && method.ident.name == sym::new
1971 && let hir::TyKind::Path(hir::QPath::Resolved(.., box_path)) = ty.kind
1972 && box_path
1973 .res
1974 .opt_def_id()
1975 .is_some_and(|def_id| self.tcx.is_lang_item(def_id, LangItem::OwnedBox))
1976 {
1977 vec![]
1979 } else {
1980 vec![
1981 (span.shrink_to_lo(), "Box::new(".to_string()),
1982 (span.shrink_to_hi(), ")".to_string()),
1983 ]
1984 }
1985 }));
1986
1987 err.multipart_suggestion(
1988 format!(
1989 "alternatively, box the return type, and wrap all of the returned values in \
1990 `Box::new`",
1991 ),
1992 sugg,
1993 Applicability::MaybeIncorrect,
1994 );
1995
1996 true
1997 }
1998
1999 pub(super) fn report_closure_arg_mismatch(
2000 &self,
2001 span: Span,
2002 found_span: Option<Span>,
2003 found: ty::TraitRef<'tcx>,
2004 expected: ty::TraitRef<'tcx>,
2005 cause: &ObligationCauseCode<'tcx>,
2006 found_node: Option<Node<'_>>,
2007 param_env: ty::ParamEnv<'tcx>,
2008 ) -> Diag<'a> {
2009 pub(crate) fn build_fn_sig_ty<'tcx>(
2010 infcx: &InferCtxt<'tcx>,
2011 trait_ref: ty::TraitRef<'tcx>,
2012 ) -> Ty<'tcx> {
2013 let inputs = trait_ref.args.type_at(1);
2014 let sig = match inputs.kind() {
2015 ty::Tuple(inputs) if infcx.tcx.is_fn_trait(trait_ref.def_id) => {
2016 infcx.tcx.mk_fn_sig(
2017 *inputs,
2018 infcx.next_ty_var(DUMMY_SP),
2019 false,
2020 hir::Safety::Safe,
2021 ExternAbi::Rust,
2022 )
2023 }
2024 _ => infcx.tcx.mk_fn_sig(
2025 [inputs],
2026 infcx.next_ty_var(DUMMY_SP),
2027 false,
2028 hir::Safety::Safe,
2029 ExternAbi::Rust,
2030 ),
2031 };
2032
2033 Ty::new_fn_ptr(infcx.tcx, ty::Binder::dummy(sig))
2034 }
2035
2036 let argument_kind = match expected.self_ty().kind() {
2037 ty::Closure(..) => "closure",
2038 ty::Coroutine(..) => "coroutine",
2039 _ => "function",
2040 };
2041 let mut err = struct_span_code_err!(
2042 self.dcx(),
2043 span,
2044 E0631,
2045 "type mismatch in {argument_kind} arguments",
2046 );
2047
2048 err.span_label(span, "expected due to this");
2049
2050 let found_span = found_span.unwrap_or(span);
2051 err.span_label(found_span, "found signature defined here");
2052
2053 let expected = build_fn_sig_ty(self, expected);
2054 let found = build_fn_sig_ty(self, found);
2055
2056 let (expected_str, found_str) = self.cmp(expected, found);
2057
2058 let signature_kind = format!("{argument_kind} signature");
2059 err.note_expected_found(&signature_kind, expected_str, &signature_kind, found_str);
2060
2061 self.note_conflicting_fn_args(&mut err, cause, expected, found, param_env);
2062 self.note_conflicting_closure_bounds(cause, &mut err);
2063
2064 if let Some(found_node) = found_node {
2065 hint_missing_borrow(self, param_env, span, found, expected, found_node, &mut err);
2066 }
2067
2068 err
2069 }
2070
2071 fn note_conflicting_fn_args(
2072 &self,
2073 err: &mut Diag<'_>,
2074 cause: &ObligationCauseCode<'tcx>,
2075 expected: Ty<'tcx>,
2076 found: Ty<'tcx>,
2077 param_env: ty::ParamEnv<'tcx>,
2078 ) {
2079 let ObligationCauseCode::FunctionArg { arg_hir_id, .. } = cause else {
2080 return;
2081 };
2082 let ty::FnPtr(sig_tys, hdr) = expected.kind() else {
2083 return;
2084 };
2085 let expected = sig_tys.with(*hdr);
2086 let ty::FnPtr(sig_tys, hdr) = found.kind() else {
2087 return;
2088 };
2089 let found = sig_tys.with(*hdr);
2090 let Node::Expr(arg) = self.tcx.hir_node(*arg_hir_id) else {
2091 return;
2092 };
2093 let hir::ExprKind::Path(path) = arg.kind else {
2094 return;
2095 };
2096 let expected_inputs = self.tcx.instantiate_bound_regions_with_erased(expected).inputs();
2097 let found_inputs = self.tcx.instantiate_bound_regions_with_erased(found).inputs();
2098 let both_tys = expected_inputs.iter().copied().zip(found_inputs.iter().copied());
2099
2100 let arg_expr = |infcx: &InferCtxt<'tcx>, name, expected: Ty<'tcx>, found: Ty<'tcx>| {
2101 let (expected_ty, expected_refs) = get_deref_type_and_refs(expected);
2102 let (found_ty, found_refs) = get_deref_type_and_refs(found);
2103
2104 if infcx.can_eq(param_env, found_ty, expected_ty) {
2105 if found_refs.len() == expected_refs.len()
2106 && found_refs.iter().eq(expected_refs.iter())
2107 {
2108 name
2109 } else if found_refs.len() > expected_refs.len() {
2110 let refs = &found_refs[..found_refs.len() - expected_refs.len()];
2111 if found_refs[..expected_refs.len()].iter().eq(expected_refs.iter()) {
2112 format!(
2113 "{}{name}",
2114 refs.iter()
2115 .map(|mutbl| format!("&{}", mutbl.prefix_str()))
2116 .collect::<Vec<_>>()
2117 .join(""),
2118 )
2119 } else {
2120 format!(
2122 "{}*{name}",
2123 refs.iter()
2124 .map(|mutbl| format!("&{}", mutbl.prefix_str()))
2125 .collect::<Vec<_>>()
2126 .join(""),
2127 )
2128 }
2129 } else if expected_refs.len() > found_refs.len() {
2130 format!(
2131 "{}{name}",
2132 (0..(expected_refs.len() - found_refs.len()))
2133 .map(|_| "*")
2134 .collect::<Vec<_>>()
2135 .join(""),
2136 )
2137 } else {
2138 format!(
2139 "{}{name}",
2140 found_refs
2141 .iter()
2142 .map(|mutbl| format!("&{}", mutbl.prefix_str()))
2143 .chain(found_refs.iter().map(|_| "*".to_string()))
2144 .collect::<Vec<_>>()
2145 .join(""),
2146 )
2147 }
2148 } else {
2149 format!("/* {found} */")
2150 }
2151 };
2152 let args_have_same_underlying_type = both_tys.clone().all(|(expected, found)| {
2153 let (expected_ty, _) = get_deref_type_and_refs(expected);
2154 let (found_ty, _) = get_deref_type_and_refs(found);
2155 self.can_eq(param_env, found_ty, expected_ty)
2156 });
2157 let (closure_names, call_names): (Vec<_>, Vec<_>) = if args_have_same_underlying_type
2158 && !expected_inputs.is_empty()
2159 && expected_inputs.len() == found_inputs.len()
2160 && let Some(typeck) = &self.typeck_results
2161 && let Res::Def(res_kind, fn_def_id) = typeck.qpath_res(&path, *arg_hir_id)
2162 && res_kind.is_fn_like()
2163 {
2164 let closure: Vec<_> = self
2165 .tcx
2166 .fn_arg_idents(fn_def_id)
2167 .iter()
2168 .enumerate()
2169 .map(|(i, ident)| {
2170 if let Some(ident) = ident
2171 && !matches!(ident, Ident { name: kw::Underscore | kw::SelfLower, .. })
2172 {
2173 format!("{ident}")
2174 } else {
2175 format!("arg{i}")
2176 }
2177 })
2178 .collect();
2179 let args = closure
2180 .iter()
2181 .zip(both_tys)
2182 .map(|(name, (expected, found))| {
2183 arg_expr(self.infcx, name.to_owned(), expected, found)
2184 })
2185 .collect();
2186 (closure, args)
2187 } else {
2188 let closure_args = expected_inputs
2189 .iter()
2190 .enumerate()
2191 .map(|(i, _)| format!("arg{i}"))
2192 .collect::<Vec<_>>();
2193 let call_args = both_tys
2194 .enumerate()
2195 .map(|(i, (expected, found))| {
2196 arg_expr(self.infcx, format!("arg{i}"), expected, found)
2197 })
2198 .collect::<Vec<_>>();
2199 (closure_args, call_args)
2200 };
2201 let closure_names: Vec<_> = closure_names
2202 .into_iter()
2203 .zip(expected_inputs.iter())
2204 .map(|(name, ty)| {
2205 format!(
2206 "{name}{}",
2207 if ty.has_infer_types() {
2208 String::new()
2209 } else if ty.references_error() {
2210 ": /* type */".to_string()
2211 } else {
2212 format!(": {ty}")
2213 }
2214 )
2215 })
2216 .collect();
2217 err.multipart_suggestion(
2218 "consider wrapping the function in a closure",
2219 vec![
2220 (arg.span.shrink_to_lo(), format!("|{}| ", closure_names.join(", "))),
2221 (arg.span.shrink_to_hi(), format!("({})", call_names.join(", "))),
2222 ],
2223 Applicability::MaybeIncorrect,
2224 );
2225 }
2226
2227 fn note_conflicting_closure_bounds(
2230 &self,
2231 cause: &ObligationCauseCode<'tcx>,
2232 err: &mut Diag<'_>,
2233 ) {
2234 if let ObligationCauseCode::WhereClauseInExpr(def_id, _, _, idx) = cause
2238 && let predicates = self.tcx.predicates_of(def_id).instantiate_identity(self.tcx)
2239 && let Some(pred) = predicates.predicates.get(*idx)
2240 && let ty::ClauseKind::Trait(trait_pred) = pred.kind().skip_binder()
2241 && self.tcx.is_fn_trait(trait_pred.def_id())
2242 {
2243 let expected_self =
2244 self.tcx.anonymize_bound_vars(pred.kind().rebind(trait_pred.self_ty()));
2245 let expected_args =
2246 self.tcx.anonymize_bound_vars(pred.kind().rebind(trait_pred.trait_ref.args));
2247
2248 let other_pred = predicates.into_iter().enumerate().find(|(other_idx, (pred, _))| {
2251 match pred.kind().skip_binder() {
2252 ty::ClauseKind::Trait(trait_pred)
2253 if self.tcx.is_fn_trait(trait_pred.def_id())
2254 && other_idx != idx
2255 && expected_self
2258 == self.tcx.anonymize_bound_vars(
2259 pred.kind().rebind(trait_pred.self_ty()),
2260 )
2261 && expected_args
2263 != self.tcx.anonymize_bound_vars(
2264 pred.kind().rebind(trait_pred.trait_ref.args),
2265 ) =>
2266 {
2267 true
2268 }
2269 _ => false,
2270 }
2271 });
2272 if let Some((_, (_, other_pred_span))) = other_pred {
2274 err.span_note(
2275 other_pred_span,
2276 "closure inferred to have a different signature due to this bound",
2277 );
2278 }
2279 }
2280 }
2281
2282 pub(super) fn suggest_fully_qualified_path(
2283 &self,
2284 err: &mut Diag<'_>,
2285 item_def_id: DefId,
2286 span: Span,
2287 trait_ref: DefId,
2288 ) {
2289 if let Some(assoc_item) = self.tcx.opt_associated_item(item_def_id)
2290 && let ty::AssocKind::Const { .. } | ty::AssocKind::Type { .. } = assoc_item.kind
2291 {
2292 err.note(format!(
2293 "{}s cannot be accessed directly on a `trait`, they can only be \
2294 accessed through a specific `impl`",
2295 self.tcx.def_kind_descr(assoc_item.as_def_kind(), item_def_id)
2296 ));
2297
2298 if !assoc_item.is_impl_trait_in_trait() {
2299 err.span_suggestion_verbose(
2300 span,
2301 "use the fully qualified path to an implementation",
2302 format!(
2303 "<Type as {}>::{}",
2304 self.tcx.def_path_str(trait_ref),
2305 assoc_item.name()
2306 ),
2307 Applicability::HasPlaceholders,
2308 );
2309 }
2310 }
2311 }
2312
2313 #[instrument(level = "debug", skip_all, fields(?obligation.predicate, ?obligation.cause.span))]
2356 pub fn maybe_note_obligation_cause_for_async_await<G: EmissionGuarantee>(
2357 &self,
2358 err: &mut Diag<'_, G>,
2359 obligation: &PredicateObligation<'tcx>,
2360 ) -> bool {
2361 let (mut trait_ref, mut target_ty) = match obligation.predicate.kind().skip_binder() {
2384 ty::PredicateKind::Clause(ty::ClauseKind::Trait(p)) => (Some(p), Some(p.self_ty())),
2385 _ => (None, None),
2386 };
2387 let mut coroutine = None;
2388 let mut outer_coroutine = None;
2389 let mut next_code = Some(obligation.cause.code());
2390
2391 let mut seen_upvar_tys_infer_tuple = false;
2392
2393 while let Some(code) = next_code {
2394 debug!(?code);
2395 match code {
2396 ObligationCauseCode::FunctionArg { parent_code, .. } => {
2397 next_code = Some(parent_code);
2398 }
2399 ObligationCauseCode::ImplDerived(cause) => {
2400 let ty = cause.derived.parent_trait_pred.skip_binder().self_ty();
2401 debug!(
2402 parent_trait_ref = ?cause.derived.parent_trait_pred,
2403 self_ty.kind = ?ty.kind(),
2404 "ImplDerived",
2405 );
2406
2407 match *ty.kind() {
2408 ty::Coroutine(did, ..) | ty::CoroutineWitness(did, _) => {
2409 coroutine = coroutine.or(Some(did));
2410 outer_coroutine = Some(did);
2411 }
2412 ty::Tuple(_) if !seen_upvar_tys_infer_tuple => {
2413 seen_upvar_tys_infer_tuple = true;
2418 }
2419 _ if coroutine.is_none() => {
2420 trait_ref = Some(cause.derived.parent_trait_pred.skip_binder());
2421 target_ty = Some(ty);
2422 }
2423 _ => {}
2424 }
2425
2426 next_code = Some(&cause.derived.parent_code);
2427 }
2428 ObligationCauseCode::WellFormedDerived(derived_obligation)
2429 | ObligationCauseCode::BuiltinDerived(derived_obligation) => {
2430 let ty = derived_obligation.parent_trait_pred.skip_binder().self_ty();
2431 debug!(
2432 parent_trait_ref = ?derived_obligation.parent_trait_pred,
2433 self_ty.kind = ?ty.kind(),
2434 );
2435
2436 match *ty.kind() {
2437 ty::Coroutine(did, ..) | ty::CoroutineWitness(did, ..) => {
2438 coroutine = coroutine.or(Some(did));
2439 outer_coroutine = Some(did);
2440 }
2441 ty::Tuple(_) if !seen_upvar_tys_infer_tuple => {
2442 seen_upvar_tys_infer_tuple = true;
2447 }
2448 _ if coroutine.is_none() => {
2449 trait_ref = Some(derived_obligation.parent_trait_pred.skip_binder());
2450 target_ty = Some(ty);
2451 }
2452 _ => {}
2453 }
2454
2455 next_code = Some(&derived_obligation.parent_code);
2456 }
2457 _ => break,
2458 }
2459 }
2460
2461 debug!(?coroutine, ?trait_ref, ?target_ty);
2463 let (Some(coroutine_did), Some(trait_ref), Some(target_ty)) =
2464 (coroutine, trait_ref, target_ty)
2465 else {
2466 return false;
2467 };
2468
2469 let span = self.tcx.def_span(coroutine_did);
2470
2471 let coroutine_did_root = self.tcx.typeck_root_def_id(coroutine_did);
2472 debug!(
2473 ?coroutine_did,
2474 ?coroutine_did_root,
2475 typeck_results.hir_owner = ?self.typeck_results.as_ref().map(|t| t.hir_owner),
2476 ?span,
2477 );
2478
2479 let coroutine_body =
2480 coroutine_did.as_local().and_then(|def_id| self.tcx.hir_maybe_body_owned_by(def_id));
2481 let mut visitor = AwaitsVisitor::default();
2482 if let Some(body) = coroutine_body {
2483 visitor.visit_body(&body);
2484 }
2485 debug!(awaits = ?visitor.awaits);
2486
2487 let target_ty_erased = self.tcx.erase_and_anonymize_regions(target_ty);
2490 let ty_matches = |ty| -> bool {
2491 let ty_erased = self.tcx.instantiate_bound_regions_with_erased(ty);
2504 let ty_erased = self.tcx.erase_and_anonymize_regions(ty_erased);
2505 let eq = ty_erased == target_ty_erased;
2506 debug!(?ty_erased, ?target_ty_erased, ?eq);
2507 eq
2508 };
2509
2510 let coroutine_data = match &self.typeck_results {
2515 Some(t) if t.hir_owner.to_def_id() == coroutine_did_root => CoroutineData(t),
2516 _ if coroutine_did.is_local() => {
2517 CoroutineData(self.tcx.typeck(coroutine_did.expect_local()))
2518 }
2519 _ => return false,
2520 };
2521
2522 let coroutine_within_in_progress_typeck = match &self.typeck_results {
2523 Some(t) => t.hir_owner.to_def_id() == coroutine_did_root,
2524 _ => false,
2525 };
2526
2527 let mut interior_or_upvar_span = None;
2528
2529 let from_awaited_ty = coroutine_data.get_from_await_ty(visitor, self.tcx, ty_matches);
2530 debug!(?from_awaited_ty);
2531
2532 if coroutine_did.is_local()
2534 && !coroutine_within_in_progress_typeck
2536 && let Some(coroutine_info) = self.tcx.mir_coroutine_witnesses(coroutine_did)
2537 {
2538 debug!(?coroutine_info);
2539 'find_source: for (variant, source_info) in
2540 coroutine_info.variant_fields.iter().zip(&coroutine_info.variant_source_info)
2541 {
2542 debug!(?variant);
2543 for &local in variant {
2544 let decl = &coroutine_info.field_tys[local];
2545 debug!(?decl);
2546 if ty_matches(ty::Binder::dummy(decl.ty)) && !decl.ignore_for_traits {
2547 interior_or_upvar_span = Some(CoroutineInteriorOrUpvar::Interior(
2548 decl.source_info.span,
2549 Some((source_info.span, from_awaited_ty)),
2550 ));
2551 break 'find_source;
2552 }
2553 }
2554 }
2555 }
2556
2557 if interior_or_upvar_span.is_none() {
2558 interior_or_upvar_span =
2559 coroutine_data.try_get_upvar_span(self, coroutine_did, ty_matches);
2560 }
2561
2562 if interior_or_upvar_span.is_none() && !coroutine_did.is_local() {
2563 interior_or_upvar_span = Some(CoroutineInteriorOrUpvar::Interior(span, None));
2564 }
2565
2566 debug!(?interior_or_upvar_span);
2567 if let Some(interior_or_upvar_span) = interior_or_upvar_span {
2568 let is_async = self.tcx.coroutine_is_async(coroutine_did);
2569 self.note_obligation_cause_for_async_await(
2570 err,
2571 interior_or_upvar_span,
2572 is_async,
2573 outer_coroutine,
2574 trait_ref,
2575 target_ty,
2576 obligation,
2577 next_code,
2578 );
2579 true
2580 } else {
2581 false
2582 }
2583 }
2584
2585 #[instrument(level = "debug", skip_all)]
2588 fn note_obligation_cause_for_async_await<G: EmissionGuarantee>(
2589 &self,
2590 err: &mut Diag<'_, G>,
2591 interior_or_upvar_span: CoroutineInteriorOrUpvar,
2592 is_async: bool,
2593 outer_coroutine: Option<DefId>,
2594 trait_pred: ty::TraitPredicate<'tcx>,
2595 target_ty: Ty<'tcx>,
2596 obligation: &PredicateObligation<'tcx>,
2597 next_code: Option<&ObligationCauseCode<'tcx>>,
2598 ) {
2599 let source_map = self.tcx.sess.source_map();
2600
2601 let (await_or_yield, an_await_or_yield) =
2602 if is_async { ("await", "an await") } else { ("yield", "a yield") };
2603 let future_or_coroutine = if is_async { "future" } else { "coroutine" };
2604
2605 let trait_explanation = if let Some(name @ (sym::Send | sym::Sync)) =
2608 self.tcx.get_diagnostic_name(trait_pred.def_id())
2609 {
2610 let (trait_name, trait_verb) =
2611 if name == sym::Send { ("`Send`", "sent") } else { ("`Sync`", "shared") };
2612
2613 err.code = None;
2614 err.primary_message(format!(
2615 "{future_or_coroutine} cannot be {trait_verb} between threads safely"
2616 ));
2617
2618 let original_span = err.span.primary_span().unwrap();
2619 let mut span = MultiSpan::from_span(original_span);
2620
2621 let message = outer_coroutine
2622 .and_then(|coroutine_did| {
2623 Some(match self.tcx.coroutine_kind(coroutine_did).unwrap() {
2624 CoroutineKind::Coroutine(_) => format!("coroutine is not {trait_name}"),
2625 CoroutineKind::Desugared(
2626 CoroutineDesugaring::Async,
2627 CoroutineSource::Fn,
2628 ) => self
2629 .tcx
2630 .parent(coroutine_did)
2631 .as_local()
2632 .map(|parent_did| self.tcx.local_def_id_to_hir_id(parent_did))
2633 .and_then(|parent_hir_id| self.tcx.hir_opt_name(parent_hir_id))
2634 .map(|name| {
2635 format!("future returned by `{name}` is not {trait_name}")
2636 })?,
2637 CoroutineKind::Desugared(
2638 CoroutineDesugaring::Async,
2639 CoroutineSource::Block,
2640 ) => {
2641 format!("future created by async block is not {trait_name}")
2642 }
2643 CoroutineKind::Desugared(
2644 CoroutineDesugaring::Async,
2645 CoroutineSource::Closure,
2646 ) => {
2647 format!("future created by async closure is not {trait_name}")
2648 }
2649 CoroutineKind::Desugared(
2650 CoroutineDesugaring::AsyncGen,
2651 CoroutineSource::Fn,
2652 ) => self
2653 .tcx
2654 .parent(coroutine_did)
2655 .as_local()
2656 .map(|parent_did| self.tcx.local_def_id_to_hir_id(parent_did))
2657 .and_then(|parent_hir_id| self.tcx.hir_opt_name(parent_hir_id))
2658 .map(|name| {
2659 format!("async iterator returned by `{name}` is not {trait_name}")
2660 })?,
2661 CoroutineKind::Desugared(
2662 CoroutineDesugaring::AsyncGen,
2663 CoroutineSource::Block,
2664 ) => {
2665 format!("async iterator created by async gen block is not {trait_name}")
2666 }
2667 CoroutineKind::Desugared(
2668 CoroutineDesugaring::AsyncGen,
2669 CoroutineSource::Closure,
2670 ) => {
2671 format!(
2672 "async iterator created by async gen closure is not {trait_name}"
2673 )
2674 }
2675 CoroutineKind::Desugared(CoroutineDesugaring::Gen, CoroutineSource::Fn) => {
2676 self.tcx
2677 .parent(coroutine_did)
2678 .as_local()
2679 .map(|parent_did| self.tcx.local_def_id_to_hir_id(parent_did))
2680 .and_then(|parent_hir_id| self.tcx.hir_opt_name(parent_hir_id))
2681 .map(|name| {
2682 format!("iterator returned by `{name}` is not {trait_name}")
2683 })?
2684 }
2685 CoroutineKind::Desugared(
2686 CoroutineDesugaring::Gen,
2687 CoroutineSource::Block,
2688 ) => {
2689 format!("iterator created by gen block is not {trait_name}")
2690 }
2691 CoroutineKind::Desugared(
2692 CoroutineDesugaring::Gen,
2693 CoroutineSource::Closure,
2694 ) => {
2695 format!("iterator created by gen closure is not {trait_name}")
2696 }
2697 })
2698 })
2699 .unwrap_or_else(|| format!("{future_or_coroutine} is not {trait_name}"));
2700
2701 span.push_span_label(original_span, message);
2702 err.span(span);
2703
2704 format!("is not {trait_name}")
2705 } else {
2706 format!("does not implement `{}`", trait_pred.print_modifiers_and_trait_path())
2707 };
2708
2709 let mut explain_yield = |interior_span: Span, yield_span: Span| {
2710 let mut span = MultiSpan::from_span(yield_span);
2711 let snippet = match source_map.span_to_snippet(interior_span) {
2712 Ok(snippet) if !snippet.contains('\n') => format!("`{snippet}`"),
2715 _ => "the value".to_string(),
2716 };
2717 span.push_span_label(
2734 yield_span,
2735 format!("{await_or_yield} occurs here, with {snippet} maybe used later"),
2736 );
2737 span.push_span_label(
2738 interior_span,
2739 format!("has type `{target_ty}` which {trait_explanation}"),
2740 );
2741 err.span_note(
2742 span,
2743 format!("{future_or_coroutine} {trait_explanation} as this value is used across {an_await_or_yield}"),
2744 );
2745 };
2746 match interior_or_upvar_span {
2747 CoroutineInteriorOrUpvar::Interior(interior_span, interior_extra_info) => {
2748 if let Some((yield_span, from_awaited_ty)) = interior_extra_info {
2749 if let Some(await_span) = from_awaited_ty {
2750 let mut span = MultiSpan::from_span(await_span);
2752 span.push_span_label(
2753 await_span,
2754 format!(
2755 "await occurs here on type `{target_ty}`, which {trait_explanation}"
2756 ),
2757 );
2758 err.span_note(
2759 span,
2760 format!(
2761 "future {trait_explanation} as it awaits another future which {trait_explanation}"
2762 ),
2763 );
2764 } else {
2765 explain_yield(interior_span, yield_span);
2767 }
2768 }
2769 }
2770 CoroutineInteriorOrUpvar::Upvar(upvar_span) => {
2771 let non_send = match target_ty.kind() {
2773 ty::Ref(_, ref_ty, mutability) => match self.evaluate_obligation(obligation) {
2774 Ok(eval) if !eval.may_apply() => Some((ref_ty, mutability.is_mut())),
2775 _ => None,
2776 },
2777 _ => None,
2778 };
2779
2780 let (span_label, span_note) = match non_send {
2781 Some((ref_ty, is_mut)) => {
2785 let ref_ty_trait = if is_mut { "Send" } else { "Sync" };
2786 let ref_kind = if is_mut { "&mut" } else { "&" };
2787 (
2788 format!(
2789 "has type `{target_ty}` which {trait_explanation}, because `{ref_ty}` is not `{ref_ty_trait}`"
2790 ),
2791 format!(
2792 "captured value {trait_explanation} because `{ref_kind}` references cannot be sent unless their referent is `{ref_ty_trait}`"
2793 ),
2794 )
2795 }
2796 None => (
2797 format!("has type `{target_ty}` which {trait_explanation}"),
2798 format!("captured value {trait_explanation}"),
2799 ),
2800 };
2801
2802 let mut span = MultiSpan::from_span(upvar_span);
2803 span.push_span_label(upvar_span, span_label);
2804 err.span_note(span, span_note);
2805 }
2806 }
2807
2808 debug!(?next_code);
2811 self.note_obligation_cause_code(
2812 obligation.cause.body_id,
2813 err,
2814 obligation.predicate,
2815 obligation.param_env,
2816 next_code.unwrap(),
2817 &mut Vec::new(),
2818 &mut Default::default(),
2819 );
2820 }
2821
2822 pub(super) fn note_obligation_cause_code<G: EmissionGuarantee, T>(
2823 &self,
2824 body_id: LocalDefId,
2825 err: &mut Diag<'_, G>,
2826 predicate: T,
2827 param_env: ty::ParamEnv<'tcx>,
2828 cause_code: &ObligationCauseCode<'tcx>,
2829 obligated_types: &mut Vec<Ty<'tcx>>,
2830 seen_requirements: &mut FxHashSet<DefId>,
2831 ) where
2832 T: Upcast<TyCtxt<'tcx>, ty::Predicate<'tcx>>,
2833 {
2834 let tcx = self.tcx;
2835 let predicate = predicate.upcast(tcx);
2836 let suggest_remove_deref = |err: &mut Diag<'_, G>, expr: &hir::Expr<'_>| {
2837 if let Some(pred) = predicate.as_trait_clause()
2838 && tcx.is_lang_item(pred.def_id(), LangItem::Sized)
2839 && let hir::ExprKind::Unary(hir::UnOp::Deref, inner) = expr.kind
2840 {
2841 err.span_suggestion_verbose(
2842 expr.span.until(inner.span),
2843 "references are always `Sized`, even if they point to unsized data; consider \
2844 not dereferencing the expression",
2845 String::new(),
2846 Applicability::MaybeIncorrect,
2847 );
2848 }
2849 };
2850 match *cause_code {
2851 ObligationCauseCode::ExprAssignable
2852 | ObligationCauseCode::MatchExpressionArm { .. }
2853 | ObligationCauseCode::Pattern { .. }
2854 | ObligationCauseCode::IfExpression { .. }
2855 | ObligationCauseCode::IfExpressionWithNoElse
2856 | ObligationCauseCode::MainFunctionType
2857 | ObligationCauseCode::LangFunctionType(_)
2858 | ObligationCauseCode::IntrinsicType
2859 | ObligationCauseCode::MethodReceiver
2860 | ObligationCauseCode::ReturnNoExpression
2861 | ObligationCauseCode::Misc
2862 | ObligationCauseCode::WellFormed(..)
2863 | ObligationCauseCode::MatchImpl(..)
2864 | ObligationCauseCode::ReturnValue(_)
2865 | ObligationCauseCode::BlockTailExpression(..)
2866 | ObligationCauseCode::AwaitableExpr(_)
2867 | ObligationCauseCode::ForLoopIterator
2868 | ObligationCauseCode::QuestionMark
2869 | ObligationCauseCode::CheckAssociatedTypeBounds { .. }
2870 | ObligationCauseCode::LetElse
2871 | ObligationCauseCode::UnOp { .. }
2872 | ObligationCauseCode::BinOp { .. }
2873 | ObligationCauseCode::AscribeUserTypeProvePredicate(..)
2874 | ObligationCauseCode::AlwaysApplicableImpl
2875 | ObligationCauseCode::ConstParam(_)
2876 | ObligationCauseCode::ReferenceOutlivesReferent(..)
2877 | ObligationCauseCode::ObjectTypeBound(..) => {}
2878 ObligationCauseCode::RustCall => {
2879 if let Some(pred) = predicate.as_trait_clause()
2880 && tcx.is_lang_item(pred.def_id(), LangItem::Sized)
2881 {
2882 err.note("argument required to be sized due to `extern \"rust-call\"` ABI");
2883 }
2884 }
2885 ObligationCauseCode::SliceOrArrayElem => {
2886 err.note("slice and array elements must have `Sized` type");
2887 }
2888 ObligationCauseCode::ArrayLen(array_ty) => {
2889 err.note(format!("the length of array `{array_ty}` must be type `usize`"));
2890 }
2891 ObligationCauseCode::TupleElem => {
2892 err.note("only the last element of a tuple may have a dynamically sized type");
2893 }
2894 ObligationCauseCode::DynCompatible(span) => {
2895 err.multipart_suggestion(
2896 "you might have meant to use `Self` to refer to the implementing type",
2897 vec![(span, "Self".into())],
2898 Applicability::MachineApplicable,
2899 );
2900 }
2901 ObligationCauseCode::WhereClause(item_def_id, span)
2902 | ObligationCauseCode::WhereClauseInExpr(item_def_id, span, ..)
2903 | ObligationCauseCode::HostEffectInExpr(item_def_id, span, ..)
2904 if !span.is_dummy() =>
2905 {
2906 if let ObligationCauseCode::WhereClauseInExpr(_, _, hir_id, pos) = &cause_code {
2907 if let Node::Expr(expr) = tcx.parent_hir_node(*hir_id)
2908 && let hir::ExprKind::Call(_, args) = expr.kind
2909 && let Some(expr) = args.get(*pos)
2910 {
2911 suggest_remove_deref(err, &expr);
2912 } else if let Node::Expr(expr) = self.tcx.hir_node(*hir_id)
2913 && let hir::ExprKind::MethodCall(_, _, args, _) = expr.kind
2914 && let Some(expr) = args.get(*pos)
2915 {
2916 suggest_remove_deref(err, &expr);
2917 }
2918 }
2919 let item_name = tcx.def_path_str(item_def_id);
2920 let short_item_name = with_forced_trimmed_paths!(tcx.def_path_str(item_def_id));
2921 let mut multispan = MultiSpan::from(span);
2922 let sm = tcx.sess.source_map();
2923 if let Some(ident) = tcx.opt_item_ident(item_def_id) {
2924 let same_line =
2925 match (sm.lookup_line(ident.span.hi()), sm.lookup_line(span.lo())) {
2926 (Ok(l), Ok(r)) => l.line == r.line,
2927 _ => true,
2928 };
2929 if ident.span.is_visible(sm) && !ident.span.overlaps(span) && !same_line {
2930 multispan.push_span_label(
2931 ident.span,
2932 format!(
2933 "required by a bound in this {}",
2934 tcx.def_kind(item_def_id).descr(item_def_id)
2935 ),
2936 );
2937 }
2938 }
2939 let mut a = "a";
2940 let mut this = "this bound";
2941 let mut note = None;
2942 let mut help = None;
2943 if let ty::PredicateKind::Clause(clause) = predicate.kind().skip_binder() {
2944 match clause {
2945 ty::ClauseKind::Trait(trait_pred) => {
2946 let def_id = trait_pred.def_id();
2947 let visible_item = if let Some(local) = def_id.as_local() {
2948 let ty = trait_pred.self_ty();
2949 if let ty::Adt(adt, _) = ty.kind() {
2953 let visibilities = &tcx.resolutions(()).effective_visibilities;
2954 visibilities.effective_vis(local).is_none_or(|v| {
2955 v.at_level(Level::Reexported)
2956 .is_accessible_from(adt.did(), tcx)
2957 })
2958 } else {
2959 true
2961 }
2962 } else {
2963 tcx.visible_parent_map(()).get(&def_id).is_some()
2965 };
2966 if tcx.is_lang_item(def_id, LangItem::Sized) {
2967 if tcx
2969 .generics_of(item_def_id)
2970 .own_params
2971 .iter()
2972 .any(|param| tcx.def_span(param.def_id) == span)
2973 {
2974 a = "an implicit `Sized`";
2975 this =
2976 "the implicit `Sized` requirement on this type parameter";
2977 }
2978 if let Some(hir::Node::TraitItem(hir::TraitItem {
2979 generics,
2980 kind: hir::TraitItemKind::Type(bounds, None),
2981 ..
2982 })) = tcx.hir_get_if_local(item_def_id)
2983 && !bounds.iter()
2985 .filter_map(|bound| bound.trait_ref())
2986 .any(|tr| tr.trait_def_id().is_some_and(|def_id| tcx.is_lang_item(def_id, LangItem::Sized)))
2987 {
2988 let (span, separator) = if let [.., last] = bounds {
2989 (last.span().shrink_to_hi(), " +")
2990 } else {
2991 (generics.span.shrink_to_hi(), ":")
2992 };
2993 err.span_suggestion_verbose(
2994 span,
2995 "consider relaxing the implicit `Sized` restriction",
2996 format!("{separator} ?Sized"),
2997 Applicability::MachineApplicable,
2998 );
2999 }
3000 }
3001 if let DefKind::Trait = tcx.def_kind(item_def_id)
3002 && !visible_item
3003 {
3004 note = Some(format!(
3005 "`{short_item_name}` is a \"sealed trait\", because to implement it \
3006 you also need to implement `{}`, which is not accessible; this is \
3007 usually done to force you to use one of the provided types that \
3008 already implement it",
3009 with_no_trimmed_paths!(tcx.def_path_str(def_id)),
3010 ));
3011 let impls_of = tcx.trait_impls_of(def_id);
3012 let impls = impls_of
3013 .non_blanket_impls()
3014 .values()
3015 .flatten()
3016 .chain(impls_of.blanket_impls().iter())
3017 .collect::<Vec<_>>();
3018 if !impls.is_empty() {
3019 let len = impls.len();
3020 let mut types = impls
3021 .iter()
3022 .map(|t| {
3023 with_no_trimmed_paths!(format!(
3024 " {}",
3025 tcx.type_of(*t).instantiate_identity(),
3026 ))
3027 })
3028 .collect::<Vec<_>>();
3029 let post = if types.len() > 9 {
3030 types.truncate(8);
3031 format!("\nand {} others", len - 8)
3032 } else {
3033 String::new()
3034 };
3035 help = Some(format!(
3036 "the following type{} implement{} the trait:\n{}{post}",
3037 pluralize!(len),
3038 if len == 1 { "s" } else { "" },
3039 types.join("\n"),
3040 ));
3041 }
3042 }
3043 }
3044 ty::ClauseKind::ConstArgHasType(..) => {
3045 let descr =
3046 format!("required by a const generic parameter in `{item_name}`");
3047 if span.is_visible(sm) {
3048 let msg = format!(
3049 "required by this const generic parameter in `{short_item_name}`"
3050 );
3051 multispan.push_span_label(span, msg);
3052 err.span_note(multispan, descr);
3053 } else {
3054 err.span_note(tcx.def_span(item_def_id), descr);
3055 }
3056 return;
3057 }
3058 _ => (),
3059 }
3060 }
3061
3062 let is_in_fmt_lit = if let Some(s) = err.span.primary_span() {
3065 matches!(s.desugaring_kind(), Some(DesugaringKind::FormatLiteral { .. }))
3066 } else {
3067 false
3068 };
3069 if !is_in_fmt_lit {
3070 let descr = format!("required by {a} bound in `{item_name}`");
3071 if span.is_visible(sm) {
3072 let msg = format!("required by {this} in `{short_item_name}`");
3073 multispan.push_span_label(span, msg);
3074 err.span_note(multispan, descr);
3075 } else {
3076 err.span_note(tcx.def_span(item_def_id), descr);
3077 }
3078 }
3079 if let Some(note) = note {
3080 err.note(note);
3081 }
3082 if let Some(help) = help {
3083 err.help(help);
3084 }
3085 }
3086 ObligationCauseCode::WhereClause(..)
3087 | ObligationCauseCode::WhereClauseInExpr(..)
3088 | ObligationCauseCode::HostEffectInExpr(..) => {
3089 }
3092 ObligationCauseCode::OpaqueTypeBound(span, definition_def_id) => {
3093 err.span_note(span, "required by a bound in an opaque type");
3094 if let Some(definition_def_id) = definition_def_id
3095 && self.tcx.typeck(definition_def_id).coroutine_stalled_predicates.is_empty()
3099 {
3100 err.span_note(
3103 tcx.def_span(definition_def_id),
3104 "this definition site has more where clauses than the opaque type",
3105 );
3106 }
3107 }
3108 ObligationCauseCode::Coercion { source, target } => {
3109 let source =
3110 tcx.short_string(self.resolve_vars_if_possible(source), err.long_ty_path());
3111 let target =
3112 tcx.short_string(self.resolve_vars_if_possible(target), err.long_ty_path());
3113 err.note(with_forced_trimmed_paths!(format!(
3114 "required for the cast from `{source}` to `{target}`",
3115 )));
3116 }
3117 ObligationCauseCode::RepeatElementCopy { is_constable, elt_span } => {
3118 err.note(
3119 "the `Copy` trait is required because this value will be copied for each element of the array",
3120 );
3121 let sm = tcx.sess.source_map();
3122 if matches!(is_constable, IsConstable::Fn | IsConstable::Ctor)
3123 && let Ok(_) = sm.span_to_snippet(elt_span)
3124 {
3125 err.multipart_suggestion(
3126 "create an inline `const` block",
3127 vec![
3128 (elt_span.shrink_to_lo(), "const { ".to_string()),
3129 (elt_span.shrink_to_hi(), " }".to_string()),
3130 ],
3131 Applicability::MachineApplicable,
3132 );
3133 } else {
3134 err.help("consider using `core::array::from_fn` to initialize the array");
3136 err.help("see https://doc.rust-lang.org/stable/std/array/fn.from_fn.html for more information");
3137 }
3138 }
3139 ObligationCauseCode::VariableType(hir_id) => {
3140 if let Some(typeck_results) = &self.typeck_results
3141 && let Some(ty) = typeck_results.node_type_opt(hir_id)
3142 && let ty::Error(_) = ty.kind()
3143 {
3144 err.note(format!(
3145 "`{predicate}` isn't satisfied, but the type of this pattern is \
3146 `{{type error}}`",
3147 ));
3148 err.downgrade_to_delayed_bug();
3149 }
3150 let mut local = true;
3151 match tcx.parent_hir_node(hir_id) {
3152 Node::LetStmt(hir::LetStmt { ty: Some(ty), .. }) => {
3153 err.span_suggestion_verbose(
3154 ty.span.shrink_to_lo(),
3155 "consider borrowing here",
3156 "&",
3157 Applicability::MachineApplicable,
3158 );
3159 }
3160 Node::LetStmt(hir::LetStmt {
3161 init: Some(hir::Expr { kind: hir::ExprKind::Index(..), span, .. }),
3162 ..
3163 }) => {
3164 err.span_suggestion_verbose(
3168 span.shrink_to_lo(),
3169 "consider borrowing here",
3170 "&",
3171 Applicability::MachineApplicable,
3172 );
3173 }
3174 Node::LetStmt(hir::LetStmt { init: Some(expr), .. }) => {
3175 suggest_remove_deref(err, &expr);
3178 }
3179 Node::Param(param) => {
3180 err.span_suggestion_verbose(
3181 param.ty_span.shrink_to_lo(),
3182 "function arguments must have a statically known size, borrowed types \
3183 always have a known size",
3184 "&",
3185 Applicability::MachineApplicable,
3186 );
3187 local = false;
3188 }
3189 _ => {}
3190 }
3191 if local {
3192 err.note("all local variables must have a statically known size");
3193 }
3194 }
3195 ObligationCauseCode::SizedArgumentType(hir_id) => {
3196 let mut ty = None;
3197 let borrowed_msg = "function arguments must have a statically known size, borrowed \
3198 types always have a known size";
3199 if let Some(hir_id) = hir_id
3200 && let hir::Node::Param(param) = self.tcx.hir_node(hir_id)
3201 && let Some(decl) = self.tcx.parent_hir_node(hir_id).fn_decl()
3202 && let Some(t) = decl.inputs.iter().find(|t| param.ty_span.contains(t.span))
3203 {
3204 ty = Some(t);
3212 } else if let Some(hir_id) = hir_id
3213 && let hir::Node::Ty(t) = self.tcx.hir_node(hir_id)
3214 {
3215 ty = Some(t);
3216 }
3217 if let Some(ty) = ty {
3218 match ty.kind {
3219 hir::TyKind::TraitObject(traits, _) => {
3220 let (span, kw) = match traits {
3221 [first, ..] if first.span.lo() == ty.span.lo() => {
3222 (ty.span.shrink_to_lo(), "dyn ")
3224 }
3225 [first, ..] => (ty.span.until(first.span), ""),
3226 [] => span_bug!(ty.span, "trait object with no traits: {ty:?}"),
3227 };
3228 let needs_parens = traits.len() != 1;
3229 if let Some(hir_id) = hir_id
3231 && matches!(
3232 self.tcx.parent_hir_node(hir_id),
3233 hir::Node::Item(hir::Item {
3234 kind: hir::ItemKind::Fn { .. },
3235 ..
3236 })
3237 )
3238 {
3239 err.span_suggestion_verbose(
3240 span,
3241 "you can use `impl Trait` as the argument type",
3242 "impl ",
3243 Applicability::MaybeIncorrect,
3244 );
3245 }
3246 let sugg = if !needs_parens {
3247 vec![(span.shrink_to_lo(), format!("&{kw}"))]
3248 } else {
3249 vec![
3250 (span.shrink_to_lo(), format!("&({kw}")),
3251 (ty.span.shrink_to_hi(), ")".to_string()),
3252 ]
3253 };
3254 err.multipart_suggestion_verbose(
3255 borrowed_msg,
3256 sugg,
3257 Applicability::MachineApplicable,
3258 );
3259 }
3260 hir::TyKind::Slice(_ty) => {
3261 err.span_suggestion_verbose(
3262 ty.span.shrink_to_lo(),
3263 "function arguments must have a statically known size, borrowed \
3264 slices always have a known size",
3265 "&",
3266 Applicability::MachineApplicable,
3267 );
3268 }
3269 hir::TyKind::Path(_) => {
3270 err.span_suggestion_verbose(
3271 ty.span.shrink_to_lo(),
3272 borrowed_msg,
3273 "&",
3274 Applicability::MachineApplicable,
3275 );
3276 }
3277 _ => {}
3278 }
3279 } else {
3280 err.note("all function arguments must have a statically known size");
3281 }
3282 if tcx.sess.opts.unstable_features.is_nightly_build()
3283 && !tcx.features().unsized_fn_params()
3284 {
3285 err.help("unsized fn params are gated as an unstable feature");
3286 }
3287 }
3288 ObligationCauseCode::SizedReturnType | ObligationCauseCode::SizedCallReturnType => {
3289 err.note("the return type of a function must have a statically known size");
3290 }
3291 ObligationCauseCode::SizedYieldType => {
3292 err.note("the yield type of a coroutine must have a statically known size");
3293 }
3294 ObligationCauseCode::AssignmentLhsSized => {
3295 err.note("the left-hand-side of an assignment must have a statically known size");
3296 }
3297 ObligationCauseCode::TupleInitializerSized => {
3298 err.note("tuples must have a statically known size to be initialized");
3299 }
3300 ObligationCauseCode::StructInitializerSized => {
3301 err.note("structs must have a statically known size to be initialized");
3302 }
3303 ObligationCauseCode::FieldSized { adt_kind: ref item, last, span } => {
3304 match *item {
3305 AdtKind::Struct => {
3306 if last {
3307 err.note(
3308 "the last field of a packed struct may only have a \
3309 dynamically sized type if it does not need drop to be run",
3310 );
3311 } else {
3312 err.note(
3313 "only the last field of a struct may have a dynamically sized type",
3314 );
3315 }
3316 }
3317 AdtKind::Union => {
3318 err.note("no field of a union may have a dynamically sized type");
3319 }
3320 AdtKind::Enum => {
3321 err.note("no field of an enum variant may have a dynamically sized type");
3322 }
3323 }
3324 err.help("change the field's type to have a statically known size");
3325 err.span_suggestion_verbose(
3326 span.shrink_to_lo(),
3327 "borrowed types always have a statically known size",
3328 "&",
3329 Applicability::MachineApplicable,
3330 );
3331 err.multipart_suggestion_verbose(
3332 "the `Box` type always has a statically known size and allocates its contents \
3333 in the heap",
3334 vec![
3335 (span.shrink_to_lo(), "Box<".to_string()),
3336 (span.shrink_to_hi(), ">".to_string()),
3337 ],
3338 Applicability::MachineApplicable,
3339 );
3340 }
3341 ObligationCauseCode::SizedConstOrStatic => {
3342 err.note("statics and constants must have a statically known size");
3343 }
3344 ObligationCauseCode::InlineAsmSized => {
3345 err.note("all inline asm arguments must have a statically known size");
3346 }
3347 ObligationCauseCode::SizedClosureCapture(closure_def_id) => {
3348 err.note(
3349 "all values captured by value by a closure must have a statically known size",
3350 );
3351 let hir::ExprKind::Closure(closure) =
3352 tcx.hir_node_by_def_id(closure_def_id).expect_expr().kind
3353 else {
3354 bug!("expected closure in SizedClosureCapture obligation");
3355 };
3356 if let hir::CaptureBy::Value { .. } = closure.capture_clause
3357 && let Some(span) = closure.fn_arg_span
3358 {
3359 err.span_label(span, "this closure captures all values by move");
3360 }
3361 }
3362 ObligationCauseCode::SizedCoroutineInterior(coroutine_def_id) => {
3363 let what = match tcx.coroutine_kind(coroutine_def_id) {
3364 None
3365 | Some(hir::CoroutineKind::Coroutine(_))
3366 | Some(hir::CoroutineKind::Desugared(hir::CoroutineDesugaring::Gen, _)) => {
3367 "yield"
3368 }
3369 Some(hir::CoroutineKind::Desugared(hir::CoroutineDesugaring::Async, _)) => {
3370 "await"
3371 }
3372 Some(hir::CoroutineKind::Desugared(hir::CoroutineDesugaring::AsyncGen, _)) => {
3373 "yield`/`await"
3374 }
3375 };
3376 err.note(format!(
3377 "all values live across `{what}` must have a statically known size"
3378 ));
3379 }
3380 ObligationCauseCode::SharedStatic => {
3381 err.note("shared static variables must have a type that implements `Sync`");
3382 }
3383 ObligationCauseCode::BuiltinDerived(ref data) => {
3384 let parent_trait_ref = self.resolve_vars_if_possible(data.parent_trait_pred);
3385 let ty = parent_trait_ref.skip_binder().self_ty();
3386 if parent_trait_ref.references_error() {
3387 err.downgrade_to_delayed_bug();
3390 return;
3391 }
3392
3393 let is_upvar_tys_infer_tuple = if !matches!(ty.kind(), ty::Tuple(..)) {
3396 false
3397 } else if let ObligationCauseCode::BuiltinDerived(data) = &*data.parent_code {
3398 let parent_trait_ref = self.resolve_vars_if_possible(data.parent_trait_pred);
3399 let nested_ty = parent_trait_ref.skip_binder().self_ty();
3400 matches!(nested_ty.kind(), ty::Coroutine(..))
3401 || matches!(nested_ty.kind(), ty::Closure(..))
3402 } else {
3403 false
3404 };
3405
3406 let is_builtin_async_fn_trait =
3407 tcx.async_fn_trait_kind_from_def_id(data.parent_trait_pred.def_id()).is_some();
3408
3409 if !is_upvar_tys_infer_tuple && !is_builtin_async_fn_trait {
3410 let mut msg = || {
3411 let ty_str = tcx.short_string(ty, err.long_ty_path());
3412 format!("required because it appears within the type `{ty_str}`")
3413 };
3414 match ty.kind() {
3415 ty::Adt(def, _) => {
3416 let msg = msg();
3417 match tcx.opt_item_ident(def.did()) {
3418 Some(ident) => {
3419 err.span_note(ident.span, msg);
3420 }
3421 None => {
3422 err.note(msg);
3423 }
3424 }
3425 }
3426 ty::Alias(ty::Opaque, ty::AliasTy { def_id, .. }) => {
3427 let is_future = tcx.ty_is_opaque_future(ty);
3430 debug!(
3431 ?obligated_types,
3432 ?is_future,
3433 "note_obligation_cause_code: check for async fn"
3434 );
3435 if is_future
3436 && obligated_types.last().is_some_and(|ty| match ty.kind() {
3437 ty::Coroutine(last_def_id, ..) => {
3438 tcx.coroutine_is_async(*last_def_id)
3439 }
3440 _ => false,
3441 })
3442 {
3443 } else {
3445 let msg = msg();
3446 err.span_note(tcx.def_span(def_id), msg);
3447 }
3448 }
3449 ty::Coroutine(def_id, _) => {
3450 let sp = tcx.def_span(def_id);
3451
3452 let kind = tcx.coroutine_kind(def_id).unwrap();
3454 err.span_note(
3455 sp,
3456 with_forced_trimmed_paths!(format!(
3457 "required because it's used within this {kind:#}",
3458 )),
3459 );
3460 }
3461 ty::CoroutineWitness(..) => {
3462 }
3465 ty::Closure(def_id, _) | ty::CoroutineClosure(def_id, _) => {
3466 err.span_note(
3467 tcx.def_span(def_id),
3468 "required because it's used within this closure",
3469 );
3470 }
3471 ty::Str => {
3472 err.note("`str` is considered to contain a `[u8]` slice for auto trait purposes");
3473 }
3474 _ => {
3475 let msg = msg();
3476 err.note(msg);
3477 }
3478 };
3479 }
3480
3481 obligated_types.push(ty);
3482
3483 let parent_predicate = parent_trait_ref;
3484 if !self.is_recursive_obligation(obligated_types, &data.parent_code) {
3485 ensure_sufficient_stack(|| {
3487 self.note_obligation_cause_code(
3488 body_id,
3489 err,
3490 parent_predicate,
3491 param_env,
3492 &data.parent_code,
3493 obligated_types,
3494 seen_requirements,
3495 )
3496 });
3497 } else {
3498 ensure_sufficient_stack(|| {
3499 self.note_obligation_cause_code(
3500 body_id,
3501 err,
3502 parent_predicate,
3503 param_env,
3504 cause_code.peel_derives(),
3505 obligated_types,
3506 seen_requirements,
3507 )
3508 });
3509 }
3510 }
3511 ObligationCauseCode::ImplDerived(ref data) => {
3512 let mut parent_trait_pred =
3513 self.resolve_vars_if_possible(data.derived.parent_trait_pred);
3514 let parent_def_id = parent_trait_pred.def_id();
3515 if tcx.is_diagnostic_item(sym::FromResidual, parent_def_id)
3516 && !tcx.features().enabled(sym::try_trait_v2)
3517 {
3518 return;
3522 }
3523 if tcx.is_diagnostic_item(sym::PinDerefMutHelper, parent_def_id) {
3524 let parent_predicate =
3525 self.resolve_vars_if_possible(data.derived.parent_trait_pred);
3526
3527 ensure_sufficient_stack(|| {
3529 self.note_obligation_cause_code(
3530 body_id,
3531 err,
3532 parent_predicate,
3533 param_env,
3534 &data.derived.parent_code,
3535 obligated_types,
3536 seen_requirements,
3537 )
3538 });
3539 return;
3540 }
3541 let self_ty_str =
3542 tcx.short_string(parent_trait_pred.skip_binder().self_ty(), err.long_ty_path());
3543 let trait_name = tcx.short_string(
3544 parent_trait_pred.print_modifiers_and_trait_path(),
3545 err.long_ty_path(),
3546 );
3547 let msg = format!("required for `{self_ty_str}` to implement `{trait_name}`");
3548 let mut is_auto_trait = false;
3549 match tcx.hir_get_if_local(data.impl_or_alias_def_id) {
3550 Some(Node::Item(hir::Item {
3551 kind: hir::ItemKind::Trait(_, is_auto, _, ident, ..),
3552 ..
3553 })) => {
3554 is_auto_trait = matches!(is_auto, hir::IsAuto::Yes);
3557 err.span_note(ident.span, msg);
3558 }
3559 Some(Node::Item(hir::Item {
3560 kind: hir::ItemKind::Impl(hir::Impl { of_trait, self_ty, generics, .. }),
3561 ..
3562 })) => {
3563 let mut spans = Vec::with_capacity(2);
3564 if let Some(of_trait) = of_trait {
3565 spans.push(of_trait.trait_ref.path.span);
3566 }
3567 spans.push(self_ty.span);
3568 let mut spans: MultiSpan = spans.into();
3569 if matches!(
3570 self_ty.span.ctxt().outer_expn_data().kind,
3571 ExpnKind::Macro(MacroKind::Derive, _)
3572 ) || matches!(
3573 of_trait.map(|t| t.trait_ref.path.span.ctxt().outer_expn_data().kind),
3574 Some(ExpnKind::Macro(MacroKind::Derive, _))
3575 ) {
3576 spans.push_span_label(
3577 data.span,
3578 "unsatisfied trait bound introduced in this `derive` macro",
3579 );
3580 } else if !data.span.is_dummy() && !data.span.overlaps(self_ty.span) {
3581 spans.push_span_label(
3582 data.span,
3583 "unsatisfied trait bound introduced here",
3584 );
3585 }
3586 err.span_note(spans, msg);
3587 point_at_assoc_type_restriction(
3588 tcx,
3589 err,
3590 &self_ty_str,
3591 &trait_name,
3592 predicate,
3593 &generics,
3594 &data,
3595 );
3596 }
3597 _ => {
3598 err.note(msg);
3599 }
3600 };
3601
3602 let mut parent_predicate = parent_trait_pred;
3603 let mut data = &data.derived;
3604 let mut count = 0;
3605 seen_requirements.insert(parent_def_id);
3606 if is_auto_trait {
3607 while let ObligationCauseCode::BuiltinDerived(derived) = &*data.parent_code {
3610 let child_trait_ref =
3611 self.resolve_vars_if_possible(derived.parent_trait_pred);
3612 let child_def_id = child_trait_ref.def_id();
3613 if seen_requirements.insert(child_def_id) {
3614 break;
3615 }
3616 data = derived;
3617 parent_predicate = child_trait_ref.upcast(tcx);
3618 parent_trait_pred = child_trait_ref;
3619 }
3620 }
3621 while let ObligationCauseCode::ImplDerived(child) = &*data.parent_code {
3622 let child_trait_pred =
3624 self.resolve_vars_if_possible(child.derived.parent_trait_pred);
3625 let child_def_id = child_trait_pred.def_id();
3626 if seen_requirements.insert(child_def_id) {
3627 break;
3628 }
3629 count += 1;
3630 data = &child.derived;
3631 parent_predicate = child_trait_pred.upcast(tcx);
3632 parent_trait_pred = child_trait_pred;
3633 }
3634 if count > 0 {
3635 err.note(format!(
3636 "{} redundant requirement{} hidden",
3637 count,
3638 pluralize!(count)
3639 ));
3640 let self_ty = tcx.short_string(
3641 parent_trait_pred.skip_binder().self_ty(),
3642 err.long_ty_path(),
3643 );
3644 let trait_path = tcx.short_string(
3645 parent_trait_pred.print_modifiers_and_trait_path(),
3646 err.long_ty_path(),
3647 );
3648 err.note(format!("required for `{self_ty}` to implement `{trait_path}`"));
3649 }
3650 ensure_sufficient_stack(|| {
3652 self.note_obligation_cause_code(
3653 body_id,
3654 err,
3655 parent_predicate,
3656 param_env,
3657 &data.parent_code,
3658 obligated_types,
3659 seen_requirements,
3660 )
3661 });
3662 }
3663 ObligationCauseCode::ImplDerivedHost(ref data) => {
3664 let self_ty = tcx.short_string(
3665 self.resolve_vars_if_possible(data.derived.parent_host_pred.self_ty()),
3666 err.long_ty_path(),
3667 );
3668 let trait_path = tcx.short_string(
3669 data.derived
3670 .parent_host_pred
3671 .map_bound(|pred| pred.trait_ref)
3672 .print_only_trait_path(),
3673 err.long_ty_path(),
3674 );
3675 let msg = format!(
3676 "required for `{self_ty}` to implement `{} {trait_path}`",
3677 data.derived.parent_host_pred.skip_binder().constness,
3678 );
3679 match tcx.hir_get_if_local(data.impl_def_id) {
3680 Some(Node::Item(hir::Item {
3681 kind: hir::ItemKind::Impl(hir::Impl { of_trait, self_ty, .. }),
3682 ..
3683 })) => {
3684 let mut spans = vec![self_ty.span];
3685 spans.extend(of_trait.map(|t| t.trait_ref.path.span));
3686 let mut spans: MultiSpan = spans.into();
3687 spans.push_span_label(data.span, "unsatisfied trait bound introduced here");
3688 err.span_note(spans, msg);
3689 }
3690 _ => {
3691 err.note(msg);
3692 }
3693 }
3694 ensure_sufficient_stack(|| {
3695 self.note_obligation_cause_code(
3696 body_id,
3697 err,
3698 data.derived.parent_host_pred,
3699 param_env,
3700 &data.derived.parent_code,
3701 obligated_types,
3702 seen_requirements,
3703 )
3704 });
3705 }
3706 ObligationCauseCode::BuiltinDerivedHost(ref data) => {
3707 ensure_sufficient_stack(|| {
3708 self.note_obligation_cause_code(
3709 body_id,
3710 err,
3711 data.parent_host_pred,
3712 param_env,
3713 &data.parent_code,
3714 obligated_types,
3715 seen_requirements,
3716 )
3717 });
3718 }
3719 ObligationCauseCode::WellFormedDerived(ref data) => {
3720 let parent_trait_ref = self.resolve_vars_if_possible(data.parent_trait_pred);
3721 let parent_predicate = parent_trait_ref;
3722 ensure_sufficient_stack(|| {
3724 self.note_obligation_cause_code(
3725 body_id,
3726 err,
3727 parent_predicate,
3728 param_env,
3729 &data.parent_code,
3730 obligated_types,
3731 seen_requirements,
3732 )
3733 });
3734 }
3735 ObligationCauseCode::TypeAlias(ref nested, span, def_id) => {
3736 ensure_sufficient_stack(|| {
3738 self.note_obligation_cause_code(
3739 body_id,
3740 err,
3741 predicate,
3742 param_env,
3743 nested,
3744 obligated_types,
3745 seen_requirements,
3746 )
3747 });
3748 let mut multispan = MultiSpan::from(span);
3749 multispan.push_span_label(span, "required by this bound");
3750 err.span_note(
3751 multispan,
3752 format!("required by a bound on the type alias `{}`", tcx.item_name(def_id)),
3753 );
3754 }
3755 ObligationCauseCode::FunctionArg {
3756 arg_hir_id, call_hir_id, ref parent_code, ..
3757 } => {
3758 self.note_function_argument_obligation(
3759 body_id,
3760 err,
3761 arg_hir_id,
3762 parent_code,
3763 param_env,
3764 predicate,
3765 call_hir_id,
3766 );
3767 ensure_sufficient_stack(|| {
3768 self.note_obligation_cause_code(
3769 body_id,
3770 err,
3771 predicate,
3772 param_env,
3773 parent_code,
3774 obligated_types,
3775 seen_requirements,
3776 )
3777 });
3778 }
3779 ObligationCauseCode::CompareImplItem { trait_item_def_id, .. }
3782 if tcx.is_impl_trait_in_trait(trait_item_def_id) => {}
3783 ObligationCauseCode::CompareImplItem { trait_item_def_id, kind, .. } => {
3784 let item_name = tcx.item_name(trait_item_def_id);
3785 let msg = format!(
3786 "the requirement `{predicate}` appears on the `impl`'s {kind} \
3787 `{item_name}` but not on the corresponding trait's {kind}",
3788 );
3789 let sp = tcx
3790 .opt_item_ident(trait_item_def_id)
3791 .map(|i| i.span)
3792 .unwrap_or_else(|| tcx.def_span(trait_item_def_id));
3793 let mut assoc_span: MultiSpan = sp.into();
3794 assoc_span.push_span_label(
3795 sp,
3796 format!("this trait's {kind} doesn't have the requirement `{predicate}`"),
3797 );
3798 if let Some(ident) = tcx
3799 .opt_associated_item(trait_item_def_id)
3800 .and_then(|i| tcx.opt_item_ident(i.container_id(tcx)))
3801 {
3802 assoc_span.push_span_label(ident.span, "in this trait");
3803 }
3804 err.span_note(assoc_span, msg);
3805 }
3806 ObligationCauseCode::TrivialBound => {
3807 err.help("see issue #48214");
3808 tcx.disabled_nightly_features(err, [(String::new(), sym::trivial_bounds)]);
3809 }
3810 ObligationCauseCode::OpaqueReturnType(expr_info) => {
3811 let (expr_ty, expr) = if let Some((expr_ty, hir_id)) = expr_info {
3812 let expr_ty = tcx.short_string(expr_ty, err.long_ty_path());
3813 let expr = tcx.hir_expect_expr(hir_id);
3814 (expr_ty, expr)
3815 } else if let Some(body_id) = tcx.hir_node_by_def_id(body_id).body_id()
3816 && let body = tcx.hir_body(body_id)
3817 && let hir::ExprKind::Block(block, _) = body.value.kind
3818 && let Some(expr) = block.expr
3819 && let Some(expr_ty) = self
3820 .typeck_results
3821 .as_ref()
3822 .and_then(|typeck| typeck.node_type_opt(expr.hir_id))
3823 && let Some(pred) = predicate.as_clause()
3824 && let ty::ClauseKind::Trait(pred) = pred.kind().skip_binder()
3825 && self.can_eq(param_env, pred.self_ty(), expr_ty)
3826 {
3827 let expr_ty = tcx.short_string(expr_ty, err.long_ty_path());
3828 (expr_ty, expr)
3829 } else {
3830 return;
3831 };
3832 err.span_label(
3833 expr.span,
3834 with_forced_trimmed_paths!(format!(
3835 "return type was inferred to be `{expr_ty}` here",
3836 )),
3837 );
3838 suggest_remove_deref(err, &expr);
3839 }
3840 ObligationCauseCode::UnsizedNonPlaceExpr(span) => {
3841 err.span_note(
3842 span,
3843 "unsized values must be place expressions and cannot be put in temporaries",
3844 );
3845 }
3846 ObligationCauseCode::CompareEii { .. } => {
3847 panic!("trait bounds on EII not yet supported ")
3848 }
3849 }
3850 }
3851
3852 #[instrument(
3853 level = "debug", skip(self, err), fields(trait_pred.self_ty = ?trait_pred.self_ty())
3854 )]
3855 pub(super) fn suggest_await_before_try(
3856 &self,
3857 err: &mut Diag<'_>,
3858 obligation: &PredicateObligation<'tcx>,
3859 trait_pred: ty::PolyTraitPredicate<'tcx>,
3860 span: Span,
3861 ) {
3862 let future_trait = self.tcx.require_lang_item(LangItem::Future, span);
3863
3864 let self_ty = self.resolve_vars_if_possible(trait_pred.self_ty());
3865 let impls_future = self.type_implements_trait(
3866 future_trait,
3867 [self.tcx.instantiate_bound_regions_with_erased(self_ty)],
3868 obligation.param_env,
3869 );
3870 if !impls_future.must_apply_modulo_regions() {
3871 return;
3872 }
3873
3874 let item_def_id = self.tcx.associated_item_def_ids(future_trait)[0];
3875 let projection_ty = trait_pred.map_bound(|trait_pred| {
3877 Ty::new_projection(
3878 self.tcx,
3879 item_def_id,
3880 [trait_pred.self_ty()],
3882 )
3883 });
3884 let InferOk { value: projection_ty, .. } =
3885 self.at(&obligation.cause, obligation.param_env).normalize(projection_ty);
3886
3887 debug!(
3888 normalized_projection_type = ?self.resolve_vars_if_possible(projection_ty)
3889 );
3890 let try_obligation = self.mk_trait_obligation_with_new_self_ty(
3891 obligation.param_env,
3892 trait_pred.map_bound(|trait_pred| (trait_pred, projection_ty.skip_binder())),
3893 );
3894 debug!(try_trait_obligation = ?try_obligation);
3895 if self.predicate_may_hold(&try_obligation)
3896 && let Ok(snippet) = self.tcx.sess.source_map().span_to_snippet(span)
3897 && snippet.ends_with('?')
3898 {
3899 match self.tcx.coroutine_kind(obligation.cause.body_id) {
3900 Some(hir::CoroutineKind::Desugared(hir::CoroutineDesugaring::Async, _)) => {
3901 err.span_suggestion_verbose(
3902 span.with_hi(span.hi() - BytePos(1)).shrink_to_hi(),
3903 "consider `await`ing on the `Future`",
3904 ".await",
3905 Applicability::MaybeIncorrect,
3906 );
3907 }
3908 _ => {
3909 let mut span: MultiSpan = span.with_lo(span.hi() - BytePos(1)).into();
3910 span.push_span_label(
3911 self.tcx.def_span(obligation.cause.body_id),
3912 "this is not `async`",
3913 );
3914 err.span_note(
3915 span,
3916 "this implements `Future` and its output type supports \
3917 `?`, but the future cannot be awaited in a synchronous function",
3918 );
3919 }
3920 }
3921 }
3922 }
3923
3924 pub(super) fn suggest_floating_point_literal(
3925 &self,
3926 obligation: &PredicateObligation<'tcx>,
3927 err: &mut Diag<'_>,
3928 trait_pred: ty::PolyTraitPredicate<'tcx>,
3929 ) {
3930 let rhs_span = match obligation.cause.code() {
3931 ObligationCauseCode::BinOp { rhs_span, rhs_is_lit, .. } if *rhs_is_lit => rhs_span,
3932 _ => return,
3933 };
3934 if let ty::Float(_) = trait_pred.skip_binder().self_ty().kind()
3935 && let ty::Infer(InferTy::IntVar(_)) =
3936 trait_pred.skip_binder().trait_ref.args.type_at(1).kind()
3937 {
3938 err.span_suggestion_verbose(
3939 rhs_span.shrink_to_hi(),
3940 "consider using a floating-point literal by writing it with `.0`",
3941 ".0",
3942 Applicability::MaybeIncorrect,
3943 );
3944 }
3945 }
3946
3947 pub fn can_suggest_derive(
3948 &self,
3949 obligation: &PredicateObligation<'tcx>,
3950 trait_pred: ty::PolyTraitPredicate<'tcx>,
3951 ) -> bool {
3952 if trait_pred.polarity() == ty::PredicatePolarity::Negative {
3953 return false;
3954 }
3955 let Some(diagnostic_name) = self.tcx.get_diagnostic_name(trait_pred.def_id()) else {
3956 return false;
3957 };
3958 let (adt, args) = match trait_pred.skip_binder().self_ty().kind() {
3959 ty::Adt(adt, args) if adt.did().is_local() => (adt, args),
3960 _ => return false,
3961 };
3962 let is_derivable_trait = match diagnostic_name {
3963 sym::Default => !adt.is_enum(),
3964 sym::PartialEq | sym::PartialOrd => {
3965 let rhs_ty = trait_pred.skip_binder().trait_ref.args.type_at(1);
3966 trait_pred.skip_binder().self_ty() == rhs_ty
3967 }
3968 sym::Eq | sym::Ord | sym::Clone | sym::Copy | sym::Hash | sym::Debug => true,
3969 _ => false,
3970 };
3971 is_derivable_trait &&
3972 adt.all_fields().all(|field| {
3974 let field_ty = ty::GenericArg::from(field.ty(self.tcx, args));
3975 let trait_args = match diagnostic_name {
3976 sym::PartialEq | sym::PartialOrd => {
3977 Some(field_ty)
3978 }
3979 _ => None,
3980 };
3981 let trait_pred = trait_pred.map_bound_ref(|tr| ty::TraitPredicate {
3982 trait_ref: ty::TraitRef::new(self.tcx,
3983 trait_pred.def_id(),
3984 [field_ty].into_iter().chain(trait_args),
3985 ),
3986 ..*tr
3987 });
3988 let field_obl = Obligation::new(
3989 self.tcx,
3990 obligation.cause.clone(),
3991 obligation.param_env,
3992 trait_pred,
3993 );
3994 self.predicate_must_hold_modulo_regions(&field_obl)
3995 })
3996 }
3997
3998 pub fn suggest_derive(
3999 &self,
4000 obligation: &PredicateObligation<'tcx>,
4001 err: &mut Diag<'_>,
4002 trait_pred: ty::PolyTraitPredicate<'tcx>,
4003 ) {
4004 let Some(diagnostic_name) = self.tcx.get_diagnostic_name(trait_pred.def_id()) else {
4005 return;
4006 };
4007 let adt = match trait_pred.skip_binder().self_ty().kind() {
4008 ty::Adt(adt, _) if adt.did().is_local() => adt,
4009 _ => return,
4010 };
4011 if self.can_suggest_derive(obligation, trait_pred) {
4012 err.span_suggestion_verbose(
4013 self.tcx.def_span(adt.did()).shrink_to_lo(),
4014 format!(
4015 "consider annotating `{}` with `#[derive({})]`",
4016 trait_pred.skip_binder().self_ty(),
4017 diagnostic_name,
4018 ),
4019 format!("#[derive({diagnostic_name})]\n"),
4021 Applicability::MaybeIncorrect,
4022 );
4023 }
4024 }
4025
4026 pub(super) fn suggest_dereferencing_index(
4027 &self,
4028 obligation: &PredicateObligation<'tcx>,
4029 err: &mut Diag<'_>,
4030 trait_pred: ty::PolyTraitPredicate<'tcx>,
4031 ) {
4032 if let ObligationCauseCode::ImplDerived(_) = obligation.cause.code()
4033 && self
4034 .tcx
4035 .is_diagnostic_item(sym::SliceIndex, trait_pred.skip_binder().trait_ref.def_id)
4036 && let ty::Slice(_) = trait_pred.skip_binder().trait_ref.args.type_at(1).kind()
4037 && let ty::Ref(_, inner_ty, _) = trait_pred.skip_binder().self_ty().kind()
4038 && let ty::Uint(ty::UintTy::Usize) = inner_ty.kind()
4039 {
4040 err.span_suggestion_verbose(
4041 obligation.cause.span.shrink_to_lo(),
4042 "dereference this index",
4043 '*',
4044 Applicability::MachineApplicable,
4045 );
4046 }
4047 }
4048
4049 fn note_function_argument_obligation<G: EmissionGuarantee>(
4050 &self,
4051 body_id: LocalDefId,
4052 err: &mut Diag<'_, G>,
4053 arg_hir_id: HirId,
4054 parent_code: &ObligationCauseCode<'tcx>,
4055 param_env: ty::ParamEnv<'tcx>,
4056 failed_pred: ty::Predicate<'tcx>,
4057 call_hir_id: HirId,
4058 ) {
4059 let tcx = self.tcx;
4060 if let Node::Expr(expr) = tcx.hir_node(arg_hir_id)
4061 && let Some(typeck_results) = &self.typeck_results
4062 {
4063 if let hir::Expr { kind: hir::ExprKind::MethodCall(_, rcvr, _, _), .. } = expr
4064 && let Some(ty) = typeck_results.node_type_opt(rcvr.hir_id)
4065 && let Some(failed_pred) = failed_pred.as_trait_clause()
4066 && let pred = failed_pred.map_bound(|pred| pred.with_replaced_self_ty(tcx, ty))
4067 && self.predicate_must_hold_modulo_regions(&Obligation::misc(
4068 tcx, expr.span, body_id, param_env, pred,
4069 ))
4070 && expr.span.hi() != rcvr.span.hi()
4071 {
4072 let should_sugg = match tcx.hir_node(call_hir_id) {
4073 Node::Expr(hir::Expr {
4074 kind: hir::ExprKind::MethodCall(_, call_receiver, _, _),
4075 ..
4076 }) if let Some((DefKind::AssocFn, did)) =
4077 typeck_results.type_dependent_def(call_hir_id)
4078 && call_receiver.hir_id == arg_hir_id =>
4079 {
4080 if tcx.inherent_impl_of_assoc(did).is_some() {
4084 Some(ty) == typeck_results.node_type_opt(arg_hir_id)
4086 } else {
4087 let trait_id = tcx
4089 .trait_of_assoc(did)
4090 .unwrap_or_else(|| tcx.impl_trait_id(tcx.parent(did)));
4091 let args = typeck_results.node_args(call_hir_id);
4092 let tr = ty::TraitRef::from_assoc(tcx, trait_id, args)
4093 .with_replaced_self_ty(tcx, ty);
4094 self.type_implements_trait(tr.def_id, tr.args, param_env)
4095 .must_apply_modulo_regions()
4096 }
4097 }
4098 _ => true,
4099 };
4100
4101 if should_sugg {
4102 err.span_suggestion_verbose(
4103 expr.span.with_lo(rcvr.span.hi()),
4104 format!(
4105 "consider removing this method call, as the receiver has type `{ty}` and \
4106 `{pred}` trivially holds",
4107 ),
4108 "",
4109 Applicability::MaybeIncorrect,
4110 );
4111 }
4112 }
4113 if let hir::Expr { kind: hir::ExprKind::Block(block, _), .. } = expr {
4114 let inner_expr = expr.peel_blocks();
4115 let ty = typeck_results
4116 .expr_ty_adjusted_opt(inner_expr)
4117 .unwrap_or(Ty::new_misc_error(tcx));
4118 let span = inner_expr.span;
4119 if Some(span) != err.span.primary_span()
4120 && !span.in_external_macro(tcx.sess.source_map())
4121 {
4122 err.span_label(
4123 span,
4124 if ty.references_error() {
4125 String::new()
4126 } else {
4127 let ty = with_forced_trimmed_paths!(self.ty_to_string(ty));
4128 format!("this tail expression is of type `{ty}`")
4129 },
4130 );
4131 if let ty::PredicateKind::Clause(clause) = failed_pred.kind().skip_binder()
4132 && let ty::ClauseKind::Trait(pred) = clause
4133 && tcx.fn_trait_kind_from_def_id(pred.def_id()).is_some()
4134 {
4135 if let [stmt, ..] = block.stmts
4136 && let hir::StmtKind::Semi(value) = stmt.kind
4137 && let hir::ExprKind::Closure(hir::Closure {
4138 body, fn_decl_span, ..
4139 }) = value.kind
4140 && let body = tcx.hir_body(*body)
4141 && !matches!(body.value.kind, hir::ExprKind::Block(..))
4142 {
4143 err.multipart_suggestion(
4146 "you might have meant to open the closure body instead of placing \
4147 a closure within a block",
4148 vec![
4149 (expr.span.with_hi(value.span.lo()), String::new()),
4150 (fn_decl_span.shrink_to_hi(), " {".to_string()),
4151 ],
4152 Applicability::MaybeIncorrect,
4153 );
4154 } else {
4155 err.span_suggestion_verbose(
4157 expr.span.shrink_to_lo(),
4158 "you might have meant to create the closure instead of a block",
4159 format!(
4160 "|{}| ",
4161 (0..pred.trait_ref.args.len() - 1)
4162 .map(|_| "_")
4163 .collect::<Vec<_>>()
4164 .join(", ")
4165 ),
4166 Applicability::MaybeIncorrect,
4167 );
4168 }
4169 }
4170 }
4171 }
4172
4173 let mut type_diffs = vec![];
4178 if let ObligationCauseCode::WhereClauseInExpr(def_id, _, _, idx) = parent_code
4179 && let Some(node_args) = typeck_results.node_args_opt(call_hir_id)
4180 && let where_clauses =
4181 self.tcx.predicates_of(def_id).instantiate(self.tcx, node_args)
4182 && let Some(where_pred) = where_clauses.predicates.get(*idx)
4183 {
4184 if let Some(where_pred) = where_pred.as_trait_clause()
4185 && let Some(failed_pred) = failed_pred.as_trait_clause()
4186 && where_pred.def_id() == failed_pred.def_id()
4187 {
4188 self.enter_forall(where_pred, |where_pred| {
4189 let failed_pred = self.instantiate_binder_with_fresh_vars(
4190 expr.span,
4191 BoundRegionConversionTime::FnCall,
4192 failed_pred,
4193 );
4194
4195 let zipped =
4196 iter::zip(where_pred.trait_ref.args, failed_pred.trait_ref.args);
4197 for (expected, actual) in zipped {
4198 self.probe(|_| {
4199 match self
4200 .at(&ObligationCause::misc(expr.span, body_id), param_env)
4201 .eq(DefineOpaqueTypes::Yes, expected, actual)
4204 {
4205 Ok(_) => (), Err(err) => type_diffs.push(err),
4207 }
4208 })
4209 }
4210 })
4211 } else if let Some(where_pred) = where_pred.as_projection_clause()
4212 && let Some(failed_pred) = failed_pred.as_projection_clause()
4213 && let Some(found) = failed_pred.skip_binder().term.as_type()
4214 {
4215 type_diffs = vec![TypeError::Sorts(ty::error::ExpectedFound {
4216 expected: where_pred
4217 .skip_binder()
4218 .projection_term
4219 .expect_ty(self.tcx)
4220 .to_ty(self.tcx),
4221 found,
4222 })];
4223 }
4224 }
4225 if let hir::ExprKind::Path(hir::QPath::Resolved(None, path)) = expr.kind
4226 && let hir::Path { res: Res::Local(hir_id), .. } = path
4227 && let hir::Node::Pat(binding) = self.tcx.hir_node(*hir_id)
4228 && let hir::Node::LetStmt(local) = self.tcx.parent_hir_node(binding.hir_id)
4229 && let Some(binding_expr) = local.init
4230 {
4231 self.point_at_chain(binding_expr, typeck_results, type_diffs, param_env, err);
4235 } else {
4236 self.point_at_chain(expr, typeck_results, type_diffs, param_env, err);
4237 }
4238 }
4239 let call_node = tcx.hir_node(call_hir_id);
4240 if let Node::Expr(hir::Expr { kind: hir::ExprKind::MethodCall(path, rcvr, ..), .. }) =
4241 call_node
4242 {
4243 if Some(rcvr.span) == err.span.primary_span() {
4244 err.replace_span_with(path.ident.span, true);
4245 }
4246 }
4247
4248 if let Node::Expr(expr) = call_node {
4249 if let hir::ExprKind::Call(hir::Expr { span, .. }, _)
4250 | hir::ExprKind::MethodCall(
4251 hir::PathSegment { ident: Ident { span, .. }, .. },
4252 ..,
4253 ) = expr.kind
4254 {
4255 if Some(*span) != err.span.primary_span() {
4256 let msg = if span.is_desugaring(DesugaringKind::FormatLiteral { source: true })
4257 {
4258 "required by this formatting parameter"
4259 } else if span.is_desugaring(DesugaringKind::FormatLiteral { source: false }) {
4260 "required by a formatting parameter in this expression"
4261 } else {
4262 "required by a bound introduced by this call"
4263 };
4264 err.span_label(*span, msg);
4265 }
4266 }
4267
4268 if let hir::ExprKind::MethodCall(_, expr, ..) = expr.kind {
4269 self.suggest_option_method_if_applicable(failed_pred, param_env, err, expr);
4270 }
4271 }
4272 }
4273
4274 fn suggest_option_method_if_applicable<G: EmissionGuarantee>(
4275 &self,
4276 failed_pred: ty::Predicate<'tcx>,
4277 param_env: ty::ParamEnv<'tcx>,
4278 err: &mut Diag<'_, G>,
4279 expr: &hir::Expr<'_>,
4280 ) {
4281 let tcx = self.tcx;
4282 let infcx = self.infcx;
4283 let Some(typeck_results) = self.typeck_results.as_ref() else { return };
4284
4285 let Some(option_ty_adt) = typeck_results.expr_ty_adjusted(expr).ty_adt_def() else {
4287 return;
4288 };
4289 if !tcx.is_diagnostic_item(sym::Option, option_ty_adt.did()) {
4290 return;
4291 }
4292
4293 if let ty::PredicateKind::Clause(ty::ClauseKind::Trait(ty::TraitPredicate { trait_ref, .. }))
4296 = failed_pred.kind().skip_binder()
4297 && tcx.is_fn_trait(trait_ref.def_id)
4298 && let [self_ty, found_ty] = trait_ref.args.as_slice()
4299 && let Some(fn_ty) = self_ty.as_type().filter(|ty| ty.is_fn())
4300 && let fn_sig @ ty::FnSig {
4301 abi: ExternAbi::Rust,
4302 c_variadic: false,
4303 safety: hir::Safety::Safe,
4304 ..
4305 } = fn_ty.fn_sig(tcx).skip_binder()
4306
4307 && let Some(&ty::Ref(_, target_ty, needs_mut)) = fn_sig.inputs().first().map(|t| t.kind())
4309 && !target_ty.has_escaping_bound_vars()
4310
4311 && let Some(ty::Tuple(tys)) = found_ty.as_type().map(Ty::kind)
4313 && let &[found_ty] = tys.as_slice()
4314 && !found_ty.has_escaping_bound_vars()
4315
4316 && let Some(deref_target_did) = tcx.lang_items().deref_target()
4318 && let projection = Ty::new_projection_from_args(tcx,deref_target_did, tcx.mk_args(&[ty::GenericArg::from(found_ty)]))
4319 && let InferOk { value: deref_target, obligations } = infcx.at(&ObligationCause::dummy(), param_env).normalize(projection)
4320 && obligations.iter().all(|obligation| infcx.predicate_must_hold_modulo_regions(obligation))
4321 && infcx.can_eq(param_env, deref_target, target_ty)
4322 {
4323 let help = if let hir::Mutability::Mut = needs_mut
4324 && let Some(deref_mut_did) = tcx.lang_items().deref_mut_trait()
4325 && infcx
4326 .type_implements_trait(deref_mut_did, iter::once(found_ty), param_env)
4327 .must_apply_modulo_regions()
4328 {
4329 Some(("call `Option::as_deref_mut()` first", ".as_deref_mut()"))
4330 } else if let hir::Mutability::Not = needs_mut {
4331 Some(("call `Option::as_deref()` first", ".as_deref()"))
4332 } else {
4333 None
4334 };
4335
4336 if let Some((msg, sugg)) = help {
4337 err.span_suggestion_with_style(
4338 expr.span.shrink_to_hi(),
4339 msg,
4340 sugg,
4341 Applicability::MaybeIncorrect,
4342 SuggestionStyle::ShowAlways,
4343 );
4344 }
4345 }
4346 }
4347
4348 fn look_for_iterator_item_mistakes<G: EmissionGuarantee>(
4349 &self,
4350 assocs_in_this_method: &[Option<(Span, (DefId, Ty<'tcx>))>],
4351 typeck_results: &TypeckResults<'tcx>,
4352 type_diffs: &[TypeError<'tcx>],
4353 param_env: ty::ParamEnv<'tcx>,
4354 path_segment: &hir::PathSegment<'_>,
4355 args: &[hir::Expr<'_>],
4356 err: &mut Diag<'_, G>,
4357 ) {
4358 let tcx = self.tcx;
4359 for entry in assocs_in_this_method {
4362 let Some((_span, (def_id, ty))) = entry else {
4363 continue;
4364 };
4365 for diff in type_diffs {
4366 let TypeError::Sorts(expected_found) = diff else {
4367 continue;
4368 };
4369 if tcx.is_diagnostic_item(sym::IteratorItem, *def_id)
4370 && path_segment.ident.name == sym::map
4371 && self.can_eq(param_env, expected_found.found, *ty)
4372 && let [arg] = args
4373 && let hir::ExprKind::Closure(closure) = arg.kind
4374 {
4375 let body = tcx.hir_body(closure.body);
4376 if let hir::ExprKind::Block(block, None) = body.value.kind
4377 && let None = block.expr
4378 && let [.., stmt] = block.stmts
4379 && let hir::StmtKind::Semi(expr) = stmt.kind
4380 && expected_found.found.is_unit()
4384 && expr.span.hi() != stmt.span.hi()
4389 {
4390 err.span_suggestion_verbose(
4391 expr.span.shrink_to_hi().with_hi(stmt.span.hi()),
4392 "consider removing this semicolon",
4393 String::new(),
4394 Applicability::MachineApplicable,
4395 );
4396 }
4397 let expr = if let hir::ExprKind::Block(block, None) = body.value.kind
4398 && let Some(expr) = block.expr
4399 {
4400 expr
4401 } else {
4402 body.value
4403 };
4404 if let hir::ExprKind::MethodCall(path_segment, rcvr, [], span) = expr.kind
4405 && path_segment.ident.name == sym::clone
4406 && let Some(expr_ty) = typeck_results.expr_ty_opt(expr)
4407 && let Some(rcvr_ty) = typeck_results.expr_ty_opt(rcvr)
4408 && self.can_eq(param_env, expr_ty, rcvr_ty)
4409 && let ty::Ref(_, ty, _) = expr_ty.kind()
4410 {
4411 err.span_label(
4412 span,
4413 format!(
4414 "this method call is cloning the reference `{expr_ty}`, not \
4415 `{ty}` which doesn't implement `Clone`",
4416 ),
4417 );
4418 let ty::Param(..) = ty.kind() else {
4419 continue;
4420 };
4421 let node =
4422 tcx.hir_node_by_def_id(tcx.hir_get_parent_item(expr.hir_id).def_id);
4423
4424 let pred = ty::Binder::dummy(ty::TraitPredicate {
4425 trait_ref: ty::TraitRef::new(
4426 tcx,
4427 tcx.require_lang_item(LangItem::Clone, span),
4428 [*ty],
4429 ),
4430 polarity: ty::PredicatePolarity::Positive,
4431 });
4432 let Some(generics) = node.generics() else {
4433 continue;
4434 };
4435 let Some(body_id) = node.body_id() else {
4436 continue;
4437 };
4438 suggest_restriction(
4439 tcx,
4440 tcx.hir_body_owner_def_id(body_id),
4441 generics,
4442 &format!("type parameter `{ty}`"),
4443 err,
4444 node.fn_sig(),
4445 None,
4446 pred,
4447 None,
4448 );
4449 }
4450 }
4451 }
4452 }
4453 }
4454
4455 fn point_at_chain<G: EmissionGuarantee>(
4456 &self,
4457 expr: &hir::Expr<'_>,
4458 typeck_results: &TypeckResults<'tcx>,
4459 type_diffs: Vec<TypeError<'tcx>>,
4460 param_env: ty::ParamEnv<'tcx>,
4461 err: &mut Diag<'_, G>,
4462 ) {
4463 let mut primary_spans = vec![];
4464 let mut span_labels = vec![];
4465
4466 let tcx = self.tcx;
4467
4468 let mut print_root_expr = true;
4469 let mut assocs = vec![];
4470 let mut expr = expr;
4471 let mut prev_ty = self.resolve_vars_if_possible(
4472 typeck_results.expr_ty_adjusted_opt(expr).unwrap_or(Ty::new_misc_error(tcx)),
4473 );
4474 while let hir::ExprKind::MethodCall(path_segment, rcvr_expr, args, span) = expr.kind {
4475 expr = rcvr_expr;
4479 let assocs_in_this_method =
4480 self.probe_assoc_types_at_expr(&type_diffs, span, prev_ty, expr.hir_id, param_env);
4481 self.look_for_iterator_item_mistakes(
4482 &assocs_in_this_method,
4483 typeck_results,
4484 &type_diffs,
4485 param_env,
4486 path_segment,
4487 args,
4488 err,
4489 );
4490 assocs.push(assocs_in_this_method);
4491 prev_ty = self.resolve_vars_if_possible(
4492 typeck_results.expr_ty_adjusted_opt(expr).unwrap_or(Ty::new_misc_error(tcx)),
4493 );
4494
4495 if let hir::ExprKind::Path(hir::QPath::Resolved(None, path)) = expr.kind
4496 && let hir::Path { res: Res::Local(hir_id), .. } = path
4497 && let hir::Node::Pat(binding) = self.tcx.hir_node(*hir_id)
4498 {
4499 let parent = self.tcx.parent_hir_node(binding.hir_id);
4500 if let hir::Node::LetStmt(local) = parent
4502 && let Some(binding_expr) = local.init
4503 {
4504 expr = binding_expr;
4506 }
4507 if let hir::Node::Param(param) = parent {
4508 let prev_ty = self.resolve_vars_if_possible(
4510 typeck_results
4511 .node_type_opt(param.hir_id)
4512 .unwrap_or(Ty::new_misc_error(tcx)),
4513 );
4514 let assocs_in_this_method = self.probe_assoc_types_at_expr(
4515 &type_diffs,
4516 param.ty_span,
4517 prev_ty,
4518 param.hir_id,
4519 param_env,
4520 );
4521 if assocs_in_this_method.iter().any(|a| a.is_some()) {
4522 assocs.push(assocs_in_this_method);
4523 print_root_expr = false;
4524 }
4525 break;
4526 }
4527 }
4528 }
4529 if let Some(ty) = typeck_results.expr_ty_opt(expr)
4532 && print_root_expr
4533 {
4534 let ty = with_forced_trimmed_paths!(self.ty_to_string(ty));
4535 span_labels.push((expr.span, format!("this expression has type `{ty}`")));
4539 };
4540 let mut assocs = assocs.into_iter().peekable();
4543 while let Some(assocs_in_method) = assocs.next() {
4544 let Some(prev_assoc_in_method) = assocs.peek() else {
4545 for entry in assocs_in_method {
4546 let Some((span, (assoc, ty))) = entry else {
4547 continue;
4548 };
4549 if primary_spans.is_empty()
4550 || type_diffs.iter().any(|diff| {
4551 let TypeError::Sorts(expected_found) = diff else {
4552 return false;
4553 };
4554 self.can_eq(param_env, expected_found.found, ty)
4555 })
4556 {
4557 primary_spans.push(span);
4563 }
4564 span_labels.push((
4565 span,
4566 with_forced_trimmed_paths!(format!(
4567 "`{}` is `{ty}` here",
4568 self.tcx.def_path_str(assoc),
4569 )),
4570 ));
4571 }
4572 break;
4573 };
4574 for (entry, prev_entry) in
4575 assocs_in_method.into_iter().zip(prev_assoc_in_method.into_iter())
4576 {
4577 match (entry, prev_entry) {
4578 (Some((span, (assoc, ty))), Some((_, (_, prev_ty)))) => {
4579 let ty_str = with_forced_trimmed_paths!(self.ty_to_string(ty));
4580
4581 let assoc = with_forced_trimmed_paths!(self.tcx.def_path_str(assoc));
4582 if !self.can_eq(param_env, ty, *prev_ty) {
4583 if type_diffs.iter().any(|diff| {
4584 let TypeError::Sorts(expected_found) = diff else {
4585 return false;
4586 };
4587 self.can_eq(param_env, expected_found.found, ty)
4588 }) {
4589 primary_spans.push(span);
4590 }
4591 span_labels
4592 .push((span, format!("`{assoc}` changed to `{ty_str}` here")));
4593 } else {
4594 span_labels.push((span, format!("`{assoc}` remains `{ty_str}` here")));
4595 }
4596 }
4597 (Some((span, (assoc, ty))), None) => {
4598 span_labels.push((
4599 span,
4600 with_forced_trimmed_paths!(format!(
4601 "`{}` is `{}` here",
4602 self.tcx.def_path_str(assoc),
4603 self.ty_to_string(ty),
4604 )),
4605 ));
4606 }
4607 (None, Some(_)) | (None, None) => {}
4608 }
4609 }
4610 }
4611 if !primary_spans.is_empty() {
4612 let mut multi_span: MultiSpan = primary_spans.into();
4613 for (span, label) in span_labels {
4614 multi_span.push_span_label(span, label);
4615 }
4616 err.span_note(
4617 multi_span,
4618 "the method call chain might not have had the expected associated types",
4619 );
4620 }
4621 }
4622
4623 fn probe_assoc_types_at_expr(
4624 &self,
4625 type_diffs: &[TypeError<'tcx>],
4626 span: Span,
4627 prev_ty: Ty<'tcx>,
4628 body_id: HirId,
4629 param_env: ty::ParamEnv<'tcx>,
4630 ) -> Vec<Option<(Span, (DefId, Ty<'tcx>))>> {
4631 let ocx = ObligationCtxt::new(self.infcx);
4632 let mut assocs_in_this_method = Vec::with_capacity(type_diffs.len());
4633 for diff in type_diffs {
4634 let TypeError::Sorts(expected_found) = diff else {
4635 continue;
4636 };
4637 let ty::Alias(ty::Projection, proj) = expected_found.expected.kind() else {
4638 continue;
4639 };
4640
4641 let args = GenericArgs::for_item(self.tcx, proj.def_id, |param, _| {
4645 if param.index == 0 {
4646 debug_assert_matches!(param.kind, ty::GenericParamDefKind::Type { .. });
4647 return prev_ty.into();
4648 }
4649 self.var_for_def(span, param)
4650 });
4651 let ty = self.infcx.next_ty_var(span);
4655 let projection = ty::Binder::dummy(ty::PredicateKind::Clause(
4657 ty::ClauseKind::Projection(ty::ProjectionPredicate {
4658 projection_term: ty::AliasTerm::new_from_args(self.tcx, proj.def_id, args),
4659 term: ty.into(),
4660 }),
4661 ));
4662 let body_def_id = self.tcx.hir_enclosing_body_owner(body_id);
4663 ocx.register_obligation(Obligation::misc(
4665 self.tcx,
4666 span,
4667 body_def_id,
4668 param_env,
4669 projection,
4670 ));
4671 if ocx.try_evaluate_obligations().is_empty()
4672 && let ty = self.resolve_vars_if_possible(ty)
4673 && !ty.is_ty_var()
4674 {
4675 assocs_in_this_method.push(Some((span, (proj.def_id, ty))));
4676 } else {
4677 assocs_in_this_method.push(None);
4682 }
4683 }
4684 assocs_in_this_method
4685 }
4686
4687 pub(super) fn suggest_convert_to_slice(
4691 &self,
4692 err: &mut Diag<'_>,
4693 obligation: &PredicateObligation<'tcx>,
4694 trait_pred: ty::PolyTraitPredicate<'tcx>,
4695 candidate_impls: &[ImplCandidate<'tcx>],
4696 span: Span,
4697 ) {
4698 let (ObligationCauseCode::BinOp { .. } | ObligationCauseCode::FunctionArg { .. }) =
4701 obligation.cause.code()
4702 else {
4703 return;
4704 };
4705
4706 let (element_ty, mut mutability) = match *trait_pred.skip_binder().self_ty().kind() {
4711 ty::Array(element_ty, _) => (element_ty, None),
4712
4713 ty::Ref(_, pointee_ty, mutability) => match *pointee_ty.kind() {
4714 ty::Array(element_ty, _) => (element_ty, Some(mutability)),
4715 _ => return,
4716 },
4717
4718 _ => return,
4719 };
4720
4721 let mut is_slice = |candidate: Ty<'tcx>| match *candidate.kind() {
4724 ty::RawPtr(t, m) | ty::Ref(_, t, m) => {
4725 if matches!(*t.kind(), ty::Slice(e) if e == element_ty)
4726 && m == mutability.unwrap_or(m)
4727 {
4728 mutability = Some(m);
4730 true
4731 } else {
4732 false
4733 }
4734 }
4735 _ => false,
4736 };
4737
4738 if let Some(slice_ty) = candidate_impls
4740 .iter()
4741 .map(|trait_ref| trait_ref.trait_ref.self_ty())
4742 .find(|t| is_slice(*t))
4743 {
4744 let msg = format!("convert the array to a `{slice_ty}` slice instead");
4745
4746 if let Ok(snippet) = self.tcx.sess.source_map().span_to_snippet(span) {
4747 let mut suggestions = vec![];
4748 if snippet.starts_with('&') {
4749 } else if let Some(hir::Mutability::Mut) = mutability {
4750 suggestions.push((span.shrink_to_lo(), "&mut ".into()));
4751 } else {
4752 suggestions.push((span.shrink_to_lo(), "&".into()));
4753 }
4754 suggestions.push((span.shrink_to_hi(), "[..]".into()));
4755 err.multipart_suggestion_verbose(msg, suggestions, Applicability::MaybeIncorrect);
4756 } else {
4757 err.span_help(span, msg);
4758 }
4759 }
4760 }
4761
4762 pub(super) fn suggest_tuple_wrapping(
4767 &self,
4768 err: &mut Diag<'_>,
4769 root_obligation: &PredicateObligation<'tcx>,
4770 obligation: &PredicateObligation<'tcx>,
4771 ) {
4772 let ObligationCauseCode::FunctionArg { arg_hir_id, .. } = obligation.cause.code() else {
4773 return;
4774 };
4775
4776 let Some(root_pred) = root_obligation.predicate.as_trait_clause() else { return };
4777
4778 let trait_ref = root_pred.map_bound(|root_pred| {
4779 root_pred.trait_ref.with_replaced_self_ty(
4780 self.tcx,
4781 Ty::new_tup(self.tcx, &[root_pred.trait_ref.self_ty()]),
4782 )
4783 });
4784
4785 let obligation =
4786 Obligation::new(self.tcx, obligation.cause.clone(), obligation.param_env, trait_ref);
4787
4788 if self.predicate_must_hold_modulo_regions(&obligation) {
4789 let arg_span = self.tcx.hir_span(*arg_hir_id);
4790 err.multipart_suggestion_verbose(
4791 format!("use a unary tuple instead"),
4792 vec![(arg_span.shrink_to_lo(), "(".into()), (arg_span.shrink_to_hi(), ",)".into())],
4793 Applicability::MaybeIncorrect,
4794 );
4795 }
4796 }
4797
4798 pub(super) fn explain_hrtb_projection(
4799 &self,
4800 diag: &mut Diag<'_>,
4801 pred: ty::PolyTraitPredicate<'tcx>,
4802 param_env: ty::ParamEnv<'tcx>,
4803 cause: &ObligationCause<'tcx>,
4804 ) {
4805 if pred.skip_binder().has_escaping_bound_vars() && pred.skip_binder().has_non_region_infer()
4806 {
4807 self.probe(|_| {
4808 let ocx = ObligationCtxt::new(self);
4809 self.enter_forall(pred, |pred| {
4810 let pred = ocx.normalize(&ObligationCause::dummy(), param_env, pred);
4811 ocx.register_obligation(Obligation::new(
4812 self.tcx,
4813 ObligationCause::dummy(),
4814 param_env,
4815 pred,
4816 ));
4817 });
4818 if !ocx.try_evaluate_obligations().is_empty() {
4819 return;
4821 }
4822
4823 if let ObligationCauseCode::FunctionArg {
4824 call_hir_id,
4825 arg_hir_id,
4826 parent_code: _,
4827 } = cause.code()
4828 {
4829 let arg_span = self.tcx.hir_span(*arg_hir_id);
4830 let mut sp: MultiSpan = arg_span.into();
4831
4832 sp.push_span_label(
4833 arg_span,
4834 "the trait solver is unable to infer the \
4835 generic types that should be inferred from this argument",
4836 );
4837 sp.push_span_label(
4838 self.tcx.hir_span(*call_hir_id),
4839 "add turbofish arguments to this call to \
4840 specify the types manually, even if it's redundant",
4841 );
4842 diag.span_note(
4843 sp,
4844 "this is a known limitation of the trait solver that \
4845 will be lifted in the future",
4846 );
4847 } else {
4848 let mut sp: MultiSpan = cause.span.into();
4849 sp.push_span_label(
4850 cause.span,
4851 "try adding turbofish arguments to this expression to \
4852 specify the types manually, even if it's redundant",
4853 );
4854 diag.span_note(
4855 sp,
4856 "this is a known limitation of the trait solver that \
4857 will be lifted in the future",
4858 );
4859 }
4860 });
4861 }
4862 }
4863
4864 pub(super) fn suggest_desugaring_async_fn_in_trait(
4865 &self,
4866 err: &mut Diag<'_>,
4867 trait_pred: ty::PolyTraitPredicate<'tcx>,
4868 ) {
4869 if self.tcx.features().return_type_notation() {
4871 return;
4872 }
4873
4874 let trait_def_id = trait_pred.def_id();
4875
4876 if !self.tcx.trait_is_auto(trait_def_id) {
4878 return;
4879 }
4880
4881 let ty::Alias(ty::Projection, alias_ty) = trait_pred.self_ty().skip_binder().kind() else {
4883 return;
4884 };
4885 let Some(ty::ImplTraitInTraitData::Trait { fn_def_id, opaque_def_id }) =
4886 self.tcx.opt_rpitit_info(alias_ty.def_id)
4887 else {
4888 return;
4889 };
4890
4891 let auto_trait = self.tcx.def_path_str(trait_def_id);
4892 let Some(fn_def_id) = fn_def_id.as_local() else {
4894 if self.tcx.asyncness(fn_def_id).is_async() {
4896 err.span_note(
4897 self.tcx.def_span(fn_def_id),
4898 format!(
4899 "`{}::{}` is an `async fn` in trait, which does not \
4900 automatically imply that its future is `{auto_trait}`",
4901 alias_ty.trait_ref(self.tcx),
4902 self.tcx.item_name(fn_def_id)
4903 ),
4904 );
4905 }
4906 return;
4907 };
4908 let hir::Node::TraitItem(item) = self.tcx.hir_node_by_def_id(fn_def_id) else {
4909 return;
4910 };
4911
4912 let (sig, body) = item.expect_fn();
4914 let hir::FnRetTy::Return(hir::Ty { kind: hir::TyKind::OpaqueDef(opaq_def, ..), .. }) =
4915 sig.decl.output
4916 else {
4917 return;
4919 };
4920
4921 if opaq_def.def_id.to_def_id() != opaque_def_id {
4924 return;
4925 }
4926
4927 let Some(sugg) = suggest_desugaring_async_fn_to_impl_future_in_trait(
4928 self.tcx,
4929 *sig,
4930 *body,
4931 opaque_def_id.expect_local(),
4932 &format!(" + {auto_trait}"),
4933 ) else {
4934 return;
4935 };
4936
4937 let function_name = self.tcx.def_path_str(fn_def_id);
4938 err.multipart_suggestion(
4939 format!(
4940 "`{auto_trait}` can be made part of the associated future's \
4941 guarantees for all implementations of `{function_name}`"
4942 ),
4943 sugg,
4944 Applicability::MachineApplicable,
4945 );
4946 }
4947
4948 pub fn ty_kind_suggestion(
4949 &self,
4950 param_env: ty::ParamEnv<'tcx>,
4951 ty: Ty<'tcx>,
4952 ) -> Option<String> {
4953 let tcx = self.infcx.tcx;
4954 let implements_default = |ty| {
4955 let Some(default_trait) = tcx.get_diagnostic_item(sym::Default) else {
4956 return false;
4957 };
4958 self.type_implements_trait(default_trait, [ty], param_env).must_apply_modulo_regions()
4959 };
4960
4961 Some(match *ty.kind() {
4962 ty::Never | ty::Error(_) => return None,
4963 ty::Bool => "false".to_string(),
4964 ty::Char => "\'x\'".to_string(),
4965 ty::Int(_) | ty::Uint(_) => "42".into(),
4966 ty::Float(_) => "3.14159".into(),
4967 ty::Slice(_) => "[]".to_string(),
4968 ty::Adt(def, _) if Some(def.did()) == tcx.get_diagnostic_item(sym::Vec) => {
4969 "vec![]".to_string()
4970 }
4971 ty::Adt(def, _) if Some(def.did()) == tcx.get_diagnostic_item(sym::String) => {
4972 "String::new()".to_string()
4973 }
4974 ty::Adt(def, args) if def.is_box() => {
4975 format!("Box::new({})", self.ty_kind_suggestion(param_env, args[0].expect_ty())?)
4976 }
4977 ty::Adt(def, _) if Some(def.did()) == tcx.get_diagnostic_item(sym::Option) => {
4978 "None".to_string()
4979 }
4980 ty::Adt(def, args) if Some(def.did()) == tcx.get_diagnostic_item(sym::Result) => {
4981 format!("Ok({})", self.ty_kind_suggestion(param_env, args[0].expect_ty())?)
4982 }
4983 ty::Adt(_, _) if implements_default(ty) => "Default::default()".to_string(),
4984 ty::Ref(_, ty, mutability) => {
4985 if let (ty::Str, hir::Mutability::Not) = (ty.kind(), mutability) {
4986 "\"\"".to_string()
4987 } else {
4988 let ty = self.ty_kind_suggestion(param_env, ty)?;
4989 format!("&{}{ty}", mutability.prefix_str())
4990 }
4991 }
4992 ty::Array(ty, len) if let Some(len) = len.try_to_target_usize(tcx) => {
4993 if len == 0 {
4994 "[]".to_string()
4995 } else if self.type_is_copy_modulo_regions(param_env, ty) || len == 1 {
4996 format!("[{}; {}]", self.ty_kind_suggestion(param_env, ty)?, len)
4998 } else {
4999 "/* value */".to_string()
5000 }
5001 }
5002 ty::Tuple(tys) => format!(
5003 "({}{})",
5004 tys.iter()
5005 .map(|ty| self.ty_kind_suggestion(param_env, ty))
5006 .collect::<Option<Vec<String>>>()?
5007 .join(", "),
5008 if tys.len() == 1 { "," } else { "" }
5009 ),
5010 _ => "/* value */".to_string(),
5011 })
5012 }
5013
5014 pub(super) fn suggest_add_result_as_return_type(
5018 &self,
5019 obligation: &PredicateObligation<'tcx>,
5020 err: &mut Diag<'_>,
5021 trait_pred: ty::PolyTraitPredicate<'tcx>,
5022 ) {
5023 if ObligationCauseCode::QuestionMark != *obligation.cause.code().peel_derives() {
5024 return;
5025 }
5026
5027 fn choose_suggest_items<'tcx, 'hir>(
5034 tcx: TyCtxt<'tcx>,
5035 node: hir::Node<'hir>,
5036 ) -> Option<(&'hir hir::FnDecl<'hir>, hir::BodyId)> {
5037 match node {
5038 hir::Node::Item(item)
5039 if let hir::ItemKind::Fn { sig, body: body_id, .. } = item.kind =>
5040 {
5041 Some((sig.decl, body_id))
5042 }
5043 hir::Node::ImplItem(item)
5044 if let hir::ImplItemKind::Fn(sig, body_id) = item.kind =>
5045 {
5046 let parent = tcx.parent_hir_node(item.hir_id());
5047 if let hir::Node::Item(item) = parent
5048 && let hir::ItemKind::Impl(imp) = item.kind
5049 && imp.of_trait.is_none()
5050 {
5051 return Some((sig.decl, body_id));
5052 }
5053 None
5054 }
5055 _ => None,
5056 }
5057 }
5058
5059 let node = self.tcx.hir_node_by_def_id(obligation.cause.body_id);
5060 if let Some((fn_decl, body_id)) = choose_suggest_items(self.tcx, node)
5061 && let hir::FnRetTy::DefaultReturn(ret_span) = fn_decl.output
5062 && self.tcx.is_diagnostic_item(sym::FromResidual, trait_pred.def_id())
5063 && trait_pred.skip_binder().trait_ref.args.type_at(0).is_unit()
5064 && let ty::Adt(def, _) = trait_pred.skip_binder().trait_ref.args.type_at(1).kind()
5065 && self.tcx.is_diagnostic_item(sym::Result, def.did())
5066 {
5067 let mut sugg_spans =
5068 vec![(ret_span, " -> Result<(), Box<dyn std::error::Error>>".to_string())];
5069 let body = self.tcx.hir_body(body_id);
5070 if let hir::ExprKind::Block(b, _) = body.value.kind
5071 && b.expr.is_none()
5072 {
5073 let span = self.tcx.sess.source_map().end_point(b.span);
5075 sugg_spans.push((
5076 span.shrink_to_lo(),
5077 format!(
5078 "{}{}",
5079 " Ok(())\n",
5080 self.tcx.sess.source_map().indentation_before(span).unwrap_or_default(),
5081 ),
5082 ));
5083 }
5084 err.multipart_suggestion_verbose(
5085 format!("consider adding return type"),
5086 sugg_spans,
5087 Applicability::MaybeIncorrect,
5088 );
5089 }
5090 }
5091
5092 #[instrument(level = "debug", skip_all)]
5093 pub(super) fn suggest_unsized_bound_if_applicable(
5094 &self,
5095 err: &mut Diag<'_>,
5096 obligation: &PredicateObligation<'tcx>,
5097 ) {
5098 let ty::PredicateKind::Clause(ty::ClauseKind::Trait(pred)) =
5099 obligation.predicate.kind().skip_binder()
5100 else {
5101 return;
5102 };
5103 let (ObligationCauseCode::WhereClause(item_def_id, span)
5104 | ObligationCauseCode::WhereClauseInExpr(item_def_id, span, ..)) =
5105 *obligation.cause.code().peel_derives()
5106 else {
5107 return;
5108 };
5109 if span.is_dummy() {
5110 return;
5111 }
5112 debug!(?pred, ?item_def_id, ?span);
5113
5114 let (Some(node), true) = (
5115 self.tcx.hir_get_if_local(item_def_id),
5116 self.tcx.is_lang_item(pred.def_id(), LangItem::Sized),
5117 ) else {
5118 return;
5119 };
5120
5121 let Some(generics) = node.generics() else {
5122 return;
5123 };
5124 let sized_trait = self.tcx.lang_items().sized_trait();
5125 debug!(?generics.params);
5126 debug!(?generics.predicates);
5127 let Some(param) = generics.params.iter().find(|param| param.span == span) else {
5128 return;
5129 };
5130 let explicitly_sized = generics
5133 .bounds_for_param(param.def_id)
5134 .flat_map(|bp| bp.bounds)
5135 .any(|bound| bound.trait_ref().and_then(|tr| tr.trait_def_id()) == sized_trait);
5136 if explicitly_sized {
5137 return;
5138 }
5139 debug!(?param);
5140 match node {
5141 hir::Node::Item(
5142 item @ hir::Item {
5143 kind:
5145 hir::ItemKind::Enum(..) | hir::ItemKind::Struct(..) | hir::ItemKind::Union(..),
5146 ..
5147 },
5148 ) => {
5149 if self.suggest_indirection_for_unsized(err, item, param) {
5150 return;
5151 }
5152 }
5153 _ => {}
5154 };
5155
5156 let (span, separator, open_paren_sp) =
5158 if let Some((s, open_paren_sp)) = generics.bounds_span_for_suggestions(param.def_id) {
5159 (s, " +", open_paren_sp)
5160 } else {
5161 (param.name.ident().span.shrink_to_hi(), ":", None)
5162 };
5163
5164 let mut suggs = vec![];
5165 let suggestion = format!("{separator} ?Sized");
5166
5167 if let Some(open_paren_sp) = open_paren_sp {
5168 suggs.push((open_paren_sp, "(".to_string()));
5169 suggs.push((span, format!("){suggestion}")));
5170 } else {
5171 suggs.push((span, suggestion));
5172 }
5173
5174 err.multipart_suggestion_verbose(
5175 "consider relaxing the implicit `Sized` restriction",
5176 suggs,
5177 Applicability::MachineApplicable,
5178 );
5179 }
5180
5181 fn suggest_indirection_for_unsized(
5182 &self,
5183 err: &mut Diag<'_>,
5184 item: &hir::Item<'tcx>,
5185 param: &hir::GenericParam<'tcx>,
5186 ) -> bool {
5187 let mut visitor = FindTypeParam { param: param.name.ident().name, .. };
5191 visitor.visit_item(item);
5192 if visitor.invalid_spans.is_empty() {
5193 return false;
5194 }
5195 let mut multispan: MultiSpan = param.span.into();
5196 multispan.push_span_label(
5197 param.span,
5198 format!("this could be changed to `{}: ?Sized`...", param.name.ident()),
5199 );
5200 for sp in visitor.invalid_spans {
5201 multispan.push_span_label(
5202 sp,
5203 format!("...if indirection were used here: `Box<{}>`", param.name.ident()),
5204 );
5205 }
5206 err.span_help(
5207 multispan,
5208 format!(
5209 "you could relax the implicit `Sized` bound on `{T}` if it were \
5210 used through indirection like `&{T}` or `Box<{T}>`",
5211 T = param.name.ident(),
5212 ),
5213 );
5214 true
5215 }
5216 pub(crate) fn suggest_swapping_lhs_and_rhs<T>(
5217 &self,
5218 err: &mut Diag<'_>,
5219 predicate: T,
5220 param_env: ty::ParamEnv<'tcx>,
5221 cause_code: &ObligationCauseCode<'tcx>,
5222 ) where
5223 T: Upcast<TyCtxt<'tcx>, ty::Predicate<'tcx>>,
5224 {
5225 let tcx = self.tcx;
5226 let predicate = predicate.upcast(tcx);
5227 match *cause_code {
5228 ObligationCauseCode::BinOp { lhs_hir_id, rhs_hir_id, rhs_span, .. }
5229 if let Some(typeck_results) = &self.typeck_results
5230 && let hir::Node::Expr(lhs) = tcx.hir_node(lhs_hir_id)
5231 && let hir::Node::Expr(rhs) = tcx.hir_node(rhs_hir_id)
5232 && let Some(lhs_ty) = typeck_results.expr_ty_opt(lhs)
5233 && let Some(rhs_ty) = typeck_results.expr_ty_opt(rhs) =>
5234 {
5235 if let Some(pred) = predicate.as_trait_clause()
5236 && tcx.is_lang_item(pred.def_id(), LangItem::PartialEq)
5237 && self
5238 .infcx
5239 .type_implements_trait(pred.def_id(), [rhs_ty, lhs_ty], param_env)
5240 .must_apply_modulo_regions()
5241 {
5242 let lhs_span = tcx.hir_span(lhs_hir_id);
5243 let sm = tcx.sess.source_map();
5244 if let Ok(rhs_snippet) = sm.span_to_snippet(rhs_span)
5245 && let Ok(lhs_snippet) = sm.span_to_snippet(lhs_span)
5246 {
5247 err.note(format!("`{rhs_ty}` implements `PartialEq<{lhs_ty}>`"));
5248 err.multipart_suggestion(
5249 "consider swapping the equality",
5250 vec![(lhs_span, rhs_snippet), (rhs_span, lhs_snippet)],
5251 Applicability::MaybeIncorrect,
5252 );
5253 }
5254 }
5255 }
5256 _ => {}
5257 }
5258 }
5259}
5260
5261fn hint_missing_borrow<'tcx>(
5263 infcx: &InferCtxt<'tcx>,
5264 param_env: ty::ParamEnv<'tcx>,
5265 span: Span,
5266 found: Ty<'tcx>,
5267 expected: Ty<'tcx>,
5268 found_node: Node<'_>,
5269 err: &mut Diag<'_>,
5270) {
5271 if matches!(found_node, Node::TraitItem(..)) {
5272 return;
5273 }
5274
5275 let found_args = match found.kind() {
5276 ty::FnPtr(sig_tys, _) => infcx.enter_forall(*sig_tys, |sig_tys| sig_tys.inputs().iter()),
5277 kind => {
5278 span_bug!(span, "found was converted to a FnPtr above but is now {:?}", kind)
5279 }
5280 };
5281 let expected_args = match expected.kind() {
5282 ty::FnPtr(sig_tys, _) => infcx.enter_forall(*sig_tys, |sig_tys| sig_tys.inputs().iter()),
5283 kind => {
5284 span_bug!(span, "expected was converted to a FnPtr above but is now {:?}", kind)
5285 }
5286 };
5287
5288 let Some(fn_decl) = found_node.fn_decl() else {
5290 return;
5291 };
5292
5293 let args = fn_decl.inputs.iter();
5294
5295 let mut to_borrow = Vec::new();
5296 let mut remove_borrow = Vec::new();
5297
5298 for ((found_arg, expected_arg), arg) in found_args.zip(expected_args).zip(args) {
5299 let (found_ty, found_refs) = get_deref_type_and_refs(*found_arg);
5300 let (expected_ty, expected_refs) = get_deref_type_and_refs(*expected_arg);
5301
5302 if infcx.can_eq(param_env, found_ty, expected_ty) {
5303 if found_refs.len() < expected_refs.len()
5305 && found_refs[..] == expected_refs[expected_refs.len() - found_refs.len()..]
5306 {
5307 to_borrow.push((
5308 arg.span.shrink_to_lo(),
5309 expected_refs[..expected_refs.len() - found_refs.len()]
5310 .iter()
5311 .map(|mutbl| format!("&{}", mutbl.prefix_str()))
5312 .collect::<Vec<_>>()
5313 .join(""),
5314 ));
5315 } else if found_refs.len() > expected_refs.len() {
5316 let mut span = arg.span.shrink_to_lo();
5317 let mut left = found_refs.len() - expected_refs.len();
5318 let mut ty = arg;
5319 while let hir::TyKind::Ref(_, mut_ty) = &ty.kind
5320 && left > 0
5321 {
5322 span = span.with_hi(mut_ty.ty.span.lo());
5323 ty = mut_ty.ty;
5324 left -= 1;
5325 }
5326 if left == 0 {
5327 remove_borrow.push((span, String::new()));
5328 }
5329 }
5330 }
5331 }
5332
5333 if !to_borrow.is_empty() {
5334 err.subdiagnostic(errors::AdjustSignatureBorrow::Borrow { to_borrow });
5335 }
5336
5337 if !remove_borrow.is_empty() {
5338 err.subdiagnostic(errors::AdjustSignatureBorrow::RemoveBorrow { remove_borrow });
5339 }
5340}
5341
5342#[derive(Debug)]
5345pub struct SelfVisitor<'v> {
5346 pub paths: Vec<&'v hir::Ty<'v>> = Vec::new(),
5347 pub name: Option<Symbol>,
5348}
5349
5350impl<'v> Visitor<'v> for SelfVisitor<'v> {
5351 fn visit_ty(&mut self, ty: &'v hir::Ty<'v, AmbigArg>) {
5352 if let hir::TyKind::Path(path) = ty.kind
5353 && let hir::QPath::TypeRelative(inner_ty, segment) = path
5354 && (Some(segment.ident.name) == self.name || self.name.is_none())
5355 && let hir::TyKind::Path(inner_path) = inner_ty.kind
5356 && let hir::QPath::Resolved(None, inner_path) = inner_path
5357 && let Res::SelfTyAlias { .. } = inner_path.res
5358 {
5359 self.paths.push(ty.as_unambig_ty());
5360 }
5361 hir::intravisit::walk_ty(self, ty);
5362 }
5363}
5364
5365#[derive(Default)]
5368pub struct ReturnsVisitor<'v> {
5369 pub returns: Vec<&'v hir::Expr<'v>>,
5370 in_block_tail: bool,
5371}
5372
5373impl<'v> Visitor<'v> for ReturnsVisitor<'v> {
5374 fn visit_expr(&mut self, ex: &'v hir::Expr<'v>) {
5375 match ex.kind {
5380 hir::ExprKind::Ret(Some(ex)) => {
5381 self.returns.push(ex);
5382 }
5383 hir::ExprKind::Block(block, _) if self.in_block_tail => {
5384 self.in_block_tail = false;
5385 for stmt in block.stmts {
5386 hir::intravisit::walk_stmt(self, stmt);
5387 }
5388 self.in_block_tail = true;
5389 if let Some(expr) = block.expr {
5390 self.visit_expr(expr);
5391 }
5392 }
5393 hir::ExprKind::If(_, then, else_opt) if self.in_block_tail => {
5394 self.visit_expr(then);
5395 if let Some(el) = else_opt {
5396 self.visit_expr(el);
5397 }
5398 }
5399 hir::ExprKind::Match(_, arms, _) if self.in_block_tail => {
5400 for arm in arms {
5401 self.visit_expr(arm.body);
5402 }
5403 }
5404 _ if !self.in_block_tail => hir::intravisit::walk_expr(self, ex),
5406 _ => self.returns.push(ex),
5407 }
5408 }
5409
5410 fn visit_body(&mut self, body: &hir::Body<'v>) {
5411 assert!(!self.in_block_tail);
5412 self.in_block_tail = true;
5413 hir::intravisit::walk_body(self, body);
5414 }
5415}
5416
5417#[derive(Default)]
5419struct AwaitsVisitor {
5420 awaits: Vec<HirId>,
5421}
5422
5423impl<'v> Visitor<'v> for AwaitsVisitor {
5424 fn visit_expr(&mut self, ex: &'v hir::Expr<'v>) {
5425 if let hir::ExprKind::Yield(_, hir::YieldSource::Await { expr: Some(id) }) = ex.kind {
5426 self.awaits.push(id)
5427 }
5428 hir::intravisit::walk_expr(self, ex)
5429 }
5430}
5431
5432pub trait NextTypeParamName {
5436 fn next_type_param_name(&self, name: Option<&str>) -> String;
5437}
5438
5439impl NextTypeParamName for &[hir::GenericParam<'_>] {
5440 fn next_type_param_name(&self, name: Option<&str>) -> String {
5441 let name = name.and_then(|n| n.chars().next()).map(|c| c.to_uppercase().to_string());
5443 let name = name.as_deref();
5444
5445 let possible_names = [name.unwrap_or("T"), "T", "U", "V", "X", "Y", "Z", "A", "B", "C"];
5447
5448 let used_names: Vec<Symbol> = self
5450 .iter()
5451 .filter_map(|param| match param.name {
5452 hir::ParamName::Plain(ident) => Some(ident.name),
5453 _ => None,
5454 })
5455 .collect();
5456
5457 possible_names
5459 .iter()
5460 .find(|n| !used_names.contains(&Symbol::intern(n)))
5461 .unwrap_or(&"ParamName")
5462 .to_string()
5463 }
5464}
5465
5466struct ReplaceImplTraitVisitor<'a> {
5468 ty_spans: &'a mut Vec<Span>,
5469 param_did: DefId,
5470}
5471
5472impl<'a, 'hir> hir::intravisit::Visitor<'hir> for ReplaceImplTraitVisitor<'a> {
5473 fn visit_ty(&mut self, t: &'hir hir::Ty<'hir, AmbigArg>) {
5474 if let hir::TyKind::Path(hir::QPath::Resolved(
5475 None,
5476 hir::Path { res: Res::Def(_, segment_did), .. },
5477 )) = t.kind
5478 {
5479 if self.param_did == *segment_did {
5480 self.ty_spans.push(t.span);
5485 return;
5486 }
5487 }
5488
5489 hir::intravisit::walk_ty(self, t);
5490 }
5491}
5492
5493pub(super) fn get_explanation_based_on_obligation<'tcx>(
5494 tcx: TyCtxt<'tcx>,
5495 obligation: &PredicateObligation<'tcx>,
5496 trait_predicate: ty::PolyTraitPredicate<'tcx>,
5497 pre_message: String,
5498 long_ty_path: &mut Option<PathBuf>,
5499) -> String {
5500 if let ObligationCauseCode::MainFunctionType = obligation.cause.code() {
5501 "consider using `()`, or a `Result`".to_owned()
5502 } else {
5503 let ty_desc = match trait_predicate.self_ty().skip_binder().kind() {
5504 ty::FnDef(_, _) => Some("fn item"),
5505 ty::Closure(_, _) => Some("closure"),
5506 _ => None,
5507 };
5508
5509 let desc = match ty_desc {
5510 Some(desc) => format!(" {desc}"),
5511 None => String::new(),
5512 };
5513 if let ty::PredicatePolarity::Positive = trait_predicate.polarity() {
5514 format!(
5515 "{pre_message}the trait `{}` is not implemented for{desc} `{}`",
5516 trait_predicate.print_modifiers_and_trait_path(),
5517 tcx.short_string(trait_predicate.self_ty().skip_binder(), long_ty_path),
5518 )
5519 } else {
5520 format!("{pre_message}the trait bound `{trait_predicate}` is not satisfied")
5524 }
5525 }
5526}
5527
5528struct ReplaceImplTraitFolder<'tcx> {
5530 tcx: TyCtxt<'tcx>,
5531 param: &'tcx ty::GenericParamDef,
5532 replace_ty: Ty<'tcx>,
5533}
5534
5535impl<'tcx> TypeFolder<TyCtxt<'tcx>> for ReplaceImplTraitFolder<'tcx> {
5536 fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> {
5537 if let ty::Param(ty::ParamTy { index, .. }) = t.kind() {
5538 if self.param.index == *index {
5539 return self.replace_ty;
5540 }
5541 }
5542 t.super_fold_with(self)
5543 }
5544
5545 fn cx(&self) -> TyCtxt<'tcx> {
5546 self.tcx
5547 }
5548}
5549
5550pub fn suggest_desugaring_async_fn_to_impl_future_in_trait<'tcx>(
5551 tcx: TyCtxt<'tcx>,
5552 sig: hir::FnSig<'tcx>,
5553 body: hir::TraitFn<'tcx>,
5554 opaque_def_id: LocalDefId,
5555 add_bounds: &str,
5556) -> Option<Vec<(Span, String)>> {
5557 let hir::IsAsync::Async(async_span) = sig.header.asyncness else {
5558 return None;
5559 };
5560 let async_span = tcx.sess.source_map().span_extend_while_whitespace(async_span);
5561
5562 let future = tcx.hir_node_by_def_id(opaque_def_id).expect_opaque_ty();
5563 let [hir::GenericBound::Trait(trait_ref)] = future.bounds else {
5564 return None;
5566 };
5567 let Some(hir::PathSegment { args: Some(args), .. }) = trait_ref.trait_ref.path.segments.last()
5568 else {
5569 return None;
5571 };
5572 let Some(future_output_ty) = args.constraints.first().and_then(|constraint| constraint.ty())
5573 else {
5574 return None;
5576 };
5577
5578 let mut sugg = if future_output_ty.span.is_empty() {
5579 vec![
5580 (async_span, String::new()),
5581 (
5582 future_output_ty.span,
5583 format!(" -> impl std::future::Future<Output = ()>{add_bounds}"),
5584 ),
5585 ]
5586 } else {
5587 vec![
5588 (future_output_ty.span.shrink_to_lo(), "impl std::future::Future<Output = ".to_owned()),
5589 (future_output_ty.span.shrink_to_hi(), format!(">{add_bounds}")),
5590 (async_span, String::new()),
5591 ]
5592 };
5593
5594 if let hir::TraitFn::Provided(body) = body {
5596 let body = tcx.hir_body(body);
5597 let body_span = body.value.span;
5598 let body_span_without_braces =
5599 body_span.with_lo(body_span.lo() + BytePos(1)).with_hi(body_span.hi() - BytePos(1));
5600 if body_span_without_braces.is_empty() {
5601 sugg.push((body_span_without_braces, " async {} ".to_owned()));
5602 } else {
5603 sugg.extend([
5604 (body_span_without_braces.shrink_to_lo(), "async {".to_owned()),
5605 (body_span_without_braces.shrink_to_hi(), "} ".to_owned()),
5606 ]);
5607 }
5608 }
5609
5610 Some(sugg)
5611}
5612
5613fn point_at_assoc_type_restriction<G: EmissionGuarantee>(
5616 tcx: TyCtxt<'_>,
5617 err: &mut Diag<'_, G>,
5618 self_ty_str: &str,
5619 trait_name: &str,
5620 predicate: ty::Predicate<'_>,
5621 generics: &hir::Generics<'_>,
5622 data: &ImplDerivedCause<'_>,
5623) {
5624 let ty::PredicateKind::Clause(clause) = predicate.kind().skip_binder() else {
5625 return;
5626 };
5627 let ty::ClauseKind::Projection(proj) = clause else {
5628 return;
5629 };
5630 let name = tcx.item_name(proj.projection_term.def_id);
5631 let mut predicates = generics.predicates.iter().peekable();
5632 let mut prev: Option<(&hir::WhereBoundPredicate<'_>, Span)> = None;
5633 while let Some(pred) = predicates.next() {
5634 let curr_span = pred.span;
5635 let hir::WherePredicateKind::BoundPredicate(pred) = pred.kind else {
5636 continue;
5637 };
5638 let mut bounds = pred.bounds.iter();
5639 while let Some(bound) = bounds.next() {
5640 let Some(trait_ref) = bound.trait_ref() else {
5641 continue;
5642 };
5643 if bound.span() != data.span {
5644 continue;
5645 }
5646 if let hir::TyKind::Path(path) = pred.bounded_ty.kind
5647 && let hir::QPath::TypeRelative(ty, segment) = path
5648 && segment.ident.name == name
5649 && let hir::TyKind::Path(inner_path) = ty.kind
5650 && let hir::QPath::Resolved(None, inner_path) = inner_path
5651 && let Res::SelfTyAlias { .. } = inner_path.res
5652 {
5653 let span = if pred.origin == hir::PredicateOrigin::WhereClause
5656 && generics
5657 .predicates
5658 .iter()
5659 .filter(|p| {
5660 matches!(
5661 p.kind,
5662 hir::WherePredicateKind::BoundPredicate(p)
5663 if hir::PredicateOrigin::WhereClause == p.origin
5664 )
5665 })
5666 .count()
5667 == 1
5668 {
5669 generics.where_clause_span
5672 } else if let Some(next_pred) = predicates.peek()
5673 && let hir::WherePredicateKind::BoundPredicate(next) = next_pred.kind
5674 && pred.origin == next.origin
5675 {
5676 curr_span.until(next_pred.span)
5678 } else if let Some((prev, prev_span)) = prev
5679 && pred.origin == prev.origin
5680 {
5681 prev_span.shrink_to_hi().to(curr_span)
5683 } else if pred.origin == hir::PredicateOrigin::WhereClause {
5684 curr_span.with_hi(generics.where_clause_span.hi())
5685 } else {
5686 curr_span
5687 };
5688
5689 err.span_suggestion_verbose(
5690 span,
5691 "associated type for the current `impl` cannot be restricted in `where` \
5692 clauses, remove this bound",
5693 "",
5694 Applicability::MaybeIncorrect,
5695 );
5696 }
5697 if let Some(new) =
5698 tcx.associated_items(data.impl_or_alias_def_id).find_by_ident_and_kind(
5699 tcx,
5700 Ident::with_dummy_span(name),
5701 ty::AssocTag::Type,
5702 data.impl_or_alias_def_id,
5703 )
5704 {
5705 let span = tcx.def_span(new.def_id);
5708 err.span_label(
5709 span,
5710 format!(
5711 "associated type `<{self_ty_str} as {trait_name}>::{name}` is specified \
5712 here",
5713 ),
5714 );
5715 let mut visitor = SelfVisitor { name: Some(name), .. };
5718 visitor.visit_trait_ref(trait_ref);
5719 for path in visitor.paths {
5720 err.span_suggestion_verbose(
5721 path.span,
5722 "replace the associated type with the type specified in this `impl`",
5723 tcx.type_of(new.def_id).skip_binder(),
5724 Applicability::MachineApplicable,
5725 );
5726 }
5727 } else {
5728 let mut visitor = SelfVisitor { name: None, .. };
5729 visitor.visit_trait_ref(trait_ref);
5730 let span: MultiSpan =
5731 visitor.paths.iter().map(|p| p.span).collect::<Vec<Span>>().into();
5732 err.span_note(
5733 span,
5734 "associated types for the current `impl` cannot be restricted in `where` \
5735 clauses",
5736 );
5737 }
5738 }
5739 prev = Some((pred, curr_span));
5740 }
5741}
5742
5743fn get_deref_type_and_refs(mut ty: Ty<'_>) -> (Ty<'_>, Vec<hir::Mutability>) {
5744 let mut refs = vec![];
5745
5746 while let ty::Ref(_, new_ty, mutbl) = ty.kind() {
5747 ty = *new_ty;
5748 refs.push(*mutbl);
5749 }
5750
5751 (ty, refs)
5752}
5753
5754struct FindTypeParam {
5757 param: rustc_span::Symbol,
5758 invalid_spans: Vec<Span> = Vec::new(),
5759 nested: bool = false,
5760}
5761
5762impl<'v> Visitor<'v> for FindTypeParam {
5763 fn visit_where_predicate(&mut self, _: &'v hir::WherePredicate<'v>) {
5764 }
5766
5767 fn visit_ty(&mut self, ty: &hir::Ty<'_, AmbigArg>) {
5768 match ty.kind {
5775 hir::TyKind::Ptr(_) | hir::TyKind::Ref(..) | hir::TyKind::TraitObject(..) => {}
5776 hir::TyKind::Path(hir::QPath::Resolved(None, path))
5777 if let [segment] = path.segments
5778 && segment.ident.name == self.param =>
5779 {
5780 if !self.nested {
5781 debug!(?ty, "FindTypeParam::visit_ty");
5782 self.invalid_spans.push(ty.span);
5783 }
5784 }
5785 hir::TyKind::Path(_) => {
5786 let prev = self.nested;
5787 self.nested = true;
5788 hir::intravisit::walk_ty(self, ty);
5789 self.nested = prev;
5790 }
5791 _ => {
5792 hir::intravisit::walk_ty(self, ty);
5793 }
5794 }
5795 }
5796}
5797
5798struct ParamFinder {
5801 params: Vec<Symbol> = Vec::new(),
5802}
5803
5804impl<'tcx> TypeVisitor<TyCtxt<'tcx>> for ParamFinder {
5805 fn visit_ty(&mut self, t: Ty<'tcx>) -> Self::Result {
5806 match t.kind() {
5807 ty::Param(p) => self.params.push(p.name),
5808 _ => {}
5809 }
5810 t.super_visit_with(self)
5811 }
5812}
5813
5814impl ParamFinder {
5815 fn can_suggest_bound(&self, generics: &hir::Generics<'_>) -> bool {
5818 if self.params.is_empty() {
5819 return true;
5822 }
5823 generics.params.iter().any(|p| match p.name {
5824 hir::ParamName::Plain(p_name) => {
5825 self.params.iter().any(|p| *p == p_name.name || *p == kw::SelfUpper)
5827 }
5828 _ => true,
5829 })
5830 }
5831}