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, .. },
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 let (span, sugg) = if let Some(snippet) =
824 self.tcx.sess.source_map().span_to_snippet(obligation.cause.span).ok()
825 && snippet.starts_with("|")
826 {
827 (obligation.cause.span, format!("({snippet})({args})"))
828 } else {
829 (obligation.cause.span.shrink_to_hi(), format!("({args})"))
830 };
831
832 err.span_suggestion_verbose(span, msg, sugg, Applicability::HasPlaceholders);
837 } else if let DefIdOrName::DefId(def_id) = def_id_or_name {
838 let name = match self.tcx.hir_get_if_local(def_id) {
839 Some(hir::Node::Expr(hir::Expr {
840 kind: hir::ExprKind::Closure(hir::Closure { fn_decl_span, .. }),
841 ..
842 })) => {
843 err.span_label(*fn_decl_span, "consider calling this closure");
844 let Some(name) = self.get_closure_name(def_id, err, msg.clone()) else {
845 return false;
846 };
847 name.to_string()
848 }
849 Some(hir::Node::Item(hir::Item {
850 kind: hir::ItemKind::Fn { ident, .. }, ..
851 })) => {
852 err.span_label(ident.span, "consider calling this function");
853 ident.to_string()
854 }
855 Some(hir::Node::Ctor(..)) => {
856 let name = self.tcx.def_path_str(def_id);
857 err.span_label(
858 self.tcx.def_span(def_id),
859 format!("consider calling the constructor for `{name}`"),
860 );
861 name
862 }
863 _ => return false,
864 };
865 err.help(format!("{msg}: `{name}({args})`"));
866 }
867 true
868 }
869
870 pub(super) fn check_for_binding_assigned_block_without_tail_expression(
871 &self,
872 obligation: &PredicateObligation<'tcx>,
873 err: &mut Diag<'_>,
874 trait_pred: ty::PolyTraitPredicate<'tcx>,
875 ) {
876 let mut span = obligation.cause.span;
877 while span.from_expansion() {
878 span.remove_mark();
880 }
881 let mut expr_finder = FindExprBySpan::new(span, self.tcx);
882 let Some(body) = self.tcx.hir_maybe_body_owned_by(obligation.cause.body_id) else {
883 return;
884 };
885 expr_finder.visit_expr(body.value);
886 let Some(expr) = expr_finder.result else {
887 return;
888 };
889 let Some(typeck) = &self.typeck_results else {
890 return;
891 };
892 let Some(ty) = typeck.expr_ty_adjusted_opt(expr) else {
893 return;
894 };
895 if !ty.is_unit() {
896 return;
897 };
898 let hir::ExprKind::Path(hir::QPath::Resolved(None, path)) = expr.kind else {
899 return;
900 };
901 let Res::Local(hir_id) = path.res else {
902 return;
903 };
904 let hir::Node::Pat(pat) = self.tcx.hir_node(hir_id) else {
905 return;
906 };
907 let hir::Node::LetStmt(hir::LetStmt { ty: None, init: Some(init), .. }) =
908 self.tcx.parent_hir_node(pat.hir_id)
909 else {
910 return;
911 };
912 let hir::ExprKind::Block(block, None) = init.kind else {
913 return;
914 };
915 if block.expr.is_some() {
916 return;
917 }
918 let [.., stmt] = block.stmts else {
919 err.span_label(block.span, "this empty block is missing a tail expression");
920 return;
921 };
922 let hir::StmtKind::Semi(tail_expr) = stmt.kind else {
923 return;
924 };
925 let Some(ty) = typeck.expr_ty_opt(tail_expr) else {
926 err.span_label(block.span, "this block is missing a tail expression");
927 return;
928 };
929 let ty = self.resolve_numeric_literals_with_default(self.resolve_vars_if_possible(ty));
930 let trait_pred_and_self = trait_pred.map_bound(|trait_pred| (trait_pred, ty));
931
932 let new_obligation =
933 self.mk_trait_obligation_with_new_self_ty(obligation.param_env, trait_pred_and_self);
934 if self.predicate_must_hold_modulo_regions(&new_obligation) {
935 err.span_suggestion_short(
936 stmt.span.with_lo(tail_expr.span.hi()),
937 "remove this semicolon",
938 "",
939 Applicability::MachineApplicable,
940 );
941 } else {
942 err.span_label(block.span, "this block is missing a tail expression");
943 }
944 }
945
946 pub(super) fn suggest_add_clone_to_arg(
947 &self,
948 obligation: &PredicateObligation<'tcx>,
949 err: &mut Diag<'_>,
950 trait_pred: ty::PolyTraitPredicate<'tcx>,
951 ) -> bool {
952 let self_ty = self.resolve_vars_if_possible(trait_pred.self_ty());
953 self.enter_forall(self_ty, |ty: Ty<'_>| {
954 let Some(generics) = self.tcx.hir_get_generics(obligation.cause.body_id) else {
955 return false;
956 };
957 let ty::Ref(_, inner_ty, hir::Mutability::Not) = ty.kind() else { return false };
958 let ty::Param(param) = inner_ty.kind() else { return false };
959 let ObligationCauseCode::FunctionArg { arg_hir_id, .. } = obligation.cause.code()
960 else {
961 return false;
962 };
963
964 let clone_trait = self.tcx.require_lang_item(LangItem::Clone, obligation.cause.span);
965 let has_clone = |ty| {
966 self.type_implements_trait(clone_trait, [ty], obligation.param_env)
967 .must_apply_modulo_regions()
968 };
969
970 let existing_clone_call = match self.tcx.hir_node(*arg_hir_id) {
971 Node::Expr(Expr { kind: hir::ExprKind::Path(_), .. }) => None,
973 Node::Expr(Expr {
976 kind:
977 hir::ExprKind::MethodCall(
978 hir::PathSegment { ident, .. },
979 _receiver,
980 [],
981 call_span,
982 ),
983 hir_id,
984 ..
985 }) if ident.name == sym::clone
986 && !call_span.from_expansion()
987 && !has_clone(*inner_ty) =>
988 {
989 let Some(typeck_results) = self.typeck_results.as_ref() else { return false };
991 let Some((DefKind::AssocFn, did)) = typeck_results.type_dependent_def(*hir_id)
992 else {
993 return false;
994 };
995 if self.tcx.trait_of_assoc(did) != Some(clone_trait) {
996 return false;
997 }
998 Some(ident.span)
999 }
1000 _ => return false,
1001 };
1002
1003 let new_obligation = self.mk_trait_obligation_with_new_self_ty(
1004 obligation.param_env,
1005 trait_pred.map_bound(|trait_pred| (trait_pred, *inner_ty)),
1006 );
1007
1008 if self.predicate_may_hold(&new_obligation) && has_clone(ty) {
1009 if !has_clone(param.to_ty(self.tcx)) {
1010 suggest_constraining_type_param(
1011 self.tcx,
1012 generics,
1013 err,
1014 param.name.as_str(),
1015 "Clone",
1016 Some(clone_trait),
1017 None,
1018 );
1019 }
1020 if let Some(existing_clone_call) = existing_clone_call {
1021 err.span_note(
1022 existing_clone_call,
1023 format!(
1024 "this `clone()` copies the reference, \
1025 which does not do anything, \
1026 because `{inner_ty}` does not implement `Clone`"
1027 ),
1028 );
1029 } else {
1030 err.span_suggestion_verbose(
1031 obligation.cause.span.shrink_to_hi(),
1032 "consider using clone here",
1033 ".clone()".to_string(),
1034 Applicability::MaybeIncorrect,
1035 );
1036 }
1037 return true;
1038 }
1039 false
1040 })
1041 }
1042
1043 pub fn extract_callable_info(
1047 &self,
1048 body_id: LocalDefId,
1049 param_env: ty::ParamEnv<'tcx>,
1050 found: Ty<'tcx>,
1051 ) -> Option<(DefIdOrName, Ty<'tcx>, Vec<Ty<'tcx>>)> {
1052 let Some((def_id_or_name, output, inputs)) =
1054 (self.autoderef_steps)(found).into_iter().find_map(|(found, _)| match *found.kind() {
1055 ty::FnPtr(sig_tys, _) => Some((
1056 DefIdOrName::Name("function pointer"),
1057 sig_tys.output(),
1058 sig_tys.inputs(),
1059 )),
1060 ty::FnDef(def_id, _) => {
1061 let fn_sig = found.fn_sig(self.tcx);
1062 Some((DefIdOrName::DefId(def_id), fn_sig.output(), fn_sig.inputs()))
1063 }
1064 ty::Closure(def_id, args) => {
1065 let fn_sig = args.as_closure().sig();
1066 Some((
1067 DefIdOrName::DefId(def_id),
1068 fn_sig.output(),
1069 fn_sig.inputs().map_bound(|inputs| inputs[0].tuple_fields().as_slice()),
1070 ))
1071 }
1072 ty::CoroutineClosure(def_id, args) => {
1073 let sig_parts = args.as_coroutine_closure().coroutine_closure_sig();
1074 Some((
1075 DefIdOrName::DefId(def_id),
1076 sig_parts.map_bound(|sig| {
1077 sig.to_coroutine(
1078 self.tcx,
1079 args.as_coroutine_closure().parent_args(),
1080 self.next_ty_var(DUMMY_SP),
1083 self.tcx.coroutine_for_closure(def_id),
1084 self.next_ty_var(DUMMY_SP),
1085 )
1086 }),
1087 sig_parts.map_bound(|sig| sig.tupled_inputs_ty.tuple_fields().as_slice()),
1088 ))
1089 }
1090 ty::Alias(ty::Opaque, ty::AliasTy { def_id, args, .. }) => {
1091 self.tcx.item_self_bounds(def_id).instantiate(self.tcx, args).iter().find_map(
1092 |pred| {
1093 if let ty::ClauseKind::Projection(proj) = pred.kind().skip_binder()
1094 && self
1095 .tcx
1096 .is_lang_item(proj.projection_term.def_id, LangItem::FnOnceOutput)
1097 && let ty::Tuple(args) = proj.projection_term.args.type_at(1).kind()
1099 {
1100 Some((
1101 DefIdOrName::DefId(def_id),
1102 pred.kind().rebind(proj.term.expect_type()),
1103 pred.kind().rebind(args.as_slice()),
1104 ))
1105 } else {
1106 None
1107 }
1108 },
1109 )
1110 }
1111 ty::Dynamic(data, _, ty::Dyn) => data.iter().find_map(|pred| {
1112 if let ty::ExistentialPredicate::Projection(proj) = pred.skip_binder()
1113 && self.tcx.is_lang_item(proj.def_id, LangItem::FnOnceOutput)
1114 && let ty::Tuple(args) = proj.args.type_at(0).kind()
1116 {
1117 Some((
1118 DefIdOrName::Name("trait object"),
1119 pred.rebind(proj.term.expect_type()),
1120 pred.rebind(args.as_slice()),
1121 ))
1122 } else {
1123 None
1124 }
1125 }),
1126 ty::Param(param) => {
1127 let generics = self.tcx.generics_of(body_id);
1128 let name = if generics.count() > param.index as usize
1129 && let def = generics.param_at(param.index as usize, self.tcx)
1130 && matches!(def.kind, ty::GenericParamDefKind::Type { .. })
1131 && def.name == param.name
1132 {
1133 DefIdOrName::DefId(def.def_id)
1134 } else {
1135 DefIdOrName::Name("type parameter")
1136 };
1137 param_env.caller_bounds().iter().find_map(|pred| {
1138 if let ty::ClauseKind::Projection(proj) = pred.kind().skip_binder()
1139 && self
1140 .tcx
1141 .is_lang_item(proj.projection_term.def_id, LangItem::FnOnceOutput)
1142 && proj.projection_term.self_ty() == found
1143 && let ty::Tuple(args) = proj.projection_term.args.type_at(1).kind()
1145 {
1146 Some((
1147 name,
1148 pred.kind().rebind(proj.term.expect_type()),
1149 pred.kind().rebind(args.as_slice()),
1150 ))
1151 } else {
1152 None
1153 }
1154 })
1155 }
1156 _ => None,
1157 })
1158 else {
1159 return None;
1160 };
1161
1162 let output = self.instantiate_binder_with_fresh_vars(
1163 DUMMY_SP,
1164 BoundRegionConversionTime::FnCall,
1165 output,
1166 );
1167 let inputs = inputs
1168 .skip_binder()
1169 .iter()
1170 .map(|ty| {
1171 self.instantiate_binder_with_fresh_vars(
1172 DUMMY_SP,
1173 BoundRegionConversionTime::FnCall,
1174 inputs.rebind(*ty),
1175 )
1176 })
1177 .collect();
1178
1179 let InferOk { value: output, obligations: _ } =
1183 self.at(&ObligationCause::dummy(), param_env).normalize(output);
1184
1185 if output.is_ty_var() { None } else { Some((def_id_or_name, output, inputs)) }
1186 }
1187
1188 pub(super) fn suggest_add_reference_to_arg(
1189 &self,
1190 obligation: &PredicateObligation<'tcx>,
1191 err: &mut Diag<'_>,
1192 poly_trait_pred: ty::PolyTraitPredicate<'tcx>,
1193 has_custom_message: bool,
1194 ) -> bool {
1195 let span = obligation.cause.span;
1196 let param_env = obligation.param_env;
1197
1198 let mk_result = |trait_pred_and_new_ty| {
1199 let obligation =
1200 self.mk_trait_obligation_with_new_self_ty(param_env, trait_pred_and_new_ty);
1201 self.predicate_must_hold_modulo_regions(&obligation)
1202 };
1203
1204 let code = match obligation.cause.code() {
1205 ObligationCauseCode::FunctionArg { parent_code, .. } => parent_code,
1206 c @ ObligationCauseCode::WhereClauseInExpr(_, _, hir_id, _)
1209 if self.tcx.hir_span(*hir_id).lo() == span.lo() =>
1210 {
1211 if let hir::Node::Expr(expr) = self.tcx.parent_hir_node(*hir_id)
1215 && let hir::ExprKind::Call(base, _) = expr.kind
1216 && let hir::ExprKind::Path(hir::QPath::TypeRelative(ty, segment)) = base.kind
1217 && let hir::Node::Expr(outer) = self.tcx.parent_hir_node(expr.hir_id)
1218 && let hir::ExprKind::AddrOf(hir::BorrowKind::Ref, mtbl, _) = outer.kind
1219 && ty.span == span
1220 {
1221 let trait_pred_and_imm_ref = poly_trait_pred.map_bound(|p| {
1227 (p, Ty::new_imm_ref(self.tcx, self.tcx.lifetimes.re_static, p.self_ty()))
1228 });
1229 let trait_pred_and_mut_ref = poly_trait_pred.map_bound(|p| {
1230 (p, Ty::new_mut_ref(self.tcx, self.tcx.lifetimes.re_static, p.self_ty()))
1231 });
1232
1233 let imm_ref_self_ty_satisfies_pred = mk_result(trait_pred_and_imm_ref);
1234 let mut_ref_self_ty_satisfies_pred = mk_result(trait_pred_and_mut_ref);
1235 let sugg_msg = |pre: &str| {
1236 format!(
1237 "you likely meant to call the associated function `{FN}` for type \
1238 `&{pre}{TY}`, but the code as written calls associated function `{FN}` on \
1239 type `{TY}`",
1240 FN = segment.ident,
1241 TY = poly_trait_pred.self_ty(),
1242 )
1243 };
1244 match (imm_ref_self_ty_satisfies_pred, mut_ref_self_ty_satisfies_pred, mtbl) {
1245 (true, _, hir::Mutability::Not) | (_, true, hir::Mutability::Mut) => {
1246 err.multipart_suggestion_verbose(
1247 sugg_msg(mtbl.prefix_str()),
1248 vec![
1249 (outer.span.shrink_to_lo(), "<".to_string()),
1250 (span.shrink_to_hi(), ">".to_string()),
1251 ],
1252 Applicability::MachineApplicable,
1253 );
1254 }
1255 (true, _, hir::Mutability::Mut) => {
1256 err.multipart_suggestion_verbose(
1258 sugg_msg("mut "),
1259 vec![
1260 (outer.span.shrink_to_lo().until(span), "<&".to_string()),
1261 (span.shrink_to_hi(), ">".to_string()),
1262 ],
1263 Applicability::MachineApplicable,
1264 );
1265 }
1266 (_, true, hir::Mutability::Not) => {
1267 err.multipart_suggestion_verbose(
1268 sugg_msg(""),
1269 vec![
1270 (outer.span.shrink_to_lo().until(span), "<&mut ".to_string()),
1271 (span.shrink_to_hi(), ">".to_string()),
1272 ],
1273 Applicability::MachineApplicable,
1274 );
1275 }
1276 _ => {}
1277 }
1278 return false;
1280 }
1281 c
1282 }
1283 c if matches!(
1284 span.ctxt().outer_expn_data().kind,
1285 ExpnKind::Desugaring(DesugaringKind::ForLoop)
1286 ) =>
1287 {
1288 c
1289 }
1290 _ => return false,
1291 };
1292
1293 let mut never_suggest_borrow: Vec<_> =
1297 [LangItem::Copy, LangItem::Clone, LangItem::Unpin, LangItem::Sized]
1298 .iter()
1299 .filter_map(|lang_item| self.tcx.lang_items().get(*lang_item))
1300 .collect();
1301
1302 if let Some(def_id) = self.tcx.get_diagnostic_item(sym::Send) {
1303 never_suggest_borrow.push(def_id);
1304 }
1305
1306 let mut try_borrowing = |old_pred: ty::PolyTraitPredicate<'tcx>,
1308 blacklist: &[DefId]|
1309 -> bool {
1310 if blacklist.contains(&old_pred.def_id()) {
1311 return false;
1312 }
1313 let trait_pred_and_imm_ref = old_pred.map_bound(|trait_pred| {
1315 (
1316 trait_pred,
1317 Ty::new_imm_ref(self.tcx, self.tcx.lifetimes.re_static, trait_pred.self_ty()),
1318 )
1319 });
1320 let trait_pred_and_mut_ref = old_pred.map_bound(|trait_pred| {
1321 (
1322 trait_pred,
1323 Ty::new_mut_ref(self.tcx, self.tcx.lifetimes.re_static, trait_pred.self_ty()),
1324 )
1325 });
1326
1327 let imm_ref_self_ty_satisfies_pred = mk_result(trait_pred_and_imm_ref);
1328 let mut_ref_self_ty_satisfies_pred = mk_result(trait_pred_and_mut_ref);
1329
1330 let (ref_inner_ty_satisfies_pred, ref_inner_ty_is_mut) =
1331 if let ObligationCauseCode::WhereClauseInExpr(..) = obligation.cause.code()
1332 && let ty::Ref(_, ty, mutability) = old_pred.self_ty().skip_binder().kind()
1333 {
1334 (
1335 mk_result(old_pred.map_bound(|trait_pred| (trait_pred, *ty))),
1336 mutability.is_mut(),
1337 )
1338 } else {
1339 (false, false)
1340 };
1341
1342 let is_immut = imm_ref_self_ty_satisfies_pred
1343 || (ref_inner_ty_satisfies_pred && !ref_inner_ty_is_mut);
1344 let is_mut = mut_ref_self_ty_satisfies_pred || ref_inner_ty_is_mut;
1345 if !is_immut && !is_mut {
1346 return false;
1347 }
1348 let Ok(_snippet) = self.tcx.sess.source_map().span_to_snippet(span) else {
1349 return false;
1350 };
1351 if !matches!(
1359 span.ctxt().outer_expn_data().kind,
1360 ExpnKind::Root | ExpnKind::Desugaring(DesugaringKind::ForLoop)
1361 ) {
1362 return false;
1363 }
1364 let mut label = || {
1371 let msg = format!(
1372 "the trait bound `{}` is not satisfied",
1373 self.tcx.short_string(old_pred, err.long_ty_path()),
1374 );
1375 let self_ty_str =
1376 self.tcx.short_string(old_pred.self_ty().skip_binder(), err.long_ty_path());
1377 let trait_path = self
1378 .tcx
1379 .short_string(old_pred.print_modifiers_and_trait_path(), err.long_ty_path());
1380
1381 if has_custom_message {
1382 err.note(msg);
1383 } else {
1384 err.messages = vec![(rustc_errors::DiagMessage::from(msg), Style::NoStyle)];
1385 }
1386 err.span_label(
1387 span,
1388 format!("the trait `{trait_path}` is not implemented for `{self_ty_str}`"),
1389 );
1390 };
1391
1392 let mut sugg_prefixes = vec![];
1393 if is_immut {
1394 sugg_prefixes.push("&");
1395 }
1396 if is_mut {
1397 sugg_prefixes.push("&mut ");
1398 }
1399 let sugg_msg = format!(
1400 "consider{} borrowing here",
1401 if is_mut && !is_immut { " mutably" } else { "" },
1402 );
1403
1404 let Some(body) = self.tcx.hir_maybe_body_owned_by(obligation.cause.body_id) else {
1408 return false;
1409 };
1410 let mut expr_finder = FindExprBySpan::new(span, self.tcx);
1411 expr_finder.visit_expr(body.value);
1412
1413 if let Some(ty) = expr_finder.ty_result {
1414 if let hir::Node::Expr(expr) = self.tcx.parent_hir_node(ty.hir_id)
1415 && let hir::ExprKind::Path(hir::QPath::TypeRelative(_, _)) = expr.kind
1416 && ty.span == span
1417 {
1418 label();
1421 err.multipart_suggestions(
1422 sugg_msg,
1423 sugg_prefixes.into_iter().map(|sugg_prefix| {
1424 vec![
1425 (span.shrink_to_lo(), format!("<{sugg_prefix}")),
1426 (span.shrink_to_hi(), ">".to_string()),
1427 ]
1428 }),
1429 Applicability::MaybeIncorrect,
1430 );
1431 return true;
1432 }
1433 return false;
1434 }
1435 let Some(expr) = expr_finder.result else {
1436 return false;
1437 };
1438 if let hir::ExprKind::AddrOf(_, _, _) = expr.kind {
1439 return false;
1440 }
1441 let needs_parens_post = expr_needs_parens(expr);
1442 let needs_parens_pre = match self.tcx.parent_hir_node(expr.hir_id) {
1443 Node::Expr(e)
1444 if let hir::ExprKind::MethodCall(_, base, _, _) = e.kind
1445 && base.hir_id == expr.hir_id =>
1446 {
1447 true
1448 }
1449 _ => false,
1450 };
1451
1452 label();
1453 let suggestions = sugg_prefixes.into_iter().map(|sugg_prefix| {
1454 match (needs_parens_pre, needs_parens_post) {
1455 (false, false) => vec![(span.shrink_to_lo(), sugg_prefix.to_string())],
1456 (false, true) => vec![
1459 (span.shrink_to_lo(), format!("{sugg_prefix}(")),
1460 (span.shrink_to_hi(), ")".to_string()),
1461 ],
1462 (true, false) => vec![
1465 (span.shrink_to_lo(), format!("({sugg_prefix}")),
1466 (span.shrink_to_hi(), ")".to_string()),
1467 ],
1468 (true, true) => vec![
1469 (span.shrink_to_lo(), format!("({sugg_prefix}(")),
1470 (span.shrink_to_hi(), "))".to_string()),
1471 ],
1472 }
1473 });
1474 err.multipart_suggestions(sugg_msg, suggestions, Applicability::MaybeIncorrect);
1475 return true;
1476 };
1477
1478 if let ObligationCauseCode::ImplDerived(cause) = &*code {
1479 try_borrowing(cause.derived.parent_trait_pred, &[])
1480 } else if let ObligationCauseCode::WhereClause(..)
1481 | ObligationCauseCode::WhereClauseInExpr(..) = code
1482 {
1483 try_borrowing(poly_trait_pred, &never_suggest_borrow)
1484 } else {
1485 false
1486 }
1487 }
1488
1489 pub(super) fn suggest_borrowing_for_object_cast(
1491 &self,
1492 err: &mut Diag<'_>,
1493 obligation: &PredicateObligation<'tcx>,
1494 self_ty: Ty<'tcx>,
1495 target_ty: Ty<'tcx>,
1496 ) {
1497 let ty::Ref(_, object_ty, hir::Mutability::Not) = target_ty.kind() else {
1498 return;
1499 };
1500 let ty::Dynamic(predicates, _, ty::Dyn) = object_ty.kind() else {
1501 return;
1502 };
1503 let self_ref_ty = Ty::new_imm_ref(self.tcx, self.tcx.lifetimes.re_erased, self_ty);
1504
1505 for predicate in predicates.iter() {
1506 if !self.predicate_must_hold_modulo_regions(
1507 &obligation.with(self.tcx, predicate.with_self_ty(self.tcx, self_ref_ty)),
1508 ) {
1509 return;
1510 }
1511 }
1512
1513 err.span_suggestion_verbose(
1514 obligation.cause.span.shrink_to_lo(),
1515 format!(
1516 "consider borrowing the value, since `&{self_ty}` can be coerced into `{target_ty}`"
1517 ),
1518 "&",
1519 Applicability::MaybeIncorrect,
1520 );
1521 }
1522
1523 pub(super) fn suggest_remove_reference(
1526 &self,
1527 obligation: &PredicateObligation<'tcx>,
1528 err: &mut Diag<'_>,
1529 trait_pred: ty::PolyTraitPredicate<'tcx>,
1530 ) -> bool {
1531 let mut span = obligation.cause.span;
1532 let mut trait_pred = trait_pred;
1533 let mut code = obligation.cause.code();
1534 while let Some((c, Some(parent_trait_pred))) = code.parent_with_predicate() {
1535 code = c;
1538 trait_pred = parent_trait_pred;
1539 }
1540 while span.desugaring_kind().is_some() {
1541 span.remove_mark();
1543 }
1544 let mut expr_finder = super::FindExprBySpan::new(span, self.tcx);
1545 let Some(body) = self.tcx.hir_maybe_body_owned_by(obligation.cause.body_id) else {
1546 return false;
1547 };
1548 expr_finder.visit_expr(body.value);
1549 let mut maybe_suggest = |suggested_ty, count, suggestions| {
1550 let trait_pred_and_suggested_ty =
1552 trait_pred.map_bound(|trait_pred| (trait_pred, suggested_ty));
1553
1554 let new_obligation = self.mk_trait_obligation_with_new_self_ty(
1555 obligation.param_env,
1556 trait_pred_and_suggested_ty,
1557 );
1558
1559 if self.predicate_may_hold(&new_obligation) {
1560 let msg = if count == 1 {
1561 "consider removing the leading `&`-reference".to_string()
1562 } else {
1563 format!("consider removing {count} leading `&`-references")
1564 };
1565
1566 err.multipart_suggestion_verbose(
1567 msg,
1568 suggestions,
1569 Applicability::MachineApplicable,
1570 );
1571 true
1572 } else {
1573 false
1574 }
1575 };
1576
1577 let mut count = 0;
1580 let mut suggestions = vec![];
1581 let mut suggested_ty = trait_pred.self_ty().skip_binder();
1583 if let Some(mut hir_ty) = expr_finder.ty_result {
1584 while let hir::TyKind::Ref(_, mut_ty) = &hir_ty.kind {
1585 count += 1;
1586 let span = hir_ty.span.until(mut_ty.ty.span);
1587 suggestions.push((span, String::new()));
1588
1589 let ty::Ref(_, inner_ty, _) = suggested_ty.kind() else {
1590 break;
1591 };
1592 suggested_ty = *inner_ty;
1593
1594 hir_ty = mut_ty.ty;
1595
1596 if maybe_suggest(suggested_ty, count, suggestions.clone()) {
1597 return true;
1598 }
1599 }
1600 }
1601
1602 let Some(mut expr) = expr_finder.result else {
1604 return false;
1605 };
1606 let mut count = 0;
1607 let mut suggestions = vec![];
1608 let mut suggested_ty = trait_pred.self_ty().skip_binder();
1610 'outer: loop {
1611 while let hir::ExprKind::AddrOf(_, _, borrowed) = expr.kind {
1612 count += 1;
1613 let span =
1614 if let Some(borrowed_span) = borrowed.span.find_ancestor_inside(expr.span) {
1615 expr.span.until(borrowed_span)
1616 } else {
1617 break 'outer;
1618 };
1619
1620 match self.tcx.sess.source_map().span_to_snippet(span) {
1623 Ok(snippet) if snippet.starts_with("&") => {}
1624 _ => break 'outer,
1625 }
1626
1627 suggestions.push((span, String::new()));
1628
1629 let ty::Ref(_, inner_ty, _) = suggested_ty.kind() else {
1630 break 'outer;
1631 };
1632 suggested_ty = *inner_ty;
1633
1634 expr = borrowed;
1635
1636 if maybe_suggest(suggested_ty, count, suggestions.clone()) {
1637 return true;
1638 }
1639 }
1640 if let hir::ExprKind::Path(hir::QPath::Resolved(None, path)) = expr.kind
1641 && let Res::Local(hir_id) = path.res
1642 && let hir::Node::Pat(binding) = self.tcx.hir_node(hir_id)
1643 && let hir::Node::LetStmt(local) = self.tcx.parent_hir_node(binding.hir_id)
1644 && let None = local.ty
1645 && let Some(binding_expr) = local.init
1646 {
1647 expr = binding_expr;
1648 } else {
1649 break 'outer;
1650 }
1651 }
1652 false
1653 }
1654
1655 pub(super) fn suggest_remove_await(
1656 &self,
1657 obligation: &PredicateObligation<'tcx>,
1658 err: &mut Diag<'_>,
1659 ) {
1660 if let ObligationCauseCode::AwaitableExpr(hir_id) = obligation.cause.code().peel_derives()
1661 && let hir::Node::Expr(expr) = self.tcx.hir_node(*hir_id)
1662 {
1663 if let Some((_, hir::Node::Expr(await_expr))) = self.tcx.hir_parent_iter(*hir_id).nth(1)
1670 && let Some(expr_span) = expr.span.find_ancestor_inside_same_ctxt(await_expr.span)
1671 {
1672 let removal_span = self
1673 .tcx
1674 .sess
1675 .source_map()
1676 .span_extend_while_whitespace(expr_span)
1677 .shrink_to_hi()
1678 .to(await_expr.span.shrink_to_hi());
1679 err.span_suggestion_verbose(
1680 removal_span,
1681 "remove the `.await`",
1682 "",
1683 Applicability::MachineApplicable,
1684 );
1685 } else {
1686 err.span_label(obligation.cause.span, "remove the `.await`");
1687 }
1688 if let hir::Expr { span, kind: hir::ExprKind::Call(base, _), .. } = expr {
1690 if let ty::PredicateKind::Clause(ty::ClauseKind::Trait(pred)) =
1691 obligation.predicate.kind().skip_binder()
1692 {
1693 err.span_label(*span, format!("this call returns `{}`", pred.self_ty()));
1694 }
1695 if let Some(typeck_results) = &self.typeck_results
1696 && let ty = typeck_results.expr_ty_adjusted(base)
1697 && let ty::FnDef(def_id, _args) = ty.kind()
1698 && let Some(hir::Node::Item(item)) = self.tcx.hir_get_if_local(*def_id)
1699 {
1700 let (ident, _, _, _) = item.expect_fn();
1701 let msg = format!("alternatively, consider making `fn {ident}` asynchronous");
1702 if item.vis_span.is_empty() {
1703 err.span_suggestion_verbose(
1704 item.span.shrink_to_lo(),
1705 msg,
1706 "async ",
1707 Applicability::MaybeIncorrect,
1708 );
1709 } else {
1710 err.span_suggestion_verbose(
1711 item.vis_span.shrink_to_hi(),
1712 msg,
1713 " async",
1714 Applicability::MaybeIncorrect,
1715 );
1716 }
1717 }
1718 }
1719 }
1720 }
1721
1722 pub(super) fn suggest_change_mut(
1725 &self,
1726 obligation: &PredicateObligation<'tcx>,
1727 err: &mut Diag<'_>,
1728 trait_pred: ty::PolyTraitPredicate<'tcx>,
1729 ) {
1730 let points_at_arg =
1731 matches!(obligation.cause.code(), ObligationCauseCode::FunctionArg { .. },);
1732
1733 let span = obligation.cause.span;
1734 if let Ok(snippet) = self.tcx.sess.source_map().span_to_snippet(span) {
1735 let refs_number =
1736 snippet.chars().filter(|c| !c.is_whitespace()).take_while(|c| *c == '&').count();
1737 if let Some('\'') = snippet.chars().filter(|c| !c.is_whitespace()).nth(refs_number) {
1738 return;
1740 }
1741 let trait_pred = self.resolve_vars_if_possible(trait_pred);
1742 if trait_pred.has_non_region_infer() {
1743 return;
1746 }
1747
1748 if let ty::Ref(region, t_type, mutability) = *trait_pred.skip_binder().self_ty().kind()
1750 {
1751 let suggested_ty = match mutability {
1752 hir::Mutability::Mut => Ty::new_imm_ref(self.tcx, region, t_type),
1753 hir::Mutability::Not => Ty::new_mut_ref(self.tcx, region, t_type),
1754 };
1755
1756 let trait_pred_and_suggested_ty =
1758 trait_pred.map_bound(|trait_pred| (trait_pred, suggested_ty));
1759
1760 let new_obligation = self.mk_trait_obligation_with_new_self_ty(
1761 obligation.param_env,
1762 trait_pred_and_suggested_ty,
1763 );
1764 let suggested_ty_would_satisfy_obligation = self
1765 .evaluate_obligation_no_overflow(&new_obligation)
1766 .must_apply_modulo_regions();
1767 if suggested_ty_would_satisfy_obligation {
1768 let sp = self
1769 .tcx
1770 .sess
1771 .source_map()
1772 .span_take_while(span, |c| c.is_whitespace() || *c == '&');
1773 if points_at_arg && mutability.is_not() && refs_number > 0 {
1774 if snippet
1776 .trim_start_matches(|c: char| c.is_whitespace() || c == '&')
1777 .starts_with("mut")
1778 {
1779 return;
1780 }
1781 err.span_suggestion_verbose(
1782 sp,
1783 "consider changing this borrow's mutability",
1784 "&mut ",
1785 Applicability::MachineApplicable,
1786 );
1787 } else {
1788 err.note(format!(
1789 "`{}` is implemented for `{}`, but not for `{}`",
1790 trait_pred.print_modifiers_and_trait_path(),
1791 suggested_ty,
1792 trait_pred.skip_binder().self_ty(),
1793 ));
1794 }
1795 }
1796 }
1797 }
1798 }
1799
1800 pub(super) fn suggest_semicolon_removal(
1801 &self,
1802 obligation: &PredicateObligation<'tcx>,
1803 err: &mut Diag<'_>,
1804 span: Span,
1805 trait_pred: ty::PolyTraitPredicate<'tcx>,
1806 ) -> bool {
1807 let node = self.tcx.hir_node_by_def_id(obligation.cause.body_id);
1808 if let hir::Node::Item(hir::Item { kind: hir::ItemKind::Fn {sig, body: body_id, .. }, .. }) = node
1809 && let hir::ExprKind::Block(blk, _) = &self.tcx.hir_body(*body_id).value.kind
1810 && sig.decl.output.span().overlaps(span)
1811 && blk.expr.is_none()
1812 && trait_pred.self_ty().skip_binder().is_unit()
1813 && let Some(stmt) = blk.stmts.last()
1814 && let hir::StmtKind::Semi(expr) = stmt.kind
1815 && let Some(typeck_results) = &self.typeck_results
1817 && let Some(ty) = typeck_results.expr_ty_opt(expr)
1818 && self.predicate_may_hold(&self.mk_trait_obligation_with_new_self_ty(
1819 obligation.param_env, trait_pred.map_bound(|trait_pred| (trait_pred, ty))
1820 ))
1821 {
1822 err.span_label(
1823 expr.span,
1824 format!(
1825 "this expression has type `{}`, which implements `{}`",
1826 ty,
1827 trait_pred.print_modifiers_and_trait_path()
1828 ),
1829 );
1830 err.span_suggestion(
1831 self.tcx.sess.source_map().end_point(stmt.span),
1832 "remove this semicolon",
1833 "",
1834 Applicability::MachineApplicable,
1835 );
1836 return true;
1837 }
1838 false
1839 }
1840
1841 pub(super) fn return_type_span(&self, obligation: &PredicateObligation<'tcx>) -> Option<Span> {
1842 let hir::Node::Item(hir::Item { kind: hir::ItemKind::Fn { sig, .. }, .. }) =
1843 self.tcx.hir_node_by_def_id(obligation.cause.body_id)
1844 else {
1845 return None;
1846 };
1847
1848 if let hir::FnRetTy::Return(ret_ty) = sig.decl.output { Some(ret_ty.span) } else { None }
1849 }
1850
1851 pub(super) fn suggest_impl_trait(
1855 &self,
1856 err: &mut Diag<'_>,
1857 obligation: &PredicateObligation<'tcx>,
1858 trait_pred: ty::PolyTraitPredicate<'tcx>,
1859 ) -> bool {
1860 let ObligationCauseCode::SizedReturnType = obligation.cause.code() else {
1861 return false;
1862 };
1863 let ty::Dynamic(_, _, ty::Dyn) = trait_pred.self_ty().skip_binder().kind() else {
1864 return false;
1865 };
1866
1867 err.code(E0746);
1868 err.primary_message("return type cannot be a trait object without pointer indirection");
1869 err.children.clear();
1870
1871 let span = obligation.cause.span;
1872 let body = self.tcx.hir_body_owned_by(obligation.cause.body_id);
1873
1874 let mut visitor = ReturnsVisitor::default();
1875 visitor.visit_body(&body);
1876
1877 let (pre, impl_span) = if let Ok(snip) = self.tcx.sess.source_map().span_to_snippet(span)
1878 && snip.starts_with("dyn ")
1879 {
1880 ("", span.with_hi(span.lo() + BytePos(4)))
1881 } else {
1882 ("dyn ", span.shrink_to_lo())
1883 };
1884
1885 err.span_suggestion_verbose(
1886 impl_span,
1887 "consider returning an `impl Trait` instead of a `dyn Trait`",
1888 "impl ",
1889 Applicability::MaybeIncorrect,
1890 );
1891
1892 let mut sugg = vec![
1893 (span.shrink_to_lo(), format!("Box<{pre}")),
1894 (span.shrink_to_hi(), ">".to_string()),
1895 ];
1896 sugg.extend(visitor.returns.into_iter().flat_map(|expr| {
1897 let span =
1898 expr.span.find_ancestor_in_same_ctxt(obligation.cause.span).unwrap_or(expr.span);
1899 if !span.can_be_used_for_suggestions() {
1900 vec![]
1901 } else if let hir::ExprKind::Call(path, ..) = expr.kind
1902 && let hir::ExprKind::Path(hir::QPath::TypeRelative(ty, method)) = path.kind
1903 && method.ident.name == sym::new
1904 && let hir::TyKind::Path(hir::QPath::Resolved(.., box_path)) = ty.kind
1905 && box_path
1906 .res
1907 .opt_def_id()
1908 .is_some_and(|def_id| self.tcx.is_lang_item(def_id, LangItem::OwnedBox))
1909 {
1910 vec![]
1912 } else {
1913 vec![
1914 (span.shrink_to_lo(), "Box::new(".to_string()),
1915 (span.shrink_to_hi(), ")".to_string()),
1916 ]
1917 }
1918 }));
1919
1920 err.multipart_suggestion(
1921 format!(
1922 "alternatively, box the return type, and wrap all of the returned values in \
1923 `Box::new`",
1924 ),
1925 sugg,
1926 Applicability::MaybeIncorrect,
1927 );
1928
1929 true
1930 }
1931
1932 pub(super) fn report_closure_arg_mismatch(
1933 &self,
1934 span: Span,
1935 found_span: Option<Span>,
1936 found: ty::TraitRef<'tcx>,
1937 expected: ty::TraitRef<'tcx>,
1938 cause: &ObligationCauseCode<'tcx>,
1939 found_node: Option<Node<'_>>,
1940 param_env: ty::ParamEnv<'tcx>,
1941 ) -> Diag<'a> {
1942 pub(crate) fn build_fn_sig_ty<'tcx>(
1943 infcx: &InferCtxt<'tcx>,
1944 trait_ref: ty::TraitRef<'tcx>,
1945 ) -> Ty<'tcx> {
1946 let inputs = trait_ref.args.type_at(1);
1947 let sig = match inputs.kind() {
1948 ty::Tuple(inputs) if infcx.tcx.is_fn_trait(trait_ref.def_id) => {
1949 infcx.tcx.mk_fn_sig(
1950 *inputs,
1951 infcx.next_ty_var(DUMMY_SP),
1952 false,
1953 hir::Safety::Safe,
1954 ExternAbi::Rust,
1955 )
1956 }
1957 _ => infcx.tcx.mk_fn_sig(
1958 [inputs],
1959 infcx.next_ty_var(DUMMY_SP),
1960 false,
1961 hir::Safety::Safe,
1962 ExternAbi::Rust,
1963 ),
1964 };
1965
1966 Ty::new_fn_ptr(infcx.tcx, ty::Binder::dummy(sig))
1967 }
1968
1969 let argument_kind = match expected.self_ty().kind() {
1970 ty::Closure(..) => "closure",
1971 ty::Coroutine(..) => "coroutine",
1972 _ => "function",
1973 };
1974 let mut err = struct_span_code_err!(
1975 self.dcx(),
1976 span,
1977 E0631,
1978 "type mismatch in {argument_kind} arguments",
1979 );
1980
1981 err.span_label(span, "expected due to this");
1982
1983 let found_span = found_span.unwrap_or(span);
1984 err.span_label(found_span, "found signature defined here");
1985
1986 let expected = build_fn_sig_ty(self, expected);
1987 let found = build_fn_sig_ty(self, found);
1988
1989 let (expected_str, found_str) = self.cmp(expected, found);
1990
1991 let signature_kind = format!("{argument_kind} signature");
1992 err.note_expected_found(&signature_kind, expected_str, &signature_kind, found_str);
1993
1994 self.note_conflicting_fn_args(&mut err, cause, expected, found, param_env);
1995 self.note_conflicting_closure_bounds(cause, &mut err);
1996
1997 if let Some(found_node) = found_node {
1998 hint_missing_borrow(self, param_env, span, found, expected, found_node, &mut err);
1999 }
2000
2001 err
2002 }
2003
2004 fn note_conflicting_fn_args(
2005 &self,
2006 err: &mut Diag<'_>,
2007 cause: &ObligationCauseCode<'tcx>,
2008 expected: Ty<'tcx>,
2009 found: Ty<'tcx>,
2010 param_env: ty::ParamEnv<'tcx>,
2011 ) {
2012 let ObligationCauseCode::FunctionArg { arg_hir_id, .. } = cause else {
2013 return;
2014 };
2015 let ty::FnPtr(sig_tys, hdr) = expected.kind() else {
2016 return;
2017 };
2018 let expected = sig_tys.with(*hdr);
2019 let ty::FnPtr(sig_tys, hdr) = found.kind() else {
2020 return;
2021 };
2022 let found = sig_tys.with(*hdr);
2023 let Node::Expr(arg) = self.tcx.hir_node(*arg_hir_id) else {
2024 return;
2025 };
2026 let hir::ExprKind::Path(path) = arg.kind else {
2027 return;
2028 };
2029 let expected_inputs = self.tcx.instantiate_bound_regions_with_erased(expected).inputs();
2030 let found_inputs = self.tcx.instantiate_bound_regions_with_erased(found).inputs();
2031 let both_tys = expected_inputs.iter().copied().zip(found_inputs.iter().copied());
2032
2033 let arg_expr = |infcx: &InferCtxt<'tcx>, name, expected: Ty<'tcx>, found: Ty<'tcx>| {
2034 let (expected_ty, expected_refs) = get_deref_type_and_refs(expected);
2035 let (found_ty, found_refs) = get_deref_type_and_refs(found);
2036
2037 if infcx.can_eq(param_env, found_ty, expected_ty) {
2038 if found_refs.len() == expected_refs.len()
2039 && found_refs.iter().eq(expected_refs.iter())
2040 {
2041 name
2042 } else if found_refs.len() > expected_refs.len() {
2043 let refs = &found_refs[..found_refs.len() - expected_refs.len()];
2044 if found_refs[..expected_refs.len()].iter().eq(expected_refs.iter()) {
2045 format!(
2046 "{}{name}",
2047 refs.iter()
2048 .map(|mutbl| format!("&{}", mutbl.prefix_str()))
2049 .collect::<Vec<_>>()
2050 .join(""),
2051 )
2052 } else {
2053 format!(
2055 "{}*{name}",
2056 refs.iter()
2057 .map(|mutbl| format!("&{}", mutbl.prefix_str()))
2058 .collect::<Vec<_>>()
2059 .join(""),
2060 )
2061 }
2062 } else if expected_refs.len() > found_refs.len() {
2063 format!(
2064 "{}{name}",
2065 (0..(expected_refs.len() - found_refs.len()))
2066 .map(|_| "*")
2067 .collect::<Vec<_>>()
2068 .join(""),
2069 )
2070 } else {
2071 format!(
2072 "{}{name}",
2073 found_refs
2074 .iter()
2075 .map(|mutbl| format!("&{}", mutbl.prefix_str()))
2076 .chain(found_refs.iter().map(|_| "*".to_string()))
2077 .collect::<Vec<_>>()
2078 .join(""),
2079 )
2080 }
2081 } else {
2082 format!("/* {found} */")
2083 }
2084 };
2085 let args_have_same_underlying_type = both_tys.clone().all(|(expected, found)| {
2086 let (expected_ty, _) = get_deref_type_and_refs(expected);
2087 let (found_ty, _) = get_deref_type_and_refs(found);
2088 self.can_eq(param_env, found_ty, expected_ty)
2089 });
2090 let (closure_names, call_names): (Vec<_>, Vec<_>) = if args_have_same_underlying_type
2091 && !expected_inputs.is_empty()
2092 && expected_inputs.len() == found_inputs.len()
2093 && let Some(typeck) = &self.typeck_results
2094 && let Res::Def(res_kind, fn_def_id) = typeck.qpath_res(&path, *arg_hir_id)
2095 && res_kind.is_fn_like()
2096 {
2097 let closure: Vec<_> = self
2098 .tcx
2099 .fn_arg_idents(fn_def_id)
2100 .iter()
2101 .enumerate()
2102 .map(|(i, ident)| {
2103 if let Some(ident) = ident
2104 && !matches!(ident, Ident { name: kw::Underscore | kw::SelfLower, .. })
2105 {
2106 format!("{ident}")
2107 } else {
2108 format!("arg{i}")
2109 }
2110 })
2111 .collect();
2112 let args = closure
2113 .iter()
2114 .zip(both_tys)
2115 .map(|(name, (expected, found))| {
2116 arg_expr(self.infcx, name.to_owned(), expected, found)
2117 })
2118 .collect();
2119 (closure, args)
2120 } else {
2121 let closure_args = expected_inputs
2122 .iter()
2123 .enumerate()
2124 .map(|(i, _)| format!("arg{i}"))
2125 .collect::<Vec<_>>();
2126 let call_args = both_tys
2127 .enumerate()
2128 .map(|(i, (expected, found))| {
2129 arg_expr(self.infcx, format!("arg{i}"), expected, found)
2130 })
2131 .collect::<Vec<_>>();
2132 (closure_args, call_args)
2133 };
2134 let closure_names: Vec<_> = closure_names
2135 .into_iter()
2136 .zip(expected_inputs.iter())
2137 .map(|(name, ty)| {
2138 format!(
2139 "{name}{}",
2140 if ty.has_infer_types() {
2141 String::new()
2142 } else if ty.references_error() {
2143 ": /* type */".to_string()
2144 } else {
2145 format!(": {ty}")
2146 }
2147 )
2148 })
2149 .collect();
2150 err.multipart_suggestion(
2151 "consider wrapping the function in a closure",
2152 vec![
2153 (arg.span.shrink_to_lo(), format!("|{}| ", closure_names.join(", "))),
2154 (arg.span.shrink_to_hi(), format!("({})", call_names.join(", "))),
2155 ],
2156 Applicability::MaybeIncorrect,
2157 );
2158 }
2159
2160 fn note_conflicting_closure_bounds(
2163 &self,
2164 cause: &ObligationCauseCode<'tcx>,
2165 err: &mut Diag<'_>,
2166 ) {
2167 if let ObligationCauseCode::WhereClauseInExpr(def_id, _, _, idx) = cause
2171 && let predicates = self.tcx.predicates_of(def_id).instantiate_identity(self.tcx)
2172 && let Some(pred) = predicates.predicates.get(*idx)
2173 && let ty::ClauseKind::Trait(trait_pred) = pred.kind().skip_binder()
2174 && self.tcx.is_fn_trait(trait_pred.def_id())
2175 {
2176 let expected_self =
2177 self.tcx.anonymize_bound_vars(pred.kind().rebind(trait_pred.self_ty()));
2178 let expected_args =
2179 self.tcx.anonymize_bound_vars(pred.kind().rebind(trait_pred.trait_ref.args));
2180
2181 let other_pred = predicates.into_iter().enumerate().find(|(other_idx, (pred, _))| {
2184 match pred.kind().skip_binder() {
2185 ty::ClauseKind::Trait(trait_pred)
2186 if self.tcx.is_fn_trait(trait_pred.def_id())
2187 && other_idx != idx
2188 && expected_self
2191 == self.tcx.anonymize_bound_vars(
2192 pred.kind().rebind(trait_pred.self_ty()),
2193 )
2194 && expected_args
2196 != self.tcx.anonymize_bound_vars(
2197 pred.kind().rebind(trait_pred.trait_ref.args),
2198 ) =>
2199 {
2200 true
2201 }
2202 _ => false,
2203 }
2204 });
2205 if let Some((_, (_, other_pred_span))) = other_pred {
2207 err.span_note(
2208 other_pred_span,
2209 "closure inferred to have a different signature due to this bound",
2210 );
2211 }
2212 }
2213 }
2214
2215 pub(super) fn suggest_fully_qualified_path(
2216 &self,
2217 err: &mut Diag<'_>,
2218 item_def_id: DefId,
2219 span: Span,
2220 trait_ref: DefId,
2221 ) {
2222 if let Some(assoc_item) = self.tcx.opt_associated_item(item_def_id)
2223 && let ty::AssocKind::Const { .. } | ty::AssocKind::Type { .. } = assoc_item.kind
2224 {
2225 err.note(format!(
2226 "{}s cannot be accessed directly on a `trait`, they can only be \
2227 accessed through a specific `impl`",
2228 self.tcx.def_kind_descr(assoc_item.as_def_kind(), item_def_id)
2229 ));
2230
2231 if !assoc_item.is_impl_trait_in_trait() {
2232 err.span_suggestion_verbose(
2233 span,
2234 "use the fully qualified path to an implementation",
2235 format!(
2236 "<Type as {}>::{}",
2237 self.tcx.def_path_str(trait_ref),
2238 assoc_item.name()
2239 ),
2240 Applicability::HasPlaceholders,
2241 );
2242 }
2243 }
2244 }
2245
2246 #[instrument(level = "debug", skip_all, fields(?obligation.predicate, ?obligation.cause.span))]
2289 pub fn maybe_note_obligation_cause_for_async_await<G: EmissionGuarantee>(
2290 &self,
2291 err: &mut Diag<'_, G>,
2292 obligation: &PredicateObligation<'tcx>,
2293 ) -> bool {
2294 let (mut trait_ref, mut target_ty) = match obligation.predicate.kind().skip_binder() {
2317 ty::PredicateKind::Clause(ty::ClauseKind::Trait(p)) => (Some(p), Some(p.self_ty())),
2318 _ => (None, None),
2319 };
2320 let mut coroutine = None;
2321 let mut outer_coroutine = None;
2322 let mut next_code = Some(obligation.cause.code());
2323
2324 let mut seen_upvar_tys_infer_tuple = false;
2325
2326 while let Some(code) = next_code {
2327 debug!(?code);
2328 match code {
2329 ObligationCauseCode::FunctionArg { parent_code, .. } => {
2330 next_code = Some(parent_code);
2331 }
2332 ObligationCauseCode::ImplDerived(cause) => {
2333 let ty = cause.derived.parent_trait_pred.skip_binder().self_ty();
2334 debug!(
2335 parent_trait_ref = ?cause.derived.parent_trait_pred,
2336 self_ty.kind = ?ty.kind(),
2337 "ImplDerived",
2338 );
2339
2340 match *ty.kind() {
2341 ty::Coroutine(did, ..) | ty::CoroutineWitness(did, _) => {
2342 coroutine = coroutine.or(Some(did));
2343 outer_coroutine = Some(did);
2344 }
2345 ty::Tuple(_) if !seen_upvar_tys_infer_tuple => {
2346 seen_upvar_tys_infer_tuple = true;
2351 }
2352 _ if coroutine.is_none() => {
2353 trait_ref = Some(cause.derived.parent_trait_pred.skip_binder());
2354 target_ty = Some(ty);
2355 }
2356 _ => {}
2357 }
2358
2359 next_code = Some(&cause.derived.parent_code);
2360 }
2361 ObligationCauseCode::WellFormedDerived(derived_obligation)
2362 | ObligationCauseCode::BuiltinDerived(derived_obligation) => {
2363 let ty = derived_obligation.parent_trait_pred.skip_binder().self_ty();
2364 debug!(
2365 parent_trait_ref = ?derived_obligation.parent_trait_pred,
2366 self_ty.kind = ?ty.kind(),
2367 );
2368
2369 match *ty.kind() {
2370 ty::Coroutine(did, ..) | ty::CoroutineWitness(did, ..) => {
2371 coroutine = coroutine.or(Some(did));
2372 outer_coroutine = Some(did);
2373 }
2374 ty::Tuple(_) if !seen_upvar_tys_infer_tuple => {
2375 seen_upvar_tys_infer_tuple = true;
2380 }
2381 _ if coroutine.is_none() => {
2382 trait_ref = Some(derived_obligation.parent_trait_pred.skip_binder());
2383 target_ty = Some(ty);
2384 }
2385 _ => {}
2386 }
2387
2388 next_code = Some(&derived_obligation.parent_code);
2389 }
2390 _ => break,
2391 }
2392 }
2393
2394 debug!(?coroutine, ?trait_ref, ?target_ty);
2396 let (Some(coroutine_did), Some(trait_ref), Some(target_ty)) =
2397 (coroutine, trait_ref, target_ty)
2398 else {
2399 return false;
2400 };
2401
2402 let span = self.tcx.def_span(coroutine_did);
2403
2404 let coroutine_did_root = self.tcx.typeck_root_def_id(coroutine_did);
2405 debug!(
2406 ?coroutine_did,
2407 ?coroutine_did_root,
2408 typeck_results.hir_owner = ?self.typeck_results.as_ref().map(|t| t.hir_owner),
2409 ?span,
2410 );
2411
2412 let coroutine_body =
2413 coroutine_did.as_local().and_then(|def_id| self.tcx.hir_maybe_body_owned_by(def_id));
2414 let mut visitor = AwaitsVisitor::default();
2415 if let Some(body) = coroutine_body {
2416 visitor.visit_body(&body);
2417 }
2418 debug!(awaits = ?visitor.awaits);
2419
2420 let target_ty_erased = self.tcx.erase_regions(target_ty);
2423 let ty_matches = |ty| -> bool {
2424 let ty_erased = self.tcx.instantiate_bound_regions_with_erased(ty);
2437 let ty_erased = self.tcx.erase_regions(ty_erased);
2438 let eq = ty_erased == target_ty_erased;
2439 debug!(?ty_erased, ?target_ty_erased, ?eq);
2440 eq
2441 };
2442
2443 let coroutine_data = match &self.typeck_results {
2448 Some(t) if t.hir_owner.to_def_id() == coroutine_did_root => CoroutineData(t),
2449 _ if coroutine_did.is_local() => {
2450 CoroutineData(self.tcx.typeck(coroutine_did.expect_local()))
2451 }
2452 _ => return false,
2453 };
2454
2455 let coroutine_within_in_progress_typeck = match &self.typeck_results {
2456 Some(t) => t.hir_owner.to_def_id() == coroutine_did_root,
2457 _ => false,
2458 };
2459
2460 let mut interior_or_upvar_span = None;
2461
2462 let from_awaited_ty = coroutine_data.get_from_await_ty(visitor, self.tcx, ty_matches);
2463 debug!(?from_awaited_ty);
2464
2465 if coroutine_did.is_local()
2467 && !coroutine_within_in_progress_typeck
2469 && let Some(coroutine_info) = self.tcx.mir_coroutine_witnesses(coroutine_did)
2470 {
2471 debug!(?coroutine_info);
2472 'find_source: for (variant, source_info) in
2473 coroutine_info.variant_fields.iter().zip(&coroutine_info.variant_source_info)
2474 {
2475 debug!(?variant);
2476 for &local in variant {
2477 let decl = &coroutine_info.field_tys[local];
2478 debug!(?decl);
2479 if ty_matches(ty::Binder::dummy(decl.ty)) && !decl.ignore_for_traits {
2480 interior_or_upvar_span = Some(CoroutineInteriorOrUpvar::Interior(
2481 decl.source_info.span,
2482 Some((source_info.span, from_awaited_ty)),
2483 ));
2484 break 'find_source;
2485 }
2486 }
2487 }
2488 }
2489
2490 if interior_or_upvar_span.is_none() {
2491 interior_or_upvar_span =
2492 coroutine_data.try_get_upvar_span(self, coroutine_did, ty_matches);
2493 }
2494
2495 if interior_or_upvar_span.is_none() && !coroutine_did.is_local() {
2496 interior_or_upvar_span = Some(CoroutineInteriorOrUpvar::Interior(span, None));
2497 }
2498
2499 debug!(?interior_or_upvar_span);
2500 if let Some(interior_or_upvar_span) = interior_or_upvar_span {
2501 let is_async = self.tcx.coroutine_is_async(coroutine_did);
2502 self.note_obligation_cause_for_async_await(
2503 err,
2504 interior_or_upvar_span,
2505 is_async,
2506 outer_coroutine,
2507 trait_ref,
2508 target_ty,
2509 obligation,
2510 next_code,
2511 );
2512 true
2513 } else {
2514 false
2515 }
2516 }
2517
2518 #[instrument(level = "debug", skip_all)]
2521 fn note_obligation_cause_for_async_await<G: EmissionGuarantee>(
2522 &self,
2523 err: &mut Diag<'_, G>,
2524 interior_or_upvar_span: CoroutineInteriorOrUpvar,
2525 is_async: bool,
2526 outer_coroutine: Option<DefId>,
2527 trait_pred: ty::TraitPredicate<'tcx>,
2528 target_ty: Ty<'tcx>,
2529 obligation: &PredicateObligation<'tcx>,
2530 next_code: Option<&ObligationCauseCode<'tcx>>,
2531 ) {
2532 let source_map = self.tcx.sess.source_map();
2533
2534 let (await_or_yield, an_await_or_yield) =
2535 if is_async { ("await", "an await") } else { ("yield", "a yield") };
2536 let future_or_coroutine = if is_async { "future" } else { "coroutine" };
2537
2538 let trait_explanation = if let Some(name @ (sym::Send | sym::Sync)) =
2541 self.tcx.get_diagnostic_name(trait_pred.def_id())
2542 {
2543 let (trait_name, trait_verb) =
2544 if name == sym::Send { ("`Send`", "sent") } else { ("`Sync`", "shared") };
2545
2546 err.code = None;
2547 err.primary_message(format!(
2548 "{future_or_coroutine} cannot be {trait_verb} between threads safely"
2549 ));
2550
2551 let original_span = err.span.primary_span().unwrap();
2552 let mut span = MultiSpan::from_span(original_span);
2553
2554 let message = outer_coroutine
2555 .and_then(|coroutine_did| {
2556 Some(match self.tcx.coroutine_kind(coroutine_did).unwrap() {
2557 CoroutineKind::Coroutine(_) => format!("coroutine is not {trait_name}"),
2558 CoroutineKind::Desugared(
2559 CoroutineDesugaring::Async,
2560 CoroutineSource::Fn,
2561 ) => self
2562 .tcx
2563 .parent(coroutine_did)
2564 .as_local()
2565 .map(|parent_did| self.tcx.local_def_id_to_hir_id(parent_did))
2566 .and_then(|parent_hir_id| self.tcx.hir_opt_name(parent_hir_id))
2567 .map(|name| {
2568 format!("future returned by `{name}` is not {trait_name}")
2569 })?,
2570 CoroutineKind::Desugared(
2571 CoroutineDesugaring::Async,
2572 CoroutineSource::Block,
2573 ) => {
2574 format!("future created by async block is not {trait_name}")
2575 }
2576 CoroutineKind::Desugared(
2577 CoroutineDesugaring::Async,
2578 CoroutineSource::Closure,
2579 ) => {
2580 format!("future created by async closure is not {trait_name}")
2581 }
2582 CoroutineKind::Desugared(
2583 CoroutineDesugaring::AsyncGen,
2584 CoroutineSource::Fn,
2585 ) => self
2586 .tcx
2587 .parent(coroutine_did)
2588 .as_local()
2589 .map(|parent_did| self.tcx.local_def_id_to_hir_id(parent_did))
2590 .and_then(|parent_hir_id| self.tcx.hir_opt_name(parent_hir_id))
2591 .map(|name| {
2592 format!("async iterator returned by `{name}` is not {trait_name}")
2593 })?,
2594 CoroutineKind::Desugared(
2595 CoroutineDesugaring::AsyncGen,
2596 CoroutineSource::Block,
2597 ) => {
2598 format!("async iterator created by async gen block is not {trait_name}")
2599 }
2600 CoroutineKind::Desugared(
2601 CoroutineDesugaring::AsyncGen,
2602 CoroutineSource::Closure,
2603 ) => {
2604 format!(
2605 "async iterator created by async gen closure is not {trait_name}"
2606 )
2607 }
2608 CoroutineKind::Desugared(CoroutineDesugaring::Gen, CoroutineSource::Fn) => {
2609 self.tcx
2610 .parent(coroutine_did)
2611 .as_local()
2612 .map(|parent_did| self.tcx.local_def_id_to_hir_id(parent_did))
2613 .and_then(|parent_hir_id| self.tcx.hir_opt_name(parent_hir_id))
2614 .map(|name| {
2615 format!("iterator returned by `{name}` is not {trait_name}")
2616 })?
2617 }
2618 CoroutineKind::Desugared(
2619 CoroutineDesugaring::Gen,
2620 CoroutineSource::Block,
2621 ) => {
2622 format!("iterator created by gen block is not {trait_name}")
2623 }
2624 CoroutineKind::Desugared(
2625 CoroutineDesugaring::Gen,
2626 CoroutineSource::Closure,
2627 ) => {
2628 format!("iterator created by gen closure is not {trait_name}")
2629 }
2630 })
2631 })
2632 .unwrap_or_else(|| format!("{future_or_coroutine} is not {trait_name}"));
2633
2634 span.push_span_label(original_span, message);
2635 err.span(span);
2636
2637 format!("is not {trait_name}")
2638 } else {
2639 format!("does not implement `{}`", trait_pred.print_modifiers_and_trait_path())
2640 };
2641
2642 let mut explain_yield = |interior_span: Span, yield_span: Span| {
2643 let mut span = MultiSpan::from_span(yield_span);
2644 let snippet = match source_map.span_to_snippet(interior_span) {
2645 Ok(snippet) if !snippet.contains('\n') => format!("`{snippet}`"),
2648 _ => "the value".to_string(),
2649 };
2650 span.push_span_label(
2667 yield_span,
2668 format!("{await_or_yield} occurs here, with {snippet} maybe used later"),
2669 );
2670 span.push_span_label(
2671 interior_span,
2672 format!("has type `{target_ty}` which {trait_explanation}"),
2673 );
2674 err.span_note(
2675 span,
2676 format!("{future_or_coroutine} {trait_explanation} as this value is used across {an_await_or_yield}"),
2677 );
2678 };
2679 match interior_or_upvar_span {
2680 CoroutineInteriorOrUpvar::Interior(interior_span, interior_extra_info) => {
2681 if let Some((yield_span, from_awaited_ty)) = interior_extra_info {
2682 if let Some(await_span) = from_awaited_ty {
2683 let mut span = MultiSpan::from_span(await_span);
2685 span.push_span_label(
2686 await_span,
2687 format!(
2688 "await occurs here on type `{target_ty}`, which {trait_explanation}"
2689 ),
2690 );
2691 err.span_note(
2692 span,
2693 format!(
2694 "future {trait_explanation} as it awaits another future which {trait_explanation}"
2695 ),
2696 );
2697 } else {
2698 explain_yield(interior_span, yield_span);
2700 }
2701 }
2702 }
2703 CoroutineInteriorOrUpvar::Upvar(upvar_span) => {
2704 let non_send = match target_ty.kind() {
2706 ty::Ref(_, ref_ty, mutability) => match self.evaluate_obligation(obligation) {
2707 Ok(eval) if !eval.may_apply() => Some((ref_ty, mutability.is_mut())),
2708 _ => None,
2709 },
2710 _ => None,
2711 };
2712
2713 let (span_label, span_note) = match non_send {
2714 Some((ref_ty, is_mut)) => {
2718 let ref_ty_trait = if is_mut { "Send" } else { "Sync" };
2719 let ref_kind = if is_mut { "&mut" } else { "&" };
2720 (
2721 format!(
2722 "has type `{target_ty}` which {trait_explanation}, because `{ref_ty}` is not `{ref_ty_trait}`"
2723 ),
2724 format!(
2725 "captured value {trait_explanation} because `{ref_kind}` references cannot be sent unless their referent is `{ref_ty_trait}`"
2726 ),
2727 )
2728 }
2729 None => (
2730 format!("has type `{target_ty}` which {trait_explanation}"),
2731 format!("captured value {trait_explanation}"),
2732 ),
2733 };
2734
2735 let mut span = MultiSpan::from_span(upvar_span);
2736 span.push_span_label(upvar_span, span_label);
2737 err.span_note(span, span_note);
2738 }
2739 }
2740
2741 debug!(?next_code);
2744 self.note_obligation_cause_code(
2745 obligation.cause.body_id,
2746 err,
2747 obligation.predicate,
2748 obligation.param_env,
2749 next_code.unwrap(),
2750 &mut Vec::new(),
2751 &mut Default::default(),
2752 );
2753 }
2754
2755 pub(super) fn note_obligation_cause_code<G: EmissionGuarantee, T>(
2756 &self,
2757 body_id: LocalDefId,
2758 err: &mut Diag<'_, G>,
2759 predicate: T,
2760 param_env: ty::ParamEnv<'tcx>,
2761 cause_code: &ObligationCauseCode<'tcx>,
2762 obligated_types: &mut Vec<Ty<'tcx>>,
2763 seen_requirements: &mut FxHashSet<DefId>,
2764 ) where
2765 T: Upcast<TyCtxt<'tcx>, ty::Predicate<'tcx>>,
2766 {
2767 let tcx = self.tcx;
2768 let predicate = predicate.upcast(tcx);
2769 let suggest_remove_deref = |err: &mut Diag<'_, G>, expr: &hir::Expr<'_>| {
2770 if let Some(pred) = predicate.as_trait_clause()
2771 && tcx.is_lang_item(pred.def_id(), LangItem::Sized)
2772 && let hir::ExprKind::Unary(hir::UnOp::Deref, inner) = expr.kind
2773 {
2774 err.span_suggestion_verbose(
2775 expr.span.until(inner.span),
2776 "references are always `Sized`, even if they point to unsized data; consider \
2777 not dereferencing the expression",
2778 String::new(),
2779 Applicability::MaybeIncorrect,
2780 );
2781 }
2782 };
2783 match *cause_code {
2784 ObligationCauseCode::ExprAssignable
2785 | ObligationCauseCode::MatchExpressionArm { .. }
2786 | ObligationCauseCode::Pattern { .. }
2787 | ObligationCauseCode::IfExpression { .. }
2788 | ObligationCauseCode::IfExpressionWithNoElse
2789 | ObligationCauseCode::MainFunctionType
2790 | ObligationCauseCode::LangFunctionType(_)
2791 | ObligationCauseCode::IntrinsicType
2792 | ObligationCauseCode::MethodReceiver
2793 | ObligationCauseCode::ReturnNoExpression
2794 | ObligationCauseCode::Misc
2795 | ObligationCauseCode::WellFormed(..)
2796 | ObligationCauseCode::MatchImpl(..)
2797 | ObligationCauseCode::ReturnValue(_)
2798 | ObligationCauseCode::BlockTailExpression(..)
2799 | ObligationCauseCode::AwaitableExpr(_)
2800 | ObligationCauseCode::ForLoopIterator
2801 | ObligationCauseCode::QuestionMark
2802 | ObligationCauseCode::CheckAssociatedTypeBounds { .. }
2803 | ObligationCauseCode::LetElse
2804 | ObligationCauseCode::UnOp { .. }
2805 | ObligationCauseCode::BinOp { .. }
2806 | ObligationCauseCode::AscribeUserTypeProvePredicate(..)
2807 | ObligationCauseCode::AlwaysApplicableImpl
2808 | ObligationCauseCode::ConstParam(_)
2809 | ObligationCauseCode::ReferenceOutlivesReferent(..)
2810 | ObligationCauseCode::ObjectTypeBound(..) => {}
2811 ObligationCauseCode::RustCall => {
2812 if let Some(pred) = predicate.as_trait_clause()
2813 && tcx.is_lang_item(pred.def_id(), LangItem::Sized)
2814 {
2815 err.note("argument required to be sized due to `extern \"rust-call\"` ABI");
2816 }
2817 }
2818 ObligationCauseCode::SliceOrArrayElem => {
2819 err.note("slice and array elements must have `Sized` type");
2820 }
2821 ObligationCauseCode::ArrayLen(array_ty) => {
2822 err.note(format!("the length of array `{array_ty}` must be type `usize`"));
2823 }
2824 ObligationCauseCode::TupleElem => {
2825 err.note("only the last element of a tuple may have a dynamically sized type");
2826 }
2827 ObligationCauseCode::DynCompatible(span) => {
2828 err.multipart_suggestion(
2829 "you might have meant to use `Self` to refer to the implementing type",
2830 vec![(span, "Self".into())],
2831 Applicability::MachineApplicable,
2832 );
2833 }
2834 ObligationCauseCode::WhereClause(item_def_id, span)
2835 | ObligationCauseCode::WhereClauseInExpr(item_def_id, span, ..)
2836 | ObligationCauseCode::HostEffectInExpr(item_def_id, span, ..)
2837 if !span.is_dummy() =>
2838 {
2839 if let ObligationCauseCode::WhereClauseInExpr(_, _, hir_id, pos) = &cause_code {
2840 if let Node::Expr(expr) = tcx.parent_hir_node(*hir_id)
2841 && let hir::ExprKind::Call(_, args) = expr.kind
2842 && let Some(expr) = args.get(*pos)
2843 {
2844 suggest_remove_deref(err, &expr);
2845 } else if let Node::Expr(expr) = self.tcx.hir_node(*hir_id)
2846 && let hir::ExprKind::MethodCall(_, _, args, _) = expr.kind
2847 && let Some(expr) = args.get(*pos)
2848 {
2849 suggest_remove_deref(err, &expr);
2850 }
2851 }
2852 let item_name = tcx.def_path_str(item_def_id);
2853 let short_item_name = with_forced_trimmed_paths!(tcx.def_path_str(item_def_id));
2854 let mut multispan = MultiSpan::from(span);
2855 let sm = tcx.sess.source_map();
2856 if let Some(ident) = tcx.opt_item_ident(item_def_id) {
2857 let same_line =
2858 match (sm.lookup_line(ident.span.hi()), sm.lookup_line(span.lo())) {
2859 (Ok(l), Ok(r)) => l.line == r.line,
2860 _ => true,
2861 };
2862 if ident.span.is_visible(sm) && !ident.span.overlaps(span) && !same_line {
2863 multispan.push_span_label(
2864 ident.span,
2865 format!(
2866 "required by a bound in this {}",
2867 tcx.def_kind(item_def_id).descr(item_def_id)
2868 ),
2869 );
2870 }
2871 }
2872 let mut a = "a";
2873 let mut this = "this bound";
2874 let mut note = None;
2875 let mut help = None;
2876 if let ty::PredicateKind::Clause(clause) = predicate.kind().skip_binder() {
2877 match clause {
2878 ty::ClauseKind::Trait(trait_pred) => {
2879 let def_id = trait_pred.def_id();
2880 let visible_item = if let Some(local) = def_id.as_local() {
2881 let ty = trait_pred.self_ty();
2882 if let ty::Adt(adt, _) = ty.kind() {
2886 let visibilities = &tcx.resolutions(()).effective_visibilities;
2887 visibilities.effective_vis(local).is_none_or(|v| {
2888 v.at_level(Level::Reexported)
2889 .is_accessible_from(adt.did(), tcx)
2890 })
2891 } else {
2892 true
2894 }
2895 } else {
2896 tcx.visible_parent_map(()).get(&def_id).is_some()
2898 };
2899 if tcx.is_lang_item(def_id, LangItem::Sized) {
2900 if tcx
2902 .generics_of(item_def_id)
2903 .own_params
2904 .iter()
2905 .any(|param| tcx.def_span(param.def_id) == span)
2906 {
2907 a = "an implicit `Sized`";
2908 this =
2909 "the implicit `Sized` requirement on this type parameter";
2910 }
2911 if let Some(hir::Node::TraitItem(hir::TraitItem {
2912 generics,
2913 kind: hir::TraitItemKind::Type(bounds, None),
2914 ..
2915 })) = tcx.hir_get_if_local(item_def_id)
2916 && !bounds.iter()
2918 .filter_map(|bound| bound.trait_ref())
2919 .any(|tr| tr.trait_def_id().is_some_and(|def_id| tcx.is_lang_item(def_id, LangItem::Sized)))
2920 {
2921 let (span, separator) = if let [.., last] = bounds {
2922 (last.span().shrink_to_hi(), " +")
2923 } else {
2924 (generics.span.shrink_to_hi(), ":")
2925 };
2926 err.span_suggestion_verbose(
2927 span,
2928 "consider relaxing the implicit `Sized` restriction",
2929 format!("{separator} ?Sized"),
2930 Applicability::MachineApplicable,
2931 );
2932 }
2933 }
2934 if let DefKind::Trait = tcx.def_kind(item_def_id)
2935 && !visible_item
2936 {
2937 note = Some(format!(
2938 "`{short_item_name}` is a \"sealed trait\", because to implement it \
2939 you also need to implement `{}`, which is not accessible; this is \
2940 usually done to force you to use one of the provided types that \
2941 already implement it",
2942 with_no_trimmed_paths!(tcx.def_path_str(def_id)),
2943 ));
2944 let impls_of = tcx.trait_impls_of(def_id);
2945 let impls = impls_of
2946 .non_blanket_impls()
2947 .values()
2948 .flatten()
2949 .chain(impls_of.blanket_impls().iter())
2950 .collect::<Vec<_>>();
2951 if !impls.is_empty() {
2952 let len = impls.len();
2953 let mut types = impls
2954 .iter()
2955 .map(|t| {
2956 with_no_trimmed_paths!(format!(
2957 " {}",
2958 tcx.type_of(*t).instantiate_identity(),
2959 ))
2960 })
2961 .collect::<Vec<_>>();
2962 let post = if types.len() > 9 {
2963 types.truncate(8);
2964 format!("\nand {} others", len - 8)
2965 } else {
2966 String::new()
2967 };
2968 help = Some(format!(
2969 "the following type{} implement{} the trait:\n{}{post}",
2970 pluralize!(len),
2971 if len == 1 { "s" } else { "" },
2972 types.join("\n"),
2973 ));
2974 }
2975 }
2976 }
2977 ty::ClauseKind::ConstArgHasType(..) => {
2978 let descr =
2979 format!("required by a const generic parameter in `{item_name}`");
2980 if span.is_visible(sm) {
2981 let msg = format!(
2982 "required by this const generic parameter in `{short_item_name}`"
2983 );
2984 multispan.push_span_label(span, msg);
2985 err.span_note(multispan, descr);
2986 } else {
2987 err.span_note(tcx.def_span(item_def_id), descr);
2988 }
2989 return;
2990 }
2991 _ => (),
2992 }
2993 }
2994
2995 let is_in_fmt_lit = if let Some(s) = err.span.primary_span() {
2998 matches!(s.desugaring_kind(), Some(DesugaringKind::FormatLiteral { .. }))
2999 } else {
3000 false
3001 };
3002 if !is_in_fmt_lit {
3003 let descr = format!("required by {a} bound in `{item_name}`");
3004 if span.is_visible(sm) {
3005 let msg = format!("required by {this} in `{short_item_name}`");
3006 multispan.push_span_label(span, msg);
3007 err.span_note(multispan, descr);
3008 } else {
3009 err.span_note(tcx.def_span(item_def_id), descr);
3010 }
3011 }
3012 if let Some(note) = note {
3013 err.note(note);
3014 }
3015 if let Some(help) = help {
3016 err.help(help);
3017 }
3018 }
3019 ObligationCauseCode::WhereClause(..)
3020 | ObligationCauseCode::WhereClauseInExpr(..)
3021 | ObligationCauseCode::HostEffectInExpr(..) => {
3022 }
3025 ObligationCauseCode::OpaqueTypeBound(span, definition_def_id) => {
3026 err.span_note(span, "required by a bound in an opaque type");
3027 if let Some(definition_def_id) = definition_def_id
3028 && self.tcx.typeck(definition_def_id).coroutine_stalled_predicates.is_empty()
3032 {
3033 err.span_note(
3036 tcx.def_span(definition_def_id),
3037 "this definition site has more where clauses than the opaque type",
3038 );
3039 }
3040 }
3041 ObligationCauseCode::Coercion { source, target } => {
3042 let source =
3043 tcx.short_string(self.resolve_vars_if_possible(source), err.long_ty_path());
3044 let target =
3045 tcx.short_string(self.resolve_vars_if_possible(target), err.long_ty_path());
3046 err.note(with_forced_trimmed_paths!(format!(
3047 "required for the cast from `{source}` to `{target}`",
3048 )));
3049 }
3050 ObligationCauseCode::RepeatElementCopy { is_constable, elt_span } => {
3051 err.note(
3052 "the `Copy` trait is required because this value will be copied for each element of the array",
3053 );
3054 let sm = tcx.sess.source_map();
3055 if matches!(is_constable, IsConstable::Fn | IsConstable::Ctor)
3056 && let Ok(_) = sm.span_to_snippet(elt_span)
3057 {
3058 err.multipart_suggestion(
3059 "create an inline `const` block",
3060 vec![
3061 (elt_span.shrink_to_lo(), "const { ".to_string()),
3062 (elt_span.shrink_to_hi(), " }".to_string()),
3063 ],
3064 Applicability::MachineApplicable,
3065 );
3066 } else {
3067 err.help("consider using `core::array::from_fn` to initialize the array");
3069 err.help("see https://doc.rust-lang.org/stable/std/array/fn.from_fn.html for more information");
3070 }
3071 }
3072 ObligationCauseCode::VariableType(hir_id) => {
3073 if let Some(typeck_results) = &self.typeck_results
3074 && let Some(ty) = typeck_results.node_type_opt(hir_id)
3075 && let ty::Error(_) = ty.kind()
3076 {
3077 err.note(format!(
3078 "`{predicate}` isn't satisfied, but the type of this pattern is \
3079 `{{type error}}`",
3080 ));
3081 err.downgrade_to_delayed_bug();
3082 }
3083 let mut local = true;
3084 match tcx.parent_hir_node(hir_id) {
3085 Node::LetStmt(hir::LetStmt { ty: Some(ty), .. }) => {
3086 err.span_suggestion_verbose(
3087 ty.span.shrink_to_lo(),
3088 "consider borrowing here",
3089 "&",
3090 Applicability::MachineApplicable,
3091 );
3092 }
3093 Node::LetStmt(hir::LetStmt {
3094 init: Some(hir::Expr { kind: hir::ExprKind::Index(..), span, .. }),
3095 ..
3096 }) => {
3097 err.span_suggestion_verbose(
3101 span.shrink_to_lo(),
3102 "consider borrowing here",
3103 "&",
3104 Applicability::MachineApplicable,
3105 );
3106 }
3107 Node::LetStmt(hir::LetStmt { init: Some(expr), .. }) => {
3108 suggest_remove_deref(err, &expr);
3111 }
3112 Node::Param(param) => {
3113 err.span_suggestion_verbose(
3114 param.ty_span.shrink_to_lo(),
3115 "function arguments must have a statically known size, borrowed types \
3116 always have a known size",
3117 "&",
3118 Applicability::MachineApplicable,
3119 );
3120 local = false;
3121 }
3122 _ => {}
3123 }
3124 if local {
3125 err.note("all local variables must have a statically known size");
3126 }
3127 }
3128 ObligationCauseCode::SizedArgumentType(hir_id) => {
3129 let mut ty = None;
3130 let borrowed_msg = "function arguments must have a statically known size, borrowed \
3131 types always have a known size";
3132 if let Some(hir_id) = hir_id
3133 && let hir::Node::Param(param) = self.tcx.hir_node(hir_id)
3134 && let Some(decl) = self.tcx.parent_hir_node(hir_id).fn_decl()
3135 && let Some(t) = decl.inputs.iter().find(|t| param.ty_span.contains(t.span))
3136 {
3137 ty = Some(t);
3145 } else if let Some(hir_id) = hir_id
3146 && let hir::Node::Ty(t) = self.tcx.hir_node(hir_id)
3147 {
3148 ty = Some(t);
3149 }
3150 if let Some(ty) = ty {
3151 match ty.kind {
3152 hir::TyKind::TraitObject(traits, _) => {
3153 let (span, kw) = match traits {
3154 [first, ..] if first.span.lo() == ty.span.lo() => {
3155 (ty.span.shrink_to_lo(), "dyn ")
3157 }
3158 [first, ..] => (ty.span.until(first.span), ""),
3159 [] => span_bug!(ty.span, "trait object with no traits: {ty:?}"),
3160 };
3161 let needs_parens = traits.len() != 1;
3162 if let Some(hir_id) = hir_id
3164 && matches!(
3165 self.tcx.parent_hir_node(hir_id),
3166 hir::Node::Item(hir::Item {
3167 kind: hir::ItemKind::Fn { .. },
3168 ..
3169 })
3170 )
3171 {
3172 err.span_suggestion_verbose(
3173 span,
3174 "you can use `impl Trait` as the argument type",
3175 "impl ",
3176 Applicability::MaybeIncorrect,
3177 );
3178 }
3179 let sugg = if !needs_parens {
3180 vec![(span.shrink_to_lo(), format!("&{kw}"))]
3181 } else {
3182 vec![
3183 (span.shrink_to_lo(), format!("&({kw}")),
3184 (ty.span.shrink_to_hi(), ")".to_string()),
3185 ]
3186 };
3187 err.multipart_suggestion_verbose(
3188 borrowed_msg,
3189 sugg,
3190 Applicability::MachineApplicable,
3191 );
3192 }
3193 hir::TyKind::Slice(_ty) => {
3194 err.span_suggestion_verbose(
3195 ty.span.shrink_to_lo(),
3196 "function arguments must have a statically known size, borrowed \
3197 slices always have a known size",
3198 "&",
3199 Applicability::MachineApplicable,
3200 );
3201 }
3202 hir::TyKind::Path(_) => {
3203 err.span_suggestion_verbose(
3204 ty.span.shrink_to_lo(),
3205 borrowed_msg,
3206 "&",
3207 Applicability::MachineApplicable,
3208 );
3209 }
3210 _ => {}
3211 }
3212 } else {
3213 err.note("all function arguments must have a statically known size");
3214 }
3215 if tcx.sess.opts.unstable_features.is_nightly_build()
3216 && !tcx.features().unsized_fn_params()
3217 {
3218 err.help("unsized fn params are gated as an unstable feature");
3219 }
3220 }
3221 ObligationCauseCode::SizedReturnType | ObligationCauseCode::SizedCallReturnType => {
3222 err.note("the return type of a function must have a statically known size");
3223 }
3224 ObligationCauseCode::SizedYieldType => {
3225 err.note("the yield type of a coroutine must have a statically known size");
3226 }
3227 ObligationCauseCode::AssignmentLhsSized => {
3228 err.note("the left-hand-side of an assignment must have a statically known size");
3229 }
3230 ObligationCauseCode::TupleInitializerSized => {
3231 err.note("tuples must have a statically known size to be initialized");
3232 }
3233 ObligationCauseCode::StructInitializerSized => {
3234 err.note("structs must have a statically known size to be initialized");
3235 }
3236 ObligationCauseCode::FieldSized { adt_kind: ref item, last, span } => {
3237 match *item {
3238 AdtKind::Struct => {
3239 if last {
3240 err.note(
3241 "the last field of a packed struct may only have a \
3242 dynamically sized type if it does not need drop to be run",
3243 );
3244 } else {
3245 err.note(
3246 "only the last field of a struct may have a dynamically sized type",
3247 );
3248 }
3249 }
3250 AdtKind::Union => {
3251 err.note("no field of a union may have a dynamically sized type");
3252 }
3253 AdtKind::Enum => {
3254 err.note("no field of an enum variant may have a dynamically sized type");
3255 }
3256 }
3257 err.help("change the field's type to have a statically known size");
3258 err.span_suggestion_verbose(
3259 span.shrink_to_lo(),
3260 "borrowed types always have a statically known size",
3261 "&",
3262 Applicability::MachineApplicable,
3263 );
3264 err.multipart_suggestion_verbose(
3265 "the `Box` type always has a statically known size and allocates its contents \
3266 in the heap",
3267 vec![
3268 (span.shrink_to_lo(), "Box<".to_string()),
3269 (span.shrink_to_hi(), ">".to_string()),
3270 ],
3271 Applicability::MachineApplicable,
3272 );
3273 }
3274 ObligationCauseCode::SizedConstOrStatic => {
3275 err.note("statics and constants must have a statically known size");
3276 }
3277 ObligationCauseCode::InlineAsmSized => {
3278 err.note("all inline asm arguments must have a statically known size");
3279 }
3280 ObligationCauseCode::SizedClosureCapture(closure_def_id) => {
3281 err.note(
3282 "all values captured by value by a closure must have a statically known size",
3283 );
3284 let hir::ExprKind::Closure(closure) =
3285 tcx.hir_node_by_def_id(closure_def_id).expect_expr().kind
3286 else {
3287 bug!("expected closure in SizedClosureCapture obligation");
3288 };
3289 if let hir::CaptureBy::Value { .. } = closure.capture_clause
3290 && let Some(span) = closure.fn_arg_span
3291 {
3292 err.span_label(span, "this closure captures all values by move");
3293 }
3294 }
3295 ObligationCauseCode::SizedCoroutineInterior(coroutine_def_id) => {
3296 let what = match tcx.coroutine_kind(coroutine_def_id) {
3297 None
3298 | Some(hir::CoroutineKind::Coroutine(_))
3299 | Some(hir::CoroutineKind::Desugared(hir::CoroutineDesugaring::Gen, _)) => {
3300 "yield"
3301 }
3302 Some(hir::CoroutineKind::Desugared(hir::CoroutineDesugaring::Async, _)) => {
3303 "await"
3304 }
3305 Some(hir::CoroutineKind::Desugared(hir::CoroutineDesugaring::AsyncGen, _)) => {
3306 "yield`/`await"
3307 }
3308 };
3309 err.note(format!(
3310 "all values live across `{what}` must have a statically known size"
3311 ));
3312 }
3313 ObligationCauseCode::SharedStatic => {
3314 err.note("shared static variables must have a type that implements `Sync`");
3315 }
3316 ObligationCauseCode::BuiltinDerived(ref data) => {
3317 let parent_trait_ref = self.resolve_vars_if_possible(data.parent_trait_pred);
3318 let ty = parent_trait_ref.skip_binder().self_ty();
3319 if parent_trait_ref.references_error() {
3320 err.downgrade_to_delayed_bug();
3323 return;
3324 }
3325
3326 let is_upvar_tys_infer_tuple = if !matches!(ty.kind(), ty::Tuple(..)) {
3329 false
3330 } else if let ObligationCauseCode::BuiltinDerived(data) = &*data.parent_code {
3331 let parent_trait_ref = self.resolve_vars_if_possible(data.parent_trait_pred);
3332 let nested_ty = parent_trait_ref.skip_binder().self_ty();
3333 matches!(nested_ty.kind(), ty::Coroutine(..))
3334 || matches!(nested_ty.kind(), ty::Closure(..))
3335 } else {
3336 false
3337 };
3338
3339 let is_builtin_async_fn_trait =
3340 tcx.async_fn_trait_kind_from_def_id(data.parent_trait_pred.def_id()).is_some();
3341
3342 if !is_upvar_tys_infer_tuple && !is_builtin_async_fn_trait {
3343 let mut msg = || {
3344 let ty_str = tcx.short_string(ty, err.long_ty_path());
3345 format!("required because it appears within the type `{ty_str}`")
3346 };
3347 match ty.kind() {
3348 ty::Adt(def, _) => {
3349 let msg = msg();
3350 match tcx.opt_item_ident(def.did()) {
3351 Some(ident) => {
3352 err.span_note(ident.span, msg);
3353 }
3354 None => {
3355 err.note(msg);
3356 }
3357 }
3358 }
3359 ty::Alias(ty::Opaque, ty::AliasTy { def_id, .. }) => {
3360 let is_future = tcx.ty_is_opaque_future(ty);
3363 debug!(
3364 ?obligated_types,
3365 ?is_future,
3366 "note_obligation_cause_code: check for async fn"
3367 );
3368 if is_future
3369 && obligated_types.last().is_some_and(|ty| match ty.kind() {
3370 ty::Coroutine(last_def_id, ..) => {
3371 tcx.coroutine_is_async(*last_def_id)
3372 }
3373 _ => false,
3374 })
3375 {
3376 } else {
3378 let msg = msg();
3379 err.span_note(tcx.def_span(def_id), msg);
3380 }
3381 }
3382 ty::Coroutine(def_id, _) => {
3383 let sp = tcx.def_span(def_id);
3384
3385 let kind = tcx.coroutine_kind(def_id).unwrap();
3387 err.span_note(
3388 sp,
3389 with_forced_trimmed_paths!(format!(
3390 "required because it's used within this {kind:#}",
3391 )),
3392 );
3393 }
3394 ty::CoroutineWitness(..) => {
3395 }
3398 ty::Closure(def_id, _) | ty::CoroutineClosure(def_id, _) => {
3399 err.span_note(
3400 tcx.def_span(def_id),
3401 "required because it's used within this closure",
3402 );
3403 }
3404 ty::Str => {
3405 err.note("`str` is considered to contain a `[u8]` slice for auto trait purposes");
3406 }
3407 _ => {
3408 let msg = msg();
3409 err.note(msg);
3410 }
3411 };
3412 }
3413
3414 obligated_types.push(ty);
3415
3416 let parent_predicate = parent_trait_ref;
3417 if !self.is_recursive_obligation(obligated_types, &data.parent_code) {
3418 ensure_sufficient_stack(|| {
3420 self.note_obligation_cause_code(
3421 body_id,
3422 err,
3423 parent_predicate,
3424 param_env,
3425 &data.parent_code,
3426 obligated_types,
3427 seen_requirements,
3428 )
3429 });
3430 } else {
3431 ensure_sufficient_stack(|| {
3432 self.note_obligation_cause_code(
3433 body_id,
3434 err,
3435 parent_predicate,
3436 param_env,
3437 cause_code.peel_derives(),
3438 obligated_types,
3439 seen_requirements,
3440 )
3441 });
3442 }
3443 }
3444 ObligationCauseCode::ImplDerived(ref data) => {
3445 let mut parent_trait_pred =
3446 self.resolve_vars_if_possible(data.derived.parent_trait_pred);
3447 let parent_def_id = parent_trait_pred.def_id();
3448 if tcx.is_diagnostic_item(sym::FromResidual, parent_def_id)
3449 && !tcx.features().enabled(sym::try_trait_v2)
3450 {
3451 return;
3455 }
3456 let self_ty_str =
3457 tcx.short_string(parent_trait_pred.skip_binder().self_ty(), err.long_ty_path());
3458 let trait_name = tcx.short_string(
3459 parent_trait_pred.print_modifiers_and_trait_path(),
3460 err.long_ty_path(),
3461 );
3462 let msg = format!("required for `{self_ty_str}` to implement `{trait_name}`");
3463 let mut is_auto_trait = false;
3464 match tcx.hir_get_if_local(data.impl_or_alias_def_id) {
3465 Some(Node::Item(hir::Item {
3466 kind: hir::ItemKind::Trait(_, is_auto, _, ident, ..),
3467 ..
3468 })) => {
3469 is_auto_trait = matches!(is_auto, hir::IsAuto::Yes);
3472 err.span_note(ident.span, msg);
3473 }
3474 Some(Node::Item(hir::Item {
3475 kind: hir::ItemKind::Impl(hir::Impl { of_trait, self_ty, generics, .. }),
3476 ..
3477 })) => {
3478 let mut spans = Vec::with_capacity(2);
3479 if let Some(of_trait) = of_trait {
3480 spans.push(of_trait.trait_ref.path.span);
3481 }
3482 spans.push(self_ty.span);
3483 let mut spans: MultiSpan = spans.into();
3484 if matches!(
3485 self_ty.span.ctxt().outer_expn_data().kind,
3486 ExpnKind::Macro(MacroKind::Derive, _)
3487 ) || matches!(
3488 of_trait.map(|t| t.trait_ref.path.span.ctxt().outer_expn_data().kind),
3489 Some(ExpnKind::Macro(MacroKind::Derive, _))
3490 ) {
3491 spans.push_span_label(
3492 data.span,
3493 "unsatisfied trait bound introduced in this `derive` macro",
3494 );
3495 } else if !data.span.is_dummy() && !data.span.overlaps(self_ty.span) {
3496 spans.push_span_label(
3497 data.span,
3498 "unsatisfied trait bound introduced here",
3499 );
3500 }
3501 err.span_note(spans, msg);
3502 point_at_assoc_type_restriction(
3503 tcx,
3504 err,
3505 &self_ty_str,
3506 &trait_name,
3507 predicate,
3508 &generics,
3509 &data,
3510 );
3511 }
3512 _ => {
3513 err.note(msg);
3514 }
3515 };
3516
3517 let mut parent_predicate = parent_trait_pred;
3518 let mut data = &data.derived;
3519 let mut count = 0;
3520 seen_requirements.insert(parent_def_id);
3521 if is_auto_trait {
3522 while let ObligationCauseCode::BuiltinDerived(derived) = &*data.parent_code {
3525 let child_trait_ref =
3526 self.resolve_vars_if_possible(derived.parent_trait_pred);
3527 let child_def_id = child_trait_ref.def_id();
3528 if seen_requirements.insert(child_def_id) {
3529 break;
3530 }
3531 data = derived;
3532 parent_predicate = child_trait_ref.upcast(tcx);
3533 parent_trait_pred = child_trait_ref;
3534 }
3535 }
3536 while let ObligationCauseCode::ImplDerived(child) = &*data.parent_code {
3537 let child_trait_pred =
3539 self.resolve_vars_if_possible(child.derived.parent_trait_pred);
3540 let child_def_id = child_trait_pred.def_id();
3541 if seen_requirements.insert(child_def_id) {
3542 break;
3543 }
3544 count += 1;
3545 data = &child.derived;
3546 parent_predicate = child_trait_pred.upcast(tcx);
3547 parent_trait_pred = child_trait_pred;
3548 }
3549 if count > 0 {
3550 err.note(format!(
3551 "{} redundant requirement{} hidden",
3552 count,
3553 pluralize!(count)
3554 ));
3555 let self_ty = tcx.short_string(
3556 parent_trait_pred.skip_binder().self_ty(),
3557 err.long_ty_path(),
3558 );
3559 let trait_path = tcx.short_string(
3560 parent_trait_pred.print_modifiers_and_trait_path(),
3561 err.long_ty_path(),
3562 );
3563 err.note(format!("required for `{self_ty}` to implement `{trait_path}`"));
3564 }
3565 ensure_sufficient_stack(|| {
3567 self.note_obligation_cause_code(
3568 body_id,
3569 err,
3570 parent_predicate,
3571 param_env,
3572 &data.parent_code,
3573 obligated_types,
3574 seen_requirements,
3575 )
3576 });
3577 }
3578 ObligationCauseCode::ImplDerivedHost(ref data) => {
3579 let self_ty = tcx.short_string(
3580 self.resolve_vars_if_possible(data.derived.parent_host_pred.self_ty()),
3581 err.long_ty_path(),
3582 );
3583 let trait_path = tcx.short_string(
3584 data.derived
3585 .parent_host_pred
3586 .map_bound(|pred| pred.trait_ref)
3587 .print_only_trait_path(),
3588 err.long_ty_path(),
3589 );
3590 let msg = format!(
3591 "required for `{self_ty}` to implement `{} {trait_path}`",
3592 data.derived.parent_host_pred.skip_binder().constness,
3593 );
3594 match tcx.hir_get_if_local(data.impl_def_id) {
3595 Some(Node::Item(hir::Item {
3596 kind: hir::ItemKind::Impl(hir::Impl { of_trait, self_ty, .. }),
3597 ..
3598 })) => {
3599 let mut spans = vec![self_ty.span];
3600 spans.extend(of_trait.map(|t| t.trait_ref.path.span));
3601 let mut spans: MultiSpan = spans.into();
3602 spans.push_span_label(data.span, "unsatisfied trait bound introduced here");
3603 err.span_note(spans, msg);
3604 }
3605 _ => {
3606 err.note(msg);
3607 }
3608 }
3609 ensure_sufficient_stack(|| {
3610 self.note_obligation_cause_code(
3611 body_id,
3612 err,
3613 data.derived.parent_host_pred,
3614 param_env,
3615 &data.derived.parent_code,
3616 obligated_types,
3617 seen_requirements,
3618 )
3619 });
3620 }
3621 ObligationCauseCode::BuiltinDerivedHost(ref data) => {
3622 ensure_sufficient_stack(|| {
3623 self.note_obligation_cause_code(
3624 body_id,
3625 err,
3626 data.parent_host_pred,
3627 param_env,
3628 &data.parent_code,
3629 obligated_types,
3630 seen_requirements,
3631 )
3632 });
3633 }
3634 ObligationCauseCode::WellFormedDerived(ref data) => {
3635 let parent_trait_ref = self.resolve_vars_if_possible(data.parent_trait_pred);
3636 let parent_predicate = parent_trait_ref;
3637 ensure_sufficient_stack(|| {
3639 self.note_obligation_cause_code(
3640 body_id,
3641 err,
3642 parent_predicate,
3643 param_env,
3644 &data.parent_code,
3645 obligated_types,
3646 seen_requirements,
3647 )
3648 });
3649 }
3650 ObligationCauseCode::TypeAlias(ref nested, span, def_id) => {
3651 ensure_sufficient_stack(|| {
3653 self.note_obligation_cause_code(
3654 body_id,
3655 err,
3656 predicate,
3657 param_env,
3658 nested,
3659 obligated_types,
3660 seen_requirements,
3661 )
3662 });
3663 let mut multispan = MultiSpan::from(span);
3664 multispan.push_span_label(span, "required by this bound");
3665 err.span_note(
3666 multispan,
3667 format!("required by a bound on the type alias `{}`", tcx.item_name(def_id)),
3668 );
3669 }
3670 ObligationCauseCode::FunctionArg {
3671 arg_hir_id, call_hir_id, ref parent_code, ..
3672 } => {
3673 self.note_function_argument_obligation(
3674 body_id,
3675 err,
3676 arg_hir_id,
3677 parent_code,
3678 param_env,
3679 predicate,
3680 call_hir_id,
3681 );
3682 ensure_sufficient_stack(|| {
3683 self.note_obligation_cause_code(
3684 body_id,
3685 err,
3686 predicate,
3687 param_env,
3688 parent_code,
3689 obligated_types,
3690 seen_requirements,
3691 )
3692 });
3693 }
3694 ObligationCauseCode::CompareImplItem { trait_item_def_id, .. }
3697 if tcx.is_impl_trait_in_trait(trait_item_def_id) => {}
3698 ObligationCauseCode::CompareImplItem { trait_item_def_id, kind, .. } => {
3699 let item_name = tcx.item_name(trait_item_def_id);
3700 let msg = format!(
3701 "the requirement `{predicate}` appears on the `impl`'s {kind} \
3702 `{item_name}` but not on the corresponding trait's {kind}",
3703 );
3704 let sp = tcx
3705 .opt_item_ident(trait_item_def_id)
3706 .map(|i| i.span)
3707 .unwrap_or_else(|| tcx.def_span(trait_item_def_id));
3708 let mut assoc_span: MultiSpan = sp.into();
3709 assoc_span.push_span_label(
3710 sp,
3711 format!("this trait's {kind} doesn't have the requirement `{predicate}`"),
3712 );
3713 if let Some(ident) = tcx
3714 .opt_associated_item(trait_item_def_id)
3715 .and_then(|i| tcx.opt_item_ident(i.container_id(tcx)))
3716 {
3717 assoc_span.push_span_label(ident.span, "in this trait");
3718 }
3719 err.span_note(assoc_span, msg);
3720 }
3721 ObligationCauseCode::TrivialBound => {
3722 err.help("see issue #48214");
3723 tcx.disabled_nightly_features(err, [(String::new(), sym::trivial_bounds)]);
3724 }
3725 ObligationCauseCode::OpaqueReturnType(expr_info) => {
3726 let (expr_ty, expr) = if let Some((expr_ty, hir_id)) = expr_info {
3727 let expr_ty = tcx.short_string(expr_ty, err.long_ty_path());
3728 let expr = tcx.hir_expect_expr(hir_id);
3729 (expr_ty, expr)
3730 } else if let Some(body_id) = tcx.hir_node_by_def_id(body_id).body_id()
3731 && let body = tcx.hir_body(body_id)
3732 && let hir::ExprKind::Block(block, _) = body.value.kind
3733 && let Some(expr) = block.expr
3734 && let Some(expr_ty) = self
3735 .typeck_results
3736 .as_ref()
3737 .and_then(|typeck| typeck.node_type_opt(expr.hir_id))
3738 && let Some(pred) = predicate.as_clause()
3739 && let ty::ClauseKind::Trait(pred) = pred.kind().skip_binder()
3740 && self.can_eq(param_env, pred.self_ty(), expr_ty)
3741 {
3742 let expr_ty = tcx.short_string(expr_ty, err.long_ty_path());
3743 (expr_ty, expr)
3744 } else {
3745 return;
3746 };
3747 err.span_label(
3748 expr.span,
3749 with_forced_trimmed_paths!(format!(
3750 "return type was inferred to be `{expr_ty}` here",
3751 )),
3752 );
3753 suggest_remove_deref(err, &expr);
3754 }
3755 ObligationCauseCode::UnsizedNonPlaceExpr(span) => {
3756 err.span_note(
3757 span,
3758 "unsized values must be place expressions and cannot be put in temporaries",
3759 );
3760 }
3761 }
3762 }
3763
3764 #[instrument(
3765 level = "debug", skip(self, err), fields(trait_pred.self_ty = ?trait_pred.self_ty())
3766 )]
3767 pub(super) fn suggest_await_before_try(
3768 &self,
3769 err: &mut Diag<'_>,
3770 obligation: &PredicateObligation<'tcx>,
3771 trait_pred: ty::PolyTraitPredicate<'tcx>,
3772 span: Span,
3773 ) {
3774 let future_trait = self.tcx.require_lang_item(LangItem::Future, span);
3775
3776 let self_ty = self.resolve_vars_if_possible(trait_pred.self_ty());
3777 let impls_future = self.type_implements_trait(
3778 future_trait,
3779 [self.tcx.instantiate_bound_regions_with_erased(self_ty)],
3780 obligation.param_env,
3781 );
3782 if !impls_future.must_apply_modulo_regions() {
3783 return;
3784 }
3785
3786 let item_def_id = self.tcx.associated_item_def_ids(future_trait)[0];
3787 let projection_ty = trait_pred.map_bound(|trait_pred| {
3789 Ty::new_projection(
3790 self.tcx,
3791 item_def_id,
3792 [trait_pred.self_ty()],
3794 )
3795 });
3796 let InferOk { value: projection_ty, .. } =
3797 self.at(&obligation.cause, obligation.param_env).normalize(projection_ty);
3798
3799 debug!(
3800 normalized_projection_type = ?self.resolve_vars_if_possible(projection_ty)
3801 );
3802 let try_obligation = self.mk_trait_obligation_with_new_self_ty(
3803 obligation.param_env,
3804 trait_pred.map_bound(|trait_pred| (trait_pred, projection_ty.skip_binder())),
3805 );
3806 debug!(try_trait_obligation = ?try_obligation);
3807 if self.predicate_may_hold(&try_obligation)
3808 && let Ok(snippet) = self.tcx.sess.source_map().span_to_snippet(span)
3809 && snippet.ends_with('?')
3810 {
3811 match self.tcx.coroutine_kind(obligation.cause.body_id) {
3812 Some(hir::CoroutineKind::Desugared(hir::CoroutineDesugaring::Async, _)) => {
3813 err.span_suggestion_verbose(
3814 span.with_hi(span.hi() - BytePos(1)).shrink_to_hi(),
3815 "consider `await`ing on the `Future`",
3816 ".await",
3817 Applicability::MaybeIncorrect,
3818 );
3819 }
3820 _ => {
3821 let mut span: MultiSpan = span.with_lo(span.hi() - BytePos(1)).into();
3822 span.push_span_label(
3823 self.tcx.def_span(obligation.cause.body_id),
3824 "this is not `async`",
3825 );
3826 err.span_note(
3827 span,
3828 "this implements `Future` and its output type supports \
3829 `?`, but the future cannot be awaited in a synchronous function",
3830 );
3831 }
3832 }
3833 }
3834 }
3835
3836 pub(super) fn suggest_floating_point_literal(
3837 &self,
3838 obligation: &PredicateObligation<'tcx>,
3839 err: &mut Diag<'_>,
3840 trait_pred: ty::PolyTraitPredicate<'tcx>,
3841 ) {
3842 let rhs_span = match obligation.cause.code() {
3843 ObligationCauseCode::BinOp { rhs_span, rhs_is_lit, .. } if *rhs_is_lit => rhs_span,
3844 _ => return,
3845 };
3846 if let ty::Float(_) = trait_pred.skip_binder().self_ty().kind()
3847 && let ty::Infer(InferTy::IntVar(_)) =
3848 trait_pred.skip_binder().trait_ref.args.type_at(1).kind()
3849 {
3850 err.span_suggestion_verbose(
3851 rhs_span.shrink_to_hi(),
3852 "consider using a floating-point literal by writing it with `.0`",
3853 ".0",
3854 Applicability::MaybeIncorrect,
3855 );
3856 }
3857 }
3858
3859 pub fn can_suggest_derive(
3860 &self,
3861 obligation: &PredicateObligation<'tcx>,
3862 trait_pred: ty::PolyTraitPredicate<'tcx>,
3863 ) -> bool {
3864 if trait_pred.polarity() == ty::PredicatePolarity::Negative {
3865 return false;
3866 }
3867 let Some(diagnostic_name) = self.tcx.get_diagnostic_name(trait_pred.def_id()) else {
3868 return false;
3869 };
3870 let (adt, args) = match trait_pred.skip_binder().self_ty().kind() {
3871 ty::Adt(adt, args) if adt.did().is_local() => (adt, args),
3872 _ => return false,
3873 };
3874 let is_derivable_trait = match diagnostic_name {
3875 sym::Default => !adt.is_enum(),
3876 sym::PartialEq | sym::PartialOrd => {
3877 let rhs_ty = trait_pred.skip_binder().trait_ref.args.type_at(1);
3878 trait_pred.skip_binder().self_ty() == rhs_ty
3879 }
3880 sym::Eq | sym::Ord | sym::Clone | sym::Copy | sym::Hash | sym::Debug => true,
3881 _ => false,
3882 };
3883 is_derivable_trait &&
3884 adt.all_fields().all(|field| {
3886 let field_ty = ty::GenericArg::from(field.ty(self.tcx, args));
3887 let trait_args = match diagnostic_name {
3888 sym::PartialEq | sym::PartialOrd => {
3889 Some(field_ty)
3890 }
3891 _ => None,
3892 };
3893 let trait_pred = trait_pred.map_bound_ref(|tr| ty::TraitPredicate {
3894 trait_ref: ty::TraitRef::new(self.tcx,
3895 trait_pred.def_id(),
3896 [field_ty].into_iter().chain(trait_args),
3897 ),
3898 ..*tr
3899 });
3900 let field_obl = Obligation::new(
3901 self.tcx,
3902 obligation.cause.clone(),
3903 obligation.param_env,
3904 trait_pred,
3905 );
3906 self.predicate_must_hold_modulo_regions(&field_obl)
3907 })
3908 }
3909
3910 pub fn suggest_derive(
3911 &self,
3912 obligation: &PredicateObligation<'tcx>,
3913 err: &mut Diag<'_>,
3914 trait_pred: ty::PolyTraitPredicate<'tcx>,
3915 ) {
3916 let Some(diagnostic_name) = self.tcx.get_diagnostic_name(trait_pred.def_id()) else {
3917 return;
3918 };
3919 let adt = match trait_pred.skip_binder().self_ty().kind() {
3920 ty::Adt(adt, _) if adt.did().is_local() => adt,
3921 _ => return,
3922 };
3923 if self.can_suggest_derive(obligation, trait_pred) {
3924 err.span_suggestion_verbose(
3925 self.tcx.def_span(adt.did()).shrink_to_lo(),
3926 format!(
3927 "consider annotating `{}` with `#[derive({})]`",
3928 trait_pred.skip_binder().self_ty(),
3929 diagnostic_name,
3930 ),
3931 format!("#[derive({diagnostic_name})]\n"),
3933 Applicability::MaybeIncorrect,
3934 );
3935 }
3936 }
3937
3938 pub(super) fn suggest_dereferencing_index(
3939 &self,
3940 obligation: &PredicateObligation<'tcx>,
3941 err: &mut Diag<'_>,
3942 trait_pred: ty::PolyTraitPredicate<'tcx>,
3943 ) {
3944 if let ObligationCauseCode::ImplDerived(_) = obligation.cause.code()
3945 && self
3946 .tcx
3947 .is_diagnostic_item(sym::SliceIndex, trait_pred.skip_binder().trait_ref.def_id)
3948 && let ty::Slice(_) = trait_pred.skip_binder().trait_ref.args.type_at(1).kind()
3949 && let ty::Ref(_, inner_ty, _) = trait_pred.skip_binder().self_ty().kind()
3950 && let ty::Uint(ty::UintTy::Usize) = inner_ty.kind()
3951 {
3952 err.span_suggestion_verbose(
3953 obligation.cause.span.shrink_to_lo(),
3954 "dereference this index",
3955 '*',
3956 Applicability::MachineApplicable,
3957 );
3958 }
3959 }
3960
3961 fn note_function_argument_obligation<G: EmissionGuarantee>(
3962 &self,
3963 body_id: LocalDefId,
3964 err: &mut Diag<'_, G>,
3965 arg_hir_id: HirId,
3966 parent_code: &ObligationCauseCode<'tcx>,
3967 param_env: ty::ParamEnv<'tcx>,
3968 failed_pred: ty::Predicate<'tcx>,
3969 call_hir_id: HirId,
3970 ) {
3971 let tcx = self.tcx;
3972 if let Node::Expr(expr) = tcx.hir_node(arg_hir_id)
3973 && let Some(typeck_results) = &self.typeck_results
3974 {
3975 if let hir::Expr { kind: hir::ExprKind::MethodCall(_, rcvr, _, _), .. } = expr
3976 && let Some(ty) = typeck_results.node_type_opt(rcvr.hir_id)
3977 && let Some(failed_pred) = failed_pred.as_trait_clause()
3978 && let pred = failed_pred.map_bound(|pred| pred.with_replaced_self_ty(tcx, ty))
3979 && self.predicate_must_hold_modulo_regions(&Obligation::misc(
3980 tcx, expr.span, body_id, param_env, pred,
3981 ))
3982 && expr.span.hi() != rcvr.span.hi()
3983 {
3984 err.span_suggestion_verbose(
3985 expr.span.with_lo(rcvr.span.hi()),
3986 format!(
3987 "consider removing this method call, as the receiver has type `{ty}` and \
3988 `{pred}` trivially holds",
3989 ),
3990 "",
3991 Applicability::MaybeIncorrect,
3992 );
3993 }
3994 if let hir::Expr { kind: hir::ExprKind::Block(block, _), .. } = expr {
3995 let inner_expr = expr.peel_blocks();
3996 let ty = typeck_results
3997 .expr_ty_adjusted_opt(inner_expr)
3998 .unwrap_or(Ty::new_misc_error(tcx));
3999 let span = inner_expr.span;
4000 if Some(span) != err.span.primary_span()
4001 && !span.in_external_macro(tcx.sess.source_map())
4002 {
4003 err.span_label(
4004 span,
4005 if ty.references_error() {
4006 String::new()
4007 } else {
4008 let ty = with_forced_trimmed_paths!(self.ty_to_string(ty));
4009 format!("this tail expression is of type `{ty}`")
4010 },
4011 );
4012 if let ty::PredicateKind::Clause(clause) = failed_pred.kind().skip_binder()
4013 && let ty::ClauseKind::Trait(pred) = clause
4014 && tcx.fn_trait_kind_from_def_id(pred.def_id()).is_some()
4015 {
4016 if let [stmt, ..] = block.stmts
4017 && let hir::StmtKind::Semi(value) = stmt.kind
4018 && let hir::ExprKind::Closure(hir::Closure {
4019 body, fn_decl_span, ..
4020 }) = value.kind
4021 && let body = tcx.hir_body(*body)
4022 && !matches!(body.value.kind, hir::ExprKind::Block(..))
4023 {
4024 err.multipart_suggestion(
4027 "you might have meant to open the closure body instead of placing \
4028 a closure within a block",
4029 vec![
4030 (expr.span.with_hi(value.span.lo()), String::new()),
4031 (fn_decl_span.shrink_to_hi(), " {".to_string()),
4032 ],
4033 Applicability::MaybeIncorrect,
4034 );
4035 } else {
4036 err.span_suggestion_verbose(
4038 expr.span.shrink_to_lo(),
4039 "you might have meant to create the closure instead of a block",
4040 format!(
4041 "|{}| ",
4042 (0..pred.trait_ref.args.len() - 1)
4043 .map(|_| "_")
4044 .collect::<Vec<_>>()
4045 .join(", ")
4046 ),
4047 Applicability::MaybeIncorrect,
4048 );
4049 }
4050 }
4051 }
4052 }
4053
4054 let mut type_diffs = vec![];
4059 if let ObligationCauseCode::WhereClauseInExpr(def_id, _, _, idx) = parent_code
4060 && let Some(node_args) = typeck_results.node_args_opt(call_hir_id)
4061 && let where_clauses =
4062 self.tcx.predicates_of(def_id).instantiate(self.tcx, node_args)
4063 && let Some(where_pred) = where_clauses.predicates.get(*idx)
4064 {
4065 if let Some(where_pred) = where_pred.as_trait_clause()
4066 && let Some(failed_pred) = failed_pred.as_trait_clause()
4067 && where_pred.def_id() == failed_pred.def_id()
4068 {
4069 self.enter_forall(where_pred, |where_pred| {
4070 let failed_pred = self.instantiate_binder_with_fresh_vars(
4071 expr.span,
4072 BoundRegionConversionTime::FnCall,
4073 failed_pred,
4074 );
4075
4076 let zipped =
4077 iter::zip(where_pred.trait_ref.args, failed_pred.trait_ref.args);
4078 for (expected, actual) in zipped {
4079 self.probe(|_| {
4080 match self
4081 .at(&ObligationCause::misc(expr.span, body_id), param_env)
4082 .eq(DefineOpaqueTypes::Yes, expected, actual)
4085 {
4086 Ok(_) => (), Err(err) => type_diffs.push(err),
4088 }
4089 })
4090 }
4091 })
4092 } else if let Some(where_pred) = where_pred.as_projection_clause()
4093 && let Some(failed_pred) = failed_pred.as_projection_clause()
4094 && let Some(found) = failed_pred.skip_binder().term.as_type()
4095 {
4096 type_diffs = vec![TypeError::Sorts(ty::error::ExpectedFound {
4097 expected: where_pred
4098 .skip_binder()
4099 .projection_term
4100 .expect_ty(self.tcx)
4101 .to_ty(self.tcx),
4102 found,
4103 })];
4104 }
4105 }
4106 if let hir::ExprKind::Path(hir::QPath::Resolved(None, path)) = expr.kind
4107 && let hir::Path { res: Res::Local(hir_id), .. } = path
4108 && let hir::Node::Pat(binding) = self.tcx.hir_node(*hir_id)
4109 && let hir::Node::LetStmt(local) = self.tcx.parent_hir_node(binding.hir_id)
4110 && let Some(binding_expr) = local.init
4111 {
4112 self.point_at_chain(binding_expr, typeck_results, type_diffs, param_env, err);
4116 } else {
4117 self.point_at_chain(expr, typeck_results, type_diffs, param_env, err);
4118 }
4119 }
4120 let call_node = tcx.hir_node(call_hir_id);
4121 if let Node::Expr(hir::Expr { kind: hir::ExprKind::MethodCall(path, rcvr, ..), .. }) =
4122 call_node
4123 {
4124 if Some(rcvr.span) == err.span.primary_span() {
4125 err.replace_span_with(path.ident.span, true);
4126 }
4127 }
4128
4129 if let Node::Expr(expr) = call_node {
4130 if let hir::ExprKind::Call(hir::Expr { span, .. }, _)
4131 | hir::ExprKind::MethodCall(
4132 hir::PathSegment { ident: Ident { span, .. }, .. },
4133 ..,
4134 ) = expr.kind
4135 {
4136 if Some(*span) != err.span.primary_span() {
4137 let msg = if span.is_desugaring(DesugaringKind::FormatLiteral { source: true })
4138 {
4139 "required by this formatting parameter"
4140 } else if span.is_desugaring(DesugaringKind::FormatLiteral { source: false }) {
4141 "required by a formatting parameter in this expression"
4142 } else {
4143 "required by a bound introduced by this call"
4144 };
4145 err.span_label(*span, msg);
4146 }
4147 }
4148
4149 if let hir::ExprKind::MethodCall(_, expr, ..) = expr.kind {
4150 self.suggest_option_method_if_applicable(failed_pred, param_env, err, expr);
4151 }
4152 }
4153 }
4154
4155 fn suggest_option_method_if_applicable<G: EmissionGuarantee>(
4156 &self,
4157 failed_pred: ty::Predicate<'tcx>,
4158 param_env: ty::ParamEnv<'tcx>,
4159 err: &mut Diag<'_, G>,
4160 expr: &hir::Expr<'_>,
4161 ) {
4162 let tcx = self.tcx;
4163 let infcx = self.infcx;
4164 let Some(typeck_results) = self.typeck_results.as_ref() else { return };
4165
4166 let Some(option_ty_adt) = typeck_results.expr_ty_adjusted(expr).ty_adt_def() else {
4168 return;
4169 };
4170 if !tcx.is_diagnostic_item(sym::Option, option_ty_adt.did()) {
4171 return;
4172 }
4173
4174 if let ty::PredicateKind::Clause(ty::ClauseKind::Trait(ty::TraitPredicate { trait_ref, .. }))
4177 = failed_pred.kind().skip_binder()
4178 && tcx.is_fn_trait(trait_ref.def_id)
4179 && let [self_ty, found_ty] = trait_ref.args.as_slice()
4180 && let Some(fn_ty) = self_ty.as_type().filter(|ty| ty.is_fn())
4181 && let fn_sig @ ty::FnSig {
4182 abi: ExternAbi::Rust,
4183 c_variadic: false,
4184 safety: hir::Safety::Safe,
4185 ..
4186 } = fn_ty.fn_sig(tcx).skip_binder()
4187
4188 && let Some(&ty::Ref(_, target_ty, needs_mut)) = fn_sig.inputs().first().map(|t| t.kind())
4190 && !target_ty.has_escaping_bound_vars()
4191
4192 && let Some(ty::Tuple(tys)) = found_ty.as_type().map(Ty::kind)
4194 && let &[found_ty] = tys.as_slice()
4195 && !found_ty.has_escaping_bound_vars()
4196
4197 && let Some(deref_target_did) = tcx.lang_items().deref_target()
4199 && let projection = Ty::new_projection_from_args(tcx,deref_target_did, tcx.mk_args(&[ty::GenericArg::from(found_ty)]))
4200 && let InferOk { value: deref_target, obligations } = infcx.at(&ObligationCause::dummy(), param_env).normalize(projection)
4201 && obligations.iter().all(|obligation| infcx.predicate_must_hold_modulo_regions(obligation))
4202 && infcx.can_eq(param_env, deref_target, target_ty)
4203 {
4204 let help = if let hir::Mutability::Mut = needs_mut
4205 && let Some(deref_mut_did) = tcx.lang_items().deref_mut_trait()
4206 && infcx
4207 .type_implements_trait(deref_mut_did, iter::once(found_ty), param_env)
4208 .must_apply_modulo_regions()
4209 {
4210 Some(("call `Option::as_deref_mut()` first", ".as_deref_mut()"))
4211 } else if let hir::Mutability::Not = needs_mut {
4212 Some(("call `Option::as_deref()` first", ".as_deref()"))
4213 } else {
4214 None
4215 };
4216
4217 if let Some((msg, sugg)) = help {
4218 err.span_suggestion_with_style(
4219 expr.span.shrink_to_hi(),
4220 msg,
4221 sugg,
4222 Applicability::MaybeIncorrect,
4223 SuggestionStyle::ShowAlways,
4224 );
4225 }
4226 }
4227 }
4228
4229 fn look_for_iterator_item_mistakes<G: EmissionGuarantee>(
4230 &self,
4231 assocs_in_this_method: &[Option<(Span, (DefId, Ty<'tcx>))>],
4232 typeck_results: &TypeckResults<'tcx>,
4233 type_diffs: &[TypeError<'tcx>],
4234 param_env: ty::ParamEnv<'tcx>,
4235 path_segment: &hir::PathSegment<'_>,
4236 args: &[hir::Expr<'_>],
4237 err: &mut Diag<'_, G>,
4238 ) {
4239 let tcx = self.tcx;
4240 for entry in assocs_in_this_method {
4243 let Some((_span, (def_id, ty))) = entry else {
4244 continue;
4245 };
4246 for diff in type_diffs {
4247 let TypeError::Sorts(expected_found) = diff else {
4248 continue;
4249 };
4250 if tcx.is_diagnostic_item(sym::IteratorItem, *def_id)
4251 && path_segment.ident.name == sym::map
4252 && self.can_eq(param_env, expected_found.found, *ty)
4253 && let [arg] = args
4254 && let hir::ExprKind::Closure(closure) = arg.kind
4255 {
4256 let body = tcx.hir_body(closure.body);
4257 if let hir::ExprKind::Block(block, None) = body.value.kind
4258 && let None = block.expr
4259 && let [.., stmt] = block.stmts
4260 && let hir::StmtKind::Semi(expr) = stmt.kind
4261 && expected_found.found.is_unit()
4265 && expr.span.hi() != stmt.span.hi()
4270 {
4271 err.span_suggestion_verbose(
4272 expr.span.shrink_to_hi().with_hi(stmt.span.hi()),
4273 "consider removing this semicolon",
4274 String::new(),
4275 Applicability::MachineApplicable,
4276 );
4277 }
4278 let expr = if let hir::ExprKind::Block(block, None) = body.value.kind
4279 && let Some(expr) = block.expr
4280 {
4281 expr
4282 } else {
4283 body.value
4284 };
4285 if let hir::ExprKind::MethodCall(path_segment, rcvr, [], span) = expr.kind
4286 && path_segment.ident.name == sym::clone
4287 && let Some(expr_ty) = typeck_results.expr_ty_opt(expr)
4288 && let Some(rcvr_ty) = typeck_results.expr_ty_opt(rcvr)
4289 && self.can_eq(param_env, expr_ty, rcvr_ty)
4290 && let ty::Ref(_, ty, _) = expr_ty.kind()
4291 {
4292 err.span_label(
4293 span,
4294 format!(
4295 "this method call is cloning the reference `{expr_ty}`, not \
4296 `{ty}` which doesn't implement `Clone`",
4297 ),
4298 );
4299 let ty::Param(..) = ty.kind() else {
4300 continue;
4301 };
4302 let node =
4303 tcx.hir_node_by_def_id(tcx.hir_get_parent_item(expr.hir_id).def_id);
4304
4305 let pred = ty::Binder::dummy(ty::TraitPredicate {
4306 trait_ref: ty::TraitRef::new(
4307 tcx,
4308 tcx.require_lang_item(LangItem::Clone, span),
4309 [*ty],
4310 ),
4311 polarity: ty::PredicatePolarity::Positive,
4312 });
4313 let Some(generics) = node.generics() else {
4314 continue;
4315 };
4316 let Some(body_id) = node.body_id() else {
4317 continue;
4318 };
4319 suggest_restriction(
4320 tcx,
4321 tcx.hir_body_owner_def_id(body_id),
4322 generics,
4323 &format!("type parameter `{ty}`"),
4324 err,
4325 node.fn_sig(),
4326 None,
4327 pred,
4328 None,
4329 );
4330 }
4331 }
4332 }
4333 }
4334 }
4335
4336 fn point_at_chain<G: EmissionGuarantee>(
4337 &self,
4338 expr: &hir::Expr<'_>,
4339 typeck_results: &TypeckResults<'tcx>,
4340 type_diffs: Vec<TypeError<'tcx>>,
4341 param_env: ty::ParamEnv<'tcx>,
4342 err: &mut Diag<'_, G>,
4343 ) {
4344 let mut primary_spans = vec![];
4345 let mut span_labels = vec![];
4346
4347 let tcx = self.tcx;
4348
4349 let mut print_root_expr = true;
4350 let mut assocs = vec![];
4351 let mut expr = expr;
4352 let mut prev_ty = self.resolve_vars_if_possible(
4353 typeck_results.expr_ty_adjusted_opt(expr).unwrap_or(Ty::new_misc_error(tcx)),
4354 );
4355 while let hir::ExprKind::MethodCall(path_segment, rcvr_expr, args, span) = expr.kind {
4356 expr = rcvr_expr;
4360 let assocs_in_this_method =
4361 self.probe_assoc_types_at_expr(&type_diffs, span, prev_ty, expr.hir_id, param_env);
4362 self.look_for_iterator_item_mistakes(
4363 &assocs_in_this_method,
4364 typeck_results,
4365 &type_diffs,
4366 param_env,
4367 path_segment,
4368 args,
4369 err,
4370 );
4371 assocs.push(assocs_in_this_method);
4372 prev_ty = self.resolve_vars_if_possible(
4373 typeck_results.expr_ty_adjusted_opt(expr).unwrap_or(Ty::new_misc_error(tcx)),
4374 );
4375
4376 if let hir::ExprKind::Path(hir::QPath::Resolved(None, path)) = expr.kind
4377 && let hir::Path { res: Res::Local(hir_id), .. } = path
4378 && let hir::Node::Pat(binding) = self.tcx.hir_node(*hir_id)
4379 {
4380 let parent = self.tcx.parent_hir_node(binding.hir_id);
4381 if let hir::Node::LetStmt(local) = parent
4383 && let Some(binding_expr) = local.init
4384 {
4385 expr = binding_expr;
4387 }
4388 if let hir::Node::Param(param) = parent {
4389 let prev_ty = self.resolve_vars_if_possible(
4391 typeck_results
4392 .node_type_opt(param.hir_id)
4393 .unwrap_or(Ty::new_misc_error(tcx)),
4394 );
4395 let assocs_in_this_method = self.probe_assoc_types_at_expr(
4396 &type_diffs,
4397 param.ty_span,
4398 prev_ty,
4399 param.hir_id,
4400 param_env,
4401 );
4402 if assocs_in_this_method.iter().any(|a| a.is_some()) {
4403 assocs.push(assocs_in_this_method);
4404 print_root_expr = false;
4405 }
4406 break;
4407 }
4408 }
4409 }
4410 if let Some(ty) = typeck_results.expr_ty_opt(expr)
4413 && print_root_expr
4414 {
4415 let ty = with_forced_trimmed_paths!(self.ty_to_string(ty));
4416 span_labels.push((expr.span, format!("this expression has type `{ty}`")));
4420 };
4421 let mut assocs = assocs.into_iter().peekable();
4424 while let Some(assocs_in_method) = assocs.next() {
4425 let Some(prev_assoc_in_method) = assocs.peek() else {
4426 for entry in assocs_in_method {
4427 let Some((span, (assoc, ty))) = entry else {
4428 continue;
4429 };
4430 if primary_spans.is_empty()
4431 || type_diffs.iter().any(|diff| {
4432 let TypeError::Sorts(expected_found) = diff else {
4433 return false;
4434 };
4435 self.can_eq(param_env, expected_found.found, ty)
4436 })
4437 {
4438 primary_spans.push(span);
4444 }
4445 span_labels.push((
4446 span,
4447 with_forced_trimmed_paths!(format!(
4448 "`{}` is `{ty}` here",
4449 self.tcx.def_path_str(assoc),
4450 )),
4451 ));
4452 }
4453 break;
4454 };
4455 for (entry, prev_entry) in
4456 assocs_in_method.into_iter().zip(prev_assoc_in_method.into_iter())
4457 {
4458 match (entry, prev_entry) {
4459 (Some((span, (assoc, ty))), Some((_, (_, prev_ty)))) => {
4460 let ty_str = with_forced_trimmed_paths!(self.ty_to_string(ty));
4461
4462 let assoc = with_forced_trimmed_paths!(self.tcx.def_path_str(assoc));
4463 if !self.can_eq(param_env, ty, *prev_ty) {
4464 if type_diffs.iter().any(|diff| {
4465 let TypeError::Sorts(expected_found) = diff else {
4466 return false;
4467 };
4468 self.can_eq(param_env, expected_found.found, ty)
4469 }) {
4470 primary_spans.push(span);
4471 }
4472 span_labels
4473 .push((span, format!("`{assoc}` changed to `{ty_str}` here")));
4474 } else {
4475 span_labels.push((span, format!("`{assoc}` remains `{ty_str}` here")));
4476 }
4477 }
4478 (Some((span, (assoc, ty))), None) => {
4479 span_labels.push((
4480 span,
4481 with_forced_trimmed_paths!(format!(
4482 "`{}` is `{}` here",
4483 self.tcx.def_path_str(assoc),
4484 self.ty_to_string(ty),
4485 )),
4486 ));
4487 }
4488 (None, Some(_)) | (None, None) => {}
4489 }
4490 }
4491 }
4492 if !primary_spans.is_empty() {
4493 let mut multi_span: MultiSpan = primary_spans.into();
4494 for (span, label) in span_labels {
4495 multi_span.push_span_label(span, label);
4496 }
4497 err.span_note(
4498 multi_span,
4499 "the method call chain might not have had the expected associated types",
4500 );
4501 }
4502 }
4503
4504 fn probe_assoc_types_at_expr(
4505 &self,
4506 type_diffs: &[TypeError<'tcx>],
4507 span: Span,
4508 prev_ty: Ty<'tcx>,
4509 body_id: HirId,
4510 param_env: ty::ParamEnv<'tcx>,
4511 ) -> Vec<Option<(Span, (DefId, Ty<'tcx>))>> {
4512 let ocx = ObligationCtxt::new(self.infcx);
4513 let mut assocs_in_this_method = Vec::with_capacity(type_diffs.len());
4514 for diff in type_diffs {
4515 let TypeError::Sorts(expected_found) = diff else {
4516 continue;
4517 };
4518 let ty::Alias(ty::Projection, proj) = expected_found.expected.kind() else {
4519 continue;
4520 };
4521
4522 let args = GenericArgs::for_item(self.tcx, proj.def_id, |param, _| {
4526 if param.index == 0 {
4527 debug_assert_matches!(param.kind, ty::GenericParamDefKind::Type { .. });
4528 return prev_ty.into();
4529 }
4530 self.var_for_def(span, param)
4531 });
4532 let ty = self.infcx.next_ty_var(span);
4536 let projection = ty::Binder::dummy(ty::PredicateKind::Clause(
4538 ty::ClauseKind::Projection(ty::ProjectionPredicate {
4539 projection_term: ty::AliasTerm::new_from_args(self.tcx, proj.def_id, args),
4540 term: ty.into(),
4541 }),
4542 ));
4543 let body_def_id = self.tcx.hir_enclosing_body_owner(body_id);
4544 ocx.register_obligation(Obligation::misc(
4546 self.tcx,
4547 span,
4548 body_def_id,
4549 param_env,
4550 projection,
4551 ));
4552 if ocx.select_where_possible().is_empty()
4553 && let ty = self.resolve_vars_if_possible(ty)
4554 && !ty.is_ty_var()
4555 {
4556 assocs_in_this_method.push(Some((span, (proj.def_id, ty))));
4557 } else {
4558 assocs_in_this_method.push(None);
4563 }
4564 }
4565 assocs_in_this_method
4566 }
4567
4568 pub(super) fn suggest_convert_to_slice(
4572 &self,
4573 err: &mut Diag<'_>,
4574 obligation: &PredicateObligation<'tcx>,
4575 trait_pred: ty::PolyTraitPredicate<'tcx>,
4576 candidate_impls: &[ImplCandidate<'tcx>],
4577 span: Span,
4578 ) {
4579 let (ObligationCauseCode::BinOp { .. } | ObligationCauseCode::FunctionArg { .. }) =
4582 obligation.cause.code()
4583 else {
4584 return;
4585 };
4586
4587 let (element_ty, mut mutability) = match *trait_pred.skip_binder().self_ty().kind() {
4592 ty::Array(element_ty, _) => (element_ty, None),
4593
4594 ty::Ref(_, pointee_ty, mutability) => match *pointee_ty.kind() {
4595 ty::Array(element_ty, _) => (element_ty, Some(mutability)),
4596 _ => return,
4597 },
4598
4599 _ => return,
4600 };
4601
4602 let mut is_slice = |candidate: Ty<'tcx>| match *candidate.kind() {
4605 ty::RawPtr(t, m) | ty::Ref(_, t, m) => {
4606 if matches!(*t.kind(), ty::Slice(e) if e == element_ty)
4607 && m == mutability.unwrap_or(m)
4608 {
4609 mutability = Some(m);
4611 true
4612 } else {
4613 false
4614 }
4615 }
4616 _ => false,
4617 };
4618
4619 if let Some(slice_ty) = candidate_impls
4621 .iter()
4622 .map(|trait_ref| trait_ref.trait_ref.self_ty())
4623 .find(|t| is_slice(*t))
4624 {
4625 let msg = format!("convert the array to a `{slice_ty}` slice instead");
4626
4627 if let Ok(snippet) = self.tcx.sess.source_map().span_to_snippet(span) {
4628 let mut suggestions = vec![];
4629 if snippet.starts_with('&') {
4630 } else if let Some(hir::Mutability::Mut) = mutability {
4631 suggestions.push((span.shrink_to_lo(), "&mut ".into()));
4632 } else {
4633 suggestions.push((span.shrink_to_lo(), "&".into()));
4634 }
4635 suggestions.push((span.shrink_to_hi(), "[..]".into()));
4636 err.multipart_suggestion_verbose(msg, suggestions, Applicability::MaybeIncorrect);
4637 } else {
4638 err.span_help(span, msg);
4639 }
4640 }
4641 }
4642
4643 pub(super) fn suggest_tuple_wrapping(
4648 &self,
4649 err: &mut Diag<'_>,
4650 root_obligation: &PredicateObligation<'tcx>,
4651 obligation: &PredicateObligation<'tcx>,
4652 ) {
4653 let ObligationCauseCode::FunctionArg { arg_hir_id, .. } = obligation.cause.code() else {
4654 return;
4655 };
4656
4657 let Some(root_pred) = root_obligation.predicate.as_trait_clause() else { return };
4658
4659 let trait_ref = root_pred.map_bound(|root_pred| {
4660 root_pred.trait_ref.with_replaced_self_ty(
4661 self.tcx,
4662 Ty::new_tup(self.tcx, &[root_pred.trait_ref.self_ty()]),
4663 )
4664 });
4665
4666 let obligation =
4667 Obligation::new(self.tcx, obligation.cause.clone(), obligation.param_env, trait_ref);
4668
4669 if self.predicate_must_hold_modulo_regions(&obligation) {
4670 let arg_span = self.tcx.hir_span(*arg_hir_id);
4671 err.multipart_suggestion_verbose(
4672 format!("use a unary tuple instead"),
4673 vec![(arg_span.shrink_to_lo(), "(".into()), (arg_span.shrink_to_hi(), ",)".into())],
4674 Applicability::MaybeIncorrect,
4675 );
4676 }
4677 }
4678
4679 pub(super) fn explain_hrtb_projection(
4680 &self,
4681 diag: &mut Diag<'_>,
4682 pred: ty::PolyTraitPredicate<'tcx>,
4683 param_env: ty::ParamEnv<'tcx>,
4684 cause: &ObligationCause<'tcx>,
4685 ) {
4686 if pred.skip_binder().has_escaping_bound_vars() && pred.skip_binder().has_non_region_infer()
4687 {
4688 self.probe(|_| {
4689 let ocx = ObligationCtxt::new(self);
4690 self.enter_forall(pred, |pred| {
4691 let pred = ocx.normalize(&ObligationCause::dummy(), param_env, pred);
4692 ocx.register_obligation(Obligation::new(
4693 self.tcx,
4694 ObligationCause::dummy(),
4695 param_env,
4696 pred,
4697 ));
4698 });
4699 if !ocx.select_where_possible().is_empty() {
4700 return;
4702 }
4703
4704 if let ObligationCauseCode::FunctionArg {
4705 call_hir_id,
4706 arg_hir_id,
4707 parent_code: _,
4708 } = cause.code()
4709 {
4710 let arg_span = self.tcx.hir_span(*arg_hir_id);
4711 let mut sp: MultiSpan = arg_span.into();
4712
4713 sp.push_span_label(
4714 arg_span,
4715 "the trait solver is unable to infer the \
4716 generic types that should be inferred from this argument",
4717 );
4718 sp.push_span_label(
4719 self.tcx.hir_span(*call_hir_id),
4720 "add turbofish arguments to this call to \
4721 specify the types manually, even if it's redundant",
4722 );
4723 diag.span_note(
4724 sp,
4725 "this is a known limitation of the trait solver that \
4726 will be lifted in the future",
4727 );
4728 } else {
4729 let mut sp: MultiSpan = cause.span.into();
4730 sp.push_span_label(
4731 cause.span,
4732 "try adding turbofish arguments to this expression to \
4733 specify the types manually, even if it's redundant",
4734 );
4735 diag.span_note(
4736 sp,
4737 "this is a known limitation of the trait solver that \
4738 will be lifted in the future",
4739 );
4740 }
4741 });
4742 }
4743 }
4744
4745 pub(super) fn suggest_desugaring_async_fn_in_trait(
4746 &self,
4747 err: &mut Diag<'_>,
4748 trait_pred: ty::PolyTraitPredicate<'tcx>,
4749 ) {
4750 if self.tcx.features().return_type_notation() {
4752 return;
4753 }
4754
4755 let trait_def_id = trait_pred.def_id();
4756
4757 if !self.tcx.trait_is_auto(trait_def_id) {
4759 return;
4760 }
4761
4762 let ty::Alias(ty::Projection, alias_ty) = trait_pred.self_ty().skip_binder().kind() else {
4764 return;
4765 };
4766 let Some(ty::ImplTraitInTraitData::Trait { fn_def_id, opaque_def_id }) =
4767 self.tcx.opt_rpitit_info(alias_ty.def_id)
4768 else {
4769 return;
4770 };
4771
4772 let auto_trait = self.tcx.def_path_str(trait_def_id);
4773 let Some(fn_def_id) = fn_def_id.as_local() else {
4775 if self.tcx.asyncness(fn_def_id).is_async() {
4777 err.span_note(
4778 self.tcx.def_span(fn_def_id),
4779 format!(
4780 "`{}::{}` is an `async fn` in trait, which does not \
4781 automatically imply that its future is `{auto_trait}`",
4782 alias_ty.trait_ref(self.tcx),
4783 self.tcx.item_name(fn_def_id)
4784 ),
4785 );
4786 }
4787 return;
4788 };
4789 let hir::Node::TraitItem(item) = self.tcx.hir_node_by_def_id(fn_def_id) else {
4790 return;
4791 };
4792
4793 let (sig, body) = item.expect_fn();
4795 let hir::FnRetTy::Return(hir::Ty { kind: hir::TyKind::OpaqueDef(opaq_def, ..), .. }) =
4796 sig.decl.output
4797 else {
4798 return;
4800 };
4801
4802 if opaq_def.def_id.to_def_id() != opaque_def_id {
4805 return;
4806 }
4807
4808 let Some(sugg) = suggest_desugaring_async_fn_to_impl_future_in_trait(
4809 self.tcx,
4810 *sig,
4811 *body,
4812 opaque_def_id.expect_local(),
4813 &format!(" + {auto_trait}"),
4814 ) else {
4815 return;
4816 };
4817
4818 let function_name = self.tcx.def_path_str(fn_def_id);
4819 err.multipart_suggestion(
4820 format!(
4821 "`{auto_trait}` can be made part of the associated future's \
4822 guarantees for all implementations of `{function_name}`"
4823 ),
4824 sugg,
4825 Applicability::MachineApplicable,
4826 );
4827 }
4828
4829 pub fn ty_kind_suggestion(
4830 &self,
4831 param_env: ty::ParamEnv<'tcx>,
4832 ty: Ty<'tcx>,
4833 ) -> Option<String> {
4834 let tcx = self.infcx.tcx;
4835 let implements_default = |ty| {
4836 let Some(default_trait) = tcx.get_diagnostic_item(sym::Default) else {
4837 return false;
4838 };
4839 self.type_implements_trait(default_trait, [ty], param_env).must_apply_modulo_regions()
4840 };
4841
4842 Some(match *ty.kind() {
4843 ty::Never | ty::Error(_) => return None,
4844 ty::Bool => "false".to_string(),
4845 ty::Char => "\'x\'".to_string(),
4846 ty::Int(_) | ty::Uint(_) => "42".into(),
4847 ty::Float(_) => "3.14159".into(),
4848 ty::Slice(_) => "[]".to_string(),
4849 ty::Adt(def, _) if Some(def.did()) == tcx.get_diagnostic_item(sym::Vec) => {
4850 "vec![]".to_string()
4851 }
4852 ty::Adt(def, _) if Some(def.did()) == tcx.get_diagnostic_item(sym::String) => {
4853 "String::new()".to_string()
4854 }
4855 ty::Adt(def, args) if def.is_box() => {
4856 format!("Box::new({})", self.ty_kind_suggestion(param_env, args[0].expect_ty())?)
4857 }
4858 ty::Adt(def, _) if Some(def.did()) == tcx.get_diagnostic_item(sym::Option) => {
4859 "None".to_string()
4860 }
4861 ty::Adt(def, args) if Some(def.did()) == tcx.get_diagnostic_item(sym::Result) => {
4862 format!("Ok({})", self.ty_kind_suggestion(param_env, args[0].expect_ty())?)
4863 }
4864 ty::Adt(_, _) if implements_default(ty) => "Default::default()".to_string(),
4865 ty::Ref(_, ty, mutability) => {
4866 if let (ty::Str, hir::Mutability::Not) = (ty.kind(), mutability) {
4867 "\"\"".to_string()
4868 } else {
4869 let ty = self.ty_kind_suggestion(param_env, ty)?;
4870 format!("&{}{ty}", mutability.prefix_str())
4871 }
4872 }
4873 ty::Array(ty, len) if let Some(len) = len.try_to_target_usize(tcx) => {
4874 if len == 0 {
4875 "[]".to_string()
4876 } else if self.type_is_copy_modulo_regions(param_env, ty) || len == 1 {
4877 format!("[{}; {}]", self.ty_kind_suggestion(param_env, ty)?, len)
4879 } else {
4880 "/* value */".to_string()
4881 }
4882 }
4883 ty::Tuple(tys) => format!(
4884 "({}{})",
4885 tys.iter()
4886 .map(|ty| self.ty_kind_suggestion(param_env, ty))
4887 .collect::<Option<Vec<String>>>()?
4888 .join(", "),
4889 if tys.len() == 1 { "," } else { "" }
4890 ),
4891 _ => "/* value */".to_string(),
4892 })
4893 }
4894
4895 pub(super) fn suggest_add_result_as_return_type(
4899 &self,
4900 obligation: &PredicateObligation<'tcx>,
4901 err: &mut Diag<'_>,
4902 trait_pred: ty::PolyTraitPredicate<'tcx>,
4903 ) {
4904 if ObligationCauseCode::QuestionMark != *obligation.cause.code().peel_derives() {
4905 return;
4906 }
4907
4908 fn choose_suggest_items<'tcx, 'hir>(
4915 tcx: TyCtxt<'tcx>,
4916 node: hir::Node<'hir>,
4917 ) -> Option<(&'hir hir::FnDecl<'hir>, hir::BodyId)> {
4918 match node {
4919 hir::Node::Item(item)
4920 if let hir::ItemKind::Fn { sig, body: body_id, .. } = item.kind =>
4921 {
4922 Some((sig.decl, body_id))
4923 }
4924 hir::Node::ImplItem(item)
4925 if let hir::ImplItemKind::Fn(sig, body_id) = item.kind =>
4926 {
4927 let parent = tcx.parent_hir_node(item.hir_id());
4928 if let hir::Node::Item(item) = parent
4929 && let hir::ItemKind::Impl(imp) = item.kind
4930 && imp.of_trait.is_none()
4931 {
4932 return Some((sig.decl, body_id));
4933 }
4934 None
4935 }
4936 _ => None,
4937 }
4938 }
4939
4940 let node = self.tcx.hir_node_by_def_id(obligation.cause.body_id);
4941 if let Some((fn_decl, body_id)) = choose_suggest_items(self.tcx, node)
4942 && let hir::FnRetTy::DefaultReturn(ret_span) = fn_decl.output
4943 && self.tcx.is_diagnostic_item(sym::FromResidual, trait_pred.def_id())
4944 && trait_pred.skip_binder().trait_ref.args.type_at(0).is_unit()
4945 && let ty::Adt(def, _) = trait_pred.skip_binder().trait_ref.args.type_at(1).kind()
4946 && self.tcx.is_diagnostic_item(sym::Result, def.did())
4947 {
4948 let mut sugg_spans =
4949 vec![(ret_span, " -> Result<(), Box<dyn std::error::Error>>".to_string())];
4950 let body = self.tcx.hir_body(body_id);
4951 if let hir::ExprKind::Block(b, _) = body.value.kind
4952 && b.expr.is_none()
4953 {
4954 let span = self.tcx.sess.source_map().end_point(b.span);
4956 sugg_spans.push((
4957 span.shrink_to_lo(),
4958 format!(
4959 "{}{}",
4960 " Ok(())\n",
4961 self.tcx.sess.source_map().indentation_before(span).unwrap_or_default(),
4962 ),
4963 ));
4964 }
4965 err.multipart_suggestion_verbose(
4966 format!("consider adding return type"),
4967 sugg_spans,
4968 Applicability::MaybeIncorrect,
4969 );
4970 }
4971 }
4972
4973 #[instrument(level = "debug", skip_all)]
4974 pub(super) fn suggest_unsized_bound_if_applicable(
4975 &self,
4976 err: &mut Diag<'_>,
4977 obligation: &PredicateObligation<'tcx>,
4978 ) {
4979 let ty::PredicateKind::Clause(ty::ClauseKind::Trait(pred)) =
4980 obligation.predicate.kind().skip_binder()
4981 else {
4982 return;
4983 };
4984 let (ObligationCauseCode::WhereClause(item_def_id, span)
4985 | ObligationCauseCode::WhereClauseInExpr(item_def_id, span, ..)) =
4986 *obligation.cause.code().peel_derives()
4987 else {
4988 return;
4989 };
4990 if span.is_dummy() {
4991 return;
4992 }
4993 debug!(?pred, ?item_def_id, ?span);
4994
4995 let (Some(node), true) = (
4996 self.tcx.hir_get_if_local(item_def_id),
4997 self.tcx.is_lang_item(pred.def_id(), LangItem::Sized),
4998 ) else {
4999 return;
5000 };
5001
5002 let Some(generics) = node.generics() else {
5003 return;
5004 };
5005 let sized_trait = self.tcx.lang_items().sized_trait();
5006 debug!(?generics.params);
5007 debug!(?generics.predicates);
5008 let Some(param) = generics.params.iter().find(|param| param.span == span) else {
5009 return;
5010 };
5011 let explicitly_sized = generics
5014 .bounds_for_param(param.def_id)
5015 .flat_map(|bp| bp.bounds)
5016 .any(|bound| bound.trait_ref().and_then(|tr| tr.trait_def_id()) == sized_trait);
5017 if explicitly_sized {
5018 return;
5019 }
5020 debug!(?param);
5021 match node {
5022 hir::Node::Item(
5023 item @ hir::Item {
5024 kind:
5026 hir::ItemKind::Enum(..) | hir::ItemKind::Struct(..) | hir::ItemKind::Union(..),
5027 ..
5028 },
5029 ) => {
5030 if self.suggest_indirection_for_unsized(err, item, param) {
5031 return;
5032 }
5033 }
5034 _ => {}
5035 };
5036
5037 let (span, separator, open_paren_sp) =
5039 if let Some((s, open_paren_sp)) = generics.bounds_span_for_suggestions(param.def_id) {
5040 (s, " +", open_paren_sp)
5041 } else {
5042 (param.name.ident().span.shrink_to_hi(), ":", None)
5043 };
5044
5045 let mut suggs = vec![];
5046 let suggestion = format!("{separator} ?Sized");
5047
5048 if let Some(open_paren_sp) = open_paren_sp {
5049 suggs.push((open_paren_sp, "(".to_string()));
5050 suggs.push((span, format!("){suggestion}")));
5051 } else {
5052 suggs.push((span, suggestion));
5053 }
5054
5055 err.multipart_suggestion_verbose(
5056 "consider relaxing the implicit `Sized` restriction",
5057 suggs,
5058 Applicability::MachineApplicable,
5059 );
5060 }
5061
5062 fn suggest_indirection_for_unsized(
5063 &self,
5064 err: &mut Diag<'_>,
5065 item: &hir::Item<'tcx>,
5066 param: &hir::GenericParam<'tcx>,
5067 ) -> bool {
5068 let mut visitor =
5072 FindTypeParam { param: param.name.ident().name, invalid_spans: vec![], nested: false };
5073 visitor.visit_item(item);
5074 if visitor.invalid_spans.is_empty() {
5075 return false;
5076 }
5077 let mut multispan: MultiSpan = param.span.into();
5078 multispan.push_span_label(
5079 param.span,
5080 format!("this could be changed to `{}: ?Sized`...", param.name.ident()),
5081 );
5082 for sp in visitor.invalid_spans {
5083 multispan.push_span_label(
5084 sp,
5085 format!("...if indirection were used here: `Box<{}>`", param.name.ident()),
5086 );
5087 }
5088 err.span_help(
5089 multispan,
5090 format!(
5091 "you could relax the implicit `Sized` bound on `{T}` if it were \
5092 used through indirection like `&{T}` or `Box<{T}>`",
5093 T = param.name.ident(),
5094 ),
5095 );
5096 true
5097 }
5098 pub(crate) fn suggest_swapping_lhs_and_rhs<T>(
5099 &self,
5100 err: &mut Diag<'_>,
5101 predicate: T,
5102 param_env: ty::ParamEnv<'tcx>,
5103 cause_code: &ObligationCauseCode<'tcx>,
5104 ) where
5105 T: Upcast<TyCtxt<'tcx>, ty::Predicate<'tcx>>,
5106 {
5107 let tcx = self.tcx;
5108 let predicate = predicate.upcast(tcx);
5109 match *cause_code {
5110 ObligationCauseCode::BinOp { lhs_hir_id, rhs_hir_id, rhs_span, .. }
5111 if let Some(typeck_results) = &self.typeck_results
5112 && let hir::Node::Expr(lhs) = tcx.hir_node(lhs_hir_id)
5113 && let hir::Node::Expr(rhs) = tcx.hir_node(rhs_hir_id)
5114 && let Some(lhs_ty) = typeck_results.expr_ty_opt(lhs)
5115 && let Some(rhs_ty) = typeck_results.expr_ty_opt(rhs) =>
5116 {
5117 if let Some(pred) = predicate.as_trait_clause()
5118 && tcx.is_lang_item(pred.def_id(), LangItem::PartialEq)
5119 && self
5120 .infcx
5121 .type_implements_trait(pred.def_id(), [rhs_ty, lhs_ty], param_env)
5122 .must_apply_modulo_regions()
5123 {
5124 let lhs_span = tcx.hir_span(lhs_hir_id);
5125 let sm = tcx.sess.source_map();
5126 if let Ok(rhs_snippet) = sm.span_to_snippet(rhs_span)
5127 && let Ok(lhs_snippet) = sm.span_to_snippet(lhs_span)
5128 {
5129 err.note(format!("`{rhs_ty}` implements `PartialEq<{lhs_ty}>`"));
5130 err.multipart_suggestion(
5131 "consider swapping the equality",
5132 vec![(lhs_span, rhs_snippet), (rhs_span, lhs_snippet)],
5133 Applicability::MaybeIncorrect,
5134 );
5135 }
5136 }
5137 }
5138 _ => {}
5139 }
5140 }
5141}
5142
5143fn hint_missing_borrow<'tcx>(
5145 infcx: &InferCtxt<'tcx>,
5146 param_env: ty::ParamEnv<'tcx>,
5147 span: Span,
5148 found: Ty<'tcx>,
5149 expected: Ty<'tcx>,
5150 found_node: Node<'_>,
5151 err: &mut Diag<'_>,
5152) {
5153 if matches!(found_node, Node::TraitItem(..)) {
5154 return;
5155 }
5156
5157 let found_args = match found.kind() {
5158 ty::FnPtr(sig_tys, _) => infcx.enter_forall(*sig_tys, |sig_tys| sig_tys.inputs().iter()),
5159 kind => {
5160 span_bug!(span, "found was converted to a FnPtr above but is now {:?}", kind)
5161 }
5162 };
5163 let expected_args = match expected.kind() {
5164 ty::FnPtr(sig_tys, _) => infcx.enter_forall(*sig_tys, |sig_tys| sig_tys.inputs().iter()),
5165 kind => {
5166 span_bug!(span, "expected was converted to a FnPtr above but is now {:?}", kind)
5167 }
5168 };
5169
5170 let Some(fn_decl) = found_node.fn_decl() else {
5172 return;
5173 };
5174
5175 let args = fn_decl.inputs.iter();
5176
5177 let mut to_borrow = Vec::new();
5178 let mut remove_borrow = Vec::new();
5179
5180 for ((found_arg, expected_arg), arg) in found_args.zip(expected_args).zip(args) {
5181 let (found_ty, found_refs) = get_deref_type_and_refs(*found_arg);
5182 let (expected_ty, expected_refs) = get_deref_type_and_refs(*expected_arg);
5183
5184 if infcx.can_eq(param_env, found_ty, expected_ty) {
5185 if found_refs.len() < expected_refs.len()
5187 && found_refs[..] == expected_refs[expected_refs.len() - found_refs.len()..]
5188 {
5189 to_borrow.push((
5190 arg.span.shrink_to_lo(),
5191 expected_refs[..expected_refs.len() - found_refs.len()]
5192 .iter()
5193 .map(|mutbl| format!("&{}", mutbl.prefix_str()))
5194 .collect::<Vec<_>>()
5195 .join(""),
5196 ));
5197 } else if found_refs.len() > expected_refs.len() {
5198 let mut span = arg.span.shrink_to_lo();
5199 let mut left = found_refs.len() - expected_refs.len();
5200 let mut ty = arg;
5201 while let hir::TyKind::Ref(_, mut_ty) = &ty.kind
5202 && left > 0
5203 {
5204 span = span.with_hi(mut_ty.ty.span.lo());
5205 ty = mut_ty.ty;
5206 left -= 1;
5207 }
5208 let sugg = if left == 0 {
5209 (span, String::new())
5210 } else {
5211 (arg.span, expected_arg.to_string())
5212 };
5213 remove_borrow.push(sugg);
5214 }
5215 }
5216 }
5217
5218 if !to_borrow.is_empty() {
5219 err.subdiagnostic(errors::AdjustSignatureBorrow::Borrow { to_borrow });
5220 }
5221
5222 if !remove_borrow.is_empty() {
5223 err.subdiagnostic(errors::AdjustSignatureBorrow::RemoveBorrow { remove_borrow });
5224 }
5225}
5226
5227#[derive(Debug)]
5230pub struct SelfVisitor<'v> {
5231 pub paths: Vec<&'v hir::Ty<'v>>,
5232 pub name: Option<Symbol>,
5233}
5234
5235impl<'v> Visitor<'v> for SelfVisitor<'v> {
5236 fn visit_ty(&mut self, ty: &'v hir::Ty<'v, AmbigArg>) {
5237 if let hir::TyKind::Path(path) = ty.kind
5238 && let hir::QPath::TypeRelative(inner_ty, segment) = path
5239 && (Some(segment.ident.name) == self.name || self.name.is_none())
5240 && let hir::TyKind::Path(inner_path) = inner_ty.kind
5241 && let hir::QPath::Resolved(None, inner_path) = inner_path
5242 && let Res::SelfTyAlias { .. } = inner_path.res
5243 {
5244 self.paths.push(ty.as_unambig_ty());
5245 }
5246 hir::intravisit::walk_ty(self, ty);
5247 }
5248}
5249
5250#[derive(Default)]
5253pub struct ReturnsVisitor<'v> {
5254 pub returns: Vec<&'v hir::Expr<'v>>,
5255 in_block_tail: bool,
5256}
5257
5258impl<'v> Visitor<'v> for ReturnsVisitor<'v> {
5259 fn visit_expr(&mut self, ex: &'v hir::Expr<'v>) {
5260 match ex.kind {
5265 hir::ExprKind::Ret(Some(ex)) => {
5266 self.returns.push(ex);
5267 }
5268 hir::ExprKind::Block(block, _) if self.in_block_tail => {
5269 self.in_block_tail = false;
5270 for stmt in block.stmts {
5271 hir::intravisit::walk_stmt(self, stmt);
5272 }
5273 self.in_block_tail = true;
5274 if let Some(expr) = block.expr {
5275 self.visit_expr(expr);
5276 }
5277 }
5278 hir::ExprKind::If(_, then, else_opt) if self.in_block_tail => {
5279 self.visit_expr(then);
5280 if let Some(el) = else_opt {
5281 self.visit_expr(el);
5282 }
5283 }
5284 hir::ExprKind::Match(_, arms, _) if self.in_block_tail => {
5285 for arm in arms {
5286 self.visit_expr(arm.body);
5287 }
5288 }
5289 _ if !self.in_block_tail => hir::intravisit::walk_expr(self, ex),
5291 _ => self.returns.push(ex),
5292 }
5293 }
5294
5295 fn visit_body(&mut self, body: &hir::Body<'v>) {
5296 assert!(!self.in_block_tail);
5297 self.in_block_tail = true;
5298 hir::intravisit::walk_body(self, body);
5299 }
5300}
5301
5302#[derive(Default)]
5304struct AwaitsVisitor {
5305 awaits: Vec<HirId>,
5306}
5307
5308impl<'v> Visitor<'v> for AwaitsVisitor {
5309 fn visit_expr(&mut self, ex: &'v hir::Expr<'v>) {
5310 if let hir::ExprKind::Yield(_, hir::YieldSource::Await { expr: Some(id) }) = ex.kind {
5311 self.awaits.push(id)
5312 }
5313 hir::intravisit::walk_expr(self, ex)
5314 }
5315}
5316
5317pub trait NextTypeParamName {
5321 fn next_type_param_name(&self, name: Option<&str>) -> String;
5322}
5323
5324impl NextTypeParamName for &[hir::GenericParam<'_>] {
5325 fn next_type_param_name(&self, name: Option<&str>) -> String {
5326 let name = name.and_then(|n| n.chars().next()).map(|c| c.to_uppercase().to_string());
5328 let name = name.as_deref();
5329
5330 let possible_names = [name.unwrap_or("T"), "T", "U", "V", "X", "Y", "Z", "A", "B", "C"];
5332
5333 let used_names: Vec<Symbol> = self
5335 .iter()
5336 .filter_map(|param| match param.name {
5337 hir::ParamName::Plain(ident) => Some(ident.name),
5338 _ => None,
5339 })
5340 .collect();
5341
5342 possible_names
5344 .iter()
5345 .find(|n| !used_names.contains(&Symbol::intern(n)))
5346 .unwrap_or(&"ParamName")
5347 .to_string()
5348 }
5349}
5350
5351struct ReplaceImplTraitVisitor<'a> {
5353 ty_spans: &'a mut Vec<Span>,
5354 param_did: DefId,
5355}
5356
5357impl<'a, 'hir> hir::intravisit::Visitor<'hir> for ReplaceImplTraitVisitor<'a> {
5358 fn visit_ty(&mut self, t: &'hir hir::Ty<'hir, AmbigArg>) {
5359 if let hir::TyKind::Path(hir::QPath::Resolved(
5360 None,
5361 hir::Path { res: Res::Def(_, segment_did), .. },
5362 )) = t.kind
5363 {
5364 if self.param_did == *segment_did {
5365 self.ty_spans.push(t.span);
5370 return;
5371 }
5372 }
5373
5374 hir::intravisit::walk_ty(self, t);
5375 }
5376}
5377
5378pub(super) fn get_explanation_based_on_obligation<'tcx>(
5379 tcx: TyCtxt<'tcx>,
5380 obligation: &PredicateObligation<'tcx>,
5381 trait_predicate: ty::PolyTraitPredicate<'tcx>,
5382 pre_message: String,
5383 long_ty_path: &mut Option<PathBuf>,
5384) -> String {
5385 if let ObligationCauseCode::MainFunctionType = obligation.cause.code() {
5386 "consider using `()`, or a `Result`".to_owned()
5387 } else {
5388 let ty_desc = match trait_predicate.self_ty().skip_binder().kind() {
5389 ty::FnDef(_, _) => Some("fn item"),
5390 ty::Closure(_, _) => Some("closure"),
5391 _ => None,
5392 };
5393
5394 let desc = match ty_desc {
5395 Some(desc) => format!(" {desc}"),
5396 None => String::new(),
5397 };
5398 if let ty::PredicatePolarity::Positive = trait_predicate.polarity() {
5399 format!(
5400 "{pre_message}the trait `{}` is not implemented for{desc} `{}`",
5401 trait_predicate.print_modifiers_and_trait_path(),
5402 tcx.short_string(trait_predicate.self_ty().skip_binder(), long_ty_path),
5403 )
5404 } else {
5405 format!("{pre_message}the trait bound `{trait_predicate}` is not satisfied")
5409 }
5410 }
5411}
5412
5413struct ReplaceImplTraitFolder<'tcx> {
5415 tcx: TyCtxt<'tcx>,
5416 param: &'tcx ty::GenericParamDef,
5417 replace_ty: Ty<'tcx>,
5418}
5419
5420impl<'tcx> TypeFolder<TyCtxt<'tcx>> for ReplaceImplTraitFolder<'tcx> {
5421 fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> {
5422 if let ty::Param(ty::ParamTy { index, .. }) = t.kind() {
5423 if self.param.index == *index {
5424 return self.replace_ty;
5425 }
5426 }
5427 t.super_fold_with(self)
5428 }
5429
5430 fn cx(&self) -> TyCtxt<'tcx> {
5431 self.tcx
5432 }
5433}
5434
5435pub fn suggest_desugaring_async_fn_to_impl_future_in_trait<'tcx>(
5436 tcx: TyCtxt<'tcx>,
5437 sig: hir::FnSig<'tcx>,
5438 body: hir::TraitFn<'tcx>,
5439 opaque_def_id: LocalDefId,
5440 add_bounds: &str,
5441) -> Option<Vec<(Span, String)>> {
5442 let hir::IsAsync::Async(async_span) = sig.header.asyncness else {
5443 return None;
5444 };
5445 let async_span = tcx.sess.source_map().span_extend_while_whitespace(async_span);
5446
5447 let future = tcx.hir_node_by_def_id(opaque_def_id).expect_opaque_ty();
5448 let [hir::GenericBound::Trait(trait_ref)] = future.bounds else {
5449 return None;
5451 };
5452 let Some(hir::PathSegment { args: Some(args), .. }) = trait_ref.trait_ref.path.segments.last()
5453 else {
5454 return None;
5456 };
5457 let Some(future_output_ty) = args.constraints.first().and_then(|constraint| constraint.ty())
5458 else {
5459 return None;
5461 };
5462
5463 let mut sugg = if future_output_ty.span.is_empty() {
5464 vec![
5465 (async_span, String::new()),
5466 (
5467 future_output_ty.span,
5468 format!(" -> impl std::future::Future<Output = ()>{add_bounds}"),
5469 ),
5470 ]
5471 } else {
5472 vec![
5473 (future_output_ty.span.shrink_to_lo(), "impl std::future::Future<Output = ".to_owned()),
5474 (future_output_ty.span.shrink_to_hi(), format!(">{add_bounds}")),
5475 (async_span, String::new()),
5476 ]
5477 };
5478
5479 if let hir::TraitFn::Provided(body) = body {
5481 let body = tcx.hir_body(body);
5482 let body_span = body.value.span;
5483 let body_span_without_braces =
5484 body_span.with_lo(body_span.lo() + BytePos(1)).with_hi(body_span.hi() - BytePos(1));
5485 if body_span_without_braces.is_empty() {
5486 sugg.push((body_span_without_braces, " async {} ".to_owned()));
5487 } else {
5488 sugg.extend([
5489 (body_span_without_braces.shrink_to_lo(), "async {".to_owned()),
5490 (body_span_without_braces.shrink_to_hi(), "} ".to_owned()),
5491 ]);
5492 }
5493 }
5494
5495 Some(sugg)
5496}
5497
5498fn point_at_assoc_type_restriction<G: EmissionGuarantee>(
5501 tcx: TyCtxt<'_>,
5502 err: &mut Diag<'_, G>,
5503 self_ty_str: &str,
5504 trait_name: &str,
5505 predicate: ty::Predicate<'_>,
5506 generics: &hir::Generics<'_>,
5507 data: &ImplDerivedCause<'_>,
5508) {
5509 let ty::PredicateKind::Clause(clause) = predicate.kind().skip_binder() else {
5510 return;
5511 };
5512 let ty::ClauseKind::Projection(proj) = clause else {
5513 return;
5514 };
5515 let name = tcx.item_name(proj.projection_term.def_id);
5516 let mut predicates = generics.predicates.iter().peekable();
5517 let mut prev: Option<(&hir::WhereBoundPredicate<'_>, Span)> = None;
5518 while let Some(pred) = predicates.next() {
5519 let curr_span = pred.span;
5520 let hir::WherePredicateKind::BoundPredicate(pred) = pred.kind else {
5521 continue;
5522 };
5523 let mut bounds = pred.bounds.iter();
5524 while let Some(bound) = bounds.next() {
5525 let Some(trait_ref) = bound.trait_ref() else {
5526 continue;
5527 };
5528 if bound.span() != data.span {
5529 continue;
5530 }
5531 if let hir::TyKind::Path(path) = pred.bounded_ty.kind
5532 && let hir::QPath::TypeRelative(ty, segment) = path
5533 && segment.ident.name == name
5534 && let hir::TyKind::Path(inner_path) = ty.kind
5535 && let hir::QPath::Resolved(None, inner_path) = inner_path
5536 && let Res::SelfTyAlias { .. } = inner_path.res
5537 {
5538 let span = if pred.origin == hir::PredicateOrigin::WhereClause
5541 && generics
5542 .predicates
5543 .iter()
5544 .filter(|p| {
5545 matches!(
5546 p.kind,
5547 hir::WherePredicateKind::BoundPredicate(p)
5548 if hir::PredicateOrigin::WhereClause == p.origin
5549 )
5550 })
5551 .count()
5552 == 1
5553 {
5554 generics.where_clause_span
5557 } else if let Some(next_pred) = predicates.peek()
5558 && let hir::WherePredicateKind::BoundPredicate(next) = next_pred.kind
5559 && pred.origin == next.origin
5560 {
5561 curr_span.until(next_pred.span)
5563 } else if let Some((prev, prev_span)) = prev
5564 && pred.origin == prev.origin
5565 {
5566 prev_span.shrink_to_hi().to(curr_span)
5568 } else if pred.origin == hir::PredicateOrigin::WhereClause {
5569 curr_span.with_hi(generics.where_clause_span.hi())
5570 } else {
5571 curr_span
5572 };
5573
5574 err.span_suggestion_verbose(
5575 span,
5576 "associated type for the current `impl` cannot be restricted in `where` \
5577 clauses, remove this bound",
5578 "",
5579 Applicability::MaybeIncorrect,
5580 );
5581 }
5582 if let Some(new) =
5583 tcx.associated_items(data.impl_or_alias_def_id).find_by_ident_and_kind(
5584 tcx,
5585 Ident::with_dummy_span(name),
5586 ty::AssocTag::Type,
5587 data.impl_or_alias_def_id,
5588 )
5589 {
5590 let span = tcx.def_span(new.def_id);
5593 err.span_label(
5594 span,
5595 format!(
5596 "associated type `<{self_ty_str} as {trait_name}>::{name}` is specified \
5597 here",
5598 ),
5599 );
5600 let mut visitor = SelfVisitor { paths: vec![], name: Some(name) };
5603 visitor.visit_trait_ref(trait_ref);
5604 for path in visitor.paths {
5605 err.span_suggestion_verbose(
5606 path.span,
5607 "replace the associated type with the type specified in this `impl`",
5608 tcx.type_of(new.def_id).skip_binder(),
5609 Applicability::MachineApplicable,
5610 );
5611 }
5612 } else {
5613 let mut visitor = SelfVisitor { paths: vec![], name: None };
5614 visitor.visit_trait_ref(trait_ref);
5615 let span: MultiSpan =
5616 visitor.paths.iter().map(|p| p.span).collect::<Vec<Span>>().into();
5617 err.span_note(
5618 span,
5619 "associated types for the current `impl` cannot be restricted in `where` \
5620 clauses",
5621 );
5622 }
5623 }
5624 prev = Some((pred, curr_span));
5625 }
5626}
5627
5628fn get_deref_type_and_refs(mut ty: Ty<'_>) -> (Ty<'_>, Vec<hir::Mutability>) {
5629 let mut refs = vec![];
5630
5631 while let ty::Ref(_, new_ty, mutbl) = ty.kind() {
5632 ty = *new_ty;
5633 refs.push(*mutbl);
5634 }
5635
5636 (ty, refs)
5637}
5638
5639struct FindTypeParam {
5642 param: rustc_span::Symbol,
5643 invalid_spans: Vec<Span>,
5644 nested: bool,
5645}
5646
5647impl<'v> Visitor<'v> for FindTypeParam {
5648 fn visit_where_predicate(&mut self, _: &'v hir::WherePredicate<'v>) {
5649 }
5651
5652 fn visit_ty(&mut self, ty: &hir::Ty<'_, AmbigArg>) {
5653 match ty.kind {
5660 hir::TyKind::Ptr(_) | hir::TyKind::Ref(..) | hir::TyKind::TraitObject(..) => {}
5661 hir::TyKind::Path(hir::QPath::Resolved(None, path))
5662 if let [segment] = path.segments
5663 && segment.ident.name == self.param =>
5664 {
5665 if !self.nested {
5666 debug!(?ty, "FindTypeParam::visit_ty");
5667 self.invalid_spans.push(ty.span);
5668 }
5669 }
5670 hir::TyKind::Path(_) => {
5671 let prev = self.nested;
5672 self.nested = true;
5673 hir::intravisit::walk_ty(self, ty);
5674 self.nested = prev;
5675 }
5676 _ => {
5677 hir::intravisit::walk_ty(self, ty);
5678 }
5679 }
5680 }
5681}