Skip to main content

rustc_trait_selection/error_reporting/infer/
mod.rs

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