rustc_trait_selection/error_reporting/traits/
suggestions.rs

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