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