rustc_trait_selection/error_reporting/traits/
suggestions.rs

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