Skip to main content

rustc_trait_selection/error_reporting/traits/
suggestions.rs

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