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