Skip to main content

rustc_trait_selection/error_reporting/traits/
suggestions.rs

1// ignore-tidy-filelength
2
3use std::borrow::Cow;
4use std::path::PathBuf;
5use std::{debug_assert_matches, iter};
6
7use itertools::{EitherOrBoth, Itertools};
8use rustc_abi::ExternAbi;
9use rustc_data_structures::fx::FxHashSet;
10use rustc_data_structures::stack::ensure_sufficient_stack;
11use rustc_errors::codes::*;
12use rustc_errors::{
13    Applicability, Diag, EmissionGuarantee, MultiSpan, Style, SuggestionStyle, pluralize,
14    struct_span_code_err,
15};
16use rustc_hir::def::{CtorOf, DefKind, Res};
17use rustc_hir::def_id::DefId;
18use rustc_hir::intravisit::{Visitor, VisitorExt};
19use rustc_hir::lang_items::LangItem;
20use rustc_hir::{
21    self as hir, AmbigArg, CoroutineDesugaring, CoroutineKind, CoroutineSource, Expr, HirId, Node,
22    expr_needs_parens,
23};
24use rustc_infer::infer::{BoundRegionConversionTime, DefineOpaqueTypes, InferCtxt, InferOk};
25use rustc_infer::traits::ImplSource;
26use rustc_middle::middle::privacy::Level;
27use rustc_middle::traits::IsConstable;
28use rustc_middle::ty::adjustment::{Adjust, DerefAdjustKind};
29use rustc_middle::ty::error::TypeError;
30use rustc_middle::ty::print::{
31    PrintPolyTraitPredicateExt as _, PrintPolyTraitRefExt, PrintTraitPredicateExt as _,
32    PrintTraitRefExt as _, with_forced_trimmed_paths, with_no_trimmed_paths,
33    with_types_for_suggestion,
34};
35use rustc_middle::ty::{
36    self, AdtKind, GenericArgs, InferTy, IsSuggestable, Ty, TyCtxt, TypeFoldable, TypeFolder,
37    TypeSuperFoldable, TypeSuperVisitable, TypeVisitableExt, TypeVisitor, TypeckResults, Upcast,
38    suggest_arbitrary_trait_bound, suggest_constraining_type_param,
39};
40use rustc_middle::{bug, span_bug};
41use rustc_span::def_id::LocalDefId;
42use rustc_span::{
43    BytePos, DUMMY_SP, DesugaringKind, ExpnKind, Ident, MacroKind, Span, Symbol, kw, sym,
44};
45use tracing::{debug, instrument};
46
47use super::{
48    DefIdOrName, FindExprBySpan, ImplCandidate, Obligation, ObligationCause, ObligationCauseCode,
49    PredicateObligation,
50};
51use crate::error_reporting::TypeErrCtxt;
52use crate::errors;
53use crate::infer::InferCtxtExt as _;
54use crate::traits::query::evaluate_obligation::InferCtxtExt as _;
55use crate::traits::{ImplDerivedCause, NormalizeExt, ObligationCtxt, SelectionContext};
56
57#[derive(#[automatically_derived]
impl ::core::fmt::Debug for CoroutineInteriorOrUpvar {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        match self {
            CoroutineInteriorOrUpvar::Interior(__self_0, __self_1) =>
                ::core::fmt::Formatter::debug_tuple_field2_finish(f,
                    "Interior", __self_0, &__self_1),
            CoroutineInteriorOrUpvar::Upvar(__self_0) =>
                ::core::fmt::Formatter::debug_tuple_field1_finish(f, "Upvar",
                    &__self_0),
        }
    }
}Debug)]
58pub enum CoroutineInteriorOrUpvar {
59    // span of interior type
60    Interior(Span, Option<(Span, Option<Span>)>),
61    // span of upvar
62    Upvar(Span),
63}
64
65// This type provides a uniform interface to retrieve data on coroutines, whether it originated from
66// the local crate being compiled or from a foreign crate.
67#[derive(#[automatically_derived]
impl<'a, 'tcx> ::core::fmt::Debug for CoroutineData<'a, 'tcx> {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::debug_tuple_field1_finish(f, "CoroutineData",
            &&self.0)
    }
}Debug)]
68struct CoroutineData<'a, 'tcx>(&'a TypeckResults<'tcx>);
69
70impl<'a, 'tcx> CoroutineData<'a, 'tcx> {
71    /// Try to get information about variables captured by the coroutine that matches a type we are
72    /// looking for with `ty_matches` function. We uses it to find upvar which causes a failure to
73    /// meet an obligation
74    fn try_get_upvar_span<F>(
75        &self,
76        infer_context: &InferCtxt<'tcx>,
77        coroutine_did: DefId,
78        ty_matches: F,
79    ) -> Option<CoroutineInteriorOrUpvar>
80    where
81        F: Fn(ty::Binder<'tcx, Ty<'tcx>>) -> bool,
82    {
83        infer_context.tcx.upvars_mentioned(coroutine_did).and_then(|upvars| {
84            upvars.iter().find_map(|(upvar_id, upvar)| {
85                let upvar_ty = self.0.node_type(*upvar_id);
86                let upvar_ty = infer_context.resolve_vars_if_possible(upvar_ty);
87                ty_matches(ty::Binder::dummy(upvar_ty))
88                    .then(|| CoroutineInteriorOrUpvar::Upvar(upvar.span))
89            })
90        })
91    }
92
93    /// Try to get the span of a type being awaited on that matches the type we are looking with the
94    /// `ty_matches` function. We uses it to find awaited type which causes a failure to meet an
95    /// obligation
96    fn get_from_await_ty<F>(
97        &self,
98        visitor: AwaitsVisitor,
99        tcx: TyCtxt<'tcx>,
100        ty_matches: F,
101    ) -> Option<Span>
102    where
103        F: Fn(ty::Binder<'tcx, Ty<'tcx>>) -> bool,
104    {
105        visitor
106            .awaits
107            .into_iter()
108            .map(|id| tcx.hir_expect_expr(id))
109            .find(|await_expr| ty_matches(ty::Binder::dummy(self.0.expr_ty_adjusted(await_expr))))
110            .map(|expr| expr.span)
111    }
112}
113
114fn predicate_constraint(generics: &hir::Generics<'_>, pred: ty::Predicate<'_>) -> (Span, String) {
115    (
116        generics.tail_span_for_predicate_suggestion(),
117        {
    let _guard =
        ::rustc_middle::ty::print::pretty::RtnModeHelper::with(RtnMode::ForSuggestion);
    ::alloc::__export::must_use({
            ::alloc::fmt::format(format_args!("{0} {1}",
                    generics.add_where_or_trailing_comma(), pred))
        })
}with_types_for_suggestion!(format!("{} {}", generics.add_where_or_trailing_comma(), pred)),
118    )
119}
120
121/// Type parameter needs more bounds. The trivial case is `T` `where T: Bound`, but
122/// it can also be an `impl Trait` param that needs to be decomposed to a type
123/// param for cleaner code.
124pub fn suggest_restriction<'tcx, G: EmissionGuarantee>(
125    tcx: TyCtxt<'tcx>,
126    item_id: LocalDefId,
127    hir_generics: &hir::Generics<'tcx>,
128    msg: &str,
129    err: &mut Diag<'_, G>,
130    fn_sig: Option<&hir::FnSig<'_>>,
131    projection: Option<ty::AliasTy<'_>>,
132    trait_pred: ty::PolyTraitPredicate<'tcx>,
133    // When we are dealing with a trait, `super_traits` will be `Some`:
134    // Given `trait T: A + B + C {}`
135    //              -  ^^^^^^^^^ GenericBounds
136    //              |
137    //              &Ident
138    super_traits: Option<(&Ident, &hir::GenericBounds<'_>)>,
139) {
140    if hir_generics.where_clause_span.from_expansion()
141        || hir_generics.where_clause_span.desugaring_kind().is_some()
142        || projection.is_some_and(|projection| {
143            (tcx.is_impl_trait_in_trait(projection.kind.def_id())
144                && !tcx.features().return_type_notation())
145                || tcx
146                    .lookup_stability(projection.kind.def_id())
147                    .is_some_and(|stab| stab.is_unstable())
148        })
149    {
150        return;
151    }
152    let generics = tcx.generics_of(item_id);
153    // Given `fn foo(t: impl Trait)` where `Trait` requires assoc type `A`...
154    if let Some((param, bound_str, fn_sig)) =
155        fn_sig.zip(projection).and_then(|(sig, p)| match *p.self_ty().kind() {
156            // Shenanigans to get the `Trait` from the `impl Trait`.
157            ty::Param(param) => {
158                let param_def = generics.type_param(param, tcx);
159                if param_def.kind.is_synthetic() {
160                    let bound_str =
161                        param_def.name.as_str().strip_prefix("impl ")?.trim_start().to_string();
162                    return Some((param_def, bound_str, sig));
163                }
164                None
165            }
166            _ => None,
167        })
168    {
169        let type_param_name = hir_generics.params.next_type_param_name(Some(&bound_str));
170        let trait_pred = trait_pred.fold_with(&mut ReplaceImplTraitFolder {
171            tcx,
172            param,
173            replace_ty: ty::ParamTy::new(generics.count() as u32, Symbol::intern(&type_param_name))
174                .to_ty(tcx),
175        });
176        if !trait_pred.is_suggestable(tcx, false) {
177            return;
178        }
179        // We know we have an `impl Trait` that doesn't satisfy a required projection.
180
181        // Find all of the occurrences of `impl Trait` for `Trait` in the function arguments'
182        // types. There should be at least one, but there might be *more* than one. In that
183        // case we could just ignore it and try to identify which one needs the restriction,
184        // but instead we choose to suggest replacing all instances of `impl Trait` with `T`
185        // where `T: Trait`.
186        let mut ty_spans = ::alloc::vec::Vec::new()vec![];
187        for input in fn_sig.decl.inputs {
188            ReplaceImplTraitVisitor { ty_spans: &mut ty_spans, param_did: param.def_id }
189                .visit_ty_unambig(input);
190        }
191        // The type param `T: Trait` we will suggest to introduce.
192        let type_param = ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{0}: {1}", type_param_name,
                bound_str))
    })format!("{type_param_name}: {bound_str}");
193
194        let mut sugg = ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [if let Some(span) = hir_generics.span_for_param_suggestion() {
                    (span,
                        ::alloc::__export::must_use({
                                ::alloc::fmt::format(format_args!(", {0}", type_param))
                            }))
                } else {
                    (hir_generics.span,
                        ::alloc::__export::must_use({
                                ::alloc::fmt::format(format_args!("<{0}>", type_param))
                            }))
                },
                predicate_constraint(hir_generics, trait_pred.upcast(tcx))]))vec![
195            if let Some(span) = hir_generics.span_for_param_suggestion() {
196                (span, format!(", {type_param}"))
197            } else {
198                (hir_generics.span, format!("<{type_param}>"))
199            },
200            // `fn foo(t: impl Trait)`
201            //                       ^ suggest `where <T as Trait>::A: Bound`
202            predicate_constraint(hir_generics, trait_pred.upcast(tcx)),
203        ];
204        sugg.extend(ty_spans.into_iter().map(|s| (s, type_param_name.to_string())));
205
206        // Suggest `fn foo<T: Trait>(t: T) where <T as Trait>::A: Bound`.
207        // FIXME: we should suggest `fn foo(t: impl Trait<A: Bound>)` instead.
208        err.multipart_suggestion(
209            "introduce a type parameter with a trait bound instead of using `impl Trait`",
210            sugg,
211            Applicability::MaybeIncorrect,
212        );
213    } else {
214        if !trait_pred.is_suggestable(tcx, false) {
215            return;
216        }
217        // Trivial case: `T` needs an extra bound: `T: Bound`.
218        let (sp, suggestion) = match (
219            hir_generics
220                .params
221                .iter()
222                .find(|p| !#[allow(non_exhaustive_omitted_patterns)] match p.kind {
    hir::GenericParamKind::Type { synthetic: true, .. } => true,
    _ => false,
}matches!(p.kind, hir::GenericParamKind::Type { synthetic: true, .. })),
223            super_traits,
224        ) {
225            (_, None) => predicate_constraint(hir_generics, trait_pred.upcast(tcx)),
226            (None, Some((ident, []))) => (
227                ident.span.shrink_to_hi(),
228                ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!(": {0}",
                trait_pred.print_modifiers_and_trait_path()))
    })format!(": {}", trait_pred.print_modifiers_and_trait_path()),
229            ),
230            (_, Some((_, [.., bounds]))) => (
231                bounds.span().shrink_to_hi(),
232                ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!(" + {0}",
                trait_pred.print_modifiers_and_trait_path()))
    })format!(" + {}", trait_pred.print_modifiers_and_trait_path()),
233            ),
234            (Some(_), Some((_, []))) => (
235                hir_generics.span.shrink_to_hi(),
236                ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!(": {0}",
                trait_pred.print_modifiers_and_trait_path()))
    })format!(": {}", trait_pred.print_modifiers_and_trait_path()),
237            ),
238        };
239
240        err.span_suggestion_verbose(
241            sp,
242            ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("consider further restricting {0}",
                msg))
    })format!("consider further restricting {msg}"),
243            suggestion,
244            Applicability::MachineApplicable,
245        );
246    }
247}
248
249impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
250    pub fn note_field_shadowed_by_private_candidate_in_cause(
251        &self,
252        err: &mut Diag<'_>,
253        cause: &ObligationCause<'tcx>,
254        param_env: ty::ParamEnv<'tcx>,
255    ) {
256        let mut hir_ids = FxHashSet::default();
257        // Walk the parent chain so we can recover
258        // the source expression from whichever layer carries them.
259        let mut next_code = Some(cause.code());
260        while let Some(cause_code) = next_code {
261            match cause_code {
262                ObligationCauseCode::BinOp { lhs_hir_id, rhs_hir_id, .. } => {
263                    hir_ids.insert(*lhs_hir_id);
264                    hir_ids.insert(*rhs_hir_id);
265                }
266                ObligationCauseCode::FunctionArg { arg_hir_id, .. }
267                | ObligationCauseCode::ReturnValue(arg_hir_id)
268                | ObligationCauseCode::AwaitableExpr(arg_hir_id)
269                | ObligationCauseCode::BlockTailExpression(arg_hir_id, _)
270                | ObligationCauseCode::UnOp { hir_id: arg_hir_id } => {
271                    hir_ids.insert(*arg_hir_id);
272                }
273                ObligationCauseCode::OpaqueReturnType(Some((_, hir_id))) => {
274                    hir_ids.insert(*hir_id);
275                }
276                _ => {}
277            }
278            next_code = cause_code.parent();
279        }
280
281        if !cause.span.is_dummy()
282            && let Some(body) = self.tcx.hir_maybe_body_owned_by(cause.body_id)
283        {
284            let mut expr_finder = FindExprBySpan::new(cause.span, self.tcx);
285            expr_finder.visit_body(body);
286            if let Some(expr) = expr_finder.result {
287                hir_ids.insert(expr.hir_id);
288            }
289        }
290
291        // we will sort immediately by source order before emitting any diagnostics
292        #[allow(rustc::potential_query_instability)]
293        let mut hir_ids: Vec<_> = hir_ids.into_iter().collect();
294        let source_map = self.tcx.sess.source_map();
295        hir_ids.sort_by_cached_key(|hir_id| {
296            let span = self.tcx.hir_span(*hir_id);
297            let lo = source_map.lookup_byte_offset(span.lo());
298            let hi = source_map.lookup_byte_offset(span.hi());
299            (lo.sf.name.prefer_remapped_unconditionally().to_string(), lo.pos.0, hi.pos.0)
300        });
301
302        for hir_id in hir_ids {
303            self.note_field_shadowed_by_private_candidate(err, hir_id, param_env);
304        }
305    }
306
307    pub fn note_field_shadowed_by_private_candidate(
308        &self,
309        err: &mut Diag<'_>,
310        hir_id: hir::HirId,
311        param_env: ty::ParamEnv<'tcx>,
312    ) {
313        let Some(typeck_results) = &self.typeck_results else {
314            return;
315        };
316        let Node::Expr(expr) = self.tcx.hir_node(hir_id) else {
317            return;
318        };
319        let hir::ExprKind::Field(base_expr, field_ident) = expr.kind else {
320            return;
321        };
322
323        let Some(base_ty) = typeck_results.expr_ty_opt(base_expr) else {
324            return;
325        };
326        let base_ty = self.resolve_vars_if_possible(base_ty);
327        if base_ty.references_error() {
328            return;
329        }
330
331        let fn_body_hir_id = self.tcx.local_def_id_to_hir_id(typeck_results.hir_owner.def_id);
332        let mut private_candidate: Option<(Ty<'tcx>, Ty<'tcx>, Span)> = None;
333
334        for (deref_base_ty, _) in (self.autoderef_steps)(base_ty) {
335            let ty::Adt(base_def, args) = deref_base_ty.kind() else {
336                continue;
337            };
338
339            if base_def.is_enum() {
340                continue;
341            }
342
343            let (adjusted_ident, def_scope) =
344                self.tcx.adjust_ident_and_get_scope(field_ident, base_def.did(), fn_body_hir_id);
345
346            let Some((_, field_def)) =
347                base_def.non_enum_variant().fields.iter_enumerated().find(|(_, field)| {
348                    field.ident(self.tcx).normalize_to_macros_2_0() == adjusted_ident
349                })
350            else {
351                continue;
352            };
353            let field_span = self
354                .tcx
355                .def_ident_span(field_def.did)
356                .unwrap_or_else(|| self.tcx.def_span(field_def.did));
357
358            if field_def.vis.is_accessible_from(def_scope, self.tcx) {
359                let accessible_field_ty = field_def.ty(self.tcx, args);
360                if let Some((private_base_ty, private_field_ty, private_field_span)) =
361                    private_candidate
362                    && !self.can_eq(param_env, private_field_ty, accessible_field_ty)
363                {
364                    let private_struct_span = match private_base_ty.kind() {
365                        ty::Adt(private_base_def, _) => self
366                            .tcx
367                            .def_ident_span(private_base_def.did())
368                            .unwrap_or_else(|| self.tcx.def_span(private_base_def.did())),
369                        _ => DUMMY_SP,
370                    };
371                    let accessible_struct_span = self
372                        .tcx
373                        .def_ident_span(base_def.did())
374                        .unwrap_or_else(|| self.tcx.def_span(base_def.did()));
375                    let deref_impl_span = (typeck_results
376                        .expr_adjustments(base_expr)
377                        .iter()
378                        .filter(|adj| {
379                            #[allow(non_exhaustive_omitted_patterns)] match adj.kind {
    Adjust::Deref(DerefAdjustKind::Overloaded(_)) => true,
    _ => false,
}matches!(adj.kind, Adjust::Deref(DerefAdjustKind::Overloaded(_)))
380                        })
381                        .count()
382                        == 1)
383                        .then(|| {
384                            self.probe(|_| {
385                                let deref_trait_did =
386                                    self.tcx.require_lang_item(LangItem::Deref, DUMMY_SP);
387                                let trait_ref =
388                                    ty::TraitRef::new(self.tcx, deref_trait_did, [private_base_ty]);
389                                let obligation: Obligation<'tcx, ty::Predicate<'tcx>> =
390                                    Obligation::new(
391                                        self.tcx,
392                                        ObligationCause::dummy(),
393                                        param_env,
394                                        trait_ref,
395                                    );
396                                let Ok(Some(ImplSource::UserDefined(impl_data))) =
397                                    SelectionContext::new(self)
398                                        .select(&obligation.with(self.tcx, trait_ref))
399                                else {
400                                    return None;
401                                };
402                                Some(self.tcx.def_span(impl_data.impl_def_id))
403                            })
404                        })
405                        .flatten();
406
407                    let mut note_spans: MultiSpan = private_struct_span.into();
408                    if private_struct_span != DUMMY_SP {
409                        note_spans.push_span_label(private_struct_span, "in this struct");
410                    }
411                    if private_field_span != DUMMY_SP {
412                        note_spans.push_span_label(
413                            private_field_span,
414                            "if this field wasn't private, it would be accessible",
415                        );
416                    }
417                    if accessible_struct_span != DUMMY_SP {
418                        note_spans.push_span_label(
419                            accessible_struct_span,
420                            "this struct is accessible through auto-deref",
421                        );
422                    }
423                    if field_span != DUMMY_SP {
424                        note_spans
425                            .push_span_label(field_span, "this is the field that was accessed");
426                    }
427                    if let Some(deref_impl_span) = deref_impl_span
428                        && deref_impl_span != DUMMY_SP
429                    {
430                        note_spans.push_span_label(
431                            deref_impl_span,
432                            "the field was accessed through this `Deref`",
433                        );
434                    }
435
436                    err.span_note(
437                        note_spans,
438                        ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("there is a field `{0}` on `{1}` with type `{2}` but it is private; `{0}` from `{3}` was accessed through auto-deref instead",
                field_ident, private_base_ty, private_field_ty,
                deref_base_ty))
    })format!(
439                            "there is a field `{field_ident}` on `{private_base_ty}` with type `{private_field_ty}` but it is private; `{field_ident}` from `{deref_base_ty}` was accessed through auto-deref instead"
440                        ),
441                    );
442                }
443
444                // we finally get to the accessible field,
445                // so we can return early without checking the rest of the autoderef candidates
446                return;
447            }
448
449            private_candidate.get_or_insert((
450                deref_base_ty,
451                field_def.ty(self.tcx, args),
452                field_span,
453            ));
454        }
455    }
456
457    pub fn suggest_restricting_param_bound(
458        &self,
459        err: &mut Diag<'_>,
460        trait_pred: ty::PolyTraitPredicate<'tcx>,
461        associated_ty: Option<(&'static str, Ty<'tcx>)>,
462        mut body_id: LocalDefId,
463    ) {
464        if trait_pred.skip_binder().polarity != ty::PredicatePolarity::Positive {
465            return;
466        }
467
468        let trait_pred = self.resolve_numeric_literals_with_default(trait_pred);
469
470        let self_ty = trait_pred.skip_binder().self_ty();
471        let (param_ty, projection) = match *self_ty.kind() {
472            ty::Param(_) => (true, None),
473            ty::Alias(projection @ ty::AliasTy { kind: ty::Projection { .. }, .. }) => {
474                (false, Some(projection))
475            }
476            _ => (false, None),
477        };
478
479        let mut finder = ParamFinder { .. };
480        finder.visit_binder(&trait_pred);
481
482        // FIXME: Add check for trait bound that is already present, particularly `?Sized` so we
483        //        don't suggest `T: Sized + ?Sized`.
484        loop {
485            let node = self.tcx.hir_node_by_def_id(body_id);
486            match node {
487                hir::Node::Item(hir::Item {
488                    kind: hir::ItemKind::Trait(_, _, _, _, ident, generics, bounds, _),
489                    ..
490                }) if self_ty == self.tcx.types.self_param => {
491                    if !param_ty { ::core::panicking::panic("assertion failed: param_ty") };assert!(param_ty);
492                    // Restricting `Self` for a single method.
493                    suggest_restriction(
494                        self.tcx,
495                        body_id,
496                        generics,
497                        "`Self`",
498                        err,
499                        None,
500                        projection,
501                        trait_pred,
502                        Some((&ident, bounds)),
503                    );
504                    return;
505                }
506
507                hir::Node::TraitItem(hir::TraitItem {
508                    generics,
509                    kind: hir::TraitItemKind::Fn(..),
510                    ..
511                }) if self_ty == self.tcx.types.self_param => {
512                    if !param_ty { ::core::panicking::panic("assertion failed: param_ty") };assert!(param_ty);
513                    // Restricting `Self` for a single method.
514                    suggest_restriction(
515                        self.tcx, body_id, generics, "`Self`", err, None, projection, trait_pred,
516                        None,
517                    );
518                    return;
519                }
520
521                hir::Node::TraitItem(hir::TraitItem {
522                    generics,
523                    kind: hir::TraitItemKind::Fn(fn_sig, ..),
524                    ..
525                })
526                | hir::Node::ImplItem(hir::ImplItem {
527                    generics,
528                    kind: hir::ImplItemKind::Fn(fn_sig, ..),
529                    ..
530                })
531                | hir::Node::Item(hir::Item {
532                    kind: hir::ItemKind::Fn { sig: fn_sig, generics, .. },
533                    ..
534                }) if projection.is_some() => {
535                    // Missing restriction on associated type of type parameter (unmet projection).
536                    suggest_restriction(
537                        self.tcx,
538                        body_id,
539                        generics,
540                        "the associated type",
541                        err,
542                        Some(fn_sig),
543                        projection,
544                        trait_pred,
545                        None,
546                    );
547                    return;
548                }
549                hir::Node::Item(hir::Item {
550                    kind:
551                        hir::ItemKind::Trait(_, _, _, _, _, generics, ..)
552                        | hir::ItemKind::Impl(hir::Impl { generics, .. }),
553                    ..
554                }) if projection.is_some() => {
555                    // Missing restriction on associated type of type parameter (unmet projection).
556                    suggest_restriction(
557                        self.tcx,
558                        body_id,
559                        generics,
560                        "the associated type",
561                        err,
562                        None,
563                        projection,
564                        trait_pred,
565                        None,
566                    );
567                    return;
568                }
569
570                hir::Node::Item(hir::Item {
571                    kind:
572                        hir::ItemKind::Struct(_, generics, _)
573                        | hir::ItemKind::Enum(_, generics, _)
574                        | hir::ItemKind::Union(_, generics, _)
575                        | hir::ItemKind::Trait(_, _, _, _, _, generics, ..)
576                        | hir::ItemKind::Impl(hir::Impl { generics, .. })
577                        | hir::ItemKind::Fn { generics, .. }
578                        | hir::ItemKind::TyAlias(_, generics, _)
579                        | hir::ItemKind::Const(_, generics, _, _)
580                        | hir::ItemKind::TraitAlias(_, _, generics, _),
581                    ..
582                })
583                | hir::Node::TraitItem(hir::TraitItem { generics, .. })
584                | hir::Node::ImplItem(hir::ImplItem { generics, .. })
585                    if param_ty =>
586                {
587                    // We skip the 0'th arg (self) because we do not want
588                    // to consider the predicate as not suggestible if the
589                    // self type is an arg position `impl Trait` -- instead,
590                    // we handle that by adding ` + Bound` below.
591                    // FIXME(compiler-errors): It would be nice to do the same
592                    // this that we do in `suggest_restriction` and pull the
593                    // `impl Trait` into a new generic if it shows up somewhere
594                    // else in the predicate.
595                    if !trait_pred.skip_binder().trait_ref.args[1..]
596                        .iter()
597                        .all(|g| g.is_suggestable(self.tcx, false))
598                    {
599                        return;
600                    }
601                    // Missing generic type parameter bound.
602                    let param_name = self_ty.to_string();
603                    let mut constraint = {
    let _guard = NoTrimmedGuard::new();
    trait_pred.print_modifiers_and_trait_path().to_string()
}with_no_trimmed_paths!(
604                        trait_pred.print_modifiers_and_trait_path().to_string()
605                    );
606
607                    if let Some((name, term)) = associated_ty {
608                        // FIXME: this case overlaps with code in TyCtxt::note_and_explain_type_err.
609                        // That should be extracted into a helper function.
610                        if let Some(stripped) = constraint.strip_suffix('>') {
611                            constraint = ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{0}, {1} = {2}>", stripped, name,
                term))
    })format!("{stripped}, {name} = {term}>");
612                        } else {
613                            constraint.push_str(&::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("<{0} = {1}>", name, term))
    })format!("<{name} = {term}>"));
614                        }
615                    }
616
617                    if suggest_constraining_type_param(
618                        self.tcx,
619                        generics,
620                        err,
621                        &param_name,
622                        &constraint,
623                        Some(trait_pred.def_id()),
624                        None,
625                    ) {
626                        return;
627                    }
628                }
629
630                hir::Node::TraitItem(hir::TraitItem {
631                    generics,
632                    kind: hir::TraitItemKind::Fn(..),
633                    ..
634                })
635                | hir::Node::ImplItem(hir::ImplItem {
636                    generics,
637                    impl_kind: hir::ImplItemImplKind::Inherent { .. },
638                    kind: hir::ImplItemKind::Fn(..),
639                    ..
640                }) if finder.can_suggest_bound(generics) => {
641                    // Missing generic type parameter bound.
642                    suggest_arbitrary_trait_bound(
643                        self.tcx,
644                        generics,
645                        err,
646                        trait_pred,
647                        associated_ty,
648                    );
649                }
650                hir::Node::Item(hir::Item {
651                    kind:
652                        hir::ItemKind::Struct(_, generics, _)
653                        | hir::ItemKind::Enum(_, generics, _)
654                        | hir::ItemKind::Union(_, generics, _)
655                        | hir::ItemKind::Trait(_, _, _, _, _, generics, ..)
656                        | hir::ItemKind::Impl(hir::Impl { generics, .. })
657                        | hir::ItemKind::Fn { generics, .. }
658                        | hir::ItemKind::TyAlias(_, generics, _)
659                        | hir::ItemKind::Const(_, generics, _, _)
660                        | hir::ItemKind::TraitAlias(_, _, generics, _),
661                    ..
662                }) if finder.can_suggest_bound(generics) => {
663                    // Missing generic type parameter bound.
664                    if suggest_arbitrary_trait_bound(
665                        self.tcx,
666                        generics,
667                        err,
668                        trait_pred,
669                        associated_ty,
670                    ) {
671                        return;
672                    }
673                }
674                hir::Node::Crate(..) => return,
675
676                _ => {}
677            }
678            body_id = self.tcx.local_parent(body_id);
679        }
680    }
681
682    /// Provide a suggestion to dereference arguments to functions and binary operators, if that
683    /// would satisfy trait bounds.
684    pub(super) fn suggest_dereferences(
685        &self,
686        obligation: &PredicateObligation<'tcx>,
687        err: &mut Diag<'_>,
688        trait_pred: ty::PolyTraitPredicate<'tcx>,
689    ) -> bool {
690        let mut code = obligation.cause.code();
691        if let ObligationCauseCode::FunctionArg { arg_hir_id, call_hir_id, .. } = code
692            && let Some(typeck_results) = &self.typeck_results
693            && let hir::Node::Expr(expr) = self.tcx.hir_node(*arg_hir_id)
694            && let Some(arg_ty) = typeck_results.expr_ty_adjusted_opt(expr)
695        {
696            // Suggest dereferencing the argument to a function/method call if possible
697
698            // Get the root obligation, since the leaf obligation we have may be unhelpful (#87437)
699            let mut real_trait_pred = trait_pred;
700            while let Some((parent_code, parent_trait_pred)) = code.parent_with_predicate() {
701                code = parent_code;
702                if let Some(parent_trait_pred) = parent_trait_pred {
703                    real_trait_pred = parent_trait_pred;
704                }
705            }
706
707            // We `instantiate_bound_regions_with_erased` here because `make_subregion` does not handle
708            // `ReBound`, and we don't particularly care about the regions.
709            let real_ty = self.tcx.instantiate_bound_regions_with_erased(real_trait_pred.self_ty());
710            if !self.can_eq(obligation.param_env, real_ty, arg_ty) {
711                return false;
712            }
713
714            // Potentially, we'll want to place our dereferences under a `&`. We don't try this for
715            // `&mut`, since we can't be sure users will get the side-effects they want from it.
716            // If this doesn't work, we'll try removing the `&` in `suggest_remove_reference`.
717            // FIXME(dianne): this misses the case where users need both to deref and remove `&`s.
718            // This method could be combined with `TypeErrCtxt::suggest_remove_reference` to handle
719            // that, similar to what `FnCtxt::suggest_deref_or_ref` does.
720            let (is_under_ref, base_ty, span) = match expr.kind {
721                hir::ExprKind::AddrOf(hir::BorrowKind::Ref, hir::Mutability::Not, subexpr)
722                    if let &ty::Ref(region, base_ty, hir::Mutability::Not) = real_ty.kind() =>
723                {
724                    (Some(region), base_ty, subexpr.span)
725                }
726                // Don't suggest `*&mut`, etc.
727                hir::ExprKind::AddrOf(..) => return false,
728                _ => (None, real_ty, obligation.cause.span),
729            };
730
731            let autoderef = (self.autoderef_steps)(base_ty);
732            let mut is_boxed = base_ty.is_box();
733            if let Some(steps) = autoderef.into_iter().position(|(mut ty, obligations)| {
734                // Ensure one of the following for dereferencing to be valid: we're passing by
735                // reference, `ty` is `Copy`, or we're moving out of a (potentially nested) `Box`.
736                let can_deref = is_under_ref.is_some()
737                    || self.type_is_copy_modulo_regions(obligation.param_env, ty)
738                    || ty.is_numeric() // for inference vars (presumably but not provably `Copy`)
739                    || is_boxed && self.type_is_sized_modulo_regions(obligation.param_env, ty);
740                is_boxed &= ty.is_box();
741
742                // Re-add the `&` if necessary
743                if let Some(region) = is_under_ref {
744                    ty = Ty::new_ref(self.tcx, region, ty, hir::Mutability::Not);
745                }
746
747                // Remapping bound vars here
748                let real_trait_pred_and_ty =
749                    real_trait_pred.map_bound(|inner_trait_pred| (inner_trait_pred, ty));
750                let obligation = self.mk_trait_obligation_with_new_self_ty(
751                    obligation.param_env,
752                    real_trait_pred_and_ty,
753                );
754
755                can_deref
756                    && obligations
757                        .iter()
758                        .chain([&obligation])
759                        .all(|obligation| self.predicate_may_hold(obligation))
760            }) && steps > 0
761            {
762                if span.in_external_macro(self.tcx.sess.source_map()) {
763                    return false;
764                }
765                let derefs = "*".repeat(steps);
766                let msg = "consider dereferencing here";
767
768                let call_node = self.tcx.hir_node(*call_hir_id);
769                let is_receiver = #[allow(non_exhaustive_omitted_patterns)] match call_node {
    Node::Expr(hir::Expr {
        kind: hir::ExprKind::MethodCall(_, receiver_expr, ..), .. }) if
        receiver_expr.hir_id == *arg_hir_id => true,
    _ => false,
}matches!(
770                    call_node,
771                    Node::Expr(hir::Expr {
772                        kind: hir::ExprKind::MethodCall(_, receiver_expr, ..),
773                        ..
774                    })
775                    if receiver_expr.hir_id == *arg_hir_id
776                );
777                if is_receiver {
778                    err.multipart_suggestion(
779                        msg,
780                        ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [(span.shrink_to_lo(),
                    ::alloc::__export::must_use({
                            ::alloc::fmt::format(format_args!("({0}", derefs))
                        })), (span.shrink_to_hi(), ")".to_string())]))vec![
781                            (span.shrink_to_lo(), format!("({derefs}")),
782                            (span.shrink_to_hi(), ")".to_string()),
783                        ],
784                        Applicability::MachineApplicable,
785                    )
786                } else {
787                    err.span_suggestion_verbose(
788                        span.shrink_to_lo(),
789                        msg,
790                        derefs,
791                        Applicability::MachineApplicable,
792                    )
793                };
794                return true;
795            }
796        } else if let (
797            ObligationCauseCode::BinOp { lhs_hir_id, rhs_hir_id, .. },
798            predicate,
799        ) = code.peel_derives_with_predicate()
800            && let Some(typeck_results) = &self.typeck_results
801            && let hir::Node::Expr(lhs) = self.tcx.hir_node(*lhs_hir_id)
802            && let hir::Node::Expr(rhs) = self.tcx.hir_node(*rhs_hir_id)
803            && let Some(rhs_ty) = typeck_results.expr_ty_opt(rhs)
804            && let trait_pred = predicate.unwrap_or(trait_pred)
805            // Only run this code on binary operators
806            && hir::lang_items::BINARY_OPERATORS
807                .iter()
808                .filter_map(|&op| self.tcx.lang_items().get(op))
809                .any(|op| {
810                    op == trait_pred.skip_binder().trait_ref.def_id
811                })
812        {
813            // Suggest dereferencing the LHS, RHS, or both terms of a binop if possible
814            let trait_pred = predicate.unwrap_or(trait_pred);
815            let lhs_ty = self.tcx.instantiate_bound_regions_with_erased(trait_pred.self_ty());
816            let lhs_autoderef = (self.autoderef_steps)(lhs_ty);
817            let rhs_autoderef = (self.autoderef_steps)(rhs_ty);
818            let first_lhs = lhs_autoderef.first().unwrap().clone();
819            let first_rhs = rhs_autoderef.first().unwrap().clone();
820            let mut autoderefs = lhs_autoderef
821                .into_iter()
822                .enumerate()
823                .rev()
824                .zip_longest(rhs_autoderef.into_iter().enumerate().rev())
825                .map(|t| match t {
826                    EitherOrBoth::Both(a, b) => (a, b),
827                    EitherOrBoth::Left(a) => (a, (0, first_rhs.clone())),
828                    EitherOrBoth::Right(b) => ((0, first_lhs.clone()), b),
829                })
830                .rev();
831            if let Some((lsteps, rsteps)) =
832                autoderefs.find_map(|((lsteps, (l_ty, _)), (rsteps, (r_ty, _)))| {
833                    // Create a new predicate with the dereferenced LHS and RHS
834                    // We simultaneously dereference both sides rather than doing them
835                    // one at a time to account for cases such as &Box<T> == &&T
836                    let trait_pred_and_ty = trait_pred.map_bound(|inner| {
837                        (
838                            ty::TraitPredicate {
839                                trait_ref: ty::TraitRef::new_from_args(
840                                    self.tcx,
841                                    inner.trait_ref.def_id,
842                                    self.tcx.mk_args(
843                                        &[&[l_ty.into(), r_ty.into()], &inner.trait_ref.args[2..]]
844                                            .concat(),
845                                    ),
846                                ),
847                                ..inner
848                            },
849                            l_ty,
850                        )
851                    });
852                    let obligation = self.mk_trait_obligation_with_new_self_ty(
853                        obligation.param_env,
854                        trait_pred_and_ty,
855                    );
856                    self.predicate_may_hold(&obligation).then_some(match (lsteps, rsteps) {
857                        (_, 0) => (Some(lsteps), None),
858                        (0, _) => (None, Some(rsteps)),
859                        _ => (Some(lsteps), Some(rsteps)),
860                    })
861                })
862            {
863                let make_sugg = |mut expr: &Expr<'_>, mut steps| {
864                    if expr.span.in_external_macro(self.tcx.sess.source_map()) {
865                        return None;
866                    }
867                    let mut prefix_span = expr.span.shrink_to_lo();
868                    let mut msg = "consider dereferencing here";
869                    if let hir::ExprKind::AddrOf(_, _, inner) = expr.kind {
870                        msg = "consider removing the borrow and dereferencing instead";
871                        if let hir::ExprKind::AddrOf(..) = inner.kind {
872                            msg = "consider removing the borrows and dereferencing instead";
873                        }
874                    }
875                    while let hir::ExprKind::AddrOf(_, _, inner) = expr.kind
876                        && steps > 0
877                    {
878                        prefix_span = prefix_span.with_hi(inner.span.lo());
879                        expr = inner;
880                        steps -= 1;
881                    }
882                    // Empty suggestions with empty spans ICE with debug assertions
883                    if steps == 0 {
884                        return Some((
885                            msg.trim_end_matches(" and dereferencing instead"),
886                            ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [(prefix_span, String::new())]))vec![(prefix_span, String::new())],
887                        ));
888                    }
889                    let derefs = "*".repeat(steps);
890                    let needs_parens = steps > 0 && expr_needs_parens(expr);
891                    let mut suggestion = if needs_parens {
892                        ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [(expr.span.with_lo(prefix_span.hi()).shrink_to_lo(),
                    ::alloc::__export::must_use({
                            ::alloc::fmt::format(format_args!("{0}(", derefs))
                        })), (expr.span.shrink_to_hi(), ")".to_string())]))vec![
893                            (
894                                expr.span.with_lo(prefix_span.hi()).shrink_to_lo(),
895                                format!("{derefs}("),
896                            ),
897                            (expr.span.shrink_to_hi(), ")".to_string()),
898                        ]
899                    } else {
900                        ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [(expr.span.with_lo(prefix_span.hi()).shrink_to_lo(),
                    ::alloc::__export::must_use({
                            ::alloc::fmt::format(format_args!("{0}", derefs))
                        }))]))vec![(
901                            expr.span.with_lo(prefix_span.hi()).shrink_to_lo(),
902                            format!("{derefs}"),
903                        )]
904                    };
905                    // Empty suggestions with empty spans ICE with debug assertions
906                    if !prefix_span.is_empty() {
907                        suggestion.push((prefix_span, String::new()));
908                    }
909                    Some((msg, suggestion))
910                };
911
912                if let Some(lsteps) = lsteps
913                    && let Some(rsteps) = rsteps
914                    && lsteps > 0
915                    && rsteps > 0
916                {
917                    let Some((_, mut suggestion)) = make_sugg(lhs, lsteps) else {
918                        return false;
919                    };
920                    let Some((_, mut rhs_suggestion)) = make_sugg(rhs, rsteps) else {
921                        return false;
922                    };
923                    suggestion.append(&mut rhs_suggestion);
924                    err.multipart_suggestion(
925                        "consider dereferencing both sides of the expression",
926                        suggestion,
927                        Applicability::MachineApplicable,
928                    );
929                    return true;
930                } else if let Some(lsteps) = lsteps
931                    && lsteps > 0
932                {
933                    let Some((msg, suggestion)) = make_sugg(lhs, lsteps) else {
934                        return false;
935                    };
936                    err.multipart_suggestion(msg, suggestion, Applicability::MachineApplicable);
937                    return true;
938                } else if let Some(rsteps) = rsteps
939                    && rsteps > 0
940                {
941                    let Some((msg, suggestion)) = make_sugg(rhs, rsteps) else {
942                        return false;
943                    };
944                    err.multipart_suggestion(msg, suggestion, Applicability::MachineApplicable);
945                    return true;
946                }
947            }
948        }
949        false
950    }
951
952    /// Given a closure's `DefId`, return the given name of the closure.
953    ///
954    /// This doesn't account for reassignments, but it's only used for suggestions.
955    fn get_closure_name(
956        &self,
957        def_id: DefId,
958        err: &mut Diag<'_>,
959        msg: Cow<'static, str>,
960    ) -> Option<Symbol> {
961        let get_name = |err: &mut Diag<'_>, kind: &hir::PatKind<'_>| -> Option<Symbol> {
962            // Get the local name of this closure. This can be inaccurate because
963            // of the possibility of reassignment, but this should be good enough.
964            match &kind {
965                hir::PatKind::Binding(hir::BindingMode::NONE, _, ident, None) => Some(ident.name),
966                _ => {
967                    err.note(msg);
968                    None
969                }
970            }
971        };
972
973        let hir_id = self.tcx.local_def_id_to_hir_id(def_id.as_local()?);
974        match self.tcx.parent_hir_node(hir_id) {
975            hir::Node::Stmt(hir::Stmt { kind: hir::StmtKind::Let(local), .. }) => {
976                get_name(err, &local.pat.kind)
977            }
978            // Different to previous arm because one is `&hir::Local` and the other
979            // is `Box<hir::Local>`.
980            hir::Node::LetStmt(local) => get_name(err, &local.pat.kind),
981            _ => None,
982        }
983    }
984
985    /// We tried to apply the bound to an `fn` or closure. Check whether calling it would
986    /// evaluate to a type that *would* satisfy the trait bound. If it would, suggest calling
987    /// it: `bar(foo)` → `bar(foo())`. This case is *very* likely to be hit if `foo` is `async`.
988    pub(super) fn suggest_fn_call(
989        &self,
990        obligation: &PredicateObligation<'tcx>,
991        err: &mut Diag<'_>,
992        trait_pred: ty::PolyTraitPredicate<'tcx>,
993    ) -> bool {
994        // It doesn't make sense to make this suggestion outside of typeck...
995        // (also autoderef will ICE...)
996        if self.typeck_results.is_none() {
997            return false;
998        }
999
1000        if let ty::PredicateKind::Clause(ty::ClauseKind::Trait(trait_pred)) =
1001            obligation.predicate.kind().skip_binder()
1002            && self.tcx.is_lang_item(trait_pred.def_id(), LangItem::Sized)
1003        {
1004            // Don't suggest calling to turn an unsized type into a sized type
1005            return false;
1006        }
1007
1008        let self_ty = self.instantiate_binder_with_fresh_vars(
1009            DUMMY_SP,
1010            BoundRegionConversionTime::FnCall,
1011            trait_pred.self_ty(),
1012        );
1013
1014        let Some((def_id_or_name, output, inputs)) =
1015            self.extract_callable_info(obligation.cause.body_id, obligation.param_env, self_ty)
1016        else {
1017            return false;
1018        };
1019
1020        // Remapping bound vars here
1021        let trait_pred_and_self = trait_pred.map_bound(|trait_pred| (trait_pred, output));
1022
1023        let new_obligation =
1024            self.mk_trait_obligation_with_new_self_ty(obligation.param_env, trait_pred_and_self);
1025        if !self.predicate_must_hold_modulo_regions(&new_obligation) {
1026            return false;
1027        }
1028
1029        // If this is a zero-argument async closure directly passed as an argument
1030        // and the expected type is `Future`, suggest using `async {}` block instead
1031        // of `async || {}`
1032        if let ty::CoroutineClosure(def_id, args) = *self_ty.kind()
1033            && let sig = args.as_coroutine_closure().coroutine_closure_sig().skip_binder()
1034            && let ty::Tuple(inputs) = *sig.tupled_inputs_ty.kind()
1035            && inputs.is_empty()
1036            && self.tcx.is_lang_item(trait_pred.def_id(), LangItem::Future)
1037            && let Some(hir::Node::Expr(hir::Expr {
1038                kind:
1039                    hir::ExprKind::Closure(hir::Closure {
1040                        kind: hir::ClosureKind::CoroutineClosure(CoroutineDesugaring::Async),
1041                        fn_arg_span: Some(arg_span),
1042                        ..
1043                    }),
1044                ..
1045            })) = self.tcx.hir_get_if_local(def_id)
1046            && obligation.cause.span.contains(*arg_span)
1047        {
1048            let sm = self.tcx.sess.source_map();
1049            let removal_span = if let Ok(snippet) =
1050                sm.span_to_snippet(arg_span.with_hi(arg_span.hi() + rustc_span::BytePos(1)))
1051                && snippet.ends_with(' ')
1052            {
1053                // There's a space after `||`, include it in the removal
1054                arg_span.with_hi(arg_span.hi() + rustc_span::BytePos(1))
1055            } else {
1056                *arg_span
1057            };
1058            err.span_suggestion_verbose(
1059                removal_span,
1060                "use `async {}` instead of `async || {}` to introduce an async block",
1061                "",
1062                Applicability::MachineApplicable,
1063            );
1064            return true;
1065        }
1066
1067        // Get the name of the callable and the arguments to be used in the suggestion.
1068        let msg = match def_id_or_name {
1069            DefIdOrName::DefId(def_id) => match self.tcx.def_kind(def_id) {
1070                DefKind::Ctor(CtorOf::Struct, _) => {
1071                    Cow::from("use parentheses to construct this tuple struct")
1072                }
1073                DefKind::Ctor(CtorOf::Variant, _) => {
1074                    Cow::from("use parentheses to construct this tuple variant")
1075                }
1076                kind => Cow::from(::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("use parentheses to call this {0}",
                self.tcx.def_kind_descr(kind, def_id)))
    })format!(
1077                    "use parentheses to call this {}",
1078                    self.tcx.def_kind_descr(kind, def_id)
1079                )),
1080            },
1081            DefIdOrName::Name(name) => Cow::from(::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("use parentheses to call this {0}",
                name))
    })format!("use parentheses to call this {name}")),
1082        };
1083
1084        let args = inputs
1085            .into_iter()
1086            .map(|ty| {
1087                if ty.is_suggestable(self.tcx, false) {
1088                    ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("/* {0} */", ty))
    })format!("/* {ty} */")
1089                } else {
1090                    "/* value */".to_string()
1091                }
1092            })
1093            .collect::<Vec<_>>()
1094            .join(", ");
1095
1096        if #[allow(non_exhaustive_omitted_patterns)] match obligation.cause.code() {
    ObligationCauseCode::FunctionArg { .. } => true,
    _ => false,
}matches!(obligation.cause.code(), ObligationCauseCode::FunctionArg { .. })
1097            && obligation.cause.span.can_be_used_for_suggestions()
1098        {
1099            let (span, sugg) = if let Some(snippet) =
1100                self.tcx.sess.source_map().span_to_snippet(obligation.cause.span).ok()
1101                && snippet.starts_with("|")
1102            {
1103                (obligation.cause.span, ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("({0})({1})", snippet, args))
    })format!("({snippet})({args})"))
1104            } else {
1105                (obligation.cause.span.shrink_to_hi(), ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("({0})", args))
    })format!("({args})"))
1106            };
1107
1108            // When the obligation error has been ensured to have been caused by
1109            // an argument, the `obligation.cause.span` points at the expression
1110            // of the argument, so we can provide a suggestion. Otherwise, we give
1111            // a more general note.
1112            err.span_suggestion_verbose(span, msg, sugg, Applicability::HasPlaceholders);
1113        } else if let DefIdOrName::DefId(def_id) = def_id_or_name {
1114            let name = match self.tcx.hir_get_if_local(def_id) {
1115                Some(hir::Node::Expr(hir::Expr {
1116                    kind: hir::ExprKind::Closure(hir::Closure { fn_decl_span, .. }),
1117                    ..
1118                })) => {
1119                    err.span_label(*fn_decl_span, "consider calling this closure");
1120                    let Some(name) = self.get_closure_name(def_id, err, msg.clone()) else {
1121                        return false;
1122                    };
1123                    name.to_string()
1124                }
1125                Some(hir::Node::Item(hir::Item {
1126                    kind: hir::ItemKind::Fn { ident, .. }, ..
1127                })) => {
1128                    err.span_label(ident.span, "consider calling this function");
1129                    ident.to_string()
1130                }
1131                Some(hir::Node::Ctor(..)) => {
1132                    let name = self.tcx.def_path_str(def_id);
1133                    err.span_label(
1134                        self.tcx.def_span(def_id),
1135                        ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("consider calling the constructor for `{0}`",
                name))
    })format!("consider calling the constructor for `{name}`"),
1136                    );
1137                    name
1138                }
1139                _ => return false,
1140            };
1141            err.help(::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{0}: `{1}({2})`", msg, name, args))
    })format!("{msg}: `{name}({args})`"));
1142        }
1143        true
1144    }
1145
1146    pub(super) fn suggest_cast_to_fn_pointer(
1147        &self,
1148        obligation: &PredicateObligation<'tcx>,
1149        err: &mut Diag<'_>,
1150        leaf_trait_predicate: ty::PolyTraitPredicate<'tcx>,
1151        main_trait_predicate: ty::PolyTraitPredicate<'tcx>,
1152        span: Span,
1153    ) -> bool {
1154        let &[candidate] = &self.find_similar_impl_candidates(leaf_trait_predicate)[..] else {
1155            return false;
1156        };
1157        let candidate = candidate.trait_ref;
1158
1159        if !#[allow(non_exhaustive_omitted_patterns)] match (candidate.self_ty().kind(),
        main_trait_predicate.self_ty().skip_binder().kind()) {
    (ty::FnPtr(..), ty::FnDef(..)) => true,
    _ => false,
}matches!(
1160            (candidate.self_ty().kind(), main_trait_predicate.self_ty().skip_binder().kind(),),
1161            (ty::FnPtr(..), ty::FnDef(..))
1162        ) {
1163            return false;
1164        }
1165
1166        let parenthesized_cast = |span: Span| {
1167            ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [(span.shrink_to_lo(), "(".to_string()),
                (span.shrink_to_hi(),
                    ::alloc::__export::must_use({
                            ::alloc::fmt::format(format_args!(" as {0})",
                                    candidate.self_ty()))
                        }))]))vec![
1168                (span.shrink_to_lo(), "(".to_string()),
1169                (span.shrink_to_hi(), format!(" as {})", candidate.self_ty())),
1170            ]
1171        };
1172        // Wrap method receivers and `&`-references in parens.
1173        let suggestion = if self.tcx.sess.source_map().span_followed_by(span, ".").is_some() {
1174            parenthesized_cast(span)
1175        } else if let Some(body) = self.tcx.hir_maybe_body_owned_by(obligation.cause.body_id) {
1176            let mut expr_finder = FindExprBySpan::new(span, self.tcx);
1177            expr_finder.visit_expr(body.value);
1178            if let Some(expr) = expr_finder.result
1179                && let hir::ExprKind::AddrOf(_, _, expr) = expr.kind
1180            {
1181                parenthesized_cast(expr.span)
1182            } else {
1183                ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [(span.shrink_to_hi(),
                    ::alloc::__export::must_use({
                            ::alloc::fmt::format(format_args!(" as {0}",
                                    candidate.self_ty()))
                        }))]))vec![(span.shrink_to_hi(), format!(" as {}", candidate.self_ty()))]
1184            }
1185        } else {
1186            ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [(span.shrink_to_hi(),
                    ::alloc::__export::must_use({
                            ::alloc::fmt::format(format_args!(" as {0}",
                                    candidate.self_ty()))
                        }))]))vec![(span.shrink_to_hi(), format!(" as {}", candidate.self_ty()))]
1187        };
1188
1189        let trait_ = self.tcx.short_string(candidate.print_trait_sugared(), err.long_ty_path());
1190        let self_ty = self.tcx.short_string(candidate.self_ty(), err.long_ty_path());
1191        err.multipart_suggestion(
1192            ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("the trait `{0}` is implemented for fn pointer `{1}`, try casting using `as`",
                trait_, self_ty))
    })format!(
1193                "the trait `{trait_}` is implemented for fn pointer \
1194                 `{self_ty}`, try casting using `as`",
1195            ),
1196            suggestion,
1197            Applicability::MaybeIncorrect,
1198        );
1199        true
1200    }
1201
1202    pub(super) fn check_for_binding_assigned_block_without_tail_expression(
1203        &self,
1204        obligation: &PredicateObligation<'tcx>,
1205        err: &mut Diag<'_>,
1206        trait_pred: ty::PolyTraitPredicate<'tcx>,
1207    ) {
1208        let mut span = obligation.cause.span;
1209        while span.from_expansion() {
1210            // Remove all the desugaring and macro contexts.
1211            span.remove_mark();
1212        }
1213        let mut expr_finder = FindExprBySpan::new(span, self.tcx);
1214        let Some(body) = self.tcx.hir_maybe_body_owned_by(obligation.cause.body_id) else {
1215            return;
1216        };
1217        expr_finder.visit_expr(body.value);
1218        let Some(expr) = expr_finder.result else {
1219            return;
1220        };
1221        let Some(typeck) = &self.typeck_results else {
1222            return;
1223        };
1224        let Some(ty) = typeck.expr_ty_adjusted_opt(expr) else {
1225            return;
1226        };
1227        if !ty.is_unit() {
1228            return;
1229        };
1230        let hir::ExprKind::Path(hir::QPath::Resolved(None, path)) = expr.kind else {
1231            return;
1232        };
1233        let Res::Local(hir_id) = path.res else {
1234            return;
1235        };
1236        let hir::Node::Pat(pat) = self.tcx.hir_node(hir_id) else {
1237            return;
1238        };
1239        let hir::Node::LetStmt(hir::LetStmt { ty: None, init: Some(init), .. }) =
1240            self.tcx.parent_hir_node(pat.hir_id)
1241        else {
1242            return;
1243        };
1244        let hir::ExprKind::Block(block, None) = init.kind else {
1245            return;
1246        };
1247        if block.expr.is_some() {
1248            return;
1249        }
1250        let [.., stmt] = block.stmts else {
1251            err.span_label(block.span, "this empty block is missing a tail expression");
1252            return;
1253        };
1254        // FIXME expr and stmt have the same span if expr comes from expansion
1255        // cc: https://github.com/rust-lang/rust/pull/147416#discussion_r2499407523
1256        if stmt.span.from_expansion() {
1257            return;
1258        }
1259        let hir::StmtKind::Semi(tail_expr) = stmt.kind else {
1260            return;
1261        };
1262        let Some(ty) = typeck.expr_ty_opt(tail_expr) else {
1263            err.span_label(block.span, "this block is missing a tail expression");
1264            return;
1265        };
1266        let ty = self.resolve_numeric_literals_with_default(self.resolve_vars_if_possible(ty));
1267        let trait_pred_and_self = trait_pred.map_bound(|trait_pred| (trait_pred, ty));
1268
1269        let new_obligation =
1270            self.mk_trait_obligation_with_new_self_ty(obligation.param_env, trait_pred_and_self);
1271        if !#[allow(non_exhaustive_omitted_patterns)] match tail_expr.kind {
    hir::ExprKind::Err(_) => true,
    _ => false,
}matches!(tail_expr.kind, hir::ExprKind::Err(_))
1272            && self.predicate_must_hold_modulo_regions(&new_obligation)
1273        {
1274            err.span_suggestion_short(
1275                stmt.span.with_lo(tail_expr.span.hi()),
1276                "remove this semicolon",
1277                "",
1278                Applicability::MachineApplicable,
1279            );
1280        } else {
1281            err.span_label(block.span, "this block is missing a tail expression");
1282        }
1283    }
1284
1285    pub(super) fn suggest_add_clone_to_arg(
1286        &self,
1287        obligation: &PredicateObligation<'tcx>,
1288        err: &mut Diag<'_>,
1289        trait_pred: ty::PolyTraitPredicate<'tcx>,
1290    ) -> bool {
1291        let self_ty = self.resolve_vars_if_possible(trait_pred.self_ty());
1292        self.enter_forall(self_ty, |ty: Ty<'_>| {
1293            let Some(generics) = self.tcx.hir_get_generics(obligation.cause.body_id) else {
1294                return false;
1295            };
1296            let ty::Ref(_, inner_ty, hir::Mutability::Not) = ty.kind() else { return false };
1297            let ty::Param(param) = inner_ty.kind() else { return false };
1298            let ObligationCauseCode::FunctionArg { arg_hir_id, .. } = obligation.cause.code()
1299            else {
1300                return false;
1301            };
1302
1303            let clone_trait = self.tcx.require_lang_item(LangItem::Clone, obligation.cause.span);
1304            let has_clone = |ty| {
1305                self.type_implements_trait(clone_trait, [ty], obligation.param_env)
1306                    .must_apply_modulo_regions()
1307            };
1308
1309            let existing_clone_call = match self.tcx.hir_node(*arg_hir_id) {
1310                // It's just a variable. Propose cloning it.
1311                Node::Expr(Expr { kind: hir::ExprKind::Path(_), .. }) => None,
1312                // It's already a call to `clone()`. We might be able to suggest
1313                // adding a `+ Clone` bound, though.
1314                Node::Expr(Expr {
1315                    kind:
1316                        hir::ExprKind::MethodCall(
1317                            hir::PathSegment { ident, .. },
1318                            _receiver,
1319                            [],
1320                            call_span,
1321                        ),
1322                    hir_id,
1323                    ..
1324                }) if ident.name == sym::clone
1325                    && !call_span.from_expansion()
1326                    && !has_clone(*inner_ty) =>
1327                {
1328                    // We only care about method calls corresponding to the real `Clone` trait.
1329                    let Some(typeck_results) = self.typeck_results.as_ref() else { return false };
1330                    let Some((DefKind::AssocFn, did)) = typeck_results.type_dependent_def(*hir_id)
1331                    else {
1332                        return false;
1333                    };
1334                    if self.tcx.trait_of_assoc(did) != Some(clone_trait) {
1335                        return false;
1336                    }
1337                    Some(ident.span)
1338                }
1339                _ => return false,
1340            };
1341
1342            let new_obligation = self.mk_trait_obligation_with_new_self_ty(
1343                obligation.param_env,
1344                trait_pred.map_bound(|trait_pred| (trait_pred, *inner_ty)),
1345            );
1346
1347            if self.predicate_may_hold(&new_obligation) && has_clone(ty) {
1348                if !has_clone(param.to_ty(self.tcx)) {
1349                    suggest_constraining_type_param(
1350                        self.tcx,
1351                        generics,
1352                        err,
1353                        param.name.as_str(),
1354                        "Clone",
1355                        Some(clone_trait),
1356                        None,
1357                    );
1358                }
1359                if let Some(existing_clone_call) = existing_clone_call {
1360                    err.span_note(
1361                        existing_clone_call,
1362                        ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("this `clone()` copies the reference, which does not do anything, because `{0}` does not implement `Clone`",
                inner_ty))
    })format!(
1363                            "this `clone()` copies the reference, \
1364                            which does not do anything, \
1365                            because `{inner_ty}` does not implement `Clone`"
1366                        ),
1367                    );
1368                } else {
1369                    err.span_suggestion_verbose(
1370                        obligation.cause.span.shrink_to_hi(),
1371                        "consider using clone here",
1372                        ".clone()".to_string(),
1373                        Applicability::MaybeIncorrect,
1374                    );
1375                }
1376                return true;
1377            }
1378            false
1379        })
1380    }
1381
1382    /// Extracts information about a callable type for diagnostics. This is a
1383    /// heuristic -- it doesn't necessarily mean that a type is always callable,
1384    /// because the callable type must also be well-formed to be called.
1385    pub fn extract_callable_info(
1386        &self,
1387        body_id: LocalDefId,
1388        param_env: ty::ParamEnv<'tcx>,
1389        found: Ty<'tcx>,
1390    ) -> Option<(DefIdOrName, Ty<'tcx>, Vec<Ty<'tcx>>)> {
1391        // Autoderef is useful here because sometimes we box callables, etc.
1392        let Some((def_id_or_name, output, inputs)) =
1393            (self.autoderef_steps)(found).into_iter().find_map(|(found, _)| match *found.kind() {
1394                ty::FnPtr(sig_tys, _) => Some((
1395                    DefIdOrName::Name("function pointer"),
1396                    sig_tys.output(),
1397                    sig_tys.inputs(),
1398                )),
1399                ty::FnDef(def_id, _) => {
1400                    let fn_sig = found.fn_sig(self.tcx);
1401                    Some((DefIdOrName::DefId(def_id), fn_sig.output(), fn_sig.inputs()))
1402                }
1403                ty::Closure(def_id, args) => {
1404                    let fn_sig = args.as_closure().sig();
1405                    Some((
1406                        DefIdOrName::DefId(def_id),
1407                        fn_sig.output(),
1408                        fn_sig.inputs().map_bound(|inputs| inputs[0].tuple_fields().as_slice()),
1409                    ))
1410                }
1411                ty::CoroutineClosure(def_id, args) => {
1412                    let sig_parts = args.as_coroutine_closure().coroutine_closure_sig();
1413                    Some((
1414                        DefIdOrName::DefId(def_id),
1415                        sig_parts.map_bound(|sig| {
1416                            sig.to_coroutine(
1417                                self.tcx,
1418                                args.as_coroutine_closure().parent_args(),
1419                                // Just use infer vars here, since we  don't really care
1420                                // what these types are, just that we're returning a coroutine.
1421                                self.next_ty_var(DUMMY_SP),
1422                                self.tcx.coroutine_for_closure(def_id),
1423                                self.next_ty_var(DUMMY_SP),
1424                            )
1425                        }),
1426                        sig_parts.map_bound(|sig| sig.tupled_inputs_ty.tuple_fields().as_slice()),
1427                    ))
1428                }
1429                ty::Alias(ty::AliasTy { kind: ty::Opaque { def_id }, args, .. }) => {
1430                    self.tcx.item_self_bounds(def_id).instantiate(self.tcx, args).iter().find_map(
1431                        |pred| {
1432                            if let ty::ClauseKind::Projection(proj) = pred.kind().skip_binder()
1433                            && self
1434                                .tcx
1435                                .is_lang_item(proj.projection_term.def_id, LangItem::FnOnceOutput)
1436                            // args tuple will always be args[1]
1437                            && let ty::Tuple(args) = proj.projection_term.args.type_at(1).kind()
1438                            {
1439                                Some((
1440                                    DefIdOrName::DefId(def_id),
1441                                    pred.kind().rebind(proj.term.expect_type()),
1442                                    pred.kind().rebind(args.as_slice()),
1443                                ))
1444                            } else {
1445                                None
1446                            }
1447                        },
1448                    )
1449                }
1450                ty::Dynamic(data, _) => data.iter().find_map(|pred| {
1451                    if let ty::ExistentialPredicate::Projection(proj) = pred.skip_binder()
1452                        && self.tcx.is_lang_item(proj.def_id, LangItem::FnOnceOutput)
1453                        // for existential projection, args are shifted over by 1
1454                        && let ty::Tuple(args) = proj.args.type_at(0).kind()
1455                    {
1456                        Some((
1457                            DefIdOrName::Name("trait object"),
1458                            pred.rebind(proj.term.expect_type()),
1459                            pred.rebind(args.as_slice()),
1460                        ))
1461                    } else {
1462                        None
1463                    }
1464                }),
1465                ty::Param(param) => {
1466                    let generics = self.tcx.generics_of(body_id);
1467                    let name = if generics.count() > param.index as usize
1468                        && let def = generics.param_at(param.index as usize, self.tcx)
1469                        && #[allow(non_exhaustive_omitted_patterns)] match def.kind {
    ty::GenericParamDefKind::Type { .. } => true,
    _ => false,
}matches!(def.kind, ty::GenericParamDefKind::Type { .. })
1470                        && def.name == param.name
1471                    {
1472                        DefIdOrName::DefId(def.def_id)
1473                    } else {
1474                        DefIdOrName::Name("type parameter")
1475                    };
1476                    param_env.caller_bounds().iter().find_map(|pred| {
1477                        if let ty::ClauseKind::Projection(proj) = pred.kind().skip_binder()
1478                            && self
1479                                .tcx
1480                                .is_lang_item(proj.projection_term.def_id, LangItem::FnOnceOutput)
1481                            && proj.projection_term.self_ty() == found
1482                            // args tuple will always be args[1]
1483                            && let ty::Tuple(args) = proj.projection_term.args.type_at(1).kind()
1484                        {
1485                            Some((
1486                                name,
1487                                pred.kind().rebind(proj.term.expect_type()),
1488                                pred.kind().rebind(args.as_slice()),
1489                            ))
1490                        } else {
1491                            None
1492                        }
1493                    })
1494                }
1495                _ => None,
1496            })
1497        else {
1498            return None;
1499        };
1500
1501        let output = self.instantiate_binder_with_fresh_vars(
1502            DUMMY_SP,
1503            BoundRegionConversionTime::FnCall,
1504            output,
1505        );
1506        let inputs = inputs
1507            .skip_binder()
1508            .iter()
1509            .map(|ty| {
1510                self.instantiate_binder_with_fresh_vars(
1511                    DUMMY_SP,
1512                    BoundRegionConversionTime::FnCall,
1513                    inputs.rebind(*ty),
1514                )
1515            })
1516            .collect();
1517
1518        // We don't want to register any extra obligations, which should be
1519        // implied by wf, but also because that would possibly result in
1520        // erroneous errors later on.
1521        let InferOk { value: output, obligations: _ } =
1522            self.at(&ObligationCause::dummy(), param_env).normalize(output);
1523
1524        if output.is_ty_var() { None } else { Some((def_id_or_name, output, inputs)) }
1525    }
1526
1527    pub(super) fn suggest_add_reference_to_arg(
1528        &self,
1529        obligation: &PredicateObligation<'tcx>,
1530        err: &mut Diag<'_>,
1531        poly_trait_pred: ty::PolyTraitPredicate<'tcx>,
1532        has_custom_message: bool,
1533    ) -> bool {
1534        let span = obligation.cause.span;
1535        let param_env = obligation.param_env;
1536
1537        let mk_result = |trait_pred_and_new_ty| {
1538            let obligation =
1539                self.mk_trait_obligation_with_new_self_ty(param_env, trait_pred_and_new_ty);
1540            self.predicate_must_hold_modulo_regions(&obligation)
1541        };
1542
1543        let code = match obligation.cause.code() {
1544            ObligationCauseCode::FunctionArg { parent_code, .. } => parent_code,
1545            // FIXME(compiler-errors): This is kind of a mess, but required for obligations
1546            // that come from a path expr to affect the *call* expr.
1547            c @ ObligationCauseCode::WhereClauseInExpr(_, _, hir_id, _)
1548                if self.tcx.hir_span(*hir_id).lo() == span.lo() =>
1549            {
1550                // `hir_id` corresponds to the HIR node that introduced a `where`-clause obligation.
1551                // If that obligation comes from a type in an associated method call, we need
1552                // special handling here.
1553                if let hir::Node::Expr(expr) = self.tcx.parent_hir_node(*hir_id)
1554                    && let hir::ExprKind::Call(base, _) = expr.kind
1555                    && let hir::ExprKind::Path(hir::QPath::TypeRelative(ty, segment)) = base.kind
1556                    && let hir::Node::Expr(outer) = self.tcx.parent_hir_node(expr.hir_id)
1557                    && let hir::ExprKind::AddrOf(hir::BorrowKind::Ref, mtbl, _) = outer.kind
1558                    && ty.span == span
1559                {
1560                    // We've encountered something like `&str::from("")`, where the intended code
1561                    // was likely `<&str>::from("")`. The former is interpreted as "call method
1562                    // `from` on `str` and borrow the result", while the latter means "call method
1563                    // `from` on `&str`".
1564
1565                    let trait_pred_and_imm_ref = poly_trait_pred.map_bound(|p| {
1566                        (p, Ty::new_imm_ref(self.tcx, self.tcx.lifetimes.re_static, p.self_ty()))
1567                    });
1568                    let trait_pred_and_mut_ref = poly_trait_pred.map_bound(|p| {
1569                        (p, Ty::new_mut_ref(self.tcx, self.tcx.lifetimes.re_static, p.self_ty()))
1570                    });
1571
1572                    let imm_ref_self_ty_satisfies_pred = mk_result(trait_pred_and_imm_ref);
1573                    let mut_ref_self_ty_satisfies_pred = mk_result(trait_pred_and_mut_ref);
1574                    let sugg_msg = |pre: &str| {
1575                        ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("you likely meant to call the associated function `{0}` for type `&{2}{1}`, but the code as written calls associated function `{0}` on type `{1}`",
                segment.ident, poly_trait_pred.self_ty(), pre))
    })format!(
1576                            "you likely meant to call the associated function `{FN}` for type \
1577                             `&{pre}{TY}`, but the code as written calls associated function `{FN}` on \
1578                             type `{TY}`",
1579                            FN = segment.ident,
1580                            TY = poly_trait_pred.self_ty(),
1581                        )
1582                    };
1583                    match (imm_ref_self_ty_satisfies_pred, mut_ref_self_ty_satisfies_pred, mtbl) {
1584                        (true, _, hir::Mutability::Not) | (_, true, hir::Mutability::Mut) => {
1585                            err.multipart_suggestion(
1586                                sugg_msg(mtbl.prefix_str()),
1587                                ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [(outer.span.shrink_to_lo(), "<".to_string()),
                (span.shrink_to_hi(), ">".to_string())]))vec![
1588                                    (outer.span.shrink_to_lo(), "<".to_string()),
1589                                    (span.shrink_to_hi(), ">".to_string()),
1590                                ],
1591                                Applicability::MachineApplicable,
1592                            );
1593                        }
1594                        (true, _, hir::Mutability::Mut) => {
1595                            // There's an associated function found on the immutable borrow of the
1596                            err.multipart_suggestion(
1597                                sugg_msg("mut "),
1598                                ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [(outer.span.shrink_to_lo().until(span), "<&".to_string()),
                (span.shrink_to_hi(), ">".to_string())]))vec![
1599                                    (outer.span.shrink_to_lo().until(span), "<&".to_string()),
1600                                    (span.shrink_to_hi(), ">".to_string()),
1601                                ],
1602                                Applicability::MachineApplicable,
1603                            );
1604                        }
1605                        (_, true, hir::Mutability::Not) => {
1606                            err.multipart_suggestion(
1607                                sugg_msg(""),
1608                                ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [(outer.span.shrink_to_lo().until(span), "<&mut ".to_string()),
                (span.shrink_to_hi(), ">".to_string())]))vec![
1609                                    (outer.span.shrink_to_lo().until(span), "<&mut ".to_string()),
1610                                    (span.shrink_to_hi(), ">".to_string()),
1611                                ],
1612                                Applicability::MachineApplicable,
1613                            );
1614                        }
1615                        _ => {}
1616                    }
1617                    // If we didn't return early here, we would instead suggest `&&str::from("")`.
1618                    return false;
1619                }
1620                c
1621            }
1622            c if #[allow(non_exhaustive_omitted_patterns)] match span.ctxt().outer_expn_data().kind
    {
    ExpnKind::Desugaring(DesugaringKind::ForLoop) => true,
    _ => false,
}matches!(
1623                span.ctxt().outer_expn_data().kind,
1624                ExpnKind::Desugaring(DesugaringKind::ForLoop)
1625            ) =>
1626            {
1627                c
1628            }
1629            _ => return false,
1630        };
1631
1632        // List of traits for which it would be nonsensical to suggest borrowing.
1633        // For instance, immutable references are always Copy, so suggesting to
1634        // borrow would always succeed, but it's probably not what the user wanted.
1635        let mut never_suggest_borrow: Vec<_> =
1636            [LangItem::Copy, LangItem::Clone, LangItem::Unpin, LangItem::Sized]
1637                .iter()
1638                .filter_map(|lang_item| self.tcx.lang_items().get(*lang_item))
1639                .collect();
1640
1641        if let Some(def_id) = self.tcx.get_diagnostic_item(sym::Send) {
1642            never_suggest_borrow.push(def_id);
1643        }
1644
1645        // Try to apply the original trait bound by borrowing.
1646        let mut try_borrowing = |old_pred: ty::PolyTraitPredicate<'tcx>,
1647                                 blacklist: &[DefId]|
1648         -> bool {
1649            if blacklist.contains(&old_pred.def_id()) {
1650                return false;
1651            }
1652            // We map bounds to `&T` and `&mut T`
1653            let trait_pred_and_imm_ref = old_pred.map_bound(|trait_pred| {
1654                (
1655                    trait_pred,
1656                    Ty::new_imm_ref(self.tcx, self.tcx.lifetimes.re_static, trait_pred.self_ty()),
1657                )
1658            });
1659            let trait_pred_and_mut_ref = old_pred.map_bound(|trait_pred| {
1660                (
1661                    trait_pred,
1662                    Ty::new_mut_ref(self.tcx, self.tcx.lifetimes.re_static, trait_pred.self_ty()),
1663                )
1664            });
1665
1666            let imm_ref_self_ty_satisfies_pred = mk_result(trait_pred_and_imm_ref);
1667            let mut_ref_self_ty_satisfies_pred = mk_result(trait_pred_and_mut_ref);
1668
1669            let (ref_inner_ty_satisfies_pred, ref_inner_ty_is_mut) =
1670                if let ObligationCauseCode::WhereClauseInExpr(..) = obligation.cause.code()
1671                    && let ty::Ref(_, ty, mutability) = old_pred.self_ty().skip_binder().kind()
1672                {
1673                    (
1674                        mk_result(old_pred.map_bound(|trait_pred| (trait_pred, *ty))),
1675                        mutability.is_mut(),
1676                    )
1677                } else {
1678                    (false, false)
1679                };
1680
1681            let is_immut = imm_ref_self_ty_satisfies_pred
1682                || (ref_inner_ty_satisfies_pred && !ref_inner_ty_is_mut);
1683            let is_mut = mut_ref_self_ty_satisfies_pred || ref_inner_ty_is_mut;
1684            if !is_immut && !is_mut {
1685                return false;
1686            }
1687            let Ok(_snippet) = self.tcx.sess.source_map().span_to_snippet(span) else {
1688                return false;
1689            };
1690            // We don't want a borrowing suggestion on the fields in structs
1691            // ```
1692            // #[derive(Clone)]
1693            // struct Foo {
1694            //     the_foos: Vec<Foo>
1695            // }
1696            // ```
1697            if !#[allow(non_exhaustive_omitted_patterns)] match span.ctxt().outer_expn_data().kind
    {
    ExpnKind::Root | ExpnKind::Desugaring(DesugaringKind::ForLoop) => true,
    _ => false,
}matches!(
1698                span.ctxt().outer_expn_data().kind,
1699                ExpnKind::Root | ExpnKind::Desugaring(DesugaringKind::ForLoop)
1700            ) {
1701                return false;
1702            }
1703            // We have a very specific type of error, where just borrowing this argument
1704            // might solve the problem. In cases like this, the important part is the
1705            // original type obligation, not the last one that failed, which is arbitrary.
1706            // Because of this, we modify the error to refer to the original obligation and
1707            // return early in the caller.
1708
1709            let mut label = || {
1710                // Special case `Sized` as `old_pred` will be the trait itself instead of
1711                // `Sized` when the trait bound is the source of the error.
1712                let is_sized = match obligation.predicate.kind().skip_binder() {
1713                    ty::PredicateKind::Clause(ty::ClauseKind::Trait(trait_pred)) => {
1714                        self.tcx.is_lang_item(trait_pred.def_id(), LangItem::Sized)
1715                    }
1716                    _ => false,
1717                };
1718
1719                let msg = ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("the trait bound `{0}` is not satisfied",
                self.tcx.short_string(old_pred, err.long_ty_path())))
    })format!(
1720                    "the trait bound `{}` is not satisfied",
1721                    self.tcx.short_string(old_pred, err.long_ty_path()),
1722                );
1723                let self_ty_str = self.tcx.short_string(old_pred.self_ty(), err.long_ty_path());
1724                let trait_path = self
1725                    .tcx
1726                    .short_string(old_pred.print_modifiers_and_trait_path(), err.long_ty_path());
1727
1728                if has_custom_message {
1729                    let msg = if is_sized {
1730                        "the trait bound `Sized` is not satisfied".into()
1731                    } else {
1732                        msg
1733                    };
1734                    err.note(msg);
1735                } else {
1736                    err.messages = ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [(rustc_errors::DiagMessage::from(msg), Style::NoStyle)]))vec![(rustc_errors::DiagMessage::from(msg), Style::NoStyle)];
1737                }
1738                if is_sized {
1739                    err.span_label(
1740                        span,
1741                        ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("the trait `Sized` is not implemented for `{0}`",
                self_ty_str))
    })format!("the trait `Sized` is not implemented for `{self_ty_str}`"),
1742                    );
1743                } else {
1744                    err.span_label(
1745                        span,
1746                        ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("the trait `{0}` is not implemented for `{1}`",
                trait_path, self_ty_str))
    })format!("the trait `{trait_path}` is not implemented for `{self_ty_str}`"),
1747                    );
1748                }
1749            };
1750
1751            let mut sugg_prefixes = ::alloc::vec::Vec::new()vec![];
1752            if is_immut {
1753                sugg_prefixes.push("&");
1754            }
1755            if is_mut {
1756                sugg_prefixes.push("&mut ");
1757            }
1758            let sugg_msg = ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("consider{0} borrowing here",
                if is_mut && !is_immut { " mutably" } else { "" }))
    })format!(
1759                "consider{} borrowing here",
1760                if is_mut && !is_immut { " mutably" } else { "" },
1761            );
1762
1763            // Issue #104961, we need to add parentheses properly for compound expressions
1764            // for example, `x.starts_with("hi".to_string() + "you")`
1765            // should be `x.starts_with(&("hi".to_string() + "you"))`
1766            let Some(body) = self.tcx.hir_maybe_body_owned_by(obligation.cause.body_id) else {
1767                return false;
1768            };
1769            let mut expr_finder = FindExprBySpan::new(span, self.tcx);
1770            expr_finder.visit_expr(body.value);
1771
1772            if let Some(ty) = expr_finder.ty_result {
1773                if let hir::Node::Expr(expr) = self.tcx.parent_hir_node(ty.hir_id)
1774                    && let hir::ExprKind::Path(hir::QPath::TypeRelative(_, _)) = expr.kind
1775                    && ty.span == span
1776                {
1777                    // We've encountered something like `str::from("")`, where the intended code
1778                    // was likely `<&str>::from("")`. #143393.
1779                    label();
1780                    err.multipart_suggestions(
1781                        sugg_msg,
1782                        sugg_prefixes.into_iter().map(|sugg_prefix| {
1783                            ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [(span.shrink_to_lo(),
                    ::alloc::__export::must_use({
                            ::alloc::fmt::format(format_args!("<{0}", sugg_prefix))
                        })), (span.shrink_to_hi(), ">".to_string())]))vec![
1784                                (span.shrink_to_lo(), format!("<{sugg_prefix}")),
1785                                (span.shrink_to_hi(), ">".to_string()),
1786                            ]
1787                        }),
1788                        Applicability::MaybeIncorrect,
1789                    );
1790                    return true;
1791                }
1792                return false;
1793            }
1794            let Some(expr) = expr_finder.result else {
1795                return false;
1796            };
1797            if let hir::ExprKind::AddrOf(_, _, _) = expr.kind {
1798                return false;
1799            }
1800            let needs_parens_post = expr_needs_parens(expr);
1801            let needs_parens_pre = match self.tcx.parent_hir_node(expr.hir_id) {
1802                Node::Expr(e)
1803                    if let hir::ExprKind::MethodCall(_, base, _, _) = e.kind
1804                        && base.hir_id == expr.hir_id =>
1805                {
1806                    true
1807                }
1808                _ => false,
1809            };
1810
1811            label();
1812            let suggestions = sugg_prefixes.into_iter().map(|sugg_prefix| {
1813                match (needs_parens_pre, needs_parens_post) {
1814                    (false, false) => ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [(span.shrink_to_lo(), sugg_prefix.to_string())]))vec![(span.shrink_to_lo(), sugg_prefix.to_string())],
1815                    // We have something like `foo.bar()`, where we want to bororw foo, so we need
1816                    // to suggest `(&mut foo).bar()`.
1817                    (false, true) => ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [(span.shrink_to_lo(),
                    ::alloc::__export::must_use({
                            ::alloc::fmt::format(format_args!("{0}(", sugg_prefix))
                        })), (span.shrink_to_hi(), ")".to_string())]))vec![
1818                        (span.shrink_to_lo(), format!("{sugg_prefix}(")),
1819                        (span.shrink_to_hi(), ")".to_string()),
1820                    ],
1821                    // Issue #109436, we need to add parentheses properly for method calls
1822                    // for example, `foo.into()` should be `(&foo).into()`
1823                    (true, false) => ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [(span.shrink_to_lo(),
                    ::alloc::__export::must_use({
                            ::alloc::fmt::format(format_args!("({0}", sugg_prefix))
                        })), (span.shrink_to_hi(), ")".to_string())]))vec![
1824                        (span.shrink_to_lo(), format!("({sugg_prefix}")),
1825                        (span.shrink_to_hi(), ")".to_string()),
1826                    ],
1827                    (true, true) => ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [(span.shrink_to_lo(),
                    ::alloc::__export::must_use({
                            ::alloc::fmt::format(format_args!("({0}(", sugg_prefix))
                        })), (span.shrink_to_hi(), "))".to_string())]))vec![
1828                        (span.shrink_to_lo(), format!("({sugg_prefix}(")),
1829                        (span.shrink_to_hi(), "))".to_string()),
1830                    ],
1831                }
1832            });
1833            err.multipart_suggestions(sugg_msg, suggestions, Applicability::MaybeIncorrect);
1834            return true;
1835        };
1836
1837        if let ObligationCauseCode::ImplDerived(cause) = &*code {
1838            try_borrowing(cause.derived.parent_trait_pred, &[])
1839        } else if let ObligationCauseCode::WhereClause(..)
1840        | ObligationCauseCode::WhereClauseInExpr(..) = code
1841        {
1842            try_borrowing(poly_trait_pred, &never_suggest_borrow)
1843        } else {
1844            false
1845        }
1846    }
1847
1848    // Suggest borrowing the type
1849    pub(super) fn suggest_borrowing_for_object_cast(
1850        &self,
1851        err: &mut Diag<'_>,
1852        obligation: &PredicateObligation<'tcx>,
1853        self_ty: Ty<'tcx>,
1854        target_ty: Ty<'tcx>,
1855    ) {
1856        let ty::Ref(_, object_ty, hir::Mutability::Not) = target_ty.kind() else {
1857            return;
1858        };
1859        let ty::Dynamic(predicates, _) = object_ty.kind() else {
1860            return;
1861        };
1862        let self_ref_ty = Ty::new_imm_ref(self.tcx, self.tcx.lifetimes.re_erased, self_ty);
1863
1864        for predicate in predicates.iter() {
1865            if !self.predicate_must_hold_modulo_regions(
1866                &obligation.with(self.tcx, predicate.with_self_ty(self.tcx, self_ref_ty)),
1867            ) {
1868                return;
1869            }
1870        }
1871
1872        err.span_suggestion_verbose(
1873            obligation.cause.span.shrink_to_lo(),
1874            ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("consider borrowing the value, since `&{0}` can be coerced into `{1}`",
                self_ty, target_ty))
    })format!(
1875                "consider borrowing the value, since `&{self_ty}` can be coerced into `{target_ty}`"
1876            ),
1877            "&",
1878            Applicability::MaybeIncorrect,
1879        );
1880    }
1881
1882    /// Whenever references are used by mistake, like `for (i, e) in &vec.iter().enumerate()`,
1883    /// suggest removing these references until we reach a type that implements the trait.
1884    pub(super) fn suggest_remove_reference(
1885        &self,
1886        obligation: &PredicateObligation<'tcx>,
1887        err: &mut Diag<'_>,
1888        trait_pred: ty::PolyTraitPredicate<'tcx>,
1889    ) -> bool {
1890        let mut span = obligation.cause.span;
1891        let mut trait_pred = trait_pred;
1892        let mut code = obligation.cause.code();
1893        while let Some((c, Some(parent_trait_pred))) = code.parent_with_predicate() {
1894            // We want the root obligation, in order to detect properly handle
1895            // `for _ in &mut &mut vec![] {}`.
1896            code = c;
1897            trait_pred = parent_trait_pred;
1898        }
1899        while span.desugaring_kind().is_some() {
1900            // Remove all the hir desugaring contexts while maintaining the macro contexts.
1901            span.remove_mark();
1902        }
1903        let mut expr_finder = super::FindExprBySpan::new(span, self.tcx);
1904        let Some(body) = self.tcx.hir_maybe_body_owned_by(obligation.cause.body_id) else {
1905            return false;
1906        };
1907        expr_finder.visit_expr(body.value);
1908        let mut maybe_suggest = |suggested_ty, count, suggestions| {
1909            // Remapping bound vars here
1910            let trait_pred_and_suggested_ty =
1911                trait_pred.map_bound(|trait_pred| (trait_pred, suggested_ty));
1912
1913            let new_obligation = self.mk_trait_obligation_with_new_self_ty(
1914                obligation.param_env,
1915                trait_pred_and_suggested_ty,
1916            );
1917
1918            if self.predicate_may_hold(&new_obligation) {
1919                let msg = if count == 1 {
1920                    "consider removing the leading `&`-reference".to_string()
1921                } else {
1922                    ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("consider removing {0} leading `&`-references",
                count))
    })format!("consider removing {count} leading `&`-references")
1923                };
1924
1925                err.multipart_suggestion(msg, suggestions, Applicability::MachineApplicable);
1926                true
1927            } else {
1928                false
1929            }
1930        };
1931
1932        // Maybe suggest removal of borrows from types in type parameters, like in
1933        // `src/test/ui/not-panic/not-panic-safe.rs`.
1934        let mut count = 0;
1935        let mut suggestions = ::alloc::vec::Vec::new()vec![];
1936        // Skipping binder here, remapping below
1937        let mut suggested_ty = trait_pred.self_ty().skip_binder();
1938        if let Some(mut hir_ty) = expr_finder.ty_result {
1939            while let hir::TyKind::Ref(_, mut_ty) = &hir_ty.kind {
1940                count += 1;
1941                let span = hir_ty.span.until(mut_ty.ty.span);
1942                suggestions.push((span, String::new()));
1943
1944                let ty::Ref(_, inner_ty, _) = suggested_ty.kind() else {
1945                    break;
1946                };
1947                suggested_ty = *inner_ty;
1948
1949                hir_ty = mut_ty.ty;
1950
1951                if maybe_suggest(suggested_ty, count, suggestions.clone()) {
1952                    return true;
1953                }
1954            }
1955        }
1956
1957        // Maybe suggest removal of borrows from expressions, like in `for i in &&&foo {}`.
1958        let Some(mut expr) = expr_finder.result else {
1959            return false;
1960        };
1961        let mut count = 0;
1962        let mut suggestions = ::alloc::vec::Vec::new()vec![];
1963        // Skipping binder here, remapping below
1964        let mut suggested_ty = trait_pred.self_ty().skip_binder();
1965        'outer: loop {
1966            while let hir::ExprKind::AddrOf(_, _, borrowed) = expr.kind {
1967                count += 1;
1968                let span =
1969                    if let Some(borrowed_span) = borrowed.span.find_ancestor_inside(expr.span) {
1970                        expr.span.until(borrowed_span)
1971                    } else {
1972                        break 'outer;
1973                    };
1974
1975                // Double check that the span we extracted actually corresponds to a borrow,
1976                // rather than some macro garbage.
1977                match self.tcx.sess.source_map().span_to_snippet(span) {
1978                    Ok(snippet) if snippet.starts_with("&") => {}
1979                    _ => break 'outer,
1980                }
1981
1982                suggestions.push((span, String::new()));
1983
1984                let ty::Ref(_, inner_ty, _) = suggested_ty.kind() else {
1985                    break 'outer;
1986                };
1987                suggested_ty = *inner_ty;
1988
1989                expr = borrowed;
1990
1991                if maybe_suggest(suggested_ty, count, suggestions.clone()) {
1992                    return true;
1993                }
1994            }
1995            if let hir::ExprKind::Path(hir::QPath::Resolved(None, path)) = expr.kind
1996                && let Res::Local(hir_id) = path.res
1997                && let hir::Node::Pat(binding) = self.tcx.hir_node(hir_id)
1998                && let hir::Node::LetStmt(local) = self.tcx.parent_hir_node(binding.hir_id)
1999                && let None = local.ty
2000                && let Some(binding_expr) = local.init
2001            {
2002                expr = binding_expr;
2003            } else {
2004                break 'outer;
2005            }
2006        }
2007        false
2008    }
2009
2010    pub(super) fn suggest_remove_await(
2011        &self,
2012        obligation: &PredicateObligation<'tcx>,
2013        err: &mut Diag<'_>,
2014    ) {
2015        if let ObligationCauseCode::AwaitableExpr(hir_id) = obligation.cause.code().peel_derives()
2016            && let hir::Node::Expr(expr) = self.tcx.hir_node(*hir_id)
2017        {
2018            // FIXME: use `obligation.predicate.kind()...trait_ref.self_ty()` to see if we have `()`
2019            // and if not maybe suggest doing something else? If we kept the expression around we
2020            // could also check if it is an fn call (very likely) and suggest changing *that*, if
2021            // it is from the local crate.
2022
2023            // use nth(1) to skip one layer of desugaring from `IntoIter::into_iter`
2024            if let Some((_, hir::Node::Expr(await_expr))) = self.tcx.hir_parent_iter(*hir_id).nth(1)
2025                && let Some(expr_span) = expr.span.find_ancestor_inside_same_ctxt(await_expr.span)
2026            {
2027                let removal_span = self
2028                    .tcx
2029                    .sess
2030                    .source_map()
2031                    .span_extend_while_whitespace(expr_span)
2032                    .shrink_to_hi()
2033                    .to(await_expr.span.shrink_to_hi());
2034                err.span_suggestion_verbose(
2035                    removal_span,
2036                    "remove the `.await`",
2037                    "",
2038                    Applicability::MachineApplicable,
2039                );
2040            } else {
2041                err.span_label(obligation.cause.span, "remove the `.await`");
2042            }
2043            // FIXME: account for associated `async fn`s.
2044            if let hir::Expr { span, kind: hir::ExprKind::Call(base, _), .. } = expr {
2045                if let ty::PredicateKind::Clause(ty::ClauseKind::Trait(pred)) =
2046                    obligation.predicate.kind().skip_binder()
2047                {
2048                    err.span_label(*span, ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("this call returns `{0}`",
                pred.self_ty()))
    })format!("this call returns `{}`", pred.self_ty()));
2049                }
2050                if let Some(typeck_results) = &self.typeck_results
2051                    && let ty = typeck_results.expr_ty_adjusted(base)
2052                    && let ty::FnDef(def_id, _args) = ty.kind()
2053                    && let Some(hir::Node::Item(item)) = self.tcx.hir_get_if_local(*def_id)
2054                {
2055                    let (ident, _, _, _) = item.expect_fn();
2056                    let msg = ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("alternatively, consider making `fn {0}` asynchronous",
                ident))
    })format!("alternatively, consider making `fn {ident}` asynchronous");
2057                    if item.vis_span.is_empty() {
2058                        err.span_suggestion_verbose(
2059                            item.span.shrink_to_lo(),
2060                            msg,
2061                            "async ",
2062                            Applicability::MaybeIncorrect,
2063                        );
2064                    } else {
2065                        err.span_suggestion_verbose(
2066                            item.vis_span.shrink_to_hi(),
2067                            msg,
2068                            " async",
2069                            Applicability::MaybeIncorrect,
2070                        );
2071                    }
2072                }
2073            }
2074        }
2075    }
2076
2077    /// Check if the trait bound is implemented for a different mutability and note it in the
2078    /// final error.
2079    pub(super) fn suggest_change_mut(
2080        &self,
2081        obligation: &PredicateObligation<'tcx>,
2082        err: &mut Diag<'_>,
2083        trait_pred: ty::PolyTraitPredicate<'tcx>,
2084    ) {
2085        let points_at_arg =
2086            #[allow(non_exhaustive_omitted_patterns)] match obligation.cause.code() {
    ObligationCauseCode::FunctionArg { .. } => true,
    _ => false,
}matches!(obligation.cause.code(), ObligationCauseCode::FunctionArg { .. },);
2087
2088        let span = obligation.cause.span;
2089        if let Ok(snippet) = self.tcx.sess.source_map().span_to_snippet(span) {
2090            let refs_number =
2091                snippet.chars().filter(|c| !c.is_whitespace()).take_while(|c| *c == '&').count();
2092            if let Some('\'') = snippet.chars().filter(|c| !c.is_whitespace()).nth(refs_number) {
2093                // Do not suggest removal of borrow from type arguments.
2094                return;
2095            }
2096            let trait_pred = self.resolve_vars_if_possible(trait_pred);
2097            if trait_pred.has_non_region_infer() {
2098                // Do not ICE while trying to find if a reborrow would succeed on a trait with
2099                // unresolved bindings.
2100                return;
2101            }
2102
2103            // Skipping binder here, remapping below
2104            if let ty::Ref(region, t_type, mutability) = *trait_pred.skip_binder().self_ty().kind()
2105            {
2106                let suggested_ty = match mutability {
2107                    hir::Mutability::Mut => Ty::new_imm_ref(self.tcx, region, t_type),
2108                    hir::Mutability::Not => Ty::new_mut_ref(self.tcx, region, t_type),
2109                };
2110
2111                // Remapping bound vars here
2112                let trait_pred_and_suggested_ty =
2113                    trait_pred.map_bound(|trait_pred| (trait_pred, suggested_ty));
2114
2115                let new_obligation = self.mk_trait_obligation_with_new_self_ty(
2116                    obligation.param_env,
2117                    trait_pred_and_suggested_ty,
2118                );
2119                let suggested_ty_would_satisfy_obligation = self
2120                    .evaluate_obligation_no_overflow(&new_obligation)
2121                    .must_apply_modulo_regions();
2122                if suggested_ty_would_satisfy_obligation {
2123                    let sp = self
2124                        .tcx
2125                        .sess
2126                        .source_map()
2127                        .span_take_while(span, |c| c.is_whitespace() || *c == '&');
2128                    if points_at_arg && mutability.is_not() && refs_number > 0 {
2129                        // If we have a call like foo(&mut buf), then don't suggest foo(&mut mut buf)
2130                        if snippet
2131                            .trim_start_matches(|c: char| c.is_whitespace() || c == '&')
2132                            .starts_with("mut")
2133                        {
2134                            return;
2135                        }
2136                        err.span_suggestion_verbose(
2137                            sp,
2138                            "consider changing this borrow's mutability",
2139                            "&mut ",
2140                            Applicability::MachineApplicable,
2141                        );
2142                    } else {
2143                        err.note(::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("`{0}` is implemented for `{1}`, but not for `{2}`",
                trait_pred.print_modifiers_and_trait_path(), suggested_ty,
                trait_pred.skip_binder().self_ty()))
    })format!(
2144                            "`{}` is implemented for `{}`, but not for `{}`",
2145                            trait_pred.print_modifiers_and_trait_path(),
2146                            suggested_ty,
2147                            trait_pred.skip_binder().self_ty(),
2148                        ));
2149                    }
2150                }
2151            }
2152        }
2153    }
2154
2155    pub(super) fn suggest_semicolon_removal(
2156        &self,
2157        obligation: &PredicateObligation<'tcx>,
2158        err: &mut Diag<'_>,
2159        span: Span,
2160        trait_pred: ty::PolyTraitPredicate<'tcx>,
2161    ) -> bool {
2162        let node = self.tcx.hir_node_by_def_id(obligation.cause.body_id);
2163        if let hir::Node::Item(hir::Item { kind: hir::ItemKind::Fn {sig, body: body_id, .. }, .. }) = node
2164            && let hir::ExprKind::Block(blk, _) = &self.tcx.hir_body(*body_id).value.kind
2165            && sig.decl.output.span().overlaps(span)
2166            && blk.expr.is_none()
2167            && trait_pred.self_ty().skip_binder().is_unit()
2168            && let Some(stmt) = blk.stmts.last()
2169            && let hir::StmtKind::Semi(expr) = stmt.kind
2170            // Only suggest this if the expression behind the semicolon implements the predicate
2171            && let Some(typeck_results) = &self.typeck_results
2172            && let Some(ty) = typeck_results.expr_ty_opt(expr)
2173            && self.predicate_may_hold(&self.mk_trait_obligation_with_new_self_ty(
2174                obligation.param_env, trait_pred.map_bound(|trait_pred| (trait_pred, ty))
2175            ))
2176        {
2177            err.span_label(
2178                expr.span,
2179                ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("this expression has type `{0}`, which implements `{1}`",
                ty, trait_pred.print_modifiers_and_trait_path()))
    })format!(
2180                    "this expression has type `{}`, which implements `{}`",
2181                    ty,
2182                    trait_pred.print_modifiers_and_trait_path()
2183                ),
2184            );
2185            err.span_suggestion(
2186                self.tcx.sess.source_map().end_point(stmt.span),
2187                "remove this semicolon",
2188                "",
2189                Applicability::MachineApplicable,
2190            );
2191            return true;
2192        }
2193        false
2194    }
2195
2196    pub(super) fn return_type_span(&self, obligation: &PredicateObligation<'tcx>) -> Option<Span> {
2197        let hir::Node::Item(hir::Item { kind: hir::ItemKind::Fn { sig, .. }, .. }) =
2198            self.tcx.hir_node_by_def_id(obligation.cause.body_id)
2199        else {
2200            return None;
2201        };
2202
2203        if let hir::FnRetTy::Return(ret_ty) = sig.decl.output { Some(ret_ty.span) } else { None }
2204    }
2205
2206    /// If all conditions are met to identify a returned `dyn Trait`, suggest using `impl Trait` if
2207    /// applicable and signal that the error has been expanded appropriately and needs to be
2208    /// emitted.
2209    pub(super) fn suggest_impl_trait(
2210        &self,
2211        err: &mut Diag<'_>,
2212        obligation: &PredicateObligation<'tcx>,
2213        trait_pred: ty::PolyTraitPredicate<'tcx>,
2214    ) -> bool {
2215        let ObligationCauseCode::SizedReturnType = obligation.cause.code() else {
2216            return false;
2217        };
2218        let ty::Dynamic(_, _) = trait_pred.self_ty().skip_binder().kind() else {
2219            return false;
2220        };
2221        if let Node::Item(hir::Item { kind: hir::ItemKind::Fn { sig: fn_sig, .. }, .. })
2222        | Node::ImplItem(hir::ImplItem { kind: hir::ImplItemKind::Fn(fn_sig, _), .. })
2223        | Node::TraitItem(hir::TraitItem { kind: hir::TraitItemKind::Fn(fn_sig, _), .. }) =
2224            self.tcx.hir_node_by_def_id(obligation.cause.body_id)
2225            && let hir::FnRetTy::Return(ty) = fn_sig.decl.output
2226            && let hir::TyKind::Path(qpath) = ty.kind
2227            && let hir::QPath::Resolved(None, path) = qpath
2228            && let Res::Def(DefKind::TyAlias, def_id) = path.res
2229        {
2230            // Do not suggest
2231            // type T = dyn Trait;
2232            // fn foo() -> impl T { .. }
2233            err.span_note(self.tcx.def_span(def_id), "this type alias is unsized");
2234            err.multipart_suggestion(
2235                ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("consider boxing the return type, and wrapping all of the returned values in `Box::new`"))
    })format!(
2236                    "consider boxing the return type, and wrapping all of the returned values in \
2237                    `Box::new`",
2238                ),
2239                ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [(ty.span.shrink_to_lo(), "Box<".to_string()),
                (ty.span.shrink_to_hi(), ">".to_string())]))vec![
2240                    (ty.span.shrink_to_lo(), "Box<".to_string()),
2241                    (ty.span.shrink_to_hi(), ">".to_string()),
2242                ],
2243                Applicability::MaybeIncorrect,
2244            );
2245            return false;
2246        }
2247
2248        err.code(E0746);
2249        err.primary_message("return type cannot be a trait object without pointer indirection");
2250        err.children.clear();
2251
2252        let mut span = obligation.cause.span;
2253        if let DefKind::Closure = self.tcx.def_kind(obligation.cause.body_id)
2254            && let parent = self.tcx.local_parent(obligation.cause.body_id)
2255            && let DefKind::Fn | DefKind::AssocFn = self.tcx.def_kind(parent)
2256            && self.tcx.asyncness(parent).is_async()
2257            && let Node::Item(hir::Item { kind: hir::ItemKind::Fn { sig: fn_sig, .. }, .. })
2258            | Node::ImplItem(hir::ImplItem { kind: hir::ImplItemKind::Fn(fn_sig, _), .. })
2259            | Node::TraitItem(hir::TraitItem {
2260                kind: hir::TraitItemKind::Fn(fn_sig, _), ..
2261            }) = self.tcx.hir_node_by_def_id(parent)
2262        {
2263            // Do not suggest (#147894)
2264            // async fn foo() -> dyn Display impl { .. }
2265            // and
2266            // async fn foo() -> dyn Display Box<dyn { .. }>
2267            span = fn_sig.decl.output.span();
2268            err.span(span);
2269        }
2270        let body = self.tcx.hir_body_owned_by(obligation.cause.body_id);
2271
2272        let mut visitor = ReturnsVisitor::default();
2273        visitor.visit_body(&body);
2274
2275        let (pre, impl_span) = if let Ok(snip) = self.tcx.sess.source_map().span_to_snippet(span)
2276            && snip.starts_with("dyn ")
2277        {
2278            ("", span.with_hi(span.lo() + BytePos(4)))
2279        } else {
2280            ("dyn ", span.shrink_to_lo())
2281        };
2282
2283        err.span_suggestion_verbose(
2284            impl_span,
2285            "consider returning an `impl Trait` instead of a `dyn Trait`",
2286            "impl ",
2287            Applicability::MaybeIncorrect,
2288        );
2289
2290        let mut sugg = ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [(span.shrink_to_lo(),
                    ::alloc::__export::must_use({
                            ::alloc::fmt::format(format_args!("Box<{0}", pre))
                        })), (span.shrink_to_hi(), ">".to_string())]))vec![
2291            (span.shrink_to_lo(), format!("Box<{pre}")),
2292            (span.shrink_to_hi(), ">".to_string()),
2293        ];
2294        sugg.extend(visitor.returns.into_iter().flat_map(|expr| {
2295            let span =
2296                expr.span.find_ancestor_in_same_ctxt(obligation.cause.span).unwrap_or(expr.span);
2297            if !span.can_be_used_for_suggestions() {
2298                ::alloc::vec::Vec::new()vec![]
2299            } else if let hir::ExprKind::Call(path, ..) = expr.kind
2300                && let hir::ExprKind::Path(hir::QPath::TypeRelative(ty, method)) = path.kind
2301                && method.ident.name == sym::new
2302                && let hir::TyKind::Path(hir::QPath::Resolved(.., box_path)) = ty.kind
2303                && box_path
2304                    .res
2305                    .opt_def_id()
2306                    .is_some_and(|def_id| self.tcx.is_lang_item(def_id, LangItem::OwnedBox))
2307            {
2308                // Don't box `Box::new`
2309                ::alloc::vec::Vec::new()vec![]
2310            } else {
2311                ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [(span.shrink_to_lo(), "Box::new(".to_string()),
                (span.shrink_to_hi(), ")".to_string())]))vec![
2312                    (span.shrink_to_lo(), "Box::new(".to_string()),
2313                    (span.shrink_to_hi(), ")".to_string()),
2314                ]
2315            }
2316        }));
2317
2318        err.multipart_suggestion(
2319            ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("alternatively, box the return type, and wrap all of the returned values in `Box::new`"))
    })format!(
2320                "alternatively, box the return type, and wrap all of the returned values in \
2321                 `Box::new`",
2322            ),
2323            sugg,
2324            Applicability::MaybeIncorrect,
2325        );
2326
2327        true
2328    }
2329
2330    pub(super) fn report_closure_arg_mismatch(
2331        &self,
2332        span: Span,
2333        found_span: Option<Span>,
2334        found: ty::TraitRef<'tcx>,
2335        expected: ty::TraitRef<'tcx>,
2336        cause: &ObligationCauseCode<'tcx>,
2337        found_node: Option<Node<'_>>,
2338        param_env: ty::ParamEnv<'tcx>,
2339    ) -> Diag<'a> {
2340        pub(crate) fn build_fn_sig_ty<'tcx>(
2341            infcx: &InferCtxt<'tcx>,
2342            trait_ref: ty::TraitRef<'tcx>,
2343        ) -> Ty<'tcx> {
2344            let inputs = trait_ref.args.type_at(1);
2345            let sig = match inputs.kind() {
2346                ty::Tuple(inputs) if infcx.tcx.is_fn_trait(trait_ref.def_id) => {
2347                    infcx.tcx.mk_fn_sig(
2348                        *inputs,
2349                        infcx.next_ty_var(DUMMY_SP),
2350                        false,
2351                        hir::Safety::Safe,
2352                        ExternAbi::Rust,
2353                    )
2354                }
2355                _ => infcx.tcx.mk_fn_sig(
2356                    [inputs],
2357                    infcx.next_ty_var(DUMMY_SP),
2358                    false,
2359                    hir::Safety::Safe,
2360                    ExternAbi::Rust,
2361                ),
2362            };
2363
2364            Ty::new_fn_ptr(infcx.tcx, ty::Binder::dummy(sig))
2365        }
2366
2367        let argument_kind = match expected.self_ty().kind() {
2368            ty::Closure(..) => "closure",
2369            ty::Coroutine(..) => "coroutine",
2370            _ => "function",
2371        };
2372        let mut err = {
    self.dcx().struct_span_err(span,
            ::alloc::__export::must_use({
                    ::alloc::fmt::format(format_args!("type mismatch in {0} arguments",
                            argument_kind))
                })).with_code(E0631)
}struct_span_code_err!(
2373            self.dcx(),
2374            span,
2375            E0631,
2376            "type mismatch in {argument_kind} arguments",
2377        );
2378
2379        err.span_label(span, "expected due to this");
2380
2381        let found_span = found_span.unwrap_or(span);
2382        err.span_label(found_span, "found signature defined here");
2383
2384        let expected = build_fn_sig_ty(self, expected);
2385        let found = build_fn_sig_ty(self, found);
2386
2387        let (expected_str, found_str) = self.cmp(expected, found);
2388
2389        let signature_kind = ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{0} signature", argument_kind))
    })format!("{argument_kind} signature");
2390        err.note_expected_found(&signature_kind, expected_str, &signature_kind, found_str);
2391
2392        self.note_conflicting_fn_args(&mut err, cause, expected, found, param_env);
2393        self.note_conflicting_closure_bounds(cause, &mut err);
2394
2395        if let Some(found_node) = found_node {
2396            hint_missing_borrow(self, param_env, span, found, expected, found_node, &mut err);
2397        }
2398
2399        err
2400    }
2401
2402    fn note_conflicting_fn_args(
2403        &self,
2404        err: &mut Diag<'_>,
2405        cause: &ObligationCauseCode<'tcx>,
2406        expected: Ty<'tcx>,
2407        found: Ty<'tcx>,
2408        param_env: ty::ParamEnv<'tcx>,
2409    ) {
2410        let ObligationCauseCode::FunctionArg { arg_hir_id, .. } = cause else {
2411            return;
2412        };
2413        let ty::FnPtr(sig_tys, hdr) = expected.kind() else {
2414            return;
2415        };
2416        let expected = sig_tys.with(*hdr);
2417        let ty::FnPtr(sig_tys, hdr) = found.kind() else {
2418            return;
2419        };
2420        let found = sig_tys.with(*hdr);
2421        let Node::Expr(arg) = self.tcx.hir_node(*arg_hir_id) else {
2422            return;
2423        };
2424        let hir::ExprKind::Path(path) = arg.kind else {
2425            return;
2426        };
2427        let expected_inputs = self.tcx.instantiate_bound_regions_with_erased(expected).inputs();
2428        let found_inputs = self.tcx.instantiate_bound_regions_with_erased(found).inputs();
2429        let both_tys = expected_inputs.iter().copied().zip(found_inputs.iter().copied());
2430
2431        let arg_expr = |infcx: &InferCtxt<'tcx>, name, expected: Ty<'tcx>, found: Ty<'tcx>| {
2432            let (expected_ty, expected_refs) = get_deref_type_and_refs(expected);
2433            let (found_ty, found_refs) = get_deref_type_and_refs(found);
2434
2435            if infcx.can_eq(param_env, found_ty, expected_ty) {
2436                if found_refs.len() == expected_refs.len()
2437                    && found_refs.iter().eq(expected_refs.iter())
2438                {
2439                    name
2440                } else if found_refs.len() > expected_refs.len() {
2441                    let refs = &found_refs[..found_refs.len() - expected_refs.len()];
2442                    if found_refs[..expected_refs.len()].iter().eq(expected_refs.iter()) {
2443                        ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{0}{1}",
                refs.iter().map(|mutbl|
                                ::alloc::__export::must_use({
                                        ::alloc::fmt::format(format_args!("&{0}",
                                                mutbl.prefix_str()))
                                    })).collect::<Vec<_>>().join(""), name))
    })format!(
2444                            "{}{name}",
2445                            refs.iter()
2446                                .map(|mutbl| format!("&{}", mutbl.prefix_str()))
2447                                .collect::<Vec<_>>()
2448                                .join(""),
2449                        )
2450                    } else {
2451                        // The refs have different mutability.
2452                        ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{0}*{1}",
                refs.iter().map(|mutbl|
                                ::alloc::__export::must_use({
                                        ::alloc::fmt::format(format_args!("&{0}",
                                                mutbl.prefix_str()))
                                    })).collect::<Vec<_>>().join(""), name))
    })format!(
2453                            "{}*{name}",
2454                            refs.iter()
2455                                .map(|mutbl| format!("&{}", mutbl.prefix_str()))
2456                                .collect::<Vec<_>>()
2457                                .join(""),
2458                        )
2459                    }
2460                } else if expected_refs.len() > found_refs.len() {
2461                    ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{0}{1}",
                (0..(expected_refs.len() -
                                            found_refs.len())).map(|_|
                                "*").collect::<Vec<_>>().join(""), name))
    })format!(
2462                        "{}{name}",
2463                        (0..(expected_refs.len() - found_refs.len()))
2464                            .map(|_| "*")
2465                            .collect::<Vec<_>>()
2466                            .join(""),
2467                    )
2468                } else {
2469                    ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{0}{1}",
                found_refs.iter().map(|mutbl|
                                    ::alloc::__export::must_use({
                                            ::alloc::fmt::format(format_args!("&{0}",
                                                    mutbl.prefix_str()))
                                        })).chain(found_refs.iter().map(|_|
                                    "*".to_string())).collect::<Vec<_>>().join(""), name))
    })format!(
2470                        "{}{name}",
2471                        found_refs
2472                            .iter()
2473                            .map(|mutbl| format!("&{}", mutbl.prefix_str()))
2474                            .chain(found_refs.iter().map(|_| "*".to_string()))
2475                            .collect::<Vec<_>>()
2476                            .join(""),
2477                    )
2478                }
2479            } else {
2480                ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("/* {0} */", found))
    })format!("/* {found} */")
2481            }
2482        };
2483        let args_have_same_underlying_type = both_tys.clone().all(|(expected, found)| {
2484            let (expected_ty, _) = get_deref_type_and_refs(expected);
2485            let (found_ty, _) = get_deref_type_and_refs(found);
2486            self.can_eq(param_env, found_ty, expected_ty)
2487        });
2488        let (closure_names, call_names): (Vec<_>, Vec<_>) = if args_have_same_underlying_type
2489            && !expected_inputs.is_empty()
2490            && expected_inputs.len() == found_inputs.len()
2491            && let Some(typeck) = &self.typeck_results
2492            && let Res::Def(res_kind, fn_def_id) = typeck.qpath_res(&path, *arg_hir_id)
2493            && res_kind.is_fn_like()
2494        {
2495            let closure: Vec<_> = self
2496                .tcx
2497                .fn_arg_idents(fn_def_id)
2498                .iter()
2499                .enumerate()
2500                .map(|(i, ident)| {
2501                    if let Some(ident) = ident
2502                        && !#[allow(non_exhaustive_omitted_patterns)] match ident {
    Ident { name: kw::Underscore | kw::SelfLower, .. } => true,
    _ => false,
}matches!(ident, Ident { name: kw::Underscore | kw::SelfLower, .. })
2503                    {
2504                        ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{0}", ident))
    })format!("{ident}")
2505                    } else {
2506                        ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("arg{0}", i))
    })format!("arg{i}")
2507                    }
2508                })
2509                .collect();
2510            let args = closure
2511                .iter()
2512                .zip(both_tys)
2513                .map(|(name, (expected, found))| {
2514                    arg_expr(self.infcx, name.to_owned(), expected, found)
2515                })
2516                .collect();
2517            (closure, args)
2518        } else {
2519            let closure_args = expected_inputs
2520                .iter()
2521                .enumerate()
2522                .map(|(i, _)| ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("arg{0}", i))
    })format!("arg{i}"))
2523                .collect::<Vec<_>>();
2524            let call_args = both_tys
2525                .enumerate()
2526                .map(|(i, (expected, found))| {
2527                    arg_expr(self.infcx, ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("arg{0}", i))
    })format!("arg{i}"), expected, found)
2528                })
2529                .collect::<Vec<_>>();
2530            (closure_args, call_args)
2531        };
2532        let closure_names: Vec<_> = closure_names
2533            .into_iter()
2534            .zip(expected_inputs.iter())
2535            .map(|(name, ty)| {
2536                ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{1}{0}",
                if ty.has_infer_types() {
                    String::new()
                } else if ty.references_error() {
                    ": /* type */".to_string()
                } else {
                    ::alloc::__export::must_use({
                            ::alloc::fmt::format(format_args!(": {0}", ty))
                        })
                }, name))
    })format!(
2537                    "{name}{}",
2538                    if ty.has_infer_types() {
2539                        String::new()
2540                    } else if ty.references_error() {
2541                        ": /* type */".to_string()
2542                    } else {
2543                        format!(": {ty}")
2544                    }
2545                )
2546            })
2547            .collect();
2548        err.multipart_suggestion(
2549            "consider wrapping the function in a closure",
2550            ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [(arg.span.shrink_to_lo(),
                    ::alloc::__export::must_use({
                            ::alloc::fmt::format(format_args!("|{0}| ",
                                    closure_names.join(", ")))
                        })),
                (arg.span.shrink_to_hi(),
                    ::alloc::__export::must_use({
                            ::alloc::fmt::format(format_args!("({0})",
                                    call_names.join(", ")))
                        }))]))vec![
2551                (arg.span.shrink_to_lo(), format!("|{}| ", closure_names.join(", "))),
2552                (arg.span.shrink_to_hi(), format!("({})", call_names.join(", "))),
2553            ],
2554            Applicability::MaybeIncorrect,
2555        );
2556    }
2557
2558    // Add a note if there are two `Fn`-family bounds that have conflicting argument
2559    // requirements, which will always cause a closure to have a type error.
2560    fn note_conflicting_closure_bounds(
2561        &self,
2562        cause: &ObligationCauseCode<'tcx>,
2563        err: &mut Diag<'_>,
2564    ) {
2565        // First, look for an `WhereClauseInExpr`, which means we can get
2566        // the uninstantiated predicate list of the called function. And check
2567        // that the predicate that we failed to satisfy is a `Fn`-like trait.
2568        if let ObligationCauseCode::WhereClauseInExpr(def_id, _, _, idx) = *cause
2569            && let predicates = self.tcx.predicates_of(def_id).instantiate_identity(self.tcx)
2570            && let Some(pred) = predicates.predicates.get(idx)
2571            && let ty::ClauseKind::Trait(trait_pred) = pred.kind().skip_binder()
2572            && self.tcx.is_fn_trait(trait_pred.def_id())
2573        {
2574            let expected_self =
2575                self.tcx.anonymize_bound_vars(pred.kind().rebind(trait_pred.self_ty()));
2576            let expected_args =
2577                self.tcx.anonymize_bound_vars(pred.kind().rebind(trait_pred.trait_ref.args));
2578
2579            // Find another predicate whose self-type is equal to the expected self type,
2580            // but whose args don't match.
2581            let other_pred = predicates.into_iter().enumerate().find(|&(other_idx, (pred, _))| {
2582                match pred.kind().skip_binder() {
2583                    ty::ClauseKind::Trait(trait_pred)
2584                        if self.tcx.is_fn_trait(trait_pred.def_id())
2585                            && other_idx != idx
2586                            // Make sure that the self type matches
2587                            // (i.e. constraining this closure)
2588                            && expected_self
2589                                == self.tcx.anonymize_bound_vars(
2590                                    pred.kind().rebind(trait_pred.self_ty()),
2591                                )
2592                            // But the args don't match (i.e. incompatible args)
2593                            && expected_args
2594                                != self.tcx.anonymize_bound_vars(
2595                                    pred.kind().rebind(trait_pred.trait_ref.args),
2596                                ) =>
2597                    {
2598                        true
2599                    }
2600                    _ => false,
2601                }
2602            });
2603            // If we found one, then it's very likely the cause of the error.
2604            if let Some((_, (_, other_pred_span))) = other_pred {
2605                err.span_note(
2606                    other_pred_span,
2607                    "closure inferred to have a different signature due to this bound",
2608                );
2609            }
2610        }
2611    }
2612
2613    pub(super) fn suggest_fully_qualified_path(
2614        &self,
2615        err: &mut Diag<'_>,
2616        item_def_id: DefId,
2617        span: Span,
2618        trait_ref: DefId,
2619    ) {
2620        if let Some(assoc_item) = self.tcx.opt_associated_item(item_def_id)
2621            && let ty::AssocKind::Const { .. } | ty::AssocKind::Type { .. } = assoc_item.kind
2622        {
2623            err.note(::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{0}s cannot be accessed directly on a `trait`, they can only be accessed through a specific `impl`",
                self.tcx.def_kind_descr(assoc_item.as_def_kind(),
                    item_def_id)))
    })format!(
2624                "{}s cannot be accessed directly on a `trait`, they can only be \
2625                        accessed through a specific `impl`",
2626                self.tcx.def_kind_descr(assoc_item.as_def_kind(), item_def_id)
2627            ));
2628
2629            if !assoc_item.is_impl_trait_in_trait() {
2630                err.span_suggestion_verbose(
2631                    span,
2632                    "use the fully qualified path to an implementation",
2633                    ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("<Type as {0}>::{1}",
                self.tcx.def_path_str(trait_ref), assoc_item.name()))
    })format!(
2634                        "<Type as {}>::{}",
2635                        self.tcx.def_path_str(trait_ref),
2636                        assoc_item.name()
2637                    ),
2638                    Applicability::HasPlaceholders,
2639                );
2640            }
2641        }
2642    }
2643
2644    /// Adds an async-await specific note to the diagnostic when the future does not implement
2645    /// an auto trait because of a captured type.
2646    ///
2647    /// ```text
2648    /// note: future does not implement `Qux` as this value is used across an await
2649    ///   --> $DIR/issue-64130-3-other.rs:17:5
2650    ///    |
2651    /// LL |     let x = Foo;
2652    ///    |         - has type `Foo`
2653    /// LL |     baz().await;
2654    ///    |     ^^^^^^^^^^^ await occurs here, with `x` maybe used later
2655    /// LL | }
2656    ///    | - `x` is later dropped here
2657    /// ```
2658    ///
2659    /// When the diagnostic does not implement `Send` or `Sync` specifically, then the diagnostic
2660    /// is "replaced" with a different message and a more specific error.
2661    ///
2662    /// ```text
2663    /// error: future cannot be sent between threads safely
2664    ///   --> $DIR/issue-64130-2-send.rs:21:5
2665    ///    |
2666    /// LL | fn is_send<T: Send>(t: T) { }
2667    ///    |               ---- required by this bound in `is_send`
2668    /// ...
2669    /// LL |     is_send(bar());
2670    ///    |     ^^^^^^^ future returned by `bar` is not send
2671    ///    |
2672    ///    = help: within `impl std::future::Future`, the trait `std::marker::Send` is not
2673    ///            implemented for `Foo`
2674    /// note: future is not send as this value is used across an await
2675    ///   --> $DIR/issue-64130-2-send.rs:15:5
2676    ///    |
2677    /// LL |     let x = Foo;
2678    ///    |         - has type `Foo`
2679    /// LL |     baz().await;
2680    ///    |     ^^^^^^^^^^^ await occurs here, with `x` maybe used later
2681    /// LL | }
2682    ///    | - `x` is later dropped here
2683    /// ```
2684    ///
2685    /// Returns `true` if an async-await specific note was added to the diagnostic.
2686    #[allow(clippy :: suspicious_else_formatting)]
{
    let __tracing_attr_span;
    let __tracing_attr_guard;
    if ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() ||
            { false } {
        __tracing_attr_span =
            {
                use ::tracing::__macro_support::Callsite as _;
                static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                    {
                        static META: ::tracing::Metadata<'static> =
                            {
                                ::tracing_core::metadata::Metadata::new("maybe_note_obligation_cause_for_async_await",
                                    "rustc_trait_selection::error_reporting::traits::suggestions",
                                    ::tracing::Level::DEBUG,
                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs"),
                                    ::tracing_core::__macro_support::Option::Some(2686u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_trait_selection::error_reporting::traits::suggestions"),
                                    ::tracing_core::field::FieldSet::new(&["obligation.predicate",
                                                    "obligation.cause.span"],
                                        ::tracing_core::callsite::Identifier(&__CALLSITE)),
                                    ::tracing::metadata::Kind::SPAN)
                            };
                        ::tracing::callsite::DefaultCallsite::new(&META)
                    };
                let mut interest = ::tracing::subscriber::Interest::never();
                if ::tracing::Level::DEBUG <=
                                    ::tracing::level_filters::STATIC_MAX_LEVEL &&
                                ::tracing::Level::DEBUG <=
                                    ::tracing::level_filters::LevelFilter::current() &&
                            { interest = __CALLSITE.interest(); !interest.is_never() }
                        &&
                        ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                            interest) {
                    let meta = __CALLSITE.metadata();
                    ::tracing::Span::new(meta,
                        &{
                                #[allow(unused_imports)]
                                use ::tracing::field::{debug, display, Value};
                                let mut iter = meta.fields().iter();
                                meta.fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                    ::tracing::__macro_support::Option::Some(&debug(&obligation.predicate)
                                                            as &dyn Value)),
                                                (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                    ::tracing::__macro_support::Option::Some(&debug(&obligation.cause.span)
                                                            as &dyn Value))])
                            })
                } else {
                    let span =
                        ::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
                    {};
                    span
                }
            };
        __tracing_attr_guard = __tracing_attr_span.enter();
    }

    #[warn(clippy :: suspicious_else_formatting)]
    {

        #[allow(unknown_lints, unreachable_code, clippy ::
        diverging_sub_expression, clippy :: empty_loop, clippy ::
        let_unit_value, clippy :: let_with_type_underscore, clippy ::
        needless_return, clippy :: unreachable)]
        if false {
            let __tracing_attr_fake_return: bool = loop {};
            return __tracing_attr_fake_return;
        }
        {
            let (mut trait_ref, mut target_ty) =
                match obligation.predicate.kind().skip_binder() {
                    ty::PredicateKind::Clause(ty::ClauseKind::Trait(p)) =>
                        (Some(p), Some(p.self_ty())),
                    _ => (None, None),
                };
            let mut coroutine = None;
            let mut outer_coroutine = None;
            let mut next_code = Some(obligation.cause.code());
            let mut seen_upvar_tys_infer_tuple = false;
            while let Some(code) = next_code {
                {
                    use ::tracing::__macro_support::Callsite as _;
                    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                        {
                            static META: ::tracing::Metadata<'static> =
                                {
                                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs:2725",
                                        "rustc_trait_selection::error_reporting::traits::suggestions",
                                        ::tracing::Level::DEBUG,
                                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs"),
                                        ::tracing_core::__macro_support::Option::Some(2725u32),
                                        ::tracing_core::__macro_support::Option::Some("rustc_trait_selection::error_reporting::traits::suggestions"),
                                        ::tracing_core::field::FieldSet::new(&["code"],
                                            ::tracing_core::callsite::Identifier(&__CALLSITE)),
                                        ::tracing::metadata::Kind::EVENT)
                                };
                            ::tracing::callsite::DefaultCallsite::new(&META)
                        };
                    let enabled =
                        ::tracing::Level::DEBUG <=
                                    ::tracing::level_filters::STATIC_MAX_LEVEL &&
                                ::tracing::Level::DEBUG <=
                                    ::tracing::level_filters::LevelFilter::current() &&
                            {
                                let interest = __CALLSITE.interest();
                                !interest.is_never() &&
                                    ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                                        interest)
                            };
                    if enabled {
                        (|value_set: ::tracing::field::ValueSet|
                                    {
                                        let meta = __CALLSITE.metadata();
                                        ::tracing::Event::dispatch(meta, &value_set);
                                        ;
                                    })({
                                #[allow(unused_imports)]
                                use ::tracing::field::{debug, display, Value};
                                let mut iter = __CALLSITE.metadata().fields().iter();
                                __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                    ::tracing::__macro_support::Option::Some(&debug(&code) as
                                                            &dyn Value))])
                            });
                    } else { ; }
                };
                match code {
                    ObligationCauseCode::FunctionArg { parent_code, .. } => {
                        next_code = Some(parent_code);
                    }
                    ObligationCauseCode::ImplDerived(cause) => {
                        let ty =
                            cause.derived.parent_trait_pred.skip_binder().self_ty();
                        {
                            use ::tracing::__macro_support::Callsite as _;
                            static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                                {
                                    static META: ::tracing::Metadata<'static> =
                                        {
                                            ::tracing_core::metadata::Metadata::new("event compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs:2732",
                                                "rustc_trait_selection::error_reporting::traits::suggestions",
                                                ::tracing::Level::DEBUG,
                                                ::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs"),
                                                ::tracing_core::__macro_support::Option::Some(2732u32),
                                                ::tracing_core::__macro_support::Option::Some("rustc_trait_selection::error_reporting::traits::suggestions"),
                                                ::tracing_core::field::FieldSet::new(&["message",
                                                                "parent_trait_ref", "self_ty.kind"],
                                                    ::tracing_core::callsite::Identifier(&__CALLSITE)),
                                                ::tracing::metadata::Kind::EVENT)
                                        };
                                    ::tracing::callsite::DefaultCallsite::new(&META)
                                };
                            let enabled =
                                ::tracing::Level::DEBUG <=
                                            ::tracing::level_filters::STATIC_MAX_LEVEL &&
                                        ::tracing::Level::DEBUG <=
                                            ::tracing::level_filters::LevelFilter::current() &&
                                    {
                                        let interest = __CALLSITE.interest();
                                        !interest.is_never() &&
                                            ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                                                interest)
                                    };
                            if enabled {
                                (|value_set: ::tracing::field::ValueSet|
                                            {
                                                let meta = __CALLSITE.metadata();
                                                ::tracing::Event::dispatch(meta, &value_set);
                                                ;
                                            })({
                                        #[allow(unused_imports)]
                                        use ::tracing::field::{debug, display, Value};
                                        let mut iter = __CALLSITE.metadata().fields().iter();
                                        __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                            ::tracing::__macro_support::Option::Some(&format_args!("ImplDerived")
                                                                    as &dyn Value)),
                                                        (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                            ::tracing::__macro_support::Option::Some(&debug(&cause.derived.parent_trait_pred)
                                                                    as &dyn Value)),
                                                        (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                            ::tracing::__macro_support::Option::Some(&debug(&ty.kind())
                                                                    as &dyn Value))])
                                    });
                            } else { ; }
                        };
                        match *ty.kind() {
                            ty::Coroutine(did, ..) | ty::CoroutineWitness(did, _) => {
                                coroutine = coroutine.or(Some(did));
                                outer_coroutine = Some(did);
                            }
                            ty::Tuple(_) if !seen_upvar_tys_infer_tuple => {
                                seen_upvar_tys_infer_tuple = true;
                            }
                            _ if coroutine.is_none() => {
                                trait_ref =
                                    Some(cause.derived.parent_trait_pred.skip_binder());
                                target_ty = Some(ty);
                            }
                            _ => {}
                        }
                        next_code = Some(&cause.derived.parent_code);
                    }
                    ObligationCauseCode::WellFormedDerived(derived_obligation) |
                        ObligationCauseCode::BuiltinDerived(derived_obligation) => {
                        let ty =
                            derived_obligation.parent_trait_pred.skip_binder().self_ty();
                        {
                            use ::tracing::__macro_support::Callsite as _;
                            static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                                {
                                    static META: ::tracing::Metadata<'static> =
                                        {
                                            ::tracing_core::metadata::Metadata::new("event compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs:2762",
                                                "rustc_trait_selection::error_reporting::traits::suggestions",
                                                ::tracing::Level::DEBUG,
                                                ::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs"),
                                                ::tracing_core::__macro_support::Option::Some(2762u32),
                                                ::tracing_core::__macro_support::Option::Some("rustc_trait_selection::error_reporting::traits::suggestions"),
                                                ::tracing_core::field::FieldSet::new(&["parent_trait_ref",
                                                                "self_ty.kind"],
                                                    ::tracing_core::callsite::Identifier(&__CALLSITE)),
                                                ::tracing::metadata::Kind::EVENT)
                                        };
                                    ::tracing::callsite::DefaultCallsite::new(&META)
                                };
                            let enabled =
                                ::tracing::Level::DEBUG <=
                                            ::tracing::level_filters::STATIC_MAX_LEVEL &&
                                        ::tracing::Level::DEBUG <=
                                            ::tracing::level_filters::LevelFilter::current() &&
                                    {
                                        let interest = __CALLSITE.interest();
                                        !interest.is_never() &&
                                            ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                                                interest)
                                    };
                            if enabled {
                                (|value_set: ::tracing::field::ValueSet|
                                            {
                                                let meta = __CALLSITE.metadata();
                                                ::tracing::Event::dispatch(meta, &value_set);
                                                ;
                                            })({
                                        #[allow(unused_imports)]
                                        use ::tracing::field::{debug, display, Value};
                                        let mut iter = __CALLSITE.metadata().fields().iter();
                                        __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                            ::tracing::__macro_support::Option::Some(&debug(&derived_obligation.parent_trait_pred)
                                                                    as &dyn Value)),
                                                        (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                            ::tracing::__macro_support::Option::Some(&debug(&ty.kind())
                                                                    as &dyn Value))])
                                    });
                            } else { ; }
                        };
                        match *ty.kind() {
                            ty::Coroutine(did, ..) | ty::CoroutineWitness(did, ..) => {
                                coroutine = coroutine.or(Some(did));
                                outer_coroutine = Some(did);
                            }
                            ty::Tuple(_) if !seen_upvar_tys_infer_tuple => {
                                seen_upvar_tys_infer_tuple = true;
                            }
                            _ if coroutine.is_none() => {
                                trait_ref =
                                    Some(derived_obligation.parent_trait_pred.skip_binder());
                                target_ty = Some(ty);
                            }
                            _ => {}
                        }
                        next_code = Some(&derived_obligation.parent_code);
                    }
                    _ => break,
                }
            }
            {
                use ::tracing::__macro_support::Callsite as _;
                static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                    {
                        static META: ::tracing::Metadata<'static> =
                            {
                                ::tracing_core::metadata::Metadata::new("event compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs:2793",
                                    "rustc_trait_selection::error_reporting::traits::suggestions",
                                    ::tracing::Level::DEBUG,
                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs"),
                                    ::tracing_core::__macro_support::Option::Some(2793u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_trait_selection::error_reporting::traits::suggestions"),
                                    ::tracing_core::field::FieldSet::new(&["coroutine",
                                                    "trait_ref", "target_ty"],
                                        ::tracing_core::callsite::Identifier(&__CALLSITE)),
                                    ::tracing::metadata::Kind::EVENT)
                            };
                        ::tracing::callsite::DefaultCallsite::new(&META)
                    };
                let enabled =
                    ::tracing::Level::DEBUG <=
                                ::tracing::level_filters::STATIC_MAX_LEVEL &&
                            ::tracing::Level::DEBUG <=
                                ::tracing::level_filters::LevelFilter::current() &&
                        {
                            let interest = __CALLSITE.interest();
                            !interest.is_never() &&
                                ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                                    interest)
                        };
                if enabled {
                    (|value_set: ::tracing::field::ValueSet|
                                {
                                    let meta = __CALLSITE.metadata();
                                    ::tracing::Event::dispatch(meta, &value_set);
                                    ;
                                })({
                            #[allow(unused_imports)]
                            use ::tracing::field::{debug, display, Value};
                            let mut iter = __CALLSITE.metadata().fields().iter();
                            __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                ::tracing::__macro_support::Option::Some(&debug(&coroutine)
                                                        as &dyn Value)),
                                            (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                ::tracing::__macro_support::Option::Some(&debug(&trait_ref)
                                                        as &dyn Value)),
                                            (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                ::tracing::__macro_support::Option::Some(&debug(&target_ty)
                                                        as &dyn Value))])
                        });
                } else { ; }
            };
            let (Some(coroutine_did), Some(trait_ref), Some(target_ty)) =
                (coroutine, trait_ref, target_ty) else { return false; };
            let span = self.tcx.def_span(coroutine_did);
            let coroutine_did_root =
                self.tcx.typeck_root_def_id(coroutine_did);
            {
                use ::tracing::__macro_support::Callsite as _;
                static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                    {
                        static META: ::tracing::Metadata<'static> =
                            {
                                ::tracing_core::metadata::Metadata::new("event compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs:2803",
                                    "rustc_trait_selection::error_reporting::traits::suggestions",
                                    ::tracing::Level::DEBUG,
                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs"),
                                    ::tracing_core::__macro_support::Option::Some(2803u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_trait_selection::error_reporting::traits::suggestions"),
                                    ::tracing_core::field::FieldSet::new(&["coroutine_did",
                                                    "coroutine_did_root", "typeck_results.hir_owner", "span"],
                                        ::tracing_core::callsite::Identifier(&__CALLSITE)),
                                    ::tracing::metadata::Kind::EVENT)
                            };
                        ::tracing::callsite::DefaultCallsite::new(&META)
                    };
                let enabled =
                    ::tracing::Level::DEBUG <=
                                ::tracing::level_filters::STATIC_MAX_LEVEL &&
                            ::tracing::Level::DEBUG <=
                                ::tracing::level_filters::LevelFilter::current() &&
                        {
                            let interest = __CALLSITE.interest();
                            !interest.is_never() &&
                                ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                                    interest)
                        };
                if enabled {
                    (|value_set: ::tracing::field::ValueSet|
                                {
                                    let meta = __CALLSITE.metadata();
                                    ::tracing::Event::dispatch(meta, &value_set);
                                    ;
                                })({
                            #[allow(unused_imports)]
                            use ::tracing::field::{debug, display, Value};
                            let mut iter = __CALLSITE.metadata().fields().iter();
                            __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                ::tracing::__macro_support::Option::Some(&debug(&coroutine_did)
                                                        as &dyn Value)),
                                            (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                ::tracing::__macro_support::Option::Some(&debug(&coroutine_did_root)
                                                        as &dyn Value)),
                                            (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                ::tracing::__macro_support::Option::Some(&debug(&self.typeck_results.as_ref().map(|t|
                                                                            t.hir_owner)) as &dyn Value)),
                                            (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                ::tracing::__macro_support::Option::Some(&debug(&span) as
                                                        &dyn Value))])
                        });
                } else { ; }
            };
            let coroutine_body =
                coroutine_did.as_local().and_then(|def_id|
                        self.tcx.hir_maybe_body_owned_by(def_id));
            let mut visitor = AwaitsVisitor::default();
            if let Some(body) = coroutine_body { visitor.visit_body(&body); }
            {
                use ::tracing::__macro_support::Callsite as _;
                static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                    {
                        static META: ::tracing::Metadata<'static> =
                            {
                                ::tracing_core::metadata::Metadata::new("event compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs:2816",
                                    "rustc_trait_selection::error_reporting::traits::suggestions",
                                    ::tracing::Level::DEBUG,
                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs"),
                                    ::tracing_core::__macro_support::Option::Some(2816u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_trait_selection::error_reporting::traits::suggestions"),
                                    ::tracing_core::field::FieldSet::new(&["awaits"],
                                        ::tracing_core::callsite::Identifier(&__CALLSITE)),
                                    ::tracing::metadata::Kind::EVENT)
                            };
                        ::tracing::callsite::DefaultCallsite::new(&META)
                    };
                let enabled =
                    ::tracing::Level::DEBUG <=
                                ::tracing::level_filters::STATIC_MAX_LEVEL &&
                            ::tracing::Level::DEBUG <=
                                ::tracing::level_filters::LevelFilter::current() &&
                        {
                            let interest = __CALLSITE.interest();
                            !interest.is_never() &&
                                ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                                    interest)
                        };
                if enabled {
                    (|value_set: ::tracing::field::ValueSet|
                                {
                                    let meta = __CALLSITE.metadata();
                                    ::tracing::Event::dispatch(meta, &value_set);
                                    ;
                                })({
                            #[allow(unused_imports)]
                            use ::tracing::field::{debug, display, Value};
                            let mut iter = __CALLSITE.metadata().fields().iter();
                            __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                ::tracing::__macro_support::Option::Some(&debug(&visitor.awaits)
                                                        as &dyn Value))])
                        });
                } else { ; }
            };
            let target_ty_erased =
                self.tcx.erase_and_anonymize_regions(target_ty);
            let ty_matches =
                |ty| -> bool
                    {
                        let ty_erased =
                            self.tcx.instantiate_bound_regions_with_erased(ty);
                        let ty_erased =
                            self.tcx.erase_and_anonymize_regions(ty_erased);
                        let eq = ty_erased == target_ty_erased;
                        {
                            use ::tracing::__macro_support::Callsite as _;
                            static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                                {
                                    static META: ::tracing::Metadata<'static> =
                                        {
                                            ::tracing_core::metadata::Metadata::new("event compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs:2837",
                                                "rustc_trait_selection::error_reporting::traits::suggestions",
                                                ::tracing::Level::DEBUG,
                                                ::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs"),
                                                ::tracing_core::__macro_support::Option::Some(2837u32),
                                                ::tracing_core::__macro_support::Option::Some("rustc_trait_selection::error_reporting::traits::suggestions"),
                                                ::tracing_core::field::FieldSet::new(&["ty_erased",
                                                                "target_ty_erased", "eq"],
                                                    ::tracing_core::callsite::Identifier(&__CALLSITE)),
                                                ::tracing::metadata::Kind::EVENT)
                                        };
                                    ::tracing::callsite::DefaultCallsite::new(&META)
                                };
                            let enabled =
                                ::tracing::Level::DEBUG <=
                                            ::tracing::level_filters::STATIC_MAX_LEVEL &&
                                        ::tracing::Level::DEBUG <=
                                            ::tracing::level_filters::LevelFilter::current() &&
                                    {
                                        let interest = __CALLSITE.interest();
                                        !interest.is_never() &&
                                            ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                                                interest)
                                    };
                            if enabled {
                                (|value_set: ::tracing::field::ValueSet|
                                            {
                                                let meta = __CALLSITE.metadata();
                                                ::tracing::Event::dispatch(meta, &value_set);
                                                ;
                                            })({
                                        #[allow(unused_imports)]
                                        use ::tracing::field::{debug, display, Value};
                                        let mut iter = __CALLSITE.metadata().fields().iter();
                                        __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                            ::tracing::__macro_support::Option::Some(&debug(&ty_erased)
                                                                    as &dyn Value)),
                                                        (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                            ::tracing::__macro_support::Option::Some(&debug(&target_ty_erased)
                                                                    as &dyn Value)),
                                                        (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                            ::tracing::__macro_support::Option::Some(&debug(&eq) as
                                                                    &dyn Value))])
                                    });
                            } else { ; }
                        };
                        eq
                    };
            let coroutine_data =
                match &self.typeck_results {
                    Some(t) if t.hir_owner.to_def_id() == coroutine_did_root =>
                        CoroutineData(t),
                    _ if coroutine_did.is_local() => {
                        CoroutineData(self.tcx.typeck(coroutine_did.expect_local()))
                    }
                    _ => return false,
                };
            let coroutine_within_in_progress_typeck =
                match &self.typeck_results {
                    Some(t) => t.hir_owner.to_def_id() == coroutine_did_root,
                    _ => false,
                };
            let mut interior_or_upvar_span = None;
            let from_awaited_ty =
                coroutine_data.get_from_await_ty(visitor, self.tcx,
                    ty_matches);
            {
                use ::tracing::__macro_support::Callsite as _;
                static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                    {
                        static META: ::tracing::Metadata<'static> =
                            {
                                ::tracing_core::metadata::Metadata::new("event compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs:2861",
                                    "rustc_trait_selection::error_reporting::traits::suggestions",
                                    ::tracing::Level::DEBUG,
                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs"),
                                    ::tracing_core::__macro_support::Option::Some(2861u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_trait_selection::error_reporting::traits::suggestions"),
                                    ::tracing_core::field::FieldSet::new(&["from_awaited_ty"],
                                        ::tracing_core::callsite::Identifier(&__CALLSITE)),
                                    ::tracing::metadata::Kind::EVENT)
                            };
                        ::tracing::callsite::DefaultCallsite::new(&META)
                    };
                let enabled =
                    ::tracing::Level::DEBUG <=
                                ::tracing::level_filters::STATIC_MAX_LEVEL &&
                            ::tracing::Level::DEBUG <=
                                ::tracing::level_filters::LevelFilter::current() &&
                        {
                            let interest = __CALLSITE.interest();
                            !interest.is_never() &&
                                ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                                    interest)
                        };
                if enabled {
                    (|value_set: ::tracing::field::ValueSet|
                                {
                                    let meta = __CALLSITE.metadata();
                                    ::tracing::Event::dispatch(meta, &value_set);
                                    ;
                                })({
                            #[allow(unused_imports)]
                            use ::tracing::field::{debug, display, Value};
                            let mut iter = __CALLSITE.metadata().fields().iter();
                            __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                ::tracing::__macro_support::Option::Some(&debug(&from_awaited_ty)
                                                        as &dyn Value))])
                        });
                } else { ; }
            };
            if coroutine_did.is_local() &&
                        !coroutine_within_in_progress_typeck &&
                    let Some(coroutine_info) =
                        self.tcx.mir_coroutine_witnesses(coroutine_did) {
                {
                    use ::tracing::__macro_support::Callsite as _;
                    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                        {
                            static META: ::tracing::Metadata<'static> =
                                {
                                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs:2869",
                                        "rustc_trait_selection::error_reporting::traits::suggestions",
                                        ::tracing::Level::DEBUG,
                                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs"),
                                        ::tracing_core::__macro_support::Option::Some(2869u32),
                                        ::tracing_core::__macro_support::Option::Some("rustc_trait_selection::error_reporting::traits::suggestions"),
                                        ::tracing_core::field::FieldSet::new(&["coroutine_info"],
                                            ::tracing_core::callsite::Identifier(&__CALLSITE)),
                                        ::tracing::metadata::Kind::EVENT)
                                };
                            ::tracing::callsite::DefaultCallsite::new(&META)
                        };
                    let enabled =
                        ::tracing::Level::DEBUG <=
                                    ::tracing::level_filters::STATIC_MAX_LEVEL &&
                                ::tracing::Level::DEBUG <=
                                    ::tracing::level_filters::LevelFilter::current() &&
                            {
                                let interest = __CALLSITE.interest();
                                !interest.is_never() &&
                                    ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                                        interest)
                            };
                    if enabled {
                        (|value_set: ::tracing::field::ValueSet|
                                    {
                                        let meta = __CALLSITE.metadata();
                                        ::tracing::Event::dispatch(meta, &value_set);
                                        ;
                                    })({
                                #[allow(unused_imports)]
                                use ::tracing::field::{debug, display, Value};
                                let mut iter = __CALLSITE.metadata().fields().iter();
                                __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                    ::tracing::__macro_support::Option::Some(&debug(&coroutine_info)
                                                            as &dyn Value))])
                            });
                    } else { ; }
                };
                'find_source:
                    for (variant, source_info) in
                    coroutine_info.variant_fields.iter().zip(&coroutine_info.variant_source_info)
                    {
                    {
                        use ::tracing::__macro_support::Callsite as _;
                        static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                            {
                                static META: ::tracing::Metadata<'static> =
                                    {
                                        ::tracing_core::metadata::Metadata::new("event compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs:2873",
                                            "rustc_trait_selection::error_reporting::traits::suggestions",
                                            ::tracing::Level::DEBUG,
                                            ::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs"),
                                            ::tracing_core::__macro_support::Option::Some(2873u32),
                                            ::tracing_core::__macro_support::Option::Some("rustc_trait_selection::error_reporting::traits::suggestions"),
                                            ::tracing_core::field::FieldSet::new(&["variant"],
                                                ::tracing_core::callsite::Identifier(&__CALLSITE)),
                                            ::tracing::metadata::Kind::EVENT)
                                    };
                                ::tracing::callsite::DefaultCallsite::new(&META)
                            };
                        let enabled =
                            ::tracing::Level::DEBUG <=
                                        ::tracing::level_filters::STATIC_MAX_LEVEL &&
                                    ::tracing::Level::DEBUG <=
                                        ::tracing::level_filters::LevelFilter::current() &&
                                {
                                    let interest = __CALLSITE.interest();
                                    !interest.is_never() &&
                                        ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                                            interest)
                                };
                        if enabled {
                            (|value_set: ::tracing::field::ValueSet|
                                        {
                                            let meta = __CALLSITE.metadata();
                                            ::tracing::Event::dispatch(meta, &value_set);
                                            ;
                                        })({
                                    #[allow(unused_imports)]
                                    use ::tracing::field::{debug, display, Value};
                                    let mut iter = __CALLSITE.metadata().fields().iter();
                                    __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                        ::tracing::__macro_support::Option::Some(&debug(&variant) as
                                                                &dyn Value))])
                                });
                        } else { ; }
                    };
                    for &local in variant {
                        let decl = &coroutine_info.field_tys[local];
                        {
                            use ::tracing::__macro_support::Callsite as _;
                            static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                                {
                                    static META: ::tracing::Metadata<'static> =
                                        {
                                            ::tracing_core::metadata::Metadata::new("event compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs:2876",
                                                "rustc_trait_selection::error_reporting::traits::suggestions",
                                                ::tracing::Level::DEBUG,
                                                ::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs"),
                                                ::tracing_core::__macro_support::Option::Some(2876u32),
                                                ::tracing_core::__macro_support::Option::Some("rustc_trait_selection::error_reporting::traits::suggestions"),
                                                ::tracing_core::field::FieldSet::new(&["decl"],
                                                    ::tracing_core::callsite::Identifier(&__CALLSITE)),
                                                ::tracing::metadata::Kind::EVENT)
                                        };
                                    ::tracing::callsite::DefaultCallsite::new(&META)
                                };
                            let enabled =
                                ::tracing::Level::DEBUG <=
                                            ::tracing::level_filters::STATIC_MAX_LEVEL &&
                                        ::tracing::Level::DEBUG <=
                                            ::tracing::level_filters::LevelFilter::current() &&
                                    {
                                        let interest = __CALLSITE.interest();
                                        !interest.is_never() &&
                                            ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                                                interest)
                                    };
                            if enabled {
                                (|value_set: ::tracing::field::ValueSet|
                                            {
                                                let meta = __CALLSITE.metadata();
                                                ::tracing::Event::dispatch(meta, &value_set);
                                                ;
                                            })({
                                        #[allow(unused_imports)]
                                        use ::tracing::field::{debug, display, Value};
                                        let mut iter = __CALLSITE.metadata().fields().iter();
                                        __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                            ::tracing::__macro_support::Option::Some(&debug(&decl) as
                                                                    &dyn Value))])
                                    });
                            } else { ; }
                        };
                        if ty_matches(ty::Binder::dummy(decl.ty)) &&
                                !decl.ignore_for_traits {
                            interior_or_upvar_span =
                                Some(CoroutineInteriorOrUpvar::Interior(decl.source_info.span,
                                        Some((source_info.span, from_awaited_ty))));
                            break 'find_source;
                        }
                    }
                }
            }
            if interior_or_upvar_span.is_none() {
                interior_or_upvar_span =
                    coroutine_data.try_get_upvar_span(self, coroutine_did,
                        ty_matches);
            }
            if interior_or_upvar_span.is_none() && !coroutine_did.is_local() {
                interior_or_upvar_span =
                    Some(CoroutineInteriorOrUpvar::Interior(span, None));
            }
            {
                use ::tracing::__macro_support::Callsite as _;
                static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                    {
                        static META: ::tracing::Metadata<'static> =
                            {
                                ::tracing_core::metadata::Metadata::new("event compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs:2897",
                                    "rustc_trait_selection::error_reporting::traits::suggestions",
                                    ::tracing::Level::DEBUG,
                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs"),
                                    ::tracing_core::__macro_support::Option::Some(2897u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_trait_selection::error_reporting::traits::suggestions"),
                                    ::tracing_core::field::FieldSet::new(&["interior_or_upvar_span"],
                                        ::tracing_core::callsite::Identifier(&__CALLSITE)),
                                    ::tracing::metadata::Kind::EVENT)
                            };
                        ::tracing::callsite::DefaultCallsite::new(&META)
                    };
                let enabled =
                    ::tracing::Level::DEBUG <=
                                ::tracing::level_filters::STATIC_MAX_LEVEL &&
                            ::tracing::Level::DEBUG <=
                                ::tracing::level_filters::LevelFilter::current() &&
                        {
                            let interest = __CALLSITE.interest();
                            !interest.is_never() &&
                                ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                                    interest)
                        };
                if enabled {
                    (|value_set: ::tracing::field::ValueSet|
                                {
                                    let meta = __CALLSITE.metadata();
                                    ::tracing::Event::dispatch(meta, &value_set);
                                    ;
                                })({
                            #[allow(unused_imports)]
                            use ::tracing::field::{debug, display, Value};
                            let mut iter = __CALLSITE.metadata().fields().iter();
                            __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                ::tracing::__macro_support::Option::Some(&debug(&interior_or_upvar_span)
                                                        as &dyn Value))])
                        });
                } else { ; }
            };
            if let Some(interior_or_upvar_span) = interior_or_upvar_span {
                let is_async = self.tcx.coroutine_is_async(coroutine_did);
                self.note_obligation_cause_for_async_await(err,
                    interior_or_upvar_span, is_async, outer_coroutine,
                    trait_ref, target_ty, obligation, next_code);
                true
            } else { false }
        }
    }
}#[instrument(level = "debug", skip_all, fields(?obligation.predicate, ?obligation.cause.span))]
2687    pub fn maybe_note_obligation_cause_for_async_await<G: EmissionGuarantee>(
2688        &self,
2689        err: &mut Diag<'_, G>,
2690        obligation: &PredicateObligation<'tcx>,
2691    ) -> bool {
2692        // Attempt to detect an async-await error by looking at the obligation causes, looking
2693        // for a coroutine to be present.
2694        //
2695        // When a future does not implement a trait because of a captured type in one of the
2696        // coroutines somewhere in the call stack, then the result is a chain of obligations.
2697        //
2698        // Given an `async fn` A that calls an `async fn` B which captures a non-send type and that
2699        // future is passed as an argument to a function C which requires a `Send` type, then the
2700        // chain looks something like this:
2701        //
2702        // - `BuiltinDerivedObligation` with a coroutine witness (B)
2703        // - `BuiltinDerivedObligation` with a coroutine (B)
2704        // - `BuiltinDerivedObligation` with `impl std::future::Future` (B)
2705        // - `BuiltinDerivedObligation` with a coroutine witness (A)
2706        // - `BuiltinDerivedObligation` with a coroutine (A)
2707        // - `BuiltinDerivedObligation` with `impl std::future::Future` (A)
2708        // - `BindingObligation` with `impl_send` (Send requirement)
2709        //
2710        // The first obligation in the chain is the most useful and has the coroutine that captured
2711        // the type. The last coroutine (`outer_coroutine` below) has information about where the
2712        // bound was introduced. At least one coroutine should be present for this diagnostic to be
2713        // modified.
2714        let (mut trait_ref, mut target_ty) = match obligation.predicate.kind().skip_binder() {
2715            ty::PredicateKind::Clause(ty::ClauseKind::Trait(p)) => (Some(p), Some(p.self_ty())),
2716            _ => (None, None),
2717        };
2718        let mut coroutine = None;
2719        let mut outer_coroutine = None;
2720        let mut next_code = Some(obligation.cause.code());
2721
2722        let mut seen_upvar_tys_infer_tuple = false;
2723
2724        while let Some(code) = next_code {
2725            debug!(?code);
2726            match code {
2727                ObligationCauseCode::FunctionArg { parent_code, .. } => {
2728                    next_code = Some(parent_code);
2729                }
2730                ObligationCauseCode::ImplDerived(cause) => {
2731                    let ty = cause.derived.parent_trait_pred.skip_binder().self_ty();
2732                    debug!(
2733                        parent_trait_ref = ?cause.derived.parent_trait_pred,
2734                        self_ty.kind = ?ty.kind(),
2735                        "ImplDerived",
2736                    );
2737
2738                    match *ty.kind() {
2739                        ty::Coroutine(did, ..) | ty::CoroutineWitness(did, _) => {
2740                            coroutine = coroutine.or(Some(did));
2741                            outer_coroutine = Some(did);
2742                        }
2743                        ty::Tuple(_) if !seen_upvar_tys_infer_tuple => {
2744                            // By introducing a tuple of upvar types into the chain of obligations
2745                            // of a coroutine, the first non-coroutine item is now the tuple itself,
2746                            // we shall ignore this.
2747
2748                            seen_upvar_tys_infer_tuple = true;
2749                        }
2750                        _ if coroutine.is_none() => {
2751                            trait_ref = Some(cause.derived.parent_trait_pred.skip_binder());
2752                            target_ty = Some(ty);
2753                        }
2754                        _ => {}
2755                    }
2756
2757                    next_code = Some(&cause.derived.parent_code);
2758                }
2759                ObligationCauseCode::WellFormedDerived(derived_obligation)
2760                | ObligationCauseCode::BuiltinDerived(derived_obligation) => {
2761                    let ty = derived_obligation.parent_trait_pred.skip_binder().self_ty();
2762                    debug!(
2763                        parent_trait_ref = ?derived_obligation.parent_trait_pred,
2764                        self_ty.kind = ?ty.kind(),
2765                    );
2766
2767                    match *ty.kind() {
2768                        ty::Coroutine(did, ..) | ty::CoroutineWitness(did, ..) => {
2769                            coroutine = coroutine.or(Some(did));
2770                            outer_coroutine = Some(did);
2771                        }
2772                        ty::Tuple(_) if !seen_upvar_tys_infer_tuple => {
2773                            // By introducing a tuple of upvar types into the chain of obligations
2774                            // of a coroutine, the first non-coroutine item is now the tuple itself,
2775                            // we shall ignore this.
2776
2777                            seen_upvar_tys_infer_tuple = true;
2778                        }
2779                        _ if coroutine.is_none() => {
2780                            trait_ref = Some(derived_obligation.parent_trait_pred.skip_binder());
2781                            target_ty = Some(ty);
2782                        }
2783                        _ => {}
2784                    }
2785
2786                    next_code = Some(&derived_obligation.parent_code);
2787                }
2788                _ => break,
2789            }
2790        }
2791
2792        // Only continue if a coroutine was found.
2793        debug!(?coroutine, ?trait_ref, ?target_ty);
2794        let (Some(coroutine_did), Some(trait_ref), Some(target_ty)) =
2795            (coroutine, trait_ref, target_ty)
2796        else {
2797            return false;
2798        };
2799
2800        let span = self.tcx.def_span(coroutine_did);
2801
2802        let coroutine_did_root = self.tcx.typeck_root_def_id(coroutine_did);
2803        debug!(
2804            ?coroutine_did,
2805            ?coroutine_did_root,
2806            typeck_results.hir_owner = ?self.typeck_results.as_ref().map(|t| t.hir_owner),
2807            ?span,
2808        );
2809
2810        let coroutine_body =
2811            coroutine_did.as_local().and_then(|def_id| self.tcx.hir_maybe_body_owned_by(def_id));
2812        let mut visitor = AwaitsVisitor::default();
2813        if let Some(body) = coroutine_body {
2814            visitor.visit_body(&body);
2815        }
2816        debug!(awaits = ?visitor.awaits);
2817
2818        // Look for a type inside the coroutine interior that matches the target type to get
2819        // a span.
2820        let target_ty_erased = self.tcx.erase_and_anonymize_regions(target_ty);
2821        let ty_matches = |ty| -> bool {
2822            // Careful: the regions for types that appear in the
2823            // coroutine interior are not generally known, so we
2824            // want to erase them when comparing (and anyway,
2825            // `Send` and other bounds are generally unaffected by
2826            // the choice of region). When erasing regions, we
2827            // also have to erase late-bound regions. This is
2828            // because the types that appear in the coroutine
2829            // interior generally contain "bound regions" to
2830            // represent regions that are part of the suspended
2831            // coroutine frame. Bound regions are preserved by
2832            // `erase_and_anonymize_regions` and so we must also call
2833            // `instantiate_bound_regions_with_erased`.
2834            let ty_erased = self.tcx.instantiate_bound_regions_with_erased(ty);
2835            let ty_erased = self.tcx.erase_and_anonymize_regions(ty_erased);
2836            let eq = ty_erased == target_ty_erased;
2837            debug!(?ty_erased, ?target_ty_erased, ?eq);
2838            eq
2839        };
2840
2841        // Get the typeck results from the infcx if the coroutine is the function we are currently
2842        // type-checking; otherwise, get them by performing a query. This is needed to avoid
2843        // cycles. If we can't use resolved types because the coroutine comes from another crate,
2844        // we still provide a targeted error but without all the relevant spans.
2845        let coroutine_data = match &self.typeck_results {
2846            Some(t) if t.hir_owner.to_def_id() == coroutine_did_root => CoroutineData(t),
2847            _ if coroutine_did.is_local() => {
2848                CoroutineData(self.tcx.typeck(coroutine_did.expect_local()))
2849            }
2850            _ => return false,
2851        };
2852
2853        let coroutine_within_in_progress_typeck = match &self.typeck_results {
2854            Some(t) => t.hir_owner.to_def_id() == coroutine_did_root,
2855            _ => false,
2856        };
2857
2858        let mut interior_or_upvar_span = None;
2859
2860        let from_awaited_ty = coroutine_data.get_from_await_ty(visitor, self.tcx, ty_matches);
2861        debug!(?from_awaited_ty);
2862
2863        // Avoid disclosing internal information to downstream crates.
2864        if coroutine_did.is_local()
2865            // Try to avoid cycles.
2866            && !coroutine_within_in_progress_typeck
2867            && let Some(coroutine_info) = self.tcx.mir_coroutine_witnesses(coroutine_did)
2868        {
2869            debug!(?coroutine_info);
2870            'find_source: for (variant, source_info) in
2871                coroutine_info.variant_fields.iter().zip(&coroutine_info.variant_source_info)
2872            {
2873                debug!(?variant);
2874                for &local in variant {
2875                    let decl = &coroutine_info.field_tys[local];
2876                    debug!(?decl);
2877                    if ty_matches(ty::Binder::dummy(decl.ty)) && !decl.ignore_for_traits {
2878                        interior_or_upvar_span = Some(CoroutineInteriorOrUpvar::Interior(
2879                            decl.source_info.span,
2880                            Some((source_info.span, from_awaited_ty)),
2881                        ));
2882                        break 'find_source;
2883                    }
2884                }
2885            }
2886        }
2887
2888        if interior_or_upvar_span.is_none() {
2889            interior_or_upvar_span =
2890                coroutine_data.try_get_upvar_span(self, coroutine_did, ty_matches);
2891        }
2892
2893        if interior_or_upvar_span.is_none() && !coroutine_did.is_local() {
2894            interior_or_upvar_span = Some(CoroutineInteriorOrUpvar::Interior(span, None));
2895        }
2896
2897        debug!(?interior_or_upvar_span);
2898        if let Some(interior_or_upvar_span) = interior_or_upvar_span {
2899            let is_async = self.tcx.coroutine_is_async(coroutine_did);
2900            self.note_obligation_cause_for_async_await(
2901                err,
2902                interior_or_upvar_span,
2903                is_async,
2904                outer_coroutine,
2905                trait_ref,
2906                target_ty,
2907                obligation,
2908                next_code,
2909            );
2910            true
2911        } else {
2912            false
2913        }
2914    }
2915
2916    /// Unconditionally adds the diagnostic note described in
2917    /// `maybe_note_obligation_cause_for_async_await`'s documentation comment.
2918    #[allow(clippy :: suspicious_else_formatting)]
{
    let __tracing_attr_span;
    let __tracing_attr_guard;
    if ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() ||
            { false } {
        __tracing_attr_span =
            {
                use ::tracing::__macro_support::Callsite as _;
                static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                    {
                        static META: ::tracing::Metadata<'static> =
                            {
                                ::tracing_core::metadata::Metadata::new("note_obligation_cause_for_async_await",
                                    "rustc_trait_selection::error_reporting::traits::suggestions",
                                    ::tracing::Level::DEBUG,
                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs"),
                                    ::tracing_core::__macro_support::Option::Some(2918u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_trait_selection::error_reporting::traits::suggestions"),
                                    ::tracing_core::field::FieldSet::new(&[],
                                        ::tracing_core::callsite::Identifier(&__CALLSITE)),
                                    ::tracing::metadata::Kind::SPAN)
                            };
                        ::tracing::callsite::DefaultCallsite::new(&META)
                    };
                let mut interest = ::tracing::subscriber::Interest::never();
                if ::tracing::Level::DEBUG <=
                                    ::tracing::level_filters::STATIC_MAX_LEVEL &&
                                ::tracing::Level::DEBUG <=
                                    ::tracing::level_filters::LevelFilter::current() &&
                            { interest = __CALLSITE.interest(); !interest.is_never() }
                        &&
                        ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                            interest) {
                    let meta = __CALLSITE.metadata();
                    ::tracing::Span::new(meta,
                        &{ meta.fields().value_set(&[]) })
                } else {
                    let span =
                        ::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
                    {};
                    span
                }
            };
        __tracing_attr_guard = __tracing_attr_span.enter();
    }

    #[warn(clippy :: suspicious_else_formatting)]
    {

        #[allow(unknown_lints, unreachable_code, clippy ::
        diverging_sub_expression, clippy :: empty_loop, clippy ::
        let_unit_value, clippy :: let_with_type_underscore, clippy ::
        needless_return, clippy :: unreachable)]
        if false {
            let __tracing_attr_fake_return: () = loop {};
            return __tracing_attr_fake_return;
        }
        {
            let source_map = self.tcx.sess.source_map();
            let (await_or_yield, an_await_or_yield) =
                if is_async {
                    ("await", "an await")
                } else { ("yield", "a yield") };
            let future_or_coroutine =
                if is_async { "future" } else { "coroutine" };
            let trait_explanation =
                if let Some(name @ (sym::Send | sym::Sync)) =
                        self.tcx.get_diagnostic_name(trait_pred.def_id()) {
                    let (trait_name, trait_verb) =
                        if name == sym::Send {
                            ("`Send`", "sent")
                        } else { ("`Sync`", "shared") };
                    err.code = None;
                    err.primary_message(::alloc::__export::must_use({
                                ::alloc::fmt::format(format_args!("{0} cannot be {1} between threads safely",
                                        future_or_coroutine, trait_verb))
                            }));
                    let original_span = err.span.primary_span().unwrap();
                    let mut span = MultiSpan::from_span(original_span);
                    let message =
                        outer_coroutine.and_then(|coroutine_did|
                                    {
                                        Some(match self.tcx.coroutine_kind(coroutine_did).unwrap() {
                                                CoroutineKind::Coroutine(_) =>
                                                    ::alloc::__export::must_use({
                                                            ::alloc::fmt::format(format_args!("coroutine is not {0}",
                                                                    trait_name))
                                                        }),
                                                CoroutineKind::Desugared(CoroutineDesugaring::Async,
                                                    CoroutineSource::Fn) =>
                                                    self.tcx.parent(coroutine_did).as_local().map(|parent_did|
                                                                        self.tcx.local_def_id_to_hir_id(parent_did)).and_then(|parent_hir_id|
                                                                    self.tcx.hir_opt_name(parent_hir_id)).map(|name|
                                                                {
                                                                    ::alloc::__export::must_use({
                                                                            ::alloc::fmt::format(format_args!("future returned by `{0}` is not {1}",
                                                                                    name, trait_name))
                                                                        })
                                                                })?,
                                                CoroutineKind::Desugared(CoroutineDesugaring::Async,
                                                    CoroutineSource::Block) => {
                                                    ::alloc::__export::must_use({
                                                            ::alloc::fmt::format(format_args!("future created by async block is not {0}",
                                                                    trait_name))
                                                        })
                                                }
                                                CoroutineKind::Desugared(CoroutineDesugaring::Async,
                                                    CoroutineSource::Closure) => {
                                                    ::alloc::__export::must_use({
                                                            ::alloc::fmt::format(format_args!("future created by async closure is not {0}",
                                                                    trait_name))
                                                        })
                                                }
                                                CoroutineKind::Desugared(CoroutineDesugaring::AsyncGen,
                                                    CoroutineSource::Fn) =>
                                                    self.tcx.parent(coroutine_did).as_local().map(|parent_did|
                                                                        self.tcx.local_def_id_to_hir_id(parent_did)).and_then(|parent_hir_id|
                                                                    self.tcx.hir_opt_name(parent_hir_id)).map(|name|
                                                                {
                                                                    ::alloc::__export::must_use({
                                                                            ::alloc::fmt::format(format_args!("async iterator returned by `{0}` is not {1}",
                                                                                    name, trait_name))
                                                                        })
                                                                })?,
                                                CoroutineKind::Desugared(CoroutineDesugaring::AsyncGen,
                                                    CoroutineSource::Block) => {
                                                    ::alloc::__export::must_use({
                                                            ::alloc::fmt::format(format_args!("async iterator created by async gen block is not {0}",
                                                                    trait_name))
                                                        })
                                                }
                                                CoroutineKind::Desugared(CoroutineDesugaring::AsyncGen,
                                                    CoroutineSource::Closure) => {
                                                    ::alloc::__export::must_use({
                                                            ::alloc::fmt::format(format_args!("async iterator created by async gen closure is not {0}",
                                                                    trait_name))
                                                        })
                                                }
                                                CoroutineKind::Desugared(CoroutineDesugaring::Gen,
                                                    CoroutineSource::Fn) => {
                                                    self.tcx.parent(coroutine_did).as_local().map(|parent_did|
                                                                        self.tcx.local_def_id_to_hir_id(parent_did)).and_then(|parent_hir_id|
                                                                    self.tcx.hir_opt_name(parent_hir_id)).map(|name|
                                                                {
                                                                    ::alloc::__export::must_use({
                                                                            ::alloc::fmt::format(format_args!("iterator returned by `{0}` is not {1}",
                                                                                    name, trait_name))
                                                                        })
                                                                })?
                                                }
                                                CoroutineKind::Desugared(CoroutineDesugaring::Gen,
                                                    CoroutineSource::Block) => {
                                                    ::alloc::__export::must_use({
                                                            ::alloc::fmt::format(format_args!("iterator created by gen block is not {0}",
                                                                    trait_name))
                                                        })
                                                }
                                                CoroutineKind::Desugared(CoroutineDesugaring::Gen,
                                                    CoroutineSource::Closure) => {
                                                    ::alloc::__export::must_use({
                                                            ::alloc::fmt::format(format_args!("iterator created by gen closure is not {0}",
                                                                    trait_name))
                                                        })
                                                }
                                            })
                                    }).unwrap_or_else(||
                                ::alloc::__export::must_use({
                                        ::alloc::fmt::format(format_args!("{0} is not {1}",
                                                future_or_coroutine, trait_name))
                                    }));
                    span.push_span_label(original_span, message);
                    err.span(span);
                    ::alloc::__export::must_use({
                            ::alloc::fmt::format(format_args!("is not {0}", trait_name))
                        })
                } else {
                    ::alloc::__export::must_use({
                            ::alloc::fmt::format(format_args!("does not implement `{0}`",
                                    trait_pred.print_modifiers_and_trait_path()))
                        })
                };
            let mut explain_yield =
                |interior_span: Span, yield_span: Span|
                    {
                        let mut span = MultiSpan::from_span(yield_span);
                        let snippet =
                            match source_map.span_to_snippet(interior_span) {
                                Ok(snippet) if !snippet.contains('\n') =>
                                    ::alloc::__export::must_use({
                                            ::alloc::fmt::format(format_args!("`{0}`", snippet))
                                        }),
                                _ => "the value".to_string(),
                            };
                        span.push_span_label(yield_span,
                            ::alloc::__export::must_use({
                                    ::alloc::fmt::format(format_args!("{0} occurs here, with {1} maybe used later",
                                            await_or_yield, snippet))
                                }));
                        span.push_span_label(interior_span,
                            ::alloc::__export::must_use({
                                    ::alloc::fmt::format(format_args!("has type `{0}` which {1}",
                                            target_ty, trait_explanation))
                                }));
                        err.span_note(span,
                            ::alloc::__export::must_use({
                                    ::alloc::fmt::format(format_args!("{0} {1} as this value is used across {2}",
                                            future_or_coroutine, trait_explanation, an_await_or_yield))
                                }));
                    };
            match interior_or_upvar_span {
                CoroutineInteriorOrUpvar::Interior(interior_span,
                    interior_extra_info) => {
                    if let Some((yield_span, from_awaited_ty)) =
                            interior_extra_info {
                        if let Some(await_span) = from_awaited_ty {
                            let mut span = MultiSpan::from_span(await_span);
                            span.push_span_label(await_span,
                                ::alloc::__export::must_use({
                                        ::alloc::fmt::format(format_args!("await occurs here on type `{0}`, which {1}",
                                                target_ty, trait_explanation))
                                    }));
                            err.span_note(span,
                                ::alloc::__export::must_use({
                                        ::alloc::fmt::format(format_args!("future {0} as it awaits another future which {0}",
                                                trait_explanation))
                                    }));
                        } else { explain_yield(interior_span, yield_span); }
                    }
                }
                CoroutineInteriorOrUpvar::Upvar(upvar_span) => {
                    let non_send =
                        match target_ty.kind() {
                            ty::Ref(_, ref_ty, mutability) =>
                                match self.evaluate_obligation(obligation) {
                                    Ok(eval) if !eval.may_apply() =>
                                        Some((ref_ty, mutability.is_mut())),
                                    _ => None,
                                },
                            _ => None,
                        };
                    let (span_label, span_note) =
                        match non_send {
                            Some((ref_ty, is_mut)) => {
                                let ref_ty_trait = if is_mut { "Send" } else { "Sync" };
                                let ref_kind = if is_mut { "&mut" } else { "&" };
                                (::alloc::__export::must_use({
                                            ::alloc::fmt::format(format_args!("has type `{0}` which {1}, because `{2}` is not `{3}`",
                                                    target_ty, trait_explanation, ref_ty, ref_ty_trait))
                                        }),
                                    ::alloc::__export::must_use({
                                            ::alloc::fmt::format(format_args!("captured value {0} because `{1}` references cannot be sent unless their referent is `{2}`",
                                                    trait_explanation, ref_kind, ref_ty_trait))
                                        }))
                            }
                            None =>
                                (::alloc::__export::must_use({
                                            ::alloc::fmt::format(format_args!("has type `{0}` which {1}",
                                                    target_ty, trait_explanation))
                                        }),
                                    ::alloc::__export::must_use({
                                            ::alloc::fmt::format(format_args!("captured value {0}",
                                                    trait_explanation))
                                        })),
                        };
                    let mut span = MultiSpan::from_span(upvar_span);
                    span.push_span_label(upvar_span, span_label);
                    err.span_note(span, span_note);
                }
            }
            {
                use ::tracing::__macro_support::Callsite as _;
                static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                    {
                        static META: ::tracing::Metadata<'static> =
                            {
                                ::tracing_core::metadata::Metadata::new("event compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs:3141",
                                    "rustc_trait_selection::error_reporting::traits::suggestions",
                                    ::tracing::Level::DEBUG,
                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs"),
                                    ::tracing_core::__macro_support::Option::Some(3141u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_trait_selection::error_reporting::traits::suggestions"),
                                    ::tracing_core::field::FieldSet::new(&["next_code"],
                                        ::tracing_core::callsite::Identifier(&__CALLSITE)),
                                    ::tracing::metadata::Kind::EVENT)
                            };
                        ::tracing::callsite::DefaultCallsite::new(&META)
                    };
                let enabled =
                    ::tracing::Level::DEBUG <=
                                ::tracing::level_filters::STATIC_MAX_LEVEL &&
                            ::tracing::Level::DEBUG <=
                                ::tracing::level_filters::LevelFilter::current() &&
                        {
                            let interest = __CALLSITE.interest();
                            !interest.is_never() &&
                                ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                                    interest)
                        };
                if enabled {
                    (|value_set: ::tracing::field::ValueSet|
                                {
                                    let meta = __CALLSITE.metadata();
                                    ::tracing::Event::dispatch(meta, &value_set);
                                    ;
                                })({
                            #[allow(unused_imports)]
                            use ::tracing::field::{debug, display, Value};
                            let mut iter = __CALLSITE.metadata().fields().iter();
                            __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                ::tracing::__macro_support::Option::Some(&debug(&next_code)
                                                        as &dyn Value))])
                        });
                } else { ; }
            };
            self.note_obligation_cause_code(obligation.cause.body_id, err,
                obligation.predicate, obligation.param_env,
                next_code.unwrap(), &mut Vec::new(), &mut Default::default());
        }
    }
}#[instrument(level = "debug", skip_all)]
2919    fn note_obligation_cause_for_async_await<G: EmissionGuarantee>(
2920        &self,
2921        err: &mut Diag<'_, G>,
2922        interior_or_upvar_span: CoroutineInteriorOrUpvar,
2923        is_async: bool,
2924        outer_coroutine: Option<DefId>,
2925        trait_pred: ty::TraitPredicate<'tcx>,
2926        target_ty: Ty<'tcx>,
2927        obligation: &PredicateObligation<'tcx>,
2928        next_code: Option<&ObligationCauseCode<'tcx>>,
2929    ) {
2930        let source_map = self.tcx.sess.source_map();
2931
2932        let (await_or_yield, an_await_or_yield) =
2933            if is_async { ("await", "an await") } else { ("yield", "a yield") };
2934        let future_or_coroutine = if is_async { "future" } else { "coroutine" };
2935
2936        // Special case the primary error message when send or sync is the trait that was
2937        // not implemented.
2938        let trait_explanation = if let Some(name @ (sym::Send | sym::Sync)) =
2939            self.tcx.get_diagnostic_name(trait_pred.def_id())
2940        {
2941            let (trait_name, trait_verb) =
2942                if name == sym::Send { ("`Send`", "sent") } else { ("`Sync`", "shared") };
2943
2944            err.code = None;
2945            err.primary_message(format!(
2946                "{future_or_coroutine} cannot be {trait_verb} between threads safely"
2947            ));
2948
2949            let original_span = err.span.primary_span().unwrap();
2950            let mut span = MultiSpan::from_span(original_span);
2951
2952            let message = outer_coroutine
2953                .and_then(|coroutine_did| {
2954                    Some(match self.tcx.coroutine_kind(coroutine_did).unwrap() {
2955                        CoroutineKind::Coroutine(_) => format!("coroutine is not {trait_name}"),
2956                        CoroutineKind::Desugared(
2957                            CoroutineDesugaring::Async,
2958                            CoroutineSource::Fn,
2959                        ) => self
2960                            .tcx
2961                            .parent(coroutine_did)
2962                            .as_local()
2963                            .map(|parent_did| self.tcx.local_def_id_to_hir_id(parent_did))
2964                            .and_then(|parent_hir_id| self.tcx.hir_opt_name(parent_hir_id))
2965                            .map(|name| {
2966                                format!("future returned by `{name}` is not {trait_name}")
2967                            })?,
2968                        CoroutineKind::Desugared(
2969                            CoroutineDesugaring::Async,
2970                            CoroutineSource::Block,
2971                        ) => {
2972                            format!("future created by async block is not {trait_name}")
2973                        }
2974                        CoroutineKind::Desugared(
2975                            CoroutineDesugaring::Async,
2976                            CoroutineSource::Closure,
2977                        ) => {
2978                            format!("future created by async closure is not {trait_name}")
2979                        }
2980                        CoroutineKind::Desugared(
2981                            CoroutineDesugaring::AsyncGen,
2982                            CoroutineSource::Fn,
2983                        ) => self
2984                            .tcx
2985                            .parent(coroutine_did)
2986                            .as_local()
2987                            .map(|parent_did| self.tcx.local_def_id_to_hir_id(parent_did))
2988                            .and_then(|parent_hir_id| self.tcx.hir_opt_name(parent_hir_id))
2989                            .map(|name| {
2990                                format!("async iterator returned by `{name}` is not {trait_name}")
2991                            })?,
2992                        CoroutineKind::Desugared(
2993                            CoroutineDesugaring::AsyncGen,
2994                            CoroutineSource::Block,
2995                        ) => {
2996                            format!("async iterator created by async gen block is not {trait_name}")
2997                        }
2998                        CoroutineKind::Desugared(
2999                            CoroutineDesugaring::AsyncGen,
3000                            CoroutineSource::Closure,
3001                        ) => {
3002                            format!(
3003                                "async iterator created by async gen closure is not {trait_name}"
3004                            )
3005                        }
3006                        CoroutineKind::Desugared(CoroutineDesugaring::Gen, CoroutineSource::Fn) => {
3007                            self.tcx
3008                                .parent(coroutine_did)
3009                                .as_local()
3010                                .map(|parent_did| self.tcx.local_def_id_to_hir_id(parent_did))
3011                                .and_then(|parent_hir_id| self.tcx.hir_opt_name(parent_hir_id))
3012                                .map(|name| {
3013                                    format!("iterator returned by `{name}` is not {trait_name}")
3014                                })?
3015                        }
3016                        CoroutineKind::Desugared(
3017                            CoroutineDesugaring::Gen,
3018                            CoroutineSource::Block,
3019                        ) => {
3020                            format!("iterator created by gen block is not {trait_name}")
3021                        }
3022                        CoroutineKind::Desugared(
3023                            CoroutineDesugaring::Gen,
3024                            CoroutineSource::Closure,
3025                        ) => {
3026                            format!("iterator created by gen closure is not {trait_name}")
3027                        }
3028                    })
3029                })
3030                .unwrap_or_else(|| format!("{future_or_coroutine} is not {trait_name}"));
3031
3032            span.push_span_label(original_span, message);
3033            err.span(span);
3034
3035            format!("is not {trait_name}")
3036        } else {
3037            format!("does not implement `{}`", trait_pred.print_modifiers_and_trait_path())
3038        };
3039
3040        let mut explain_yield = |interior_span: Span, yield_span: Span| {
3041            let mut span = MultiSpan::from_span(yield_span);
3042            let snippet = match source_map.span_to_snippet(interior_span) {
3043                // #70935: If snippet contains newlines, display "the value" instead
3044                // so that we do not emit complex diagnostics.
3045                Ok(snippet) if !snippet.contains('\n') => format!("`{snippet}`"),
3046                _ => "the value".to_string(),
3047            };
3048            // note: future is not `Send` as this value is used across an await
3049            //   --> $DIR/issue-70935-complex-spans.rs:13:9
3050            //    |
3051            // LL |            baz(|| async {
3052            //    |  ______________-
3053            //    | |
3054            //    | |
3055            // LL | |              foo(tx.clone());
3056            // LL | |          }).await;
3057            //    | |          - ^^^^^^ await occurs here, with value maybe used later
3058            //    | |__________|
3059            //    |            has type `closure` which is not `Send`
3060            // note: value is later dropped here
3061            // LL | |          }).await;
3062            //    | |                  ^
3063            //
3064            span.push_span_label(
3065                yield_span,
3066                format!("{await_or_yield} occurs here, with {snippet} maybe used later"),
3067            );
3068            span.push_span_label(
3069                interior_span,
3070                format!("has type `{target_ty}` which {trait_explanation}"),
3071            );
3072            err.span_note(
3073                span,
3074                format!("{future_or_coroutine} {trait_explanation} as this value is used across {an_await_or_yield}"),
3075            );
3076        };
3077        match interior_or_upvar_span {
3078            CoroutineInteriorOrUpvar::Interior(interior_span, interior_extra_info) => {
3079                if let Some((yield_span, from_awaited_ty)) = interior_extra_info {
3080                    if let Some(await_span) = from_awaited_ty {
3081                        // The type causing this obligation is one being awaited at await_span.
3082                        let mut span = MultiSpan::from_span(await_span);
3083                        span.push_span_label(
3084                            await_span,
3085                            format!(
3086                                "await occurs here on type `{target_ty}`, which {trait_explanation}"
3087                            ),
3088                        );
3089                        err.span_note(
3090                            span,
3091                            format!(
3092                                "future {trait_explanation} as it awaits another future which {trait_explanation}"
3093                            ),
3094                        );
3095                    } else {
3096                        // Look at the last interior type to get a span for the `.await`.
3097                        explain_yield(interior_span, yield_span);
3098                    }
3099                }
3100            }
3101            CoroutineInteriorOrUpvar::Upvar(upvar_span) => {
3102                // `Some((ref_ty, is_mut))` if `target_ty` is `&T` or `&mut T` and fails to impl `Send`
3103                let non_send = match target_ty.kind() {
3104                    ty::Ref(_, ref_ty, mutability) => match self.evaluate_obligation(obligation) {
3105                        Ok(eval) if !eval.may_apply() => Some((ref_ty, mutability.is_mut())),
3106                        _ => None,
3107                    },
3108                    _ => None,
3109                };
3110
3111                let (span_label, span_note) = match non_send {
3112                    // if `target_ty` is `&T` or `&mut T` and fails to impl `Send`,
3113                    // include suggestions to make `T: Sync` so that `&T: Send`,
3114                    // or to make `T: Send` so that `&mut T: Send`
3115                    Some((ref_ty, is_mut)) => {
3116                        let ref_ty_trait = if is_mut { "Send" } else { "Sync" };
3117                        let ref_kind = if is_mut { "&mut" } else { "&" };
3118                        (
3119                            format!(
3120                                "has type `{target_ty}` which {trait_explanation}, because `{ref_ty}` is not `{ref_ty_trait}`"
3121                            ),
3122                            format!(
3123                                "captured value {trait_explanation} because `{ref_kind}` references cannot be sent unless their referent is `{ref_ty_trait}`"
3124                            ),
3125                        )
3126                    }
3127                    None => (
3128                        format!("has type `{target_ty}` which {trait_explanation}"),
3129                        format!("captured value {trait_explanation}"),
3130                    ),
3131                };
3132
3133                let mut span = MultiSpan::from_span(upvar_span);
3134                span.push_span_label(upvar_span, span_label);
3135                err.span_note(span, span_note);
3136            }
3137        }
3138
3139        // Add a note for the item obligation that remains - normally a note pointing to the
3140        // bound that introduced the obligation (e.g. `T: Send`).
3141        debug!(?next_code);
3142        self.note_obligation_cause_code(
3143            obligation.cause.body_id,
3144            err,
3145            obligation.predicate,
3146            obligation.param_env,
3147            next_code.unwrap(),
3148            &mut Vec::new(),
3149            &mut Default::default(),
3150        );
3151    }
3152
3153    pub(super) fn note_obligation_cause_code<G: EmissionGuarantee, T>(
3154        &self,
3155        body_id: LocalDefId,
3156        err: &mut Diag<'_, G>,
3157        predicate: T,
3158        param_env: ty::ParamEnv<'tcx>,
3159        cause_code: &ObligationCauseCode<'tcx>,
3160        obligated_types: &mut Vec<Ty<'tcx>>,
3161        seen_requirements: &mut FxHashSet<DefId>,
3162    ) where
3163        T: Upcast<TyCtxt<'tcx>, ty::Predicate<'tcx>>,
3164    {
3165        let tcx = self.tcx;
3166        let predicate = predicate.upcast(tcx);
3167        let suggest_remove_deref = |err: &mut Diag<'_, G>, expr: &hir::Expr<'_>| {
3168            if let Some(pred) = predicate.as_trait_clause()
3169                && tcx.is_lang_item(pred.def_id(), LangItem::Sized)
3170                && let hir::ExprKind::Unary(hir::UnOp::Deref, inner) = expr.kind
3171            {
3172                err.span_suggestion_verbose(
3173                    expr.span.until(inner.span),
3174                    "references are always `Sized`, even if they point to unsized data; consider \
3175                     not dereferencing the expression",
3176                    String::new(),
3177                    Applicability::MaybeIncorrect,
3178                );
3179            }
3180        };
3181        match *cause_code {
3182            ObligationCauseCode::ExprAssignable
3183            | ObligationCauseCode::MatchExpressionArm { .. }
3184            | ObligationCauseCode::Pattern { .. }
3185            | ObligationCauseCode::IfExpression { .. }
3186            | ObligationCauseCode::IfExpressionWithNoElse
3187            | ObligationCauseCode::MainFunctionType
3188            | ObligationCauseCode::LangFunctionType(_)
3189            | ObligationCauseCode::IntrinsicType
3190            | ObligationCauseCode::MethodReceiver
3191            | ObligationCauseCode::ReturnNoExpression
3192            | ObligationCauseCode::Misc
3193            | ObligationCauseCode::WellFormed(..)
3194            | ObligationCauseCode::MatchImpl(..)
3195            | ObligationCauseCode::ReturnValue(_)
3196            | ObligationCauseCode::BlockTailExpression(..)
3197            | ObligationCauseCode::AwaitableExpr(_)
3198            | ObligationCauseCode::ForLoopIterator
3199            | ObligationCauseCode::QuestionMark
3200            | ObligationCauseCode::CheckAssociatedTypeBounds { .. }
3201            | ObligationCauseCode::LetElse
3202            | ObligationCauseCode::UnOp { .. }
3203            | ObligationCauseCode::AscribeUserTypeProvePredicate(..)
3204            | ObligationCauseCode::AlwaysApplicableImpl
3205            | ObligationCauseCode::ConstParam(_)
3206            | ObligationCauseCode::ReferenceOutlivesReferent(..)
3207            | ObligationCauseCode::ObjectTypeBound(..) => {}
3208            ObligationCauseCode::BinOp { lhs_hir_id, rhs_hir_id, .. } => {
3209                if let hir::Node::Expr(lhs) = tcx.hir_node(lhs_hir_id)
3210                    && let hir::Node::Expr(rhs) = tcx.hir_node(rhs_hir_id)
3211                    && tcx.sess.source_map().lookup_char_pos(lhs.span.lo()).line
3212                        != tcx.sess.source_map().lookup_char_pos(rhs.span.hi()).line
3213                {
3214                    err.span_label(lhs.span, "");
3215                    err.span_label(rhs.span, "");
3216                }
3217            }
3218            ObligationCauseCode::RustCall => {
3219                if let Some(pred) = predicate.as_trait_clause()
3220                    && tcx.is_lang_item(pred.def_id(), LangItem::Sized)
3221                {
3222                    err.note("argument required to be sized due to `extern \"rust-call\"` ABI");
3223                }
3224            }
3225            ObligationCauseCode::SliceOrArrayElem => {
3226                err.note("slice and array elements must have `Sized` type");
3227            }
3228            ObligationCauseCode::ArrayLen(array_ty) => {
3229                err.note(::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("the length of array `{0}` must be type `usize`",
                array_ty))
    })format!("the length of array `{array_ty}` must be type `usize`"));
3230            }
3231            ObligationCauseCode::TupleElem => {
3232                err.note("only the last element of a tuple may have a dynamically sized type");
3233            }
3234            ObligationCauseCode::DynCompatible(span) => {
3235                err.multipart_suggestion(
3236                    "you might have meant to use `Self` to refer to the implementing type",
3237                    ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [(span, "Self".into())]))vec![(span, "Self".into())],
3238                    Applicability::MachineApplicable,
3239                );
3240            }
3241            ObligationCauseCode::WhereClause(item_def_id, span)
3242            | ObligationCauseCode::WhereClauseInExpr(item_def_id, span, ..)
3243            | ObligationCauseCode::HostEffectInExpr(item_def_id, span, ..)
3244                if !span.is_dummy() =>
3245            {
3246                if let ObligationCauseCode::WhereClauseInExpr(_, _, hir_id, pos) = &cause_code {
3247                    if let Node::Expr(expr) = tcx.parent_hir_node(*hir_id)
3248                        && let hir::ExprKind::Call(_, args) = expr.kind
3249                        && let Some(expr) = args.get(*pos)
3250                    {
3251                        suggest_remove_deref(err, &expr);
3252                    } else if let Node::Expr(expr) = self.tcx.hir_node(*hir_id)
3253                        && let hir::ExprKind::MethodCall(_, _, args, _) = expr.kind
3254                        && let Some(expr) = args.get(*pos)
3255                    {
3256                        suggest_remove_deref(err, &expr);
3257                    }
3258                }
3259                let item_name = tcx.def_path_str(item_def_id);
3260                let short_item_name = { let _guard = ForceTrimmedGuard::new(); tcx.def_path_str(item_def_id) }with_forced_trimmed_paths!(tcx.def_path_str(item_def_id));
3261                let mut multispan = MultiSpan::from(span);
3262                let sm = tcx.sess.source_map();
3263                if let Some(ident) = tcx.opt_item_ident(item_def_id) {
3264                    let same_line =
3265                        match (sm.lookup_line(ident.span.hi()), sm.lookup_line(span.lo())) {
3266                            (Ok(l), Ok(r)) => l.line == r.line,
3267                            _ => true,
3268                        };
3269                    if ident.span.is_visible(sm) && !ident.span.overlaps(span) && !same_line {
3270                        multispan.push_span_label(
3271                            ident.span,
3272                            ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("required by a bound in this {0}",
                tcx.def_kind(item_def_id).descr(item_def_id)))
    })format!(
3273                                "required by a bound in this {}",
3274                                tcx.def_kind(item_def_id).descr(item_def_id)
3275                            ),
3276                        );
3277                    }
3278                }
3279                let mut a = "a";
3280                let mut this = "this bound";
3281                let mut note = None;
3282                let mut help = None;
3283                if let ty::PredicateKind::Clause(clause) = predicate.kind().skip_binder() {
3284                    match clause {
3285                        ty::ClauseKind::Trait(trait_pred) => {
3286                            let def_id = trait_pred.def_id();
3287                            let visible_item = if let Some(local) = def_id.as_local() {
3288                                let ty = trait_pred.self_ty();
3289                                // when `TraitA: TraitB` and `S` only impl TraitA,
3290                                // we check if `TraitB` can be reachable from `S`
3291                                // to determine whether to note `TraitA` is sealed trait.
3292                                if let ty::Adt(adt, _) = ty.kind() {
3293                                    let visibilities = &tcx.resolutions(()).effective_visibilities;
3294                                    visibilities.effective_vis(local).is_none_or(|v| {
3295                                        v.at_level(Level::Reexported)
3296                                            .is_accessible_from(adt.did(), tcx)
3297                                    })
3298                                } else {
3299                                    // FIXME(xizheyin): if the type is not ADT, we should not suggest it
3300                                    true
3301                                }
3302                            } else {
3303                                // Check for foreign traits being reachable.
3304                                tcx.visible_parent_map(()).get(&def_id).is_some()
3305                            };
3306                            if tcx.is_lang_item(def_id, LangItem::Sized) {
3307                                // Check if this is an implicit bound, even in foreign crates.
3308                                if tcx
3309                                    .generics_of(item_def_id)
3310                                    .own_params
3311                                    .iter()
3312                                    .any(|param| tcx.def_span(param.def_id) == span)
3313                                {
3314                                    a = "an implicit `Sized`";
3315                                    this =
3316                                        "the implicit `Sized` requirement on this type parameter";
3317                                }
3318                                if let Some(hir::Node::TraitItem(hir::TraitItem {
3319                                    generics,
3320                                    kind: hir::TraitItemKind::Type(bounds, None),
3321                                    ..
3322                                })) = tcx.hir_get_if_local(item_def_id)
3323                                    // Do not suggest relaxing if there is an explicit `Sized` obligation.
3324                                    && !bounds.iter()
3325                                        .filter_map(|bound| bound.trait_ref())
3326                                        .any(|tr| tr.trait_def_id().is_some_and(|def_id| tcx.is_lang_item(def_id, LangItem::Sized)))
3327                                {
3328                                    let (span, separator) = if let [.., last] = bounds {
3329                                        (last.span().shrink_to_hi(), " +")
3330                                    } else {
3331                                        (generics.span.shrink_to_hi(), ":")
3332                                    };
3333                                    err.span_suggestion_verbose(
3334                                        span,
3335                                        "consider relaxing the implicit `Sized` restriction",
3336                                        ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{0} ?Sized", separator))
    })format!("{separator} ?Sized"),
3337                                        Applicability::MachineApplicable,
3338                                    );
3339                                }
3340                            }
3341                            if let DefKind::Trait = tcx.def_kind(item_def_id)
3342                                && !visible_item
3343                            {
3344                                note = Some(::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("`{1}` is a \"sealed trait\", because to implement it you also need to implement `{0}`, which is not accessible; this is usually done to force you to use one of the provided types that already implement it",
                {
                    let _guard = NoTrimmedGuard::new();
                    tcx.def_path_str(def_id)
                }, short_item_name))
    })format!(
3345                                    "`{short_item_name}` is a \"sealed trait\", because to implement it \
3346                                    you also need to implement `{}`, which is not accessible; this is \
3347                                    usually done to force you to use one of the provided types that \
3348                                    already implement it",
3349                                    with_no_trimmed_paths!(tcx.def_path_str(def_id)),
3350                                ));
3351                                let impls_of = tcx.trait_impls_of(def_id);
3352                                let impls = impls_of
3353                                    .non_blanket_impls()
3354                                    .values()
3355                                    .flatten()
3356                                    .chain(impls_of.blanket_impls().iter())
3357                                    .collect::<Vec<_>>();
3358                                if !impls.is_empty() {
3359                                    let len = impls.len();
3360                                    let mut types = impls
3361                                        .iter()
3362                                        .map(|&&t| {
3363                                            {
    let _guard = NoTrimmedGuard::new();
    ::alloc::__export::must_use({
            ::alloc::fmt::format(format_args!("  {0}",
                    tcx.type_of(t).instantiate_identity()))
        })
}with_no_trimmed_paths!(format!(
3364                                                "  {}",
3365                                                tcx.type_of(t).instantiate_identity(),
3366                                            ))
3367                                        })
3368                                        .collect::<Vec<_>>();
3369                                    let post = if types.len() > 9 {
3370                                        types.truncate(8);
3371                                        ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("\nand {0} others", len - 8))
    })format!("\nand {} others", len - 8)
3372                                    } else {
3373                                        String::new()
3374                                    };
3375                                    help = Some(::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("the following type{0} implement{1} the trait:\n{2}{3}",
                if len == 1 { "" } else { "s" },
                if len == 1 { "s" } else { "" }, types.join("\n"), post))
    })format!(
3376                                        "the following type{} implement{} the trait:\n{}{post}",
3377                                        pluralize!(len),
3378                                        if len == 1 { "s" } else { "" },
3379                                        types.join("\n"),
3380                                    ));
3381                                }
3382                            }
3383                        }
3384                        ty::ClauseKind::ConstArgHasType(..) => {
3385                            let descr =
3386                                ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("required by a const generic parameter in `{0}`",
                item_name))
    })format!("required by a const generic parameter in `{item_name}`");
3387                            if span.is_visible(sm) {
3388                                let msg = ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("required by this const generic parameter in `{0}`",
                short_item_name))
    })format!(
3389                                    "required by this const generic parameter in `{short_item_name}`"
3390                                );
3391                                multispan.push_span_label(span, msg);
3392                                err.span_note(multispan, descr);
3393                            } else {
3394                                err.span_note(tcx.def_span(item_def_id), descr);
3395                            }
3396                            return;
3397                        }
3398                        _ => (),
3399                    }
3400                }
3401
3402                // If this is from a format string literal desugaring,
3403                // we've already said "required by this formatting parameter"
3404                let is_in_fmt_lit = if let Some(s) = err.span.primary_span() {
3405                    #[allow(non_exhaustive_omitted_patterns)] match s.desugaring_kind() {
    Some(DesugaringKind::FormatLiteral { .. }) => true,
    _ => false,
}matches!(s.desugaring_kind(), Some(DesugaringKind::FormatLiteral { .. }))
3406                } else {
3407                    false
3408                };
3409                if !is_in_fmt_lit {
3410                    let descr = ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("required by {0} bound in `{1}`", a,
                item_name))
    })format!("required by {a} bound in `{item_name}`");
3411                    if span.is_visible(sm) {
3412                        let msg = ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("required by {0} in `{1}`", this,
                short_item_name))
    })format!("required by {this} in `{short_item_name}`");
3413                        multispan.push_span_label(span, msg);
3414                        err.span_note(multispan, descr);
3415                    } else {
3416                        err.span_note(tcx.def_span(item_def_id), descr);
3417                    }
3418                }
3419                if let Some(note) = note {
3420                    err.note(note);
3421                }
3422                if let Some(help) = help {
3423                    err.help(help);
3424                }
3425            }
3426            ObligationCauseCode::WhereClause(..)
3427            | ObligationCauseCode::WhereClauseInExpr(..)
3428            | ObligationCauseCode::HostEffectInExpr(..) => {
3429                // We hold the `DefId` of the item introducing the obligation, but displaying it
3430                // doesn't add user usable information. It always point at an associated item.
3431            }
3432            ObligationCauseCode::OpaqueTypeBound(span, definition_def_id) => {
3433                err.span_note(span, "required by a bound in an opaque type");
3434                if let Some(definition_def_id) = definition_def_id
3435                    // If there are any stalled coroutine obligations, then this
3436                    // error may be due to that, and not because the body has more
3437                    // where-clauses.
3438                    && self.tcx.typeck(definition_def_id).coroutine_stalled_predicates.is_empty()
3439                {
3440                    // FIXME(compiler-errors): We could probably point to something
3441                    // specific here if we tried hard enough...
3442                    err.span_note(
3443                        tcx.def_span(definition_def_id),
3444                        "this definition site has more where clauses than the opaque type",
3445                    );
3446                }
3447            }
3448            ObligationCauseCode::Coercion { source, target } => {
3449                let source =
3450                    tcx.short_string(self.resolve_vars_if_possible(source), err.long_ty_path());
3451                let target =
3452                    tcx.short_string(self.resolve_vars_if_possible(target), err.long_ty_path());
3453                err.note({
    let _guard = ForceTrimmedGuard::new();
    ::alloc::__export::must_use({
            ::alloc::fmt::format(format_args!("required for the cast from `{0}` to `{1}`",
                    source, target))
        })
}with_forced_trimmed_paths!(format!(
3454                    "required for the cast from `{source}` to `{target}`",
3455                )));
3456            }
3457            ObligationCauseCode::RepeatElementCopy { is_constable, elt_span } => {
3458                err.note(
3459                    "the `Copy` trait is required because this value will be copied for each element of the array",
3460                );
3461                let sm = tcx.sess.source_map();
3462                if #[allow(non_exhaustive_omitted_patterns)] match is_constable {
    IsConstable::Fn | IsConstable::Ctor => true,
    _ => false,
}matches!(is_constable, IsConstable::Fn | IsConstable::Ctor)
3463                    && let Ok(_) = sm.span_to_snippet(elt_span)
3464                {
3465                    err.multipart_suggestion(
3466                        "create an inline `const` block",
3467                        ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [(elt_span.shrink_to_lo(), "const { ".to_string()),
                (elt_span.shrink_to_hi(), " }".to_string())]))vec![
3468                            (elt_span.shrink_to_lo(), "const { ".to_string()),
3469                            (elt_span.shrink_to_hi(), " }".to_string()),
3470                        ],
3471                        Applicability::MachineApplicable,
3472                    );
3473                } else {
3474                    // FIXME: we may suggest array::repeat instead
3475                    err.help("consider using `core::array::from_fn` to initialize the array");
3476                    err.help("see https://doc.rust-lang.org/stable/std/array/fn.from_fn.html for more information");
3477                }
3478            }
3479            ObligationCauseCode::VariableType(hir_id) => {
3480                if let Some(typeck_results) = &self.typeck_results
3481                    && let Some(ty) = typeck_results.node_type_opt(hir_id)
3482                    && let ty::Error(_) = ty.kind()
3483                {
3484                    err.note(::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("`{0}` isn\'t satisfied, but the type of this pattern is `{{type error}}`",
                predicate))
    })format!(
3485                        "`{predicate}` isn't satisfied, but the type of this pattern is \
3486                         `{{type error}}`",
3487                    ));
3488                    err.downgrade_to_delayed_bug();
3489                }
3490                let mut local = true;
3491                match tcx.parent_hir_node(hir_id) {
3492                    Node::LetStmt(hir::LetStmt { ty: Some(ty), .. }) => {
3493                        err.span_suggestion_verbose(
3494                            ty.span.shrink_to_lo(),
3495                            "consider borrowing here",
3496                            "&",
3497                            Applicability::MachineApplicable,
3498                        );
3499                    }
3500                    Node::LetStmt(hir::LetStmt {
3501                        init: Some(hir::Expr { kind: hir::ExprKind::Index(..), span, .. }),
3502                        ..
3503                    }) => {
3504                        // When encountering an assignment of an unsized trait, like
3505                        // `let x = ""[..];`, provide a suggestion to borrow the initializer in
3506                        // order to use have a slice instead.
3507                        err.span_suggestion_verbose(
3508                            span.shrink_to_lo(),
3509                            "consider borrowing here",
3510                            "&",
3511                            Applicability::MachineApplicable,
3512                        );
3513                    }
3514                    Node::LetStmt(hir::LetStmt { init: Some(expr), .. }) => {
3515                        // When encountering an assignment of an unsized trait, like `let x = *"";`,
3516                        // we check if the RHS is a deref operation, to suggest removing it.
3517                        suggest_remove_deref(err, &expr);
3518                    }
3519                    Node::Param(param) => {
3520                        err.span_suggestion_verbose(
3521                            param.ty_span.shrink_to_lo(),
3522                            "function arguments must have a statically known size, borrowed types \
3523                            always have a known size",
3524                            "&",
3525                            Applicability::MachineApplicable,
3526                        );
3527                        local = false;
3528                    }
3529                    _ => {}
3530                }
3531                if local {
3532                    err.note("all local variables must have a statically known size");
3533                }
3534            }
3535            ObligationCauseCode::SizedArgumentType(hir_id) => {
3536                let mut ty = None;
3537                let borrowed_msg = "function arguments must have a statically known size, borrowed \
3538                                    types always have a known size";
3539                if let Some(hir_id) = hir_id
3540                    && let hir::Node::Param(param) = self.tcx.hir_node(hir_id)
3541                    && let Some(decl) = self.tcx.parent_hir_node(hir_id).fn_decl()
3542                    && let Some(t) = decl.inputs.iter().find(|t| param.ty_span.contains(t.span))
3543                {
3544                    // We use `contains` because the type might be surrounded by parentheses,
3545                    // which makes `ty_span` and `t.span` disagree with each other, but one
3546                    // fully contains the other: `foo: (dyn Foo + Bar)`
3547                    //                                 ^-------------^
3548                    //                                 ||
3549                    //                                 |t.span
3550                    //                                 param._ty_span
3551                    ty = Some(t);
3552                } else if let Some(hir_id) = hir_id
3553                    && let hir::Node::Ty(t) = self.tcx.hir_node(hir_id)
3554                {
3555                    ty = Some(t);
3556                }
3557                if let Some(ty) = ty {
3558                    match ty.kind {
3559                        hir::TyKind::TraitObject(traits, _) => {
3560                            let (span, kw) = match traits {
3561                                [first, ..] if first.span.lo() == ty.span.lo() => {
3562                                    // Missing `dyn` in front of trait object.
3563                                    (ty.span.shrink_to_lo(), "dyn ")
3564                                }
3565                                [first, ..] => (ty.span.until(first.span), ""),
3566                                [] => ::rustc_middle::util::bug::span_bug_fmt(ty.span,
    format_args!("trait object with no traits: {0:?}", ty))span_bug!(ty.span, "trait object with no traits: {ty:?}"),
3567                            };
3568                            let needs_parens = traits.len() != 1;
3569                            // Don't recommend impl Trait as a closure argument
3570                            if let Some(hir_id) = hir_id
3571                                && #[allow(non_exhaustive_omitted_patterns)] match self.tcx.parent_hir_node(hir_id)
    {
    hir::Node::Item(hir::Item { kind: hir::ItemKind::Fn { .. }, .. }) => true,
    _ => false,
}matches!(
3572                                    self.tcx.parent_hir_node(hir_id),
3573                                    hir::Node::Item(hir::Item {
3574                                        kind: hir::ItemKind::Fn { .. },
3575                                        ..
3576                                    })
3577                                )
3578                            {
3579                                err.span_suggestion_verbose(
3580                                    span,
3581                                    "you can use `impl Trait` as the argument type",
3582                                    "impl ",
3583                                    Applicability::MaybeIncorrect,
3584                                );
3585                            }
3586                            let sugg = if !needs_parens {
3587                                ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [(span.shrink_to_lo(),
                    ::alloc::__export::must_use({
                            ::alloc::fmt::format(format_args!("&{0}", kw))
                        }))]))vec![(span.shrink_to_lo(), format!("&{kw}"))]
3588                            } else {
3589                                ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [(span.shrink_to_lo(),
                    ::alloc::__export::must_use({
                            ::alloc::fmt::format(format_args!("&({0}", kw))
                        })), (ty.span.shrink_to_hi(), ")".to_string())]))vec![
3590                                    (span.shrink_to_lo(), format!("&({kw}")),
3591                                    (ty.span.shrink_to_hi(), ")".to_string()),
3592                                ]
3593                            };
3594                            err.multipart_suggestion(
3595                                borrowed_msg,
3596                                sugg,
3597                                Applicability::MachineApplicable,
3598                            );
3599                        }
3600                        hir::TyKind::Slice(_ty) => {
3601                            err.span_suggestion_verbose(
3602                                ty.span.shrink_to_lo(),
3603                                "function arguments must have a statically known size, borrowed \
3604                                 slices always have a known size",
3605                                "&",
3606                                Applicability::MachineApplicable,
3607                            );
3608                        }
3609                        hir::TyKind::Path(_) => {
3610                            err.span_suggestion_verbose(
3611                                ty.span.shrink_to_lo(),
3612                                borrowed_msg,
3613                                "&",
3614                                Applicability::MachineApplicable,
3615                            );
3616                        }
3617                        _ => {}
3618                    }
3619                } else {
3620                    err.note("all function arguments must have a statically known size");
3621                }
3622                if tcx.sess.opts.unstable_features.is_nightly_build()
3623                    && !tcx.features().unsized_fn_params()
3624                {
3625                    err.help("unsized fn params are gated as an unstable feature");
3626                }
3627            }
3628            ObligationCauseCode::SizedReturnType | ObligationCauseCode::SizedCallReturnType => {
3629                err.note("the return type of a function must have a statically known size");
3630            }
3631            ObligationCauseCode::SizedYieldType => {
3632                err.note("the yield type of a coroutine must have a statically known size");
3633            }
3634            ObligationCauseCode::AssignmentLhsSized => {
3635                err.note("the left-hand-side of an assignment must have a statically known size");
3636            }
3637            ObligationCauseCode::TupleInitializerSized => {
3638                err.note("tuples must have a statically known size to be initialized");
3639            }
3640            ObligationCauseCode::StructInitializerSized => {
3641                err.note("structs must have a statically known size to be initialized");
3642            }
3643            ObligationCauseCode::FieldSized { adt_kind: ref item, last, span } => {
3644                match *item {
3645                    AdtKind::Struct => {
3646                        if last {
3647                            err.note(
3648                                "the last field of a packed struct may only have a \
3649                                dynamically sized type if it does not need drop to be run",
3650                            );
3651                        } else {
3652                            err.note(
3653                                "only the last field of a struct may have a dynamically sized type",
3654                            );
3655                        }
3656                    }
3657                    AdtKind::Union => {
3658                        err.note("no field of a union may have a dynamically sized type");
3659                    }
3660                    AdtKind::Enum => {
3661                        err.note("no field of an enum variant may have a dynamically sized type");
3662                    }
3663                }
3664                err.help("change the field's type to have a statically known size");
3665                err.span_suggestion_verbose(
3666                    span.shrink_to_lo(),
3667                    "borrowed types always have a statically known size",
3668                    "&",
3669                    Applicability::MachineApplicable,
3670                );
3671                err.multipart_suggestion(
3672                    "the `Box` type always has a statically known size and allocates its contents \
3673                     in the heap",
3674                    ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [(span.shrink_to_lo(), "Box<".to_string()),
                (span.shrink_to_hi(), ">".to_string())]))vec![
3675                        (span.shrink_to_lo(), "Box<".to_string()),
3676                        (span.shrink_to_hi(), ">".to_string()),
3677                    ],
3678                    Applicability::MachineApplicable,
3679                );
3680            }
3681            ObligationCauseCode::SizedConstOrStatic => {
3682                err.note("statics and constants must have a statically known size");
3683            }
3684            ObligationCauseCode::InlineAsmSized => {
3685                err.note("all inline asm arguments must have a statically known size");
3686            }
3687            ObligationCauseCode::SizedClosureCapture(closure_def_id) => {
3688                err.note(
3689                    "all values captured by value by a closure must have a statically known size",
3690                );
3691                let hir::ExprKind::Closure(closure) =
3692                    tcx.hir_node_by_def_id(closure_def_id).expect_expr().kind
3693                else {
3694                    ::rustc_middle::util::bug::bug_fmt(format_args!("expected closure in SizedClosureCapture obligation"));bug!("expected closure in SizedClosureCapture obligation");
3695                };
3696                if let hir::CaptureBy::Value { .. } = closure.capture_clause
3697                    && let Some(span) = closure.fn_arg_span
3698                {
3699                    err.span_label(span, "this closure captures all values by move");
3700                }
3701            }
3702            ObligationCauseCode::SizedCoroutineInterior(coroutine_def_id) => {
3703                let what = match tcx.coroutine_kind(coroutine_def_id) {
3704                    None
3705                    | Some(hir::CoroutineKind::Coroutine(_))
3706                    | Some(hir::CoroutineKind::Desugared(hir::CoroutineDesugaring::Gen, _)) => {
3707                        "yield"
3708                    }
3709                    Some(hir::CoroutineKind::Desugared(hir::CoroutineDesugaring::Async, _)) => {
3710                        "await"
3711                    }
3712                    Some(hir::CoroutineKind::Desugared(hir::CoroutineDesugaring::AsyncGen, _)) => {
3713                        "yield`/`await"
3714                    }
3715                };
3716                err.note(::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("all values live across `{0}` must have a statically known size",
                what))
    })format!(
3717                    "all values live across `{what}` must have a statically known size"
3718                ));
3719            }
3720            ObligationCauseCode::SharedStatic => {
3721                err.note("shared static variables must have a type that implements `Sync`");
3722            }
3723            ObligationCauseCode::BuiltinDerived(ref data) => {
3724                let parent_trait_ref = self.resolve_vars_if_possible(data.parent_trait_pred);
3725                let ty = parent_trait_ref.skip_binder().self_ty();
3726                if parent_trait_ref.references_error() {
3727                    // NOTE(eddyb) this was `.cancel()`, but `err`
3728                    // is borrowed, so we can't fully defuse it.
3729                    err.downgrade_to_delayed_bug();
3730                    return;
3731                }
3732
3733                // If the obligation for a tuple is set directly by a Coroutine or Closure,
3734                // then the tuple must be the one containing capture types.
3735                let is_upvar_tys_infer_tuple = if !#[allow(non_exhaustive_omitted_patterns)] match ty.kind() {
    ty::Tuple(..) => true,
    _ => false,
}matches!(ty.kind(), ty::Tuple(..)) {
3736                    false
3737                } else if let ObligationCauseCode::BuiltinDerived(data) = &*data.parent_code {
3738                    let parent_trait_ref = self.resolve_vars_if_possible(data.parent_trait_pred);
3739                    let nested_ty = parent_trait_ref.skip_binder().self_ty();
3740                    #[allow(non_exhaustive_omitted_patterns)] match nested_ty.kind() {
    ty::Coroutine(..) => true,
    _ => false,
}matches!(nested_ty.kind(), ty::Coroutine(..))
3741                        || #[allow(non_exhaustive_omitted_patterns)] match nested_ty.kind() {
    ty::Closure(..) => true,
    _ => false,
}matches!(nested_ty.kind(), ty::Closure(..))
3742                } else {
3743                    false
3744                };
3745
3746                let is_builtin_async_fn_trait =
3747                    tcx.async_fn_trait_kind_from_def_id(data.parent_trait_pred.def_id()).is_some();
3748
3749                if !is_upvar_tys_infer_tuple && !is_builtin_async_fn_trait {
3750                    let mut msg = || {
3751                        let ty_str = tcx.short_string(ty, err.long_ty_path());
3752                        ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("required because it appears within the type `{0}`",
                ty_str))
    })format!("required because it appears within the type `{ty_str}`")
3753                    };
3754                    match *ty.kind() {
3755                        ty::Adt(def, _) => {
3756                            let msg = msg();
3757                            match tcx.opt_item_ident(def.did()) {
3758                                Some(ident) => {
3759                                    err.span_note(ident.span, msg);
3760                                }
3761                                None => {
3762                                    err.note(msg);
3763                                }
3764                            }
3765                        }
3766                        ty::Alias(ty::AliasTy { kind: ty::Opaque { def_id }, .. }) => {
3767                            // If the previous type is async fn, this is the future generated by the body of an async function.
3768                            // Avoid printing it twice (it was already printed in the `ty::Coroutine` arm below).
3769                            let is_future = tcx.ty_is_opaque_future(ty);
3770                            {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs:3770",
                        "rustc_trait_selection::error_reporting::traits::suggestions",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs"),
                        ::tracing_core::__macro_support::Option::Some(3770u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_trait_selection::error_reporting::traits::suggestions"),
                        ::tracing_core::field::FieldSet::new(&["message",
                                        "obligated_types", "is_future"],
                            ::tracing_core::callsite::Identifier(&__CALLSITE)),
                        ::tracing::metadata::Kind::EVENT)
                };
            ::tracing::callsite::DefaultCallsite::new(&META)
        };
    let enabled =
        ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() &&
            {
                let interest = __CALLSITE.interest();
                !interest.is_never() &&
                    ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                        interest)
            };
    if enabled {
        (|value_set: ::tracing::field::ValueSet|
                    {
                        let meta = __CALLSITE.metadata();
                        ::tracing::Event::dispatch(meta, &value_set);
                        ;
                    })({
                #[allow(unused_imports)]
                use ::tracing::field::{debug, display, Value};
                let mut iter = __CALLSITE.metadata().fields().iter();
                __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&format_args!("note_obligation_cause_code: check for async fn")
                                            as &dyn Value)),
                                (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&debug(&obligated_types)
                                            as &dyn Value)),
                                (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&debug(&is_future)
                                            as &dyn Value))])
            });
    } else { ; }
};debug!(
3771                                ?obligated_types,
3772                                ?is_future,
3773                                "note_obligation_cause_code: check for async fn"
3774                            );
3775                            if is_future
3776                                && obligated_types.last().is_some_and(|ty| match ty.kind() {
3777                                    ty::Coroutine(last_def_id, ..) => {
3778                                        tcx.coroutine_is_async(*last_def_id)
3779                                    }
3780                                    _ => false,
3781                                })
3782                            {
3783                                // See comment above; skip printing twice.
3784                            } else {
3785                                let msg = msg();
3786                                err.span_note(tcx.def_span(def_id), msg);
3787                            }
3788                        }
3789                        ty::Coroutine(def_id, _) => {
3790                            let sp = tcx.def_span(def_id);
3791
3792                            // Special-case this to say "async block" instead of `[static coroutine]`.
3793                            let kind = tcx.coroutine_kind(def_id).unwrap();
3794                            err.span_note(
3795                                sp,
3796                                {
    let _guard = ForceTrimmedGuard::new();
    ::alloc::__export::must_use({
            ::alloc::fmt::format(format_args!("required because it\'s used within this {0:#}",
                    kind))
        })
}with_forced_trimmed_paths!(format!(
3797                                    "required because it's used within this {kind:#}",
3798                                )),
3799                            );
3800                        }
3801                        ty::CoroutineWitness(..) => {
3802                            // Skip printing coroutine-witnesses, since we'll drill into
3803                            // the bad field in another derived obligation cause.
3804                        }
3805                        ty::Closure(def_id, _) | ty::CoroutineClosure(def_id, _) => {
3806                            err.span_note(
3807                                tcx.def_span(def_id),
3808                                "required because it's used within this closure",
3809                            );
3810                        }
3811                        ty::Str => {
3812                            err.note("`str` is considered to contain a `[u8]` slice for auto trait purposes");
3813                        }
3814                        _ => {
3815                            let msg = msg();
3816                            err.note(msg);
3817                        }
3818                    };
3819                }
3820
3821                obligated_types.push(ty);
3822
3823                let parent_predicate = parent_trait_ref;
3824                if !self.is_recursive_obligation(obligated_types, &data.parent_code) {
3825                    // #74711: avoid a stack overflow
3826                    ensure_sufficient_stack(|| {
3827                        self.note_obligation_cause_code(
3828                            body_id,
3829                            err,
3830                            parent_predicate,
3831                            param_env,
3832                            &data.parent_code,
3833                            obligated_types,
3834                            seen_requirements,
3835                        )
3836                    });
3837                } else {
3838                    ensure_sufficient_stack(|| {
3839                        self.note_obligation_cause_code(
3840                            body_id,
3841                            err,
3842                            parent_predicate,
3843                            param_env,
3844                            cause_code.peel_derives(),
3845                            obligated_types,
3846                            seen_requirements,
3847                        )
3848                    });
3849                }
3850            }
3851            ObligationCauseCode::ImplDerived(ref data) => {
3852                let mut parent_trait_pred =
3853                    self.resolve_vars_if_possible(data.derived.parent_trait_pred);
3854                let parent_def_id = parent_trait_pred.def_id();
3855                if tcx.is_diagnostic_item(sym::FromResidual, parent_def_id)
3856                    && !tcx.features().enabled(sym::try_trait_v2)
3857                {
3858                    // If `#![feature(try_trait_v2)]` is not enabled, then there's no point on
3859                    // talking about `FromResidual<Result<A, B>>`, as the end user has nothing they
3860                    // can do about it. As far as they are concerned, `?` is compiler magic.
3861                    return;
3862                }
3863                if tcx.is_diagnostic_item(sym::PinDerefMutHelper, parent_def_id) {
3864                    let parent_predicate =
3865                        self.resolve_vars_if_possible(data.derived.parent_trait_pred);
3866
3867                    // Skip PinDerefMutHelper in suggestions, but still show downstream suggestions.
3868                    ensure_sufficient_stack(|| {
3869                        self.note_obligation_cause_code(
3870                            body_id,
3871                            err,
3872                            parent_predicate,
3873                            param_env,
3874                            &data.derived.parent_code,
3875                            obligated_types,
3876                            seen_requirements,
3877                        )
3878                    });
3879                    return;
3880                }
3881                let self_ty_str =
3882                    tcx.short_string(parent_trait_pred.skip_binder().self_ty(), err.long_ty_path());
3883                let trait_name = tcx.short_string(
3884                    parent_trait_pred.print_modifiers_and_trait_path(),
3885                    err.long_ty_path(),
3886                );
3887                let msg = ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("required for `{0}` to implement `{1}`",
                self_ty_str, trait_name))
    })format!("required for `{self_ty_str}` to implement `{trait_name}`");
3888                let mut is_auto_trait = false;
3889                match tcx.hir_get_if_local(data.impl_or_alias_def_id) {
3890                    Some(Node::Item(hir::Item {
3891                        kind: hir::ItemKind::Trait(_, is_auto, _, _, ident, _, _, _),
3892                        ..
3893                    })) => {
3894                        // FIXME: we should do something else so that it works even on crate foreign
3895                        // auto traits.
3896                        is_auto_trait = #[allow(non_exhaustive_omitted_patterns)] match is_auto {
    hir::IsAuto::Yes => true,
    _ => false,
}matches!(is_auto, hir::IsAuto::Yes);
3897                        err.span_note(ident.span, msg);
3898                    }
3899                    Some(Node::Item(hir::Item {
3900                        kind: hir::ItemKind::Impl(hir::Impl { of_trait, self_ty, generics, .. }),
3901                        ..
3902                    })) => {
3903                        let mut spans = Vec::with_capacity(2);
3904                        if let Some(of_trait) = of_trait
3905                            && !of_trait.trait_ref.path.span.in_derive_expansion()
3906                        {
3907                            spans.push(of_trait.trait_ref.path.span);
3908                        }
3909                        spans.push(self_ty.span);
3910                        let mut spans: MultiSpan = spans.into();
3911                        let mut derived = false;
3912                        if #[allow(non_exhaustive_omitted_patterns)] match self_ty.span.ctxt().outer_expn_data().kind
    {
    ExpnKind::Macro(MacroKind::Derive, _) => true,
    _ => false,
}matches!(
3913                            self_ty.span.ctxt().outer_expn_data().kind,
3914                            ExpnKind::Macro(MacroKind::Derive, _)
3915                        ) || #[allow(non_exhaustive_omitted_patterns)] match of_trait.map(|t|
            t.trait_ref.path.span.ctxt().outer_expn_data().kind) {
    Some(ExpnKind::Macro(MacroKind::Derive, _)) => true,
    _ => false,
}matches!(
3916                            of_trait.map(|t| t.trait_ref.path.span.ctxt().outer_expn_data().kind),
3917                            Some(ExpnKind::Macro(MacroKind::Derive, _))
3918                        ) {
3919                            derived = true;
3920                            spans.push_span_label(
3921                                data.span,
3922                                if data.span.in_derive_expansion() {
3923                                    ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("type parameter would need to implement `{0}`",
                trait_name))
    })format!("type parameter would need to implement `{trait_name}`")
3924                                } else {
3925                                    ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("unsatisfied trait bound"))
    })format!("unsatisfied trait bound")
3926                                },
3927                            );
3928                        } else if !data.span.is_dummy() && !data.span.overlaps(self_ty.span) {
3929                            // `Sized` may be an explicit or implicit trait bound. If it is
3930                            // implicit, mention it as such.
3931                            if let Some(pred) = predicate.as_trait_clause()
3932                                && self.tcx.is_lang_item(pred.def_id(), LangItem::Sized)
3933                                && self
3934                                    .tcx
3935                                    .generics_of(data.impl_or_alias_def_id)
3936                                    .own_params
3937                                    .iter()
3938                                    .any(|param| self.tcx.def_span(param.def_id) == data.span)
3939                            {
3940                                spans.push_span_label(
3941                                    data.span,
3942                                    "unsatisfied trait bound implicitly introduced here",
3943                                );
3944                            } else {
3945                                spans.push_span_label(
3946                                    data.span,
3947                                    "unsatisfied trait bound introduced here",
3948                                );
3949                            }
3950                        }
3951                        err.span_note(spans, msg);
3952                        if derived && trait_name != "Copy" {
3953                            err.help(::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("consider manually implementing `{0}` to avoid undesired bounds",
                trait_name))
    })format!(
3954                                "consider manually implementing `{trait_name}` to avoid undesired \
3955                                 bounds",
3956                            ));
3957                        }
3958                        point_at_assoc_type_restriction(
3959                            tcx,
3960                            err,
3961                            &self_ty_str,
3962                            &trait_name,
3963                            predicate,
3964                            &generics,
3965                            &data,
3966                        );
3967                    }
3968                    _ => {
3969                        err.note(msg);
3970                    }
3971                };
3972
3973                let mut parent_predicate = parent_trait_pred;
3974                let mut data = &data.derived;
3975                let mut count = 0;
3976                seen_requirements.insert(parent_def_id);
3977                if is_auto_trait {
3978                    // We don't want to point at the ADT saying "required because it appears within
3979                    // the type `X`", like we would otherwise do in test `supertrait-auto-trait.rs`.
3980                    while let ObligationCauseCode::BuiltinDerived(derived) = &*data.parent_code {
3981                        let child_trait_ref =
3982                            self.resolve_vars_if_possible(derived.parent_trait_pred);
3983                        let child_def_id = child_trait_ref.def_id();
3984                        if seen_requirements.insert(child_def_id) {
3985                            break;
3986                        }
3987                        data = derived;
3988                        parent_predicate = child_trait_ref.upcast(tcx);
3989                        parent_trait_pred = child_trait_ref;
3990                    }
3991                }
3992                while let ObligationCauseCode::ImplDerived(child) = &*data.parent_code {
3993                    // Skip redundant recursive obligation notes. See `ui/issue-20413.rs`.
3994                    let child_trait_pred =
3995                        self.resolve_vars_if_possible(child.derived.parent_trait_pred);
3996                    let child_def_id = child_trait_pred.def_id();
3997                    if seen_requirements.insert(child_def_id) {
3998                        break;
3999                    }
4000                    count += 1;
4001                    data = &child.derived;
4002                    parent_predicate = child_trait_pred.upcast(tcx);
4003                    parent_trait_pred = child_trait_pred;
4004                }
4005                if count > 0 {
4006                    err.note(::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{0} redundant requirement{1} hidden",
                count, if count == 1 { "" } else { "s" }))
    })format!(
4007                        "{} redundant requirement{} hidden",
4008                        count,
4009                        pluralize!(count)
4010                    ));
4011                    let self_ty = tcx.short_string(
4012                        parent_trait_pred.skip_binder().self_ty(),
4013                        err.long_ty_path(),
4014                    );
4015                    let trait_path = tcx.short_string(
4016                        parent_trait_pred.print_modifiers_and_trait_path(),
4017                        err.long_ty_path(),
4018                    );
4019                    err.note(::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("required for `{0}` to implement `{1}`",
                self_ty, trait_path))
    })format!("required for `{self_ty}` to implement `{trait_path}`"));
4020                }
4021                // #74711: avoid a stack overflow
4022                ensure_sufficient_stack(|| {
4023                    self.note_obligation_cause_code(
4024                        body_id,
4025                        err,
4026                        parent_predicate,
4027                        param_env,
4028                        &data.parent_code,
4029                        obligated_types,
4030                        seen_requirements,
4031                    )
4032                });
4033            }
4034            ObligationCauseCode::ImplDerivedHost(ref data) => {
4035                let self_ty = tcx.short_string(
4036                    self.resolve_vars_if_possible(data.derived.parent_host_pred.self_ty()),
4037                    err.long_ty_path(),
4038                );
4039                let trait_path = tcx.short_string(
4040                    data.derived
4041                        .parent_host_pred
4042                        .map_bound(|pred| pred.trait_ref)
4043                        .print_only_trait_path(),
4044                    err.long_ty_path(),
4045                );
4046                let msg = ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("required for `{1}` to implement `{0} {2}`",
                data.derived.parent_host_pred.skip_binder().constness,
                self_ty, trait_path))
    })format!(
4047                    "required for `{self_ty}` to implement `{} {trait_path}`",
4048                    data.derived.parent_host_pred.skip_binder().constness,
4049                );
4050                match tcx.hir_get_if_local(data.impl_def_id) {
4051                    Some(Node::Item(hir::Item {
4052                        kind: hir::ItemKind::Impl(hir::Impl { of_trait, self_ty, .. }),
4053                        ..
4054                    })) => {
4055                        let mut spans = ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [self_ty.span]))vec![self_ty.span];
4056                        spans.extend(of_trait.map(|t| t.trait_ref.path.span));
4057                        let mut spans: MultiSpan = spans.into();
4058                        spans.push_span_label(data.span, "unsatisfied trait bound introduced here");
4059                        err.span_note(spans, msg);
4060                    }
4061                    _ => {
4062                        err.note(msg);
4063                    }
4064                }
4065                ensure_sufficient_stack(|| {
4066                    self.note_obligation_cause_code(
4067                        body_id,
4068                        err,
4069                        data.derived.parent_host_pred,
4070                        param_env,
4071                        &data.derived.parent_code,
4072                        obligated_types,
4073                        seen_requirements,
4074                    )
4075                });
4076            }
4077            ObligationCauseCode::BuiltinDerivedHost(ref data) => {
4078                ensure_sufficient_stack(|| {
4079                    self.note_obligation_cause_code(
4080                        body_id,
4081                        err,
4082                        data.parent_host_pred,
4083                        param_env,
4084                        &data.parent_code,
4085                        obligated_types,
4086                        seen_requirements,
4087                    )
4088                });
4089            }
4090            ObligationCauseCode::WellFormedDerived(ref data) => {
4091                let parent_trait_ref = self.resolve_vars_if_possible(data.parent_trait_pred);
4092                let parent_predicate = parent_trait_ref;
4093                // #74711: avoid a stack overflow
4094                ensure_sufficient_stack(|| {
4095                    self.note_obligation_cause_code(
4096                        body_id,
4097                        err,
4098                        parent_predicate,
4099                        param_env,
4100                        &data.parent_code,
4101                        obligated_types,
4102                        seen_requirements,
4103                    )
4104                });
4105            }
4106            ObligationCauseCode::TypeAlias(ref nested, span, def_id) => {
4107                // #74711: avoid a stack overflow
4108                ensure_sufficient_stack(|| {
4109                    self.note_obligation_cause_code(
4110                        body_id,
4111                        err,
4112                        predicate,
4113                        param_env,
4114                        nested,
4115                        obligated_types,
4116                        seen_requirements,
4117                    )
4118                });
4119                let mut multispan = MultiSpan::from(span);
4120                multispan.push_span_label(span, "required by this bound");
4121                err.span_note(
4122                    multispan,
4123                    ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("required by a bound on the type alias `{0}`",
                tcx.item_name(def_id)))
    })format!("required by a bound on the type alias `{}`", tcx.item_name(def_id)),
4124                );
4125            }
4126            ObligationCauseCode::FunctionArg {
4127                arg_hir_id, call_hir_id, ref parent_code, ..
4128            } => {
4129                self.note_function_argument_obligation(
4130                    body_id,
4131                    err,
4132                    arg_hir_id,
4133                    parent_code,
4134                    param_env,
4135                    predicate,
4136                    call_hir_id,
4137                );
4138                ensure_sufficient_stack(|| {
4139                    self.note_obligation_cause_code(
4140                        body_id,
4141                        err,
4142                        predicate,
4143                        param_env,
4144                        parent_code,
4145                        obligated_types,
4146                        seen_requirements,
4147                    )
4148                });
4149            }
4150            // Suppress `compare_type_predicate_entailment` errors for RPITITs, since they
4151            // should be implied by the parent method.
4152            ObligationCauseCode::CompareImplItem { trait_item_def_id, .. }
4153                if tcx.is_impl_trait_in_trait(trait_item_def_id) => {}
4154            ObligationCauseCode::CompareImplItem { trait_item_def_id, kind, .. } => {
4155                let item_name = tcx.item_name(trait_item_def_id);
4156                let msg = ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("the requirement `{0}` appears on the `impl`\'s {1} `{2}` but not on the corresponding trait\'s {1}",
                predicate, kind, item_name))
    })format!(
4157                    "the requirement `{predicate}` appears on the `impl`'s {kind} \
4158                     `{item_name}` but not on the corresponding trait's {kind}",
4159                );
4160                let sp = tcx
4161                    .opt_item_ident(trait_item_def_id)
4162                    .map(|i| i.span)
4163                    .unwrap_or_else(|| tcx.def_span(trait_item_def_id));
4164                let mut assoc_span: MultiSpan = sp.into();
4165                assoc_span.push_span_label(
4166                    sp,
4167                    ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("this trait\'s {0} doesn\'t have the requirement `{1}`",
                kind, predicate))
    })format!("this trait's {kind} doesn't have the requirement `{predicate}`"),
4168                );
4169                if let Some(ident) = tcx
4170                    .opt_associated_item(trait_item_def_id)
4171                    .and_then(|i| tcx.opt_item_ident(i.container_id(tcx)))
4172                {
4173                    assoc_span.push_span_label(ident.span, "in this trait");
4174                }
4175                err.span_note(assoc_span, msg);
4176            }
4177            ObligationCauseCode::TrivialBound => {
4178                tcx.disabled_nightly_features(err, [(String::new(), sym::trivial_bounds)]);
4179            }
4180            ObligationCauseCode::OpaqueReturnType(expr_info) => {
4181                let (expr_ty, expr) = if let Some((expr_ty, hir_id)) = expr_info {
4182                    let expr_ty = tcx.short_string(expr_ty, err.long_ty_path());
4183                    let expr = tcx.hir_expect_expr(hir_id);
4184                    (expr_ty, expr)
4185                } else if let Some(body_id) = tcx.hir_node_by_def_id(body_id).body_id()
4186                    && let body = tcx.hir_body(body_id)
4187                    && let hir::ExprKind::Block(block, _) = body.value.kind
4188                    && let Some(expr) = block.expr
4189                    && let Some(expr_ty) = self
4190                        .typeck_results
4191                        .as_ref()
4192                        .and_then(|typeck| typeck.node_type_opt(expr.hir_id))
4193                    && let Some(pred) = predicate.as_clause()
4194                    && let ty::ClauseKind::Trait(pred) = pred.kind().skip_binder()
4195                    && self.can_eq(param_env, pred.self_ty(), expr_ty)
4196                {
4197                    let expr_ty = tcx.short_string(expr_ty, err.long_ty_path());
4198                    (expr_ty, expr)
4199                } else {
4200                    return;
4201                };
4202                err.span_label(
4203                    expr.span,
4204                    {
    let _guard = ForceTrimmedGuard::new();
    ::alloc::__export::must_use({
            ::alloc::fmt::format(format_args!("return type was inferred to be `{0}` here",
                    expr_ty))
        })
}with_forced_trimmed_paths!(format!(
4205                        "return type was inferred to be `{expr_ty}` here",
4206                    )),
4207                );
4208                suggest_remove_deref(err, &expr);
4209            }
4210            ObligationCauseCode::UnsizedNonPlaceExpr(span) => {
4211                err.span_note(
4212                    span,
4213                    "unsized values must be place expressions and cannot be put in temporaries",
4214                );
4215            }
4216            ObligationCauseCode::CompareEii { .. } => {
4217                {
    ::core::panicking::panic_fmt(format_args!("trait bounds on EII not yet supported "));
}panic!("trait bounds on EII not yet supported ")
4218            }
4219        }
4220    }
4221
4222    #[allow(clippy :: suspicious_else_formatting)]
{
    let __tracing_attr_span;
    let __tracing_attr_guard;
    if ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() ||
            { false } {
        __tracing_attr_span =
            {
                use ::tracing::__macro_support::Callsite as _;
                static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                    {
                        static META: ::tracing::Metadata<'static> =
                            {
                                ::tracing_core::metadata::Metadata::new("suggest_await_before_try",
                                    "rustc_trait_selection::error_reporting::traits::suggestions",
                                    ::tracing::Level::DEBUG,
                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs"),
                                    ::tracing_core::__macro_support::Option::Some(4222u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_trait_selection::error_reporting::traits::suggestions"),
                                    ::tracing_core::field::FieldSet::new(&["obligation",
                                                    "trait_pred", "span", "trait_pred.self_ty"],
                                        ::tracing_core::callsite::Identifier(&__CALLSITE)),
                                    ::tracing::metadata::Kind::SPAN)
                            };
                        ::tracing::callsite::DefaultCallsite::new(&META)
                    };
                let mut interest = ::tracing::subscriber::Interest::never();
                if ::tracing::Level::DEBUG <=
                                    ::tracing::level_filters::STATIC_MAX_LEVEL &&
                                ::tracing::Level::DEBUG <=
                                    ::tracing::level_filters::LevelFilter::current() &&
                            { interest = __CALLSITE.interest(); !interest.is_never() }
                        &&
                        ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                            interest) {
                    let meta = __CALLSITE.metadata();
                    ::tracing::Span::new(meta,
                        &{
                                #[allow(unused_imports)]
                                use ::tracing::field::{debug, display, Value};
                                let mut iter = meta.fields().iter();
                                meta.fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                    ::tracing::__macro_support::Option::Some(&::tracing::field::debug(&obligation)
                                                            as &dyn Value)),
                                                (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                    ::tracing::__macro_support::Option::Some(&::tracing::field::debug(&trait_pred)
                                                            as &dyn Value)),
                                                (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                    ::tracing::__macro_support::Option::Some(&::tracing::field::debug(&span)
                                                            as &dyn Value)),
                                                (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                    ::tracing::__macro_support::Option::Some(&debug(&trait_pred.self_ty())
                                                            as &dyn Value))])
                            })
                } else {
                    let span =
                        ::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
                    {};
                    span
                }
            };
        __tracing_attr_guard = __tracing_attr_span.enter();
    }

    #[warn(clippy :: suspicious_else_formatting)]
    {

        #[allow(unknown_lints, unreachable_code, clippy ::
        diverging_sub_expression, clippy :: empty_loop, clippy ::
        let_unit_value, clippy :: let_with_type_underscore, clippy ::
        needless_return, clippy :: unreachable)]
        if false {
            let __tracing_attr_fake_return: () = loop {};
            return __tracing_attr_fake_return;
        }
        {
            let future_trait =
                self.tcx.require_lang_item(LangItem::Future, span);
            let self_ty = self.resolve_vars_if_possible(trait_pred.self_ty());
            let impls_future =
                self.type_implements_trait(future_trait,
                    [self.tcx.instantiate_bound_regions_with_erased(self_ty)],
                    obligation.param_env);
            if !impls_future.must_apply_modulo_regions() { return; }
            let item_def_id =
                self.tcx.associated_item_def_ids(future_trait)[0];
            let projection_ty =
                trait_pred.map_bound(|trait_pred|
                        {
                            Ty::new_projection(self.tcx, item_def_id,
                                [trait_pred.self_ty()])
                        });
            let InferOk { value: projection_ty, .. } =
                self.at(&obligation.cause,
                        obligation.param_env).normalize(projection_ty);
            {
                use ::tracing::__macro_support::Callsite as _;
                static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                    {
                        static META: ::tracing::Metadata<'static> =
                            {
                                ::tracing_core::metadata::Metadata::new("event compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs:4257",
                                    "rustc_trait_selection::error_reporting::traits::suggestions",
                                    ::tracing::Level::DEBUG,
                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs"),
                                    ::tracing_core::__macro_support::Option::Some(4257u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_trait_selection::error_reporting::traits::suggestions"),
                                    ::tracing_core::field::FieldSet::new(&["normalized_projection_type"],
                                        ::tracing_core::callsite::Identifier(&__CALLSITE)),
                                    ::tracing::metadata::Kind::EVENT)
                            };
                        ::tracing::callsite::DefaultCallsite::new(&META)
                    };
                let enabled =
                    ::tracing::Level::DEBUG <=
                                ::tracing::level_filters::STATIC_MAX_LEVEL &&
                            ::tracing::Level::DEBUG <=
                                ::tracing::level_filters::LevelFilter::current() &&
                        {
                            let interest = __CALLSITE.interest();
                            !interest.is_never() &&
                                ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                                    interest)
                        };
                if enabled {
                    (|value_set: ::tracing::field::ValueSet|
                                {
                                    let meta = __CALLSITE.metadata();
                                    ::tracing::Event::dispatch(meta, &value_set);
                                    ;
                                })({
                            #[allow(unused_imports)]
                            use ::tracing::field::{debug, display, Value};
                            let mut iter = __CALLSITE.metadata().fields().iter();
                            __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                ::tracing::__macro_support::Option::Some(&debug(&self.resolve_vars_if_possible(projection_ty))
                                                        as &dyn Value))])
                        });
                } else { ; }
            };
            let try_obligation =
                self.mk_trait_obligation_with_new_self_ty(obligation.param_env,
                    trait_pred.map_bound(|trait_pred|
                            (trait_pred, projection_ty.skip_binder())));
            {
                use ::tracing::__macro_support::Callsite as _;
                static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                    {
                        static META: ::tracing::Metadata<'static> =
                            {
                                ::tracing_core::metadata::Metadata::new("event compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs:4264",
                                    "rustc_trait_selection::error_reporting::traits::suggestions",
                                    ::tracing::Level::DEBUG,
                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs"),
                                    ::tracing_core::__macro_support::Option::Some(4264u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_trait_selection::error_reporting::traits::suggestions"),
                                    ::tracing_core::field::FieldSet::new(&["try_trait_obligation"],
                                        ::tracing_core::callsite::Identifier(&__CALLSITE)),
                                    ::tracing::metadata::Kind::EVENT)
                            };
                        ::tracing::callsite::DefaultCallsite::new(&META)
                    };
                let enabled =
                    ::tracing::Level::DEBUG <=
                                ::tracing::level_filters::STATIC_MAX_LEVEL &&
                            ::tracing::Level::DEBUG <=
                                ::tracing::level_filters::LevelFilter::current() &&
                        {
                            let interest = __CALLSITE.interest();
                            !interest.is_never() &&
                                ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                                    interest)
                        };
                if enabled {
                    (|value_set: ::tracing::field::ValueSet|
                                {
                                    let meta = __CALLSITE.metadata();
                                    ::tracing::Event::dispatch(meta, &value_set);
                                    ;
                                })({
                            #[allow(unused_imports)]
                            use ::tracing::field::{debug, display, Value};
                            let mut iter = __CALLSITE.metadata().fields().iter();
                            __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                ::tracing::__macro_support::Option::Some(&debug(&try_obligation)
                                                        as &dyn Value))])
                        });
                } else { ; }
            };
            if self.predicate_may_hold(&try_obligation) &&
                        let Ok(snippet) =
                            self.tcx.sess.source_map().span_to_snippet(span) &&
                    snippet.ends_with('?') {
                match self.tcx.coroutine_kind(obligation.cause.body_id) {
                    Some(hir::CoroutineKind::Desugared(hir::CoroutineDesugaring::Async,
                        _)) => {
                        err.span_suggestion_verbose(span.with_hi(span.hi() -
                                        BytePos(1)).shrink_to_hi(),
                            "consider `await`ing on the `Future`", ".await",
                            Applicability::MaybeIncorrect);
                    }
                    _ => {
                        let mut span: MultiSpan =
                            span.with_lo(span.hi() - BytePos(1)).into();
                        span.push_span_label(self.tcx.def_span(obligation.cause.body_id),
                            "this is not `async`");
                        err.span_note(span,
                            "this implements `Future` and its output type supports \
                        `?`, but the future cannot be awaited in a synchronous function");
                    }
                }
            }
        }
    }
}#[instrument(
4223        level = "debug", skip(self, err), fields(trait_pred.self_ty = ?trait_pred.self_ty())
4224    )]
4225    pub(super) fn suggest_await_before_try(
4226        &self,
4227        err: &mut Diag<'_>,
4228        obligation: &PredicateObligation<'tcx>,
4229        trait_pred: ty::PolyTraitPredicate<'tcx>,
4230        span: Span,
4231    ) {
4232        let future_trait = self.tcx.require_lang_item(LangItem::Future, span);
4233
4234        let self_ty = self.resolve_vars_if_possible(trait_pred.self_ty());
4235        let impls_future = self.type_implements_trait(
4236            future_trait,
4237            [self.tcx.instantiate_bound_regions_with_erased(self_ty)],
4238            obligation.param_env,
4239        );
4240        if !impls_future.must_apply_modulo_regions() {
4241            return;
4242        }
4243
4244        let item_def_id = self.tcx.associated_item_def_ids(future_trait)[0];
4245        // `<T as Future>::Output`
4246        let projection_ty = trait_pred.map_bound(|trait_pred| {
4247            Ty::new_projection(
4248                self.tcx,
4249                item_def_id,
4250                // Future::Output has no args
4251                [trait_pred.self_ty()],
4252            )
4253        });
4254        let InferOk { value: projection_ty, .. } =
4255            self.at(&obligation.cause, obligation.param_env).normalize(projection_ty);
4256
4257        debug!(
4258            normalized_projection_type = ?self.resolve_vars_if_possible(projection_ty)
4259        );
4260        let try_obligation = self.mk_trait_obligation_with_new_self_ty(
4261            obligation.param_env,
4262            trait_pred.map_bound(|trait_pred| (trait_pred, projection_ty.skip_binder())),
4263        );
4264        debug!(try_trait_obligation = ?try_obligation);
4265        if self.predicate_may_hold(&try_obligation)
4266            && let Ok(snippet) = self.tcx.sess.source_map().span_to_snippet(span)
4267            && snippet.ends_with('?')
4268        {
4269            match self.tcx.coroutine_kind(obligation.cause.body_id) {
4270                Some(hir::CoroutineKind::Desugared(hir::CoroutineDesugaring::Async, _)) => {
4271                    err.span_suggestion_verbose(
4272                        span.with_hi(span.hi() - BytePos(1)).shrink_to_hi(),
4273                        "consider `await`ing on the `Future`",
4274                        ".await",
4275                        Applicability::MaybeIncorrect,
4276                    );
4277                }
4278                _ => {
4279                    let mut span: MultiSpan = span.with_lo(span.hi() - BytePos(1)).into();
4280                    span.push_span_label(
4281                        self.tcx.def_span(obligation.cause.body_id),
4282                        "this is not `async`",
4283                    );
4284                    err.span_note(
4285                        span,
4286                        "this implements `Future` and its output type supports \
4287                        `?`, but the future cannot be awaited in a synchronous function",
4288                    );
4289                }
4290            }
4291        }
4292    }
4293
4294    pub(super) fn suggest_floating_point_literal(
4295        &self,
4296        obligation: &PredicateObligation<'tcx>,
4297        err: &mut Diag<'_>,
4298        trait_pred: ty::PolyTraitPredicate<'tcx>,
4299    ) {
4300        let rhs_span = match obligation.cause.code() {
4301            ObligationCauseCode::BinOp { rhs_span, rhs_is_lit, .. } if *rhs_is_lit => rhs_span,
4302            _ => return,
4303        };
4304        if let ty::Float(_) = trait_pred.skip_binder().self_ty().kind()
4305            && let ty::Infer(InferTy::IntVar(_)) =
4306                trait_pred.skip_binder().trait_ref.args.type_at(1).kind()
4307        {
4308            err.span_suggestion_verbose(
4309                rhs_span.shrink_to_hi(),
4310                "consider using a floating-point literal by writing it with `.0`",
4311                ".0",
4312                Applicability::MaybeIncorrect,
4313            );
4314        }
4315    }
4316
4317    pub fn can_suggest_derive(
4318        &self,
4319        obligation: &PredicateObligation<'tcx>,
4320        trait_pred: ty::PolyTraitPredicate<'tcx>,
4321    ) -> bool {
4322        if trait_pred.polarity() == ty::PredicatePolarity::Negative {
4323            return false;
4324        }
4325        let Some(diagnostic_name) = self.tcx.get_diagnostic_name(trait_pred.def_id()) else {
4326            return false;
4327        };
4328        let (adt, args) = match trait_pred.skip_binder().self_ty().kind() {
4329            ty::Adt(adt, args) if adt.did().is_local() => (adt, args),
4330            _ => return false,
4331        };
4332        let is_derivable_trait = match diagnostic_name {
4333            sym::Copy | sym::Clone => true,
4334            _ if adt.is_union() => false,
4335            sym::PartialEq | sym::PartialOrd => {
4336                let rhs_ty = trait_pred.skip_binder().trait_ref.args.type_at(1);
4337                trait_pred.skip_binder().self_ty() == rhs_ty
4338            }
4339            sym::Eq | sym::Ord | sym::Hash | sym::Debug | sym::Default => true,
4340            _ => false,
4341        };
4342        is_derivable_trait &&
4343            // Ensure all fields impl the trait.
4344            adt.all_fields().all(|field| {
4345                let field_ty = ty::GenericArg::from(field.ty(self.tcx, args));
4346                let trait_args = match diagnostic_name {
4347                    sym::PartialEq | sym::PartialOrd => {
4348                        Some(field_ty)
4349                    }
4350                    _ => None,
4351                };
4352                let trait_pred = trait_pred.map_bound_ref(|tr| ty::TraitPredicate {
4353                    trait_ref: ty::TraitRef::new(self.tcx,
4354                        trait_pred.def_id(),
4355                        [field_ty].into_iter().chain(trait_args),
4356                    ),
4357                    ..*tr
4358                });
4359                let field_obl = Obligation::new(
4360                    self.tcx,
4361                    obligation.cause.clone(),
4362                    obligation.param_env,
4363                    trait_pred,
4364                );
4365                self.predicate_must_hold_modulo_regions(&field_obl)
4366            })
4367    }
4368
4369    pub fn suggest_derive(
4370        &self,
4371        obligation: &PredicateObligation<'tcx>,
4372        err: &mut Diag<'_>,
4373        trait_pred: ty::PolyTraitPredicate<'tcx>,
4374    ) {
4375        let Some(diagnostic_name) = self.tcx.get_diagnostic_name(trait_pred.def_id()) else {
4376            return;
4377        };
4378        let adt = match trait_pred.skip_binder().self_ty().kind() {
4379            ty::Adt(adt, _) if adt.did().is_local() => adt,
4380            _ => return,
4381        };
4382        if self.can_suggest_derive(obligation, trait_pred) {
4383            err.span_suggestion_verbose(
4384                self.tcx.def_span(adt.did()).shrink_to_lo(),
4385                ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("consider annotating `{0}` with `#[derive({1})]`",
                trait_pred.skip_binder().self_ty(), diagnostic_name))
    })format!(
4386                    "consider annotating `{}` with `#[derive({})]`",
4387                    trait_pred.skip_binder().self_ty(),
4388                    diagnostic_name,
4389                ),
4390                // FIXME(const_trait_impl) derive_const as suggestion?
4391                ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("#[derive({0})]\n",
                diagnostic_name))
    })format!("#[derive({diagnostic_name})]\n"),
4392                Applicability::MaybeIncorrect,
4393            );
4394        }
4395    }
4396
4397    pub(super) fn suggest_dereferencing_index(
4398        &self,
4399        obligation: &PredicateObligation<'tcx>,
4400        err: &mut Diag<'_>,
4401        trait_pred: ty::PolyTraitPredicate<'tcx>,
4402    ) {
4403        if let ObligationCauseCode::ImplDerived(_) = obligation.cause.code()
4404            && self
4405                .tcx
4406                .is_diagnostic_item(sym::SliceIndex, trait_pred.skip_binder().trait_ref.def_id)
4407            && let ty::Slice(_) = trait_pred.skip_binder().trait_ref.args.type_at(1).kind()
4408            && let ty::Ref(_, inner_ty, _) = trait_pred.skip_binder().self_ty().kind()
4409            && let ty::Uint(ty::UintTy::Usize) = inner_ty.kind()
4410        {
4411            err.span_suggestion_verbose(
4412                obligation.cause.span.shrink_to_lo(),
4413                "dereference this index",
4414                '*',
4415                Applicability::MachineApplicable,
4416            );
4417        }
4418    }
4419
4420    fn note_function_argument_obligation<G: EmissionGuarantee>(
4421        &self,
4422        body_id: LocalDefId,
4423        err: &mut Diag<'_, G>,
4424        arg_hir_id: HirId,
4425        parent_code: &ObligationCauseCode<'tcx>,
4426        param_env: ty::ParamEnv<'tcx>,
4427        failed_pred: ty::Predicate<'tcx>,
4428        call_hir_id: HirId,
4429    ) {
4430        let tcx = self.tcx;
4431        if let Node::Expr(expr) = tcx.hir_node(arg_hir_id)
4432            && let Some(typeck_results) = &self.typeck_results
4433        {
4434            if let hir::Expr { kind: hir::ExprKind::MethodCall(_, rcvr, _, _), .. } = expr
4435                && let Some(ty) = typeck_results.node_type_opt(rcvr.hir_id)
4436                && let Some(failed_pred) = failed_pred.as_trait_clause()
4437                && let pred = failed_pred.map_bound(|pred| pred.with_replaced_self_ty(tcx, ty))
4438                && self.predicate_must_hold_modulo_regions(&Obligation::misc(
4439                    tcx, expr.span, body_id, param_env, pred,
4440                ))
4441                && expr.span.hi() != rcvr.span.hi()
4442            {
4443                let should_sugg = match tcx.hir_node(call_hir_id) {
4444                    Node::Expr(hir::Expr {
4445                        kind: hir::ExprKind::MethodCall(_, call_receiver, _, _),
4446                        ..
4447                    }) if let Some((DefKind::AssocFn, did)) =
4448                        typeck_results.type_dependent_def(call_hir_id)
4449                        && call_receiver.hir_id == arg_hir_id =>
4450                    {
4451                        // Avoid suggesting removing a method call if the argument is the receiver of the parent call and
4452                        // removing the receiver would make the method inaccessible. i.e. `x.a().b()`, suggesting removing
4453                        // `.a()` could change the type and make `.b()` unavailable.
4454                        if tcx.inherent_impl_of_assoc(did).is_some() {
4455                            // if we're calling an inherent impl method, just try to make sure that the receiver type stays the same.
4456                            Some(ty) == typeck_results.node_type_opt(arg_hir_id)
4457                        } else {
4458                            // we're calling a trait method, so we just check removing the method call still satisfies the trait.
4459                            let trait_id = tcx
4460                                .trait_of_assoc(did)
4461                                .unwrap_or_else(|| tcx.impl_trait_id(tcx.parent(did)));
4462                            let args = typeck_results.node_args(call_hir_id);
4463                            let tr = ty::TraitRef::from_assoc(tcx, trait_id, args)
4464                                .with_replaced_self_ty(tcx, ty);
4465                            self.type_implements_trait(tr.def_id, tr.args, param_env)
4466                                .must_apply_modulo_regions()
4467                        }
4468                    }
4469                    _ => true,
4470                };
4471
4472                if should_sugg {
4473                    err.span_suggestion_verbose(
4474                        expr.span.with_lo(rcvr.span.hi()),
4475                        ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("consider removing this method call, as the receiver has type `{0}` and `{1}` trivially holds",
                ty, pred))
    })format!(
4476                            "consider removing this method call, as the receiver has type `{ty}` and \
4477                            `{pred}` trivially holds",
4478                        ),
4479                        "",
4480                        Applicability::MaybeIncorrect,
4481                    );
4482                }
4483            }
4484            if let hir::Expr { kind: hir::ExprKind::Block(block, _), .. } = expr {
4485                let inner_expr = expr.peel_blocks();
4486                let ty = typeck_results
4487                    .expr_ty_adjusted_opt(inner_expr)
4488                    .unwrap_or(Ty::new_misc_error(tcx));
4489                let span = inner_expr.span;
4490                if Some(span) != err.span.primary_span()
4491                    && !span.in_external_macro(tcx.sess.source_map())
4492                {
4493                    err.span_label(
4494                        span,
4495                        if ty.references_error() {
4496                            String::new()
4497                        } else {
4498                            let ty = { let _guard = ForceTrimmedGuard::new(); self.ty_to_string(ty) }with_forced_trimmed_paths!(self.ty_to_string(ty));
4499                            ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("this tail expression is of type `{0}`",
                ty))
    })format!("this tail expression is of type `{ty}`")
4500                        },
4501                    );
4502                    if let ty::PredicateKind::Clause(clause) = failed_pred.kind().skip_binder()
4503                        && let ty::ClauseKind::Trait(pred) = clause
4504                        && tcx.fn_trait_kind_from_def_id(pred.def_id()).is_some()
4505                    {
4506                        if let [stmt, ..] = block.stmts
4507                            && let hir::StmtKind::Semi(value) = stmt.kind
4508                            && let hir::ExprKind::Closure(hir::Closure {
4509                                body, fn_decl_span, ..
4510                            }) = value.kind
4511                            && let body = tcx.hir_body(*body)
4512                            && !#[allow(non_exhaustive_omitted_patterns)] match body.value.kind {
    hir::ExprKind::Block(..) => true,
    _ => false,
}matches!(body.value.kind, hir::ExprKind::Block(..))
4513                        {
4514                            // Check if the failed predicate was an expectation of a closure type
4515                            // and if there might have been a `{ |args|` typo instead of `|args| {`.
4516                            err.multipart_suggestion(
4517                                "you might have meant to open the closure body instead of placing \
4518                                 a closure within a block",
4519                                ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [(expr.span.with_hi(value.span.lo()), String::new()),
                (fn_decl_span.shrink_to_hi(), " {".to_string())]))vec![
4520                                    (expr.span.with_hi(value.span.lo()), String::new()),
4521                                    (fn_decl_span.shrink_to_hi(), " {".to_string()),
4522                                ],
4523                                Applicability::MaybeIncorrect,
4524                            );
4525                        } else {
4526                            // Maybe the bare block was meant to be a closure.
4527                            err.span_suggestion_verbose(
4528                                expr.span.shrink_to_lo(),
4529                                "you might have meant to create the closure instead of a block",
4530                                ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("|{0}| ",
                (0..pred.trait_ref.args.len() -
                                        1).map(|_| "_").collect::<Vec<_>>().join(", ")))
    })format!(
4531                                    "|{}| ",
4532                                    (0..pred.trait_ref.args.len() - 1)
4533                                        .map(|_| "_")
4534                                        .collect::<Vec<_>>()
4535                                        .join(", ")
4536                                ),
4537                                Applicability::MaybeIncorrect,
4538                            );
4539                        }
4540                    }
4541                }
4542            }
4543
4544            // FIXME: visit the ty to see if there's any closure involved, and if there is,
4545            // check whether its evaluated return type is the same as the one corresponding
4546            // to an associated type (as seen from `trait_pred`) in the predicate. Like in
4547            // trait_pred `S: Sum<<Self as Iterator>::Item>` and predicate `i32: Sum<&()>`
4548            let mut type_diffs = ::alloc::vec::Vec::new()vec![];
4549            if let ObligationCauseCode::WhereClauseInExpr(def_id, _, _, idx) = *parent_code
4550                && let Some(node_args) = typeck_results.node_args_opt(call_hir_id)
4551                && let where_clauses =
4552                    self.tcx.predicates_of(def_id).instantiate(self.tcx, node_args)
4553                && let Some(where_pred) = where_clauses.predicates.get(idx)
4554            {
4555                if let Some(where_pred) = where_pred.as_trait_clause()
4556                    && let Some(failed_pred) = failed_pred.as_trait_clause()
4557                    && where_pred.def_id() == failed_pred.def_id()
4558                {
4559                    self.enter_forall(where_pred, |where_pred| {
4560                        let failed_pred = self.instantiate_binder_with_fresh_vars(
4561                            expr.span,
4562                            BoundRegionConversionTime::FnCall,
4563                            failed_pred,
4564                        );
4565
4566                        let zipped =
4567                            iter::zip(where_pred.trait_ref.args, failed_pred.trait_ref.args);
4568                        for (expected, actual) in zipped {
4569                            self.probe(|_| {
4570                                match self
4571                                    .at(&ObligationCause::misc(expr.span, body_id), param_env)
4572                                    // Doesn't actually matter if we define opaque types here, this is just used for
4573                                    // diagnostics, and the result is never kept around.
4574                                    .eq(DefineOpaqueTypes::Yes, expected, actual)
4575                                {
4576                                    Ok(_) => (), // We ignore nested obligations here for now.
4577                                    Err(err) => type_diffs.push(err),
4578                                }
4579                            })
4580                        }
4581                    })
4582                } else if let Some(where_pred) = where_pred.as_projection_clause()
4583                    && let Some(failed_pred) = failed_pred.as_projection_clause()
4584                    && let Some(found) = failed_pred.skip_binder().term.as_type()
4585                {
4586                    type_diffs = ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [TypeError::Sorts(ty::error::ExpectedFound {
                        expected: where_pred.skip_binder().projection_term.expect_ty(self.tcx).to_ty(self.tcx),
                        found,
                    })]))vec![TypeError::Sorts(ty::error::ExpectedFound {
4587                        expected: where_pred
4588                            .skip_binder()
4589                            .projection_term
4590                            .expect_ty(self.tcx)
4591                            .to_ty(self.tcx),
4592                        found,
4593                    })];
4594                }
4595            }
4596            if let hir::ExprKind::Path(hir::QPath::Resolved(None, path)) = expr.kind
4597                && let hir::Path { res: Res::Local(hir_id), .. } = path
4598                && let hir::Node::Pat(binding) = self.tcx.hir_node(*hir_id)
4599                && let hir::Node::LetStmt(local) = self.tcx.parent_hir_node(binding.hir_id)
4600                && let Some(binding_expr) = local.init
4601            {
4602                // If the expression we're calling on is a binding, we want to point at the
4603                // `let` when talking about the type. Otherwise we'll point at every part
4604                // of the method chain with the type.
4605                self.point_at_chain(binding_expr, typeck_results, type_diffs, param_env, err);
4606            } else {
4607                self.point_at_chain(expr, typeck_results, type_diffs, param_env, err);
4608            }
4609        }
4610        let call_node = tcx.hir_node(call_hir_id);
4611        if let Node::Expr(hir::Expr { kind: hir::ExprKind::MethodCall(path, rcvr, ..), .. }) =
4612            call_node
4613        {
4614            if Some(rcvr.span) == err.span.primary_span() {
4615                err.replace_span_with(path.ident.span, true);
4616            }
4617        }
4618
4619        if let Node::Expr(expr) = call_node {
4620            if let hir::ExprKind::Call(hir::Expr { span, .. }, _)
4621            | hir::ExprKind::MethodCall(
4622                hir::PathSegment { ident: Ident { span, .. }, .. },
4623                ..,
4624            ) = expr.kind
4625            {
4626                if Some(*span) != err.span.primary_span() {
4627                    let msg = if span.is_desugaring(DesugaringKind::FormatLiteral { source: true })
4628                    {
4629                        "required by this formatting parameter"
4630                    } else if span.is_desugaring(DesugaringKind::FormatLiteral { source: false }) {
4631                        "required by a formatting parameter in this expression"
4632                    } else {
4633                        "required by a bound introduced by this call"
4634                    };
4635                    err.span_label(*span, msg);
4636                }
4637            }
4638
4639            if let hir::ExprKind::MethodCall(_, expr, ..) = expr.kind {
4640                self.suggest_option_method_if_applicable(failed_pred, param_env, err, expr);
4641            }
4642        }
4643    }
4644
4645    fn suggest_option_method_if_applicable<G: EmissionGuarantee>(
4646        &self,
4647        failed_pred: ty::Predicate<'tcx>,
4648        param_env: ty::ParamEnv<'tcx>,
4649        err: &mut Diag<'_, G>,
4650        expr: &hir::Expr<'_>,
4651    ) {
4652        let tcx = self.tcx;
4653        let infcx = self.infcx;
4654        let Some(typeck_results) = self.typeck_results.as_ref() else { return };
4655
4656        // Make sure we're dealing with the `Option` type.
4657        let Some(option_ty_adt) = typeck_results.expr_ty_adjusted(expr).ty_adt_def() else {
4658            return;
4659        };
4660        if !tcx.is_diagnostic_item(sym::Option, option_ty_adt.did()) {
4661            return;
4662        }
4663
4664        // Given the predicate `fn(&T): FnOnce<(U,)>`, extract `fn(&T)` and `(U,)`,
4665        // then suggest `Option::as_deref(_mut)` if `U` can deref to `T`
4666        if let ty::PredicateKind::Clause(ty::ClauseKind::Trait(ty::TraitPredicate { trait_ref, .. }))
4667            = failed_pred.kind().skip_binder()
4668            && tcx.is_fn_trait(trait_ref.def_id)
4669            && let [self_ty, found_ty] = trait_ref.args.as_slice()
4670            && let Some(fn_ty) = self_ty.as_type().filter(|ty| ty.is_fn())
4671            && let fn_sig @ ty::FnSig {
4672                abi: ExternAbi::Rust,
4673                c_variadic: false,
4674                safety: hir::Safety::Safe,
4675                ..
4676            } = fn_ty.fn_sig(tcx).skip_binder()
4677
4678            // Extract first param of fn sig with peeled refs, e.g. `fn(&T)` -> `T`
4679            && let Some(&ty::Ref(_, target_ty, needs_mut)) = fn_sig.inputs().first().map(|t| t.kind())
4680            && !target_ty.has_escaping_bound_vars()
4681
4682            // Extract first tuple element out of fn trait, e.g. `FnOnce<(U,)>` -> `U`
4683            && let Some(ty::Tuple(tys)) = found_ty.as_type().map(Ty::kind)
4684            && let &[found_ty] = tys.as_slice()
4685            && !found_ty.has_escaping_bound_vars()
4686
4687            // Extract `<U as Deref>::Target` assoc type and check that it is `T`
4688            && let Some(deref_target_did) = tcx.lang_items().deref_target()
4689            && let projection = Ty::new_projection_from_args(tcx,deref_target_did, tcx.mk_args(&[ty::GenericArg::from(found_ty)]))
4690            && let InferOk { value: deref_target, obligations } = infcx.at(&ObligationCause::dummy(), param_env).normalize(projection)
4691            && obligations.iter().all(|obligation| infcx.predicate_must_hold_modulo_regions(obligation))
4692            && infcx.can_eq(param_env, deref_target, target_ty)
4693        {
4694            let help = if let hir::Mutability::Mut = needs_mut
4695                && let Some(deref_mut_did) = tcx.lang_items().deref_mut_trait()
4696                && infcx
4697                    .type_implements_trait(deref_mut_did, iter::once(found_ty), param_env)
4698                    .must_apply_modulo_regions()
4699            {
4700                Some(("call `Option::as_deref_mut()` first", ".as_deref_mut()"))
4701            } else if let hir::Mutability::Not = needs_mut {
4702                Some(("call `Option::as_deref()` first", ".as_deref()"))
4703            } else {
4704                None
4705            };
4706
4707            if let Some((msg, sugg)) = help {
4708                err.span_suggestion_with_style(
4709                    expr.span.shrink_to_hi(),
4710                    msg,
4711                    sugg,
4712                    Applicability::MaybeIncorrect,
4713                    SuggestionStyle::ShowAlways,
4714                );
4715            }
4716        }
4717    }
4718
4719    fn look_for_iterator_item_mistakes<G: EmissionGuarantee>(
4720        &self,
4721        assocs_in_this_method: &[Option<(Span, (DefId, Ty<'tcx>))>],
4722        typeck_results: &TypeckResults<'tcx>,
4723        type_diffs: &[TypeError<'tcx>],
4724        param_env: ty::ParamEnv<'tcx>,
4725        path_segment: &hir::PathSegment<'_>,
4726        args: &[hir::Expr<'_>],
4727        prev_ty: Ty<'_>,
4728        err: &mut Diag<'_, G>,
4729    ) {
4730        let tcx = self.tcx;
4731        // Special case for iterator chains, we look at potential failures of `Iterator::Item`
4732        // not being `: Clone` and `Iterator::map` calls with spurious trailing `;`.
4733        for entry in assocs_in_this_method {
4734            let Some((_span, (def_id, ty))) = entry else {
4735                continue;
4736            };
4737            for diff in type_diffs {
4738                let TypeError::Sorts(expected_found) = diff else {
4739                    continue;
4740                };
4741                if tcx.is_diagnostic_item(sym::IntoIteratorItem, *def_id)
4742                    && path_segment.ident.name == sym::iter
4743                    && self.can_eq(
4744                        param_env,
4745                        Ty::new_ref(
4746                            tcx,
4747                            tcx.lifetimes.re_erased,
4748                            expected_found.found,
4749                            ty::Mutability::Not,
4750                        ),
4751                        *ty,
4752                    )
4753                    && let [] = args
4754                {
4755                    // Used `.iter()` when `.into_iter()` was likely meant.
4756                    err.span_suggestion_verbose(
4757                        path_segment.ident.span,
4758                        ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("consider consuming the `{0}` to construct the `Iterator`",
                prev_ty))
    })format!("consider consuming the `{prev_ty}` to construct the `Iterator`"),
4759                        "into_iter".to_string(),
4760                        Applicability::MachineApplicable,
4761                    );
4762                }
4763                if tcx.is_diagnostic_item(sym::IntoIteratorItem, *def_id)
4764                    && path_segment.ident.name == sym::into_iter
4765                    && self.can_eq(
4766                        param_env,
4767                        expected_found.found,
4768                        Ty::new_ref(tcx, tcx.lifetimes.re_erased, *ty, ty::Mutability::Not),
4769                    )
4770                    && let [] = args
4771                {
4772                    // Used `.into_iter()` when `.iter()` was likely meant.
4773                    err.span_suggestion_verbose(
4774                        path_segment.ident.span,
4775                        ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("consider not consuming the `{0}` to construct the `Iterator`",
                prev_ty))
    })format!(
4776                            "consider not consuming the `{prev_ty}` to construct the `Iterator`"
4777                        ),
4778                        "iter".to_string(),
4779                        Applicability::MachineApplicable,
4780                    );
4781                }
4782                if tcx.is_diagnostic_item(sym::IteratorItem, *def_id)
4783                    && path_segment.ident.name == sym::map
4784                    && self.can_eq(param_env, expected_found.found, *ty)
4785                    && let [arg] = args
4786                    && let hir::ExprKind::Closure(closure) = arg.kind
4787                {
4788                    let body = tcx.hir_body(closure.body);
4789                    if let hir::ExprKind::Block(block, None) = body.value.kind
4790                        && let None = block.expr
4791                        && let [.., stmt] = block.stmts
4792                        && let hir::StmtKind::Semi(expr) = stmt.kind
4793                        // FIXME: actually check the expected vs found types, but right now
4794                        // the expected is a projection that we need to resolve.
4795                        // && let Some(tail_ty) = typeck_results.expr_ty_opt(expr)
4796                        && expected_found.found.is_unit()
4797                        // FIXME: this happens with macro calls. Need to figure out why the stmt
4798                        // `println!();` doesn't include the `;` in its `Span`. (#133845)
4799                        // We filter these out to avoid ICEs with debug assertions on caused by
4800                        // empty suggestions.
4801                        && expr.span.hi() != stmt.span.hi()
4802                    {
4803                        err.span_suggestion_verbose(
4804                            expr.span.shrink_to_hi().with_hi(stmt.span.hi()),
4805                            "consider removing this semicolon",
4806                            String::new(),
4807                            Applicability::MachineApplicable,
4808                        );
4809                    }
4810                    let expr = if let hir::ExprKind::Block(block, None) = body.value.kind
4811                        && let Some(expr) = block.expr
4812                    {
4813                        expr
4814                    } else {
4815                        body.value
4816                    };
4817                    if let hir::ExprKind::MethodCall(path_segment, rcvr, [], span) = expr.kind
4818                        && path_segment.ident.name == sym::clone
4819                        && let Some(expr_ty) = typeck_results.expr_ty_opt(expr)
4820                        && let Some(rcvr_ty) = typeck_results.expr_ty_opt(rcvr)
4821                        && self.can_eq(param_env, expr_ty, rcvr_ty)
4822                        && let ty::Ref(_, ty, _) = expr_ty.kind()
4823                    {
4824                        err.span_label(
4825                            span,
4826                            ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("this method call is cloning the reference `{0}`, not `{1}` which doesn\'t implement `Clone`",
                expr_ty, ty))
    })format!(
4827                                "this method call is cloning the reference `{expr_ty}`, not \
4828                                 `{ty}` which doesn't implement `Clone`",
4829                            ),
4830                        );
4831                        let ty::Param(..) = ty.kind() else {
4832                            continue;
4833                        };
4834                        let node =
4835                            tcx.hir_node_by_def_id(tcx.hir_get_parent_item(expr.hir_id).def_id);
4836
4837                        let pred = ty::Binder::dummy(ty::TraitPredicate {
4838                            trait_ref: ty::TraitRef::new(
4839                                tcx,
4840                                tcx.require_lang_item(LangItem::Clone, span),
4841                                [*ty],
4842                            ),
4843                            polarity: ty::PredicatePolarity::Positive,
4844                        });
4845                        let Some(generics) = node.generics() else {
4846                            continue;
4847                        };
4848                        let Some(body_id) = node.body_id() else {
4849                            continue;
4850                        };
4851                        suggest_restriction(
4852                            tcx,
4853                            tcx.hir_body_owner_def_id(body_id),
4854                            generics,
4855                            &::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("type parameter `{0}`", ty))
    })format!("type parameter `{ty}`"),
4856                            err,
4857                            node.fn_sig(),
4858                            None,
4859                            pred,
4860                            None,
4861                        );
4862                    }
4863                }
4864            }
4865        }
4866    }
4867
4868    fn point_at_chain<G: EmissionGuarantee>(
4869        &self,
4870        expr: &hir::Expr<'_>,
4871        typeck_results: &TypeckResults<'tcx>,
4872        type_diffs: Vec<TypeError<'tcx>>,
4873        param_env: ty::ParamEnv<'tcx>,
4874        err: &mut Diag<'_, G>,
4875    ) {
4876        let mut primary_spans = ::alloc::vec::Vec::new()vec![];
4877        let mut span_labels = ::alloc::vec::Vec::new()vec![];
4878
4879        let tcx = self.tcx;
4880
4881        let mut print_root_expr = true;
4882        let mut assocs = ::alloc::vec::Vec::new()vec![];
4883        let mut expr = expr;
4884        let mut prev_ty = self.resolve_vars_if_possible(
4885            typeck_results.expr_ty_adjusted_opt(expr).unwrap_or(Ty::new_misc_error(tcx)),
4886        );
4887        while let hir::ExprKind::MethodCall(path_segment, rcvr_expr, args, span) = expr.kind {
4888            // Point at every method call in the chain with the resulting type.
4889            // vec![1, 2, 3].iter().map(mapper).sum<i32>()
4890            //               ^^^^^^ ^^^^^^^^^^^
4891            expr = rcvr_expr;
4892            let assocs_in_this_method =
4893                self.probe_assoc_types_at_expr(&type_diffs, span, prev_ty, expr.hir_id, param_env);
4894            prev_ty = self.resolve_vars_if_possible(
4895                typeck_results.expr_ty_adjusted_opt(expr).unwrap_or(Ty::new_misc_error(tcx)),
4896            );
4897            self.look_for_iterator_item_mistakes(
4898                &assocs_in_this_method,
4899                typeck_results,
4900                &type_diffs,
4901                param_env,
4902                path_segment,
4903                args,
4904                prev_ty,
4905                err,
4906            );
4907            assocs.push(assocs_in_this_method);
4908
4909            if let hir::ExprKind::Path(hir::QPath::Resolved(None, path)) = expr.kind
4910                && let hir::Path { res: Res::Local(hir_id), .. } = path
4911                && let hir::Node::Pat(binding) = self.tcx.hir_node(*hir_id)
4912            {
4913                let parent = self.tcx.parent_hir_node(binding.hir_id);
4914                // We've reached the root of the method call chain...
4915                if let hir::Node::LetStmt(local) = parent
4916                    && let Some(binding_expr) = local.init
4917                {
4918                    // ...and it is a binding. Get the binding creation and continue the chain.
4919                    expr = binding_expr;
4920                }
4921                if let hir::Node::Param(param) = parent {
4922                    // ...and it is an fn argument.
4923                    let prev_ty = self.resolve_vars_if_possible(
4924                        typeck_results
4925                            .node_type_opt(param.hir_id)
4926                            .unwrap_or(Ty::new_misc_error(tcx)),
4927                    );
4928                    let assocs_in_this_method = self.probe_assoc_types_at_expr(
4929                        &type_diffs,
4930                        param.ty_span,
4931                        prev_ty,
4932                        param.hir_id,
4933                        param_env,
4934                    );
4935                    if assocs_in_this_method.iter().any(|a| a.is_some()) {
4936                        assocs.push(assocs_in_this_method);
4937                        print_root_expr = false;
4938                    }
4939                    break;
4940                }
4941            }
4942        }
4943        // We want the type before deref coercions, otherwise we talk about `&[_]`
4944        // instead of `Vec<_>`.
4945        if let Some(ty) = typeck_results.expr_ty_opt(expr)
4946            && print_root_expr
4947        {
4948            let ty = { let _guard = ForceTrimmedGuard::new(); self.ty_to_string(ty) }with_forced_trimmed_paths!(self.ty_to_string(ty));
4949            // Point at the root expression
4950            // vec![1, 2, 3].iter().map(mapper).sum<i32>()
4951            // ^^^^^^^^^^^^^
4952            span_labels.push((expr.span, ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("this expression has type `{0}`",
                ty))
    })format!("this expression has type `{ty}`")));
4953        };
4954        // Only show this if it is not a "trivial" expression (not a method
4955        // chain) and there are associated types to talk about.
4956        let mut assocs = assocs.into_iter().peekable();
4957        while let Some(assocs_in_method) = assocs.next() {
4958            let Some(prev_assoc_in_method) = assocs.peek() else {
4959                for entry in assocs_in_method {
4960                    let Some((span, (assoc, ty))) = entry else {
4961                        continue;
4962                    };
4963                    if primary_spans.is_empty()
4964                        || type_diffs.iter().any(|diff| {
4965                            let TypeError::Sorts(expected_found) = diff else {
4966                                return false;
4967                            };
4968                            self.can_eq(param_env, expected_found.found, ty)
4969                        })
4970                    {
4971                        // FIXME: this doesn't quite work for `Iterator::collect`
4972                        // because we have `Vec<i32>` and `()`, but we'd want `i32`
4973                        // to point at the `.into_iter()` call, but as long as we
4974                        // still point at the other method calls that might have
4975                        // introduced the issue, this is fine for now.
4976                        primary_spans.push(span);
4977                    }
4978                    span_labels.push((
4979                        span,
4980                        {
    let _guard = ForceTrimmedGuard::new();
    ::alloc::__export::must_use({
            ::alloc::fmt::format(format_args!("`{0}` is `{1}` here",
                    self.tcx.def_path_str(assoc), ty))
        })
}with_forced_trimmed_paths!(format!(
4981                            "`{}` is `{ty}` here",
4982                            self.tcx.def_path_str(assoc),
4983                        )),
4984                    ));
4985                }
4986                break;
4987            };
4988            for (entry, prev_entry) in
4989                assocs_in_method.into_iter().zip(prev_assoc_in_method.into_iter())
4990            {
4991                match (entry, prev_entry) {
4992                    (Some((span, (assoc, ty))), Some((_, (_, prev_ty)))) => {
4993                        let ty_str = { let _guard = ForceTrimmedGuard::new(); self.ty_to_string(ty) }with_forced_trimmed_paths!(self.ty_to_string(ty));
4994
4995                        let assoc = { let _guard = ForceTrimmedGuard::new(); self.tcx.def_path_str(assoc) }with_forced_trimmed_paths!(self.tcx.def_path_str(assoc));
4996                        if !self.can_eq(param_env, ty, *prev_ty) {
4997                            if type_diffs.iter().any(|diff| {
4998                                let TypeError::Sorts(expected_found) = diff else {
4999                                    return false;
5000                                };
5001                                self.can_eq(param_env, expected_found.found, ty)
5002                            }) {
5003                                primary_spans.push(span);
5004                            }
5005                            span_labels
5006                                .push((span, ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("`{0}` changed to `{1}` here",
                assoc, ty_str))
    })format!("`{assoc}` changed to `{ty_str}` here")));
5007                        } else {
5008                            span_labels.push((span, ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("`{0}` remains `{1}` here", assoc,
                ty_str))
    })format!("`{assoc}` remains `{ty_str}` here")));
5009                        }
5010                    }
5011                    (Some((span, (assoc, ty))), None) => {
5012                        span_labels.push((
5013                            span,
5014                            {
    let _guard = ForceTrimmedGuard::new();
    ::alloc::__export::must_use({
            ::alloc::fmt::format(format_args!("`{0}` is `{1}` here",
                    self.tcx.def_path_str(assoc), self.ty_to_string(ty)))
        })
}with_forced_trimmed_paths!(format!(
5015                                "`{}` is `{}` here",
5016                                self.tcx.def_path_str(assoc),
5017                                self.ty_to_string(ty),
5018                            )),
5019                        ));
5020                    }
5021                    (None, Some(_)) | (None, None) => {}
5022                }
5023            }
5024        }
5025        if !primary_spans.is_empty() {
5026            let mut multi_span: MultiSpan = primary_spans.into();
5027            for (span, label) in span_labels {
5028                multi_span.push_span_label(span, label);
5029            }
5030            err.span_note(
5031                multi_span,
5032                "the method call chain might not have had the expected associated types",
5033            );
5034        }
5035    }
5036
5037    fn probe_assoc_types_at_expr(
5038        &self,
5039        type_diffs: &[TypeError<'tcx>],
5040        span: Span,
5041        prev_ty: Ty<'tcx>,
5042        body_id: HirId,
5043        param_env: ty::ParamEnv<'tcx>,
5044    ) -> Vec<Option<(Span, (DefId, Ty<'tcx>))>> {
5045        let ocx = ObligationCtxt::new(self.infcx);
5046        let mut assocs_in_this_method = Vec::with_capacity(type_diffs.len());
5047        for diff in type_diffs {
5048            let TypeError::Sorts(expected_found) = diff else {
5049                continue;
5050            };
5051            let ty::Alias(ty::AliasTy { kind: ty::Projection { def_id }, .. }) =
5052                expected_found.expected.kind()
5053            else {
5054                continue;
5055            };
5056
5057            // Make `Self` be equivalent to the type of the call chain
5058            // expression we're looking at now, so that we can tell what
5059            // for example `Iterator::Item` is at this point in the chain.
5060            let args = GenericArgs::for_item(self.tcx, *def_id, |param, _| {
5061                if param.index == 0 {
5062                    if true {
    match param.kind {
        ty::GenericParamDefKind::Type { .. } => {}
        ref left_val => {
            ::core::panicking::assert_matches_failed(left_val,
                "ty::GenericParamDefKind::Type { .. }",
                ::core::option::Option::None);
        }
    };
};debug_assert_matches!(param.kind, ty::GenericParamDefKind::Type { .. });
5063                    return prev_ty.into();
5064                }
5065                self.var_for_def(span, param)
5066            });
5067            // This will hold the resolved type of the associated type, if the
5068            // current expression implements the trait that associated type is
5069            // in. For example, this would be what `Iterator::Item` is here.
5070            let ty = self.infcx.next_ty_var(span);
5071            // This corresponds to `<ExprTy as Iterator>::Item = _`.
5072            let projection = ty::Binder::dummy(ty::PredicateKind::Clause(
5073                ty::ClauseKind::Projection(ty::ProjectionPredicate {
5074                    projection_term: ty::AliasTerm::new_from_args(self.tcx, *def_id, args),
5075                    term: ty.into(),
5076                }),
5077            ));
5078            let body_def_id = self.tcx.hir_enclosing_body_owner(body_id);
5079            // Add `<ExprTy as Iterator>::Item = _` obligation.
5080            ocx.register_obligation(Obligation::misc(
5081                self.tcx,
5082                span,
5083                body_def_id,
5084                param_env,
5085                projection,
5086            ));
5087            if ocx.try_evaluate_obligations().is_empty()
5088                && let ty = self.resolve_vars_if_possible(ty)
5089                && !ty.is_ty_var()
5090            {
5091                assocs_in_this_method.push(Some((span, (*def_id, ty))));
5092            } else {
5093                // `<ExprTy as Iterator>` didn't select, so likely we've
5094                // reached the end of the iterator chain, like the originating
5095                // `Vec<_>` or the `ty` couldn't be determined.
5096                // Keep the space consistent for later zipping.
5097                assocs_in_this_method.push(None);
5098            }
5099        }
5100        assocs_in_this_method
5101    }
5102
5103    /// If the type that failed selection is an array or a reference to an array,
5104    /// but the trait is implemented for slices, suggest that the user converts
5105    /// the array into a slice.
5106    pub(super) fn suggest_convert_to_slice(
5107        &self,
5108        err: &mut Diag<'_>,
5109        obligation: &PredicateObligation<'tcx>,
5110        trait_pred: ty::PolyTraitPredicate<'tcx>,
5111        candidate_impls: &[ImplCandidate<'tcx>],
5112        span: Span,
5113    ) {
5114        if span.in_external_macro(self.tcx.sess.source_map()) {
5115            return;
5116        }
5117        // We can only suggest the slice coercion for function and binary operation arguments,
5118        // since the suggestion would make no sense in turbofish or call
5119        let (ObligationCauseCode::BinOp { .. } | ObligationCauseCode::FunctionArg { .. }) =
5120            obligation.cause.code()
5121        else {
5122            return;
5123        };
5124
5125        // Three cases where we can make a suggestion:
5126        // 1. `[T; _]` (array of T)
5127        // 2. `&[T; _]` (reference to array of T)
5128        // 3. `&mut [T; _]` (mutable reference to array of T)
5129        let (element_ty, mut mutability) = match *trait_pred.skip_binder().self_ty().kind() {
5130            ty::Array(element_ty, _) => (element_ty, None),
5131
5132            ty::Ref(_, pointee_ty, mutability) => match *pointee_ty.kind() {
5133                ty::Array(element_ty, _) => (element_ty, Some(mutability)),
5134                _ => return,
5135            },
5136
5137            _ => return,
5138        };
5139
5140        // Go through all the candidate impls to see if any of them is for
5141        // slices of `element_ty` with `mutability`.
5142        let mut is_slice = |candidate: Ty<'tcx>| match *candidate.kind() {
5143            ty::RawPtr(t, m) | ty::Ref(_, t, m) => {
5144                if let ty::Slice(e) = *t.kind()
5145                    && e == element_ty
5146                    && m == mutability.unwrap_or(m)
5147                {
5148                    // Use the candidate's mutability going forward.
5149                    mutability = Some(m);
5150                    true
5151                } else {
5152                    false
5153                }
5154            }
5155            _ => false,
5156        };
5157
5158        // Grab the first candidate that matches, if any, and make a suggestion.
5159        if let Some(slice_ty) = candidate_impls
5160            .iter()
5161            .map(|trait_ref| trait_ref.trait_ref.self_ty())
5162            .find(|t| is_slice(*t))
5163        {
5164            let msg = ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("convert the array to a `{0}` slice instead",
                slice_ty))
    })format!("convert the array to a `{slice_ty}` slice instead");
5165
5166            if let Ok(snippet) = self.tcx.sess.source_map().span_to_snippet(span) {
5167                let mut suggestions = ::alloc::vec::Vec::new()vec![];
5168                if snippet.starts_with('&') {
5169                } else if let Some(hir::Mutability::Mut) = mutability {
5170                    suggestions.push((span.shrink_to_lo(), "&mut ".into()));
5171                } else {
5172                    suggestions.push((span.shrink_to_lo(), "&".into()));
5173                }
5174                suggestions.push((span.shrink_to_hi(), "[..]".into()));
5175                err.multipart_suggestion(msg, suggestions, Applicability::MaybeIncorrect);
5176            } else {
5177                err.span_help(span, msg);
5178            }
5179        }
5180    }
5181
5182    /// If the type failed selection but the trait is implemented for `(T,)`, suggest that the user
5183    /// creates a unary tuple
5184    ///
5185    /// This is a common gotcha when using libraries that emulate variadic functions with traits for tuples.
5186    pub(super) fn suggest_tuple_wrapping(
5187        &self,
5188        err: &mut Diag<'_>,
5189        root_obligation: &PredicateObligation<'tcx>,
5190        obligation: &PredicateObligation<'tcx>,
5191    ) {
5192        let ObligationCauseCode::FunctionArg { arg_hir_id, .. } = obligation.cause.code() else {
5193            return;
5194        };
5195
5196        let Some(root_pred) = root_obligation.predicate.as_trait_clause() else { return };
5197
5198        let trait_ref = root_pred.map_bound(|root_pred| {
5199            root_pred.trait_ref.with_replaced_self_ty(
5200                self.tcx,
5201                Ty::new_tup(self.tcx, &[root_pred.trait_ref.self_ty()]),
5202            )
5203        });
5204
5205        let obligation =
5206            Obligation::new(self.tcx, obligation.cause.clone(), obligation.param_env, trait_ref);
5207
5208        if self.predicate_must_hold_modulo_regions(&obligation) {
5209            let arg_span = self.tcx.hir_span(*arg_hir_id);
5210            err.multipart_suggestion(
5211                ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("use a unary tuple instead"))
    })format!("use a unary tuple instead"),
5212                ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [(arg_span.shrink_to_lo(), "(".into()),
                (arg_span.shrink_to_hi(), ",)".into())]))vec![(arg_span.shrink_to_lo(), "(".into()), (arg_span.shrink_to_hi(), ",)".into())],
5213                Applicability::MaybeIncorrect,
5214            );
5215        }
5216    }
5217
5218    pub(super) fn suggest_shadowed_inherent_method(
5219        &self,
5220        err: &mut Diag<'_>,
5221        obligation: &PredicateObligation<'tcx>,
5222        trait_predicate: ty::PolyTraitPredicate<'tcx>,
5223    ) {
5224        let ObligationCauseCode::FunctionArg { call_hir_id, .. } = obligation.cause.code() else {
5225            return;
5226        };
5227        let Node::Expr(call) = self.tcx.hir_node(*call_hir_id) else { return };
5228        let hir::ExprKind::MethodCall(segment, rcvr, args, ..) = call.kind else { return };
5229        let Some(typeck) = &self.typeck_results else { return };
5230        let Some(rcvr_ty) = typeck.expr_ty_adjusted_opt(rcvr) else { return };
5231        let rcvr_ty = self.resolve_vars_if_possible(rcvr_ty);
5232        let autoderef = (self.autoderef_steps)(rcvr_ty);
5233        for (ty, def_id) in autoderef.iter().filter_map(|(ty, obligations)| {
5234            if let ty::Adt(def, _) = ty.kind()
5235                && *ty != rcvr_ty.peel_refs()
5236                && obligations.iter().all(|obligation| self.predicate_may_hold(obligation))
5237            {
5238                Some((ty, def.did()))
5239            } else {
5240                None
5241            }
5242        }) {
5243            for impl_def_id in self.tcx.inherent_impls(def_id) {
5244                if *impl_def_id == trait_predicate.def_id() {
5245                    continue;
5246                }
5247                for m in self
5248                    .tcx
5249                    .provided_trait_methods(*impl_def_id)
5250                    .filter(|m| m.name() == segment.ident.name)
5251                {
5252                    let fn_sig = self.tcx.fn_sig(m.def_id);
5253                    if fn_sig.skip_binder().inputs().skip_binder().len() != args.len() + 1 {
5254                        continue;
5255                    }
5256                    let rcvr_ty = fn_sig.skip_binder().input(0).skip_binder();
5257                    let (mutability, _ty) = match rcvr_ty.kind() {
5258                        ty::Ref(_, ty, hir::Mutability::Mut) => ("&mut ", ty),
5259                        ty::Ref(_, ty, _) => ("&", ty),
5260                        _ => ("", &rcvr_ty),
5261                    };
5262                    let path = self.tcx.def_path_str(def_id);
5263                    err.note(::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("there\'s an inherent method on `{0}` of the same name, which can be auto-dereferenced from `{1}`",
                ty, rcvr_ty))
    })format!(
5264                        "there's an inherent method on `{ty}` of the same name, which can be \
5265                         auto-dereferenced from `{rcvr_ty}`"
5266                    ));
5267                    err.multipart_suggestion(
5268                        ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("to access the inherent method on `{0}`, use the fully-qualified path",
                ty))
    })format!(
5269                            "to access the inherent method on `{ty}`, use the fully-qualified path",
5270                        ),
5271                        ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [(call.span.until(rcvr.span),
                    ::alloc::__export::must_use({
                            ::alloc::fmt::format(format_args!("{2}::{0}({1}", m.name(),
                                    mutability, path))
                        })),
                match &args {
                    [] =>
                        (rcvr.span.shrink_to_hi().with_hi(call.span.hi()),
                            ")".to_string()),
                    [first, ..] =>
                        (rcvr.span.between(first.span), ", ".to_string()),
                }]))vec![
5272                            (
5273                                call.span.until(rcvr.span),
5274                                format!("{path}::{}({}", m.name(), mutability),
5275                            ),
5276                            match &args {
5277                                [] => (
5278                                    rcvr.span.shrink_to_hi().with_hi(call.span.hi()),
5279                                    ")".to_string(),
5280                                ),
5281                                [first, ..] => (rcvr.span.between(first.span), ", ".to_string()),
5282                            },
5283                        ],
5284                        Applicability::MaybeIncorrect,
5285                    );
5286                }
5287            }
5288        }
5289    }
5290
5291    pub(super) fn explain_hrtb_projection(
5292        &self,
5293        diag: &mut Diag<'_>,
5294        pred: ty::PolyTraitPredicate<'tcx>,
5295        param_env: ty::ParamEnv<'tcx>,
5296        cause: &ObligationCause<'tcx>,
5297    ) {
5298        if pred.skip_binder().has_escaping_bound_vars() && pred.skip_binder().has_non_region_infer()
5299        {
5300            self.probe(|_| {
5301                let ocx = ObligationCtxt::new(self);
5302                self.enter_forall(pred, |pred| {
5303                    let pred = ocx.normalize(&ObligationCause::dummy(), param_env, pred);
5304                    ocx.register_obligation(Obligation::new(
5305                        self.tcx,
5306                        ObligationCause::dummy(),
5307                        param_env,
5308                        pred,
5309                    ));
5310                });
5311                if !ocx.try_evaluate_obligations().is_empty() {
5312                    // encountered errors.
5313                    return;
5314                }
5315
5316                if let ObligationCauseCode::FunctionArg {
5317                    call_hir_id,
5318                    arg_hir_id,
5319                    parent_code: _,
5320                } = cause.code()
5321                {
5322                    let arg_span = self.tcx.hir_span(*arg_hir_id);
5323                    let mut sp: MultiSpan = arg_span.into();
5324
5325                    sp.push_span_label(
5326                        arg_span,
5327                        "the trait solver is unable to infer the \
5328                        generic types that should be inferred from this argument",
5329                    );
5330                    sp.push_span_label(
5331                        self.tcx.hir_span(*call_hir_id),
5332                        "add turbofish arguments to this call to \
5333                        specify the types manually, even if it's redundant",
5334                    );
5335                    diag.span_note(
5336                        sp,
5337                        "this is a known limitation of the trait solver that \
5338                        will be lifted in the future",
5339                    );
5340                } else {
5341                    let mut sp: MultiSpan = cause.span.into();
5342                    sp.push_span_label(
5343                        cause.span,
5344                        "try adding turbofish arguments to this expression to \
5345                        specify the types manually, even if it's redundant",
5346                    );
5347                    diag.span_note(
5348                        sp,
5349                        "this is a known limitation of the trait solver that \
5350                        will be lifted in the future",
5351                    );
5352                }
5353            });
5354        }
5355    }
5356
5357    pub(super) fn suggest_desugaring_async_fn_in_trait(
5358        &self,
5359        err: &mut Diag<'_>,
5360        trait_pred: ty::PolyTraitPredicate<'tcx>,
5361    ) {
5362        // Don't suggest if RTN is active -- we should prefer a where-clause bound instead.
5363        if self.tcx.features().return_type_notation() {
5364            return;
5365        }
5366
5367        let trait_def_id = trait_pred.def_id();
5368
5369        // Only suggest specifying auto traits
5370        if !self.tcx.trait_is_auto(trait_def_id) {
5371            return;
5372        }
5373
5374        // Look for an RPITIT
5375        let ty::Alias(alias_ty @ ty::AliasTy { kind: ty::Projection { def_id }, .. }) =
5376            trait_pred.self_ty().skip_binder().kind()
5377        else {
5378            return;
5379        };
5380        let Some(ty::ImplTraitInTraitData::Trait { fn_def_id, opaque_def_id }) =
5381            self.tcx.opt_rpitit_info(*def_id)
5382        else {
5383            return;
5384        };
5385
5386        let auto_trait = self.tcx.def_path_str(trait_def_id);
5387        // ... which is a local function
5388        let Some(fn_def_id) = fn_def_id.as_local() else {
5389            // If it's not local, we can at least mention that the method is async, if it is.
5390            if self.tcx.asyncness(fn_def_id).is_async() {
5391                err.span_note(
5392                    self.tcx.def_span(fn_def_id),
5393                    ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("`{0}::{1}` is an `async fn` in trait, which does not automatically imply that its future is `{2}`",
                alias_ty.trait_ref(self.tcx), self.tcx.item_name(fn_def_id),
                auto_trait))
    })format!(
5394                        "`{}::{}` is an `async fn` in trait, which does not \
5395                    automatically imply that its future is `{auto_trait}`",
5396                        alias_ty.trait_ref(self.tcx),
5397                        self.tcx.item_name(fn_def_id)
5398                    ),
5399                );
5400            }
5401            return;
5402        };
5403        let hir::Node::TraitItem(item) = self.tcx.hir_node_by_def_id(fn_def_id) else {
5404            return;
5405        };
5406
5407        // ... whose signature is `async` (i.e. this is an AFIT)
5408        let (sig, body) = item.expect_fn();
5409        let hir::FnRetTy::Return(hir::Ty { kind: hir::TyKind::OpaqueDef(opaq_def, ..), .. }) =
5410            sig.decl.output
5411        else {
5412            // This should never happen, but let's not ICE.
5413            return;
5414        };
5415
5416        // Check that this is *not* a nested `impl Future` RPIT in an async fn
5417        // (i.e. `async fn foo() -> impl Future`)
5418        if opaq_def.def_id.to_def_id() != opaque_def_id {
5419            return;
5420        }
5421
5422        let Some(sugg) = suggest_desugaring_async_fn_to_impl_future_in_trait(
5423            self.tcx,
5424            *sig,
5425            *body,
5426            opaque_def_id.expect_local(),
5427            &::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!(" + {0}", auto_trait))
    })format!(" + {auto_trait}"),
5428        ) else {
5429            return;
5430        };
5431
5432        let function_name = self.tcx.def_path_str(fn_def_id);
5433        err.multipart_suggestion(
5434            ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("`{0}` can be made part of the associated future\'s guarantees for all implementations of `{1}`",
                auto_trait, function_name))
    })format!(
5435                "`{auto_trait}` can be made part of the associated future's \
5436                guarantees for all implementations of `{function_name}`"
5437            ),
5438            sugg,
5439            Applicability::MachineApplicable,
5440        );
5441    }
5442
5443    pub fn ty_kind_suggestion(
5444        &self,
5445        param_env: ty::ParamEnv<'tcx>,
5446        ty: Ty<'tcx>,
5447    ) -> Option<String> {
5448        let tcx = self.infcx.tcx;
5449        let implements_default = |ty| {
5450            let Some(default_trait) = tcx.get_diagnostic_item(sym::Default) else {
5451                return false;
5452            };
5453            self.type_implements_trait(default_trait, [ty], param_env).must_apply_modulo_regions()
5454        };
5455
5456        Some(match *ty.kind() {
5457            ty::Never | ty::Error(_) => return None,
5458            ty::Bool => "false".to_string(),
5459            ty::Char => "\'x\'".to_string(),
5460            ty::Int(_) | ty::Uint(_) => "42".into(),
5461            ty::Float(_) => "3.14159".into(),
5462            ty::Slice(_) => "[]".to_string(),
5463            ty::Adt(def, _) if Some(def.did()) == tcx.get_diagnostic_item(sym::Vec) => {
5464                "vec![]".to_string()
5465            }
5466            ty::Adt(def, _) if Some(def.did()) == tcx.get_diagnostic_item(sym::String) => {
5467                "String::new()".to_string()
5468            }
5469            ty::Adt(def, args) if def.is_box() => {
5470                ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("Box::new({0})",
                self.ty_kind_suggestion(param_env, args[0].expect_ty())?))
    })format!("Box::new({})", self.ty_kind_suggestion(param_env, args[0].expect_ty())?)
5471            }
5472            ty::Adt(def, _) if Some(def.did()) == tcx.get_diagnostic_item(sym::Option) => {
5473                "None".to_string()
5474            }
5475            ty::Adt(def, args) if Some(def.did()) == tcx.get_diagnostic_item(sym::Result) => {
5476                ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("Ok({0})",
                self.ty_kind_suggestion(param_env, args[0].expect_ty())?))
    })format!("Ok({})", self.ty_kind_suggestion(param_env, args[0].expect_ty())?)
5477            }
5478            ty::Adt(_, _) if implements_default(ty) => "Default::default()".to_string(),
5479            ty::Ref(_, ty, mutability) => {
5480                if let (ty::Str, hir::Mutability::Not) = (ty.kind(), mutability) {
5481                    "\"\"".to_string()
5482                } else {
5483                    let ty = self.ty_kind_suggestion(param_env, ty)?;
5484                    ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("&{0}{1}", mutability.prefix_str(),
                ty))
    })format!("&{}{ty}", mutability.prefix_str())
5485                }
5486            }
5487            ty::Array(ty, len) if let Some(len) = len.try_to_target_usize(tcx) => {
5488                if len == 0 {
5489                    "[]".to_string()
5490                } else if self.type_is_copy_modulo_regions(param_env, ty) || len == 1 {
5491                    // Can only suggest `[ty; 0]` if sz == 1 or copy
5492                    ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("[{0}; {1}]",
                self.ty_kind_suggestion(param_env, ty)?, len))
    })format!("[{}; {}]", self.ty_kind_suggestion(param_env, ty)?, len)
5493                } else {
5494                    "/* value */".to_string()
5495                }
5496            }
5497            ty::Tuple(tys) => ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("({0}{1})",
                tys.iter().map(|ty|
                                    self.ty_kind_suggestion(param_env,
                                        ty)).collect::<Option<Vec<String>>>()?.join(", "),
                if tys.len() == 1 { "," } else { "" }))
    })format!(
5498                "({}{})",
5499                tys.iter()
5500                    .map(|ty| self.ty_kind_suggestion(param_env, ty))
5501                    .collect::<Option<Vec<String>>>()?
5502                    .join(", "),
5503                if tys.len() == 1 { "," } else { "" }
5504            ),
5505            _ => "/* value */".to_string(),
5506        })
5507    }
5508
5509    // For E0277 when use `?` operator, suggest adding
5510    // a suitable return type in `FnSig`, and a default
5511    // return value at the end of the function's body.
5512    pub(super) fn suggest_add_result_as_return_type(
5513        &self,
5514        obligation: &PredicateObligation<'tcx>,
5515        err: &mut Diag<'_>,
5516        trait_pred: ty::PolyTraitPredicate<'tcx>,
5517    ) {
5518        if ObligationCauseCode::QuestionMark != *obligation.cause.code().peel_derives() {
5519            return;
5520        }
5521
5522        // Only suggest for local function and associated method,
5523        // because this suggest adding both return type in
5524        // the `FnSig` and a default return value in the body, so it
5525        // is not suitable for foreign function without a local body,
5526        // and neither for trait method which may be also implemented
5527        // in other place, so shouldn't change it's FnSig.
5528        fn choose_suggest_items<'tcx, 'hir>(
5529            tcx: TyCtxt<'tcx>,
5530            node: hir::Node<'hir>,
5531        ) -> Option<(&'hir hir::FnDecl<'hir>, hir::BodyId)> {
5532            match node {
5533                hir::Node::Item(item)
5534                    if let hir::ItemKind::Fn { sig, body: body_id, .. } = item.kind =>
5535                {
5536                    Some((sig.decl, body_id))
5537                }
5538                hir::Node::ImplItem(item)
5539                    if let hir::ImplItemKind::Fn(sig, body_id) = item.kind =>
5540                {
5541                    let parent = tcx.parent_hir_node(item.hir_id());
5542                    if let hir::Node::Item(item) = parent
5543                        && let hir::ItemKind::Impl(imp) = item.kind
5544                        && imp.of_trait.is_none()
5545                    {
5546                        return Some((sig.decl, body_id));
5547                    }
5548                    None
5549                }
5550                _ => None,
5551            }
5552        }
5553
5554        let node = self.tcx.hir_node_by_def_id(obligation.cause.body_id);
5555        if let Some((fn_decl, body_id)) = choose_suggest_items(self.tcx, node)
5556            && let hir::FnRetTy::DefaultReturn(ret_span) = fn_decl.output
5557            && self.tcx.is_diagnostic_item(sym::FromResidual, trait_pred.def_id())
5558            && trait_pred.skip_binder().trait_ref.args.type_at(0).is_unit()
5559            && let ty::Adt(def, _) = trait_pred.skip_binder().trait_ref.args.type_at(1).kind()
5560            && self.tcx.is_diagnostic_item(sym::Result, def.did())
5561        {
5562            let mut sugg_spans =
5563                ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [(ret_span,
                    " -> Result<(), Box<dyn std::error::Error>>".to_string())]))vec![(ret_span, " -> Result<(), Box<dyn std::error::Error>>".to_string())];
5564            let body = self.tcx.hir_body(body_id);
5565            if let hir::ExprKind::Block(b, _) = body.value.kind
5566                && b.expr.is_none()
5567            {
5568                // The span of '}' in the end of block.
5569                let span = self.tcx.sess.source_map().end_point(b.span);
5570                sugg_spans.push((
5571                    span.shrink_to_lo(),
5572                    ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{0}{1}", "    Ok(())\n",
                self.tcx.sess.source_map().indentation_before(span).unwrap_or_default()))
    })format!(
5573                        "{}{}",
5574                        "    Ok(())\n",
5575                        self.tcx.sess.source_map().indentation_before(span).unwrap_or_default(),
5576                    ),
5577                ));
5578            }
5579            err.multipart_suggestion(
5580                ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("consider adding return type"))
    })format!("consider adding return type"),
5581                sugg_spans,
5582                Applicability::MaybeIncorrect,
5583            );
5584        }
5585    }
5586
5587    #[allow(clippy :: suspicious_else_formatting)]
{
    let __tracing_attr_span;
    let __tracing_attr_guard;
    if ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() ||
            { false } {
        __tracing_attr_span =
            {
                use ::tracing::__macro_support::Callsite as _;
                static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                    {
                        static META: ::tracing::Metadata<'static> =
                            {
                                ::tracing_core::metadata::Metadata::new("suggest_unsized_bound_if_applicable",
                                    "rustc_trait_selection::error_reporting::traits::suggestions",
                                    ::tracing::Level::DEBUG,
                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs"),
                                    ::tracing_core::__macro_support::Option::Some(5587u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_trait_selection::error_reporting::traits::suggestions"),
                                    ::tracing_core::field::FieldSet::new(&[],
                                        ::tracing_core::callsite::Identifier(&__CALLSITE)),
                                    ::tracing::metadata::Kind::SPAN)
                            };
                        ::tracing::callsite::DefaultCallsite::new(&META)
                    };
                let mut interest = ::tracing::subscriber::Interest::never();
                if ::tracing::Level::DEBUG <=
                                    ::tracing::level_filters::STATIC_MAX_LEVEL &&
                                ::tracing::Level::DEBUG <=
                                    ::tracing::level_filters::LevelFilter::current() &&
                            { interest = __CALLSITE.interest(); !interest.is_never() }
                        &&
                        ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                            interest) {
                    let meta = __CALLSITE.metadata();
                    ::tracing::Span::new(meta,
                        &{ meta.fields().value_set(&[]) })
                } else {
                    let span =
                        ::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
                    {};
                    span
                }
            };
        __tracing_attr_guard = __tracing_attr_span.enter();
    }

    #[warn(clippy :: suspicious_else_formatting)]
    {

        #[allow(unknown_lints, unreachable_code, clippy ::
        diverging_sub_expression, clippy :: empty_loop, clippy ::
        let_unit_value, clippy :: let_with_type_underscore, clippy ::
        needless_return, clippy :: unreachable)]
        if false {
            let __tracing_attr_fake_return: () = loop {};
            return __tracing_attr_fake_return;
        }
        {
            let ty::PredicateKind::Clause(ty::ClauseKind::Trait(pred)) =
                obligation.predicate.kind().skip_binder() else { return; };
            let (ObligationCauseCode::WhereClause(item_def_id, span) |
                    ObligationCauseCode::WhereClauseInExpr(item_def_id, span,
                    ..)) =
                *obligation.cause.code().peel_derives() else { return; };
            if span.is_dummy() { return; }
            {
                use ::tracing::__macro_support::Callsite as _;
                static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                    {
                        static META: ::tracing::Metadata<'static> =
                            {
                                ::tracing_core::metadata::Metadata::new("event compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs:5607",
                                    "rustc_trait_selection::error_reporting::traits::suggestions",
                                    ::tracing::Level::DEBUG,
                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs"),
                                    ::tracing_core::__macro_support::Option::Some(5607u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_trait_selection::error_reporting::traits::suggestions"),
                                    ::tracing_core::field::FieldSet::new(&["pred",
                                                    "item_def_id", "span"],
                                        ::tracing_core::callsite::Identifier(&__CALLSITE)),
                                    ::tracing::metadata::Kind::EVENT)
                            };
                        ::tracing::callsite::DefaultCallsite::new(&META)
                    };
                let enabled =
                    ::tracing::Level::DEBUG <=
                                ::tracing::level_filters::STATIC_MAX_LEVEL &&
                            ::tracing::Level::DEBUG <=
                                ::tracing::level_filters::LevelFilter::current() &&
                        {
                            let interest = __CALLSITE.interest();
                            !interest.is_never() &&
                                ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                                    interest)
                        };
                if enabled {
                    (|value_set: ::tracing::field::ValueSet|
                                {
                                    let meta = __CALLSITE.metadata();
                                    ::tracing::Event::dispatch(meta, &value_set);
                                    ;
                                })({
                            #[allow(unused_imports)]
                            use ::tracing::field::{debug, display, Value};
                            let mut iter = __CALLSITE.metadata().fields().iter();
                            __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                ::tracing::__macro_support::Option::Some(&debug(&pred) as
                                                        &dyn Value)),
                                            (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                ::tracing::__macro_support::Option::Some(&debug(&item_def_id)
                                                        as &dyn Value)),
                                            (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                ::tracing::__macro_support::Option::Some(&debug(&span) as
                                                        &dyn Value))])
                        });
                } else { ; }
            };
            let (Some(node), true) =
                (self.tcx.hir_get_if_local(item_def_id),
                    self.tcx.is_lang_item(pred.def_id(),
                        LangItem::Sized)) else { return; };
            let Some(generics) = node.generics() else { return; };
            let sized_trait = self.tcx.lang_items().sized_trait();
            {
                use ::tracing::__macro_support::Callsite as _;
                static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                    {
                        static META: ::tracing::Metadata<'static> =
                            {
                                ::tracing_core::metadata::Metadata::new("event compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs:5620",
                                    "rustc_trait_selection::error_reporting::traits::suggestions",
                                    ::tracing::Level::DEBUG,
                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs"),
                                    ::tracing_core::__macro_support::Option::Some(5620u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_trait_selection::error_reporting::traits::suggestions"),
                                    ::tracing_core::field::FieldSet::new(&["generics.params"],
                                        ::tracing_core::callsite::Identifier(&__CALLSITE)),
                                    ::tracing::metadata::Kind::EVENT)
                            };
                        ::tracing::callsite::DefaultCallsite::new(&META)
                    };
                let enabled =
                    ::tracing::Level::DEBUG <=
                                ::tracing::level_filters::STATIC_MAX_LEVEL &&
                            ::tracing::Level::DEBUG <=
                                ::tracing::level_filters::LevelFilter::current() &&
                        {
                            let interest = __CALLSITE.interest();
                            !interest.is_never() &&
                                ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                                    interest)
                        };
                if enabled {
                    (|value_set: ::tracing::field::ValueSet|
                                {
                                    let meta = __CALLSITE.metadata();
                                    ::tracing::Event::dispatch(meta, &value_set);
                                    ;
                                })({
                            #[allow(unused_imports)]
                            use ::tracing::field::{debug, display, Value};
                            let mut iter = __CALLSITE.metadata().fields().iter();
                            __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                ::tracing::__macro_support::Option::Some(&debug(&generics.params)
                                                        as &dyn Value))])
                        });
                } else { ; }
            };
            {
                use ::tracing::__macro_support::Callsite as _;
                static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                    {
                        static META: ::tracing::Metadata<'static> =
                            {
                                ::tracing_core::metadata::Metadata::new("event compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs:5621",
                                    "rustc_trait_selection::error_reporting::traits::suggestions",
                                    ::tracing::Level::DEBUG,
                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs"),
                                    ::tracing_core::__macro_support::Option::Some(5621u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_trait_selection::error_reporting::traits::suggestions"),
                                    ::tracing_core::field::FieldSet::new(&["generics.predicates"],
                                        ::tracing_core::callsite::Identifier(&__CALLSITE)),
                                    ::tracing::metadata::Kind::EVENT)
                            };
                        ::tracing::callsite::DefaultCallsite::new(&META)
                    };
                let enabled =
                    ::tracing::Level::DEBUG <=
                                ::tracing::level_filters::STATIC_MAX_LEVEL &&
                            ::tracing::Level::DEBUG <=
                                ::tracing::level_filters::LevelFilter::current() &&
                        {
                            let interest = __CALLSITE.interest();
                            !interest.is_never() &&
                                ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                                    interest)
                        };
                if enabled {
                    (|value_set: ::tracing::field::ValueSet|
                                {
                                    let meta = __CALLSITE.metadata();
                                    ::tracing::Event::dispatch(meta, &value_set);
                                    ;
                                })({
                            #[allow(unused_imports)]
                            use ::tracing::field::{debug, display, Value};
                            let mut iter = __CALLSITE.metadata().fields().iter();
                            __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                ::tracing::__macro_support::Option::Some(&debug(&generics.predicates)
                                                        as &dyn Value))])
                        });
                } else { ; }
            };
            let Some(param) =
                generics.params.iter().find(|param|
                        param.span == span) else { return; };
            let explicitly_sized =
                generics.bounds_for_param(param.def_id).flat_map(|bp|
                            bp.bounds).any(|bound|
                        bound.trait_ref().and_then(|tr| tr.trait_def_id()) ==
                            sized_trait);
            if explicitly_sized { return; }
            {
                use ::tracing::__macro_support::Callsite as _;
                static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                    {
                        static META: ::tracing::Metadata<'static> =
                            {
                                ::tracing_core::metadata::Metadata::new("event compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs:5634",
                                    "rustc_trait_selection::error_reporting::traits::suggestions",
                                    ::tracing::Level::DEBUG,
                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs"),
                                    ::tracing_core::__macro_support::Option::Some(5634u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_trait_selection::error_reporting::traits::suggestions"),
                                    ::tracing_core::field::FieldSet::new(&["param"],
                                        ::tracing_core::callsite::Identifier(&__CALLSITE)),
                                    ::tracing::metadata::Kind::EVENT)
                            };
                        ::tracing::callsite::DefaultCallsite::new(&META)
                    };
                let enabled =
                    ::tracing::Level::DEBUG <=
                                ::tracing::level_filters::STATIC_MAX_LEVEL &&
                            ::tracing::Level::DEBUG <=
                                ::tracing::level_filters::LevelFilter::current() &&
                        {
                            let interest = __CALLSITE.interest();
                            !interest.is_never() &&
                                ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                                    interest)
                        };
                if enabled {
                    (|value_set: ::tracing::field::ValueSet|
                                {
                                    let meta = __CALLSITE.metadata();
                                    ::tracing::Event::dispatch(meta, &value_set);
                                    ;
                                })({
                            #[allow(unused_imports)]
                            use ::tracing::field::{debug, display, Value};
                            let mut iter = __CALLSITE.metadata().fields().iter();
                            __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                ::tracing::__macro_support::Option::Some(&debug(&param) as
                                                        &dyn Value))])
                        });
                } else { ; }
            };
            match node {
                hir::Node::Item(item @ hir::Item {
                    kind: hir::ItemKind::Enum(..) | hir::ItemKind::Struct(..) |
                        hir::ItemKind::Union(..), .. }) => {
                    if self.suggest_indirection_for_unsized(err, item, param) {
                        return;
                    }
                }
                _ => {}
            };
            let (span, separator, open_paren_sp) =
                if let Some((s, open_paren_sp)) =
                        generics.bounds_span_for_suggestions(param.def_id) {
                    (s, " +", open_paren_sp)
                } else {
                    (param.name.ident().span.shrink_to_hi(), ":", None)
                };
            let mut suggs = ::alloc::vec::Vec::new();
            let suggestion =
                ::alloc::__export::must_use({
                        ::alloc::fmt::format(format_args!("{0} ?Sized", separator))
                    });
            if let Some(open_paren_sp) = open_paren_sp {
                suggs.push((open_paren_sp, "(".to_string()));
                suggs.push((span,
                        ::alloc::__export::must_use({
                                ::alloc::fmt::format(format_args!("){0}", suggestion))
                            })));
            } else { suggs.push((span, suggestion)); }
            err.multipart_suggestion("consider relaxing the implicit `Sized` restriction",
                suggs, Applicability::MachineApplicable);
        }
    }
}#[instrument(level = "debug", skip_all)]
5588    pub(super) fn suggest_unsized_bound_if_applicable(
5589        &self,
5590        err: &mut Diag<'_>,
5591        obligation: &PredicateObligation<'tcx>,
5592    ) {
5593        let ty::PredicateKind::Clause(ty::ClauseKind::Trait(pred)) =
5594            obligation.predicate.kind().skip_binder()
5595        else {
5596            return;
5597        };
5598        let (ObligationCauseCode::WhereClause(item_def_id, span)
5599        | ObligationCauseCode::WhereClauseInExpr(item_def_id, span, ..)) =
5600            *obligation.cause.code().peel_derives()
5601        else {
5602            return;
5603        };
5604        if span.is_dummy() {
5605            return;
5606        }
5607        debug!(?pred, ?item_def_id, ?span);
5608
5609        let (Some(node), true) = (
5610            self.tcx.hir_get_if_local(item_def_id),
5611            self.tcx.is_lang_item(pred.def_id(), LangItem::Sized),
5612        ) else {
5613            return;
5614        };
5615
5616        let Some(generics) = node.generics() else {
5617            return;
5618        };
5619        let sized_trait = self.tcx.lang_items().sized_trait();
5620        debug!(?generics.params);
5621        debug!(?generics.predicates);
5622        let Some(param) = generics.params.iter().find(|param| param.span == span) else {
5623            return;
5624        };
5625        // Check that none of the explicit trait bounds is `Sized`. Assume that an explicit
5626        // `Sized` bound is there intentionally and we don't need to suggest relaxing it.
5627        let explicitly_sized = generics
5628            .bounds_for_param(param.def_id)
5629            .flat_map(|bp| bp.bounds)
5630            .any(|bound| bound.trait_ref().and_then(|tr| tr.trait_def_id()) == sized_trait);
5631        if explicitly_sized {
5632            return;
5633        }
5634        debug!(?param);
5635        match node {
5636            hir::Node::Item(
5637                item @ hir::Item {
5638                    // Only suggest indirection for uses of type parameters in ADTs.
5639                    kind:
5640                        hir::ItemKind::Enum(..) | hir::ItemKind::Struct(..) | hir::ItemKind::Union(..),
5641                    ..
5642                },
5643            ) => {
5644                if self.suggest_indirection_for_unsized(err, item, param) {
5645                    return;
5646                }
5647            }
5648            _ => {}
5649        };
5650
5651        // Didn't add an indirection suggestion, so add a general suggestion to relax `Sized`.
5652        let (span, separator, open_paren_sp) =
5653            if let Some((s, open_paren_sp)) = generics.bounds_span_for_suggestions(param.def_id) {
5654                (s, " +", open_paren_sp)
5655            } else {
5656                (param.name.ident().span.shrink_to_hi(), ":", None)
5657            };
5658
5659        let mut suggs = vec![];
5660        let suggestion = format!("{separator} ?Sized");
5661
5662        if let Some(open_paren_sp) = open_paren_sp {
5663            suggs.push((open_paren_sp, "(".to_string()));
5664            suggs.push((span, format!("){suggestion}")));
5665        } else {
5666            suggs.push((span, suggestion));
5667        }
5668
5669        err.multipart_suggestion(
5670            "consider relaxing the implicit `Sized` restriction",
5671            suggs,
5672            Applicability::MachineApplicable,
5673        );
5674    }
5675
5676    fn suggest_indirection_for_unsized(
5677        &self,
5678        err: &mut Diag<'_>,
5679        item: &hir::Item<'tcx>,
5680        param: &hir::GenericParam<'tcx>,
5681    ) -> bool {
5682        // Suggesting `T: ?Sized` is only valid in an ADT if `T` is only used in a
5683        // borrow. `struct S<'a, T: ?Sized>(&'a T);` is valid, `struct S<T: ?Sized>(T);`
5684        // is not. Look for invalid "bare" parameter uses, and suggest using indirection.
5685        let mut visitor = FindTypeParam { param: param.name.ident().name, .. };
5686        visitor.visit_item(item);
5687        if visitor.invalid_spans.is_empty() {
5688            return false;
5689        }
5690        let mut multispan: MultiSpan = param.span.into();
5691        multispan.push_span_label(
5692            param.span,
5693            ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("this could be changed to `{0}: ?Sized`...",
                param.name.ident()))
    })format!("this could be changed to `{}: ?Sized`...", param.name.ident()),
5694        );
5695        for sp in visitor.invalid_spans {
5696            multispan.push_span_label(
5697                sp,
5698                ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("...if indirection were used here: `Box<{0}>`",
                param.name.ident()))
    })format!("...if indirection were used here: `Box<{}>`", param.name.ident()),
5699            );
5700        }
5701        err.span_help(
5702            multispan,
5703            ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("you could relax the implicit `Sized` bound on `{0}` if it were used through indirection like `&{0}` or `Box<{0}>`",
                param.name.ident()))
    })format!(
5704                "you could relax the implicit `Sized` bound on `{T}` if it were \
5705                used through indirection like `&{T}` or `Box<{T}>`",
5706                T = param.name.ident(),
5707            ),
5708        );
5709        true
5710    }
5711    pub(crate) fn suggest_swapping_lhs_and_rhs<T>(
5712        &self,
5713        err: &mut Diag<'_>,
5714        predicate: T,
5715        param_env: ty::ParamEnv<'tcx>,
5716        cause_code: &ObligationCauseCode<'tcx>,
5717    ) where
5718        T: Upcast<TyCtxt<'tcx>, ty::Predicate<'tcx>>,
5719    {
5720        let tcx = self.tcx;
5721        let predicate = predicate.upcast(tcx);
5722        match *cause_code {
5723            ObligationCauseCode::BinOp { lhs_hir_id, rhs_hir_id, rhs_span, .. }
5724                if let Some(typeck_results) = &self.typeck_results
5725                    && let hir::Node::Expr(lhs) = tcx.hir_node(lhs_hir_id)
5726                    && let hir::Node::Expr(rhs) = tcx.hir_node(rhs_hir_id)
5727                    && let Some(lhs_ty) = typeck_results.expr_ty_opt(lhs)
5728                    && let Some(rhs_ty) = typeck_results.expr_ty_opt(rhs) =>
5729            {
5730                if let Some(pred) = predicate.as_trait_clause()
5731                    && tcx.is_lang_item(pred.def_id(), LangItem::PartialEq)
5732                    && self
5733                        .infcx
5734                        .type_implements_trait(pred.def_id(), [rhs_ty, lhs_ty], param_env)
5735                        .must_apply_modulo_regions()
5736                {
5737                    let lhs_span = tcx.hir_span(lhs_hir_id);
5738                    let sm = tcx.sess.source_map();
5739                    if let Ok(rhs_snippet) = sm.span_to_snippet(rhs_span)
5740                        && let Ok(lhs_snippet) = sm.span_to_snippet(lhs_span)
5741                    {
5742                        err.note(::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("`{0}` implements `PartialEq<{1}>`",
                rhs_ty, lhs_ty))
    })format!("`{rhs_ty}` implements `PartialEq<{lhs_ty}>`"));
5743                        err.multipart_suggestion(
5744                            "consider swapping the equality",
5745                            ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [(lhs_span, rhs_snippet), (rhs_span, lhs_snippet)]))vec![(lhs_span, rhs_snippet), (rhs_span, lhs_snippet)],
5746                            Applicability::MaybeIncorrect,
5747                        );
5748                    }
5749                }
5750            }
5751            _ => {}
5752        }
5753    }
5754}
5755
5756/// Add a hint to add a missing borrow or remove an unnecessary one.
5757fn hint_missing_borrow<'tcx>(
5758    infcx: &InferCtxt<'tcx>,
5759    param_env: ty::ParamEnv<'tcx>,
5760    span: Span,
5761    found: Ty<'tcx>,
5762    expected: Ty<'tcx>,
5763    found_node: Node<'_>,
5764    err: &mut Diag<'_>,
5765) {
5766    if #[allow(non_exhaustive_omitted_patterns)] match found_node {
    Node::TraitItem(..) => true,
    _ => false,
}matches!(found_node, Node::TraitItem(..)) {
5767        return;
5768    }
5769
5770    let found_args = match found.kind() {
5771        ty::FnPtr(sig_tys, _) => infcx.enter_forall(*sig_tys, |sig_tys| sig_tys.inputs().iter()),
5772        kind => {
5773            ::rustc_middle::util::bug::span_bug_fmt(span,
    format_args!("found was converted to a FnPtr above but is now {0:?}",
        kind))span_bug!(span, "found was converted to a FnPtr above but is now {:?}", kind)
5774        }
5775    };
5776    let expected_args = match expected.kind() {
5777        ty::FnPtr(sig_tys, _) => infcx.enter_forall(*sig_tys, |sig_tys| sig_tys.inputs().iter()),
5778        kind => {
5779            ::rustc_middle::util::bug::span_bug_fmt(span,
    format_args!("expected was converted to a FnPtr above but is now {0:?}",
        kind))span_bug!(span, "expected was converted to a FnPtr above but is now {:?}", kind)
5780        }
5781    };
5782
5783    // This could be a variant constructor, for example.
5784    let Some(fn_decl) = found_node.fn_decl() else {
5785        return;
5786    };
5787
5788    let args = fn_decl.inputs.iter();
5789
5790    let mut to_borrow = Vec::new();
5791    let mut remove_borrow = Vec::new();
5792
5793    for ((found_arg, expected_arg), arg) in found_args.zip(expected_args).zip(args) {
5794        let (found_ty, found_refs) = get_deref_type_and_refs(*found_arg);
5795        let (expected_ty, expected_refs) = get_deref_type_and_refs(*expected_arg);
5796
5797        if infcx.can_eq(param_env, found_ty, expected_ty) {
5798            // FIXME: This could handle more exotic cases like mutability mismatches too!
5799            if found_refs.len() < expected_refs.len()
5800                && found_refs[..] == expected_refs[expected_refs.len() - found_refs.len()..]
5801            {
5802                to_borrow.push((
5803                    arg.span.shrink_to_lo(),
5804                    expected_refs[..expected_refs.len() - found_refs.len()]
5805                        .iter()
5806                        .map(|mutbl| ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("&{0}", mutbl.prefix_str()))
    })format!("&{}", mutbl.prefix_str()))
5807                        .collect::<Vec<_>>()
5808                        .join(""),
5809                ));
5810            } else if found_refs.len() > expected_refs.len() {
5811                let mut span = arg.span.shrink_to_lo();
5812                let mut left = found_refs.len() - expected_refs.len();
5813                let mut ty = arg;
5814                while let hir::TyKind::Ref(_, mut_ty) = &ty.kind
5815                    && left > 0
5816                {
5817                    span = span.with_hi(mut_ty.ty.span.lo());
5818                    ty = mut_ty.ty;
5819                    left -= 1;
5820                }
5821                if left == 0 {
5822                    remove_borrow.push((span, String::new()));
5823                }
5824            }
5825        }
5826    }
5827
5828    if !to_borrow.is_empty() {
5829        err.subdiagnostic(errors::AdjustSignatureBorrow::Borrow { to_borrow });
5830    }
5831
5832    if !remove_borrow.is_empty() {
5833        err.subdiagnostic(errors::AdjustSignatureBorrow::RemoveBorrow { remove_borrow });
5834    }
5835}
5836
5837/// Collect all the paths that reference `Self`.
5838/// Used to suggest replacing associated types with an explicit type in `where` clauses.
5839#[derive(#[automatically_derived]
impl<'v> ::core::fmt::Debug for SelfVisitor<'v> {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::debug_struct_field2_finish(f, "SelfVisitor",
            "paths", &self.paths, "name", &&self.name)
    }
}Debug)]
5840pub struct SelfVisitor<'v> {
5841    pub paths: Vec<&'v hir::Ty<'v>> = Vec::new(),
5842    pub name: Option<Symbol>,
5843}
5844
5845impl<'v> Visitor<'v> for SelfVisitor<'v> {
5846    fn visit_ty(&mut self, ty: &'v hir::Ty<'v, AmbigArg>) {
5847        if let hir::TyKind::Path(path) = ty.kind
5848            && let hir::QPath::TypeRelative(inner_ty, segment) = path
5849            && (Some(segment.ident.name) == self.name || self.name.is_none())
5850            && let hir::TyKind::Path(inner_path) = inner_ty.kind
5851            && let hir::QPath::Resolved(None, inner_path) = inner_path
5852            && let Res::SelfTyAlias { .. } = inner_path.res
5853        {
5854            self.paths.push(ty.as_unambig_ty());
5855        }
5856        hir::intravisit::walk_ty(self, ty);
5857    }
5858}
5859
5860/// Collect all the returned expressions within the input expression.
5861/// Used to point at the return spans when we want to suggest some change to them.
5862#[derive(#[automatically_derived]
impl<'v> ::core::default::Default for ReturnsVisitor<'v> {
    #[inline]
    fn default() -> ReturnsVisitor<'v> {
        ReturnsVisitor {
            returns: ::core::default::Default::default(),
            in_block_tail: ::core::default::Default::default(),
        }
    }
}Default)]
5863pub struct ReturnsVisitor<'v> {
5864    pub returns: Vec<&'v hir::Expr<'v>>,
5865    in_block_tail: bool,
5866}
5867
5868impl<'v> Visitor<'v> for ReturnsVisitor<'v> {
5869    fn visit_expr(&mut self, ex: &'v hir::Expr<'v>) {
5870        // Visit every expression to detect `return` paths, either through the function's tail
5871        // expression or `return` statements. We walk all nodes to find `return` statements, but
5872        // we only care about tail expressions when `in_block_tail` is `true`, which means that
5873        // they're in the return path of the function body.
5874        match ex.kind {
5875            hir::ExprKind::Ret(Some(ex)) => {
5876                self.returns.push(ex);
5877            }
5878            hir::ExprKind::Block(block, _) if self.in_block_tail => {
5879                self.in_block_tail = false;
5880                for stmt in block.stmts {
5881                    hir::intravisit::walk_stmt(self, stmt);
5882                }
5883                self.in_block_tail = true;
5884                if let Some(expr) = block.expr {
5885                    self.visit_expr(expr);
5886                }
5887            }
5888            hir::ExprKind::If(_, then, else_opt) if self.in_block_tail => {
5889                self.visit_expr(then);
5890                if let Some(el) = else_opt {
5891                    self.visit_expr(el);
5892                }
5893            }
5894            hir::ExprKind::Match(_, arms, _) if self.in_block_tail => {
5895                for arm in arms {
5896                    self.visit_expr(arm.body);
5897                }
5898            }
5899            // We need to walk to find `return`s in the entire body.
5900            _ if !self.in_block_tail => hir::intravisit::walk_expr(self, ex),
5901            _ => self.returns.push(ex),
5902        }
5903    }
5904
5905    fn visit_body(&mut self, body: &hir::Body<'v>) {
5906        if !!self.in_block_tail {
    ::core::panicking::panic("assertion failed: !self.in_block_tail")
};assert!(!self.in_block_tail);
5907        self.in_block_tail = true;
5908        hir::intravisit::walk_body(self, body);
5909    }
5910}
5911
5912/// Collect all the awaited expressions within the input expression.
5913#[derive(#[automatically_derived]
impl ::core::default::Default for AwaitsVisitor {
    #[inline]
    fn default() -> AwaitsVisitor {
        AwaitsVisitor { awaits: ::core::default::Default::default() }
    }
}Default)]
5914struct AwaitsVisitor {
5915    awaits: Vec<HirId>,
5916}
5917
5918impl<'v> Visitor<'v> for AwaitsVisitor {
5919    fn visit_expr(&mut self, ex: &'v hir::Expr<'v>) {
5920        if let hir::ExprKind::Yield(_, hir::YieldSource::Await { expr: Some(id) }) = ex.kind {
5921            self.awaits.push(id)
5922        }
5923        hir::intravisit::walk_expr(self, ex)
5924    }
5925}
5926
5927/// Suggest a new type parameter name for diagnostic purposes.
5928///
5929/// `name` is the preferred name you'd like to suggest if it's not in use already.
5930pub trait NextTypeParamName {
5931    fn next_type_param_name(&self, name: Option<&str>) -> String;
5932}
5933
5934impl NextTypeParamName for &[hir::GenericParam<'_>] {
5935    fn next_type_param_name(&self, name: Option<&str>) -> String {
5936        // Type names are usually single letters in uppercase. So convert the first letter of input string to uppercase.
5937        let name = name.and_then(|n| n.chars().next()).map(|c| c.to_uppercase().to_string());
5938        let name = name.as_deref();
5939
5940        // This is the list of possible parameter names that we might suggest.
5941        let possible_names = [name.unwrap_or("T"), "T", "U", "V", "X", "Y", "Z", "A", "B", "C"];
5942
5943        // Filter out used names based on `filter_fn`.
5944        let used_names: Vec<Symbol> = self
5945            .iter()
5946            .filter_map(|param| match param.name {
5947                hir::ParamName::Plain(ident) => Some(ident.name),
5948                _ => None,
5949            })
5950            .collect();
5951
5952        // Find a name from `possible_names` that is not in `used_names`.
5953        possible_names
5954            .iter()
5955            .find(|n| !used_names.contains(&Symbol::intern(n)))
5956            .unwrap_or(&"ParamName")
5957            .to_string()
5958    }
5959}
5960
5961/// Collect the spans that we see the generic param `param_did`
5962struct ReplaceImplTraitVisitor<'a> {
5963    ty_spans: &'a mut Vec<Span>,
5964    param_did: DefId,
5965}
5966
5967impl<'a, 'hir> hir::intravisit::Visitor<'hir> for ReplaceImplTraitVisitor<'a> {
5968    fn visit_ty(&mut self, t: &'hir hir::Ty<'hir, AmbigArg>) {
5969        if let hir::TyKind::Path(hir::QPath::Resolved(
5970            None,
5971            hir::Path { res: Res::Def(_, segment_did), .. },
5972        )) = t.kind
5973        {
5974            if self.param_did == *segment_did {
5975                // `fn foo(t: impl Trait)`
5976                //            ^^^^^^^^^^ get this to suggest `T` instead
5977
5978                // There might be more than one `impl Trait`.
5979                self.ty_spans.push(t.span);
5980                return;
5981            }
5982        }
5983
5984        hir::intravisit::walk_ty(self, t);
5985    }
5986}
5987
5988pub(super) fn get_explanation_based_on_obligation<'tcx>(
5989    tcx: TyCtxt<'tcx>,
5990    obligation: &PredicateObligation<'tcx>,
5991    trait_predicate: ty::PolyTraitPredicate<'tcx>,
5992    pre_message: String,
5993    long_ty_path: &mut Option<PathBuf>,
5994) -> String {
5995    if let ObligationCauseCode::MainFunctionType = obligation.cause.code() {
5996        "consider using `()`, or a `Result`".to_owned()
5997    } else {
5998        let ty_desc = match trait_predicate.self_ty().skip_binder().kind() {
5999            ty::FnDef(_, _) => Some("fn item"),
6000            ty::Closure(_, _) => Some("closure"),
6001            _ => None,
6002        };
6003
6004        let desc = match ty_desc {
6005            Some(desc) => ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!(" {0}", desc))
    })format!(" {desc}"),
6006            None => String::new(),
6007        };
6008        if let ty::PredicatePolarity::Positive = trait_predicate.polarity() {
6009            // If the trait in question is unstable, mention that fact in the diagnostic.
6010            // But if we're building with `-Zforce-unstable-if-unmarked` then _any_ trait
6011            // not explicitly marked stable is considered unstable, so the extra text is
6012            // unhelpful noise. See <https://github.com/rust-lang/rust/issues/152692>.
6013            let mention_unstable = !tcx.sess.opts.unstable_opts.force_unstable_if_unmarked
6014                && try { tcx.lookup_stability(trait_predicate.def_id())?.level.is_stable() }
6015                    == Some(false);
6016            let unstable = if mention_unstable { "nightly-only, unstable " } else { "" };
6017
6018            ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{2}the {3}trait `{0}` is not implemented for{4} `{1}`",
                trait_predicate.print_modifiers_and_trait_path(),
                tcx.short_string(trait_predicate.self_ty().skip_binder(),
                    long_ty_path), pre_message, unstable, desc))
    })format!(
6019                "{pre_message}the {unstable}trait `{}` is not implemented for{desc} `{}`",
6020                trait_predicate.print_modifiers_and_trait_path(),
6021                tcx.short_string(trait_predicate.self_ty().skip_binder(), long_ty_path),
6022            )
6023        } else {
6024            // "the trait bound `T: !Send` is not satisfied" reads better than "`!Send` is
6025            // not implemented for `T`".
6026            // FIXME: add note explaining explicit negative trait bounds.
6027            ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{0}the trait bound `{1}` is not satisfied",
                pre_message, trait_predicate))
    })format!("{pre_message}the trait bound `{trait_predicate}` is not satisfied")
6028        }
6029    }
6030}
6031
6032// Replace `param` with `replace_ty`
6033struct ReplaceImplTraitFolder<'tcx> {
6034    tcx: TyCtxt<'tcx>,
6035    param: &'tcx ty::GenericParamDef,
6036    replace_ty: Ty<'tcx>,
6037}
6038
6039impl<'tcx> TypeFolder<TyCtxt<'tcx>> for ReplaceImplTraitFolder<'tcx> {
6040    fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> {
6041        if let ty::Param(ty::ParamTy { index, .. }) = t.kind() {
6042            if self.param.index == *index {
6043                return self.replace_ty;
6044            }
6045        }
6046        t.super_fold_with(self)
6047    }
6048
6049    fn cx(&self) -> TyCtxt<'tcx> {
6050        self.tcx
6051    }
6052}
6053
6054pub fn suggest_desugaring_async_fn_to_impl_future_in_trait<'tcx>(
6055    tcx: TyCtxt<'tcx>,
6056    sig: hir::FnSig<'tcx>,
6057    body: hir::TraitFn<'tcx>,
6058    opaque_def_id: LocalDefId,
6059    add_bounds: &str,
6060) -> Option<Vec<(Span, String)>> {
6061    let hir::IsAsync::Async(async_span) = sig.header.asyncness else {
6062        return None;
6063    };
6064    let async_span = tcx.sess.source_map().span_extend_while_whitespace(async_span);
6065
6066    let future = tcx.hir_node_by_def_id(opaque_def_id).expect_opaque_ty();
6067    let [hir::GenericBound::Trait(trait_ref)] = future.bounds else {
6068        // `async fn` should always lower to a single bound... but don't ICE.
6069        return None;
6070    };
6071    let Some(hir::PathSegment { args: Some(args), .. }) = trait_ref.trait_ref.path.segments.last()
6072    else {
6073        // desugaring to a single path segment for `Future<...>`.
6074        return None;
6075    };
6076    let Some(future_output_ty) = args.constraints.first().and_then(|constraint| constraint.ty())
6077    else {
6078        // Also should never happen.
6079        return None;
6080    };
6081
6082    let mut sugg = if future_output_ty.span.is_empty() {
6083        ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [(async_span, String::new()),
                (future_output_ty.span,
                    ::alloc::__export::must_use({
                            ::alloc::fmt::format(format_args!(" -> impl std::future::Future<Output = ()>{0}",
                                    add_bounds))
                        }))]))vec![
6084            (async_span, String::new()),
6085            (
6086                future_output_ty.span,
6087                format!(" -> impl std::future::Future<Output = ()>{add_bounds}"),
6088            ),
6089        ]
6090    } else {
6091        ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [(future_output_ty.span.shrink_to_lo(),
                    "impl std::future::Future<Output = ".to_owned()),
                (future_output_ty.span.shrink_to_hi(),
                    ::alloc::__export::must_use({
                            ::alloc::fmt::format(format_args!(">{0}", add_bounds))
                        })), (async_span, String::new())]))vec![
6092            (future_output_ty.span.shrink_to_lo(), "impl std::future::Future<Output = ".to_owned()),
6093            (future_output_ty.span.shrink_to_hi(), format!(">{add_bounds}")),
6094            (async_span, String::new()),
6095        ]
6096    };
6097
6098    // If there's a body, we also need to wrap it in `async {}`
6099    if let hir::TraitFn::Provided(body) = body {
6100        let body = tcx.hir_body(body);
6101        let body_span = body.value.span;
6102        let body_span_without_braces =
6103            body_span.with_lo(body_span.lo() + BytePos(1)).with_hi(body_span.hi() - BytePos(1));
6104        if body_span_without_braces.is_empty() {
6105            sugg.push((body_span_without_braces, " async {} ".to_owned()));
6106        } else {
6107            sugg.extend([
6108                (body_span_without_braces.shrink_to_lo(), "async {".to_owned()),
6109                (body_span_without_braces.shrink_to_hi(), "} ".to_owned()),
6110            ]);
6111        }
6112    }
6113
6114    Some(sugg)
6115}
6116
6117/// On `impl` evaluation cycles, look for `Self::AssocTy` restrictions in `where` clauses, explain
6118/// they are not allowed and if possible suggest alternatives.
6119fn point_at_assoc_type_restriction<G: EmissionGuarantee>(
6120    tcx: TyCtxt<'_>,
6121    err: &mut Diag<'_, G>,
6122    self_ty_str: &str,
6123    trait_name: &str,
6124    predicate: ty::Predicate<'_>,
6125    generics: &hir::Generics<'_>,
6126    data: &ImplDerivedCause<'_>,
6127) {
6128    let ty::PredicateKind::Clause(clause) = predicate.kind().skip_binder() else {
6129        return;
6130    };
6131    let ty::ClauseKind::Projection(proj) = clause else {
6132        return;
6133    };
6134    let Some(name) = tcx
6135        .opt_rpitit_info(proj.projection_term.def_id)
6136        .and_then(|data| match data {
6137            ty::ImplTraitInTraitData::Trait { fn_def_id, .. } => Some(tcx.item_name(fn_def_id)),
6138            ty::ImplTraitInTraitData::Impl { .. } => None,
6139        })
6140        .or_else(|| tcx.opt_item_name(proj.projection_term.def_id))
6141    else {
6142        return;
6143    };
6144    let mut predicates = generics.predicates.iter().peekable();
6145    let mut prev: Option<(&hir::WhereBoundPredicate<'_>, Span)> = None;
6146    while let Some(pred) = predicates.next() {
6147        let curr_span = pred.span;
6148        let hir::WherePredicateKind::BoundPredicate(pred) = pred.kind else {
6149            continue;
6150        };
6151        let mut bounds = pred.bounds.iter();
6152        while let Some(bound) = bounds.next() {
6153            let Some(trait_ref) = bound.trait_ref() else {
6154                continue;
6155            };
6156            if bound.span() != data.span {
6157                continue;
6158            }
6159            if let hir::TyKind::Path(path) = pred.bounded_ty.kind
6160                && let hir::QPath::TypeRelative(ty, segment) = path
6161                && segment.ident.name == name
6162                && let hir::TyKind::Path(inner_path) = ty.kind
6163                && let hir::QPath::Resolved(None, inner_path) = inner_path
6164                && let Res::SelfTyAlias { .. } = inner_path.res
6165            {
6166                // The following block is to determine the right span to delete for this bound
6167                // that will leave valid code after the suggestion is applied.
6168                let span = if pred.origin == hir::PredicateOrigin::WhereClause
6169                    && generics
6170                        .predicates
6171                        .iter()
6172                        .filter(|p| {
6173                            #[allow(non_exhaustive_omitted_patterns)] match p.kind {
    hir::WherePredicateKind::BoundPredicate(p) if
        hir::PredicateOrigin::WhereClause == p.origin => true,
    _ => false,
}matches!(
6174                                p.kind,
6175                                hir::WherePredicateKind::BoundPredicate(p)
6176                                if hir::PredicateOrigin::WhereClause == p.origin
6177                            )
6178                        })
6179                        .count()
6180                        == 1
6181                {
6182                    // There's only one `where` bound, that needs to be removed. Remove the whole
6183                    // `where` clause.
6184                    generics.where_clause_span
6185                } else if let Some(next_pred) = predicates.peek()
6186                    && let hir::WherePredicateKind::BoundPredicate(next) = next_pred.kind
6187                    && pred.origin == next.origin
6188                {
6189                    // There's another bound, include the comma for the current one.
6190                    curr_span.until(next_pred.span)
6191                } else if let Some((prev, prev_span)) = prev
6192                    && pred.origin == prev.origin
6193                {
6194                    // Last bound, try to remove the previous comma.
6195                    prev_span.shrink_to_hi().to(curr_span)
6196                } else if pred.origin == hir::PredicateOrigin::WhereClause {
6197                    curr_span.with_hi(generics.where_clause_span.hi())
6198                } else {
6199                    curr_span
6200                };
6201
6202                err.span_suggestion_verbose(
6203                    span,
6204                    "associated type for the current `impl` cannot be restricted in `where` \
6205                     clauses, remove this bound",
6206                    "",
6207                    Applicability::MaybeIncorrect,
6208                );
6209            }
6210            if let Some(new) =
6211                tcx.associated_items(data.impl_or_alias_def_id).find_by_ident_and_kind(
6212                    tcx,
6213                    Ident::with_dummy_span(name),
6214                    ty::AssocTag::Type,
6215                    data.impl_or_alias_def_id,
6216                )
6217            {
6218                // The associated type is specified in the `impl` we're
6219                // looking at. Point at it.
6220                let span = tcx.def_span(new.def_id);
6221                err.span_label(
6222                    span,
6223                    ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("associated type `<{0} as {1}>::{2}` is specified here",
                self_ty_str, trait_name, name))
    })format!(
6224                        "associated type `<{self_ty_str} as {trait_name}>::{name}` is specified \
6225                         here",
6226                    ),
6227                );
6228                // Search for the associated type `Self::{name}`, get
6229                // its type and suggest replacing the bound with it.
6230                let mut visitor = SelfVisitor { name: Some(name), .. };
6231                visitor.visit_trait_ref(trait_ref);
6232                for path in visitor.paths {
6233                    err.span_suggestion_verbose(
6234                        path.span,
6235                        "replace the associated type with the type specified in this `impl`",
6236                        tcx.type_of(new.def_id).skip_binder(),
6237                        Applicability::MachineApplicable,
6238                    );
6239                }
6240            } else {
6241                let mut visitor = SelfVisitor { name: None, .. };
6242                visitor.visit_trait_ref(trait_ref);
6243                let span: MultiSpan =
6244                    visitor.paths.iter().map(|p| p.span).collect::<Vec<Span>>().into();
6245                err.span_note(
6246                    span,
6247                    "associated types for the current `impl` cannot be restricted in `where` \
6248                     clauses",
6249                );
6250            }
6251        }
6252        prev = Some((pred, curr_span));
6253    }
6254}
6255
6256fn get_deref_type_and_refs(mut ty: Ty<'_>) -> (Ty<'_>, Vec<hir::Mutability>) {
6257    let mut refs = ::alloc::vec::Vec::new()vec![];
6258
6259    while let ty::Ref(_, new_ty, mutbl) = ty.kind() {
6260        ty = *new_ty;
6261        refs.push(*mutbl);
6262    }
6263
6264    (ty, refs)
6265}
6266
6267/// Look for type `param` in an ADT being used only through a reference to confirm that suggesting
6268/// `param: ?Sized` would be a valid constraint.
6269struct FindTypeParam {
6270    param: rustc_span::Symbol,
6271    invalid_spans: Vec<Span> = Vec::new(),
6272    nested: bool = false,
6273}
6274
6275impl<'v> Visitor<'v> for FindTypeParam {
6276    fn visit_where_predicate(&mut self, _: &'v hir::WherePredicate<'v>) {
6277        // Skip where-clauses, to avoid suggesting indirection for type parameters found there.
6278    }
6279
6280    fn visit_ty(&mut self, ty: &hir::Ty<'_, AmbigArg>) {
6281        // We collect the spans of all uses of the "bare" type param, like in `field: T` or
6282        // `field: (T, T)` where we could make `T: ?Sized` while skipping cases that are known to be
6283        // valid like `field: &'a T` or `field: *mut T` and cases that *might* have further `Sized`
6284        // obligations like `Box<T>` and `Vec<T>`, but we perform no extra analysis for those cases
6285        // and suggest `T: ?Sized` regardless of their obligations. This is fine because the errors
6286        // in that case should make what happened clear enough.
6287        match ty.kind {
6288            hir::TyKind::Ptr(_) | hir::TyKind::Ref(..) | hir::TyKind::TraitObject(..) => {}
6289            hir::TyKind::Path(hir::QPath::Resolved(None, path))
6290                if let [segment] = path.segments
6291                    && segment.ident.name == self.param =>
6292            {
6293                if !self.nested {
6294                    {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs:6294",
                        "rustc_trait_selection::error_reporting::traits::suggestions",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs"),
                        ::tracing_core::__macro_support::Option::Some(6294u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_trait_selection::error_reporting::traits::suggestions"),
                        ::tracing_core::field::FieldSet::new(&["message", "ty"],
                            ::tracing_core::callsite::Identifier(&__CALLSITE)),
                        ::tracing::metadata::Kind::EVENT)
                };
            ::tracing::callsite::DefaultCallsite::new(&META)
        };
    let enabled =
        ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() &&
            {
                let interest = __CALLSITE.interest();
                !interest.is_never() &&
                    ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                        interest)
            };
    if enabled {
        (|value_set: ::tracing::field::ValueSet|
                    {
                        let meta = __CALLSITE.metadata();
                        ::tracing::Event::dispatch(meta, &value_set);
                        ;
                    })({
                #[allow(unused_imports)]
                use ::tracing::field::{debug, display, Value};
                let mut iter = __CALLSITE.metadata().fields().iter();
                __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&format_args!("FindTypeParam::visit_ty")
                                            as &dyn Value)),
                                (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&debug(&ty) as
                                            &dyn Value))])
            });
    } else { ; }
};debug!(?ty, "FindTypeParam::visit_ty");
6295                    self.invalid_spans.push(ty.span);
6296                }
6297            }
6298            hir::TyKind::Path(_) => {
6299                let prev = self.nested;
6300                self.nested = true;
6301                hir::intravisit::walk_ty(self, ty);
6302                self.nested = prev;
6303            }
6304            _ => {
6305                hir::intravisit::walk_ty(self, ty);
6306            }
6307        }
6308    }
6309}
6310
6311/// Look for type parameters in predicates. We use this to identify whether a bound is suitable in
6312/// on a given item.
6313struct ParamFinder {
6314    params: Vec<Symbol> = Vec::new(),
6315}
6316
6317impl<'tcx> TypeVisitor<TyCtxt<'tcx>> for ParamFinder {
6318    fn visit_ty(&mut self, t: Ty<'tcx>) -> Self::Result {
6319        match t.kind() {
6320            ty::Param(p) => self.params.push(p.name),
6321            _ => {}
6322        }
6323        t.super_visit_with(self)
6324    }
6325}
6326
6327impl ParamFinder {
6328    /// Whether the `hir::Generics` of the current item can suggest the evaluated bound because its
6329    /// references to type parameters are present in the generics.
6330    fn can_suggest_bound(&self, generics: &hir::Generics<'_>) -> bool {
6331        if self.params.is_empty() {
6332            // There are no references to type parameters at all, so suggesting the bound
6333            // would be reasonable.
6334            return true;
6335        }
6336        generics.params.iter().any(|p| match p.name {
6337            hir::ParamName::Plain(p_name) => {
6338                // All of the parameters in the bound can be referenced in the current item.
6339                self.params.iter().any(|p| *p == p_name.name || *p == kw::SelfUpper)
6340            }
6341            _ => true,
6342        })
6343    }
6344}