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