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