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