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