Skip to main content

rustc_trait_selection/error_reporting/infer/
mod.rs

1//! Error Reporting Code for the inference engine
2//!
3//! Because of the way inference, and in particular region inference,
4//! works, it often happens that errors are not detected until far after
5//! the relevant line of code has been type-checked. Therefore, there is
6//! an elaborate system to track why a particular constraint in the
7//! inference graph arose so that we can explain to the user what gave
8//! rise to a particular error.
9//!
10//! The system is based around a set of "origin" types. An "origin" is the
11//! reason that a constraint or inference variable arose. There are
12//! different "origin" enums for different kinds of constraints/variables
13//! (e.g., `TypeOrigin`, `RegionVariableOrigin`). An origin always has
14//! a span, but also more information so that we can generate a meaningful
15//! error message.
16//!
17//! Having a catalog of all the different reasons an error can arise is
18//! also useful for other reasons, like cross-referencing FAQs etc, though
19//! we are not really taking advantage of this yet.
20//!
21//! # Region Inference
22//!
23//! Region inference is particularly tricky because it always succeeds "in
24//! the moment" and simply registers a constraint. Then, at the end, we
25//! can compute the full graph and report errors, so we need to be able to
26//! store and later report what gave rise to the conflicting constraints.
27//!
28//! # Subtype Trace
29//!
30//! Determining whether `T1 <: T2` often involves a number of subtypes and
31//! subconstraints along the way. A "TypeTrace" is an extended version
32//! of an origin that traces the types and other values that were being
33//! compared. It is not necessarily comprehensive (in fact, at the time of
34//! this writing it only tracks the root values being compared) but I'd
35//! like to extend it to include significant "waypoints". For example, if
36//! you are comparing `(T1, T2) <: (T3, T4)`, and the problem is that `T2
37//! <: T4` fails, I'd like the trace to include enough information to say
38//! "in the 2nd element of the tuple". Similarly, failures when comparing
39//! arguments or return types in fn types should be able to cite the
40//! specific position, etc.
41//!
42//! # Reality vs plan
43//!
44//! Of course, there is still a LOT of code in typeck that has yet to be
45//! ported to this system, and which relies on string concatenation at the
46//! time of error detection.
47
48use std::borrow::Cow;
49use std::ops::ControlFlow;
50use std::path::PathBuf;
51use std::{cmp, fmt, iter};
52
53use rustc_abi::ExternAbi;
54use rustc_data_structures::fx::{FxIndexMap, FxIndexSet};
55use rustc_errors::{Applicability, Diag, DiagStyledString, IntoDiagArg, StringPart, pluralize};
56use rustc_hir::def::DefKind;
57use rustc_hir::def_id::DefId;
58use rustc_hir::intravisit::Visitor;
59use rustc_hir::lang_items::LangItem;
60use rustc_hir::{self as hir};
61use rustc_infer::infer::DefineOpaqueTypes;
62use rustc_macros::extension;
63use rustc_middle::bug;
64use rustc_middle::traits::PatternOriginExpr;
65use rustc_middle::ty::error::{ExpectedFound, TypeError, TypeErrorToStringExt};
66use rustc_middle::ty::print::{PrintTraitRefExt as _, WrapBinderMode, with_forced_trimmed_paths};
67use rustc_middle::ty::{
68    self, List, ParamEnv, Region, Ty, TyCtxt, TypeFoldable, TypeSuperVisitable, TypeVisitable,
69    TypeVisitableExt,
70};
71use rustc_span::{BytePos, DUMMY_SP, DesugaringKind, Pos, Span, sym};
72use tracing::{debug, instrument};
73
74use crate::error_reporting::TypeErrCtxt;
75use crate::error_reporting::traits::ambiguity::{
76    CandidateSource, compute_applicable_impls_for_diagnostics,
77};
78use crate::errors::{ObligationCauseFailureCode, TypeErrorAdditionalDiags};
79use crate::infer;
80use crate::infer::relate::{self, RelateResult, TypeRelation};
81use crate::infer::{InferCtxt, InferCtxtExt as _, TypeTrace, ValuePairs};
82use crate::solve::deeply_normalize_for_diagnostics;
83use crate::traits::{
84    MatchExpressionArmCause, Obligation, ObligationCause, ObligationCauseCode, specialization_graph,
85};
86
87mod note_and_explain;
88mod suggest;
89
90pub mod need_type_info;
91pub mod nice_region_error;
92pub mod region;
93
94/// Makes a valid string literal from a string by escaping special characters (" and \),
95/// unless they are already escaped.
96fn escape_literal(s: &str) -> String {
97    let mut escaped = String::with_capacity(s.len());
98    let mut chrs = s.chars().peekable();
99    while let Some(first) = chrs.next() {
100        match (first, chrs.peek()) {
101            ('\\', Some(&delim @ '"') | Some(&delim @ '\'')) => {
102                escaped.push('\\');
103                escaped.push(delim);
104                chrs.next();
105            }
106            ('"' | '\'', _) => {
107                escaped.push('\\');
108                escaped.push(first)
109            }
110            (c, _) => escaped.push(c),
111        };
112    }
113    escaped
114}
115
116impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
117    // [Note-Type-error-reporting]
118    // An invariant is that anytime the expected or actual type is Error (the special
119    // error type, meaning that an error occurred when typechecking this expression),
120    // this is a derived error. The error cascaded from another error (that was already
121    // reported), so it's not useful to display it to the user.
122    // The following methods implement this logic.
123    // They check if either the actual or expected type is Error, and don't print the error
124    // in this case. The typechecker should only ever report type errors involving mismatched
125    // types using one of these methods, and should not call span_err directly for such
126    // errors.
127    pub fn type_error_struct_with_diag<M>(
128        &self,
129        sp: Span,
130        mk_diag: M,
131        actual_ty: Ty<'tcx>,
132    ) -> Diag<'a>
133    where
134        M: FnOnce(String) -> Diag<'a>,
135    {
136        let actual_ty = self.resolve_vars_if_possible(actual_ty);
137        {
    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/infer/mod.rs:137",
                        "rustc_trait_selection::error_reporting::infer",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/error_reporting/infer/mod.rs"),
                        ::tracing_core::__macro_support::Option::Some(137u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_trait_selection::error_reporting::infer"),
                        ::tracing_core::field::FieldSet::new(&["message"],
                            ::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!("type_error_struct_with_diag({0:?}, {1:?})",
                                                    sp, actual_ty) as &dyn Value))])
            });
    } else { ; }
};debug!("type_error_struct_with_diag({:?}, {:?})", sp, actual_ty);
138
139        let mut err = mk_diag(self.ty_to_string(actual_ty));
140
141        // Don't report an error if actual type is `Error`.
142        if actual_ty.references_error() {
143            err.downgrade_to_delayed_bug();
144        }
145
146        err
147    }
148
149    pub fn report_mismatched_types(
150        &self,
151        cause: &ObligationCause<'tcx>,
152        param_env: ty::ParamEnv<'tcx>,
153        expected: Ty<'tcx>,
154        actual: Ty<'tcx>,
155        err: TypeError<'tcx>,
156    ) -> Diag<'a> {
157        let mut diag = self.report_and_explain_type_error(
158            TypeTrace::types(cause, expected, actual),
159            param_env,
160            err,
161        );
162
163        self.suggest_param_env_shadowing(&mut diag, expected, actual, param_env);
164
165        diag
166    }
167
168    pub fn report_mismatched_consts(
169        &self,
170        cause: &ObligationCause<'tcx>,
171        param_env: ty::ParamEnv<'tcx>,
172        expected: ty::Const<'tcx>,
173        actual: ty::Const<'tcx>,
174        err: TypeError<'tcx>,
175    ) -> Diag<'a> {
176        self.report_and_explain_type_error(
177            TypeTrace::consts(cause, expected, actual),
178            param_env,
179            err,
180        )
181    }
182
183    pub fn get_impl_future_output_ty(&self, ty: Ty<'tcx>) -> Option<Ty<'tcx>> {
184        let (def_id, args) = match *ty.kind() {
185            ty::Alias(_, ty::AliasTy { def_id, args, .. })
186                if self.tcx.def_kind(def_id) == DefKind::OpaqueTy =>
187            {
188                (def_id, args)
189            }
190            ty::Alias(_, ty::AliasTy { def_id, args, .. })
191                if self.tcx.is_impl_trait_in_trait(def_id) =>
192            {
193                (def_id, args)
194            }
195            _ => return None,
196        };
197
198        let future_trait = self.tcx.require_lang_item(LangItem::Future, DUMMY_SP);
199        let item_def_id = self.tcx.associated_item_def_ids(future_trait)[0];
200
201        self.tcx
202            .explicit_item_self_bounds(def_id)
203            .iter_instantiated_copied(self.tcx, args)
204            .find_map(|(predicate, _)| {
205                predicate
206                    .kind()
207                    .map_bound(|kind| match kind {
208                        ty::ClauseKind::Projection(projection_predicate)
209                            if projection_predicate.projection_term.def_id == item_def_id =>
210                        {
211                            projection_predicate.term.as_type()
212                        }
213                        _ => None,
214                    })
215                    .no_bound_vars()
216                    .flatten()
217            })
218    }
219
220    /// Adds a note if the types come from similarly named crates
221    fn check_and_note_conflicting_crates(&self, err: &mut Diag<'_>, terr: TypeError<'tcx>) -> bool {
222        match terr {
223            TypeError::Sorts(ref exp_found) => {
224                // if they are both "path types", there's a chance of ambiguity
225                // due to different versions of the same crate
226                if let (&ty::Adt(exp_adt, _), &ty::Adt(found_adt, _)) =
227                    (exp_found.expected.kind(), exp_found.found.kind())
228                {
229                    return self.check_same_definition_different_crate(
230                        err,
231                        exp_adt.did(),
232                        [found_adt.did()].into_iter(),
233                        |did| ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [self.tcx.def_span(did)]))vec![self.tcx.def_span(did)],
234                        "type",
235                    );
236                }
237            }
238            TypeError::Traits(ref exp_found) => {
239                return self.check_same_definition_different_crate(
240                    err,
241                    exp_found.expected,
242                    [exp_found.found].into_iter(),
243                    |did| ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [self.tcx.def_span(did)]))vec![self.tcx.def_span(did)],
244                    "trait",
245                );
246            }
247            _ => (), // FIXME(#22750) handle traits and stuff
248        }
249        false
250    }
251
252    fn suggest_param_env_shadowing(
253        &self,
254        diag: &mut Diag<'_>,
255        expected: Ty<'tcx>,
256        found: Ty<'tcx>,
257        param_env: ty::ParamEnv<'tcx>,
258    ) {
259        let (alias, concrete) = match (expected.kind(), found.kind()) {
260            (ty::Alias(ty::Projection, proj), _) => (proj, found),
261            (_, ty::Alias(ty::Projection, proj)) => (proj, expected),
262            _ => return,
263        };
264
265        let tcx = self.tcx;
266
267        let trait_ref = alias.trait_ref(tcx);
268        let obligation =
269            Obligation::new(tcx, ObligationCause::dummy(), param_env, ty::Binder::dummy(trait_ref));
270
271        let applicable_impls = compute_applicable_impls_for_diagnostics(self.infcx, &obligation);
272
273        for candidate in applicable_impls {
274            let impl_def_id = match candidate {
275                CandidateSource::DefId(did) => did,
276                CandidateSource::ParamEnv(_) => continue,
277            };
278
279            let is_shadowed = self.infcx.probe(|_| {
280                let impl_substs = self.infcx.fresh_args_for_item(DUMMY_SP, impl_def_id);
281                let impl_trait_ref = tcx.impl_trait_ref(impl_def_id).instantiate(tcx, impl_substs);
282
283                let expected_trait_ref = alias.trait_ref(tcx);
284
285                if let Err(_) = self.infcx.at(&ObligationCause::dummy(), param_env).eq(
286                    DefineOpaqueTypes::No,
287                    expected_trait_ref,
288                    impl_trait_ref,
289                ) {
290                    return false;
291                }
292
293                let leaf_def = match specialization_graph::assoc_def(tcx, impl_def_id, alias.def_id)
294                {
295                    Ok(leaf) => leaf,
296                    Err(_) => return false,
297                };
298
299                let trait_def_id = alias.trait_def_id(tcx);
300                let rebased_args = alias.args.rebase_onto(tcx, trait_def_id, impl_substs);
301
302                // The impl is erroneous missing a definition for the associated type.
303                // Skipping it since calling `TyCtxt::type_of` on its assoc ty will trigger an ICE.
304                if !leaf_def.item.defaultness(tcx).has_value() {
305                    return false;
306                }
307
308                let impl_item_def_id = leaf_def.item.def_id;
309                if !tcx.check_args_compatible(impl_item_def_id, rebased_args) {
310                    return false;
311                }
312                let impl_assoc_ty = tcx.type_of(impl_item_def_id).instantiate(tcx, rebased_args);
313
314                self.infcx.can_eq(param_env, impl_assoc_ty, concrete)
315            });
316
317            if is_shadowed {
318                diag.note(::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("the associated type `{0}` is defined as `{1}` in the implementation, but the where-bound `{2}` shadows this definition\nsee issue #152409 <https://github.com/rust-lang/rust/issues/152409> for more information",
                self.ty_to_string(tcx.mk_ty_from_kind(ty::Alias(ty::Projection,
                            *alias))), self.ty_to_string(concrete),
                self.ty_to_string(alias.self_ty())))
    })format!(
319                    "the associated type `{}` is defined as `{}` in the implementation, \
320                    but the where-bound `{}` shadows this definition\n\
321                    see issue #152409 <https://github.com/rust-lang/rust/issues/152409> for more information",
322                    self.ty_to_string(tcx.mk_ty_from_kind(ty::Alias(ty::Projection, *alias))),
323                    self.ty_to_string(concrete),
324                    self.ty_to_string(alias.self_ty())
325                ));
326                return;
327            }
328        }
329    }
330
331    fn note_error_origin(
332        &self,
333        err: &mut Diag<'_>,
334        cause: &ObligationCause<'tcx>,
335        exp_found: Option<ty::error::ExpectedFound<Ty<'tcx>>>,
336        terr: TypeError<'tcx>,
337        param_env: Option<ParamEnv<'tcx>>,
338    ) {
339        match *cause.code() {
340            ObligationCauseCode::Pattern {
341                origin_expr: Some(origin_expr),
342                span: Some(span),
343                root_ty,
344            } => {
345                let expected_ty = self.resolve_vars_if_possible(root_ty);
346                if !#[allow(non_exhaustive_omitted_patterns)] match expected_ty.kind() {
    ty::Infer(ty::InferTy::TyVar(_) | ty::InferTy::FreshTy(_)) => true,
    _ => false,
}matches!(
347                    expected_ty.kind(),
348                    ty::Infer(ty::InferTy::TyVar(_) | ty::InferTy::FreshTy(_))
349                ) {
350                    // don't show type `_`
351                    if span.desugaring_kind() == Some(DesugaringKind::ForLoop)
352                        && let ty::Adt(def, args) = expected_ty.kind()
353                        && Some(def.did()) == self.tcx.get_diagnostic_item(sym::Option)
354                    {
355                        err.span_label(
356                            span,
357                            ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("this is an iterator with items of type `{0}`",
                args.type_at(0)))
    })format!("this is an iterator with items of type `{}`", args.type_at(0)),
358                        );
359                    } else if !span.overlaps(cause.span) {
360                        let expected_ty = self.tcx.short_string(expected_ty, err.long_ty_path());
361                        err.span_label(span, ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("this expression has type `{0}`",
                expected_ty))
    })format!("this expression has type `{expected_ty}`"));
362                    }
363                }
364                if let Some(ty::error::ExpectedFound { found, .. }) = exp_found
365                    && let Ok(mut peeled_snippet) =
366                        self.tcx.sess.source_map().span_to_snippet(origin_expr.peeled_span)
367                {
368                    // Parentheses are needed for cases like as casts.
369                    // We use the peeled_span for deref suggestions.
370                    // It's also safe to use for box, since box only triggers if there
371                    // wasn't a reference to begin with.
372                    if origin_expr.peeled_prefix_suggestion_parentheses {
373                        peeled_snippet = ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("({0})", peeled_snippet))
    })format!("({peeled_snippet})");
374                    }
375
376                    // Try giving a box suggestion first, as it is a special case of the
377                    // deref suggestion.
378                    if expected_ty.boxed_ty() == Some(found) {
379                        err.span_suggestion_verbose(
380                            span,
381                            "consider dereferencing the boxed value",
382                            ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("*{0}", peeled_snippet))
    })format!("*{peeled_snippet}"),
383                            Applicability::MachineApplicable,
384                        );
385                    } else if let Some(param_env) = param_env
386                        && let Some(prefix) = self.should_deref_suggestion_on_mismatch(
387                            param_env,
388                            found,
389                            expected_ty,
390                            origin_expr,
391                        )
392                    {
393                        err.span_suggestion_verbose(
394                            span,
395                            "consider dereferencing to access the inner value using the Deref trait",
396                            ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{0}{1}", prefix, peeled_snippet))
    })format!("{prefix}{peeled_snippet}"),
397                            Applicability::MaybeIncorrect,
398                        );
399                    }
400                }
401            }
402            ObligationCauseCode::Pattern { origin_expr: None, span: Some(span), .. } => {
403                err.span_label(span, "expected due to this");
404            }
405            ObligationCauseCode::BlockTailExpression(
406                _,
407                hir::MatchSource::TryDesugar(scrut_hir_id),
408            ) => {
409                if let Some(ty::error::ExpectedFound { expected, .. }) = exp_found {
410                    let scrut_expr = self.tcx.hir_expect_expr(scrut_hir_id);
411                    let scrut_ty = if let hir::ExprKind::Call(_, args) = &scrut_expr.kind {
412                        let arg_expr = args.first().expect("try desugaring call w/out arg");
413                        self.typeck_results
414                            .as_ref()
415                            .and_then(|typeck_results| typeck_results.expr_ty_opt(arg_expr))
416                    } else {
417                        ::rustc_middle::util::bug::bug_fmt(format_args!("try desugaring w/out call expr as scrutinee"));bug!("try desugaring w/out call expr as scrutinee");
418                    };
419
420                    match scrut_ty {
421                        Some(ty) if expected == ty => {
422                            let source_map = self.tcx.sess.source_map();
423                            err.span_suggestion(
424                                source_map.end_point(cause.span),
425                                "try removing this `?`",
426                                "",
427                                Applicability::MachineApplicable,
428                            );
429                        }
430                        _ => {}
431                    }
432                }
433            }
434            ObligationCauseCode::MatchExpressionArm(box MatchExpressionArmCause {
435                arm_block_id,
436                arm_span,
437                arm_ty,
438                prior_arm_block_id,
439                prior_arm_span,
440                prior_arm_ty,
441                source,
442                ref prior_non_diverging_arms,
443                scrut_span,
444                expr_span,
445                ..
446            }) => match source {
447                hir::MatchSource::TryDesugar(scrut_hir_id) => {
448                    if let Some(ty::error::ExpectedFound { expected, .. }) = exp_found {
449                        let scrut_expr = self.tcx.hir_expect_expr(scrut_hir_id);
450                        let scrut_ty = if let hir::ExprKind::Call(_, args) = &scrut_expr.kind {
451                            let arg_expr = args.first().expect("try desugaring call w/out arg");
452                            self.typeck_results
453                                .as_ref()
454                                .and_then(|typeck_results| typeck_results.expr_ty_opt(arg_expr))
455                        } else {
456                            ::rustc_middle::util::bug::bug_fmt(format_args!("try desugaring w/out call expr as scrutinee"));bug!("try desugaring w/out call expr as scrutinee");
457                        };
458
459                        match scrut_ty {
460                            Some(ty) if expected == ty => {
461                                let source_map = self.tcx.sess.source_map();
462                                err.span_suggestion(
463                                    source_map.end_point(cause.span),
464                                    "try removing this `?`",
465                                    "",
466                                    Applicability::MachineApplicable,
467                                );
468                            }
469                            _ => {}
470                        }
471                    }
472                }
473                _ => {
474                    // `prior_arm_ty` can be `!`, `expected` will have better info when present.
475                    let t = self.resolve_vars_if_possible(match exp_found {
476                        Some(ty::error::ExpectedFound { expected, .. }) => expected,
477                        _ => prior_arm_ty,
478                    });
479                    let source_map = self.tcx.sess.source_map();
480                    let mut any_multiline_arm = source_map.is_multiline(arm_span);
481                    if prior_non_diverging_arms.len() <= 4 {
482                        for sp in prior_non_diverging_arms {
483                            any_multiline_arm |= source_map.is_multiline(*sp);
484                            err.span_label(*sp, ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("this is found to be of type `{0}`",
                t))
    })format!("this is found to be of type `{t}`"));
485                        }
486                    } else if let Some(sp) = prior_non_diverging_arms.last() {
487                        any_multiline_arm |= source_map.is_multiline(*sp);
488                        err.span_label(
489                            *sp,
490                            ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("this and all prior arms are found to be of type `{0}`",
                t))
    })format!("this and all prior arms are found to be of type `{t}`"),
491                        );
492                    }
493                    let outer = if any_multiline_arm || !source_map.is_multiline(expr_span) {
494                        // Cover just `match` and the scrutinee expression, not
495                        // the entire match body, to reduce diagram noise.
496                        expr_span.shrink_to_lo().to(scrut_span)
497                    } else {
498                        expr_span
499                    };
500                    let msg = "`match` arms have incompatible types";
501                    err.span_label(outer, msg);
502                    if let Some(subdiag) = self.suggest_remove_semi_or_return_binding(
503                        prior_arm_block_id,
504                        prior_arm_ty,
505                        prior_arm_span,
506                        arm_block_id,
507                        arm_ty,
508                        arm_span,
509                    ) {
510                        err.subdiagnostic(subdiag);
511                    }
512                }
513            },
514            ObligationCauseCode::IfExpression { expr_id, .. } => {
515                let hir::Node::Expr(&hir::Expr {
516                    kind: hir::ExprKind::If(cond_expr, then_expr, Some(else_expr)),
517                    span: expr_span,
518                    ..
519                }) = self.tcx.hir_node(expr_id)
520                else {
521                    return;
522                };
523                let then_span = self.find_block_span_from_hir_id(then_expr.hir_id);
524                let then_ty = self
525                    .typeck_results
526                    .as_ref()
527                    .expect("if expression only expected inside FnCtxt")
528                    .expr_ty(then_expr);
529                let else_span = self.find_block_span_from_hir_id(else_expr.hir_id);
530                let else_ty = self
531                    .typeck_results
532                    .as_ref()
533                    .expect("if expression only expected inside FnCtxt")
534                    .expr_ty(else_expr);
535                if let hir::ExprKind::If(_cond, _then, None) = else_expr.kind
536                    && else_ty.is_unit()
537                {
538                    // Account for `let x = if a { 1 } else if b { 2 };`
539                    err.note("`if` expressions without `else` evaluate to `()`");
540                    err.note("consider adding an `else` block that evaluates to the expected type");
541                }
542                err.span_label(then_span, "expected because of this");
543
544                let outer_span = if self.tcx.sess.source_map().is_multiline(expr_span) {
545                    if then_span.hi() == expr_span.hi() || else_span.hi() == expr_span.hi() {
546                        // Point at condition only if either block has the same end point as
547                        // the whole expression, since that'll cause awkward overlapping spans.
548                        Some(expr_span.shrink_to_lo().to(cond_expr.peel_drop_temps().span))
549                    } else {
550                        Some(expr_span)
551                    }
552                } else {
553                    None
554                };
555                if let Some(sp) = outer_span {
556                    err.span_label(sp, "`if` and `else` have incompatible types");
557                }
558
559                let then_id = if let hir::ExprKind::Block(then_blk, _) = then_expr.kind {
560                    then_blk.hir_id
561                } else {
562                    then_expr.hir_id
563                };
564                let else_id = if let hir::ExprKind::Block(else_blk, _) = else_expr.kind {
565                    else_blk.hir_id
566                } else {
567                    else_expr.hir_id
568                };
569                if let Some(subdiag) = self.suggest_remove_semi_or_return_binding(
570                    Some(then_id),
571                    then_ty,
572                    then_span,
573                    Some(else_id),
574                    else_ty,
575                    else_span,
576                ) {
577                    err.subdiagnostic(subdiag);
578                }
579            }
580            ObligationCauseCode::LetElse => {
581                err.help("try adding a diverging expression, such as `return` or `panic!(..)`");
582                err.help("...or use `match` instead of `let...else`");
583            }
584            _ => {
585                if let ObligationCauseCode::WhereClause(_, span)
586                | ObligationCauseCode::WhereClauseInExpr(_, span, ..) =
587                    cause.code().peel_derives()
588                    && !span.is_dummy()
589                    && let TypeError::RegionsPlaceholderMismatch = terr
590                {
591                    err.span_note(*span, "the lifetime requirement is introduced here");
592                }
593            }
594        }
595    }
596
597    /// Determines whether deref_to == <deref_from as Deref>::Target, and if so,
598    /// returns a prefix that should be added to deref_from as a suggestion.
599    fn should_deref_suggestion_on_mismatch(
600        &self,
601        param_env: ParamEnv<'tcx>,
602        deref_to: Ty<'tcx>,
603        deref_from: Ty<'tcx>,
604        origin_expr: PatternOriginExpr,
605    ) -> Option<String> {
606        // origin_expr contains stripped away versions of our expression.
607        // We'll want to use that to avoid suggesting things like *&x.
608        // However, the type that we have access to hasn't been stripped away,
609        // so we need to ignore the first n dereferences, where n is the number
610        // that's been stripped away in origin_expr.
611
612        // Find a way to autoderef from deref_from to deref_to.
613        let Some((num_derefs, (after_deref_ty, _))) = (self.autoderef_steps)(deref_from)
614            .into_iter()
615            .enumerate()
616            .find(|(_, (ty, _))| self.infcx.can_eq(param_env, *ty, deref_to))
617        else {
618            return None;
619        };
620
621        if num_derefs <= origin_expr.peeled_count {
622            return None;
623        }
624
625        let deref_part = "*".repeat(num_derefs - origin_expr.peeled_count);
626
627        // If the user used a reference in the original expression, they probably
628        // want the suggestion to still give a reference.
629        if deref_from.is_ref() && !after_deref_ty.is_ref() {
630            Some(::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("&{0}", deref_part))
    })format!("&{deref_part}"))
631        } else {
632            Some(deref_part)
633        }
634    }
635
636    /// Given that `other_ty` is the same as a type argument for `name` in `sub`, populate `value`
637    /// highlighting `name` and every type argument that isn't at `pos` (which is `other_ty`), and
638    /// populate `other_value` with `other_ty`.
639    ///
640    /// ```text
641    /// Foo<Bar<Qux>>
642    /// ^^^^--------^ this is highlighted
643    /// |   |
644    /// |   this type argument is exactly the same as the other type, not highlighted
645    /// this is highlighted
646    /// Bar<Qux>
647    /// -------- this type is the same as a type argument in the other type, not highlighted
648    /// ```
649    fn highlight_outer(
650        &self,
651        value: &mut DiagStyledString,
652        other_value: &mut DiagStyledString,
653        name: String,
654        args: &[ty::GenericArg<'tcx>],
655        pos: usize,
656        other_ty: Ty<'tcx>,
657    ) {
658        // `value` and `other_value` hold two incomplete type representation for display.
659        // `name` is the path of both types being compared. `sub`
660        value.push_highlighted(name);
661
662        if args.is_empty() {
663            return;
664        }
665        value.push_highlighted("<");
666
667        for (i, arg) in args.iter().enumerate() {
668            if i > 0 {
669                value.push_normal(", ");
670            }
671
672            match arg.kind() {
673                ty::GenericArgKind::Lifetime(lt) => {
674                    let s = lt.to_string();
675                    value.push_normal(if s.is_empty() { "'_" } else { &s });
676                }
677                ty::GenericArgKind::Const(ct) => {
678                    value.push_normal(ct.to_string());
679                }
680                // Highlight all the type arguments that aren't at `pos` and compare
681                // the type argument at `pos` and `other_ty`.
682                ty::GenericArgKind::Type(type_arg) => {
683                    if i == pos {
684                        let values = self.cmp(type_arg, other_ty);
685                        value.0.extend((values.0).0);
686                        other_value.0.extend((values.1).0);
687                    } else {
688                        value.push_highlighted(type_arg.to_string());
689                    }
690                }
691            }
692        }
693
694        value.push_highlighted(">");
695    }
696
697    /// If `other_ty` is the same as a type argument present in `sub`, highlight `path` in `t1_out`,
698    /// as that is the difference to the other type.
699    ///
700    /// For the following code:
701    ///
702    /// ```ignore (illustrative)
703    /// let x: Foo<Bar<Qux>> = foo::<Bar<Qux>>();
704    /// ```
705    ///
706    /// The type error output will behave in the following way:
707    ///
708    /// ```text
709    /// Foo<Bar<Qux>>
710    /// ^^^^--------^ this is highlighted
711    /// |   |
712    /// |   this type argument is exactly the same as the other type, not highlighted
713    /// this is highlighted
714    /// Bar<Qux>
715    /// -------- this type is the same as a type argument in the other type, not highlighted
716    /// ```
717    fn cmp_type_arg(
718        &self,
719        t1_out: &mut DiagStyledString,
720        t2_out: &mut DiagStyledString,
721        path: String,
722        args: &'tcx [ty::GenericArg<'tcx>],
723        other_path: String,
724        other_ty: Ty<'tcx>,
725    ) -> bool {
726        for (i, arg) in args.iter().enumerate() {
727            if let Some(ta) = arg.as_type() {
728                if ta == other_ty {
729                    self.highlight_outer(t1_out, t2_out, path, args, i, other_ty);
730                    return true;
731                }
732                if let ty::Adt(def, _) = ta.kind() {
733                    let path_ = self.tcx.def_path_str(def.did());
734                    if path_ == other_path {
735                        self.highlight_outer(t1_out, t2_out, path, args, i, other_ty);
736                        return true;
737                    }
738                }
739            }
740        }
741        false
742    }
743
744    /// Adds a `,` to the type representation only if it is appropriate.
745    fn push_comma(
746        &self,
747        value: &mut DiagStyledString,
748        other_value: &mut DiagStyledString,
749        pos: usize,
750    ) {
751        if pos > 0 {
752            value.push_normal(", ");
753            other_value.push_normal(", ");
754        }
755    }
756
757    /// Given two `fn` signatures highlight only sub-parts that are different.
758    fn cmp_fn_sig(
759        &self,
760        sig1: &ty::PolyFnSig<'tcx>,
761        fn_def1: Option<(DefId, Option<&'tcx [ty::GenericArg<'tcx>]>)>,
762        sig2: &ty::PolyFnSig<'tcx>,
763        fn_def2: Option<(DefId, Option<&'tcx [ty::GenericArg<'tcx>]>)>,
764    ) -> (DiagStyledString, DiagStyledString) {
765        let sig1 = &(self.normalize_fn_sig)(*sig1);
766        let sig2 = &(self.normalize_fn_sig)(*sig2);
767
768        let get_lifetimes = |sig| {
769            use rustc_hir::def::Namespace;
770            let (sig, reg) = ty::print::FmtPrinter::new(self.tcx, Namespace::TypeNS)
771                .name_all_regions(sig, WrapBinderMode::ForAll)
772                .unwrap();
773            let lts: Vec<String> =
774                reg.into_items().map(|(_, kind)| kind.to_string()).into_sorted_stable_ord();
775            (if lts.is_empty() { String::new() } else { ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("for<{0}> ", lts.join(", ")))
    })format!("for<{}> ", lts.join(", ")) }, sig)
776        };
777
778        let (lt1, sig1) = get_lifetimes(sig1);
779        let (lt2, sig2) = get_lifetimes(sig2);
780
781        // unsafe extern "C" for<'a> fn(&'a T) -> &'a T
782        let mut values =
783            (DiagStyledString::normal("".to_string()), DiagStyledString::normal("".to_string()));
784
785        // unsafe extern "C" for<'a> fn(&'a T) -> &'a T
786        // ^^^^^^
787        let safety = |fn_def, sig: ty::FnSig<'_>| match fn_def {
788            None => sig.safety.prefix_str(),
789            Some((did, _)) => {
790                if self.tcx.codegen_fn_attrs(did).safe_target_features {
791                    "#[target_features] "
792                } else {
793                    sig.safety.prefix_str()
794                }
795            }
796        };
797        let safety1 = safety(fn_def1, sig1);
798        let safety2 = safety(fn_def2, sig2);
799        values.0.push(safety1, safety1 != safety2);
800        values.1.push(safety2, safety1 != safety2);
801
802        // unsafe extern "C" for<'a> fn(&'a T) -> &'a T
803        //        ^^^^^^^^^^
804        if sig1.abi != ExternAbi::Rust {
805            values.0.push(::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("extern {0} ", sig1.abi))
    })format!("extern {} ", sig1.abi), sig1.abi != sig2.abi);
806        }
807        if sig2.abi != ExternAbi::Rust {
808            values.1.push(::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("extern {0} ", sig2.abi))
    })format!("extern {} ", sig2.abi), sig1.abi != sig2.abi);
809        }
810
811        // unsafe extern "C" for<'a> fn(&'a T) -> &'a T
812        //                   ^^^^^^^^
813        let lifetime_diff = lt1 != lt2;
814        values.0.push(lt1, lifetime_diff);
815        values.1.push(lt2, lifetime_diff);
816
817        // unsafe extern "C" for<'a> fn(&'a T) -> &'a T
818        //                           ^^^
819        values.0.push_normal("fn(");
820        values.1.push_normal("fn(");
821
822        // unsafe extern "C" for<'a> fn(&'a T) -> &'a T
823        //                              ^^^^^
824        let len1 = sig1.inputs().len();
825        let len2 = sig2.inputs().len();
826        if len1 == len2 {
827            for (i, (l, r)) in iter::zip(sig1.inputs(), sig2.inputs()).enumerate() {
828                self.push_comma(&mut values.0, &mut values.1, i);
829                let (x1, x2) = self.cmp(*l, *r);
830                (values.0).0.extend(x1.0);
831                (values.1).0.extend(x2.0);
832            }
833        } else {
834            for (i, l) in sig1.inputs().iter().enumerate() {
835                values.0.push_highlighted(l.to_string());
836                if i != len1 - 1 {
837                    values.0.push_highlighted(", ");
838                }
839            }
840            for (i, r) in sig2.inputs().iter().enumerate() {
841                values.1.push_highlighted(r.to_string());
842                if i != len2 - 1 {
843                    values.1.push_highlighted(", ");
844                }
845            }
846        }
847
848        if sig1.c_variadic {
849            if len1 > 0 {
850                values.0.push_normal(", ");
851            }
852            values.0.push("...", !sig2.c_variadic);
853        }
854        if sig2.c_variadic {
855            if len2 > 0 {
856                values.1.push_normal(", ");
857            }
858            values.1.push("...", !sig1.c_variadic);
859        }
860
861        // unsafe extern "C" for<'a> fn(&'a T) -> &'a T
862        //                                   ^
863        values.0.push_normal(")");
864        values.1.push_normal(")");
865
866        // unsafe extern "C" for<'a> fn(&'a T) -> &'a T
867        //                                     ^^^^^^^^
868        let output1 = sig1.output();
869        let output2 = sig2.output();
870        let (x1, x2) = self.cmp(output1, output2);
871        let output_diff = x1 != x2;
872        if !output1.is_unit() || output_diff {
873            values.0.push_normal(" -> ");
874            (values.0).0.extend(x1.0);
875        }
876        if !output2.is_unit() || output_diff {
877            values.1.push_normal(" -> ");
878            (values.1).0.extend(x2.0);
879        }
880
881        let fmt = |did, args| ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!(" {{{0}}}",
                self.tcx.def_path_str_with_args(did, args)))
    })format!(" {{{}}}", self.tcx.def_path_str_with_args(did, args));
882
883        match (fn_def1, fn_def2) {
884            (Some((fn_def1, Some(fn_args1))), Some((fn_def2, Some(fn_args2)))) => {
885                let path1 = fmt(fn_def1, fn_args1);
886                let path2 = fmt(fn_def2, fn_args2);
887                let same_path = path1 == path2;
888                values.0.push(path1, !same_path);
889                values.1.push(path2, !same_path);
890            }
891            (Some((fn_def1, Some(fn_args1))), None) => {
892                values.0.push_highlighted(fmt(fn_def1, fn_args1));
893            }
894            (None, Some((fn_def2, Some(fn_args2)))) => {
895                values.1.push_highlighted(fmt(fn_def2, fn_args2));
896            }
897            _ => {}
898        }
899
900        values
901    }
902
903    pub fn cmp_traits(
904        &self,
905        def_id1: DefId,
906        args1: &[ty::GenericArg<'tcx>],
907        def_id2: DefId,
908        args2: &[ty::GenericArg<'tcx>],
909    ) -> (DiagStyledString, DiagStyledString) {
910        let mut values = (DiagStyledString::new(), DiagStyledString::new());
911
912        if def_id1 != def_id2 {
913            values.0.push_highlighted(self.tcx.def_path_str(def_id1).as_str());
914            values.1.push_highlighted(self.tcx.def_path_str(def_id2).as_str());
915        } else {
916            values.0.push_normal(self.tcx.item_name(def_id1).as_str());
917            values.1.push_normal(self.tcx.item_name(def_id2).as_str());
918        }
919
920        if args1.len() != args2.len() {
921            let (pre, post) = if args1.len() > 0 { ("<", ">") } else { ("", "") };
922            values.0.push_normal(::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{1}{0}{2}",
                args1.iter().map(|a|
                                a.to_string()).collect::<Vec<_>>().join(", "), pre, post))
    })format!(
923                "{pre}{}{post}",
924                args1.iter().map(|a| a.to_string()).collect::<Vec<_>>().join(", ")
925            ));
926            let (pre, post) = if args2.len() > 0 { ("<", ">") } else { ("", "") };
927            values.1.push_normal(::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{1}{0}{2}",
                args2.iter().map(|a|
                                a.to_string()).collect::<Vec<_>>().join(", "), pre, post))
    })format!(
928                "{pre}{}{post}",
929                args2.iter().map(|a| a.to_string()).collect::<Vec<_>>().join(", ")
930            ));
931            return values;
932        }
933
934        if args1.len() > 0 {
935            values.0.push_normal("<");
936            values.1.push_normal("<");
937        }
938        for (i, (a, b)) in std::iter::zip(args1, args2).enumerate() {
939            let a_str = a.to_string();
940            let b_str = b.to_string();
941            if let (Some(a), Some(b)) = (a.as_type(), b.as_type()) {
942                let (a, b) = self.cmp(a, b);
943                values.0.0.extend(a.0);
944                values.1.0.extend(b.0);
945            } else if a_str != b_str {
946                values.0.push_highlighted(a_str);
947                values.1.push_highlighted(b_str);
948            } else {
949                values.0.push_normal(a_str);
950                values.1.push_normal(b_str);
951            }
952            if i + 1 < args1.len() {
953                values.0.push_normal(", ");
954                values.1.push_normal(", ");
955            }
956        }
957        if args1.len() > 0 {
958            values.0.push_normal(">");
959            values.1.push_normal(">");
960        }
961        values
962    }
963
964    /// Compares two given types, eliding parts that are the same between them and highlighting
965    /// relevant differences, and return two representation of those types for highlighted printing.
966    pub fn cmp(&self, t1: Ty<'tcx>, t2: Ty<'tcx>) -> (DiagStyledString, DiagStyledString) {
967        {
    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/infer/mod.rs:967",
                        "rustc_trait_selection::error_reporting::infer",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/error_reporting/infer/mod.rs"),
                        ::tracing_core::__macro_support::Option::Some(967u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_trait_selection::error_reporting::infer"),
                        ::tracing_core::field::FieldSet::new(&["message"],
                            ::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!("cmp(t1={0}, t1.kind={1:?}, t2={2}, t2.kind={3:?})",
                                                    t1, t1.kind(), t2, t2.kind()) as &dyn Value))])
            });
    } else { ; }
};debug!("cmp(t1={}, t1.kind={:?}, t2={}, t2.kind={:?})", t1, t1.kind(), t2, t2.kind());
968
969        // helper functions
970        let recurse = |t1, t2, values: &mut (DiagStyledString, DiagStyledString)| {
971            let (x1, x2) = self.cmp(t1, t2);
972            (values.0).0.extend(x1.0);
973            (values.1).0.extend(x2.0);
974        };
975
976        fn fmt_region<'tcx>(region: ty::Region<'tcx>) -> String {
977            let mut r = region.to_string();
978            if r == "'_" {
979                r.clear();
980            } else {
981                r.push(' ');
982            }
983            ::alloc::__export::must_use({ ::alloc::fmt::format(format_args!("&{0}", r)) })format!("&{r}")
984        }
985
986        fn push_ref<'tcx>(
987            region: ty::Region<'tcx>,
988            mutbl: hir::Mutability,
989            s: &mut DiagStyledString,
990        ) {
991            s.push_highlighted(fmt_region(region));
992            s.push_highlighted(mutbl.prefix_str());
993        }
994
995        fn maybe_highlight<T: Eq + ToString>(
996            t1: T,
997            t2: T,
998            (buf1, buf2): &mut (DiagStyledString, DiagStyledString),
999            tcx: TyCtxt<'_>,
1000        ) {
1001            let highlight = t1 != t2;
1002            let (t1, t2) = if highlight || tcx.sess.opts.verbose {
1003                (t1.to_string(), t2.to_string())
1004            } else {
1005                // The two types are the same, elide and don't highlight.
1006                ("_".into(), "_".into())
1007            };
1008            buf1.push(t1, highlight);
1009            buf2.push(t2, highlight);
1010        }
1011
1012        fn cmp_ty_refs<'tcx>(
1013            r1: ty::Region<'tcx>,
1014            mut1: hir::Mutability,
1015            r2: ty::Region<'tcx>,
1016            mut2: hir::Mutability,
1017            ss: &mut (DiagStyledString, DiagStyledString),
1018        ) {
1019            let (r1, r2) = (fmt_region(r1), fmt_region(r2));
1020            if r1 != r2 {
1021                ss.0.push_highlighted(r1);
1022                ss.1.push_highlighted(r2);
1023            } else {
1024                ss.0.push_normal(r1);
1025                ss.1.push_normal(r2);
1026            }
1027
1028            if mut1 != mut2 {
1029                ss.0.push_highlighted(mut1.prefix_str());
1030                ss.1.push_highlighted(mut2.prefix_str());
1031            } else {
1032                ss.0.push_normal(mut1.prefix_str());
1033                ss.1.push_normal(mut2.prefix_str());
1034            }
1035        }
1036
1037        // process starts here
1038        match (t1.kind(), t2.kind()) {
1039            (&ty::Adt(def1, sub1), &ty::Adt(def2, sub2)) => {
1040                let did1 = def1.did();
1041                let did2 = def2.did();
1042
1043                let generics1 = self.tcx.generics_of(did1);
1044                let generics2 = self.tcx.generics_of(did2);
1045
1046                let non_default_after_default = generics1
1047                    .check_concrete_type_after_default(self.tcx, sub1)
1048                    || generics2.check_concrete_type_after_default(self.tcx, sub2);
1049                let sub_no_defaults_1 = if non_default_after_default {
1050                    generics1.own_args(sub1)
1051                } else {
1052                    generics1.own_args_no_defaults(self.tcx, sub1)
1053                };
1054                let sub_no_defaults_2 = if non_default_after_default {
1055                    generics2.own_args(sub2)
1056                } else {
1057                    generics2.own_args_no_defaults(self.tcx, sub2)
1058                };
1059                let mut values = (DiagStyledString::new(), DiagStyledString::new());
1060                let path1 = self.tcx.def_path_str(did1);
1061                let path2 = self.tcx.def_path_str(did2);
1062                if did1 == did2 {
1063                    // Easy case. Replace same types with `_` to shorten the output and highlight
1064                    // the differing ones.
1065                    //     let x: Foo<Bar, Qux> = y::<Foo<Quz, Qux>>();
1066                    //     Foo<Bar, _>
1067                    //     Foo<Quz, _>
1068                    //         ---  ^ type argument elided
1069                    //         |
1070                    //         highlighted in output
1071                    values.0.push_normal(path1);
1072                    values.1.push_normal(path2);
1073
1074                    // Avoid printing out default generic parameters that are common to both
1075                    // types.
1076                    let len1 = sub_no_defaults_1.len();
1077                    let len2 = sub_no_defaults_2.len();
1078                    let common_len = cmp::min(len1, len2);
1079                    let remainder1 = &sub1[common_len..];
1080                    let remainder2 = &sub2[common_len..];
1081                    let common_default_params =
1082                        iter::zip(remainder1.iter().rev(), remainder2.iter().rev())
1083                            .filter(|(a, b)| a == b)
1084                            .count();
1085                    let len = sub1.len() - common_default_params;
1086
1087                    // Only draw `<...>` if there are lifetime/type arguments.
1088                    if len > 0 {
1089                        values.0.push_normal("<");
1090                        values.1.push_normal("<");
1091                    }
1092
1093                    fn lifetime_display(lifetime: Region<'_>) -> String {
1094                        let s = lifetime.to_string();
1095                        if s.is_empty() { "'_".to_string() } else { s }
1096                    }
1097
1098                    for (i, (arg1, arg2)) in sub1.iter().zip(sub2).enumerate().take(len) {
1099                        self.push_comma(&mut values.0, &mut values.1, i);
1100                        match arg1.kind() {
1101                            // At one point we'd like to elide all lifetimes here, they are
1102                            // irrelevant for all diagnostics that use this output.
1103                            //
1104                            //     Foo<'x, '_, Bar>
1105                            //     Foo<'y, '_, Qux>
1106                            //         ^^  ^^  --- type arguments are not elided
1107                            //         |   |
1108                            //         |   elided as they were the same
1109                            //         not elided, they were different, but irrelevant
1110                            //
1111                            // For bound lifetimes, keep the names of the lifetimes,
1112                            // even if they are the same so that it's clear what's happening
1113                            // if we have something like
1114                            //
1115                            // for<'r, 's> fn(Inv<'r>, Inv<'s>)
1116                            // for<'r> fn(Inv<'r>, Inv<'r>)
1117                            ty::GenericArgKind::Lifetime(l1) => {
1118                                let l1_str = lifetime_display(l1);
1119                                let l2 = arg2.expect_region();
1120                                let l2_str = lifetime_display(l2);
1121                                if l1 != l2 {
1122                                    values.0.push_highlighted(l1_str);
1123                                    values.1.push_highlighted(l2_str);
1124                                } else if l1.is_bound() || self.tcx.sess.opts.verbose {
1125                                    values.0.push_normal(l1_str);
1126                                    values.1.push_normal(l2_str);
1127                                } else {
1128                                    values.0.push_normal("'_");
1129                                    values.1.push_normal("'_");
1130                                }
1131                            }
1132                            ty::GenericArgKind::Type(ta1) => {
1133                                let ta2 = arg2.expect_ty();
1134                                if ta1 == ta2 && !self.tcx.sess.opts.verbose {
1135                                    values.0.push_normal("_");
1136                                    values.1.push_normal("_");
1137                                } else {
1138                                    recurse(ta1, ta2, &mut values);
1139                                }
1140                            }
1141                            // We're comparing two types with the same path, so we compare the type
1142                            // arguments for both. If they are the same, do not highlight and elide
1143                            // from the output.
1144                            //     Foo<_, Bar>
1145                            //     Foo<_, Qux>
1146                            //         ^ elided type as this type argument was the same in both sides
1147
1148                            // Do the same for const arguments, if they are equal, do not highlight and
1149                            // elide them from the output.
1150                            ty::GenericArgKind::Const(ca1) => {
1151                                let ca2 = arg2.expect_const();
1152                                maybe_highlight(ca1, ca2, &mut values, self.tcx);
1153                            }
1154                        }
1155                    }
1156
1157                    // Close the type argument bracket.
1158                    // Only draw `<...>` if there are arguments.
1159                    if len > 0 {
1160                        values.0.push_normal(">");
1161                        values.1.push_normal(">");
1162                    }
1163                    values
1164                } else {
1165                    // Check for case:
1166                    //     let x: Foo<Bar<Qux> = foo::<Bar<Qux>>();
1167                    //     Foo<Bar<Qux>
1168                    //         ------- this type argument is exactly the same as the other type
1169                    //     Bar<Qux>
1170                    if self.cmp_type_arg(
1171                        &mut values.0,
1172                        &mut values.1,
1173                        path1.clone(),
1174                        sub_no_defaults_1,
1175                        path2.clone(),
1176                        t2,
1177                    ) {
1178                        return values;
1179                    }
1180                    // Check for case:
1181                    //     let x: Bar<Qux> = y:<Foo<Bar<Qux>>>();
1182                    //     Bar<Qux>
1183                    //     Foo<Bar<Qux>>
1184                    //         ------- this type argument is exactly the same as the other type
1185                    if self.cmp_type_arg(
1186                        &mut values.1,
1187                        &mut values.0,
1188                        path2,
1189                        sub_no_defaults_2,
1190                        path1,
1191                        t1,
1192                    ) {
1193                        return values;
1194                    }
1195
1196                    // We can't find anything in common, highlight relevant part of type path.
1197                    //     let x: foo::bar::Baz<Qux> = y:<foo::bar::Bar<Zar>>();
1198                    //     foo::bar::Baz<Qux>
1199                    //     foo::bar::Bar<Zar>
1200                    //               -------- this part of the path is different
1201
1202                    let t1_str = t1.to_string();
1203                    let t2_str = t2.to_string();
1204                    let min_len = t1_str.len().min(t2_str.len());
1205
1206                    const SEPARATOR: &str = "::";
1207                    let separator_len = SEPARATOR.len();
1208                    let split_idx: usize =
1209                        iter::zip(t1_str.split(SEPARATOR), t2_str.split(SEPARATOR))
1210                            .take_while(|(mod1_str, mod2_str)| mod1_str == mod2_str)
1211                            .map(|(mod_str, _)| mod_str.len() + separator_len)
1212                            .sum();
1213
1214                    {
    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/infer/mod.rs:1214",
                        "rustc_trait_selection::error_reporting::infer",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/error_reporting/infer/mod.rs"),
                        ::tracing_core::__macro_support::Option::Some(1214u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_trait_selection::error_reporting::infer"),
                        ::tracing_core::field::FieldSet::new(&["message",
                                        "separator_len", "split_idx", "min_len"],
                            ::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!("cmp")
                                            as &dyn Value)),
                                (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&debug(&separator_len)
                                            as &dyn Value)),
                                (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&debug(&split_idx)
                                            as &dyn Value)),
                                (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&debug(&min_len) as
                                            &dyn Value))])
            });
    } else { ; }
};debug!(?separator_len, ?split_idx, ?min_len, "cmp");
1215
1216                    if split_idx >= min_len {
1217                        // paths are identical, highlight everything
1218                        (
1219                            DiagStyledString::highlighted(t1_str),
1220                            DiagStyledString::highlighted(t2_str),
1221                        )
1222                    } else {
1223                        let (common, uniq1) = t1_str.split_at(split_idx);
1224                        let (_, uniq2) = t2_str.split_at(split_idx);
1225                        {
    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/infer/mod.rs:1225",
                        "rustc_trait_selection::error_reporting::infer",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/error_reporting/infer/mod.rs"),
                        ::tracing_core::__macro_support::Option::Some(1225u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_trait_selection::error_reporting::infer"),
                        ::tracing_core::field::FieldSet::new(&["message", "common",
                                        "uniq1", "uniq2"],
                            ::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!("cmp")
                                            as &dyn Value)),
                                (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&debug(&common) as
                                            &dyn Value)),
                                (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&debug(&uniq1) as
                                            &dyn Value)),
                                (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&debug(&uniq2) as
                                            &dyn Value))])
            });
    } else { ; }
};debug!(?common, ?uniq1, ?uniq2, "cmp");
1226
1227                        values.0.push_normal(common);
1228                        values.0.push_highlighted(uniq1);
1229                        values.1.push_normal(common);
1230                        values.1.push_highlighted(uniq2);
1231
1232                        values
1233                    }
1234                }
1235            }
1236
1237            // When finding `&T != &T`, compare the references, then recurse into pointee type
1238            (&ty::Ref(r1, ref_ty1, mutbl1), &ty::Ref(r2, ref_ty2, mutbl2)) => {
1239                let mut values = (DiagStyledString::new(), DiagStyledString::new());
1240                cmp_ty_refs(r1, mutbl1, r2, mutbl2, &mut values);
1241                recurse(ref_ty1, ref_ty2, &mut values);
1242                values
1243            }
1244            // When finding T != &T, highlight the borrow
1245            (&ty::Ref(r1, ref_ty1, mutbl1), _) => {
1246                let mut values = (DiagStyledString::new(), DiagStyledString::new());
1247                push_ref(r1, mutbl1, &mut values.0);
1248                recurse(ref_ty1, t2, &mut values);
1249                values
1250            }
1251            (_, &ty::Ref(r2, ref_ty2, mutbl2)) => {
1252                let mut values = (DiagStyledString::new(), DiagStyledString::new());
1253                push_ref(r2, mutbl2, &mut values.1);
1254                recurse(t1, ref_ty2, &mut values);
1255                values
1256            }
1257
1258            // When encountering tuples of the same size, highlight only the differing types
1259            (&ty::Tuple(args1), &ty::Tuple(args2)) if args1.len() == args2.len() => {
1260                let mut values = (DiagStyledString::normal("("), DiagStyledString::normal("("));
1261                let len = args1.len();
1262                for (i, (left, right)) in args1.iter().zip(args2).enumerate() {
1263                    self.push_comma(&mut values.0, &mut values.1, i);
1264                    recurse(left, right, &mut values);
1265                }
1266                if len == 1 {
1267                    // Keep the output for single element tuples as `(ty,)`.
1268                    values.0.push_normal(",");
1269                    values.1.push_normal(",");
1270                }
1271                values.0.push_normal(")");
1272                values.1.push_normal(")");
1273                values
1274            }
1275
1276            (ty::FnDef(did1, args1), ty::FnDef(did2, args2)) => {
1277                let sig1 = self.tcx.fn_sig(*did1).instantiate(self.tcx, args1);
1278                let sig2 = self.tcx.fn_sig(*did2).instantiate(self.tcx, args2);
1279                self.cmp_fn_sig(
1280                    &sig1,
1281                    Some((*did1, Some(args1))),
1282                    &sig2,
1283                    Some((*did2, Some(args2))),
1284                )
1285            }
1286
1287            (ty::FnDef(did1, args1), ty::FnPtr(sig_tys2, hdr2)) => {
1288                let sig1 = self.tcx.fn_sig(*did1).instantiate(self.tcx, args1);
1289                self.cmp_fn_sig(&sig1, Some((*did1, Some(args1))), &sig_tys2.with(*hdr2), None)
1290            }
1291
1292            (ty::FnPtr(sig_tys1, hdr1), ty::FnDef(did2, args2)) => {
1293                let sig2 = self.tcx.fn_sig(*did2).instantiate(self.tcx, args2);
1294                self.cmp_fn_sig(&sig_tys1.with(*hdr1), None, &sig2, Some((*did2, Some(args2))))
1295            }
1296
1297            (ty::FnPtr(sig_tys1, hdr1), ty::FnPtr(sig_tys2, hdr2)) => {
1298                self.cmp_fn_sig(&sig_tys1.with(*hdr1), None, &sig_tys2.with(*hdr2), None)
1299            }
1300
1301            _ => {
1302                let mut strs = (DiagStyledString::new(), DiagStyledString::new());
1303                maybe_highlight(t1, t2, &mut strs, self.tcx);
1304                strs
1305            }
1306        }
1307    }
1308
1309    /// Extend a type error with extra labels pointing at "non-trivial" types, like closures and
1310    /// the return type of `async fn`s.
1311    ///
1312    /// `secondary_span` gives the caller the opportunity to expand `diag` with a `span_label`.
1313    ///
1314    /// `swap_secondary_and_primary` is used to make projection errors in particular nicer by using
1315    /// the message in `secondary_span` as the primary label, and apply the message that would
1316    /// otherwise be used for the primary label on the `secondary_span` `Span`. This applies on
1317    /// E0271, like `tests/ui/issues/issue-39970.stderr`.
1318    #[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_type_err",
                                    "rustc_trait_selection::error_reporting::infer",
                                    ::tracing::Level::DEBUG,
                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/error_reporting/infer/mod.rs"),
                                    ::tracing_core::__macro_support::Option::Some(1318u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_trait_selection::error_reporting::infer"),
                                    ::tracing_core::field::FieldSet::new(&["cause", "values",
                                                    "terr", "override_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(&::tracing::field::debug(&cause)
                                                            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(&values)
                                                            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(&terr)
                                                            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(&override_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: () = loop {};
            return __tracing_attr_fake_return;
        }
        {
            let span = override_span.unwrap_or(cause.span);
            if let TypeError::CyclicTy(_) = terr { values = None; }
            struct OpaqueTypesVisitor<'tcx> {
                types: FxIndexMap<TyCategory, FxIndexSet<Span>>,
                expected: FxIndexMap<TyCategory, FxIndexSet<Span>>,
                found: FxIndexMap<TyCategory, FxIndexSet<Span>>,
                ignore_span: Span,
                tcx: TyCtxt<'tcx>,
            }
            impl<'tcx> OpaqueTypesVisitor<'tcx> {
                fn visit_expected_found(tcx: TyCtxt<'tcx>,
                    expected: impl TypeVisitable<TyCtxt<'tcx>>,
                    found: impl TypeVisitable<TyCtxt<'tcx>>, ignore_span: Span)
                    -> Self {
                    let mut types_visitor =
                        OpaqueTypesVisitor {
                            types: Default::default(),
                            expected: Default::default(),
                            found: Default::default(),
                            ignore_span,
                            tcx,
                        };
                    expected.visit_with(&mut types_visitor);
                    std::mem::swap(&mut types_visitor.expected,
                        &mut types_visitor.types);
                    found.visit_with(&mut types_visitor);
                    std::mem::swap(&mut types_visitor.found,
                        &mut types_visitor.types);
                    types_visitor
                }
                fn report(&self, err: &mut Diag<'_>) {
                    self.add_labels_for_types(err, "expected", &self.expected);
                    self.add_labels_for_types(err, "found", &self.found);
                }
                fn add_labels_for_types(&self, err: &mut Diag<'_>,
                    target: &str,
                    types: &FxIndexMap<TyCategory, FxIndexSet<Span>>) {
                    for (kind, values) in types.iter() {
                        let count = values.len();
                        for &sp in values {
                            err.span_label(sp,
                                ::alloc::__export::must_use({
                                        ::alloc::fmt::format(format_args!("{0}{1} {2:#}{3}",
                                                if count == 1 { "the " } else { "one of the " }, target,
                                                kind, if count == 1 { "" } else { "s" }))
                                    }));
                        }
                    }
                }
            }
            impl<'tcx> ty::TypeVisitor<TyCtxt<'tcx>> for
                OpaqueTypesVisitor<'tcx> {
                fn visit_ty(&mut self, t: Ty<'tcx>) {
                    if let Some((kind, def_id)) =
                            TyCategory::from_ty(self.tcx, t) {
                        let span = self.tcx.def_span(def_id);
                        if !self.ignore_span.overlaps(span) &&
                                !span.is_desugaring(DesugaringKind::Async) {
                            self.types.entry(kind).or_default().insert(span);
                        }
                    }
                    t.super_visit_with(self)
                }
            }
            {
                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/infer/mod.rs:1429",
                                    "rustc_trait_selection::error_reporting::infer",
                                    ::tracing::Level::DEBUG,
                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/error_reporting/infer/mod.rs"),
                                    ::tracing_core::__macro_support::Option::Some(1429u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_trait_selection::error_reporting::infer"),
                                    ::tracing_core::field::FieldSet::new(&["message"],
                                        ::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_type_err(diag={0:?})",
                                                                diag) as &dyn Value))])
                        });
                } else { ; }
            };
            enum Mismatch<'a> {
                Variable(ty::error::ExpectedFound<Ty<'a>>),
                Fixed(&'static str),
            }
            let (expected_found, exp_found, is_simple_error, values,
                    param_env) =
                match values {
                    None => (None, Mismatch::Fixed("type"), false, None, None),
                    Some(ty::ParamEnvAnd { param_env, value: values }) => {
                        let mut values = self.resolve_vars_if_possible(values);
                        if self.next_trait_solver() {
                            values =
                                deeply_normalize_for_diagnostics(self, param_env, values);
                        }
                        let (is_simple_error, exp_found) =
                            match values {
                                ValuePairs::Terms(ExpectedFound { expected, found }) => {
                                    match (expected.kind(), found.kind()) {
                                        (ty::TermKind::Ty(expected), ty::TermKind::Ty(found)) => {
                                            let is_simple_err =
                                                expected.is_simple_text() && found.is_simple_text();
                                            OpaqueTypesVisitor::visit_expected_found(self.tcx, expected,
                                                    found, span).report(diag);
                                            (is_simple_err,
                                                Mismatch::Variable(ExpectedFound { expected, found }))
                                        }
                                        (ty::TermKind::Const(_), ty::TermKind::Const(_)) => {
                                            (false, Mismatch::Fixed("constant"))
                                        }
                                        _ => (false, Mismatch::Fixed("type")),
                                    }
                                }
                                ValuePairs::PolySigs(ExpectedFound { expected, found }) => {
                                    OpaqueTypesVisitor::visit_expected_found(self.tcx, expected,
                                            found, span).report(diag);
                                    (false, Mismatch::Fixed("signature"))
                                }
                                ValuePairs::TraitRefs(_) =>
                                    (false, Mismatch::Fixed("trait")),
                                ValuePairs::Aliases(ExpectedFound { expected, .. }) => {
                                    (false,
                                        Mismatch::Fixed(self.tcx.def_descr(expected.def_id)))
                                }
                                ValuePairs::Regions(_) =>
                                    (false, Mismatch::Fixed("lifetime")),
                                ValuePairs::ExistentialTraitRef(_) => {
                                    (false, Mismatch::Fixed("existential trait ref"))
                                }
                                ValuePairs::ExistentialProjection(_) => {
                                    (false, Mismatch::Fixed("existential projection"))
                                }
                            };
                        let Some(vals) =
                            self.values_str(values, cause,
                                diag.long_ty_path()) else {
                                diag.downgrade_to_delayed_bug();
                                return;
                            };
                        (Some(vals), exp_found, is_simple_error, Some(values),
                            Some(param_env))
                    }
                };
            let mut label_or_note =
                |span: Span, msg: Cow<'static, str>|
                    {
                        if (prefer_label && is_simple_error) ||
                                &[span] == diag.span.primary_spans() {
                            diag.span_label(span, msg);
                        } else { diag.span_note(span, msg); }
                    };
            if let Some((secondary_span, secondary_msg,
                    swap_secondary_and_primary)) = secondary_span {
                if swap_secondary_and_primary {
                    let terr =
                        if let Some(infer::ValuePairs::Terms(ExpectedFound {
                                expected, .. })) = values {
                            Cow::from(::alloc::__export::must_use({
                                        ::alloc::fmt::format(format_args!("expected this to be `{0}`",
                                                expected))
                                    }))
                        } else { terr.to_string(self.tcx) };
                    label_or_note(secondary_span, terr);
                    label_or_note(span, secondary_msg);
                } else {
                    label_or_note(span, terr.to_string(self.tcx));
                    label_or_note(secondary_span, secondary_msg);
                }
            } else if let Some(values) = values &&
                        let Some((e, f)) = values.ty() &&
                    let TypeError::ArgumentSorts(..) | TypeError::Sorts(_) =
                        terr {
                let e = self.tcx.erase_and_anonymize_regions(e);
                let f = self.tcx.erase_and_anonymize_regions(f);
                let expected =
                    {
                        let _guard = ForceTrimmedGuard::new();
                        e.sort_string(self.tcx)
                    };
                let found =
                    {
                        let _guard = ForceTrimmedGuard::new();
                        f.sort_string(self.tcx)
                    };
                if expected == found {
                    label_or_note(span, terr.to_string(self.tcx));
                } else {
                    label_or_note(span,
                        Cow::from(::alloc::__export::must_use({
                                    ::alloc::fmt::format(format_args!("expected {0}, found {1}",
                                            expected, found))
                                })));
                }
            } else { label_or_note(span, terr.to_string(self.tcx)); }
            if self.check_and_note_conflicting_crates(diag, terr) { return; }
            if let Some((expected, found)) = expected_found {
                let (expected_label, found_label, exp_found) =
                    match exp_found {
                        Mismatch::Variable(ef) =>
                            (ef.expected.prefix_string(self.tcx),
                                ef.found.prefix_string(self.tcx), Some(ef)),
                        Mismatch::Fixed(s) => (s.into(), s.into(), None),
                    };
                enum Similar<'tcx> {
                    Adts {
                        expected: ty::AdtDef<'tcx>,
                        found: ty::AdtDef<'tcx>,
                    },
                    PrimitiveFound {
                        expected: ty::AdtDef<'tcx>,
                        found: Ty<'tcx>,
                    },
                    PrimitiveExpected {
                        expected: Ty<'tcx>,
                        found: ty::AdtDef<'tcx>,
                    },
                }
                let similarity =
                    |ExpectedFound { expected, found }: ExpectedFound<Ty<'tcx>>|
                        {
                            if let ty::Adt(expected, _) = expected.kind() &&
                                    let Some(primitive) = found.primitive_symbol() {
                                let path = self.tcx.def_path(expected.did()).data;
                                let name = path.last().unwrap().data.get_opt_name();
                                if name == Some(primitive) {
                                    return Some(Similar::PrimitiveFound {
                                                expected: *expected,
                                                found,
                                            });
                                }
                            } else if let Some(primitive) = expected.primitive_symbol()
                                    && let ty::Adt(found, _) = found.kind() {
                                let path = self.tcx.def_path(found.did()).data;
                                let name = path.last().unwrap().data.get_opt_name();
                                if name == Some(primitive) {
                                    return Some(Similar::PrimitiveExpected {
                                                expected,
                                                found: *found,
                                            });
                                }
                            } else if let ty::Adt(expected, _) = expected.kind() &&
                                    let ty::Adt(found, _) = found.kind() {
                                if !expected.did().is_local() &&
                                        expected.did().krate == found.did().krate {
                                    return None;
                                }
                                let f_path = self.tcx.def_path(found.did()).data;
                                let e_path = self.tcx.def_path(expected.did()).data;
                                if let (Some(e_last), Some(f_last)) =
                                            (e_path.last(), f_path.last()) && e_last == f_last {
                                    return Some(Similar::Adts {
                                                expected: *expected,
                                                found: *found,
                                            });
                                }
                            }
                            None
                        };
                match terr {
                    TypeError::Sorts(values) if let Some(s) = similarity(values)
                        => {
                        let diagnose_primitive =
                            |prim: Ty<'tcx>, shadow: Ty<'tcx>, defid: DefId,
                                diag: &mut Diag<'_>|
                                {
                                    let name = shadow.sort_string(self.tcx);
                                    diag.note(::alloc::__export::must_use({
                                                ::alloc::fmt::format(format_args!("`{0}` and {1} have similar names, but are actually distinct types",
                                                        prim, name))
                                            }));
                                    diag.note(::alloc::__export::must_use({
                                                ::alloc::fmt::format(format_args!("one `{0}` is a primitive defined by the language",
                                                        prim))
                                            }));
                                    let def_span = self.tcx.def_span(defid);
                                    let msg =
                                        if defid.is_local() {
                                            ::alloc::__export::must_use({
                                                    ::alloc::fmt::format(format_args!("the other {0} is defined in the current crate",
                                                            name))
                                                })
                                        } else {
                                            let crate_name = self.tcx.crate_name(defid.krate);
                                            ::alloc::__export::must_use({
                                                    ::alloc::fmt::format(format_args!("the other {0} is defined in crate `{1}`",
                                                            name, crate_name))
                                                })
                                        };
                                    diag.span_note(def_span, msg);
                                };
                        let diagnose_adts =
                            |expected_adt: ty::AdtDef<'tcx>,
                                found_adt: ty::AdtDef<'tcx>, diag: &mut Diag<'_>|
                                {
                                    let found_name = values.found.sort_string(self.tcx);
                                    let expected_name = values.expected.sort_string(self.tcx);
                                    let found_defid = found_adt.did();
                                    let expected_defid = expected_adt.did();
                                    diag.note(::alloc::__export::must_use({
                                                ::alloc::fmt::format(format_args!("{0} and {1} have similar names, but are actually distinct types",
                                                        found_name, expected_name))
                                            }));
                                    for (defid, name) in
                                        [(found_defid, found_name), (expected_defid, expected_name)]
                                        {
                                        let def_span = self.tcx.def_span(defid);
                                        let msg =
                                            if found_defid.is_local() && expected_defid.is_local() {
                                                let module =
                                                    self.tcx.parent_module_from_def_id(defid.expect_local()).to_def_id();
                                                let module_name =
                                                    self.tcx.def_path(module).to_string_no_crate_verbose();
                                                ::alloc::__export::must_use({
                                                        ::alloc::fmt::format(format_args!("{0} is defined in module `crate{1}` of the current crate",
                                                                name, module_name))
                                                    })
                                            } else if defid.is_local() {
                                                ::alloc::__export::must_use({
                                                        ::alloc::fmt::format(format_args!("{0} is defined in the current crate",
                                                                name))
                                                    })
                                            } else {
                                                let crate_name = self.tcx.crate_name(defid.krate);
                                                ::alloc::__export::must_use({
                                                        ::alloc::fmt::format(format_args!("{0} is defined in crate `{1}`",
                                                                name, crate_name))
                                                    })
                                            };
                                        diag.span_note(def_span, msg);
                                    }
                                };
                        match s {
                            Similar::Adts { expected, found } =>
                                diagnose_adts(expected, found, diag),
                            Similar::PrimitiveFound { expected, found: prim } => {
                                diagnose_primitive(prim, values.expected, expected.did(),
                                    diag)
                            }
                            Similar::PrimitiveExpected { expected: prim, found } => {
                                diagnose_primitive(prim, values.found, found.did(), diag)
                            }
                        }
                    }
                    TypeError::Sorts(values) => {
                        let extra =
                            expected == found &&
                                values.expected.sort_string(self.tcx) !=
                                    values.found.sort_string(self.tcx);
                        let sort_string =
                            |ty: Ty<'tcx>|
                                match (extra, ty.kind()) {
                                    (true, ty::Alias(ty::Opaque, ty::AliasTy { def_id, .. })) =>
                                        {
                                        let sm = self.tcx.sess.source_map();
                                        let pos =
                                            sm.lookup_char_pos(self.tcx.def_span(*def_id).lo());
                                        DiagStyledString::normal(::alloc::__export::must_use({
                                                    ::alloc::fmt::format(format_args!(" (opaque type at <{0}:{1}:{2}>)",
                                                            sm.filename_for_diagnostics(&pos.file.name), pos.line,
                                                            pos.col.to_usize() + 1))
                                                }))
                                    }
                                    (true, ty::Alias(ty::Projection, proj)) if
                                        self.tcx.is_impl_trait_in_trait(proj.def_id) => {
                                        let sm = self.tcx.sess.source_map();
                                        let pos =
                                            sm.lookup_char_pos(self.tcx.def_span(proj.def_id).lo());
                                        DiagStyledString::normal(::alloc::__export::must_use({
                                                    ::alloc::fmt::format(format_args!(" (trait associated opaque type at <{0}:{1}:{2}>)",
                                                            sm.filename_for_diagnostics(&pos.file.name), pos.line,
                                                            pos.col.to_usize() + 1))
                                                }))
                                    }
                                    (true, _) => {
                                        let mut s = DiagStyledString::normal(" (");
                                        s.push_highlighted(ty.sort_string(self.tcx));
                                        s.push_normal(")");
                                        s
                                    }
                                    (false, _) => DiagStyledString::normal(""),
                                };
                        if !(values.expected.is_simple_text() &&
                                            values.found.is_simple_text()) ||
                                (exp_found.is_some_and(|ef|
                                            {
                                                if !ef.expected.is_ty_or_numeric_infer() {
                                                    ef.expected != values.expected
                                                } else if !ef.found.is_ty_or_numeric_infer() {
                                                    ef.found != values.found
                                                } else { false }
                                            })) {
                            if let Some(ExpectedFound { found: found_ty, .. }) =
                                        exp_found && !self.tcx.ty_is_opaque_future(found_ty) {
                                diag.note_expected_found_extra(&expected_label, expected,
                                    &found_label, found, sort_string(values.expected),
                                    sort_string(values.found));
                            }
                        }
                    }
                    _ => {
                        {
                            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/infer/mod.rs:1731",
                                                "rustc_trait_selection::error_reporting::infer",
                                                ::tracing::Level::DEBUG,
                                                ::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/error_reporting/infer/mod.rs"),
                                                ::tracing_core::__macro_support::Option::Some(1731u32),
                                                ::tracing_core::__macro_support::Option::Some("rustc_trait_selection::error_reporting::infer"),
                                                ::tracing_core::field::FieldSet::new(&["message"],
                                                    ::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_type_err: exp_found={0:?}, expected={1:?} found={2:?}",
                                                                            exp_found, expected, found) as &dyn Value))])
                                    });
                            } else { ; }
                        };
                        if !is_simple_error || terr.must_include_note() {
                            diag.note_expected_found(&expected_label, expected,
                                &found_label, found);
                            if let Some(ty::Closure(_, args)) =
                                    exp_found.map(|expected_type_found|
                                            expected_type_found.found.kind()) {
                                diag.highlighted_note(::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
                                            [StringPart::normal("closure has signature: `"),
                                                    StringPart::highlighted(self.tcx.signature_unclosure(args.as_closure().sig(),
                                                                rustc_hir::Safety::Safe).to_string()),
                                                    StringPart::normal("`")])));
                            }
                        }
                    }
                }
            }
            let exp_found =
                match exp_found {
                    Mismatch::Variable(exp_found) => Some(exp_found),
                    Mismatch::Fixed(_) => None,
                };
            let exp_found =
                match terr {
                    ty::error::TypeError::Sorts(terr) if
                        exp_found.is_some_and(|ef| terr.found == ef.found) => {
                        Some(terr)
                    }
                    _ => exp_found,
                };
            {
                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/infer/mod.rs:1771",
                                    "rustc_trait_selection::error_reporting::infer",
                                    ::tracing::Level::DEBUG,
                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/error_reporting/infer/mod.rs"),
                                    ::tracing_core::__macro_support::Option::Some(1771u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_trait_selection::error_reporting::infer"),
                                    ::tracing_core::field::FieldSet::new(&["message"],
                                        ::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!("exp_found {0:?} terr {1:?} cause.code {2:?}",
                                                                exp_found, terr, cause.code()) as &dyn Value))])
                        });
                } else { ; }
            };
            if let Some(exp_found) = exp_found {
                let should_suggest_fixes =
                    if let ObligationCauseCode::Pattern { root_ty, .. } =
                            cause.code() {
                        self.same_type_modulo_infer(*root_ty, exp_found.expected)
                    } else { true };
                if should_suggest_fixes &&
                        !#[allow(non_exhaustive_omitted_patterns)] match terr {
                                TypeError::RegionsInsufficientlyPolymorphic(..) => true,
                                _ => false,
                            } {
                    self.suggest_tuple_pattern(cause, &exp_found, diag);
                    self.suggest_accessing_field_where_appropriate(cause,
                        &exp_found, diag);
                    self.suggest_await_on_expect_found(cause, span, &exp_found,
                        diag);
                    self.suggest_function_pointers(cause, span, &exp_found,
                        terr, diag);
                    self.suggest_turning_stmt_into_expr(cause, &exp_found,
                        diag);
                }
            }
            self.note_and_explain_type_err(diag, terr, cause, span,
                cause.body_id.to_def_id());
            if let Some(exp_found) = exp_found &&
                        let exp_found = TypeError::Sorts(exp_found) &&
                    exp_found != terr {
                self.note_and_explain_type_err(diag, exp_found, cause, span,
                    cause.body_id.to_def_id());
            }
            if let Some(ValuePairs::TraitRefs(exp_found)) = values &&
                            let ty::Closure(def_id, _) =
                                exp_found.expected.self_ty().kind() &&
                        let Some(def_id) = def_id.as_local() &&
                    terr.involves_regions() {
                let span = self.tcx.def_span(def_id);
                diag.span_note(span,
                    "this closure does not fulfill the lifetime requirements");
                self.suggest_for_all_lifetime_closure(span,
                    self.tcx.hir_node_by_def_id(def_id), &exp_found, diag);
            }
            self.note_error_origin(diag, cause, exp_found, terr, param_env);
            {
                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/infer/mod.rs:1823",
                                    "rustc_trait_selection::error_reporting::infer",
                                    ::tracing::Level::DEBUG,
                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/error_reporting/infer/mod.rs"),
                                    ::tracing_core::__macro_support::Option::Some(1823u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_trait_selection::error_reporting::infer"),
                                    ::tracing_core::field::FieldSet::new(&["diag"],
                                        ::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(&diag) as
                                                        &dyn Value))])
                        });
                } else { ; }
            };
        }
    }
}#[instrument(level = "debug", skip(self, diag, secondary_span, prefer_label))]
1319    pub fn note_type_err(
1320        &self,
1321        diag: &mut Diag<'_>,
1322        cause: &ObligationCause<'tcx>,
1323        secondary_span: Option<(Span, Cow<'static, str>, bool)>,
1324        mut values: Option<ty::ParamEnvAnd<'tcx, ValuePairs<'tcx>>>,
1325        terr: TypeError<'tcx>,
1326        prefer_label: bool,
1327        override_span: Option<Span>,
1328    ) {
1329        // We use `override_span` when we want the error to point at a `Span` other than
1330        // `cause.span`. This is used in E0271, when a closure is passed in where the return type
1331        // isn't what was expected. We want to point at the closure's return type (or expression),
1332        // instead of the expression where the closure is passed as call argument.
1333        let span = override_span.unwrap_or(cause.span);
1334        // For some types of errors, expected-found does not make
1335        // sense, so just ignore the values we were given.
1336        if let TypeError::CyclicTy(_) = terr {
1337            values = None;
1338        }
1339        struct OpaqueTypesVisitor<'tcx> {
1340            types: FxIndexMap<TyCategory, FxIndexSet<Span>>,
1341            expected: FxIndexMap<TyCategory, FxIndexSet<Span>>,
1342            found: FxIndexMap<TyCategory, FxIndexSet<Span>>,
1343            ignore_span: Span,
1344            tcx: TyCtxt<'tcx>,
1345        }
1346
1347        impl<'tcx> OpaqueTypesVisitor<'tcx> {
1348            fn visit_expected_found(
1349                tcx: TyCtxt<'tcx>,
1350                expected: impl TypeVisitable<TyCtxt<'tcx>>,
1351                found: impl TypeVisitable<TyCtxt<'tcx>>,
1352                ignore_span: Span,
1353            ) -> Self {
1354                let mut types_visitor = OpaqueTypesVisitor {
1355                    types: Default::default(),
1356                    expected: Default::default(),
1357                    found: Default::default(),
1358                    ignore_span,
1359                    tcx,
1360                };
1361                // The visitor puts all the relevant encountered types in `self.types`, but in
1362                // here we want to visit two separate types with no relation to each other, so we
1363                // move the results from `types` to `expected` or `found` as appropriate.
1364                expected.visit_with(&mut types_visitor);
1365                std::mem::swap(&mut types_visitor.expected, &mut types_visitor.types);
1366                found.visit_with(&mut types_visitor);
1367                std::mem::swap(&mut types_visitor.found, &mut types_visitor.types);
1368                types_visitor
1369            }
1370
1371            fn report(&self, err: &mut Diag<'_>) {
1372                self.add_labels_for_types(err, "expected", &self.expected);
1373                self.add_labels_for_types(err, "found", &self.found);
1374            }
1375
1376            fn add_labels_for_types(
1377                &self,
1378                err: &mut Diag<'_>,
1379                target: &str,
1380                types: &FxIndexMap<TyCategory, FxIndexSet<Span>>,
1381            ) {
1382                for (kind, values) in types.iter() {
1383                    let count = values.len();
1384                    for &sp in values {
1385                        err.span_label(
1386                            sp,
1387                            format!(
1388                                "{}{} {:#}{}",
1389                                if count == 1 { "the " } else { "one of the " },
1390                                target,
1391                                kind,
1392                                pluralize!(count),
1393                            ),
1394                        );
1395                    }
1396                }
1397            }
1398        }
1399
1400        impl<'tcx> ty::TypeVisitor<TyCtxt<'tcx>> for OpaqueTypesVisitor<'tcx> {
1401            fn visit_ty(&mut self, t: Ty<'tcx>) {
1402                if let Some((kind, def_id)) = TyCategory::from_ty(self.tcx, t) {
1403                    let span = self.tcx.def_span(def_id);
1404                    // Avoid cluttering the output when the "found" and error span overlap:
1405                    //
1406                    // error[E0308]: mismatched types
1407                    //   --> $DIR/issue-20862.rs:2:5
1408                    //    |
1409                    // LL |     |y| x + y
1410                    //    |     ^^^^^^^^^
1411                    //    |     |
1412                    //    |     the found closure
1413                    //    |     expected `()`, found closure
1414                    //    |
1415                    //    = note: expected unit type `()`
1416                    //                 found closure `{closure@$DIR/issue-20862.rs:2:5: 2:14 x:_}`
1417                    //
1418                    // Also ignore opaque `Future`s that come from async fns.
1419                    if !self.ignore_span.overlaps(span)
1420                        && !span.is_desugaring(DesugaringKind::Async)
1421                    {
1422                        self.types.entry(kind).or_default().insert(span);
1423                    }
1424                }
1425                t.super_visit_with(self)
1426            }
1427        }
1428
1429        debug!("note_type_err(diag={:?})", diag);
1430        enum Mismatch<'a> {
1431            Variable(ty::error::ExpectedFound<Ty<'a>>),
1432            Fixed(&'static str),
1433        }
1434        let (expected_found, exp_found, is_simple_error, values, param_env) = match values {
1435            None => (None, Mismatch::Fixed("type"), false, None, None),
1436            Some(ty::ParamEnvAnd { param_env, value: values }) => {
1437                let mut values = self.resolve_vars_if_possible(values);
1438                if self.next_trait_solver() {
1439                    values = deeply_normalize_for_diagnostics(self, param_env, values);
1440                }
1441                let (is_simple_error, exp_found) = match values {
1442                    ValuePairs::Terms(ExpectedFound { expected, found }) => {
1443                        match (expected.kind(), found.kind()) {
1444                            (ty::TermKind::Ty(expected), ty::TermKind::Ty(found)) => {
1445                                let is_simple_err =
1446                                    expected.is_simple_text() && found.is_simple_text();
1447                                OpaqueTypesVisitor::visit_expected_found(
1448                                    self.tcx, expected, found, span,
1449                                )
1450                                .report(diag);
1451
1452                                (
1453                                    is_simple_err,
1454                                    Mismatch::Variable(ExpectedFound { expected, found }),
1455                                )
1456                            }
1457                            (ty::TermKind::Const(_), ty::TermKind::Const(_)) => {
1458                                (false, Mismatch::Fixed("constant"))
1459                            }
1460                            _ => (false, Mismatch::Fixed("type")),
1461                        }
1462                    }
1463                    ValuePairs::PolySigs(ExpectedFound { expected, found }) => {
1464                        OpaqueTypesVisitor::visit_expected_found(self.tcx, expected, found, span)
1465                            .report(diag);
1466                        (false, Mismatch::Fixed("signature"))
1467                    }
1468                    ValuePairs::TraitRefs(_) => (false, Mismatch::Fixed("trait")),
1469                    ValuePairs::Aliases(ExpectedFound { expected, .. }) => {
1470                        (false, Mismatch::Fixed(self.tcx.def_descr(expected.def_id)))
1471                    }
1472                    ValuePairs::Regions(_) => (false, Mismatch::Fixed("lifetime")),
1473                    ValuePairs::ExistentialTraitRef(_) => {
1474                        (false, Mismatch::Fixed("existential trait ref"))
1475                    }
1476                    ValuePairs::ExistentialProjection(_) => {
1477                        (false, Mismatch::Fixed("existential projection"))
1478                    }
1479                };
1480                let Some(vals) = self.values_str(values, cause, diag.long_ty_path()) else {
1481                    // Derived error. Cancel the emitter.
1482                    // NOTE(eddyb) this was `.cancel()`, but `diag`
1483                    // is borrowed, so we can't fully defuse it.
1484                    diag.downgrade_to_delayed_bug();
1485                    return;
1486                };
1487                (Some(vals), exp_found, is_simple_error, Some(values), Some(param_env))
1488            }
1489        };
1490
1491        let mut label_or_note = |span: Span, msg: Cow<'static, str>| {
1492            if (prefer_label && is_simple_error) || &[span] == diag.span.primary_spans() {
1493                diag.span_label(span, msg);
1494            } else {
1495                diag.span_note(span, msg);
1496            }
1497        };
1498        if let Some((secondary_span, secondary_msg, swap_secondary_and_primary)) = secondary_span {
1499            if swap_secondary_and_primary {
1500                let terr = if let Some(infer::ValuePairs::Terms(ExpectedFound {
1501                    expected, ..
1502                })) = values
1503                {
1504                    Cow::from(format!("expected this to be `{expected}`"))
1505                } else {
1506                    terr.to_string(self.tcx)
1507                };
1508                label_or_note(secondary_span, terr);
1509                label_or_note(span, secondary_msg);
1510            } else {
1511                label_or_note(span, terr.to_string(self.tcx));
1512                label_or_note(secondary_span, secondary_msg);
1513            }
1514        } else if let Some(values) = values
1515            && let Some((e, f)) = values.ty()
1516            && let TypeError::ArgumentSorts(..) | TypeError::Sorts(_) = terr
1517        {
1518            let e = self.tcx.erase_and_anonymize_regions(e);
1519            let f = self.tcx.erase_and_anonymize_regions(f);
1520            let expected = with_forced_trimmed_paths!(e.sort_string(self.tcx));
1521            let found = with_forced_trimmed_paths!(f.sort_string(self.tcx));
1522            if expected == found {
1523                label_or_note(span, terr.to_string(self.tcx));
1524            } else {
1525                label_or_note(span, Cow::from(format!("expected {expected}, found {found}")));
1526            }
1527        } else {
1528            label_or_note(span, terr.to_string(self.tcx));
1529        }
1530
1531        if self.check_and_note_conflicting_crates(diag, terr) {
1532            return;
1533        }
1534
1535        if let Some((expected, found)) = expected_found {
1536            let (expected_label, found_label, exp_found) = match exp_found {
1537                Mismatch::Variable(ef) => (
1538                    ef.expected.prefix_string(self.tcx),
1539                    ef.found.prefix_string(self.tcx),
1540                    Some(ef),
1541                ),
1542                Mismatch::Fixed(s) => (s.into(), s.into(), None),
1543            };
1544
1545            enum Similar<'tcx> {
1546                Adts { expected: ty::AdtDef<'tcx>, found: ty::AdtDef<'tcx> },
1547                PrimitiveFound { expected: ty::AdtDef<'tcx>, found: Ty<'tcx> },
1548                PrimitiveExpected { expected: Ty<'tcx>, found: ty::AdtDef<'tcx> },
1549            }
1550
1551            let similarity = |ExpectedFound { expected, found }: ExpectedFound<Ty<'tcx>>| {
1552                if let ty::Adt(expected, _) = expected.kind()
1553                    && let Some(primitive) = found.primitive_symbol()
1554                {
1555                    let path = self.tcx.def_path(expected.did()).data;
1556                    let name = path.last().unwrap().data.get_opt_name();
1557                    if name == Some(primitive) {
1558                        return Some(Similar::PrimitiveFound { expected: *expected, found });
1559                    }
1560                } else if let Some(primitive) = expected.primitive_symbol()
1561                    && let ty::Adt(found, _) = found.kind()
1562                {
1563                    let path = self.tcx.def_path(found.did()).data;
1564                    let name = path.last().unwrap().data.get_opt_name();
1565                    if name == Some(primitive) {
1566                        return Some(Similar::PrimitiveExpected { expected, found: *found });
1567                    }
1568                } else if let ty::Adt(expected, _) = expected.kind()
1569                    && let ty::Adt(found, _) = found.kind()
1570                {
1571                    if !expected.did().is_local() && expected.did().krate == found.did().krate {
1572                        // Most likely types from different versions of the same crate
1573                        // are in play, in which case this message isn't so helpful.
1574                        // A "perhaps two different versions..." error is already emitted for that.
1575                        return None;
1576                    }
1577                    let f_path = self.tcx.def_path(found.did()).data;
1578                    let e_path = self.tcx.def_path(expected.did()).data;
1579
1580                    if let (Some(e_last), Some(f_last)) = (e_path.last(), f_path.last())
1581                        && e_last == f_last
1582                    {
1583                        return Some(Similar::Adts { expected: *expected, found: *found });
1584                    }
1585                }
1586                None
1587            };
1588
1589            match terr {
1590                // If two types mismatch but have similar names, mention that specifically.
1591                TypeError::Sorts(values) if let Some(s) = similarity(values) => {
1592                    let diagnose_primitive =
1593                        |prim: Ty<'tcx>, shadow: Ty<'tcx>, defid: DefId, diag: &mut Diag<'_>| {
1594                            let name = shadow.sort_string(self.tcx);
1595                            diag.note(format!(
1596                                "`{prim}` and {name} have similar names, but are actually distinct types"
1597                            ));
1598                            diag.note(format!(
1599                                "one `{prim}` is a primitive defined by the language",
1600                            ));
1601                            let def_span = self.tcx.def_span(defid);
1602                            let msg = if defid.is_local() {
1603                                format!("the other {name} is defined in the current crate")
1604                            } else {
1605                                let crate_name = self.tcx.crate_name(defid.krate);
1606                                format!("the other {name} is defined in crate `{crate_name}`")
1607                            };
1608                            diag.span_note(def_span, msg);
1609                        };
1610
1611                    let diagnose_adts =
1612                        |expected_adt: ty::AdtDef<'tcx>,
1613                         found_adt: ty::AdtDef<'tcx>,
1614                         diag: &mut Diag<'_>| {
1615                            let found_name = values.found.sort_string(self.tcx);
1616                            let expected_name = values.expected.sort_string(self.tcx);
1617
1618                            let found_defid = found_adt.did();
1619                            let expected_defid = expected_adt.did();
1620
1621                            diag.note(format!("{found_name} and {expected_name} have similar names, but are actually distinct types"));
1622                            for (defid, name) in
1623                                [(found_defid, found_name), (expected_defid, expected_name)]
1624                            {
1625                                let def_span = self.tcx.def_span(defid);
1626
1627                                let msg = if found_defid.is_local() && expected_defid.is_local() {
1628                                    let module = self
1629                                        .tcx
1630                                        .parent_module_from_def_id(defid.expect_local())
1631                                        .to_def_id();
1632                                    let module_name =
1633                                        self.tcx.def_path(module).to_string_no_crate_verbose();
1634                                    format!(
1635                                        "{name} is defined in module `crate{module_name}` of the current crate"
1636                                    )
1637                                } else if defid.is_local() {
1638                                    format!("{name} is defined in the current crate")
1639                                } else {
1640                                    let crate_name = self.tcx.crate_name(defid.krate);
1641                                    format!("{name} is defined in crate `{crate_name}`")
1642                                };
1643                                diag.span_note(def_span, msg);
1644                            }
1645                        };
1646
1647                    match s {
1648                        Similar::Adts { expected, found } => diagnose_adts(expected, found, diag),
1649                        Similar::PrimitiveFound { expected, found: prim } => {
1650                            diagnose_primitive(prim, values.expected, expected.did(), diag)
1651                        }
1652                        Similar::PrimitiveExpected { expected: prim, found } => {
1653                            diagnose_primitive(prim, values.found, found.did(), diag)
1654                        }
1655                    }
1656                }
1657                TypeError::Sorts(values) => {
1658                    let extra = expected == found
1659                        // Ensure that we don't ever say something like
1660                        // expected `impl Trait` (opaque type `impl Trait`)
1661                        //    found `impl Trait` (opaque type `impl Trait`)
1662                        && values.expected.sort_string(self.tcx)
1663                            != values.found.sort_string(self.tcx);
1664                    let sort_string = |ty: Ty<'tcx>| match (extra, ty.kind()) {
1665                        (true, ty::Alias(ty::Opaque, ty::AliasTy { def_id, .. })) => {
1666                            let sm = self.tcx.sess.source_map();
1667                            let pos = sm.lookup_char_pos(self.tcx.def_span(*def_id).lo());
1668                            DiagStyledString::normal(format!(
1669                                " (opaque type at <{}:{}:{}>)",
1670                                sm.filename_for_diagnostics(&pos.file.name),
1671                                pos.line,
1672                                pos.col.to_usize() + 1,
1673                            ))
1674                        }
1675                        (true, ty::Alias(ty::Projection, proj))
1676                            if self.tcx.is_impl_trait_in_trait(proj.def_id) =>
1677                        {
1678                            let sm = self.tcx.sess.source_map();
1679                            let pos = sm.lookup_char_pos(self.tcx.def_span(proj.def_id).lo());
1680                            DiagStyledString::normal(format!(
1681                                " (trait associated opaque type at <{}:{}:{}>)",
1682                                sm.filename_for_diagnostics(&pos.file.name),
1683                                pos.line,
1684                                pos.col.to_usize() + 1,
1685                            ))
1686                        }
1687                        (true, _) => {
1688                            let mut s = DiagStyledString::normal(" (");
1689                            s.push_highlighted(ty.sort_string(self.tcx));
1690                            s.push_normal(")");
1691                            s
1692                        }
1693                        (false, _) => DiagStyledString::normal(""),
1694                    };
1695                    if !(values.expected.is_simple_text() && values.found.is_simple_text())
1696                        || (exp_found.is_some_and(|ef| {
1697                            // This happens when the type error is a subset of the expectation,
1698                            // like when you have two references but one is `usize` and the other
1699                            // is `f32`. In those cases we still want to show the `note`. If the
1700                            // value from `ef` is `Infer(_)`, then we ignore it.
1701                            if !ef.expected.is_ty_or_numeric_infer() {
1702                                ef.expected != values.expected
1703                            } else if !ef.found.is_ty_or_numeric_infer() {
1704                                ef.found != values.found
1705                            } else {
1706                                false
1707                            }
1708                        }))
1709                    {
1710                        if let Some(ExpectedFound { found: found_ty, .. }) = exp_found
1711                            && !self.tcx.ty_is_opaque_future(found_ty)
1712                        {
1713                            // `Future` is a special opaque type that the compiler
1714                            // will try to hide in some case such as `async fn`, so
1715                            // to make an error more use friendly we will
1716                            // avoid to suggest a mismatch type with a
1717                            // type that the user usually are not using
1718                            // directly such as `impl Future<Output = u8>`.
1719                            diag.note_expected_found_extra(
1720                                &expected_label,
1721                                expected,
1722                                &found_label,
1723                                found,
1724                                sort_string(values.expected),
1725                                sort_string(values.found),
1726                            );
1727                        }
1728                    }
1729                }
1730                _ => {
1731                    debug!(
1732                        "note_type_err: exp_found={:?}, expected={:?} found={:?}",
1733                        exp_found, expected, found
1734                    );
1735                    if !is_simple_error || terr.must_include_note() {
1736                        diag.note_expected_found(&expected_label, expected, &found_label, found);
1737
1738                        if let Some(ty::Closure(_, args)) =
1739                            exp_found.map(|expected_type_found| expected_type_found.found.kind())
1740                        {
1741                            diag.highlighted_note(vec![
1742                                StringPart::normal("closure has signature: `"),
1743                                StringPart::highlighted(
1744                                    self.tcx
1745                                        .signature_unclosure(
1746                                            args.as_closure().sig(),
1747                                            rustc_hir::Safety::Safe,
1748                                        )
1749                                        .to_string(),
1750                                ),
1751                                StringPart::normal("`"),
1752                            ]);
1753                        }
1754                    }
1755                }
1756            }
1757        }
1758        let exp_found = match exp_found {
1759            Mismatch::Variable(exp_found) => Some(exp_found),
1760            Mismatch::Fixed(_) => None,
1761        };
1762        let exp_found = match terr {
1763            // `terr` has more accurate type information than `exp_found` in match expressions.
1764            ty::error::TypeError::Sorts(terr)
1765                if exp_found.is_some_and(|ef| terr.found == ef.found) =>
1766            {
1767                Some(terr)
1768            }
1769            _ => exp_found,
1770        };
1771        debug!("exp_found {:?} terr {:?} cause.code {:?}", exp_found, terr, cause.code());
1772        if let Some(exp_found) = exp_found {
1773            let should_suggest_fixes =
1774                if let ObligationCauseCode::Pattern { root_ty, .. } = cause.code() {
1775                    // Skip if the root_ty of the pattern is not the same as the expected_ty.
1776                    // If these types aren't equal then we've probably peeled off a layer of arrays.
1777                    self.same_type_modulo_infer(*root_ty, exp_found.expected)
1778                } else {
1779                    true
1780                };
1781
1782            // FIXME(#73154): For now, we do leak check when coercing function
1783            // pointers in typeck, instead of only during borrowck. This can lead
1784            // to these `RegionsInsufficientlyPolymorphic` errors that aren't helpful.
1785            if should_suggest_fixes
1786                && !matches!(terr, TypeError::RegionsInsufficientlyPolymorphic(..))
1787            {
1788                self.suggest_tuple_pattern(cause, &exp_found, diag);
1789                self.suggest_accessing_field_where_appropriate(cause, &exp_found, diag);
1790                self.suggest_await_on_expect_found(cause, span, &exp_found, diag);
1791                self.suggest_function_pointers(cause, span, &exp_found, terr, diag);
1792                self.suggest_turning_stmt_into_expr(cause, &exp_found, diag);
1793            }
1794        }
1795
1796        self.note_and_explain_type_err(diag, terr, cause, span, cause.body_id.to_def_id());
1797        if let Some(exp_found) = exp_found
1798            && let exp_found = TypeError::Sorts(exp_found)
1799            && exp_found != terr
1800        {
1801            self.note_and_explain_type_err(diag, exp_found, cause, span, cause.body_id.to_def_id());
1802        }
1803
1804        if let Some(ValuePairs::TraitRefs(exp_found)) = values
1805            && let ty::Closure(def_id, _) = exp_found.expected.self_ty().kind()
1806            && let Some(def_id) = def_id.as_local()
1807            && terr.involves_regions()
1808        {
1809            let span = self.tcx.def_span(def_id);
1810            diag.span_note(span, "this closure does not fulfill the lifetime requirements");
1811            self.suggest_for_all_lifetime_closure(
1812                span,
1813                self.tcx.hir_node_by_def_id(def_id),
1814                &exp_found,
1815                diag,
1816            );
1817        }
1818
1819        // It reads better to have the error origin as the final
1820        // thing.
1821        self.note_error_origin(diag, cause, exp_found, terr, param_env);
1822
1823        debug!(?diag);
1824    }
1825
1826    pub fn type_error_additional_suggestions(
1827        &self,
1828        trace: &TypeTrace<'tcx>,
1829        terr: TypeError<'tcx>,
1830        long_ty_path: &mut Option<PathBuf>,
1831    ) -> Vec<TypeErrorAdditionalDiags> {
1832        let mut suggestions = Vec::new();
1833        let span = trace.cause.span;
1834        let values = self.resolve_vars_if_possible(trace.values);
1835        if let Some((expected, found)) = values.ty() {
1836            match (expected.kind(), found.kind()) {
1837                (ty::Tuple(_), ty::Tuple(_)) => {}
1838                // If a tuple of length one was expected and the found expression has
1839                // parentheses around it, perhaps the user meant to write `(expr,)` to
1840                // build a tuple (issue #86100)
1841                (ty::Tuple(fields), _) => {
1842                    suggestions.extend(self.suggest_wrap_to_build_a_tuple(span, found, fields))
1843                }
1844                // If a byte was expected and the found expression is a char literal
1845                // containing a single ASCII character, perhaps the user meant to write `b'c'` to
1846                // specify a byte literal
1847                (ty::Uint(ty::UintTy::U8), ty::Char) => {
1848                    if let Ok(code) = self.tcx.sess.source_map().span_to_snippet(span)
1849                        && let Some(code) = code.strip_circumfix('\'', '\'')
1850                        // forbid all Unicode escapes
1851                        && !code.starts_with("\\u")
1852                        // forbids literal Unicode characters beyond ASCII
1853                        && code.chars().next().is_some_and(|c| c.is_ascii())
1854                    {
1855                        suggestions.push(TypeErrorAdditionalDiags::MeantByteLiteral {
1856                            span,
1857                            code: escape_literal(code),
1858                        })
1859                    }
1860                }
1861                // If a character was expected and the found expression is a string literal
1862                // containing a single character, perhaps the user meant to write `'c'` to
1863                // specify a character literal (issue #92479)
1864                (ty::Char, ty::Ref(_, r, _)) if r.is_str() => {
1865                    if let Ok(code) = self.tcx.sess.source_map().span_to_snippet(span)
1866                        && let Some(code) = code.strip_circumfix('"', '"')
1867                        && code.chars().count() == 1
1868                    {
1869                        suggestions.push(TypeErrorAdditionalDiags::MeantCharLiteral {
1870                            span,
1871                            code: escape_literal(code),
1872                        })
1873                    }
1874                }
1875                // If a string was expected and the found expression is a character literal,
1876                // perhaps the user meant to write `"s"` to specify a string literal.
1877                (ty::Ref(_, r, _), ty::Char) if r.is_str() => {
1878                    if let Ok(code) = self.tcx.sess.source_map().span_to_snippet(span)
1879                        && code.starts_with("'")
1880                        && code.ends_with("'")
1881                    {
1882                        suggestions.push(TypeErrorAdditionalDiags::MeantStrLiteral {
1883                            start: span.with_hi(span.lo() + BytePos(1)),
1884                            end: span.with_lo(span.hi() - BytePos(1)),
1885                        });
1886                    }
1887                }
1888                // For code `if Some(..) = expr `, the type mismatch may be expected `bool` but found `()`,
1889                // we try to suggest to add the missing `let` for `if let Some(..) = expr`
1890                (ty::Bool, ty::Tuple(list)) => {
1891                    if list.len() == 0 {
1892                        suggestions.extend(self.suggest_let_for_letchains(&trace.cause, span));
1893                    }
1894                }
1895                (ty::Array(_, _), ty::Array(_, _)) => {
1896                    suggestions.extend(self.suggest_specify_actual_length(terr, trace, span))
1897                }
1898                _ => {}
1899            }
1900        }
1901        let code = trace.cause.code();
1902        if let &(ObligationCauseCode::MatchExpressionArm(box MatchExpressionArmCause {
1903            source,
1904            ..
1905        })
1906        | ObligationCauseCode::BlockTailExpression(.., source)) = code
1907            && let hir::MatchSource::TryDesugar(_) = source
1908            && let Some((expected_ty, found_ty)) =
1909                self.values_str(trace.values, &trace.cause, long_ty_path)
1910        {
1911            suggestions.push(TypeErrorAdditionalDiags::TryCannotConvert {
1912                found: found_ty.content(),
1913                expected: expected_ty.content(),
1914            });
1915        }
1916        suggestions
1917    }
1918
1919    fn suggest_specify_actual_length(
1920        &self,
1921        terr: TypeError<'tcx>,
1922        trace: &TypeTrace<'tcx>,
1923        span: Span,
1924    ) -> Option<TypeErrorAdditionalDiags> {
1925        let TypeError::ArraySize(sz) = terr else {
1926            return None;
1927        };
1928        let tykind = match self.tcx.hir_node_by_def_id(trace.cause.body_id) {
1929            hir::Node::Item(hir::Item {
1930                kind: hir::ItemKind::Fn { body: body_id, .. }, ..
1931            }) => {
1932                let body = self.tcx.hir_body(*body_id);
1933                struct LetVisitor {
1934                    span: Span,
1935                }
1936                impl<'v> Visitor<'v> for LetVisitor {
1937                    type Result = ControlFlow<&'v hir::TyKind<'v>>;
1938                    fn visit_stmt(&mut self, s: &'v hir::Stmt<'v>) -> Self::Result {
1939                        // Find a local statement where the initializer has
1940                        // the same span as the error and the type is specified.
1941                        if let hir::Stmt {
1942                            kind:
1943                                hir::StmtKind::Let(hir::LetStmt {
1944                                    init: Some(hir::Expr { span: init_span, .. }),
1945                                    ty: Some(array_ty),
1946                                    ..
1947                                }),
1948                            ..
1949                        } = s
1950                            && init_span == &self.span
1951                        {
1952                            ControlFlow::Break(&array_ty.peel_refs().kind)
1953                        } else {
1954                            ControlFlow::Continue(())
1955                        }
1956                    }
1957                }
1958                LetVisitor { span }.visit_body(body).break_value()
1959            }
1960            hir::Node::Item(hir::Item { kind: hir::ItemKind::Const(_, _, ty, _), .. }) => {
1961                Some(&ty.peel_refs().kind)
1962            }
1963            _ => None,
1964        };
1965        if let Some(tykind) = tykind
1966            && let hir::TyKind::Array(_, length_arg) = tykind
1967            && let Some(length_val) = sz.found.try_to_target_usize(self.tcx)
1968        {
1969            Some(TypeErrorAdditionalDiags::ConsiderSpecifyingLength {
1970                span: length_arg.span,
1971                length: length_val,
1972            })
1973        } else {
1974            None
1975        }
1976    }
1977
1978    pub fn report_and_explain_type_error(
1979        &self,
1980        trace: TypeTrace<'tcx>,
1981        param_env: ty::ParamEnv<'tcx>,
1982        terr: TypeError<'tcx>,
1983    ) -> Diag<'a> {
1984        {
    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/infer/mod.rs:1984",
                        "rustc_trait_selection::error_reporting::infer",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/error_reporting/infer/mod.rs"),
                        ::tracing_core::__macro_support::Option::Some(1984u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_trait_selection::error_reporting::infer"),
                        ::tracing_core::field::FieldSet::new(&["message"],
                            ::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!("report_and_explain_type_error(trace={0:?}, terr={1:?})",
                                                    trace, terr) as &dyn Value))])
            });
    } else { ; }
};debug!("report_and_explain_type_error(trace={:?}, terr={:?})", trace, terr);
1985
1986        let span = trace.cause.span;
1987        let mut path = None;
1988        let failure_code = trace.cause.as_failure_code_diag(
1989            terr,
1990            span,
1991            self.type_error_additional_suggestions(&trace, terr, &mut path),
1992        );
1993        let mut diag = self.dcx().create_err(failure_code);
1994        *diag.long_ty_path() = path;
1995        self.note_type_err(
1996            &mut diag,
1997            &trace.cause,
1998            None,
1999            Some(param_env.and(trace.values)),
2000            terr,
2001            false,
2002            None,
2003        );
2004        diag
2005    }
2006
2007    fn suggest_wrap_to_build_a_tuple(
2008        &self,
2009        span: Span,
2010        found: Ty<'tcx>,
2011        expected_fields: &List<Ty<'tcx>>,
2012    ) -> Option<TypeErrorAdditionalDiags> {
2013        let [expected_tup_elem] = expected_fields[..] else { return None };
2014
2015        if !self.same_type_modulo_infer(expected_tup_elem, found) {
2016            return None;
2017        }
2018
2019        let Ok(code) = self.tcx.sess.source_map().span_to_snippet(span) else { return None };
2020
2021        let sugg = if code.starts_with('(') && code.ends_with(')') {
2022            let before_close = span.hi() - BytePos::from_u32(1);
2023            TypeErrorAdditionalDiags::TupleOnlyComma {
2024                span: span.with_hi(before_close).shrink_to_hi(),
2025            }
2026        } else {
2027            TypeErrorAdditionalDiags::TupleAlsoParentheses {
2028                span_low: span.shrink_to_lo(),
2029                span_high: span.shrink_to_hi(),
2030            }
2031        };
2032        Some(sugg)
2033    }
2034
2035    fn values_str(
2036        &self,
2037        values: ValuePairs<'tcx>,
2038        cause: &ObligationCause<'tcx>,
2039        long_ty_path: &mut Option<PathBuf>,
2040    ) -> Option<(DiagStyledString, DiagStyledString)> {
2041        match values {
2042            ValuePairs::Regions(exp_found) => self.expected_found_str(exp_found),
2043            ValuePairs::Terms(exp_found) => self.expected_found_str_term(exp_found, long_ty_path),
2044            ValuePairs::Aliases(exp_found) => self.expected_found_str(exp_found),
2045            ValuePairs::ExistentialTraitRef(exp_found) => self.expected_found_str(exp_found),
2046            ValuePairs::ExistentialProjection(exp_found) => self.expected_found_str(exp_found),
2047            ValuePairs::TraitRefs(exp_found) => {
2048                let pretty_exp_found = ty::error::ExpectedFound {
2049                    expected: exp_found.expected.print_trait_sugared(),
2050                    found: exp_found.found.print_trait_sugared(),
2051                };
2052                match self.expected_found_str(pretty_exp_found) {
2053                    Some((expected, found)) if expected == found => {
2054                        self.expected_found_str(exp_found)
2055                    }
2056                    ret => ret,
2057                }
2058            }
2059            ValuePairs::PolySigs(exp_found) => {
2060                let exp_found = self.resolve_vars_if_possible(exp_found);
2061                if exp_found.references_error() {
2062                    return None;
2063                }
2064                let (fn_def1, fn_def2) = if let ObligationCauseCode::CompareImplItem {
2065                    impl_item_def_id,
2066                    trait_item_def_id,
2067                    ..
2068                } = *cause.code()
2069                {
2070                    (Some((trait_item_def_id, None)), Some((impl_item_def_id.to_def_id(), None)))
2071                } else {
2072                    (None, None)
2073                };
2074
2075                Some(self.cmp_fn_sig(&exp_found.expected, fn_def1, &exp_found.found, fn_def2))
2076            }
2077        }
2078    }
2079
2080    fn expected_found_str_term(
2081        &self,
2082        exp_found: ty::error::ExpectedFound<ty::Term<'tcx>>,
2083        long_ty_path: &mut Option<PathBuf>,
2084    ) -> Option<(DiagStyledString, DiagStyledString)> {
2085        let exp_found = self.resolve_vars_if_possible(exp_found);
2086        if exp_found.references_error() {
2087            return None;
2088        }
2089
2090        Some(match (exp_found.expected.kind(), exp_found.found.kind()) {
2091            (ty::TermKind::Ty(expected), ty::TermKind::Ty(found)) => {
2092                let (mut exp, mut fnd) = self.cmp(expected, found);
2093                // Use the terminal width as the basis to determine when to compress the printed
2094                // out type, but give ourselves some leeway to avoid ending up creating a file for
2095                // a type that is somewhat shorter than the path we'd write to.
2096                let len = self.tcx.sess.diagnostic_width() + 40;
2097                let exp_s = exp.content();
2098                let fnd_s = fnd.content();
2099                if exp_s.len() > len {
2100                    let exp_s = self.tcx.short_string(expected, long_ty_path);
2101                    exp = DiagStyledString::highlighted(exp_s);
2102                }
2103                if fnd_s.len() > len {
2104                    let fnd_s = self.tcx.short_string(found, long_ty_path);
2105                    fnd = DiagStyledString::highlighted(fnd_s);
2106                }
2107                (exp, fnd)
2108            }
2109            _ => (
2110                DiagStyledString::highlighted(exp_found.expected.to_string()),
2111                DiagStyledString::highlighted(exp_found.found.to_string()),
2112            ),
2113        })
2114    }
2115
2116    /// Returns a string of the form "expected `{}`, found `{}`".
2117    fn expected_found_str<T: fmt::Display + TypeFoldable<TyCtxt<'tcx>>>(
2118        &self,
2119        exp_found: ty::error::ExpectedFound<T>,
2120    ) -> Option<(DiagStyledString, DiagStyledString)> {
2121        let exp_found = self.resolve_vars_if_possible(exp_found);
2122        if exp_found.references_error() {
2123            return None;
2124        }
2125
2126        Some((
2127            DiagStyledString::highlighted(exp_found.expected.to_string()),
2128            DiagStyledString::highlighted(exp_found.found.to_string()),
2129        ))
2130    }
2131
2132    /// Determine whether an error associated with the given span and definition
2133    /// should be treated as being caused by the implicit `From` conversion
2134    /// within `?` desugaring.
2135    pub fn is_try_conversion(&self, span: Span, trait_def_id: DefId) -> bool {
2136        span.is_desugaring(DesugaringKind::QuestionMark)
2137            && self.tcx.is_diagnostic_item(sym::From, trait_def_id)
2138    }
2139
2140    /// Structurally compares two types, modulo any inference variables.
2141    ///
2142    /// Returns `true` if two types are equal, or if one type is an inference variable compatible
2143    /// with the other type. A TyVar inference type is compatible with any type, and an IntVar or
2144    /// FloatVar inference type are compatible with themselves or their concrete types (Int and
2145    /// Float types, respectively). When comparing two ADTs, these rules apply recursively.
2146    pub fn same_type_modulo_infer<T: relate::Relate<TyCtxt<'tcx>>>(&self, a: T, b: T) -> bool {
2147        let (a, b) = self.resolve_vars_if_possible((a, b));
2148        SameTypeModuloInfer(self).relate(a, b).is_ok()
2149    }
2150}
2151
2152struct SameTypeModuloInfer<'a, 'tcx>(&'a InferCtxt<'tcx>);
2153
2154impl<'tcx> TypeRelation<TyCtxt<'tcx>> for SameTypeModuloInfer<'_, 'tcx> {
2155    fn cx(&self) -> TyCtxt<'tcx> {
2156        self.0.tcx
2157    }
2158
2159    fn relate_ty_args(
2160        &mut self,
2161        a_ty: Ty<'tcx>,
2162        _: Ty<'tcx>,
2163        _: DefId,
2164        a_args: ty::GenericArgsRef<'tcx>,
2165        b_args: ty::GenericArgsRef<'tcx>,
2166        _: impl FnOnce(ty::GenericArgsRef<'tcx>) -> Ty<'tcx>,
2167    ) -> RelateResult<'tcx, Ty<'tcx>> {
2168        relate::relate_args_invariantly(self, a_args, b_args)?;
2169        Ok(a_ty)
2170    }
2171
2172    fn relate_with_variance<T: relate::Relate<TyCtxt<'tcx>>>(
2173        &mut self,
2174        _variance: ty::Variance,
2175        _info: ty::VarianceDiagInfo<TyCtxt<'tcx>>,
2176        a: T,
2177        b: T,
2178    ) -> relate::RelateResult<'tcx, T> {
2179        self.relate(a, b)
2180    }
2181
2182    fn tys(&mut self, a: Ty<'tcx>, b: Ty<'tcx>) -> RelateResult<'tcx, Ty<'tcx>> {
2183        match (a.kind(), b.kind()) {
2184            (ty::Int(_) | ty::Uint(_), ty::Infer(ty::InferTy::IntVar(_)))
2185            | (
2186                ty::Infer(ty::InferTy::IntVar(_)),
2187                ty::Int(_) | ty::Uint(_) | ty::Infer(ty::InferTy::IntVar(_)),
2188            )
2189            | (ty::Float(_), ty::Infer(ty::InferTy::FloatVar(_)))
2190            | (
2191                ty::Infer(ty::InferTy::FloatVar(_)),
2192                ty::Float(_) | ty::Infer(ty::InferTy::FloatVar(_)),
2193            )
2194            | (ty::Infer(ty::InferTy::TyVar(_)), _)
2195            | (_, ty::Infer(ty::InferTy::TyVar(_))) => Ok(a),
2196            (ty::Infer(_), _) | (_, ty::Infer(_)) => Err(TypeError::Mismatch),
2197            _ => relate::structurally_relate_tys(self, a, b),
2198        }
2199    }
2200
2201    fn regions(
2202        &mut self,
2203        a: ty::Region<'tcx>,
2204        b: ty::Region<'tcx>,
2205    ) -> RelateResult<'tcx, ty::Region<'tcx>> {
2206        if (a.is_var() && b.is_free())
2207            || (b.is_var() && a.is_free())
2208            || (a.is_var() && b.is_var())
2209            || a == b
2210        {
2211            Ok(a)
2212        } else {
2213            Err(TypeError::Mismatch)
2214        }
2215    }
2216
2217    fn binders<T>(
2218        &mut self,
2219        a: ty::Binder<'tcx, T>,
2220        b: ty::Binder<'tcx, T>,
2221    ) -> relate::RelateResult<'tcx, ty::Binder<'tcx, T>>
2222    where
2223        T: relate::Relate<TyCtxt<'tcx>>,
2224    {
2225        Ok(a.rebind(self.relate(a.skip_binder(), b.skip_binder())?))
2226    }
2227
2228    fn consts(
2229        &mut self,
2230        a: ty::Const<'tcx>,
2231        _b: ty::Const<'tcx>,
2232    ) -> relate::RelateResult<'tcx, ty::Const<'tcx>> {
2233        // FIXME(compiler-errors): This could at least do some first-order
2234        // relation
2235        Ok(a)
2236    }
2237}
2238
2239pub enum FailureCode {
2240    Error0317,
2241    Error0580,
2242    Error0308,
2243    Error0644,
2244}
2245
2246impl<'tcx> ObligationCauseExt<'tcx> for ObligationCause<'tcx> {
    fn as_failure_code(&self, terr: TypeError<'tcx>) -> FailureCode {
        match self.code() {
            ObligationCauseCode::IfExpressionWithNoElse =>
                FailureCode::Error0317,
            ObligationCauseCode::MainFunctionType => FailureCode::Error0580,
            ObligationCauseCode::CompareImplItem { .. } |
                ObligationCauseCode::MatchExpressionArm(_) |
                ObligationCauseCode::IfExpression { .. } |
                ObligationCauseCode::LetElse |
                ObligationCauseCode::LangFunctionType(_) |
                ObligationCauseCode::IntrinsicType |
                ObligationCauseCode::MethodReceiver => FailureCode::Error0308,
            _ =>
                match terr {
                    TypeError::CyclicTy(ty) if
                        ty.is_closure() || ty.is_coroutine() ||
                            ty.is_coroutine_closure() => {
                        FailureCode::Error0644
                    }
                    TypeError::IntrinsicCast | TypeError::ForceInlineCast =>
                        FailureCode::Error0308,
                    _ => FailureCode::Error0308,
                },
        }
    }
    fn as_failure_code_diag(&self, terr: TypeError<'tcx>, span: Span,
        subdiags: Vec<TypeErrorAdditionalDiags>)
        -> ObligationCauseFailureCode {
        match self.code() {
            ObligationCauseCode::CompareImplItem {
                kind: ty::AssocKind::Fn { .. }, .. } => {
                ObligationCauseFailureCode::MethodCompat { span, subdiags }
            }
            ObligationCauseCode::CompareImplItem {
                kind: ty::AssocKind::Type { .. }, .. } => {
                ObligationCauseFailureCode::TypeCompat { span, subdiags }
            }
            ObligationCauseCode::CompareImplItem {
                kind: ty::AssocKind::Const { .. }, .. } => {
                ObligationCauseFailureCode::ConstCompat { span, subdiags }
            }
            ObligationCauseCode::BlockTailExpression(..,
                hir::MatchSource::TryDesugar(_)) => {
                ObligationCauseFailureCode::TryCompat { span, subdiags }
            }
            ObligationCauseCode::MatchExpressionArm(box MatchExpressionArmCause {
                source, .. }) =>
                match source {
                    hir::MatchSource::TryDesugar(_) => {
                        ObligationCauseFailureCode::TryCompat { span, subdiags }
                    }
                    _ =>
                        ObligationCauseFailureCode::MatchCompat { span, subdiags },
                },
            ObligationCauseCode::IfExpression { .. } => {
                ObligationCauseFailureCode::IfElseDifferent { span, subdiags }
            }
            ObligationCauseCode::IfExpressionWithNoElse => {
                ObligationCauseFailureCode::NoElse { span }
            }
            ObligationCauseCode::LetElse => {
                ObligationCauseFailureCode::NoDiverge { span, subdiags }
            }
            ObligationCauseCode::MainFunctionType => {
                ObligationCauseFailureCode::FnMainCorrectType { span }
            }
            &ObligationCauseCode::LangFunctionType(lang_item_name) => {
                ObligationCauseFailureCode::FnLangCorrectType {
                    span,
                    subdiags,
                    lang_item_name,
                }
            }
            ObligationCauseCode::IntrinsicType => {
                ObligationCauseFailureCode::IntrinsicCorrectType {
                    span,
                    subdiags,
                }
            }
            ObligationCauseCode::MethodReceiver => {
                ObligationCauseFailureCode::MethodCorrectType {
                    span,
                    subdiags,
                }
            }
            _ =>
                match terr {
                    TypeError::CyclicTy(ty) if
                        ty.is_closure() || ty.is_coroutine() ||
                            ty.is_coroutine_closure() => {
                        ObligationCauseFailureCode::ClosureSelfref { span }
                    }
                    TypeError::ForceInlineCast => {
                        ObligationCauseFailureCode::CantCoerceForceInline {
                            span,
                            subdiags,
                        }
                    }
                    TypeError::IntrinsicCast => {
                        ObligationCauseFailureCode::CantCoerceIntrinsic {
                            span,
                            subdiags,
                        }
                    }
                    _ => ObligationCauseFailureCode::Generic { span, subdiags },
                },
        }
    }
    fn as_requirement_str(&self) -> &'static str {
        match self.code() {
            ObligationCauseCode::CompareImplItem {
                kind: ty::AssocKind::Fn { .. }, .. } => {
                "method type is compatible with trait"
            }
            ObligationCauseCode::CompareImplItem {
                kind: ty::AssocKind::Type { .. }, .. } => {
                "associated type is compatible with trait"
            }
            ObligationCauseCode::CompareImplItem {
                kind: ty::AssocKind::Const { .. }, .. } => {
                "const is compatible with trait"
            }
            ObligationCauseCode::MainFunctionType =>
                "`main` function has the correct type",
            ObligationCauseCode::LangFunctionType(_) =>
                "lang item function has the correct type",
            ObligationCauseCode::IntrinsicType =>
                "intrinsic has the correct type",
            ObligationCauseCode::MethodReceiver =>
                "method receiver has the correct type",
            _ => "types are compatible",
        }
    }
}#[extension(pub trait ObligationCauseExt<'tcx>)]
2247impl<'tcx> ObligationCause<'tcx> {
2248    fn as_failure_code(&self, terr: TypeError<'tcx>) -> FailureCode {
2249        match self.code() {
2250            ObligationCauseCode::IfExpressionWithNoElse => FailureCode::Error0317,
2251            ObligationCauseCode::MainFunctionType => FailureCode::Error0580,
2252            ObligationCauseCode::CompareImplItem { .. }
2253            | ObligationCauseCode::MatchExpressionArm(_)
2254            | ObligationCauseCode::IfExpression { .. }
2255            | ObligationCauseCode::LetElse
2256            | ObligationCauseCode::LangFunctionType(_)
2257            | ObligationCauseCode::IntrinsicType
2258            | ObligationCauseCode::MethodReceiver => FailureCode::Error0308,
2259
2260            // In the case where we have no more specific thing to
2261            // say, also take a look at the error code, maybe we can
2262            // tailor to that.
2263            _ => match terr {
2264                TypeError::CyclicTy(ty)
2265                    if ty.is_closure() || ty.is_coroutine() || ty.is_coroutine_closure() =>
2266                {
2267                    FailureCode::Error0644
2268                }
2269                TypeError::IntrinsicCast | TypeError::ForceInlineCast => FailureCode::Error0308,
2270                _ => FailureCode::Error0308,
2271            },
2272        }
2273    }
2274    fn as_failure_code_diag(
2275        &self,
2276        terr: TypeError<'tcx>,
2277        span: Span,
2278        subdiags: Vec<TypeErrorAdditionalDiags>,
2279    ) -> ObligationCauseFailureCode {
2280        match self.code() {
2281            ObligationCauseCode::CompareImplItem { kind: ty::AssocKind::Fn { .. }, .. } => {
2282                ObligationCauseFailureCode::MethodCompat { span, subdiags }
2283            }
2284            ObligationCauseCode::CompareImplItem { kind: ty::AssocKind::Type { .. }, .. } => {
2285                ObligationCauseFailureCode::TypeCompat { span, subdiags }
2286            }
2287            ObligationCauseCode::CompareImplItem { kind: ty::AssocKind::Const { .. }, .. } => {
2288                ObligationCauseFailureCode::ConstCompat { span, subdiags }
2289            }
2290            ObligationCauseCode::BlockTailExpression(.., hir::MatchSource::TryDesugar(_)) => {
2291                ObligationCauseFailureCode::TryCompat { span, subdiags }
2292            }
2293            ObligationCauseCode::MatchExpressionArm(box MatchExpressionArmCause {
2294                source, ..
2295            }) => match source {
2296                hir::MatchSource::TryDesugar(_) => {
2297                    ObligationCauseFailureCode::TryCompat { span, subdiags }
2298                }
2299                _ => ObligationCauseFailureCode::MatchCompat { span, subdiags },
2300            },
2301            ObligationCauseCode::IfExpression { .. } => {
2302                ObligationCauseFailureCode::IfElseDifferent { span, subdiags }
2303            }
2304            ObligationCauseCode::IfExpressionWithNoElse => {
2305                ObligationCauseFailureCode::NoElse { span }
2306            }
2307            ObligationCauseCode::LetElse => {
2308                ObligationCauseFailureCode::NoDiverge { span, subdiags }
2309            }
2310            ObligationCauseCode::MainFunctionType => {
2311                ObligationCauseFailureCode::FnMainCorrectType { span }
2312            }
2313            &ObligationCauseCode::LangFunctionType(lang_item_name) => {
2314                ObligationCauseFailureCode::FnLangCorrectType { span, subdiags, lang_item_name }
2315            }
2316            ObligationCauseCode::IntrinsicType => {
2317                ObligationCauseFailureCode::IntrinsicCorrectType { span, subdiags }
2318            }
2319            ObligationCauseCode::MethodReceiver => {
2320                ObligationCauseFailureCode::MethodCorrectType { span, subdiags }
2321            }
2322
2323            // In the case where we have no more specific thing to
2324            // say, also take a look at the error code, maybe we can
2325            // tailor to that.
2326            _ => match terr {
2327                TypeError::CyclicTy(ty)
2328                    if ty.is_closure() || ty.is_coroutine() || ty.is_coroutine_closure() =>
2329                {
2330                    ObligationCauseFailureCode::ClosureSelfref { span }
2331                }
2332                TypeError::ForceInlineCast => {
2333                    ObligationCauseFailureCode::CantCoerceForceInline { span, subdiags }
2334                }
2335                TypeError::IntrinsicCast => {
2336                    ObligationCauseFailureCode::CantCoerceIntrinsic { span, subdiags }
2337                }
2338                _ => ObligationCauseFailureCode::Generic { span, subdiags },
2339            },
2340        }
2341    }
2342
2343    fn as_requirement_str(&self) -> &'static str {
2344        match self.code() {
2345            ObligationCauseCode::CompareImplItem { kind: ty::AssocKind::Fn { .. }, .. } => {
2346                "method type is compatible with trait"
2347            }
2348            ObligationCauseCode::CompareImplItem { kind: ty::AssocKind::Type { .. }, .. } => {
2349                "associated type is compatible with trait"
2350            }
2351            ObligationCauseCode::CompareImplItem { kind: ty::AssocKind::Const { .. }, .. } => {
2352                "const is compatible with trait"
2353            }
2354            ObligationCauseCode::MainFunctionType => "`main` function has the correct type",
2355            ObligationCauseCode::LangFunctionType(_) => "lang item function has the correct type",
2356            ObligationCauseCode::IntrinsicType => "intrinsic has the correct type",
2357            ObligationCauseCode::MethodReceiver => "method receiver has the correct type",
2358            _ => "types are compatible",
2359        }
2360    }
2361}
2362
2363/// Newtype to allow implementing IntoDiagArg
2364pub struct ObligationCauseAsDiagArg<'tcx>(pub ObligationCause<'tcx>);
2365
2366impl IntoDiagArg for ObligationCauseAsDiagArg<'_> {
2367    fn into_diag_arg(self, _: &mut Option<std::path::PathBuf>) -> rustc_errors::DiagArgValue {
2368        let kind = match self.0.code() {
2369            ObligationCauseCode::CompareImplItem { kind: ty::AssocKind::Fn { .. }, .. } => {
2370                "method_compat"
2371            }
2372            ObligationCauseCode::CompareImplItem { kind: ty::AssocKind::Type { .. }, .. } => {
2373                "type_compat"
2374            }
2375            ObligationCauseCode::CompareImplItem { kind: ty::AssocKind::Const { .. }, .. } => {
2376                "const_compat"
2377            }
2378            ObligationCauseCode::MainFunctionType => "fn_main_correct_type",
2379            ObligationCauseCode::LangFunctionType(_) => "fn_lang_correct_type",
2380            ObligationCauseCode::IntrinsicType => "intrinsic_correct_type",
2381            ObligationCauseCode::MethodReceiver => "method_correct_type",
2382            _ => "other",
2383        }
2384        .into();
2385        rustc_errors::DiagArgValue::Str(kind)
2386    }
2387}
2388
2389/// This is a bare signal of what kind of type we're dealing with. `ty::TyKind` tracks
2390/// extra information about each type, but we only care about the category.
2391#[derive(#[automatically_derived]
impl ::core::clone::Clone for TyCategory {
    #[inline]
    fn clone(&self) -> TyCategory {
        let _: ::core::clone::AssertParamIsClone<hir::CoroutineKind>;
        *self
    }
}Clone, #[automatically_derived]
impl ::core::marker::Copy for TyCategory { }Copy, #[automatically_derived]
impl ::core::cmp::PartialEq for TyCategory {
    #[inline]
    fn eq(&self, other: &TyCategory) -> bool {
        let __self_discr = ::core::intrinsics::discriminant_value(self);
        let __arg1_discr = ::core::intrinsics::discriminant_value(other);
        __self_discr == __arg1_discr &&
            match (self, other) {
                (TyCategory::Coroutine(__self_0),
                    TyCategory::Coroutine(__arg1_0)) => __self_0 == __arg1_0,
                _ => true,
            }
    }
}PartialEq, #[automatically_derived]
impl ::core::cmp::Eq for TyCategory {
    #[inline]
    #[doc(hidden)]
    #[coverage(off)]
    fn assert_receiver_is_total_eq(&self) {
        let _: ::core::cmp::AssertParamIsEq<hir::CoroutineKind>;
    }
}Eq, #[automatically_derived]
impl ::core::hash::Hash for TyCategory {
    #[inline]
    fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) {
        let __self_discr = ::core::intrinsics::discriminant_value(self);
        ::core::hash::Hash::hash(&__self_discr, state);
        match self {
            TyCategory::Coroutine(__self_0) =>
                ::core::hash::Hash::hash(__self_0, state),
            _ => {}
        }
    }
}Hash)]
2392pub enum TyCategory {
2393    Closure,
2394    Opaque,
2395    OpaqueFuture,
2396    Coroutine(hir::CoroutineKind),
2397    Foreign,
2398}
2399
2400impl fmt::Display for TyCategory {
2401    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2402        match self {
2403            Self::Closure => "closure".fmt(f),
2404            Self::Opaque => "opaque type".fmt(f),
2405            Self::OpaqueFuture => "future".fmt(f),
2406            Self::Coroutine(gk) => gk.fmt(f),
2407            Self::Foreign => "foreign type".fmt(f),
2408        }
2409    }
2410}
2411
2412impl TyCategory {
2413    pub fn from_ty(tcx: TyCtxt<'_>, ty: Ty<'_>) -> Option<(Self, DefId)> {
2414        match *ty.kind() {
2415            ty::Closure(def_id, _) => Some((Self::Closure, def_id)),
2416            ty::Alias(ty::Opaque, ty::AliasTy { def_id, .. }) => {
2417                let kind =
2418                    if tcx.ty_is_opaque_future(ty) { Self::OpaqueFuture } else { Self::Opaque };
2419                Some((kind, def_id))
2420            }
2421            ty::Coroutine(def_id, ..) => {
2422                Some((Self::Coroutine(tcx.coroutine_kind(def_id).unwrap()), def_id))
2423            }
2424            ty::Foreign(def_id) => Some((Self::Foreign, def_id)),
2425            _ => None,
2426        }
2427    }
2428}