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