1 // ignore-tidy-filelength
2 use super::{DefIdOrName, Obligation, ObligationCause, ObligationCauseCode, PredicateObligation};
4 use crate::autoderef::Autoderef;
5 use crate::infer::InferCtxt;
6 use crate::traits::{NormalizeExt, ObligationCtxt};
10 use rustc_data_structures::fx::FxHashSet;
11 use rustc_data_structures::stack::ensure_sufficient_stack;
13 error_code, pluralize, struct_span_err, Applicability, Diagnostic, DiagnosticBuilder,
14 ErrorGuaranteed, MultiSpan, Style,
17 use rustc_hir::def::DefKind;
18 use rustc_hir::def_id::DefId;
19 use rustc_hir::intravisit::Visitor;
20 use rustc_hir::lang_items::LangItem;
21 use rustc_hir::{AsyncGeneratorKind, GeneratorKind, Node};
22 use rustc_infer::infer::error_reporting::TypeErrCtxt;
23 use rustc_infer::infer::type_variable::{TypeVariableOrigin, TypeVariableOriginKind};
24 use rustc_infer::infer::{InferOk, LateBoundRegionConversionTime};
25 use rustc_middle::hir::map;
26 use rustc_middle::ty::error::TypeError::{self, Sorts};
27 use rustc_middle::ty::relate::TypeRelation;
28 use rustc_middle::ty::{
29 self, suggest_arbitrary_trait_bound, suggest_constraining_type_param, AdtKind, DefIdTree,
30 GeneratorDiagnosticData, GeneratorInteriorTypeCause, Infer, InferTy, InternalSubsts,
31 IsSuggestable, ToPredicate, Ty, TyCtxt, TypeAndMut, TypeFoldable, TypeFolder,
32 TypeSuperFoldable, TypeVisitable, TypeckResults,
34 use rustc_span::symbol::{sym, Ident, Symbol};
35 use rustc_span::{BytePos, DesugaringKind, ExpnKind, Span, DUMMY_SP};
36 use rustc_target::spec::abi;
39 use super::method_chain::CollectAllMismatches;
40 use super::InferCtxtPrivExt;
41 use crate::infer::InferCtxtExt as _;
42 use crate::traits::query::evaluate_obligation::InferCtxtExt as _;
43 use rustc_middle::ty::print::{with_forced_trimmed_paths, with_no_trimmed_paths};
46 pub enum GeneratorInteriorOrUpvar {
47 // span of interior type
48 Interior(Span, Option<(Option<Span>, Span, Option<hir::HirId>, Option<Span>)>),
53 // This type provides a uniform interface to retrieve data on generators, whether it originated from
54 // the local crate being compiled or from a foreign crate.
56 pub enum GeneratorData<'tcx, 'a> {
57 Local(&'a TypeckResults<'tcx>),
58 Foreign(&'tcx GeneratorDiagnosticData<'tcx>),
61 impl<'tcx, 'a> GeneratorData<'tcx, 'a> {
62 // Try to get information about variables captured by the generator that matches a type we are
63 // looking for with `ty_matches` function. We uses it to find upvar which causes a failure to
65 fn try_get_upvar_span<F>(
67 infer_context: &InferCtxt<'tcx>,
70 ) -> Option<GeneratorInteriorOrUpvar>
72 F: Fn(ty::Binder<'tcx, Ty<'tcx>>) -> bool,
75 GeneratorData::Local(typeck_results) => {
76 infer_context.tcx.upvars_mentioned(generator_did).and_then(|upvars| {
77 upvars.iter().find_map(|(upvar_id, upvar)| {
78 let upvar_ty = typeck_results.node_type(*upvar_id);
79 let upvar_ty = infer_context.resolve_vars_if_possible(upvar_ty);
80 if ty_matches(ty::Binder::dummy(upvar_ty)) {
81 Some(GeneratorInteriorOrUpvar::Upvar(upvar.span))
88 GeneratorData::Foreign(_) => None,
92 // Try to get the span of a type being awaited on that matches the type we are looking with the
93 // `ty_matches` function. We uses it to find awaited type which causes a failure to meet an
95 fn get_from_await_ty<F>(
97 visitor: AwaitsVisitor,
102 F: Fn(ty::Binder<'tcx, Ty<'tcx>>) -> bool,
105 GeneratorData::Local(typeck_results) => visitor
108 .map(|id| hir.expect_expr(id))
110 ty_matches(ty::Binder::dummy(typeck_results.expr_ty_adjusted(&await_expr)))
112 .map(|expr| expr.span),
113 GeneratorData::Foreign(generator_diagnostic_data) => visitor
116 .map(|id| hir.expect_expr(id))
118 ty_matches(ty::Binder::dummy(
119 generator_diagnostic_data
121 .get(&await_expr.hir_id.local_id)
122 .map_or::<&[ty::adjustment::Adjustment<'tcx>], _>(&[], |a| &a[..])
124 .map_or_else::<Ty<'tcx>, _, _>(
126 generator_diagnostic_data
128 .get(&await_expr.hir_id.local_id)
132 "node_type: no type for node `{}`",
133 ty::tls::with(|tcx| tcx
135 .node_to_string(await_expr.hir_id))
143 .map(|expr| expr.span),
147 /// Get the type, expression, span and optional scope span of all types
148 /// that are live across the yield of this generator
149 fn get_generator_interior_types(
151 ) -> ty::Binder<'tcx, &[GeneratorInteriorTypeCause<'tcx>]> {
153 GeneratorData::Local(typeck_result) => {
154 typeck_result.generator_interior_types.as_deref()
156 GeneratorData::Foreign(generator_diagnostic_data) => {
157 generator_diagnostic_data.generator_interior_types.as_deref()
162 // Used to get the source of the data, note we don't have as much information for generators
163 // originated from foreign crates
164 fn is_foreign(&self) -> bool {
166 GeneratorData::Local(_) => false,
167 GeneratorData::Foreign(_) => true,
172 // This trait is public to expose the diagnostics methods to clippy.
173 pub trait TypeErrCtxtExt<'tcx> {
174 fn suggest_restricting_param_bound(
176 err: &mut Diagnostic,
177 trait_pred: ty::PolyTraitPredicate<'tcx>,
178 associated_item: Option<(&'static str, Ty<'tcx>)>,
182 fn suggest_dereferences(
184 obligation: &PredicateObligation<'tcx>,
185 err: &mut Diagnostic,
186 trait_pred: ty::PolyTraitPredicate<'tcx>,
189 fn get_closure_name(&self, def_id: DefId, err: &mut Diagnostic, msg: &str) -> Option<Symbol>;
193 obligation: &PredicateObligation<'tcx>,
194 err: &mut Diagnostic,
195 trait_pred: ty::PolyTraitPredicate<'tcx>,
198 fn suggest_add_reference_to_arg(
200 obligation: &PredicateObligation<'tcx>,
201 err: &mut Diagnostic,
202 trait_pred: ty::PolyTraitPredicate<'tcx>,
203 has_custom_message: bool,
206 fn suggest_borrowing_for_object_cast(
208 err: &mut Diagnostic,
209 obligation: &PredicateObligation<'tcx>,
214 fn suggest_remove_reference(
216 obligation: &PredicateObligation<'tcx>,
217 err: &mut Diagnostic,
218 trait_pred: ty::PolyTraitPredicate<'tcx>,
221 fn suggest_remove_await(&self, obligation: &PredicateObligation<'tcx>, err: &mut Diagnostic);
223 fn suggest_change_mut(
225 obligation: &PredicateObligation<'tcx>,
226 err: &mut Diagnostic,
227 trait_pred: ty::PolyTraitPredicate<'tcx>,
230 fn suggest_semicolon_removal(
232 obligation: &PredicateObligation<'tcx>,
233 err: &mut Diagnostic,
235 trait_pred: ty::PolyTraitPredicate<'tcx>,
238 fn return_type_span(&self, obligation: &PredicateObligation<'tcx>) -> Option<Span>;
240 fn suggest_impl_trait(
242 err: &mut Diagnostic,
244 obligation: &PredicateObligation<'tcx>,
245 trait_pred: ty::PolyTraitPredicate<'tcx>,
248 fn point_at_returns_when_relevant(
250 err: &mut Diagnostic,
251 obligation: &PredicateObligation<'tcx>,
254 fn report_closure_arg_mismatch(
257 found_span: Option<Span>,
258 found: ty::PolyTraitRef<'tcx>,
259 expected: ty::PolyTraitRef<'tcx>,
260 cause: &ObligationCauseCode<'tcx>,
261 found_node: Option<Node<'_>>,
262 ) -> DiagnosticBuilder<'tcx, ErrorGuaranteed>;
264 fn note_conflicting_closure_bounds(
266 cause: &ObligationCauseCode<'tcx>,
267 err: &mut DiagnosticBuilder<'tcx, ErrorGuaranteed>,
270 fn suggest_fully_qualified_path(
272 err: &mut Diagnostic,
278 fn maybe_note_obligation_cause_for_async_await(
280 err: &mut Diagnostic,
281 obligation: &PredicateObligation<'tcx>,
284 fn note_obligation_cause_for_async_await(
286 err: &mut Diagnostic,
287 interior_or_upvar_span: GeneratorInteriorOrUpvar,
289 outer_generator: Option<DefId>,
290 trait_pred: ty::TraitPredicate<'tcx>,
292 typeck_results: Option<&ty::TypeckResults<'tcx>>,
293 obligation: &PredicateObligation<'tcx>,
294 next_code: Option<&ObligationCauseCode<'tcx>>,
297 fn note_obligation_cause_code<T>(
299 err: &mut Diagnostic,
301 param_env: ty::ParamEnv<'tcx>,
302 cause_code: &ObligationCauseCode<'tcx>,
303 obligated_types: &mut Vec<Ty<'tcx>>,
304 seen_requirements: &mut FxHashSet<DefId>,
306 T: ToPredicate<'tcx>;
308 /// Suggest to await before try: future? => future.await?
309 fn suggest_await_before_try(
311 err: &mut Diagnostic,
312 obligation: &PredicateObligation<'tcx>,
313 trait_pred: ty::PolyTraitPredicate<'tcx>,
317 fn suggest_floating_point_literal(
319 obligation: &PredicateObligation<'tcx>,
320 err: &mut Diagnostic,
321 trait_ref: &ty::PolyTraitRef<'tcx>,
326 obligation: &PredicateObligation<'tcx>,
327 err: &mut Diagnostic,
328 trait_pred: ty::PolyTraitPredicate<'tcx>,
331 fn suggest_dereferencing_index(
333 obligation: &PredicateObligation<'tcx>,
334 err: &mut Diagnostic,
335 trait_pred: ty::PolyTraitPredicate<'tcx>,
337 fn function_argument_obligation(
340 err: &mut Diagnostic,
341 parent_code: &ObligationCauseCode<'tcx>,
342 param_env: ty::ParamEnv<'tcx>,
343 predicate: ty::Predicate<'tcx>,
348 expr: &hir::Expr<'_>,
349 typeck_results: &TypeckResults<'tcx>,
350 type_diffs: Vec<TypeError<'tcx>>,
351 param_env: ty::ParamEnv<'tcx>,
352 err: &mut Diagnostic,
356 fn predicate_constraint(generics: &hir::Generics<'_>, pred: ty::Predicate<'_>) -> (Span, String) {
358 generics.tail_span_for_predicate_suggestion(),
359 format!("{} {}", generics.add_where_or_trailing_comma(), pred),
363 /// Type parameter needs more bounds. The trivial case is `T` `where T: Bound`, but
364 /// it can also be an `impl Trait` param that needs to be decomposed to a type
365 /// param for cleaner code.
366 fn suggest_restriction<'tcx>(
369 hir_generics: &hir::Generics<'tcx>,
371 err: &mut Diagnostic,
372 fn_sig: Option<&hir::FnSig<'_>>,
373 projection: Option<&ty::ProjectionTy<'_>>,
374 trait_pred: ty::PolyTraitPredicate<'tcx>,
375 // When we are dealing with a trait, `super_traits` will be `Some`:
376 // Given `trait T: A + B + C {}`
377 // - ^^^^^^^^^ GenericBounds
380 super_traits: Option<(&Ident, &hir::GenericBounds<'_>)>,
382 if hir_generics.where_clause_span.from_expansion()
383 || hir_generics.where_clause_span.desugaring_kind().is_some()
387 let Some(item_id) = hir_id.as_owner() else { return; };
388 let generics = tcx.generics_of(item_id);
389 // Given `fn foo(t: impl Trait)` where `Trait` requires assoc type `A`...
390 if let Some((param, bound_str, fn_sig)) =
391 fn_sig.zip(projection).and_then(|(sig, p)| match p.self_ty().kind() {
392 // Shenanigans to get the `Trait` from the `impl Trait`.
393 ty::Param(param) => {
394 let param_def = generics.type_param(param, tcx);
395 if param_def.kind.is_synthetic() {
397 param_def.name.as_str().strip_prefix("impl ")?.trim_start().to_string();
398 return Some((param_def, bound_str, sig));
405 let type_param_name = hir_generics.params.next_type_param_name(Some(&bound_str));
406 let trait_pred = trait_pred.fold_with(&mut ReplaceImplTraitFolder {
409 replace_ty: ty::ParamTy::new(generics.count() as u32, Symbol::intern(&type_param_name))
412 if !trait_pred.is_suggestable(tcx, false) {
415 // We know we have an `impl Trait` that doesn't satisfy a required projection.
417 // Find all of the occurrences of `impl Trait` for `Trait` in the function arguments'
418 // types. There should be at least one, but there might be *more* than one. In that
419 // case we could just ignore it and try to identify which one needs the restriction,
420 // but instead we choose to suggest replacing all instances of `impl Trait` with `T`
422 let mut ty_spans = vec![];
423 for input in fn_sig.decl.inputs {
424 ReplaceImplTraitVisitor { ty_spans: &mut ty_spans, param_did: param.def_id }
427 // The type param `T: Trait` we will suggest to introduce.
428 let type_param = format!("{}: {}", type_param_name, bound_str);
431 if let Some(span) = hir_generics.span_for_param_suggestion() {
432 (span, format!(", {}", type_param))
434 (hir_generics.span, format!("<{}>", type_param))
436 // `fn foo(t: impl Trait)`
437 // ^ suggest `where <T as Trait>::A: Bound`
438 predicate_constraint(hir_generics, trait_pred.to_predicate(tcx)),
440 sugg.extend(ty_spans.into_iter().map(|s| (s, type_param_name.to_string())));
442 // Suggest `fn foo<T: Trait>(t: T) where <T as Trait>::A: Bound`.
443 // FIXME: once `#![feature(associated_type_bounds)]` is stabilized, we should suggest
444 // `fn foo(t: impl Trait<A: Bound>)` instead.
445 err.multipart_suggestion(
446 "introduce a type parameter with a trait bound instead of using `impl Trait`",
448 Applicability::MaybeIncorrect,
451 if !trait_pred.is_suggestable(tcx, false) {
454 // Trivial case: `T` needs an extra bound: `T: Bound`.
455 let (sp, suggestion) = match (
459 .find(|p| !matches!(p.kind, hir::GenericParamKind::Type { synthetic: true, .. })),
462 (_, None) => predicate_constraint(hir_generics, trait_pred.to_predicate(tcx)),
463 (None, Some((ident, []))) => (
464 ident.span.shrink_to_hi(),
465 format!(": {}", trait_pred.print_modifiers_and_trait_path()),
467 (_, Some((_, [.., bounds]))) => (
468 bounds.span().shrink_to_hi(),
469 format!(" + {}", trait_pred.print_modifiers_and_trait_path()),
471 (Some(_), Some((_, []))) => (
472 hir_generics.span.shrink_to_hi(),
473 format!(": {}", trait_pred.print_modifiers_and_trait_path()),
477 err.span_suggestion_verbose(
479 &format!("consider further restricting {}", msg),
481 Applicability::MachineApplicable,
486 impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
487 fn suggest_restricting_param_bound(
489 mut err: &mut Diagnostic,
490 trait_pred: ty::PolyTraitPredicate<'tcx>,
491 associated_ty: Option<(&'static str, Ty<'tcx>)>,
494 let trait_pred = self.resolve_numeric_literals_with_default(trait_pred);
496 let self_ty = trait_pred.skip_binder().self_ty();
497 let (param_ty, projection) = match self_ty.kind() {
498 ty::Param(_) => (true, None),
499 ty::Projection(projection) => (false, Some(projection)),
503 // FIXME: Add check for trait bound that is already present, particularly `?Sized` so we
504 // don't suggest `T: Sized + ?Sized`.
505 let mut hir_id = body_id;
506 while let Some(node) = self.tcx.hir().find(hir_id) {
508 hir::Node::Item(hir::Item {
510 kind: hir::ItemKind::Trait(_, _, generics, bounds, _),
512 }) if self_ty == self.tcx.types.self_param => {
514 // Restricting `Self` for a single method.
524 Some((ident, bounds)),
529 hir::Node::TraitItem(hir::TraitItem {
531 kind: hir::TraitItemKind::Fn(..),
533 }) if self_ty == self.tcx.types.self_param => {
535 // Restricting `Self` for a single method.
537 self.tcx, hir_id, &generics, "`Self`", err, None, projection, trait_pred,
543 hir::Node::TraitItem(hir::TraitItem {
545 kind: hir::TraitItemKind::Fn(fn_sig, ..),
548 | hir::Node::ImplItem(hir::ImplItem {
550 kind: hir::ImplItemKind::Fn(fn_sig, ..),
553 | hir::Node::Item(hir::Item {
554 kind: hir::ItemKind::Fn(fn_sig, generics, _), ..
555 }) if projection.is_some() => {
556 // Missing restriction on associated type of type parameter (unmet projection).
561 "the associated type",
570 hir::Node::Item(hir::Item {
572 hir::ItemKind::Trait(_, _, generics, ..)
573 | hir::ItemKind::Impl(hir::Impl { generics, .. }),
575 }) if projection.is_some() => {
576 // Missing restriction on associated type of type parameter (unmet projection).
581 "the associated type",
591 hir::Node::Item(hir::Item {
593 hir::ItemKind::Struct(_, generics)
594 | hir::ItemKind::Enum(_, generics)
595 | hir::ItemKind::Union(_, generics)
596 | hir::ItemKind::Trait(_, _, generics, ..)
597 | hir::ItemKind::Impl(hir::Impl { generics, .. })
598 | hir::ItemKind::Fn(_, generics, _)
599 | hir::ItemKind::TyAlias(_, generics)
600 | hir::ItemKind::TraitAlias(generics, _)
601 | hir::ItemKind::OpaqueTy(hir::OpaqueTy { generics, .. }),
604 | hir::Node::TraitItem(hir::TraitItem { generics, .. })
605 | hir::Node::ImplItem(hir::ImplItem { generics, .. })
608 // We skip the 0'th subst (self) because we do not want
609 // to consider the predicate as not suggestible if the
610 // self type is an arg position `impl Trait` -- instead,
611 // we handle that by adding ` + Bound` below.
612 // FIXME(compiler-errors): It would be nice to do the same
613 // this that we do in `suggest_restriction` and pull the
614 // `impl Trait` into a new generic if it shows up somewhere
615 // else in the predicate.
616 if !trait_pred.skip_binder().trait_ref.substs[1..]
618 .all(|g| g.is_suggestable(self.tcx, false))
622 // Missing generic type parameter bound.
623 let param_name = self_ty.to_string();
624 let mut constraint = with_no_trimmed_paths!(
625 trait_pred.print_modifiers_and_trait_path().to_string()
628 if let Some((name, term)) = associated_ty {
629 // FIXME: this case overlaps with code in TyCtxt::note_and_explain_type_err.
630 // That should be extracted into a helper function.
631 if constraint.ends_with('>') {
632 constraint = format!(
634 &constraint[..constraint.len() - 1],
639 constraint.push_str(&format!("<{} = {}>", name, term));
643 if suggest_constraining_type_param(
649 Some(trait_pred.def_id()),
655 hir::Node::Item(hir::Item {
657 hir::ItemKind::Struct(_, generics)
658 | hir::ItemKind::Enum(_, generics)
659 | hir::ItemKind::Union(_, generics)
660 | hir::ItemKind::Trait(_, _, generics, ..)
661 | hir::ItemKind::Impl(hir::Impl { generics, .. })
662 | hir::ItemKind::Fn(_, generics, _)
663 | hir::ItemKind::TyAlias(_, generics)
664 | hir::ItemKind::TraitAlias(generics, _)
665 | hir::ItemKind::OpaqueTy(hir::OpaqueTy { generics, .. }),
668 // Missing generic type parameter bound.
669 if suggest_arbitrary_trait_bound(
679 hir::Node::Crate(..) => return,
684 hir_id = self.tcx.hir().get_parent_item(hir_id).into();
688 /// When after several dereferencing, the reference satisfies the trait
689 /// binding. This function provides dereference suggestion for this
690 /// specific situation.
691 fn suggest_dereferences(
693 obligation: &PredicateObligation<'tcx>,
694 err: &mut Diagnostic,
695 trait_pred: ty::PolyTraitPredicate<'tcx>,
697 // It only make sense when suggesting dereferences for arguments
698 let ObligationCauseCode::FunctionArgumentObligation { arg_hir_id, .. } = obligation.cause.code()
699 else { return false; };
700 let Some(typeck_results) = &self.typeck_results
701 else { return false; };
702 let hir::Node::Expr(expr) = self.tcx.hir().get(*arg_hir_id)
703 else { return false; };
704 let Some(arg_ty) = typeck_results.expr_ty_adjusted_opt(expr)
705 else { return false; };
707 let span = obligation.cause.span;
708 let mut real_trait_pred = trait_pred;
709 let mut code = obligation.cause.code();
710 while let Some((parent_code, parent_trait_pred)) = code.parent() {
712 if let Some(parent_trait_pred) = parent_trait_pred {
713 real_trait_pred = parent_trait_pred;
716 let real_ty = real_trait_pred.self_ty();
717 // We `erase_late_bound_regions` here because `make_subregion` does not handle
718 // `ReLateBound`, and we don't particularly care about the regions.
720 .can_eq(obligation.param_env, self.tcx.erase_late_bound_regions(real_ty), arg_ty)
726 if let ty::Ref(region, base_ty, mutbl) = *real_ty.skip_binder().kind() {
727 let mut autoderef = Autoderef::new(
729 obligation.param_env,
730 obligation.cause.body_id,
734 if let Some(steps) = autoderef.find_map(|(ty, steps)| {
736 let ty = self.tcx.mk_ref(region, TypeAndMut { ty, mutbl });
738 // Remapping bound vars here
739 let real_trait_pred_and_ty =
740 real_trait_pred.map_bound(|inner_trait_pred| (inner_trait_pred, ty));
741 let obligation = self.mk_trait_obligation_with_new_self_ty(
742 obligation.param_env,
743 real_trait_pred_and_ty,
745 Some(steps).filter(|_| self.predicate_may_hold(&obligation))
748 // Don't care about `&mut` because `DerefMut` is used less
749 // often and user will not expect autoderef happens.
750 if let Some(hir::Node::Expr(hir::Expr {
752 hir::ExprKind::AddrOf(hir::BorrowKind::Ref, hir::Mutability::Not, expr),
754 })) = self.tcx.hir().find(*arg_hir_id)
756 let derefs = "*".repeat(steps);
757 err.span_suggestion_verbose(
758 expr.span.shrink_to_lo(),
759 "consider dereferencing here",
761 Applicability::MachineApplicable,
766 } else if real_trait_pred != trait_pred {
767 // This branch addresses #87437.
769 // Remapping bound vars here
770 let real_trait_pred_and_base_ty =
771 real_trait_pred.map_bound(|inner_trait_pred| (inner_trait_pred, base_ty));
772 let obligation = self.mk_trait_obligation_with_new_self_ty(
773 obligation.param_env,
774 real_trait_pred_and_base_ty,
776 if self.predicate_may_hold(&obligation) {
777 err.span_suggestion_verbose(
779 "consider dereferencing here",
781 Applicability::MachineApplicable,
791 /// Given a closure's `DefId`, return the given name of the closure.
793 /// This doesn't account for reassignments, but it's only used for suggestions.
794 fn get_closure_name(&self, def_id: DefId, err: &mut Diagnostic, msg: &str) -> Option<Symbol> {
795 let get_name = |err: &mut Diagnostic, kind: &hir::PatKind<'_>| -> Option<Symbol> {
796 // Get the local name of this closure. This can be inaccurate because
797 // of the possibility of reassignment, but this should be good enough.
799 hir::PatKind::Binding(hir::BindingAnnotation::NONE, _, ident, None) => {
809 let hir = self.tcx.hir();
810 let hir_id = hir.local_def_id_to_hir_id(def_id.as_local()?);
811 let parent_node = hir.get_parent_node(hir_id);
812 match hir.find(parent_node) {
813 Some(hir::Node::Stmt(hir::Stmt { kind: hir::StmtKind::Local(local), .. })) => {
814 get_name(err, &local.pat.kind)
816 // Different to previous arm because one is `&hir::Local` and the other
817 // is `P<hir::Local>`.
818 Some(hir::Node::Local(local)) => get_name(err, &local.pat.kind),
823 /// We tried to apply the bound to an `fn` or closure. Check whether calling it would
824 /// evaluate to a type that *would* satisfy the trait binding. If it would, suggest calling
825 /// it: `bar(foo)` → `bar(foo())`. This case is *very* likely to be hit if `foo` is `async`.
828 obligation: &PredicateObligation<'tcx>,
829 err: &mut Diagnostic,
830 trait_pred: ty::PolyTraitPredicate<'tcx>,
832 if let ty::PredicateKind::Clause(ty::Clause::Trait(trait_pred)) = obligation.predicate.kind().skip_binder()
833 && Some(trait_pred.def_id()) == self.tcx.lang_items().sized_trait()
835 // Don't suggest calling to turn an unsized type into a sized type
839 // This is duplicated from `extract_callable_info` in typeck, which
840 // relies on autoderef, so we can't use it here.
841 let found = trait_pred.self_ty().skip_binder().peel_refs();
842 let Some((def_id_or_name, output, inputs)) = (match *found.kind()
844 ty::FnPtr(fn_sig) => {
845 Some((DefIdOrName::Name("function pointer"), fn_sig.output(), fn_sig.inputs()))
847 ty::FnDef(def_id, _) => {
848 let fn_sig = found.fn_sig(self.tcx);
849 Some((DefIdOrName::DefId(def_id), fn_sig.output(), fn_sig.inputs()))
851 ty::Closure(def_id, substs) => {
852 let fn_sig = substs.as_closure().sig();
854 DefIdOrName::DefId(def_id),
856 fn_sig.inputs().map_bound(|inputs| &inputs[1..]),
859 ty::Opaque(def_id, substs) => {
860 self.tcx.bound_item_bounds(def_id).subst(self.tcx, substs).iter().find_map(|pred| {
861 if let ty::PredicateKind::Clause(ty::Clause::Projection(proj)) = pred.kind().skip_binder()
862 && Some(proj.projection_ty.item_def_id) == self.tcx.lang_items().fn_once_output()
863 // args tuple will always be substs[1]
864 && let ty::Tuple(args) = proj.projection_ty.substs.type_at(1).kind()
867 DefIdOrName::DefId(def_id),
868 pred.kind().rebind(proj.term.ty().unwrap()),
869 pred.kind().rebind(args.as_slice()),
876 ty::Dynamic(data, _, ty::Dyn) => {
877 data.iter().find_map(|pred| {
878 if let ty::ExistentialPredicate::Projection(proj) = pred.skip_binder()
879 && Some(proj.item_def_id) == self.tcx.lang_items().fn_once_output()
880 // for existential projection, substs are shifted over by 1
881 && let ty::Tuple(args) = proj.substs.type_at(0).kind()
884 DefIdOrName::Name("trait object"),
885 pred.rebind(proj.term.ty().unwrap()),
886 pred.rebind(args.as_slice()),
894 obligation.param_env.caller_bounds().iter().find_map(|pred| {
895 if let ty::PredicateKind::Clause(ty::Clause::Projection(proj)) = pred.kind().skip_binder()
896 && Some(proj.projection_ty.item_def_id) == self.tcx.lang_items().fn_once_output()
897 && proj.projection_ty.self_ty() == found
898 // args tuple will always be substs[1]
899 && let ty::Tuple(args) = proj.projection_ty.substs.type_at(1).kind()
902 DefIdOrName::Name("type parameter"),
903 pred.kind().rebind(proj.term.ty().unwrap()),
904 pred.kind().rebind(args.as_slice()),
912 }) else { return false; };
913 let output = self.replace_bound_vars_with_fresh_vars(
914 obligation.cause.span,
915 LateBoundRegionConversionTime::FnCall,
918 let inputs = inputs.skip_binder().iter().map(|ty| {
919 self.replace_bound_vars_with_fresh_vars(
920 obligation.cause.span,
921 LateBoundRegionConversionTime::FnCall,
926 // Remapping bound vars here
927 let trait_pred_and_self = trait_pred.map_bound(|trait_pred| (trait_pred, output));
930 self.mk_trait_obligation_with_new_self_ty(obligation.param_env, trait_pred_and_self);
931 if !self.predicate_must_hold_modulo_regions(&new_obligation) {
935 // Get the name of the callable and the arguments to be used in the suggestion.
936 let hir = self.tcx.hir();
938 let msg = match def_id_or_name {
939 DefIdOrName::DefId(def_id) => match self.tcx.def_kind(def_id) {
940 DefKind::Ctor(CtorOf::Struct, _) => {
941 "use parentheses to construct this tuple struct".to_string()
943 DefKind::Ctor(CtorOf::Variant, _) => {
944 "use parentheses to construct this tuple variant".to_string()
946 kind => format!("use parentheses to call this {}", kind.descr(def_id)),
948 DefIdOrName::Name(name) => format!("use parentheses to call this {name}"),
953 if ty.is_suggestable(self.tcx, false) {
954 format!("/* {ty} */")
956 "/* value */".to_string()
962 if matches!(obligation.cause.code(), ObligationCauseCode::FunctionArgumentObligation { .. })
963 && obligation.cause.span.can_be_used_for_suggestions()
965 // When the obligation error has been ensured to have been caused by
966 // an argument, the `obligation.cause.span` points at the expression
967 // of the argument, so we can provide a suggestion. Otherwise, we give
968 // a more general note.
969 err.span_suggestion_verbose(
970 obligation.cause.span.shrink_to_hi(),
973 Applicability::HasPlaceholders,
975 } else if let DefIdOrName::DefId(def_id) = def_id_or_name {
976 let name = match hir.get_if_local(def_id) {
977 Some(hir::Node::Expr(hir::Expr {
978 kind: hir::ExprKind::Closure(hir::Closure { fn_decl_span, .. }),
981 err.span_label(*fn_decl_span, "consider calling this closure");
982 let Some(name) = self.get_closure_name(def_id, err, &msg) else {
987 Some(hir::Node::Item(hir::Item { ident, kind: hir::ItemKind::Fn(..), .. })) => {
988 err.span_label(ident.span, "consider calling this function");
991 Some(hir::Node::Ctor(..)) => {
992 let name = self.tcx.def_path_str(def_id);
994 self.tcx.def_span(def_id),
995 format!("consider calling the constructor for `{}`", name),
1001 err.help(&format!("{msg}: `{name}({args})`"));
1006 fn suggest_add_reference_to_arg(
1008 obligation: &PredicateObligation<'tcx>,
1009 err: &mut Diagnostic,
1010 poly_trait_pred: ty::PolyTraitPredicate<'tcx>,
1011 has_custom_message: bool,
1013 let span = obligation.cause.span;
1015 let code = if let ObligationCauseCode::FunctionArgumentObligation { parent_code, .. } =
1016 obligation.cause.code()
1019 } else if let ObligationCauseCode::ItemObligation(_)
1020 | ObligationCauseCode::ExprItemObligation(..) = obligation.cause.code()
1022 obligation.cause.code()
1023 } else if let ExpnKind::Desugaring(DesugaringKind::ForLoop) =
1024 span.ctxt().outer_expn_data().kind
1026 obligation.cause.code()
1031 // List of traits for which it would be nonsensical to suggest borrowing.
1032 // For instance, immutable references are always Copy, so suggesting to
1033 // borrow would always succeed, but it's probably not what the user wanted.
1034 let mut never_suggest_borrow: Vec<_> =
1035 [LangItem::Copy, LangItem::Clone, LangItem::Unpin, LangItem::Sized]
1037 .filter_map(|lang_item| self.tcx.lang_items().get(*lang_item))
1040 if let Some(def_id) = self.tcx.get_diagnostic_item(sym::Send) {
1041 never_suggest_borrow.push(def_id);
1044 let param_env = obligation.param_env;
1046 // Try to apply the original trait binding obligation by borrowing.
1047 let mut try_borrowing = |old_pred: ty::PolyTraitPredicate<'tcx>,
1048 blacklist: &[DefId]|
1050 if blacklist.contains(&old_pred.def_id()) {
1053 // We map bounds to `&T` and `&mut T`
1054 let trait_pred_and_imm_ref = old_pred.map_bound(|trait_pred| {
1057 self.tcx.mk_imm_ref(self.tcx.lifetimes.re_static, trait_pred.self_ty()),
1060 let trait_pred_and_mut_ref = old_pred.map_bound(|trait_pred| {
1063 self.tcx.mk_mut_ref(self.tcx.lifetimes.re_static, trait_pred.self_ty()),
1067 let mk_result = |trait_pred_and_new_ty| {
1069 self.mk_trait_obligation_with_new_self_ty(param_env, trait_pred_and_new_ty);
1070 self.predicate_must_hold_modulo_regions(&obligation)
1072 let imm_ref_self_ty_satisfies_pred = mk_result(trait_pred_and_imm_ref);
1073 let mut_ref_self_ty_satisfies_pred = mk_result(trait_pred_and_mut_ref);
1075 let (ref_inner_ty_satisfies_pred, ref_inner_ty_mut) =
1076 if let ObligationCauseCode::ItemObligation(_) | ObligationCauseCode::ExprItemObligation(..) = obligation.cause.code()
1077 && let ty::Ref(_, ty, mutability) = old_pred.self_ty().skip_binder().kind()
1080 mk_result(old_pred.map_bound(|trait_pred| (trait_pred, *ty))),
1081 mutability.is_mut(),
1087 if imm_ref_self_ty_satisfies_pred
1088 || mut_ref_self_ty_satisfies_pred
1089 || ref_inner_ty_satisfies_pred
1091 if let Ok(snippet) = self.tcx.sess.source_map().span_to_snippet(span) {
1092 // We don't want a borrowing suggestion on the fields in structs,
1095 // the_foos: Vec<Foo>
1099 span.ctxt().outer_expn_data().kind,
1100 ExpnKind::Root | ExpnKind::Desugaring(DesugaringKind::ForLoop)
1104 if snippet.starts_with('&') {
1105 // This is already a literal borrow and the obligation is failing
1106 // somewhere else in the obligation chain. Do not suggest non-sense.
1109 // We have a very specific type of error, where just borrowing this argument
1110 // might solve the problem. In cases like this, the important part is the
1111 // original type obligation, not the last one that failed, which is arbitrary.
1112 // Because of this, we modify the error to refer to the original obligation and
1113 // return early in the caller.
1115 let msg = format!("the trait bound `{}` is not satisfied", old_pred);
1116 if has_custom_message {
1120 vec![(rustc_errors::DiagnosticMessage::Str(msg), Style::NoStyle)];
1125 "the trait `{}` is not implemented for `{}`",
1126 old_pred.print_modifiers_and_trait_path(),
1127 old_pred.self_ty().skip_binder(),
1131 if imm_ref_self_ty_satisfies_pred && mut_ref_self_ty_satisfies_pred {
1132 err.span_suggestions(
1133 span.shrink_to_lo(),
1134 "consider borrowing here",
1135 ["&".to_string(), "&mut ".to_string()],
1136 Applicability::MaybeIncorrect,
1139 let is_mut = mut_ref_self_ty_satisfies_pred || ref_inner_ty_mut;
1140 err.span_suggestion_verbose(
1141 span.shrink_to_lo(),
1143 "consider{} borrowing here",
1144 if is_mut { " mutably" } else { "" }
1146 format!("&{}", if is_mut { "mut " } else { "" }),
1147 Applicability::MaybeIncorrect,
1156 if let ObligationCauseCode::ImplDerivedObligation(cause) = &*code {
1157 try_borrowing(cause.derived.parent_trait_pred, &[])
1158 } else if let ObligationCauseCode::BindingObligation(_, _)
1159 | ObligationCauseCode::ItemObligation(_)
1160 | ObligationCauseCode::ExprItemObligation(..)
1161 | ObligationCauseCode::ExprBindingObligation(..) = code
1163 try_borrowing(poly_trait_pred, &never_suggest_borrow)
1169 // Suggest borrowing the type
1170 fn suggest_borrowing_for_object_cast(
1172 err: &mut Diagnostic,
1173 obligation: &PredicateObligation<'tcx>,
1175 object_ty: Ty<'tcx>,
1177 let ty::Dynamic(predicates, _, ty::Dyn) = object_ty.kind() else { return; };
1178 let self_ref_ty = self.tcx.mk_imm_ref(self.tcx.lifetimes.re_erased, self_ty);
1180 for predicate in predicates.iter() {
1181 if !self.predicate_must_hold_modulo_regions(
1182 &obligation.with(self.tcx, predicate.with_self_ty(self.tcx, self_ref_ty)),
1188 err.span_suggestion(
1189 obligation.cause.span.shrink_to_lo(),
1191 "consider borrowing the value, since `&{self_ty}` can be coerced into `{object_ty}`"
1194 Applicability::MaybeIncorrect,
1198 /// Whenever references are used by mistake, like `for (i, e) in &vec.iter().enumerate()`,
1199 /// suggest removing these references until we reach a type that implements the trait.
1200 fn suggest_remove_reference(
1202 obligation: &PredicateObligation<'tcx>,
1203 err: &mut Diagnostic,
1204 trait_pred: ty::PolyTraitPredicate<'tcx>,
1206 let span = obligation.cause.span;
1208 let mut suggested = false;
1209 if let Ok(snippet) = self.tcx.sess.source_map().span_to_snippet(span) {
1211 snippet.chars().filter(|c| !c.is_whitespace()).take_while(|c| *c == '&').count();
1212 if let Some('\'') = snippet.chars().filter(|c| !c.is_whitespace()).nth(refs_number) {
1213 // Do not suggest removal of borrow from type arguments.
1217 // Skipping binder here, remapping below
1218 let mut suggested_ty = trait_pred.self_ty().skip_binder();
1220 for refs_remaining in 0..refs_number {
1221 let ty::Ref(_, inner_ty, _) = suggested_ty.kind() else {
1224 suggested_ty = *inner_ty;
1226 // Remapping bound vars here
1227 let trait_pred_and_suggested_ty =
1228 trait_pred.map_bound(|trait_pred| (trait_pred, suggested_ty));
1230 let new_obligation = self.mk_trait_obligation_with_new_self_ty(
1231 obligation.param_env,
1232 trait_pred_and_suggested_ty,
1235 if self.predicate_may_hold(&new_obligation) {
1240 .span_take_while(span, |c| c.is_whitespace() || *c == '&');
1242 let remove_refs = refs_remaining + 1;
1244 let msg = if remove_refs == 1 {
1245 "consider removing the leading `&`-reference".to_string()
1247 format!("consider removing {} leading `&`-references", remove_refs)
1250 err.span_suggestion_short(sp, &msg, "", Applicability::MachineApplicable);
1259 fn suggest_remove_await(&self, obligation: &PredicateObligation<'tcx>, err: &mut Diagnostic) {
1260 let span = obligation.cause.span;
1262 if let ObligationCauseCode::AwaitableExpr(hir_id) = obligation.cause.code().peel_derives() {
1263 let hir = self.tcx.hir();
1264 if let Some(node) = hir_id.and_then(|hir_id| hir.find(hir_id)) {
1265 if let hir::Node::Expr(expr) = node {
1266 // FIXME: use `obligation.predicate.kind()...trait_ref.self_ty()` to see if we have `()`
1267 // and if not maybe suggest doing something else? If we kept the expression around we
1268 // could also check if it is an fn call (very likely) and suggest changing *that*, if
1269 // it is from the local crate.
1270 err.span_suggestion(
1272 "remove the `.await`",
1274 Applicability::MachineApplicable,
1276 // FIXME: account for associated `async fn`s.
1277 if let hir::Expr { span, kind: hir::ExprKind::Call(base, _), .. } = expr {
1278 if let ty::PredicateKind::Clause(ty::Clause::Trait(pred)) =
1279 obligation.predicate.kind().skip_binder()
1283 &format!("this call returns `{}`", pred.self_ty()),
1286 if let Some(typeck_results) = &self.typeck_results
1287 && let ty = typeck_results.expr_ty_adjusted(base)
1288 && let ty::FnDef(def_id, _substs) = ty.kind()
1289 && let Some(hir::Node::Item(hir::Item { ident, span, vis_span, .. })) =
1290 hir.get_if_local(*def_id)
1293 "alternatively, consider making `fn {}` asynchronous",
1296 if vis_span.is_empty() {
1297 err.span_suggestion_verbose(
1298 span.shrink_to_lo(),
1301 Applicability::MaybeIncorrect,
1304 err.span_suggestion_verbose(
1305 vis_span.shrink_to_hi(),
1308 Applicability::MaybeIncorrect,
1318 /// Check if the trait bound is implemented for a different mutability and note it in the
1320 fn suggest_change_mut(
1322 obligation: &PredicateObligation<'tcx>,
1323 err: &mut Diagnostic,
1324 trait_pred: ty::PolyTraitPredicate<'tcx>,
1326 let points_at_arg = matches!(
1327 obligation.cause.code(),
1328 ObligationCauseCode::FunctionArgumentObligation { .. },
1331 let span = obligation.cause.span;
1332 if let Ok(snippet) = self.tcx.sess.source_map().span_to_snippet(span) {
1334 snippet.chars().filter(|c| !c.is_whitespace()).take_while(|c| *c == '&').count();
1335 if let Some('\'') = snippet.chars().filter(|c| !c.is_whitespace()).nth(refs_number) {
1336 // Do not suggest removal of borrow from type arguments.
1339 let trait_pred = self.resolve_vars_if_possible(trait_pred);
1340 if trait_pred.has_non_region_infer() {
1341 // Do not ICE while trying to find if a reborrow would succeed on a trait with
1342 // unresolved bindings.
1346 // Skipping binder here, remapping below
1347 if let ty::Ref(region, t_type, mutability) = *trait_pred.skip_binder().self_ty().kind()
1349 let suggested_ty = match mutability {
1350 hir::Mutability::Mut => self.tcx.mk_imm_ref(region, t_type),
1351 hir::Mutability::Not => self.tcx.mk_mut_ref(region, t_type),
1354 // Remapping bound vars here
1355 let trait_pred_and_suggested_ty =
1356 trait_pred.map_bound(|trait_pred| (trait_pred, suggested_ty));
1358 let new_obligation = self.mk_trait_obligation_with_new_self_ty(
1359 obligation.param_env,
1360 trait_pred_and_suggested_ty,
1362 let suggested_ty_would_satisfy_obligation = self
1363 .evaluate_obligation_no_overflow(&new_obligation)
1364 .must_apply_modulo_regions();
1365 if suggested_ty_would_satisfy_obligation {
1370 .span_take_while(span, |c| c.is_whitespace() || *c == '&');
1371 if points_at_arg && mutability.is_not() && refs_number > 0 {
1372 err.span_suggestion_verbose(
1374 "consider changing this borrow's mutability",
1376 Applicability::MachineApplicable,
1380 "`{}` is implemented for `{:?}`, but not for `{:?}`",
1381 trait_pred.print_modifiers_and_trait_path(),
1383 trait_pred.skip_binder().self_ty(),
1391 fn suggest_semicolon_removal(
1393 obligation: &PredicateObligation<'tcx>,
1394 err: &mut Diagnostic,
1396 trait_pred: ty::PolyTraitPredicate<'tcx>,
1398 let hir = self.tcx.hir();
1399 let parent_node = hir.get_parent_node(obligation.cause.body_id);
1400 let node = hir.find(parent_node);
1401 if let Some(hir::Node::Item(hir::Item { kind: hir::ItemKind::Fn(sig, _, body_id), .. })) = node
1402 && let hir::ExprKind::Block(blk, _) = &hir.body(*body_id).value.kind
1403 && sig.decl.output.span().overlaps(span)
1404 && blk.expr.is_none()
1405 && trait_pred.self_ty().skip_binder().is_unit()
1406 && let Some(stmt) = blk.stmts.last()
1407 && let hir::StmtKind::Semi(expr) = stmt.kind
1408 // Only suggest this if the expression behind the semicolon implements the predicate
1409 && let Some(typeck_results) = &self.typeck_results
1410 && let Some(ty) = typeck_results.expr_ty_opt(expr)
1411 && self.predicate_may_hold(&self.mk_trait_obligation_with_new_self_ty(
1412 obligation.param_env, trait_pred.map_bound(|trait_pred| (trait_pred, ty))
1418 "this expression has type `{}`, which implements `{}`",
1420 trait_pred.print_modifiers_and_trait_path()
1423 err.span_suggestion(
1424 self.tcx.sess.source_map().end_point(stmt.span),
1425 "remove this semicolon",
1427 Applicability::MachineApplicable
1434 fn return_type_span(&self, obligation: &PredicateObligation<'tcx>) -> Option<Span> {
1435 let hir = self.tcx.hir();
1436 let parent_node = hir.get_parent_node(obligation.cause.body_id);
1437 let Some(hir::Node::Item(hir::Item { kind: hir::ItemKind::Fn(sig, ..), .. })) = hir.find(parent_node) else {
1441 if let hir::FnRetTy::Return(ret_ty) = sig.decl.output { Some(ret_ty.span) } else { None }
1444 /// If all conditions are met to identify a returned `dyn Trait`, suggest using `impl Trait` if
1445 /// applicable and signal that the error has been expanded appropriately and needs to be
1447 fn suggest_impl_trait(
1449 err: &mut Diagnostic,
1451 obligation: &PredicateObligation<'tcx>,
1452 trait_pred: ty::PolyTraitPredicate<'tcx>,
1454 match obligation.cause.code().peel_derives() {
1455 // Only suggest `impl Trait` if the return type is unsized because it is `dyn Trait`.
1456 ObligationCauseCode::SizedReturnType => {}
1460 let hir = self.tcx.hir();
1461 let fn_hir_id = hir.get_parent_node(obligation.cause.body_id);
1462 let node = hir.find(fn_hir_id);
1463 let Some(hir::Node::Item(hir::Item {
1464 kind: hir::ItemKind::Fn(sig, _, body_id),
1470 let body = hir.body(*body_id);
1471 let trait_pred = self.resolve_vars_if_possible(trait_pred);
1472 let ty = trait_pred.skip_binder().self_ty();
1473 let is_object_safe = match ty.kind() {
1474 ty::Dynamic(predicates, _, ty::Dyn) => {
1475 // If the `dyn Trait` is not object safe, do not suggest `Box<dyn Trait>`.
1478 .map_or(true, |def_id| self.tcx.object_safety_violations(def_id).is_empty())
1480 // We only want to suggest `impl Trait` to `dyn Trait`s.
1481 // For example, `fn foo() -> str` needs to be filtered out.
1485 let hir::FnRetTy::Return(ret_ty) = sig.decl.output else {
1489 // Use `TypeVisitor` instead of the output type directly to find the span of `ty` for
1490 // cases like `fn foo() -> (dyn Trait, i32) {}`.
1491 // Recursively look for `TraitObject` types and if there's only one, use that span to
1492 // suggest `impl Trait`.
1494 // Visit to make sure there's a single `return` type to suggest `impl Trait`,
1495 // otherwise suggest using `Box<dyn Trait>` or an enum.
1496 let mut visitor = ReturnsVisitor::default();
1497 visitor.visit_body(&body);
1499 let typeck_results = self.typeck_results.as_ref().unwrap();
1500 let Some(liberated_sig) = typeck_results.liberated_fn_sigs().get(fn_hir_id).copied() else { return false; };
1502 let ret_types = visitor
1505 .filter_map(|expr| Some((expr.span, typeck_results.node_type_opt(expr.hir_id)?)))
1506 .map(|(expr_span, ty)| (expr_span, self.resolve_vars_if_possible(ty)));
1507 let (last_ty, all_returns_have_same_type, only_never_return) = ret_types.clone().fold(
1509 |(last_ty, mut same, only_never_return): (std::option::Option<Ty<'_>>, bool, bool),
1511 let ty = self.resolve_vars_if_possible(ty);
1513 !matches!(ty.kind(), ty::Error(_))
1514 && last_ty.map_or(true, |last_ty| {
1515 // FIXME: ideally we would use `can_coerce` here instead, but `typeck` comes
1516 // *after* in the dependency graph.
1517 match (ty.kind(), last_ty.kind()) {
1518 (Infer(InferTy::IntVar(_)), Infer(InferTy::IntVar(_)))
1519 | (Infer(InferTy::FloatVar(_)), Infer(InferTy::FloatVar(_)))
1520 | (Infer(InferTy::FreshIntTy(_)), Infer(InferTy::FreshIntTy(_)))
1522 Infer(InferTy::FreshFloatTy(_)),
1523 Infer(InferTy::FreshFloatTy(_)),
1528 (Some(ty), same, only_never_return && matches!(ty.kind(), ty::Never))
1531 let mut spans_and_needs_box = vec![];
1533 match liberated_sig.output().kind() {
1534 ty::Dynamic(predicates, _, ty::Dyn) => {
1535 let cause = ObligationCause::misc(ret_ty.span, fn_hir_id);
1536 let param_env = ty::ParamEnv::empty();
1538 if !only_never_return {
1539 for (expr_span, return_ty) in ret_types {
1540 let self_ty_satisfies_dyn_predicates = |self_ty| {
1541 predicates.iter().all(|predicate| {
1542 let pred = predicate.with_self_ty(self.tcx, self_ty);
1543 let obl = Obligation::new(self.tcx, cause.clone(), param_env, pred);
1544 self.predicate_may_hold(&obl)
1548 if let ty::Adt(def, substs) = return_ty.kind()
1550 && self_ty_satisfies_dyn_predicates(substs.type_at(0))
1552 spans_and_needs_box.push((expr_span, false));
1553 } else if self_ty_satisfies_dyn_predicates(return_ty) {
1554 spans_and_needs_box.push((expr_span, true));
1564 let sm = self.tcx.sess.source_map();
1565 if !ret_ty.span.overlaps(span) {
1568 let snippet = if let hir::TyKind::TraitObject(..) = ret_ty.kind {
1569 if let Ok(snippet) = sm.span_to_snippet(ret_ty.span) {
1575 // Substitute the type, so we can print a fixup given `type Alias = dyn Trait`
1576 let name = liberated_sig.output().to_string();
1578 name.strip_prefix('(').and_then(|name| name.strip_suffix(')')).unwrap_or(&name);
1579 if !name.starts_with("dyn ") {
1585 err.code(error_code!(E0746));
1586 err.set_primary_message("return type cannot have an unboxed trait object");
1587 err.children.clear();
1588 let impl_trait_msg = "for information on `impl Trait`, see \
1589 <https://doc.rust-lang.org/book/ch10-02-traits.html\
1590 #returning-types-that-implement-traits>";
1591 let trait_obj_msg = "for information on trait objects, see \
1592 <https://doc.rust-lang.org/book/ch17-02-trait-objects.html\
1593 #using-trait-objects-that-allow-for-values-of-different-types>";
1595 let has_dyn = snippet.split_whitespace().next().map_or(false, |s| s == "dyn");
1596 let trait_obj = if has_dyn { &snippet[4..] } else { &snippet };
1597 if only_never_return {
1598 // No return paths, probably using `panic!()` or similar.
1599 // Suggest `-> T`, `-> impl Trait`, and if `Trait` is object safe, `-> Box<dyn Trait>`.
1600 suggest_trait_object_return_type_alternatives(
1606 } else if let (Some(last_ty), true) = (last_ty, all_returns_have_same_type) {
1607 // Suggest `-> impl Trait`.
1608 err.span_suggestion(
1611 "use `impl {1}` as the return type, as all return paths are of type `{}`, \
1612 which implements `{1}`",
1615 format!("impl {}", trait_obj),
1616 Applicability::MachineApplicable,
1618 err.note(impl_trait_msg);
1621 // Suggest `-> Box<dyn Trait>` and `Box::new(returned_value)`.
1622 err.multipart_suggestion(
1623 "return a boxed trait object instead",
1625 (ret_ty.span.shrink_to_lo(), "Box<".to_string()),
1626 (span.shrink_to_hi(), ">".to_string()),
1628 Applicability::MaybeIncorrect,
1630 for (span, needs_box) in spans_and_needs_box {
1632 err.multipart_suggestion(
1633 "... and box this value",
1635 (span.shrink_to_lo(), "Box::new(".to_string()),
1636 (span.shrink_to_hi(), ")".to_string()),
1638 Applicability::MaybeIncorrect,
1643 // This is currently not possible to trigger because E0038 takes precedence, but
1644 // leave it in for completeness in case anything changes in an earlier stage.
1646 "if trait `{}` were object-safe, you could return a trait object",
1650 err.note(trait_obj_msg);
1652 "if all the returned values were of the same type you could use `impl {}` as the \
1656 err.note(impl_trait_msg);
1657 err.note("you can create a new `enum` with a variant for each returned type");
1662 fn point_at_returns_when_relevant(
1664 err: &mut Diagnostic,
1665 obligation: &PredicateObligation<'tcx>,
1667 match obligation.cause.code().peel_derives() {
1668 ObligationCauseCode::SizedReturnType => {}
1672 let hir = self.tcx.hir();
1673 let parent_node = hir.get_parent_node(obligation.cause.body_id);
1674 let node = hir.find(parent_node);
1675 if let Some(hir::Node::Item(hir::Item { kind: hir::ItemKind::Fn(_, _, body_id), .. })) =
1678 let body = hir.body(*body_id);
1679 // Point at all the `return`s in the function as they have failed trait bounds.
1680 let mut visitor = ReturnsVisitor::default();
1681 visitor.visit_body(&body);
1682 let typeck_results = self.typeck_results.as_ref().unwrap();
1683 for expr in &visitor.returns {
1684 if let Some(returned_ty) = typeck_results.node_type_opt(expr.hir_id) {
1685 let ty = self.resolve_vars_if_possible(returned_ty);
1686 err.span_label(expr.span, &format!("this returned value is of type `{}`", ty));
1692 fn report_closure_arg_mismatch(
1695 found_span: Option<Span>,
1696 found: ty::PolyTraitRef<'tcx>,
1697 expected: ty::PolyTraitRef<'tcx>,
1698 cause: &ObligationCauseCode<'tcx>,
1699 found_node: Option<Node<'_>>,
1700 ) -> DiagnosticBuilder<'tcx, ErrorGuaranteed> {
1701 pub(crate) fn build_fn_sig_ty<'tcx>(
1702 infcx: &InferCtxt<'tcx>,
1703 trait_ref: ty::PolyTraitRef<'tcx>,
1705 let inputs = trait_ref.skip_binder().substs.type_at(1);
1706 let sig = match inputs.kind() {
1707 ty::Tuple(inputs) if infcx.tcx.is_fn_trait(trait_ref.def_id()) => {
1708 infcx.tcx.mk_fn_sig(
1710 infcx.next_ty_var(TypeVariableOrigin {
1712 kind: TypeVariableOriginKind::MiscVariable,
1715 hir::Unsafety::Normal,
1719 _ => infcx.tcx.mk_fn_sig(
1720 std::iter::once(inputs),
1721 infcx.next_ty_var(TypeVariableOrigin {
1723 kind: TypeVariableOriginKind::MiscVariable,
1726 hir::Unsafety::Normal,
1731 infcx.tcx.mk_fn_ptr(trait_ref.rebind(sig))
1734 let argument_kind = match expected.skip_binder().self_ty().kind() {
1735 ty::Closure(..) => "closure",
1736 ty::Generator(..) => "generator",
1739 let mut err = struct_span_err!(
1743 "type mismatch in {argument_kind} arguments",
1746 err.span_label(span, "expected due to this");
1748 let found_span = found_span.unwrap_or(span);
1749 err.span_label(found_span, "found signature defined here");
1751 let expected = build_fn_sig_ty(self, expected);
1752 let found = build_fn_sig_ty(self, found);
1754 let (expected_str, found_str) = self.cmp(expected, found);
1756 let signature_kind = format!("{argument_kind} signature");
1757 err.note_expected_found(&signature_kind, expected_str, &signature_kind, found_str);
1759 self.note_conflicting_closure_bounds(cause, &mut err);
1761 if let Some(found_node) = found_node {
1762 hint_missing_borrow(span, found_span, found, expected, found_node, &mut err);
1768 // Add a note if there are two `Fn`-family bounds that have conflicting argument
1769 // requirements, which will always cause a closure to have a type error.
1770 fn note_conflicting_closure_bounds(
1772 cause: &ObligationCauseCode<'tcx>,
1773 err: &mut DiagnosticBuilder<'tcx, ErrorGuaranteed>,
1775 // First, look for an `ExprBindingObligation`, which means we can get
1776 // the unsubstituted predicate list of the called function. And check
1777 // that the predicate that we failed to satisfy is a `Fn`-like trait.
1778 if let ObligationCauseCode::ExprBindingObligation(def_id, _, _, idx) = cause
1779 && let predicates = self.tcx.predicates_of(def_id).instantiate_identity(self.tcx)
1780 && let Some(pred) = predicates.predicates.get(*idx)
1781 && let ty::PredicateKind::Clause(ty::Clause::Trait(trait_pred)) = pred.kind().skip_binder()
1782 && self.tcx.is_fn_trait(trait_pred.def_id())
1785 self.tcx.anonymize_late_bound_regions(pred.kind().rebind(trait_pred.self_ty()));
1786 let expected_substs = self
1788 .anonymize_late_bound_regions(pred.kind().rebind(trait_pred.trait_ref.substs));
1790 // Find another predicate whose self-type is equal to the expected self type,
1791 // but whose substs don't match.
1792 let other_pred = std::iter::zip(&predicates.predicates, &predicates.spans)
1794 .find(|(other_idx, (pred, _))| match pred.kind().skip_binder() {
1795 ty::PredicateKind::Clause(ty::Clause::Trait(trait_pred))
1796 if self.tcx.is_fn_trait(trait_pred.def_id())
1798 // Make sure that the self type matches
1799 // (i.e. constraining this closure)
1801 == self.tcx.anonymize_late_bound_regions(
1802 pred.kind().rebind(trait_pred.self_ty()),
1804 // But the substs don't match (i.e. incompatible args)
1806 != self.tcx.anonymize_late_bound_regions(
1807 pred.kind().rebind(trait_pred.trait_ref.substs),
1814 // If we found one, then it's very likely the cause of the error.
1815 if let Some((_, (_, other_pred_span))) = other_pred {
1818 "closure inferred to have a different signature due to this bound",
1824 fn suggest_fully_qualified_path(
1826 err: &mut Diagnostic,
1831 if let Some(assoc_item) = self.tcx.opt_associated_item(item_def_id) {
1832 if let ty::AssocKind::Const | ty::AssocKind::Type = assoc_item.kind {
1834 "{}s cannot be accessed directly on a `trait`, they can only be \
1835 accessed through a specific `impl`",
1836 assoc_item.kind.as_def_kind().descr(item_def_id)
1838 err.span_suggestion(
1840 "use the fully qualified path to an implementation",
1841 format!("<Type as {}>::{}", self.tcx.def_path_str(trait_ref), assoc_item.name),
1842 Applicability::HasPlaceholders,
1848 /// Adds an async-await specific note to the diagnostic when the future does not implement
1849 /// an auto trait because of a captured type.
1852 /// note: future does not implement `Qux` as this value is used across an await
1853 /// --> $DIR/issue-64130-3-other.rs:17:5
1855 /// LL | let x = Foo;
1856 /// | - has type `Foo`
1857 /// LL | baz().await;
1858 /// | ^^^^^^^^^^^ await occurs here, with `x` maybe used later
1860 /// | - `x` is later dropped here
1863 /// When the diagnostic does not implement `Send` or `Sync` specifically, then the diagnostic
1864 /// is "replaced" with a different message and a more specific error.
1867 /// error: future cannot be sent between threads safely
1868 /// --> $DIR/issue-64130-2-send.rs:21:5
1870 /// LL | fn is_send<T: Send>(t: T) { }
1871 /// | ---- required by this bound in `is_send`
1873 /// LL | is_send(bar());
1874 /// | ^^^^^^^ future returned by `bar` is not send
1876 /// = help: within `impl std::future::Future`, the trait `std::marker::Send` is not
1877 /// implemented for `Foo`
1878 /// note: future is not send as this value is used across an await
1879 /// --> $DIR/issue-64130-2-send.rs:15:5
1881 /// LL | let x = Foo;
1882 /// | - has type `Foo`
1883 /// LL | baz().await;
1884 /// | ^^^^^^^^^^^ await occurs here, with `x` maybe used later
1886 /// | - `x` is later dropped here
1889 /// Returns `true` if an async-await specific note was added to the diagnostic.
1890 #[instrument(level = "debug", skip_all, fields(?obligation.predicate, ?obligation.cause.span))]
1891 fn maybe_note_obligation_cause_for_async_await(
1893 err: &mut Diagnostic,
1894 obligation: &PredicateObligation<'tcx>,
1896 let hir = self.tcx.hir();
1898 // Attempt to detect an async-await error by looking at the obligation causes, looking
1899 // for a generator to be present.
1901 // When a future does not implement a trait because of a captured type in one of the
1902 // generators somewhere in the call stack, then the result is a chain of obligations.
1904 // Given an `async fn` A that calls an `async fn` B which captures a non-send type and that
1905 // future is passed as an argument to a function C which requires a `Send` type, then the
1906 // chain looks something like this:
1908 // - `BuiltinDerivedObligation` with a generator witness (B)
1909 // - `BuiltinDerivedObligation` with a generator (B)
1910 // - `BuiltinDerivedObligation` with `impl std::future::Future` (B)
1911 // - `BuiltinDerivedObligation` with a generator witness (A)
1912 // - `BuiltinDerivedObligation` with a generator (A)
1913 // - `BuiltinDerivedObligation` with `impl std::future::Future` (A)
1914 // - `BindingObligation` with `impl_send (Send requirement)
1916 // The first obligation in the chain is the most useful and has the generator that captured
1917 // the type. The last generator (`outer_generator` below) has information about where the
1918 // bound was introduced. At least one generator should be present for this diagnostic to be
1920 let (mut trait_ref, mut target_ty) = match obligation.predicate.kind().skip_binder() {
1921 ty::PredicateKind::Clause(ty::Clause::Trait(p)) => (Some(p), Some(p.self_ty())),
1924 let mut generator = None;
1925 let mut outer_generator = None;
1926 let mut next_code = Some(obligation.cause.code());
1928 let mut seen_upvar_tys_infer_tuple = false;
1930 while let Some(code) = next_code {
1933 ObligationCauseCode::FunctionArgumentObligation { parent_code, .. } => {
1934 next_code = Some(parent_code);
1936 ObligationCauseCode::ImplDerivedObligation(cause) => {
1937 let ty = cause.derived.parent_trait_pred.skip_binder().self_ty();
1939 parent_trait_ref = ?cause.derived.parent_trait_pred,
1940 self_ty.kind = ?ty.kind(),
1945 ty::Generator(did, ..) => {
1946 generator = generator.or(Some(did));
1947 outer_generator = Some(did);
1949 ty::GeneratorWitness(..) => {}
1950 ty::Tuple(_) if !seen_upvar_tys_infer_tuple => {
1951 // By introducing a tuple of upvar types into the chain of obligations
1952 // of a generator, the first non-generator item is now the tuple itself,
1953 // we shall ignore this.
1955 seen_upvar_tys_infer_tuple = true;
1957 _ if generator.is_none() => {
1958 trait_ref = Some(cause.derived.parent_trait_pred.skip_binder());
1959 target_ty = Some(ty);
1964 next_code = Some(&cause.derived.parent_code);
1966 ObligationCauseCode::DerivedObligation(derived_obligation)
1967 | ObligationCauseCode::BuiltinDerivedObligation(derived_obligation) => {
1968 let ty = derived_obligation.parent_trait_pred.skip_binder().self_ty();
1970 parent_trait_ref = ?derived_obligation.parent_trait_pred,
1971 self_ty.kind = ?ty.kind(),
1975 ty::Generator(did, ..) => {
1976 generator = generator.or(Some(did));
1977 outer_generator = Some(did);
1979 ty::GeneratorWitness(..) => {}
1980 ty::Tuple(_) if !seen_upvar_tys_infer_tuple => {
1981 // By introducing a tuple of upvar types into the chain of obligations
1982 // of a generator, the first non-generator item is now the tuple itself,
1983 // we shall ignore this.
1985 seen_upvar_tys_infer_tuple = true;
1987 _ if generator.is_none() => {
1988 trait_ref = Some(derived_obligation.parent_trait_pred.skip_binder());
1989 target_ty = Some(ty);
1994 next_code = Some(&derived_obligation.parent_code);
2000 // Only continue if a generator was found.
2001 debug!(?generator, ?trait_ref, ?target_ty);
2002 let (Some(generator_did), Some(trait_ref), Some(target_ty)) = (generator, trait_ref, target_ty) else {
2006 let span = self.tcx.def_span(generator_did);
2008 let generator_did_root = self.tcx.typeck_root_def_id(generator_did);
2011 ?generator_did_root,
2012 typeck_results.hir_owner = ?self.typeck_results.as_ref().map(|t| t.hir_owner),
2016 let generator_body = generator_did
2018 .and_then(|def_id| hir.maybe_body_owned_by(def_id))
2019 .map(|body_id| hir.body(body_id));
2020 let mut visitor = AwaitsVisitor::default();
2021 if let Some(body) = generator_body {
2022 visitor.visit_body(body);
2024 debug!(awaits = ?visitor.awaits);
2026 // Look for a type inside the generator interior that matches the target type to get
2028 let target_ty_erased = self.tcx.erase_regions(target_ty);
2029 let ty_matches = |ty| -> bool {
2030 // Careful: the regions for types that appear in the
2031 // generator interior are not generally known, so we
2032 // want to erase them when comparing (and anyway,
2033 // `Send` and other bounds are generally unaffected by
2034 // the choice of region). When erasing regions, we
2035 // also have to erase late-bound regions. This is
2036 // because the types that appear in the generator
2037 // interior generally contain "bound regions" to
2038 // represent regions that are part of the suspended
2039 // generator frame. Bound regions are preserved by
2040 // `erase_regions` and so we must also call
2041 // `erase_late_bound_regions`.
2042 let ty_erased = self.tcx.erase_late_bound_regions(ty);
2043 let ty_erased = self.tcx.erase_regions(ty_erased);
2044 let eq = ty_erased == target_ty_erased;
2045 debug!(?ty_erased, ?target_ty_erased, ?eq);
2049 // Get the typeck results from the infcx if the generator is the function we are currently
2050 // type-checking; otherwise, get them by performing a query. This is needed to avoid
2051 // cycles. If we can't use resolved types because the generator comes from another crate,
2052 // we still provide a targeted error but without all the relevant spans.
2053 let generator_data = match &self.typeck_results {
2054 Some(t) if t.hir_owner.to_def_id() == generator_did_root => GeneratorData::Local(&t),
2055 _ if generator_did.is_local() => {
2056 GeneratorData::Local(self.tcx.typeck(generator_did.expect_local()))
2058 _ if let Some(generator_diag_data) = self.tcx.generator_diagnostic_data(generator_did) => {
2059 GeneratorData::Foreign(generator_diag_data)
2064 let mut interior_or_upvar_span = None;
2066 let from_awaited_ty = generator_data.get_from_await_ty(visitor, hir, ty_matches);
2067 debug!(?from_awaited_ty);
2069 // The generator interior types share the same binders
2070 if let Some(cause) =
2071 generator_data.get_generator_interior_types().skip_binder().iter().find(
2072 |ty::GeneratorInteriorTypeCause { ty, .. }| {
2073 ty_matches(generator_data.get_generator_interior_types().rebind(*ty))
2077 let ty::GeneratorInteriorTypeCause { span, scope_span, yield_span, expr, .. } = cause;
2079 interior_or_upvar_span = Some(GeneratorInteriorOrUpvar::Interior(
2081 Some((*scope_span, *yield_span, *expr, from_awaited_ty)),
2085 if interior_or_upvar_span.is_none() {
2086 interior_or_upvar_span =
2087 generator_data.try_get_upvar_span(&self, generator_did, ty_matches);
2090 if interior_or_upvar_span.is_none() && generator_data.is_foreign() {
2091 interior_or_upvar_span = Some(GeneratorInteriorOrUpvar::Interior(span, None));
2094 debug!(?interior_or_upvar_span);
2095 if let Some(interior_or_upvar_span) = interior_or_upvar_span {
2096 let is_async = self.tcx.generator_is_async(generator_did);
2097 let typeck_results = match generator_data {
2098 GeneratorData::Local(typeck_results) => Some(typeck_results),
2099 GeneratorData::Foreign(_) => None,
2101 self.note_obligation_cause_for_async_await(
2103 interior_or_upvar_span,
2118 /// Unconditionally adds the diagnostic note described in
2119 /// `maybe_note_obligation_cause_for_async_await`'s documentation comment.
2120 #[instrument(level = "debug", skip_all)]
2121 fn note_obligation_cause_for_async_await(
2123 err: &mut Diagnostic,
2124 interior_or_upvar_span: GeneratorInteriorOrUpvar,
2126 outer_generator: Option<DefId>,
2127 trait_pred: ty::TraitPredicate<'tcx>,
2128 target_ty: Ty<'tcx>,
2129 typeck_results: Option<&ty::TypeckResults<'tcx>>,
2130 obligation: &PredicateObligation<'tcx>,
2131 next_code: Option<&ObligationCauseCode<'tcx>>,
2133 let source_map = self.tcx.sess.source_map();
2135 let (await_or_yield, an_await_or_yield) =
2136 if is_async { ("await", "an await") } else { ("yield", "a yield") };
2137 let future_or_generator = if is_async { "future" } else { "generator" };
2139 // Special case the primary error message when send or sync is the trait that was
2141 let hir = self.tcx.hir();
2142 let trait_explanation = if let Some(name @ (sym::Send | sym::Sync)) =
2143 self.tcx.get_diagnostic_name(trait_pred.def_id())
2145 let (trait_name, trait_verb) =
2146 if name == sym::Send { ("`Send`", "sent") } else { ("`Sync`", "shared") };
2149 err.set_primary_message(format!(
2150 "{} cannot be {} between threads safely",
2151 future_or_generator, trait_verb
2154 let original_span = err.span.primary_span().unwrap();
2155 let mut span = MultiSpan::from_span(original_span);
2157 let message = outer_generator
2158 .and_then(|generator_did| {
2159 Some(match self.tcx.generator_kind(generator_did).unwrap() {
2160 GeneratorKind::Gen => format!("generator is not {}", trait_name),
2161 GeneratorKind::Async(AsyncGeneratorKind::Fn) => self
2163 .parent(generator_did)
2165 .map(|parent_did| hir.local_def_id_to_hir_id(parent_did))
2166 .and_then(|parent_hir_id| hir.opt_name(parent_hir_id))
2168 format!("future returned by `{}` is not {}", name, trait_name)
2170 GeneratorKind::Async(AsyncGeneratorKind::Block) => {
2171 format!("future created by async block is not {}", trait_name)
2173 GeneratorKind::Async(AsyncGeneratorKind::Closure) => {
2174 format!("future created by async closure is not {}", trait_name)
2178 .unwrap_or_else(|| format!("{} is not {}", future_or_generator, trait_name));
2180 span.push_span_label(original_span, message);
2183 format!("is not {}", trait_name)
2185 format!("does not implement `{}`", trait_pred.print_modifiers_and_trait_path())
2188 let mut explain_yield = |interior_span: Span,
2190 scope_span: Option<Span>| {
2191 let mut span = MultiSpan::from_span(yield_span);
2192 if let Ok(snippet) = source_map.span_to_snippet(interior_span) {
2193 // #70935: If snippet contains newlines, display "the value" instead
2194 // so that we do not emit complex diagnostics.
2195 let snippet = &format!("`{}`", snippet);
2196 let snippet = if snippet.contains('\n') { "the value" } else { snippet };
2197 // note: future is not `Send` as this value is used across an await
2198 // --> $DIR/issue-70935-complex-spans.rs:13:9
2200 // LL | baz(|| async {
2201 // | ______________-
2204 // LL | | foo(tx.clone());
2206 // | | - ^^^^^^ await occurs here, with value maybe used later
2208 // | has type `closure` which is not `Send`
2209 // note: value is later dropped here
2213 span.push_span_label(
2215 format!("{} occurs here, with {} maybe used later", await_or_yield, snippet),
2217 span.push_span_label(
2219 format!("has type `{}` which {}", target_ty, trait_explanation),
2221 // If available, use the scope span to annotate the drop location.
2222 let mut scope_note = None;
2223 if let Some(scope_span) = scope_span {
2224 let scope_span = source_map.end_point(scope_span);
2226 let msg = format!("{} is later dropped here", snippet);
2227 if source_map.is_multiline(yield_span.between(scope_span)) {
2228 span.push_span_label(scope_span, msg);
2230 scope_note = Some((scope_span, msg));
2236 "{} {} as this value is used across {}",
2237 future_or_generator, trait_explanation, an_await_or_yield
2240 if let Some((span, msg)) = scope_note {
2241 err.span_note(span, &msg);
2245 match interior_or_upvar_span {
2246 GeneratorInteriorOrUpvar::Interior(interior_span, interior_extra_info) => {
2247 if let Some((scope_span, yield_span, expr, from_awaited_ty)) = interior_extra_info {
2248 if let Some(await_span) = from_awaited_ty {
2249 // The type causing this obligation is one being awaited at await_span.
2250 let mut span = MultiSpan::from_span(await_span);
2251 span.push_span_label(
2254 "await occurs here on type `{}`, which {}",
2255 target_ty, trait_explanation
2261 "future {not_trait} as it awaits another future which {not_trait}",
2262 not_trait = trait_explanation
2266 // Look at the last interior type to get a span for the `.await`.
2268 generator_interior_types = ?format_args!(
2269 "{:#?}", typeck_results.as_ref().map(|t| &t.generator_interior_types)
2272 explain_yield(interior_span, yield_span, scope_span);
2275 if let Some(expr_id) = expr {
2276 let expr = hir.expect_expr(expr_id);
2277 debug!("target_ty evaluated from {:?}", expr);
2279 let parent = hir.get_parent_node(expr_id);
2280 if let Some(hir::Node::Expr(e)) = hir.find(parent) {
2281 let parent_span = hir.span(parent);
2282 let parent_did = parent.owner.to_def_id();
2285 // fn foo(&self) -> i32 {}
2288 // ^^^^^^^ a temporary `&T` created inside this method call due to `&self`
2291 let is_region_borrow = if let Some(typeck_results) = typeck_results {
2293 .expr_adjustments(expr)
2295 .any(|adj| adj.is_region_borrow())
2301 // struct Foo(*const u8);
2302 // bar(Foo(std::ptr::null())).await;
2303 // ^^^^^^^^^^^^^^^^^^^^^ raw-ptr `*T` created inside this struct ctor.
2305 debug!(parent_def_kind = ?self.tcx.def_kind(parent_did));
2306 let is_raw_borrow_inside_fn_like_call =
2307 match self.tcx.def_kind(parent_did) {
2308 DefKind::Fn | DefKind::Ctor(..) => target_ty.is_unsafe_ptr(),
2311 if let Some(typeck_results) = typeck_results {
2312 if (typeck_results.is_method_call(e) && is_region_borrow)
2313 || is_raw_borrow_inside_fn_like_call
2317 "consider moving this into a `let` \
2318 binding to create a shorter lived borrow",
2326 GeneratorInteriorOrUpvar::Upvar(upvar_span) => {
2327 // `Some(ref_ty)` if `target_ty` is `&T` and `T` fails to impl `Sync`
2328 let refers_to_non_sync = match target_ty.kind() {
2329 ty::Ref(_, ref_ty, _) => match self.evaluate_obligation(&obligation) {
2330 Ok(eval) if !eval.may_apply() => Some(ref_ty),
2336 let (span_label, span_note) = match refers_to_non_sync {
2337 // if `target_ty` is `&T` and `T` fails to impl `Sync`,
2338 // include suggestions to make `T: Sync` so that `&T: Send`
2341 "has type `{}` which {}, because `{}` is not `Sync`",
2342 target_ty, trait_explanation, ref_ty
2345 "captured value {} because `&` references cannot be sent unless their referent is `Sync`",
2350 format!("has type `{}` which {}", target_ty, trait_explanation),
2351 format!("captured value {}", trait_explanation),
2355 let mut span = MultiSpan::from_span(upvar_span);
2356 span.push_span_label(upvar_span, span_label);
2357 err.span_note(span, &span_note);
2361 // Add a note for the item obligation that remains - normally a note pointing to the
2362 // bound that introduced the obligation (e.g. `T: Send`).
2364 self.note_obligation_cause_code(
2366 obligation.predicate,
2367 obligation.param_env,
2370 &mut Default::default(),
2374 fn note_obligation_cause_code<T>(
2376 err: &mut Diagnostic,
2378 param_env: ty::ParamEnv<'tcx>,
2379 cause_code: &ObligationCauseCode<'tcx>,
2380 obligated_types: &mut Vec<Ty<'tcx>>,
2381 seen_requirements: &mut FxHashSet<DefId>,
2383 T: ToPredicate<'tcx>,
2386 let predicate = predicate.to_predicate(tcx);
2388 ObligationCauseCode::ExprAssignable
2389 | ObligationCauseCode::MatchExpressionArm { .. }
2390 | ObligationCauseCode::Pattern { .. }
2391 | ObligationCauseCode::IfExpression { .. }
2392 | ObligationCauseCode::IfExpressionWithNoElse
2393 | ObligationCauseCode::MainFunctionType
2394 | ObligationCauseCode::StartFunctionType
2395 | ObligationCauseCode::IntrinsicType
2396 | ObligationCauseCode::MethodReceiver
2397 | ObligationCauseCode::ReturnNoExpression
2398 | ObligationCauseCode::UnifyReceiver(..)
2399 | ObligationCauseCode::OpaqueType
2400 | ObligationCauseCode::MiscObligation
2401 | ObligationCauseCode::WellFormed(..)
2402 | ObligationCauseCode::MatchImpl(..)
2403 | ObligationCauseCode::ReturnType
2404 | ObligationCauseCode::ReturnValue(_)
2405 | ObligationCauseCode::BlockTailExpression(_)
2406 | ObligationCauseCode::AwaitableExpr(_)
2407 | ObligationCauseCode::ForLoopIterator
2408 | ObligationCauseCode::QuestionMark
2409 | ObligationCauseCode::CheckAssociatedTypeBounds { .. }
2410 | ObligationCauseCode::LetElse
2411 | ObligationCauseCode::BinOp { .. }
2412 | ObligationCauseCode::AscribeUserTypeProvePredicate(..)
2413 | ObligationCauseCode::RustCall => {}
2414 ObligationCauseCode::SliceOrArrayElem => {
2415 err.note("slice and array elements must have `Sized` type");
2417 ObligationCauseCode::TupleElem => {
2418 err.note("only the last element of a tuple may have a dynamically sized type");
2420 ObligationCauseCode::ProjectionWf(data) => {
2421 err.note(&format!("required so that the projection `{data}` is well-formed"));
2423 ObligationCauseCode::ReferenceOutlivesReferent(ref_ty) => {
2425 "required so that reference `{ref_ty}` does not outlive its referent"
2428 ObligationCauseCode::ObjectTypeBound(object_ty, region) => {
2430 "required so that the lifetime bound of `{}` for `{}` is satisfied",
2434 ObligationCauseCode::ItemObligation(_)
2435 | ObligationCauseCode::ExprItemObligation(..) => {
2436 // We hold the `DefId` of the item introducing the obligation, but displaying it
2437 // doesn't add user usable information. It always point at an associated item.
2439 ObligationCauseCode::BindingObligation(item_def_id, span)
2440 | ObligationCauseCode::ExprBindingObligation(item_def_id, span, ..) => {
2441 let item_name = tcx.def_path_str(item_def_id);
2442 let short_item_name = with_forced_trimmed_paths!(tcx.def_path_str(item_def_id));
2443 let mut multispan = MultiSpan::from(span);
2444 let sm = tcx.sess.source_map();
2445 if let Some(ident) = tcx.opt_item_ident(item_def_id) {
2447 match (sm.lookup_line(ident.span.hi()), sm.lookup_line(span.lo())) {
2448 (Ok(l), Ok(r)) => l.line == r.line,
2451 if ident.span.is_visible(sm) && !ident.span.overlaps(span) && !same_line {
2452 multispan.push_span_label(ident.span, "required by a bound in this");
2455 let descr = format!("required by a bound in `{item_name}`");
2456 if span.is_visible(sm) {
2457 let msg = format!("required by this bound in `{short_item_name}`");
2458 multispan.push_span_label(span, msg);
2459 err.span_note(multispan, &descr);
2461 err.span_note(tcx.def_span(item_def_id), &descr);
2464 ObligationCauseCode::ObjectCastObligation(concrete_ty, object_ty) => {
2466 "required for the cast from `{}` to the object type `{}`",
2467 self.ty_to_string(concrete_ty),
2468 self.ty_to_string(object_ty)
2471 ObligationCauseCode::Coercion { source: _, target } => {
2472 err.note(&format!("required by cast to type `{}`", self.ty_to_string(target)));
2474 ObligationCauseCode::RepeatElementCopy { is_const_fn } => {
2476 "the `Copy` trait is required because this value will be copied for each element of the array",
2481 "consider creating a new `const` item and initializing it with the result \
2482 of the function call to be used in the repeat position, like \
2483 `const VAL: Type = const_fn();` and `let x = [VAL; 42];`",
2487 if self.tcx.sess.is_nightly_build() && is_const_fn {
2489 "create an inline `const` block, see RFC #2920 \
2490 <https://github.com/rust-lang/rfcs/pull/2920> for more information",
2494 ObligationCauseCode::VariableType(hir_id) => {
2495 let parent_node = self.tcx.hir().get_parent_node(hir_id);
2496 match self.tcx.hir().find(parent_node) {
2497 Some(Node::Local(hir::Local {
2498 init: Some(hir::Expr { kind: hir::ExprKind::Index(_, _), span, .. }),
2501 // When encountering an assignment of an unsized trait, like
2502 // `let x = ""[..];`, provide a suggestion to borrow the initializer in
2503 // order to use have a slice instead.
2504 err.span_suggestion_verbose(
2505 span.shrink_to_lo(),
2506 "consider borrowing here",
2508 Applicability::MachineApplicable,
2510 err.note("all local variables must have a statically known size");
2512 Some(Node::Param(param)) => {
2513 err.span_suggestion_verbose(
2514 param.ty_span.shrink_to_lo(),
2515 "function arguments must have a statically known size, borrowed types \
2516 always have a known size",
2518 Applicability::MachineApplicable,
2522 err.note("all local variables must have a statically known size");
2525 if !self.tcx.features().unsized_locals {
2526 err.help("unsized locals are gated as an unstable feature");
2529 ObligationCauseCode::SizedArgumentType(sp) => {
2530 if let Some(span) = sp {
2531 err.span_suggestion_verbose(
2532 span.shrink_to_lo(),
2533 "function arguments must have a statically known size, borrowed types \
2534 always have a known size",
2536 Applicability::MachineApplicable,
2539 err.note("all function arguments must have a statically known size");
2541 if tcx.sess.opts.unstable_features.is_nightly_build()
2542 && !self.tcx.features().unsized_fn_params
2544 err.help("unsized fn params are gated as an unstable feature");
2547 ObligationCauseCode::SizedReturnType => {
2548 err.note("the return type of a function must have a statically known size");
2550 ObligationCauseCode::SizedYieldType => {
2551 err.note("the yield type of a generator must have a statically known size");
2553 ObligationCauseCode::SizedBoxType => {
2554 err.note("the type of a box expression must have a statically known size");
2556 ObligationCauseCode::AssignmentLhsSized => {
2557 err.note("the left-hand-side of an assignment must have a statically known size");
2559 ObligationCauseCode::TupleInitializerSized => {
2560 err.note("tuples must have a statically known size to be initialized");
2562 ObligationCauseCode::StructInitializerSized => {
2563 err.note("structs must have a statically known size to be initialized");
2565 ObligationCauseCode::FieldSized { adt_kind: ref item, last, span } => {
2567 AdtKind::Struct => {
2570 "the last field of a packed struct may only have a \
2571 dynamically sized type if it does not need drop to be run",
2575 "only the last field of a struct may have a dynamically sized type",
2580 err.note("no field of a union may have a dynamically sized type");
2583 err.note("no field of an enum variant may have a dynamically sized type");
2586 err.help("change the field's type to have a statically known size");
2587 err.span_suggestion(
2588 span.shrink_to_lo(),
2589 "borrowed types always have a statically known size",
2591 Applicability::MachineApplicable,
2593 err.multipart_suggestion(
2594 "the `Box` type always has a statically known size and allocates its contents \
2597 (span.shrink_to_lo(), "Box<".to_string()),
2598 (span.shrink_to_hi(), ">".to_string()),
2600 Applicability::MachineApplicable,
2603 ObligationCauseCode::ConstSized => {
2604 err.note("constant expressions must have a statically known size");
2606 ObligationCauseCode::InlineAsmSized => {
2607 err.note("all inline asm arguments must have a statically known size");
2609 ObligationCauseCode::ConstPatternStructural => {
2610 err.note("constants used for pattern-matching must derive `PartialEq` and `Eq`");
2612 ObligationCauseCode::SharedStatic => {
2613 err.note("shared static variables must have a type that implements `Sync`");
2615 ObligationCauseCode::BuiltinDerivedObligation(ref data) => {
2616 let parent_trait_ref = self.resolve_vars_if_possible(data.parent_trait_pred);
2617 let ty = parent_trait_ref.skip_binder().self_ty();
2618 if parent_trait_ref.references_error() {
2619 // NOTE(eddyb) this was `.cancel()`, but `err`
2620 // is borrowed, so we can't fully defuse it.
2621 err.downgrade_to_delayed_bug();
2625 // If the obligation for a tuple is set directly by a Generator or Closure,
2626 // then the tuple must be the one containing capture types.
2627 let is_upvar_tys_infer_tuple = if !matches!(ty.kind(), ty::Tuple(..)) {
2630 if let ObligationCauseCode::BuiltinDerivedObligation(data) = &*data.parent_code
2632 let parent_trait_ref =
2633 self.resolve_vars_if_possible(data.parent_trait_pred);
2634 let nested_ty = parent_trait_ref.skip_binder().self_ty();
2635 matches!(nested_ty.kind(), ty::Generator(..))
2636 || matches!(nested_ty.kind(), ty::Closure(..))
2642 let identity_future = tcx.require_lang_item(LangItem::IdentityFuture, None);
2644 // Don't print the tuple of capture types
2646 if !is_upvar_tys_infer_tuple {
2647 let msg = format!("required because it appears within the type `{}`", ty);
2649 ty::Adt(def, _) => match self.tcx.opt_item_ident(def.did()) {
2650 Some(ident) => err.span_note(ident.span, &msg),
2651 None => err.note(&msg),
2653 ty::Opaque(def_id, _) => {
2654 // Avoid printing the future from `core::future::identity_future`, it's not helpful
2655 if tcx.parent(*def_id) == identity_future {
2659 // If the previous type is `identity_future`, this is the future generated by the body of an async function.
2660 // Avoid printing it twice (it was already printed in the `ty::Generator` arm below).
2661 let is_future = tcx.ty_is_opaque_future(ty);
2665 "note_obligation_cause_code: check for async fn"
2668 && obligated_types.last().map_or(false, |ty| match ty.kind() {
2669 ty::Generator(last_def_id, ..) => {
2670 tcx.generator_is_async(*last_def_id)
2677 err.span_note(self.tcx.def_span(def_id), &msg)
2679 ty::GeneratorWitness(bound_tys) => {
2680 use std::fmt::Write;
2682 // FIXME: this is kind of an unusual format for rustc, can we make it more clear?
2683 // Maybe we should just remove this note altogether?
2684 // FIXME: only print types which don't meet the trait requirement
2686 "required because it captures the following types: ".to_owned();
2687 for ty in bound_tys.skip_binder() {
2688 write!(msg, "`{}`, ", ty).unwrap();
2690 err.note(msg.trim_end_matches(", "))
2692 ty::Generator(def_id, _, _) => {
2693 let sp = self.tcx.def_span(def_id);
2695 // Special-case this to say "async block" instead of `[static generator]`.
2696 let kind = tcx.generator_kind(def_id).unwrap().descr();
2699 &format!("required because it's used within this {}", kind),
2702 ty::Closure(def_id, _) => err.span_note(
2703 self.tcx.def_span(def_id),
2704 &format!("required because it's used within this closure"),
2706 _ => err.note(&msg),
2711 obligated_types.push(ty);
2713 let parent_predicate = parent_trait_ref;
2714 if !self.is_recursive_obligation(obligated_types, &data.parent_code) {
2715 // #74711: avoid a stack overflow
2716 ensure_sufficient_stack(|| {
2717 self.note_obligation_cause_code(
2727 ensure_sufficient_stack(|| {
2728 self.note_obligation_cause_code(
2732 cause_code.peel_derives(),
2739 ObligationCauseCode::ImplDerivedObligation(ref data) => {
2740 let mut parent_trait_pred =
2741 self.resolve_vars_if_possible(data.derived.parent_trait_pred);
2742 parent_trait_pred.remap_constness_diag(param_env);
2743 let parent_def_id = parent_trait_pred.def_id();
2744 let (self_ty, file) =
2745 self.tcx.short_ty_string(parent_trait_pred.skip_binder().self_ty());
2747 "required for `{self_ty}` to implement `{}`",
2748 parent_trait_pred.print_modifiers_and_trait_path()
2750 let mut is_auto_trait = false;
2751 match self.tcx.hir().get_if_local(data.impl_def_id) {
2752 Some(Node::Item(hir::Item {
2753 kind: hir::ItemKind::Trait(is_auto, ..),
2757 // FIXME: we should do something else so that it works even on crate foreign
2759 is_auto_trait = matches!(is_auto, hir::IsAuto::Yes);
2760 err.span_note(ident.span, &msg)
2762 Some(Node::Item(hir::Item {
2763 kind: hir::ItemKind::Impl(hir::Impl { of_trait, self_ty, .. }),
2766 let mut spans = Vec::with_capacity(2);
2767 if let Some(trait_ref) = of_trait {
2768 spans.push(trait_ref.path.span);
2770 spans.push(self_ty.span);
2771 err.span_note(spans, &msg)
2773 _ => err.note(&msg),
2776 if let Some(file) = file {
2778 "the full type name has been written to '{}'",
2782 let mut parent_predicate = parent_trait_pred;
2783 let mut data = &data.derived;
2785 seen_requirements.insert(parent_def_id);
2787 // We don't want to point at the ADT saying "required because it appears within
2788 // the type `X`", like we would otherwise do in test `supertrait-auto-trait.rs`.
2789 while let ObligationCauseCode::BuiltinDerivedObligation(derived) =
2792 let child_trait_ref =
2793 self.resolve_vars_if_possible(derived.parent_trait_pred);
2794 let child_def_id = child_trait_ref.def_id();
2795 if seen_requirements.insert(child_def_id) {
2799 parent_predicate = child_trait_ref.to_predicate(tcx);
2800 parent_trait_pred = child_trait_ref;
2803 while let ObligationCauseCode::ImplDerivedObligation(child) = &*data.parent_code {
2804 // Skip redundant recursive obligation notes. See `ui/issue-20413.rs`.
2805 let child_trait_pred =
2806 self.resolve_vars_if_possible(child.derived.parent_trait_pred);
2807 let child_def_id = child_trait_pred.def_id();
2808 if seen_requirements.insert(child_def_id) {
2812 data = &child.derived;
2813 parent_predicate = child_trait_pred.to_predicate(tcx);
2814 parent_trait_pred = child_trait_pred;
2818 "{} redundant requirement{} hidden",
2822 let (self_ty, file) =
2823 self.tcx.short_ty_string(parent_trait_pred.skip_binder().self_ty());
2825 "required for `{self_ty}` to implement `{}`",
2826 parent_trait_pred.print_modifiers_and_trait_path()
2828 if let Some(file) = file {
2830 "the full type name has been written to '{}'",
2835 // #74711: avoid a stack overflow
2836 ensure_sufficient_stack(|| {
2837 self.note_obligation_cause_code(
2847 ObligationCauseCode::DerivedObligation(ref data) => {
2848 let parent_trait_ref = self.resolve_vars_if_possible(data.parent_trait_pred);
2849 let parent_predicate = parent_trait_ref;
2850 // #74711: avoid a stack overflow
2851 ensure_sufficient_stack(|| {
2852 self.note_obligation_cause_code(
2862 ObligationCauseCode::FunctionArgumentObligation {
2867 self.function_argument_obligation(
2875 ensure_sufficient_stack(|| {
2876 self.note_obligation_cause_code(
2886 ObligationCauseCode::CompareImplItemObligation { trait_item_def_id, kind, .. } => {
2887 let item_name = self.tcx.item_name(trait_item_def_id);
2889 "the requirement `{predicate}` appears on the `impl`'s {kind} \
2890 `{item_name}` but not on the corresponding trait's {kind}",
2894 .opt_item_ident(trait_item_def_id)
2896 .unwrap_or_else(|| self.tcx.def_span(trait_item_def_id));
2897 let mut assoc_span: MultiSpan = sp.into();
2898 assoc_span.push_span_label(
2900 format!("this trait's {kind} doesn't have the requirement `{predicate}`"),
2902 if let Some(ident) = self
2904 .opt_associated_item(trait_item_def_id)
2905 .and_then(|i| self.tcx.opt_item_ident(i.container_id(self.tcx)))
2907 assoc_span.push_span_label(ident.span, "in this trait");
2909 err.span_note(assoc_span, &msg);
2911 ObligationCauseCode::TrivialBound => {
2912 err.help("see issue #48214");
2913 if tcx.sess.opts.unstable_features.is_nightly_build() {
2914 err.help("add `#![feature(trivial_bounds)]` to the crate attributes to enable");
2917 ObligationCauseCode::OpaqueReturnType(expr_info) => {
2918 if let Some((expr_ty, expr_span)) = expr_info {
2919 let expr_ty = with_forced_trimmed_paths!(self.ty_to_string(expr_ty));
2922 format!("return type was inferred to be `{expr_ty}` here"),
2930 level = "debug", skip(self, err), fields(trait_pred.self_ty = ?trait_pred.self_ty())
2932 fn suggest_await_before_try(
2934 err: &mut Diagnostic,
2935 obligation: &PredicateObligation<'tcx>,
2936 trait_pred: ty::PolyTraitPredicate<'tcx>,
2939 let body_hir_id = obligation.cause.body_id;
2940 let item_id = self.tcx.hir().get_parent_node(body_hir_id);
2942 if let Some(body_id) =
2943 self.tcx.hir().maybe_body_owned_by(self.tcx.hir().local_def_id(item_id))
2945 let body = self.tcx.hir().body(body_id);
2946 if let Some(hir::GeneratorKind::Async(_)) = body.generator_kind {
2947 let future_trait = self.tcx.require_lang_item(LangItem::Future, None);
2949 let self_ty = self.resolve_vars_if_possible(trait_pred.self_ty());
2950 let impls_future = self.type_implements_trait(
2952 [self.tcx.erase_late_bound_regions(self_ty)],
2953 obligation.param_env,
2955 if !impls_future.must_apply_modulo_regions() {
2959 let item_def_id = self.tcx.associated_item_def_ids(future_trait)[0];
2960 // `<T as Future>::Output`
2961 let projection_ty = trait_pred.map_bound(|trait_pred| {
2962 self.tcx.mk_projection(
2964 // Future::Output has no substs
2965 self.tcx.mk_substs_trait(trait_pred.self_ty(), []),
2968 let InferOk { value: projection_ty, .. } =
2969 self.at(&obligation.cause, obligation.param_env).normalize(projection_ty);
2972 normalized_projection_type = ?self.resolve_vars_if_possible(projection_ty)
2974 let try_obligation = self.mk_trait_obligation_with_new_self_ty(
2975 obligation.param_env,
2976 trait_pred.map_bound(|trait_pred| (trait_pred, projection_ty.skip_binder())),
2978 debug!(try_trait_obligation = ?try_obligation);
2979 if self.predicate_may_hold(&try_obligation)
2980 && let Ok(snippet) = self.tcx.sess.source_map().span_to_snippet(span)
2981 && snippet.ends_with('?')
2983 err.span_suggestion_verbose(
2984 span.with_hi(span.hi() - BytePos(1)).shrink_to_hi(),
2985 "consider `await`ing on the `Future`",
2987 Applicability::MaybeIncorrect,
2994 fn suggest_floating_point_literal(
2996 obligation: &PredicateObligation<'tcx>,
2997 err: &mut Diagnostic,
2998 trait_ref: &ty::PolyTraitRef<'tcx>,
3000 let rhs_span = match obligation.cause.code() {
3001 ObligationCauseCode::BinOp { rhs_span: Some(span), is_lit, .. } if *is_lit => span,
3004 if let ty::Float(_) = trait_ref.skip_binder().self_ty().kind()
3005 && let ty::Infer(InferTy::IntVar(_)) = trait_ref.skip_binder().substs.type_at(1).kind()
3007 err.span_suggestion_verbose(
3008 rhs_span.shrink_to_hi(),
3009 "consider using a floating-point literal by writing it with `.0`",
3011 Applicability::MaybeIncorrect,
3018 obligation: &PredicateObligation<'tcx>,
3019 err: &mut Diagnostic,
3020 trait_pred: ty::PolyTraitPredicate<'tcx>,
3022 let Some(diagnostic_name) = self.tcx.get_diagnostic_name(trait_pred.def_id()) else {
3025 let (adt, substs) = match trait_pred.skip_binder().self_ty().kind() {
3026 ty::Adt(adt, substs) if adt.did().is_local() => (adt, substs),
3030 let is_derivable_trait = match diagnostic_name {
3031 sym::Default => !adt.is_enum(),
3032 sym::PartialEq | sym::PartialOrd => {
3033 let rhs_ty = trait_pred.skip_binder().trait_ref.substs.type_at(1);
3034 trait_pred.skip_binder().self_ty() == rhs_ty
3036 sym::Eq | sym::Ord | sym::Clone | sym::Copy | sym::Hash | sym::Debug => true,
3039 is_derivable_trait &&
3040 // Ensure all fields impl the trait.
3041 adt.all_fields().all(|field| {
3042 let field_ty = field.ty(self.tcx, substs);
3043 let trait_substs = match diagnostic_name {
3044 sym::PartialEq | sym::PartialOrd => {
3049 let trait_pred = trait_pred.map_bound_ref(|tr| ty::TraitPredicate {
3050 trait_ref: self.tcx.mk_trait_ref(
3051 trait_pred.def_id(),
3052 [field_ty].into_iter().chain(trait_substs),
3056 let field_obl = Obligation::new(
3058 obligation.cause.clone(),
3059 obligation.param_env,
3062 self.predicate_must_hold_modulo_regions(&field_obl)
3066 err.span_suggestion_verbose(
3067 self.tcx.def_span(adt.did()).shrink_to_lo(),
3069 "consider annotating `{}` with `#[derive({})]`",
3070 trait_pred.skip_binder().self_ty(),
3073 format!("#[derive({})]\n", diagnostic_name),
3074 Applicability::MaybeIncorrect,
3079 fn suggest_dereferencing_index(
3081 obligation: &PredicateObligation<'tcx>,
3082 err: &mut Diagnostic,
3083 trait_pred: ty::PolyTraitPredicate<'tcx>,
3085 if let ObligationCauseCode::ImplDerivedObligation(_) = obligation.cause.code()
3086 && self.tcx.is_diagnostic_item(sym::SliceIndex, trait_pred.skip_binder().trait_ref.def_id)
3087 && let ty::Slice(_) = trait_pred.skip_binder().trait_ref.substs.type_at(1).kind()
3088 && let ty::Ref(_, inner_ty, _) = trait_pred.skip_binder().self_ty().kind()
3089 && let ty::Uint(ty::UintTy::Usize) = inner_ty.kind()
3091 err.span_suggestion_verbose(
3092 obligation.cause.span.shrink_to_lo(),
3093 "dereference this index",
3095 Applicability::MachineApplicable,
3099 fn function_argument_obligation(
3102 err: &mut Diagnostic,
3103 parent_code: &ObligationCauseCode<'tcx>,
3104 param_env: ty::ParamEnv<'tcx>,
3105 predicate: ty::Predicate<'tcx>,
3109 let hir = tcx.hir();
3110 if let Some(Node::Expr(expr)) = hir.find(arg_hir_id) {
3111 let parent_id = hir.get_parent_item(arg_hir_id);
3112 let typeck_results: &TypeckResults<'tcx> = match &self.typeck_results {
3113 Some(t) if t.hir_owner == parent_id => t,
3114 _ => self.tcx.typeck(parent_id.def_id),
3116 if let hir::Expr { kind: hir::ExprKind::Block(..), .. } = expr {
3117 let expr = expr.peel_blocks();
3118 let ty = typeck_results.expr_ty_adjusted_opt(expr).unwrap_or(tcx.ty_error());
3119 let span = expr.span;
3120 if Some(span) != err.span.primary_span() {
3123 if ty.references_error() {
3126 let ty = with_forced_trimmed_paths!(self.ty_to_string(ty));
3127 format!("this tail expression is of type `{ty}`")
3133 // FIXME: visit the ty to see if there's any closure involved, and if there is,
3134 // check whether its evaluated return type is the same as the one corresponding
3135 // to an associated type (as seen from `trait_pred`) in the predicate. Like in
3136 // trait_pred `S: Sum<<Self as Iterator>::Item>` and predicate `i32: Sum<&()>`
3137 let mut type_diffs = vec![];
3139 if let ObligationCauseCode::ExprBindingObligation(def_id, _, _, idx) = parent_code.deref()
3140 && let predicates = self.tcx.predicates_of(def_id).instantiate_identity(self.tcx)
3141 && let Some(pred) = predicates.predicates.get(*idx)
3142 && let Ok(trait_pred) = pred.kind().try_map_bound(|pred| match pred {
3143 ty::PredicateKind::Clause(ty::Clause::Trait(trait_pred)) => Ok(trait_pred),
3147 let mut c = CollectAllMismatches {
3152 if let Ok(trait_predicate) = predicate.kind().try_map_bound(|pred| match pred {
3153 ty::PredicateKind::Clause(ty::Clause::Trait(trait_pred)) => Ok(trait_pred),
3156 if let Ok(_) = c.relate(trait_pred, trait_predicate) {
3157 type_diffs = c.errors;
3161 if let hir::ExprKind::Path(hir::QPath::Resolved(None, path)) = expr.kind
3162 && let hir::Path { res: hir::def::Res::Local(hir_id), .. } = path
3163 && let Some(hir::Node::Pat(binding)) = self.tcx.hir().find(*hir_id)
3164 && let parent_hir_id = self.tcx.hir().get_parent_node(binding.hir_id)
3165 && let Some(hir::Node::Local(local)) = self.tcx.hir().find(parent_hir_id)
3166 && let Some(binding_expr) = local.init
3168 // If the expression we're calling on is a binding, we want to point at the
3169 // `let` when talking about the type. Otherwise we'll point at every part
3170 // of the method chain with the type.
3171 self.point_at_chain(binding_expr, typeck_results, type_diffs, param_env, err);
3173 self.point_at_chain(expr, typeck_results, type_diffs, param_env, err);
3176 let call_node = hir.find(call_hir_id);
3177 if let Some(Node::Expr(hir::Expr {
3178 kind: hir::ExprKind::MethodCall(path, rcvr, ..), ..
3181 if Some(rcvr.span) == err.span.primary_span() {
3182 err.replace_span_with(path.ident.span);
3185 if let Some(Node::Expr(hir::Expr {
3187 hir::ExprKind::Call(hir::Expr { span, .. }, _)
3188 | hir::ExprKind::MethodCall(hir::PathSegment { ident: Ident { span, .. }, .. }, ..),
3190 })) = hir.find(call_hir_id)
3192 if Some(*span) != err.span.primary_span() {
3193 err.span_label(*span, "required by a bound introduced by this call");
3200 expr: &hir::Expr<'_>,
3201 typeck_results: &TypeckResults<'tcx>,
3202 type_diffs: Vec<TypeError<'tcx>>,
3203 param_env: ty::ParamEnv<'tcx>,
3204 err: &mut Diagnostic,
3206 let mut primary_spans = vec![];
3207 let mut span_labels = vec![];
3211 let mut assocs = vec![];
3212 // We still want to point at the different methods even if there hasn't
3213 // been a change of assoc type.
3214 let mut call_spans = vec![];
3215 let mut expr = expr;
3216 let mut prev_ty = self.resolve_vars_if_possible(
3217 typeck_results.expr_ty_adjusted_opt(expr).unwrap_or(tcx.ty_error()),
3219 while let hir::ExprKind::MethodCall(_path_segment, rcvr_expr, _args, span) = expr.kind {
3220 // Point at every method call in the chain with the resulting type.
3221 // vec![1, 2, 3].iter().map(mapper).sum<i32>()
3222 // ^^^^^^ ^^^^^^^^^^^
3224 let mut assocs_in_this_method = Vec::with_capacity(type_diffs.len());
3225 call_spans.push(span);
3227 let ocx = ObligationCtxt::new_in_snapshot(self.infcx);
3228 for diff in &type_diffs {
3229 let Sorts(expected_found) = diff else { continue; };
3230 let ty::Projection(proj) = expected_found.expected.kind() else { continue; };
3233 TypeVariableOrigin { kind: TypeVariableOriginKind::TypeInference, span };
3234 let trait_def_id = proj.trait_def_id(self.tcx);
3235 // Make `Self` be equivalent to the type of the call chain
3236 // expression we're looking at now, so that we can tell what
3237 // for example `Iterator::Item` is at this point in the chain.
3238 let substs = InternalSubsts::for_item(self.tcx, trait_def_id, |param, _| {
3240 ty::GenericParamDefKind::Type { .. } => {
3241 if param.index == 0 {
3242 return prev_ty.into();
3245 ty::GenericParamDefKind::Lifetime
3246 | ty::GenericParamDefKind::Const { .. } => {}
3248 self.var_for_def(span, param)
3250 // This will hold the resolved type of the associated type, if the
3251 // current expression implements the trait that associated type is
3252 // in. For example, this would be what `Iterator::Item` is here.
3253 let ty_var = self.infcx.next_ty_var(origin);
3254 // This corresponds to `<ExprTy as Iterator>::Item = _`.
3255 let trait_ref = ty::Binder::dummy(ty::PredicateKind::Clause(
3256 ty::Clause::Projection(ty::ProjectionPredicate {
3257 projection_ty: ty::ProjectionTy { substs, item_def_id: proj.item_def_id },
3258 term: ty_var.into(),
3261 // Add `<ExprTy as Iterator>::Item = _` obligation.
3262 ocx.register_obligation(Obligation::misc(
3269 if ocx.select_where_possible().is_empty() {
3270 // `ty_var` now holds the type that `Item` is for `ExprTy`.
3271 let ty_var = self.resolve_vars_if_possible(ty_var);
3272 assocs_in_this_method.push(Some((span, (proj.item_def_id, ty_var))));
3274 // `<ExprTy as Iterator>` didn't select, so likely we've
3275 // reached the end of the iterator chain, like the originating
3277 // Keep the space consistent for later zipping.
3278 assocs_in_this_method.push(None);
3281 assocs.push(assocs_in_this_method);
3282 prev_ty = self.resolve_vars_if_possible(
3283 typeck_results.expr_ty_adjusted_opt(expr).unwrap_or(tcx.ty_error()),
3286 if let hir::ExprKind::Path(hir::QPath::Resolved(None, path)) = expr.kind
3287 && let hir::Path { res: hir::def::Res::Local(hir_id), .. } = path
3288 && let Some(hir::Node::Pat(binding)) = self.tcx.hir().find(*hir_id)
3289 && let parent_hir_id = self.tcx.hir().get_parent_node(binding.hir_id)
3290 && let Some(hir::Node::Local(local)) = self.tcx.hir().find(parent_hir_id)
3291 && let Some(binding_expr) = local.init
3293 // We've reached the root of the method call chain and it is a
3294 // binding. Get the binding creation and try to continue the chain.
3295 expr = binding_expr;
3298 // We want the type before deref coercions, otherwise we talk about `&[_]`
3299 // instead of `Vec<_>`.
3300 if let Some(ty) = typeck_results.expr_ty_opt(expr) {
3301 let ty = with_forced_trimmed_paths!(self.ty_to_string(ty));
3302 // Point at the root expression
3303 // vec![1, 2, 3].iter().map(mapper).sum<i32>()
3305 span_labels.push((expr.span, format!("this expression has type `{ty}`")));
3307 // Only show this if it is not a "trivial" expression (not a method
3308 // chain) and there are associated types to talk about.
3309 let mut assocs = assocs.into_iter().peekable();
3310 while let Some(assocs_in_method) = assocs.next() {
3311 let Some(prev_assoc_in_method) = assocs.peek() else {
3312 for entry in assocs_in_method {
3313 let Some((span, (assoc, ty))) = entry else { continue; };
3314 if type_diffs.iter().any(|diff| {
3315 let Sorts(expected_found) = diff else { return false; };
3316 self.can_eq(param_env, expected_found.found, ty).is_ok()
3318 // FIXME: this doesn't quite work for `Iterator::collect`
3319 // because we have `Vec<i32>` and `()`, but we'd want `i32`
3320 // to point at the `.into_iter()` call, but as long as we
3321 // still point at the other method calls that might have
3322 // introduced the issue, this is fine for now.
3323 primary_spans.push(span);
3327 with_forced_trimmed_paths!(format!(
3328 "`{}` is `{ty}` here",
3329 self.tcx.def_path_str(assoc),
3335 for (entry, prev_entry) in
3336 assocs_in_method.into_iter().zip(prev_assoc_in_method.into_iter())
3338 match (entry, prev_entry) {
3339 (Some((span, (assoc, ty))), Some((_, (_, prev_ty)))) => {
3340 let ty_str = with_forced_trimmed_paths!(self.ty_to_string(ty));
3342 let assoc = with_forced_trimmed_paths!(self.tcx.def_path_str(assoc));
3344 if type_diffs.iter().any(|diff| {
3345 let Sorts(expected_found) = diff else { return false; };
3346 self.can_eq(param_env, expected_found.found, ty).is_ok()
3348 primary_spans.push(span);
3351 .push((span, format!("`{assoc}` changed to `{ty_str}` here")));
3353 span_labels.push((span, format!("`{assoc}` remains `{ty_str}` here")));
3356 (Some((span, (assoc, ty))), None) => {
3359 with_forced_trimmed_paths!(format!(
3360 "`{}` is `{}` here",
3361 self.tcx.def_path_str(assoc),
3362 self.ty_to_string(ty),
3366 (None, Some(_)) | (None, None) => {}
3370 for span in call_spans {
3371 if span_labels.iter().find(|(s, _)| *s == span).is_none() {
3372 // Ensure we are showing the entire chain, even if the assoc types
3374 span_labels.push((span, String::new()));
3377 if !primary_spans.is_empty() {
3378 let mut multi_span: MultiSpan = primary_spans.into();
3379 for (span, label) in span_labels {
3380 multi_span.push_span_label(span, label);
3385 "the method call chain might not have had the expected \
3393 /// Add a hint to add a missing borrow or remove an unnecessary one.
3394 fn hint_missing_borrow<'tcx>(
3399 found_node: Node<'_>,
3400 err: &mut Diagnostic,
3402 let found_args = match found.kind() {
3403 ty::FnPtr(f) => f.inputs().skip_binder().iter(),
3405 span_bug!(span, "found was converted to a FnPtr above but is now {:?}", kind)
3408 let expected_args = match expected.kind() {
3409 ty::FnPtr(f) => f.inputs().skip_binder().iter(),
3411 span_bug!(span, "expected was converted to a FnPtr above but is now {:?}", kind)
3415 let fn_decl = found_node
3417 .unwrap_or_else(|| span_bug!(found_span, "found node must be a function"));
3419 let arg_spans = fn_decl.inputs.iter().map(|ty| ty.span);
3421 fn get_deref_type_and_refs<'tcx>(mut ty: Ty<'tcx>) -> (Ty<'tcx>, usize) {
3424 while let ty::Ref(_, new_ty, _) = ty.kind() {
3432 let mut to_borrow = Vec::new();
3433 let mut remove_borrow = Vec::new();
3435 for ((found_arg, expected_arg), arg_span) in found_args.zip(expected_args).zip(arg_spans) {
3436 let (found_ty, found_refs) = get_deref_type_and_refs(*found_arg);
3437 let (expected_ty, expected_refs) = get_deref_type_and_refs(*expected_arg);
3439 if found_ty == expected_ty {
3440 if found_refs < expected_refs {
3441 to_borrow.push((arg_span, expected_arg.to_string()));
3442 } else if found_refs > expected_refs {
3443 remove_borrow.push((arg_span, expected_arg.to_string()));
3448 if !to_borrow.is_empty() {
3449 err.multipart_suggestion(
3450 "consider borrowing the argument",
3452 Applicability::MaybeIncorrect,
3456 if !remove_borrow.is_empty() {
3457 err.multipart_suggestion(
3458 "do not borrow the argument",
3460 Applicability::MaybeIncorrect,
3465 /// Collect all the returned expressions within the input expression.
3466 /// Used to point at the return spans when we want to suggest some change to them.
3468 pub struct ReturnsVisitor<'v> {
3469 pub returns: Vec<&'v hir::Expr<'v>>,
3470 in_block_tail: bool,
3473 impl<'v> Visitor<'v> for ReturnsVisitor<'v> {
3474 fn visit_expr(&mut self, ex: &'v hir::Expr<'v>) {
3475 // Visit every expression to detect `return` paths, either through the function's tail
3476 // expression or `return` statements. We walk all nodes to find `return` statements, but
3477 // we only care about tail expressions when `in_block_tail` is `true`, which means that
3478 // they're in the return path of the function body.
3480 hir::ExprKind::Ret(Some(ex)) => {
3481 self.returns.push(ex);
3483 hir::ExprKind::Block(block, _) if self.in_block_tail => {
3484 self.in_block_tail = false;
3485 for stmt in block.stmts {
3486 hir::intravisit::walk_stmt(self, stmt);
3488 self.in_block_tail = true;
3489 if let Some(expr) = block.expr {
3490 self.visit_expr(expr);
3493 hir::ExprKind::If(_, then, else_opt) if self.in_block_tail => {
3494 self.visit_expr(then);
3495 if let Some(el) = else_opt {
3496 self.visit_expr(el);
3499 hir::ExprKind::Match(_, arms, _) if self.in_block_tail => {
3501 self.visit_expr(arm.body);
3504 // We need to walk to find `return`s in the entire body.
3505 _ if !self.in_block_tail => hir::intravisit::walk_expr(self, ex),
3506 _ => self.returns.push(ex),
3510 fn visit_body(&mut self, body: &'v hir::Body<'v>) {
3511 assert!(!self.in_block_tail);
3512 if body.generator_kind().is_none() {
3513 if let hir::ExprKind::Block(block, None) = body.value.kind {
3514 if block.expr.is_some() {
3515 self.in_block_tail = true;
3519 hir::intravisit::walk_body(self, body);
3523 /// Collect all the awaited expressions within the input expression.
3525 struct AwaitsVisitor {
3526 awaits: Vec<hir::HirId>,
3529 impl<'v> Visitor<'v> for AwaitsVisitor {
3530 fn visit_expr(&mut self, ex: &'v hir::Expr<'v>) {
3531 if let hir::ExprKind::Yield(_, hir::YieldSource::Await { expr: Some(id) }) = ex.kind {
3532 self.awaits.push(id)
3534 hir::intravisit::walk_expr(self, ex)
3538 pub trait NextTypeParamName {
3539 fn next_type_param_name(&self, name: Option<&str>) -> String;
3542 impl NextTypeParamName for &[hir::GenericParam<'_>] {
3543 fn next_type_param_name(&self, name: Option<&str>) -> String {
3544 // This is the list of possible parameter names that we might suggest.
3545 let name = name.and_then(|n| n.chars().next()).map(|c| c.to_string().to_uppercase());
3546 let name = name.as_deref();
3547 let possible_names = [name.unwrap_or("T"), "T", "U", "V", "X", "Y", "Z", "A", "B", "C"];
3548 let used_names = self
3550 .filter_map(|p| match p.name {
3551 hir::ParamName::Plain(ident) => Some(ident.name),
3554 .collect::<Vec<_>>();
3558 .find(|n| !used_names.contains(&Symbol::intern(n)))
3559 .unwrap_or(&"ParamName")
3564 fn suggest_trait_object_return_type_alternatives(
3565 err: &mut Diagnostic,
3568 is_object_safe: bool,
3570 err.span_suggestion(
3572 "use some type `T` that is `T: Sized` as the return type if all return paths have the \
3575 Applicability::MaybeIncorrect,
3577 err.span_suggestion(
3580 "use `impl {}` as the return type if all return paths have the same type but you \
3581 want to expose only the trait in the signature",
3584 format!("impl {}", trait_obj),
3585 Applicability::MaybeIncorrect,
3588 err.multipart_suggestion(
3590 "use a boxed trait object if all return paths implement trait `{}`",
3594 (ret_ty.shrink_to_lo(), "Box<".to_string()),
3595 (ret_ty.shrink_to_hi(), ">".to_string()),
3597 Applicability::MaybeIncorrect,
3602 /// Collect the spans that we see the generic param `param_did`
3603 struct ReplaceImplTraitVisitor<'a> {
3604 ty_spans: &'a mut Vec<Span>,
3608 impl<'a, 'hir> hir::intravisit::Visitor<'hir> for ReplaceImplTraitVisitor<'a> {
3609 fn visit_ty(&mut self, t: &'hir hir::Ty<'hir>) {
3610 if let hir::TyKind::Path(hir::QPath::Resolved(
3612 hir::Path { res: hir::def::Res::Def(_, segment_did), .. },
3615 if self.param_did == *segment_did {
3616 // `fn foo(t: impl Trait)`
3617 // ^^^^^^^^^^ get this to suggest `T` instead
3619 // There might be more than one `impl Trait`.
3620 self.ty_spans.push(t.span);
3625 hir::intravisit::walk_ty(self, t);
3629 // Replace `param` with `replace_ty`
3630 struct ReplaceImplTraitFolder<'tcx> {
3632 param: &'tcx ty::GenericParamDef,
3633 replace_ty: Ty<'tcx>,
3636 impl<'tcx> TypeFolder<'tcx> for ReplaceImplTraitFolder<'tcx> {
3637 fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> {
3638 if let ty::Param(ty::ParamTy { index, .. }) = t.kind() {
3639 if self.param.index == *index {
3640 return self.replace_ty;
3643 t.super_fold_with(self)
3646 fn tcx(&self) -> TyCtxt<'tcx> {